/**
 * btree_create
 * @access public
 * @param path to the file to use for mmap, must be absolute
 * @param order maximum number of pointers that can be stored in a node with a limit of BTREE_MAX_ORDER
 * @param maximum total number of items allowed
 * @param size of the data in each node
 * @param error code for error handling on other side
 * @return btree struct
 *
 * This will create a new btree
 * Must be unmapped and closed using btree_close after use
 */
BTREE_API btree_tree *btree_create(const char *path, uint32_t order, uint32_t nr_of_items, size_t data_size, int *error)
{
	btree_tree *tmp_tree;
	*error = 0;

	if (order > BTREE_MAX_ORDER) {
		order = BTREE_MAX_ORDER;
	}

	*error = btree_allocate(path, order, nr_of_items, data_size);
	if (*error != 0) {
		return NULL;
	}

	tmp_tree = malloc(sizeof(btree_tree));
	if (tmp_tree == NULL) {
		*error = errno;
		return NULL;
	}

	*error = btree_open_file(tmp_tree, path);
	if (*error != 0) {
		free(tmp_tree);
		return NULL;
	}

	tmp_tree->path = path;
	tmp_tree->header->order = order;
	tmp_tree->header->max_items = nr_of_items;
	tmp_tree->header->item_size = data_size;

	btree_init(tmp_tree);

	return tmp_tree;
}
Exemple #2
0
int data_structures_init(void)
{
    if (hash_static_init() < 0)
	kernel_panic(MOD_NAME "Static hashing init error!");

    if (btree_init() < 0)
	kernel_panic(MOD_NAME "Btree init error!");

    return 0;
}
Exemple #3
0
void
DB::Tx::tableCreate(BufferCRef table)
{
	OverviewPage pgOvv(m_pio->readPage(m_pio->pgidStartPage()));
	if(PGID_INVALID != pgOvv.getTableRoot(table))
	{
		PTNK_THROW_RUNTIME_ERR("table already exists");	
	}
	
	page_id_t pgidRoot = btree_init(m_pio.get());

	pgOvv.setTableRoot(table, pgidRoot, NULL, m_pio.get());
}
Exemple #4
0
TEST(BTree, Insert)
{
	struct btree_s tree;
	btree_init(&tree, 3);
	int i;

	for (i = 0; i < 10; i++) {
		btree_insert(&tree, i);
	}

	btree_dump(&tree);

	btree_destroy(&tree);
}
Exemple #5
0
int main()
{
	btree_t tree;
	btree_init(&tree);

	btree_add(&tree, 3);
	btree_add(&tree, 1);
	btree_add(&tree, 2);
	btree_add(&tree, 4);

	btree_print(&tree);

	return 0;
}
void test_btree()
{
//	int array[10] = {10,11,12,9,6,5,4,3,2,1};
	int array[10] = {10,9,8,7,6,5,4,3,2,1};
	btnode_t* root;

  	root = btree_init(array, 10);
	if (root == NULL) {
		printf("btree init fail\n");
	}	
	printf("start preorer...\n");
	btree_preorder(root);
	printf("\nstart destroy btree..\n");
	btree_destroy(root);
}
/**
 * btree_empty
 * @access public
 * @param btree struct
 * @return int 0 on success or error value
 *
 * This will attempt to lock the current btree file and
 * completely empty all data by re-initing it as if it were new
 */
