Exemplo n.º 1
0
int main() {
    attacks_init();

    master_pgn_db = kcdbnew();
    puts("opening master-pgn.kct ...");
    if (!kcdbopen(master_pgn_db, "master-pgn.kct", KCOREADER)) {
        printf("master-pgn.kct open error: %s\n", kcecodename(kcdbecode(master_pgn_db)));
        return 1;
    }

    master_db = kcdbnew();
    puts("opening master.kch ...");
    if (!kcdbopen(master_db, "master.kch", KCOREADER)) {
        printf("master.kch open error: %s\n", kcecodename(kcdbecode(master_db)));
        return 1;
    }

    puts("opened all databases.");

    int ret = serve(5555);

    if (!kcdbclose(master_pgn_db)) {
        printf("master-pgn.kct close error: %s\n", kcecodename(kcdbecode(master_pgn_db)));
    }

    if (!kcdbclose(master_db)) {
        printf("master.kch close error: %s\n", kcecodename(kcdbecode(master_db)));
    }

    kcdbdel(master_pgn_db);
    return ret;
}
Exemplo n.º 2
0
// Open a DB.
bool open_db (KCDB *db, char *map_type, char *start_time) {
    // Figure out what db to open.
    char db_buffer[512];
    snprintf(db_buffer, 256, "%s/%s/pg_agg_map_%s.%s", BASE_DB_DIR, start_time, map_type, DB_TYPE);
    if (!kcdbopen(db, db_buffer, KCOWRITER)) {
#ifdef CF_NO_DB_IS_ERR
        ereport(ERROR,
                (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
                 errmsg("Error opening db: \"%s\", \"%s\". Make sure that the map_name is valid.", 
                        db_buffer, kcecodename(kcdbecode(db)))));
#endif
#ifdef CF_DUBUG
        ereport(NOTICE,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                 errmsg("Error opening db: \"%s\", \"%s\". Make sure that the map_name is valid.", 
                        db_buffer, kcecodename(kcdbecode(db)))));
#endif
        return false;
    }

#ifdef CF_DUBUG        
    ereport(NOTICE,
            (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
             errmsg("Opened db -- %s.", db_buffer)));
#endif 

    return true;
}
Exemplo n.º 3
0
/* main routine */
int
main (int argc, char **argv)
{
  KCDB *db;
  char *vbuf;
  size_t vsiz;
  int retour = 1;
  db = kcdbnew ();

  if (argc != 3)
    {
      fprintf (stderr, "Usage getVal DB_FILE KEY\n");
      return 100;
    }

  /* open the database */
  if (!kcdbopen (db, argv[1], KCOWRITER | KCOCREATE))
    {
      fprintf (stderr, "open error: %s\n", kcecodename (kcdbecode (db)));
      return 100;
    }

  vbuf = kcdbget (db, argv[2], strlen (argv[2]), &vsiz);
  if (vbuf)
    {
      printf ("%s\n", vbuf);
      kcfree (vbuf);
      retour = 0;
    }
  /* delete the database object */
  kcdbdel (db);

  return retour;
}
/* get_genid
 * Looks up the host's genid in the host->genid database
 */
