/** * Test that the lockfile works. */ int test_btree_lockfile(void) { int res; struct ndn_btree_io *io = NULL; struct ndn_btree_io *io2 = NULL; io = ndn_btree_io_from_directory(getenv("TEST_DIRECTORY"), NULL); CHKPTR(io); /* Make sure the locking works */ errno = 0; io2 = ndn_btree_io_from_directory(getenv("TEST_DIRECTORY"), NULL); FAILIF(io2 != NULL || errno == 0); errno=EINVAL; res = io->btdestroy(&io); CHKSYS(res); FAILIF(io != NULL); return(res); }
/** * Basic tests of ndn_btree_io_from_directory() and its methods. * * Assumes TEST_DIRECTORY has been set. */ static int test_btree_io(void) { int res; struct ndn_btree_node nodespace = {0}; struct ndn_btree_node *node = &nodespace; struct ndn_btree_io *io = NULL; /* Open it up. */ io = ndn_btree_io_from_directory(getenv("TEST_DIRECTORY"), NULL); CHKPTR(io); node->buf = ndn_charbuf_create(); CHKPTR(node->buf); node->nodeid = 12345; res = io->btopen(io, node); CHKSYS(res); FAILIF(node->iodata == NULL); ndn_charbuf_putf(node->buf, "smoke"); res = io->btwrite(io, node); CHKSYS(res); node->buf->length = 0; ndn_charbuf_putf(node->buf, "garbage"); res = io->btread(io, node, 500000); CHKSYS(res); FAILIF(node->buf->length != 5); FAILIF(node->buf->limit > 10000); node->clean = 5; ndn_charbuf_putf(node->buf, "r"); res = io->btwrite(io, node); CHKSYS(res); node->buf->length--; ndn_charbuf_putf(node->buf, "d"); res = io->btread(io, node, 1000); CHKSYS(res); FAILIF(0 != strcmp("smoker", ndn_charbuf_as_string(node->buf))); node->buf->length--; res = io->btwrite(io, node); CHKSYS(res); node->buf->length = 0; ndn_charbuf_putf(node->buf, "garbage"); node->clean = 0; res = io->btread(io, node, 1000); CHKSYS(res); res = io->btclose(io, node); CHKSYS(res); FAILIF(node->iodata != NULL); FAILIF(0 != strcmp("smoke", ndn_charbuf_as_string(node->buf))); res = io->btdestroy(&io); CHKSYS(res); ndn_charbuf_destroy(&node->buf); return(res); }
struct hash_store* hash_store_ctor(struct ndnr_handle* h) { struct hash_store* self = calloc(1, sizeof(*self)); self->h = h; h->hashstore = self; // open btree with disk IO struct ndn_charbuf* path = ndn_charbuf_create(); ndn_charbuf_putf(path, "%s/hashstore", h->directory); int res = mkdir(ndn_charbuf_as_string(path), 0700); if (res != 0 && errno != EEXIST) { free(self); ndn_charbuf_destroy(&path); return NULL; } self->btree = ndn_btree_create(); self->btree->io = ndn_btree_io_from_directory(ndn_charbuf_as_string(path), NULL); if (self->btree->io == NULL) { free(self); ndn_charbuf_destroy(&path); ndn_btree_destroy(&self->btree); return NULL; } ndn_charbuf_destroy(&path); struct ndn_btree_node* node = ndn_btree_getnode(self->btree, 1, 0); self->btree->nextnodeid = self->btree->io->maxnodeid + 1; if (node->buf->length == 0) { res = ndn_btree_init_node(node, 0, 'R', 0); } LOG("hash_store_ctor\n"); return self; }