BTREE_API int btree_empty(btree_tree *t)
{
	int error;

	error = btree_admin_lock(t);
	if (error != 0) {
		return error;
	}

	btree_init(t);

	error = btree_admin_unlock(t);
	if (error != 0) {
		return error;
	}
	return 0;
}
Exemple #8
0
TEST(BTree, RemoveALot)
{
	struct btree_s tree;
	btree_init(&tree, 3);
	int i;

	for (i = 0; i < 1000000; i++) {
		btree_insert(&tree, i);
	}

	for (i = 10; i < 999992; i++) {
		btree_remove(&tree, i);
	}

	btree_dump(&tree);

	btree_destroy(&tree);
}
Exemple #9
0
TEST(BTree, RemoveALot1)
{
	struct btree_s tree;
	btree_init(&tree, 3);
	int i;

	srand(time(0));

	for (i = 0; i < 100000; i++) {
		btree_insert(&tree, i);
	}

	btree_check(tree.root);

	for (i = 0; i < 99992; i += (rand() % 3 + 1) ) {
		btree_remove(&tree, i);
	}

	btree_destroy(&tree);
}
Exemple #10
0
void
zdb_create(zdb* db)
{
    int i;
    for(i = HOST_CLASS_IN - 1; i < ZDB_RECORDS_MAX_CLASS; i++)
    {
        zdb_zone_label* zone_label;

        ZALLOC_OR_DIE(zdb_zone_label*, zone_label, zdb_zone_label, ZDB_ZONELABEL_TAG);
        ZEROMEMORY(zone_label, sizeof (zdb_zone_label));
        zone_label->name = dnslabel_dup(ROOT_LABEL); /* . */
        dictionary_init(&zone_label->sub);

#if ZDB_CACHE_ENABLED!=0
        btree_init(&zone_label->global_resource_record_set);
#endif
        db->root[i] = zone_label;
    }

    db->alarm_handle = alarm_open((const u8*)"\010database");
}
Exemple #11
0
void
init_bmaps(xfs_mount_t *mp)
{
	xfs_agnumber_t i;

	ag_bmap = calloc(mp->m_sb.sb_agcount, sizeof(struct btree_root *));
	if (!ag_bmap)
		do_error(_("couldn't allocate block map btree roots\n"));

	ag_locks = calloc(mp->m_sb.sb_agcount, sizeof(pthread_mutex_t));
	if (!ag_locks)
		do_error(_("couldn't allocate block map locks\n"));

	for (i = 0; i < mp->m_sb.sb_agcount; i++)  {
		btree_init(&ag_bmap[i]);
		pthread_mutex_init(&ag_locks[i], NULL);
	}

	init_rt_bmap(mp);
	reset_bmaps(mp);
}
Exemple #12
0
TEST(BTree, Remove)
{
	struct btree_s tree;
	btree_init(&tree, 3);
	int i;

	for (i = 0; i < 10; i++) {
		btree_insert(&tree, i);
	}

	btree_dump(&tree);

	btree_remove(&tree, 5);
	btree_dump(&tree);

	btree_remove(&tree, 9);
	btree_dump(&tree);

	btree_remove(&tree, 8);
	btree_dump(&tree);

	btree_destroy(&tree);
}
Exemple #13
0
int main_btree(int argc, char** argv)
{
    btree_t *btree_p = btree_init(string_destroy, NULL);

    printf("%u\n", btree_size(btree_p));

    char *word = malloc(strlen("hello") + 1);
    strcpy(word, "hello");

    btree_node_t *root_node_p = NULL;

    root_node_p = btree_ins_left(btree_p, NULL, word);

    printf("%u\n", btree_size(btree_p));

    char *word2 = malloc(strlen("there") + 1);
    strcpy(word, "there");

    btree_ins_right(btree_p, root_node_p, word2);

    btree_destroy(&btree_p);

    return (EXIT_SUCCESS);
}
Exemple #14
0
TEST(BTREETEST, HandleNoneZeroInput)  {
	MEM_POOL_PTR pMemPool = mem_pool_init(M_1M); 
	BTREE_PTR pBtree = btree_init(pMemPool, "zbtree_index", "/tmp/", 3*MAX, HI_TYPE_LONGLONG);
//	BTREE_PTR pBtree = btree_init(pMemPool, "zbtree_index", "/tmp/", 3*MAX, HI_TYPE_LONG);
	BTREE_PTR pBtree2 = btree_init(pMemPool, "zbtree_index2", "/tmp/", 3*MAX, HI_TYPE_DOUBLE);

	init_profile(1000, pMemPool);
	/*
	struct timeval start, end, end2;               
	double interval; 
	gettimeofday(&start, NULL);

	srand((unsigned int)time(NULL)); 
	int res,i;
	uint64 num;
	//插入10万数据
	for ( i = 0; i < MAX; i += 1 )
	{
		num = rand()%MAX;
		res = btree_insert(pMemPool, pBtree, num, i);
		ASSERT_EQ(res, 0);
	}
	gettimeofday(&end, NULL);
	interval = 1000000*(end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
	printf("insert %d data, cost %.2f ms\n", MAX, interval /1000);

	//范围查询10万次
	BTREE_ENTRY_PTR pBtreeEntry;
	uint32 num2;
//	struct rowid_list *pRowidList;
	for ( i = 0; i < MAX; i += 1 )
	{
		num = rand()%MAX+100;
		pBtreeEntry = btree_search(pBtree, num, CT_LE);
//		num2 = rand()%MAX+100;
//		struct rowid_list * pRowidList = btree_range_query(pBtree, num, 0, num2, 0, pMemPool);
//		if(pRowidList!=NULL)
//		{
//			printf("%d:", i);
//			print_rowid_list(pRowidList);
//		}
		//		ASSERT_EQ((pBtreeEntry != NULL), 1);
	}
	gettimeofday(&end2, NULL);
	interval = 1000000*(end2.tv_sec - end.tv_sec) + (end2.tv_usec - end.tv_usec);
	printf("search %d data, cost %.2f ms\n", MAX, interval /1000);
	*/
	
	//插入3 7 7 9 11 2 4 5 6 8
	int res = btree_insert(pMemPool, pBtree, 3, 1);
	ASSERT_EQ(res, 0);
	res = btree_insert(pMemPool, pBtree, 7, 2);
	ASSERT_EQ(res, 0);
	res = btree_insert(pMemPool, pBtree, 7, 10);
	ASSERT_EQ(res, 0);
	res = btree_insert(pMemPool, pBtree, 9, 3);
	ASSERT_EQ(res, 0);
	res = btree_insert(pMemPool, pBtree, 11, 4);
	ASSERT_EQ(res, 0);
	res = btree_insert(pMemPool, pBtree, 2, 5);
	ASSERT_EQ(res, 0);
	res = btree_insert(pMemPool, pBtree, 4, 6);
	ASSERT_EQ(res, 0);
	res = btree_insert(pMemPool, pBtree, 5, 7);
	ASSERT_EQ(res, 0);
	res = btree_insert(pMemPool, pBtree, 6, 8);
	ASSERT_EQ(res, 0);
	res = btree_insert(pMemPool, pBtree, 8, 9);
	ASSERT_EQ(res, 0);

	//等值查找3 9 8
//	long long value = 3;
	BTREE_ENTRY_PTR pBtreeEntry = btree_search(pBtree, 3, CT_EQ);
	ASSERT_EQ((pBtreeEntry != NULL), 1);
	ASSERT_EQ(pBtreeEntry->key, 3);
	pBtreeEntry = btree_search(pBtree, 9, CT_EQ);
	ASSERT_EQ((pBtreeEntry != NULL), 1);
	ASSERT_EQ(pBtreeEntry->key, 9);
	pBtreeEntry = btree_search(pBtree, 1, CT_EQ);
	ASSERT_EQ((pBtreeEntry == NULL), 1);

	//查找<7 <6 
	pBtreeEntry = btree_search(pBtree, 7, CT_LT);
	ASSERT_EQ((pBtreeEntry != NULL), 1);
	ASSERT_EQ(pBtreeEntry->key, 6);
	pBtreeEntry = btree_search(pBtree, 6, CT_LT);
	ASSERT_EQ((pBtreeEntry != NULL), 1);
	ASSERT_EQ(pBtreeEntry->key, 5);

	//查找>7 >6 
	pBtreeEntry = btree_search(pBtree, 7, CT_GT);
	ASSERT_EQ((pBtreeEntry != NULL), 1);
	ASSERT_EQ(pBtreeEntry->key, 8);
	pBtreeEntry = btree_search(pBtree, 6, CT_GT);
	ASSERT_EQ((pBtreeEntry != NULL), 1);
	ASSERT_EQ(pBtreeEntry->key, 7);

	//测试btree next
	//BTREE_ENTRY_PTR pEntry = btree_search(pBtree, 2, 0);
	BTREE_ENTRY_PTR pEntry = btree_get_start_entry(pBtree);
	struct doc_row_unit *pUnit;
	while(pEntry != NULL)
	{
		std::cout << "key = " << pEntry->key << " docID: ";
		//输出doclist
		pUnit = doclist_next(pBtree->pDoclist, pEntry->doclist);
		while(pUnit != NULL)
		{
			std::cout << pUnit->doc_id << " ";
			pUnit = doclist_next(pBtree->pDoclist, pUnit->next);
		}
		pEntry = btree_next(pBtree, pEntry);
		std::cout << std::endl;
	}
	std::cout << std::endl;

	struct rowid_list * pRowidList = btree_range_query(pBtree, 0, 0, 10, 0, pMemPool);
	print_rowid_list(pRowidList);

	BTREE_KEY key;
	double value;
	//insert 3.1 11.11 111.111 1111.1 11111.1 11.11
	value =3.1;
	memcpy(&key, &value, 8);
	res = btree_insert(pMemPool, pBtree2, key, 1);
	ASSERT_EQ(res, 0);

	value =11.11;
	memcpy(&key, &value, 8);
	res = btree_insert(pMemPool, pBtree2, key, 2);
	ASSERT_EQ(res, 0);

	value =111.111;
	memcpy(&key, &value, 8);
	res = btree_insert(pMemPool, pBtree2, key, 3);
	ASSERT_EQ(res, 0);

	value =1111.1;
	memcpy(&key, &value, 8);
	res = btree_insert(pMemPool, pBtree2, key, 4);
	ASSERT_EQ(res, 0);

	value =11111.1;
	memcpy(&key, &value, 8);
	res = btree_insert(pMemPool, pBtree2, key, 5);
	ASSERT_EQ(res, 0);

	value =11.11;
	memcpy(&key, &value, 8);
	res = btree_insert(pMemPool, pBtree2, key, 6);
	ASSERT_EQ(res, 0);

	value =1111;
	memcpy(&key, &value, 8);
	pRowidList = btree_range_query(pBtree2, 0, 0, key, 0, pMemPool);
	print_rowid_list(pRowidList);

	pEntry = btree_get_start_entry(pBtree2);
	while(pEntry != NULL)
	{
		std::cout << "key = " << *(double*)&pEntry->key << " docID: ";
		//输出doclist
		pUnit = doclist_next(pBtree2->pDoclist, pEntry->doclist);
		while(pUnit != NULL)
		{
			std::cout << pUnit->doc_id << " ";
			pUnit = doclist_next(pBtree2->pDoclist, pUnit->next);
		}
		pEntry = btree_next(pBtree2, pEntry);
		std::cout << std::endl;
	}
	std::cout << std::endl;
	//测试dump
	//btree_dump(pBtree);
	btree_destroy(pBtree);
	btree_destroy(pBtree2);
}
Exemple #15
0
struct r_bin_mz_segment_t * r_bin_mz_get_segments(const struct r_bin_mz_obj_t *bin) {
#if 0
	int i;
	struct r_bin_mz_segment_t *ret;

