int parse_exp_rredir(struct s_iterator *i, struct s_btree *node) { struct s_ast_node *ast; struct s_ast_node_cmd *cmd; struct s_token *t; char *copyfile; ast = btree_get(node); if (ast == NULL) return (-1); cmd = &(ast->value.cmd); if (cmd->rtype != RT_RUNKNOWN) return (fprintf(stderr, "error: multiple right redirections\n"), -1); (void) NEXT(i); if (!HAS_NEXT(i)) return (fprintf(stderr, "expected string token after redirection" " but no token was found\n"), -1); t = NEXT(i); if (t->type != TT_STRING) return (fprintf(stderr, "expected string token after redirection\n"), -1); if ((copyfile = strdup(t->string._string)) == NULL) return (fprintf(stderr, "strdup failed\n"), -1); cmd->rtype = RT_RSIMPLE; cmd->rredir = copyfile; return (0); }
void get(SoupServer *server, SoupMessage *msg, const char *path) { struct btval key, val; key.data = (void*)path+1; key.size = strlen(path)-1; key.free_data = FALSE; key.mp = NULL; g_debug ("GET\n Path=%s\n Fetching key %s", path, (char*)key.data); const struct btree_stat *stat = btree_stat(btree); show_dbstats("",stat); if (0 == btree_get(btree,&key,&val)) { g_debug ("hmm %s", (char*)val.data); GVariant *gv = g_variant_new_from_data(G_VARIANT_TYPE_VARIANT, val.data, val.size, TRUE, NULL, NULL); char *debug = g_variant_print (gv, TRUE); g_debug ("converted to %s", debug); g_free (debug); g_variant_unref(gv); soup_message_set_status (msg, SOUP_STATUS_OK); } else { soup_message_set_status (msg, SOUP_STATUS_NOT_FOUND); } // soup_message_set_response (msg, "application/json", SOUP_MEMORY_STATIC, // ); }
static void *dmd_realloc(void *old, int size, const char *file, int line) { struct dmd_memblock *mb, sb; void *p; // debug4("realloc %s:%d (%ul -> %i)", file, line, (unsigned long) old, size); sb.ptr = old; mb = btree_get(&dmd_tree, &sb); if (!mb) { putlog(LOG_MISC, "*", "*** DMD: FAILED REALLOC %s:%d (%d). Old pointer not found. This is probably fatal!", file, line, size); return NULL; } p = mb->ptr; p = realloc(p, size); if (!p) { putlog(LOG_MISC, "*", "*** DMD: FAILED REALLOC %s:%d (%d). realloc() returned NULL", file, line, size); // fatal("Memory re-allocation failed", 0); } if (((unsigned long) p) != ((unsigned long) old)) { // debug0("newpointer"); btree_remove(&dmd_tree, mb); mb = dmd_create(p, size, line, file); btree_add(&dmd_tree, (void *) mb); } else { // debug0("oldpointer"); mb->size = size; /* p = strrchr(file, '/'); strncpy(mb->file, p ? p + 1 : file, DMD_FILE_SIZE); mb->file[DMD_FILE_SIZE] = 0; mb->line = line;*/ } return p; }
int main() { struct btree_node *n, *bt = NULL; //btree_init(&bt); struct mydata foo = { 10, "hello" }; struct mydata bar = { 20, "world" }; printf("EMPTY TREE: %d\n", btree_empty(&bt)); btree_add(&bt, &foo, mycmp); btree_add(&bt, &bar, mycmp); printf("EMPTY TREE: %d\n", btree_empty(&bt)); printf("==== go search ====\n"); /* find existent data */ struct mydata *p = btree_get(bt, &bar, mycmp); shownode("result for 20: ", p); printf("==== go search ====\n"); /* find unexistent data */ struct mydata nop = { 15, NULL }; p = btree_get(bt, &nop, mycmp); shownode("result for 15: ", p); printf("==== go get hittest ====\n"); n = btree_hittest(bt, NULL); shownode("hitest is: ", p); printf("==== go remove 20 ====\n"); if (btree_del(bt, &bar, mycmp, NULL)) printf("node found and removed\n"); else printf("oops\n"); printf("==== go search ====\n"); /* find existent data */ p = btree_get(bt, &bar, mycmp); shownode("result for 20: ", p); printf("==== go search ====\n"); /* find existent data */ p = btree_get(bt, &foo, mycmp); shownode("result for 10: ", p); btree_cleartree(bt, NULL); return 0; }
ssize_t DB::Tx::get(TableOffCache* table, BufferCRef key, BufferRef value) { OverviewPage pgOvv(m_pio->readPage(m_pio->pgidStartPage())); page_id_t pgidRoot = pgOvv.getTableRoot(table); if(pgidRoot == PGID_INVALID) PTNK_THROW_RUNTIME_ERR("table not found"); return btree_get(pgidRoot, key, value, m_pio.get()); }
static void dmd_free(void *p, const char *file, int line) { struct dmd_memblock *mb, sb; // debug2("free %s:%d", file, line); sb.ptr = p; mb = btree_get(&dmd_tree, &sb); if (!mb) { putlog(LOG_MISC, "*", "*** DMD: ATTEMPTING TO FREE NON-MALLOC'D PTR: %s:%d", file, line); return; } btree_remove(&dmd_tree, mb); free(p); }
static char *test_get_an_existing_node() { leaf *root = (leaf *)malloc(sizeof(leaf)); root->value = 100; btree_put(root, 110); btree_put(root, 87); btree_put(root, 90); btree_put(root, 153); btree_put(root, 120); btree_put(root, 124); leaf *node = btree_get(root, 153); mu_assert("The value must be present in the tree", node != NULL); mu_assert("The value recorded is not '153'", node->value == 153); return 0; }
static void *dmd_malloc(int size, const char *file, int line) { struct dmd_memblock *mb; void *ptr; // debug2("malloc %s:%d", file, line); ptr = malloc(size); // debug1("ptr: %u", (unsigned long) ptr); if (!ptr) { putlog(LOG_MISC, "*", "*** DMD: FAILED MALLOC %s (%d) (%d): %s", file, line, size, strerror(errno)); fatal("Memory allocation failed", 0); } mb = dmd_create(ptr, size, line, file); if (btree_get(&dmd_tree, mb)) putlog(LOG_MISC, "*", "*** DMD: DOUBLED POINTER?!?!?"); btree_add(&dmd_tree, (void *) mb); return mb->ptr; }
void get(SoupServer *server, SoupMessage *msg, const char *path) { struct btval key, val; key.data = (void*)path+1; key.size = strlen(path)-1; key.free_data = FALSE; key.mp = NULL; g_debug ("GET\n Path=%s\n Fetching key %s", path, (char*)key.data); const struct btree_stat *stat = btree_stat(btree); show_dbstats("",stat); if (0 == btree_get(btree,&key,&val)) { g_debug ("data checksum is %s", g_compute_checksum_for_data(G_CHECKSUM_MD5, val.data, val.size)); /* we need to make the data 64 bits aligned for gvariant. Best plan is to make all data aligned in the store, but for now, lets just copy it to somewhere aligned. TODO: think about fragmentation. it may just be ok, as so far we delete everything we malloc in this function, despite the lifetimes being interleaved. */ char *buf = g_slice_alloc(val.size + 8); char *data = ALIGN_64(buf); memcpy (data, val.data, val.size); g_debug ("aligned data checksum is %s", g_compute_checksum_for_data(G_CHECKSUM_MD5, data, val.size)); GVariant *gv = g_variant_new_from_data(G_VARIANT_TYPE_VARIANT, data, val.size, TRUE, NULL, NULL); char *debug = g_variant_print (gv, TRUE); g_debug ("converted to %s", debug); g_free (debug); int length; char* ret = json_gvariant_serialize_data(gv, &length); g_variant_unref(gv); g_slice_free1 (val.size + 8, buf); soup_message_set_status (msg, SOUP_STATUS_OK); /*TODO: does soup do anything sensible with it's memory management of responses to reduce the risk of fragmentation? probably not..*/ soup_message_set_response (msg, "application/json", SOUP_MEMORY_TAKE, ret, length); } else { soup_message_set_status (msg, SOUP_STATUS_NOT_FOUND); } }
void db_read_random_test() { uint8_t key[KEYSIZE]; int all=0,i; int start=NUM/2; int end=start+R_NUM; start_timer(); for (i = start; i <end ; ++i) { memset(key,0,sizeof(key)); sprintf(key, "%dkey", rand()%(i+1)); size_t len; void *data = btree_get(&btree,key, &len); if(data!=NULL) all++; else printf("not found:%s\n",key); free(data); if((i%10000)==0) { fprintf(stderr,"finished %d ops%30s\r",i,""); fflush(stderr); } } printf(LINE); double cost=get_timer(); printf("|readrandom (found:%ld): %.6f sec/op; %.1f reads /sec(estimated); %.1f MB/sec; cost:%.6f(sec)\n" ,R_NUM ,(double)(cost/R_NUM) ,(double)(R_NUM/cost) ,(_query_size/cost) ,cost); }
int main(int argc, char **argv) { const char *fname = "test.dat"; if (argc < 2) return 0; srand(time(NULL)); struct btree btree; uint8_t sha1[SHA1_LENGTH]; char val[100]; size_t i; if (file_exists(fname)) { if (btree_open(&btree, fname)) { warning("Unable to open database\n"); return 1; } } else { if (btree_creat(&btree, fname)) { warning("Unable to create database\n"); return 1; } } if (strcmp(argv[1], "insert") == 0) { memset(sha1, 0, sizeof sha1); start_timer(); for (i = 0; i < COUNT; ++i) { sprintf((char *) sha1, "foobar %zd", i); sprintf(val, "value %zd", i*i); btree_insert(&btree, sha1, val, strlen(val)); } printf("insert: %.6f\n", get_timer()); } if (strcmp(argv[1], "get") == 0) { memset(sha1, 0, sizeof sha1); strcpy((char *) sha1, "foobar "); strcpy(val, "value "); start_timer(); for (i = 0; i < COUNT; ++i) { /* optimize a bit */ sprintf((char *) sha1 + 7, "%zd", i); sprintf(val + 6, "%zd", i*i); size_t len; void *data = btree_get(&btree, sha1, &len); if (data == NULL) { warning("not found: %zd\n", i); continue; } if (len != strlen(val) || memcmp(val, data, len)) { warning("data mismatch: %zd\n", i); } free(data); } printf("get: %.6f\n", get_timer()); } else if (strcmp(argv[1], "refill") == 0) { /* delete half of the data, then add it back */ memset(sha1, 0, sizeof sha1); for (i = 0; i < COUNT/2; i++) { sprintf((char *) sha1, "foobar %zd", i); if (btree_delete(&btree, sha1)) warning("not found: %zd\n", i); } memset(sha1, 0, sizeof sha1); for (i = 0; i < COUNT/2; i++) { sprintf((char *) sha1, "foobar %zd", i); sprintf(val, "value %zd", i*i); btree_insert(&btree, sha1, val, strlen(val)); } } else if (strcmp(argv[1], "delete") == 0) { memset(sha1, 0, sizeof sha1); start_timer(); for (i = 0; i < COUNT; i++) { sprintf((char *) sha1, "foobar %zd", i); if (btree_delete(&btree, sha1)) warning("not found: %zd\n", i); } printf("delete: %.6f\n", get_timer()); } else warning("unknown command\n"); btree_close(&btree); return 0; }
int main(int argc, char **argv) { int c, rc = BT_FAIL; unsigned int flags = 0; struct btree *bt; struct cursor *cursor; const char *filename = "test.db"; struct btval key, data, maxkey; while ((c = getopt(argc, argv, "rf:")) != -1) { switch (c) { case 'r': flags |= BT_REVERSEKEY; break; case 'f': filename = optarg; break; } } argc -= optind; argv += optind; if (argc == 0) errx(1, "missing command"); bt = btree_open(filename, flags | BT_NOSYNC, 0644); if (bt == NULL) err(1, filename); bzero(&key, sizeof(key)); bzero(&data, sizeof(data)); bzero(&maxkey, sizeof(maxkey)); if (strcmp(argv[0], "put") == 0) { if (argc < 3) errx(1, "missing arguments"); key.data = argv[1]; key.size = strlen(key.data); data.data = argv[2]; data.size = strlen(data.data); rc = btree_put(bt, &key, &data, 0); if (rc == BT_SUCCESS) printf("OK\n"); else printf("FAIL\n"); } else if (strcmp(argv[0], "del") == 0) { if (argc < 1) errx(1, "missing argument"); key.data = argv[1]; key.size = strlen(key.data); rc = btree_del(bt, &key, NULL); if (rc == BT_SUCCESS) printf("OK\n"); else printf("FAIL\n"); } else if (strcmp(argv[0], "get") == 0) { if (argc < 2) errx(1, "missing arguments"); key.data = argv[1]; key.size = strlen(key.data); rc = btree_get(bt, &key, &data); if (rc == BT_SUCCESS) { printf("OK %.*s\n", (int)data.size, (char *)data.data); } else { printf("FAIL\n"); } } else if (strcmp(argv[0], "scan") == 0) { if (argc > 1) { key.data = argv[1]; key.size = strlen(key.data); flags = BT_CURSOR; } else flags = BT_FIRST; if (argc > 2) { maxkey.data = argv[2]; maxkey.size = strlen(key.data); } cursor = btree_cursor_open(bt); while ((rc = btree_cursor_get(cursor, &key, &data, flags)) == BT_SUCCESS) { if (argc > 2 && btree_cmp(bt, &key, &maxkey) > 0) break; printf("OK %zi %.*s\n", key.size, (int)key.size, (char *)key.data); flags = BT_NEXT; } btree_cursor_close(cursor); } else if (strcmp(argv[0], "compact") == 0) { if ((rc = btree_compact(bt)) != BT_SUCCESS) warn("compact"); } else if (strcmp(argv[0], "revert") == 0) { if ((rc = btree_revert(bt)) != BT_SUCCESS) warn("revert"); } else errx(1, "%s: invalid command", argv[0]); btree_close(bt); return rc; }
ssize_t DB::Tx::get(BufferCRef key, BufferRef value) { OverviewPage pgOvv(m_pio->readPage(m_pio->pgidStartPage())); return btree_get(pgOvv.getDefaultTableRoot(), key, value, m_pio.get()); }