static int
get_genid(char *host)
{
  KCDB *db;
  char *vbuf;
  size_t vsiz;
  int answer = 0;
  int host_size;

  /* create the database object */
  db = kcdbnew();

  /* open the database */
  if (!kcdbopen(db, genid_kyoto_db, KCOREADER | KCONOLOCK)) {
    TSDebug(PLUGIN_NAME, "could not open the genid database %s", genid_kyoto_db);
    TSError("[%s] could not open the genid database %s: %s", PLUGIN_NAME, genid_kyoto_db, strerror(errno));
    return 0;
  }

  vbuf = kcdbget(db, host, strlen(host), &vsiz);

  if (vbuf) {
    TSDebug(PLUGIN_NAME, "kcdbget(%s) = %s", host, vbuf);
    answer = (int)strtol(vbuf, NULL, 10);
    kcfree(vbuf);
  } else {
    host_size = strlen(host);
    TSDebug(PLUGIN_NAME, "kcdbget(%s) - no record found, len(%d)", host, host_size);
    answer = 0;
  }

  kcdbclose(db);
  return answer;
}
Exemplo n.º 5
0
/* main routine */
int
main (int argc, char **argv)
{
  KCDB *db;
  char *vbuf;
  size_t vsiz;
  /* Variable for time mesure */
  clock_t start, finish;
  int duration;
  start = clock ();

  db = kcdbnew ();

  /* open the database */
  if (!kcdbopen (db, argv[1], KCOWRITER | KCOCREATE))
    {
      fprintf (stderr, "open error: %s\n", kcecodename (kcdbecode (db)));
      return 100;
    }

  vbuf = kcdbget (db, argv[2], strlen (argv[2]), &vsiz);
  if (vbuf)
    {
      printf ("%s\n", vbuf);
      kcfree (vbuf);
    }
  /* delete the database object */
  kcdbdel (db);

  finish = clock ();
  duration = (finish - start);
  printf ("\nduration : %iclock (%i clock/s) \n", duration, CLOCKS_PER_SEC);

  return 0;
}
Exemplo n.º 6
0
static KCDB* rdf_kb_open_hash_storage(gchar *hashfile) {
    KCDB *hash_st = kcdbnew();
    if (!kcdbopen(hash_st, hashfile , KCOWRITER | KCOCREATE)) {
        printf("ERROR OPENINING HASH %s\n",hashfile);
        exit(-1);
     }
     return hash_st;
}
Exemplo n.º 7
0
char * pwd_save(KCDB * db, const char * origin_url)
{
	char * pk, * epk, * url;
	char hash[41];
	size_t vsiz;
	if(!kcdbopen(db, db_file, KCOWRITER|KCOCREATE))
	{
		printf("Status: 500 INTERNAL ERROR\r\n\r\nkcdb open error: %s\n", kcecodename(kcdbecode(db)));
		return NULL;
	}

	if(strstr(origin_url, "http://") == NULL && strstr(origin_url, "https://") == NULL)
	{
		url = malloc(strlen(origin_url) + 8);
		strcpy(url, "http://");
		strcat(url, origin_url);
	}
	else
	{
		url = malloc(strlen(origin_url) + 1);
		strcpy(url, origin_url);
	}

	// calc url hash and check if exists
	sha1(hash, url);
	epk = kcdbget(db, hash, strlen(hash), &vsiz);
	if(epk)
	{
		pk = malloc(strlen(epk) + 1);
		strcpy(pk, epk);
		kcfree(epk);
		free(url);
		kcdbclose(db);
		return pk;
	}

	pk = pwd_get_pk(db);
	if(pk == NULL)
	{
		free(url);
		kcdbclose(db);
		return NULL;
	}

	//TODO save pk => url && hash => pk
	if(!kcdbset(db, pk, strlen(pk), url, strlen(url)))
	{
		free(pk); pk = NULL;
	}
	if(!kcdbset(db, hash, strlen(hash), pk, strlen(pk)))
	{
		free(pk); pk = NULL;
	}

	free(url);
	kcdbclose(db);
	return pk;
}
Exemplo n.º 8
0
Arquivo: main.c Projeto: 2ion/pdfytfs
int main(int argc, char **argv)
{
  /* from config.h */
  struct pyt_private_data *d = &Config;

  /* open the databases */
  d->filesdb = kcdbnew();
  if(!kcdbopen(d->filesdb, "-", KCOREADER | KCOWRITER | KCOCREATE))
    LERROR(EXIT_FAILURE, 0, "Could not create in-memory cache database: %s",
        kcecodename(kcdbecode(d->filesdb)));

  d->cachedb = kcdbnew();
  d->cachefile = pyt_fs_kcdbpath(d->cachefile, PYT_DB_TUNING_DATA);
  if(!kcdbopen(d->cachedb, d->cachefile, KCOREADER | KCOWRITER | KCOCREATE)) {
    LERROR(0, 0, "Could not open or create cache database @ %s: %s",
        d->cachefile, kcecodename(kcdbecode(d->cachedb)));
    free(d->cachefile);
    kcdbclose(d->filesdb);
    kcdbdel(d->filesdb);
    free(d->cachefile);
    return EXIT_FAILURE;
  }

  /* register exit trap to prevent cache corruption */
  if(atexit(pyt_at_exit) != 0)
    LERROR(0, 0, "Failed to register pyt_at_exit() as a trap on exit(). "
        "If you terminate the program forcefully, cache corruption might occur!");

  /* initialize the file database with the control files */
  if(pyt_fs_init_filesdb(d) != 0) {
    LERROR(0, 0, "pyt_fs_init_filesdb() failed");
    goto main_close_databases;
  }

  /* close databases */
main_close_databases:
  kcdbclose(d->filesdb);
  kcdbdel(d->filesdb);
  kcdbclose(d->cachedb);
  kcdbdel(d->cachedb);
  d->cachedb = NULL; // for pyt_at_exit
  free(d->cachefile);
 
  return EXIT_SUCCESS;
}
Exemplo n.º 9
0
struct db *db_open(const char *path_db, int flags, duc_errno *e)
{
	struct db *db;
	int compress = 0;

	uint32_t mode = KCOREADER;
	if(flags & DUC_OPEN_RW) mode |= KCOWRITER | KCOCREATE;
	if(flags & DUC_OPEN_COMPRESS) compress = 1;

	db = duc_malloc(sizeof *db);

	db->kdb = kcdbnew();

	db->kdb = kcdbnew();
	if(!db->kdb) {
		*e = DUC_E_DB_BACKEND;
		goto err1;
	}

	char fname[DUC_PATH_MAX];
	snprintf(fname, sizeof(fname), "%s.kct#opts=c", path_db);

	int r = kcdbopen(db->kdb, fname, mode);
	if(r == 0) {
		perror(kcecodename(kcdbecode(db->kdb)));
		*e = tcdb_to_errno(db->kdb);
		goto err2;
	}