	const MZ_image_relocation_entry * const relocs = bin->relocation_entries;
	const int num_relocs = bin->dos_header->num_relocs;

	eprintf ("cs 0x%x\n", bin->dos_header->cs);
	eprintf ("ss 0x%x\n", bin->dos_header->ss);
	for (i = 0; i < num_relocs; i++) {
		eprintf ("0x%08x segment 0x%08lx\n", relocs[i].offset, relocs[i].segment);
		// ut65 paddr = r_bin_mz_seg_to_paddr (bin, relocs[i].segment) + relocs[i].offset;
		// eprintf ("pa 0x%08llx\n", paddr);
	}
	btree_add (&tree, (void *)&first_segment, cmp_segs);
	/* Add segment address of stack segment if it's resides inside dos
	executable.
	*/
	if (r_bin_mz_seg_to_paddr (bin, stack_segment) < bin->dos_file_size) {
		btree_add (&tree, (void *)&stack_segment, cmp_segs);
	}
	return NULL;
#endif
#if 1
	struct btree_node *tree;
	struct r_bin_mz_segment_t *ret;
	ut16 *segments, *curr_seg;
	int i, num_segs;
	ut64 paddr;
	const ut16 first_segment = 0;
	const ut16 stack_segment = bin->dos_header->ss;
	const MZ_image_relocation_entry * const relocs = bin->relocation_entries;
	const int num_relocs = bin->dos_header->num_relocs;
	const ut64 last_parag = ((bin->dos_file_size + 0xF) >> 4) - \
		bin->dos_header->header_paragraphs;

	btree_init (&tree);
	for (i = 0; i < num_relocs; i++) {
		paddr = r_bin_mz_seg_to_paddr (bin, relocs[i].segment) + relocs[i].offset;
		if ((paddr + 2) < bin->dos_file_size) {
			curr_seg = (ut16 *)(bin->b->buf + paddr);
			/* Add segment only if it's located inside dos executable data */
			if (r_read_le16 (curr_seg) <= last_parag) {
				btree_add (&tree, curr_seg, cmp_segs);
			}
		}
	}

	/* Add segment address of first segment to make sure that it will be
	added. If relocations empty or there isn't first segment in relocations.)
	*/
	btree_add (&tree, (void *)&first_segment, cmp_segs);
	/* Add segment address of stack segment if it's resides inside dos
	executable.
	*/
	if (r_bin_mz_seg_to_paddr (bin, stack_segment) < bin->dos_file_size) {
		btree_add (&tree, (void *)&stack_segment, cmp_segs);
	}

	if (!num_relocs) {
		btree_cleartree (tree, NULL);
		return NULL;
	}
	segments = calloc (1 + num_relocs, sizeof (*segments));
	if (!segments) {
		eprintf ("Error: calloc (segments)\n");
		btree_cleartree (tree, NULL);
		return NULL;
	}
	curr_seg = segments;
	btree_traverse (tree, 0, &curr_seg, trv_segs);
	num_segs = curr_seg - segments;
	ret = calloc (num_segs + 1, sizeof (struct r_bin_mz_segment_t));
	if (!ret) {
		free (segments);
		btree_cleartree (tree, NULL);
		eprintf ("Error: calloc (struct r_bin_mz_segment_t)\n");
		return NULL;
	}