	size_t vall;
	char *version = db_get(db, "duc_db_version", 14, &vall);
	if(version) {
		if(strcmp(version, DUC_DB_VERSION) != 0) {
			*e = DUC_E_DB_VERSION_MISMATCH;
			goto err3;
		}
		free(version);
	} else {
		db_put(db, "duc_db_version", 14, DUC_DB_VERSION, strlen(DUC_DB_VERSION));
	}

	return db;

err3:
	kcdbclose(db->kdb);
err2:
	kcdbdel(db->kdb);
err1:
	free(db);
	return NULL;
}
extern CAMLprim
value kc_open(value path, value options)
{
  CAMLparam2(path, options);

  KCDB* db = kcdbnew();
  if (! kcdbopen(db, String_val(path), decode_flags(options, OPEN_FLAGS))) {
     const char *error = kcdbemsg(db);
     kcdbdel(db);
     RAISE(error);
  }

  value caml_db = alloc_small(1, Abstract_tag);
  KCDB_val(caml_db) = db;
  CAMLreturn(caml_db);
}
Exemplo n.º 11
0
inline void set_kv_path(Cloudflare__ZoneTimeBucket *msg, char *map_name, char *start_time_uid, KCDB* db) {

    struct stat path_buf;
    char dir_buf[KC_MAX_RID];
    char *found;

    msg->kv_map_file = (char *)palloc(KC_MAX_RID * sizeof(char));
    snprintf(msg->kv_map_file, KC_MAX_RID, "%s/%s/%s/%s.%s", 
             CF_KV_MAP_BASE,
             start_time_uid,
             map_name,
             msg->db_key,
             DB_TYPE);

    snprintf(dir_buf, KC_MAX_RID, "%s", CF_KV_MAP_BASE);        
    if (stat(dir_buf, &path_buf) != 0) {
        mkdir (dir_buf, NEW_DIR_MASK);
    }
    chmod (dir_buf, NEW_DIR_MODE);
    
    found = strtok (start_time_uid, "/");
    while (found != NULL) {
        strncat (dir_buf, "/", KC_MAX_RID);
        strncat (dir_buf, found, KC_MAX_RID);
        if (stat(dir_buf, &path_buf) != 0) {
            mkdir (dir_buf, NEW_DIR_MODE);
        }
        chmod (dir_buf, NEW_DIR_MODE);
        found = strtok (NULL, "/");
    }
    strncat (dir_buf, "/", KC_MAX_RID);
    strncat (dir_buf, map_name, KC_MAX_RID);
    if (stat(dir_buf, &path_buf) != 0) {
        mkdir (dir_buf, NEW_DIR_MODE);
    }
    chmod (dir_buf, NEW_DIR_MODE);

    if (!kcdbopen(db, msg->kv_map_file, KCOWRITER | KCOCREATE)) {
        ereport(ERROR,
                (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
                 errmsg("KV open error for: %s %s\n", msg->kv_map_file, kcecodename(kcdbecode(db)))));
    }
    kcdbbegintran (db, 0);
    kcdbclear (db);
    chmod (msg->kv_map_file, FILE_MODE);
}
Exemplo n.º 12
0
int merge_using_kv_map(Cloudflare__ZoneTimeBucket *msg_old, Cloudflare__ZoneTimeBucket *msg_new, KCDB* db) {

    unsigned int i;
    int num_new = 0;
    if (msg_new->kv_map_file) {
        // Open a db for the new map file.
        KCCUR* cur;
        KCDB* new_db;
        new_db = kcdbnew();
        /* open the database */
        if (!kcdbopen(new_db, msg_new->kv_map_file, KCOWRITER)) {
            ereport(ERROR,
                    (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
                     errmsg("open error: %s", kcecodename(kcdbecode(new_db)))));
        }
        
        cur = kcdbcursor(new_db);
        kccurjump(cur);
        
        char *kv_kbuf;
        size_t kv_ksiz;
        while ((kv_kbuf = kccurgetkey(cur, &kv_ksiz, 1)) != NULL) {
            int64_t value = kcdbincrint (new_db, kv_kbuf, kv_ksiz, 0);
            
#ifdef CF_DUBUG
            ereport(NOTICE,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("Saving: %s -- %ld", kv_kbuf, value)));
#endif

            kcdbincrint (db, kv_kbuf, kv_ksiz, value);
            num_new++;
            kcfree(kv_kbuf);
        }
        kccurdel(cur);
        kcdbclose(new_db);            
    } else {
        for (i = 0; i < msg_new->n_map_entry; i++) {
            kcdbincrint (db, msg_new->map_entry[i]->key, strlen(msg_new->map_entry[i]->key), msg_new->map_entry[i]->value);
            num_new++;
        }
    }

    return num_new;
}
Exemplo n.º 13
0
char * pwd_load(KCDB * db, const char * key)
{
	char * url, * value;
	size_t vsiz;

	if(!kcdbopen(db, db_file, KCOREADER))
	{
		printf("Status: 500 INTERNAL ERROR\r\n\r\nkcdb open error: %s\n", kcecodename(kcdbecode(db)));
		return NULL;
	}

	value = kcdbget(db, key, strlen(key), &vsiz);
	if(!value)
	{
		kcdbclose(db);
		return NULL;
	}

	url = malloc(strlen(value) + 1);
	strcpy(url, value);
	kcfree(value);
	kcdbclose(db);
	return url;
}
Exemplo n.º 14
0
Datum kc_expand(PG_FUNCTION_ARGS) {

    KC_ENTRY                        *search;
    FuncCallContext                 *funcctx;
    int                             call_cntr;
    char                            *kbuf;
    size_t                          ksiz, vsiz;
    const char                      *cvbuf;
    char                            *kv_kbuf = NULL; 
    size_t                          kv_ksiz;
    int                             done;

    /* stuff done only on the first call of the function */
    if (SRF_IS_FIRSTCALL()) {
        MemoryContext   oldcontext;

        /* create a function context for cross-call persistence */
        funcctx = SRF_FIRSTCALL_INIT();

        /* switch to memory context appropriate for multiple function calls */
        oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);

        // Make sure that there are enough args.
        if (PG_NARGS() < MIN_ARGS) {
            ereport(ERROR,
                    (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
                     errmsg("Must run expand with at least %d args!", MIN_ARGS)));
        }

        /* Make the kcdb here. */
        search = (KC_ENTRY *)palloc(sizeof(KC_ENTRY)); 
        search->db = kcdbnew();
        if (open_db (search->db, text_to_cstring(PG_GETARG_TEXT_PP(0)), text_to_cstring(PG_GETARG_TEXT_PP(1)))) {

            // Set the key to jump into:
            // Call with -- map_name, result_id, class, doctype, pop, psource
            // Here, map_name describes a db to open.
            // Otherwise, result_id:class:doctype:pop:psource
            (search->jump_key) = (char *) palloc(MAX_JUMP_KEY_LEN * sizeof(char));

            int index_point;
            search->jump_key = text_to_cstring(PG_GETARG_TEXT_PP(2));
            int size_left = MAX_JUMP_KEY_LEN;
            for (index_point = START_VARIABLE_INDEX; index_point < END_VARIABLE_INDEX; index_point++) {
                if (PG_NARGS() > index_point) {
                    char *next_idx = text_to_cstring(PG_GETARG_TEXT_PP(index_point));
                    if (next_idx != NULL) {
                        size_left = size_left - (2 + strlen(next_idx));
                        strncat (search->jump_key, CF_LABEL_SEP, size_left);
                        strncat (search->jump_key, next_idx, size_left);
                    }
                }
            }
            
#ifdef CF_DUBUG
            ereport(NOTICE,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("Setting jump buffer -- [%s]", search->jump_key)));
#endif
            
            // Create a cursor, and set it to the base point looking for entries.
            search->cur = kcdbcursor(search->db);
            kccurjumpkey(search->cur, search->jump_key, MAX_JUMP_KEY_LEN);
        } else {
            search->db = NULL;
        }

        search->next_map = 0;
        search->msg = NULL;
        
        // Save the search struct for the subsequent calls.
        funcctx->user_fctx = search;

        MemoryContextSwitchTo(oldcontext);
    }

    /* stuff done on every call of the function */
    funcctx = SRF_PERCALL_SETUP();

    call_cntr = funcctx->call_cntr;
    search = (KC_ENTRY *) funcctx->user_fctx;
    
    // If no current msg, try to get the next one.
    done = 1;