	btree_cleartree (tree, NULL);

	ret[0].paddr = r_bin_mz_seg_to_paddr (bin, segments[0]);
	for (i = 1; i < num_segs; i++) {
		ret[i].paddr = r_bin_mz_seg_to_paddr (bin, segments[i]);
		ret[i - 1].size = ret[i].paddr - ret[i - 1].paddr;
	}
	ret[i - 1].size = bin->dos_file_size - ret[i - 1].paddr;
	ret[i].last = 1;
	free (segments);
	return ret;
#endif
}
Exemple #16
0
void db_init_test()
{
	random_value();
	btree_init(&btree,DBNAME);
}
Exemple #17
0
void setcache(char *folder, char *searchstring, url_llist *urls)
{
	long i, j;
	urlinfo *url;
	indexed_url *iurl;
	url_node *node = urls->front;
	btree indexed_urls;
	btree_init(&indexed_urls, (void *)indexed_urlcompare);
	
	// loop 1: index urls
	for (i = 0; i < urls->size; i++)
	{
		indexed_url *iurl = malloc(sizeof(indexed_url));
		iurl->url = node->url;
		iurl->index = i;

		btree_insert(&indexed_urls, iurl);
				
		node = node->next;
	}

	// open file for write
	char *modifiedsearch = tounderline(searchstring);
	char *path = getpath(folder, modifiedsearch);
	
	FILE *file = fopen(path, "w");
	
	// write number of links
	fprintf(file, "%d\n", urls->size);

	// loop 2: write data
	node = urls->front;
	indexed_url urltofind;
	for (i = 0; i < urls->size; i++)
	{
		url = node->url;
		
		/*
		 * Write url data
		 * format:	index url numlinks
		 *		link1 link2 link3
		 */
		// write header
		char *urlstring = url_tostring(url);
		fprintf(file, "%s %d\n", urlstring, url->outlinks.size);
		free(urlstring);
		// write links
		lnode *outlink_node = url->outlinks.front;
		for (j = 0; j < url->outlinks.size; j++)
		{	
			// find indexed url so its index can be recorded
			urltofind.url = outlink_node->data;
			indexed_url *iurl = btree_find(&indexed_urls, &urltofind);
			
			// write it, followed by a space
			fprintf(file, "%lu ", iurl->index);
			outlink_node = outlink_node->next;
		}
		if (url->outlinks.size)
			fprintf(file, "\n");
		node = node->next;
	}

	btree_free(&indexed_urls, 1);
	free(modifiedsearch);
	free(path);
	close(file);
}
Exemple #18
0
int main_bstree(int argc, char** argv)
{
    bool ok = true;

    /* Load forums from file into list. */

    list_t *list_p = NULL;

    if (ok)
        ok = (list_p = list_init()) != NULL;

    if (ok)
    {
        printf("-> Loading forum.csv...\n");
        ok = load_file("forum" ".csv", list_p);
    }

    printf("Forums: %d\n", list_size(list_p));

    /* Timing variables. */

    double t1, t2, real_time;
    struct tms tb;
    double tickspersec = (double) sysconf(_SC_CLK_TCK);

    /* Lookup a specific forum in the list for some iterations. */

    t1 = (double) times(&tb);

    unsigned int iter1, m;

    iter1 = 4 * 0;

    for (m = 0; m < iter1; m++)
    {
        unsigned int i, size;

        char *forum_name = "Wall of Eric Wilby";

        size = list_size(list_p);

        for (i = 0; i < size; i++)
        {
            forum_t *forum_p = NULL;
            list_get(list_p, (void **) &forum_p, i);
            if (!strcmp(forum_get_title(forum_p), forum_name))
            {
                //                printf("%s\n", forum_get_title(forum_p));
                break;
            }
        }
    }

    t2 = (double) times(&tb);
    real_time = (double) (t2 - t1) / tickspersec;
    printf("Completed %u list lookups in %.3f sec\n", m, real_time);

    /* Load all forums in list to binary search tree. */

    btree_t *btree_p = btree_init(forum_dstr, forum_cmp);
    if (btree_p == NULL)
        return (EXIT_FAILURE);

    unsigned int i, size;

    size = list_size(list_p);

    for (i = 0; i < size; i++)
    {
        forum_t *f_p = NULL;
        list_get(list_p, (void **) &f_p, i);
        btree_ins(btree_p, f_p);
    }
    
    printf("Loaded %u forums.\n", btree_size(btree_p));

    /* Print forums in alphabetical order, exec print function with INORDER mode. */

    unsigned int limit = 1500;
    bool ret = btree_exec(btree_p, INORDER, forum_print, &limit);
    printf("%u\n", ret);

    /* Perform lookups in binary search tree */

    forum_t *forum_p = forum_init(0, "Wall of Eric Wilby", 0);

    t1 = (double) times(&tb);

    unsigned int n, iter2;

    iter2 = 667;

    for (n = 0; n < iter2; n++)
    {
        btree_node_t *btree_node_p = btree_find(btree_p, forum_p);
        if (btree_node_p != NULL)
        {
            forum_t *f_p = (forum_t *) btree_node_get_data(btree_node_p);
            forum_get_title(f_p);
            // printf("%s\n", forum_get_title(f_p));
        }
    }

    t2 = (double) times(&tb);
    real_time = (double) (t2 - t1) / tickspersec;
    printf("Completed %u bstree lookups in %.3f sec\n", n, real_time);

    /* Free all memory */

    forum_destroy(&forum_p);
    btree_destroy(&btree_p); // btree destroys forums, which are also on the list

    //    size = list_size(list_p);
    //
    //    for (i = 0; i < size; i++)
    //    {
    //        forum_t *f_p = NULL;
    //        list_get(list_p, (void **) &f_p, i);
    //        forum_destroy(&f_p);
    //    }

    list_destroy(&list_p);

    return (EXIT_SUCCESS);
}