#ifdef CF_DUBUG
    ereport(NOTICE,
            (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
             errmsg("beginning run")));
#endif

    if (search->msg) {

#ifdef CF_DUBUG  
        ereport(NOTICE,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                 errmsg("Incrementing next from map %d -- %zu", search->next_map, search->msg->n_map_entry)));
#endif

        // Case if we are using the external cursor running over kv map.
        // Ready the next 
        if (search->msg->kv_map_file) {
            
            if ((kv_kbuf = kccurgetkey(search->kv_cur, &kv_ksiz, 1)) == NULL) {
                done = 1;
                kccurdel(search->kv_cur);
                kcdbendtran (search->kv_db, 1);            
                if (!kcdbclose(search->kv_db)) {
                    ereport(ERROR,
                            (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
                             errmsg("Error Closeing db: \"%s\"", kcecodename(kcdbecode(search->kv_db)))));
                }

                // Also need to free this.
                cloudflare__zone_time_bucket__free_unpacked(search->msg, NULL);
                search->msg = NULL;

            } else {
                done = 0;
            }


        } else {
            if (search->next_map >= search->msg->n_map_entry) {
                // Done with this msg -- move on to the next one.
                cloudflare__zone_time_bucket__free_unpacked(search->msg, NULL);
                search->msg = NULL;
            } else {
                done = 0;
            }
        }
    }

    if (search->db && !search->msg) {
      
#ifdef CF_DUBUG  
        ereport(NOTICE,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                 errmsg("Getting new buf -- %s", search->jump_key)));
#endif        

        if ((kbuf = kccurget(search->cur, &ksiz, &cvbuf, &vsiz, 1)) != NULL) {
            // Pull up the PB and expand it.
            search->msg = cloudflare__zone_time_bucket__unpack(NULL, vsiz, (const uint8_t *)cvbuf);
            if (search->msg == NULL) {   // Something failed
                ereport(ERROR,
                        (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
                         errmsg("error unpacking incoming message")));
                done = 1;
            } else {
                // Does the buffer match the searched for string?
                // @TODO -- bound this?
                if (strstr(search->msg->db_key, search->jump_key)) {
                    done = 0;
                    search->next_map = 0;

                    // And load the kvkc if needed.
                    if (search->msg->kv_map_file) {
                        
#ifdef CF_DUBUG  
                        ereport(NOTICE,
                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                 errmsg("Switching to kvs %s", search->msg->kv_map_file)));
#endif

                        search->kv_db = kcdbnew();
                        
                        if (!kcdbopen(search->kv_db, search->msg->kv_map_file, KCOWRITER)) {
#ifdef CF_NO_DB_IS_ERR
                            ereport(ERROR,
                                    (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
                                     errmsg("Error opening db: \"%s\", \"%s\". Make sure that the map_name is valid.", 
                                            search->msg->kv_map_file, kcecodename(kcdbecode(search->kv_db)))));
#endif
#ifdef CF_DUBUG
                            ereport(NOTICE,
                                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                     errmsg("Error opening db: \"%s\", \"%s\". Make sure that the map_name is valid.", 
                                            search->msg->kv_map_file, kcecodename(kcdbecode(search->kv_db)))));
#endif
                            done = 1;
                        } else {
                            kcdbbegintran (search->kv_db, 0);
                            search->kv_cur = kcdbcursor(search->kv_db);
                            kccurjump(search->kv_cur);   

                            if ((kv_kbuf = kccurgetkey(search->kv_cur, &kv_ksiz, 1)) == NULL) {
                                done = 1;
                                kccurdel(search->kv_cur);
                                kcdbendtran (search->kv_db, 1);
                                if (!kcdbclose(search->kv_db)) {
                                    ereport(ERROR,
                                            (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
                                             errmsg("Error Closeing db: \"%s\"", kcecodename(kcdbecode(search->kv_db)))));
                                }
                            } else {
                                done = 0;
                            }
                        }
                    }
                } else {
                    done = 1;
                }
            }
            kcfree(kbuf);
        } else {
#ifdef CF_DUBUG
            ereport(NOTICE,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("no msg to find")));
#endif
            done = 1;
        }
    }

#ifdef CF_DUBUG
    ereport(NOTICE,
            (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
             errmsg("Done? %d -- next buf -- %d", done, search->next_map)));   
#endif

    // Take the next itteration over the cursor. If the next is NULL or else not matching the resultid passed in
    // End. Otherwise, parse the value, populating the next row of the returning tuple.
    if (!done) {
        KC_ROW                          *out;
        Datum                           result;

        size_t size = sizeof(KC_ROW);
        out = (KC_ROW *)palloc(size);
        memset(out, '0', size);
        SET_VARSIZE(out, size);

        out->classification = (char *)palloc(MAX_KC_ROW_ENTRY * sizeof(char));
        out->doctype = (char *)palloc(MAX_KC_ROW_ENTRY * sizeof(char));
        out->pop = (char *)palloc(MAX_KC_ROW_ENTRY * sizeof(char));
        out->psource = (char *)palloc(MAX_KC_ROW_ENTRY * sizeof(char));
        out->key = (char *)palloc(MAX_KC_ROW_ENTRY * sizeof(char));

        strncpy(out->classification, search->msg->classification, MAX_KC_ROW_ENTRY);
        strncpy(out->doctype, search->msg->doctype, MAX_KC_ROW_ENTRY);
        strncpy(out->pop, search->msg->pop, MAX_KC_ROW_ENTRY);
        strncpy(out->psource, search->msg->psource, MAX_KC_ROW_ENTRY);

        if (search->msg->kv_map_file) {

#ifdef CF_DUBUG
            ereport(NOTICE,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("getting val from -- [%s]", search->msg->kv_map_file)));
#endif

            snprintf(out->key, MAX_KC_ROW_ENTRY, "%s", kv_kbuf);
            out->value = kcdbincrint (search->kv_db, kv_kbuf, kv_ksiz, 0);

            if (out->value == INT64_MIN) {
                ereport(NOTICE,
                        (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
                         errmsg("ERROR Getting val from key -- [%s], %s", kv_kbuf, kcecodename(kcdbecode(search->kv_db)))));
            }

            kcfree(kv_kbuf);
        } else {

#ifdef CF_DUBUG
            ereport(NOTICE,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("Loading %s %ld", search->msg->map_entry[search->next_map]->key, 
                            search->msg->map_entry[search->next_map]->value)));
#endif

            snprintf(out->key, MAX_KC_ROW_ENTRY, "%s", search->msg->map_entry[search->next_map]->key);        
            out->value = search->msg->map_entry[search->next_map]->value;
        }

        result = PointerGetDatum(out);

        /* clean up (this is not really necessary) */
        pfree(out->classification);
        pfree(out->doctype);
        pfree(out->pop);
        pfree(out->psource);
        pfree(out->key);
        pfree(out);

        // Remember that we are going to the next step.
        search->next_map++;

        SRF_RETURN_NEXT(funcctx, result);
    } else {    /* do when there is no more left */
        if (search->db) {
            kccurdel(search->cur);
            if (!kcdbclose(search->db)) {
                ereport(ERROR,
                        (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
                         errmsg("Error Closeing db: \"%s\"", kcecodename(kcdbecode(search->db)))));
            }
            
            if (search->msg != NULL) {
                cloudflare__zone_time_bucket__free_unpacked(search->msg, NULL);
            }
            
            pfree(search->jump_key);
        }
        pfree(search);

#ifdef CF_DUBUG
        ereport(NOTICE,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                 errmsg("Done with run")));
#endif

        // Don't delete db, this leads to segfaults.
        SRF_RETURN_DONE(funcctx);
    }
}
Exemplo n.º 15
0
/* perform order command */
static int32_t procorder(const char* path, int64_t rnum, int32_t rnd, int32_t etc,
                         int32_t tran, int32_t oflags) {
    KCDB* db;
    KCCUR* cur, *paracur;
    int32_t err;
    char kbuf[RECBUFSIZ], *vbuf, wbuf[RECBUFSIZ], *corepath, *copypath, *snappath;
    size_t ksiz, vsiz, psiz;
    int32_t wsiz;
    int64_t i, cnt;
    double stime, etime;
    VISARG visarg;
    oprintf("<In-order Test>\n  path=%s  rnum=%ld  rnd=%d  etc=%d  tran=%d  oflags=%d\n\n",
            path, (long)rnum, rnd, etc, tran, oflags);
    err = FALSE;
    db = kcdbnew();
    oprintf("opening the database:\n");
    stime = kctime();
    if (!kcdbopen(db, path, KCOWRITER | KCOCREATE | KCOTRUNCATE | oflags)) {
        dberrprint(db, __LINE__, "kcdbopen");
        err = TRUE;
    }
    etime = kctime();
    dbmetaprint(db, FALSE);
    oprintf("time: %.3f\n", etime - stime);
    oprintf("setting records:\n");
    stime = kctime();
    for (i = 1; !err && i <= rnum; i++) {
        if (tran && !kcdbbegintran(db, FALSE)) {
            dberrprint(db, __LINE__, "kcdbbegintran");
            err = TRUE;
        }
        ksiz = sprintf(kbuf, "%08ld", (long)(rnd ? myrand(rnum) + 1 : i));
        if (!kcdbset(db, kbuf, ksiz, kbuf, ksiz)) {
            dberrprint(db, __LINE__, "kcdbset");
            err = TRUE;
        }
        if (tran && !kcdbendtran(db, TRUE)) {
            dberrprint(db, __LINE__, "kcdbendtran");
            err = TRUE;
        }
        if (rnum > 250 && i % (rnum / 250) == 0) {
            oputchar('.');
            if (i == rnum || i % (rnum / 10) == 0) oprintf(" (%08ld)\n", (long)i);
        }
    }
    etime = kctime();
    dbmetaprint(db, FALSE);
    oprintf("time: %.3f\n", etime - stime);
    if (etc) {
        oprintf("adding records:\n");
        stime = kctime();
        for (i = 1; !err && i <= rnum; i++) {
            if (tran && !kcdbbegintran(db, FALSE)) {
                dberrprint(db, __LINE__, "kcdbbegintran");
                err = TRUE;
            }
            ksiz = sprintf(kbuf, "%08ld", (long)(rnd ? myrand(rnum) + 1 : i));
            if (!kcdbadd(db, kbuf, ksiz, kbuf, ksiz) && kcdbecode(db) != KCEDUPREC) {
                dberrprint(db, __LINE__, "kcdbadd");
                err = TRUE;
            }
            if (tran && !kcdbendtran(db, TRUE)) {
                dberrprint(db, __LINE__, "kcdbendtran");
                err = TRUE;
            }
            if (rnum > 250 && i % (rnum / 250) == 0) {
                oputchar('.');
                if (i == rnum || i % (rnum / 10) == 0) oprintf(" (%08ld)\n", (long)i);
            }
        }
        etime = kctime();
        dbmetaprint(db, FALSE);
        oprintf("time: %.3f\n", etime - stime);
    }
    if (etc) {
        oprintf("appending records:\n");
        stime = kctime();
        for (i = 1; !err && i <= rnum; i++) {
            if (tran && !kcdbbegintran(db, FALSE)) {
                dberrprint(db, __LINE__, "kcdbbegintran");
                err = TRUE;
            }
            ksiz = sprintf(kbuf, "%08ld", (long)(rnd ? myrand(rnum) + 1 : i));
            if (!kcdbappend(db, kbuf, ksiz, kbuf, ksiz)) {
                dberrprint(db, __LINE__, "kcdbadd");
                err = TRUE;
            }
            if (tran && !kcdbendtran(db, TRUE)) {
                dberrprint(db, __LINE__, "kcdbendtran");
                err = TRUE;
            }
            if (rnum > 250 && i % (rnum / 250) == 0) {
                oputchar('.');
                if (i == rnum || i % (rnum / 10) == 0) oprintf(" (%08ld)\n", (long)i);
            }
        }
        etime = kctime();
        dbmetaprint(db, FALSE);
        oprintf("time: %.3f\n", etime - stime);
    }
    oprintf("getting records:\n");
    stime = kctime();
    for (i = 1; !err && i <= rnum; i++) {
        if (tran && !kcdbbegintran(db, FALSE)) {
            dberrprint(db, __LINE__, "kcdbbegintran");
            err = TRUE;
        }
        ksiz = sprintf(kbuf, "%08ld", (long)(rnd ? myrand(rnum) + 1 : i));
        vbuf = kcdbget(db, kbuf, ksiz, &vsiz);
        if (vbuf) {
            if (vsiz < ksiz || memcmp(vbuf, kbuf, ksiz)) {
                dberrprint(db, __LINE__, "kcdbget");
                err = TRUE;
            }
            kcfree(vbuf);
        } else if (!rnd || kcdbecode(db) != KCENOREC) {
            dberrprint(db, __LINE__, "kcdbget");
            err = TRUE;
        }
        if (tran && !kcdbendtran(db, TRUE)) {
            dberrprint(db, __LINE__, "kcdbendtran");
            err = TRUE;
        }
        if (rnum > 250 && i % (rnum / 250) == 0) {
            oputchar('.');
            if (i == rnum || i % (rnum / 10) == 0) oprintf(" (%08ld)\n", (long)i);
        }
    }
    etime = kctime();
    dbmetaprint(db, FALSE);
    oprintf("time: %.3f\n", etime - stime);
    if (etc) {
        oprintf("getting records with a buffer:\n");
        stime = kctime();
        for (i = 1; !err && i <= rnum; i++) {
            if (tran && !kcdbbegintran(db, FALSE)) {
                dberrprint(db, __LINE__, "kcdbbegintran");
                err = TRUE;
            }
            ksiz = sprintf(kbuf, "%08ld", (long)(rnd ? myrand(rnum) + 1 : i));
            wsiz = kcdbgetbuf(db, kbuf, ksiz, wbuf, sizeof(wbuf));
            if (wsiz >= 0) {
                if (wsiz < (int32_t)ksiz || memcmp(wbuf, kbuf, ksiz)) {
                    dberrprint(db, __LINE__, "kcdbgetbuf");
                    err = TRUE;
                }
            } else if (!rnd || kcdbecode(db) != KCENOREC) {
                dberrprint(db, __LINE__, "kcdbgetbuf");
                err = TRUE;
            }
            if (tran && !kcdbendtran(db, TRUE)) {
                dberrprint(db, __LINE__, "kcdbendtran");
                err = TRUE;
            }
            if (rnum > 250 && i % (rnum / 250) == 0) {
                oputchar('.');
                if (i == rnum || i % (rnum / 10) == 0) oprintf(" (%08ld)\n", (long)i);
            }
        }
        etime = kctime();
        dbmetaprint(db, FALSE);
        oprintf("time: %.3f\n", etime - stime);
    }
    if (etc) {
        oprintf("traversing the database by the inner iterator:\n");
        stime = kctime();
        cnt = kcdbcount(db);
        visarg.rnum = rnum;
        visarg.rnd = rnd;
        visarg.cnt = 0;
        memset(visarg.rbuf, '+', sizeof(visarg.rbuf));
        if (tran && !kcdbbegintran(db, FALSE)) {
            dberrprint(db, __LINE__, "kcdbbegintran");
            err = TRUE;
        }
        if (!kcdbiterate(db, visitfull, &visarg, TRUE)) {
            dberrprint(db, __LINE__, "kcdbiterate");
            err = TRUE;
        }
        if (rnd) oprintf(" (end)\n");
        if (tran && !kcdbendtran(db, TRUE)) {
            dberrprint(db, __LINE__, "kcdbendtran");
            err = TRUE;
        }
        if (visarg.cnt != cnt) {
            dberrprint(db, __LINE__, "kcdbiterate");
            err = TRUE;
        }
        etime = kctime();
        dbmetaprint(db, FALSE);
        oprintf("time: %.3f\n", etime - stime);
    }
    if (etc) {
        oprintf("traversing the database by the outer cursor:\n");
        stime = kctime();
        cnt = kcdbcount(db);
        visarg.rnum = rnum;
        visarg.rnd = rnd;
        visarg.cnt = 0;
        if (tran && !kcdbbegintran(db, FALSE)) {
            dberrprint(db, __LINE__, "kcdbbegintran");
            err = TRUE;
        }
        cur = kcdbcursor(db);
        if (!kccurjump(cur) && kccurecode(cur) != KCENOREC) {
            dberrprint(db, __LINE__, "kccurjump");
            err = TRUE;
        }
        paracur = kcdbcursor(db);
        while (!err && kccuraccept(cur, &visitfull, &visarg, TRUE, !rnd)) {
            if (rnd) {
                ksiz = sprintf(kbuf, "%08ld", (long)myrand(rnum));
                switch (myrand(3)) {
                case 0: {
                    if (!kcdbremove(db, kbuf, ksiz) && kcdbecode(db) != KCENOREC) {
                        dberrprint(db, __LINE__, "kcdbremove");
                        err = TRUE;
                    }
                    break;
                }
                case 1: {
                    if (!kccurjumpkey(paracur, kbuf, ksiz) && kccurecode(paracur) != KCENOREC) {
                        dberrprint(db, __LINE__, "kccurjump");
                        err = TRUE;
                    }
                    break;
                }
                default: {
                    if (!kccurstep(cur) && kccurecode(cur) != KCENOREC) {
                        dberrprint(db, __LINE__, "kccurstep");
                        err = TRUE;
                    }
                    break;
                }
                }
            }
        }
        oprintf(" (end)\n");
        kccurdel(paracur);
        kccurdel(cur);
        if (tran && !kcdbendtran(db, TRUE)) {
            dberrprint(db, __LINE__, "kcdbendtran");
            err = TRUE;
        }
        if (!rnd && visarg.cnt != cnt) {
            dberrprint(db, __LINE__, "kccuraccept");
            err = TRUE;
        }
        etime = kctime();
        dbmetaprint(db, FALSE);
        oprintf("time: %.3f\n", etime - stime);
    }
    if (etc) {
        oprintf("synchronizing the database:\n");
        stime = kctime();
        if (!kcdbsync(db, FALSE, NULL, NULL)) {
            dberrprint(db, __LINE__, "kcdbsync");
            err = TRUE;
        }
        if (!kcdboccupy(db, FALSE, NULL, NULL)) {
            dberrprint(db, __LINE__, "kcdboccupy");
            err = TRUE;
        }
        etime = kctime();
        dbmetaprint(db, FALSE);
        oprintf("time: %.3f\n", etime - stime);
    }
    if (etc) {
        corepath = kcdbpath(db);
        psiz = strlen(corepath);
        if (strstr(corepath, ".kch") || strstr(corepath, ".kct")) {
            copypath = kcmalloc(psiz + 256);
            sprintf(copypath, "%s.tmp", corepath);
            snappath = kcmalloc(psiz + 256);
            sprintf(snappath, "%s.kcss", corepath);
        } else {
            copypath = kcmalloc(256);
            sprintf(copypath, "kclangctest.tmp");
            snappath = kcmalloc(256);
            sprintf(snappath, "kclangctest.kcss");
        }
        oprintf("copying the database file:\n");
        stime = kctime();
        if (!kcdbcopy(db, copypath)) {
            dberrprint(db, __LINE__, "kcdbcopy");
            err = TRUE;
        }
        etime = kctime();
        dbmetaprint(db, FALSE);
        oprintf("time: %.3f\n", etime - stime);
        remove(copypath);
        oprintf("dumping records into snapshot:\n");
        stime = kctime();
        if (!kcdbdumpsnap(db, snappath)) {
            dberrprint(db, __LINE__, "kcdbdumpsnap");
            err = TRUE;
        }
        etime = kctime();
        dbmetaprint(db, FALSE);
        oprintf("time: %.3f\n", etime - stime);
        oprintf("loading records into snapshot:\n");
        stime = kctime();
        cnt = kcdbcount(db);
        if (rnd && myrand(2) == 0 && !kcdbclear(db)) {
            dberrprint(db, __LINE__, "kcdbclear");
            err = TRUE;
        }
        if (!kcdbloadsnap(db, snappath) || kcdbcount(db) != cnt) {
            dberrprint(db, __LINE__, "kcdbloadsnap");
            err = TRUE;
        }
        etime = kctime();
        dbmetaprint(db, FALSE);
        oprintf("time: %.3f\n", etime - stime);
        remove(snappath);
        kcfree(copypath);
        kcfree(snappath);
        kcfree(corepath);
    }
    oprintf("removing records:\n");
    stime = kctime();
    for (i = 1; !err && i <= rnum; i++) {
        if (tran && !kcdbbegintran(db, FALSE)) {
            dberrprint(db, __LINE__, "kcdbbegintran");
            err = TRUE;
        }
        ksiz = sprintf(kbuf, "%08ld", (long)(rnd ? myrand(rnum) + 1 : i));
        if (!kcdbremove(db, kbuf, ksiz) &&
                ((!rnd && !etc) || kcdbecode(db) != KCENOREC)) {
            dberrprint(db, __LINE__, "kcdbremove");
            err = TRUE;
        }
        if (tran && !kcdbendtran(db, TRUE)) {
            dberrprint(db, __LINE__, "kcdbendtran");
            err = TRUE;
        }
        if (rnum > 250 && i % (rnum / 250) == 0) {
            oputchar('.');
            if (i == rnum || i % (rnum / 10) == 0) oprintf(" (%08ld)\n", (long)i);
        }
    }
    etime = kctime();
    dbmetaprint(db, TRUE);
    oprintf("time: %.3f\n", etime - stime);
    oprintf("closing the database:\n");
    stime = kctime();
    if (!kcdbclose(db)) {
        dberrprint(db, __LINE__, "kcdbclose");
        err = TRUE;
    }
    etime = kctime();
    oprintf("time: %.3f\n", etime - stime);
    kcdbdel(db);
    oprintf("%s\n\n", err ? "error" : "ok");
    return err ? 1 : 0;
}