Пример #1
0
int main(void)
{
	btree_tree *tmp;
	uint32_t data_idx;

	tmp = btree_create("test.mmap", 3, 400, 1024);
	if (!tmp) {
		printf("Couldn't create tree from disk image.\n");
		exit(1);
	}

	setup(tmp);
	btree_insert(tmp, 'd', &data_idx);
	btree_insert(tmp, 'f', &data_idx);
	btree_insert(tmp, 'v', &data_idx);

	btree_delete(tmp, 'i');
	btree_delete(tmp, 'f');
	btree_delete(tmp, 'e');
	btree_delete(tmp, 'y');
	btree_delete(tmp, 't');
	btree_dump_dot(tmp);


	btree_free(tmp);

	return 0;
}
/*!-----------------------------------------------------------------------

    d e l e t e M e m o r y C h u n k

    @brief Delete a chunk of memory from both indices.

    This function will delete a chunk of memory from both indices.

    @param[in] - The header of the memory chunk to delete.

    @return  0 = Success
            ~0 = Failure (errno value)

------------------------------------------------------------------------*/
static int deleteMemoryChunk ( memoryChunk_t* chunk )
{
    int status = 0;

    //
    //  Go remove the given memory chunk from the bySize index.
    //
    status = btree_delete ( &sysControl->availableMemoryBySize, chunk );
    if ( status != 0 )
    {
        printf ( "Error: Could not delete memory chunk by size at offset %lx "
                 "- Aborting\n", chunk->offset );
        return status;
    }
    //
    //  Go remove the given memory chunk from the byOffset index.
    //
    status = btree_delete ( &sysControl->availableMemoryByOffset, chunk );
    if ( status != 0 )
    {
        printf ( "Error: Could not delete memory chunk by offset at %lx - "
                 "Aborting\n", chunk->offset );
    }
    return status;
}
int main(void)
{
	btree_tree *tmp;
	int error = 0;

	tmp = btree_create("test.mmap", 3, 400, 1024, &error);
	if (!tmp) {
		printf("Couldn't create tree from disk image error %d.\n", error);
		exit(1);
	}

	setup(tmp);
	btree_insert(tmp, 'd');
	btree_insert(tmp, 'f');
	btree_insert(tmp, 'v');

	btree_delete(tmp, 'i');
	btree_delete(tmp, 'f');
	btree_delete(tmp, 'e');
	btree_delete(tmp, 'y');
	btree_delete(tmp, 't');
	btree_dump_dot(tmp);

	btree_close(tmp);

	return 0;
}
Пример #4
0
void
btree_delete(btree_t *root)
{
    if (!root) {
        return;
    }

    btree_delete(root->left);
    btree_delete(root->right);
    free(root);
}
Пример #5
0
Btree btree_delete(String key, Btree t)
{
  if (btree_isempty(t))
    return t;
  else if (strcmp(key, t->key) < 0){
    if (btree_isempty(t->left))
      return t;
    else{
      t->left = btree_delete(key, t->left);
      return t;
    }}
    else if (strcmp(key, t->key) > 0){
      if (btree_isempty(t->right))
	return t;
      else{
	t->right = btree_delete(key, t->right);
	return t;
      }}
    else if ((btree_isempty(t->right)) && (btree_isempty(t->left))){
      free(t->key);
      free(t->value);
      free(t);
	return btree_empty();
    }
    else if (btree_isempty(t->left)){
      Btree p = t->right;
      free(t->key);
      free(t->value);
      free(t);
	return p;
    }
    else if (btree_isempty(t->right)){
      Btree p = t->left;
      free(t->key);
      free(t->value);
      free(t);
	return p;
    }
      else{
	Btree p;
        p->key = min_key(t->right);
	p->value = min_val(t->right);
	p->right = deletemin(t->right);
	free(t->key);
	free(t->value);
	free(t);
	return p;
      }
}
Пример #6
0
int main()
{
    int arr[] = {18, 31, 12, 10, 15, 48, 45, 47, 50, 52, 23, 30, 20};
//    int arr[] = {18, 31, 12, 10};

    btree_node *root = btree_create();

    for(int i = 0; i < sizeof(arr) / sizeof(int); i++)
    {
        root = btree_insert(root, arr[i]);
        btree_level_display(root);
        btree_linear_print(root);
    }

    //int todel[] = {15, 18, 23, 30, 31, 52, 50};
    int todel[] = {45, 30, 12, 10};
    for(int i = 0; i < sizeof(todel) / sizeof(int); i++)
    {
        printf("after delete %d\n", todel[i]);
        root = btree_delete(root, todel[i]);
        btree_level_display(root);
        btree_linear_print(root);
    }

    return 0;
}
Пример #7
0
/**
* @brief test case for random number insert, search and delete.
*/
void testcase_for_random() {
	TREE* T = btree_create();
	int i = 0, n = 100;
	int _t = 0;
	srand(time(NULL));
	for (i = 0; i < n; i ++) {
		_t = rand() % n;
		printf("btree_insert(T, %d) = %d\n", _t, btree_insert(T, _t));
	}
	for (i = 0; i < n; i ++) {
		_t = rand() % n;
		printf("btree_search(T, %d) = %d\n", _t, btree_search(T, _t));
	}

	printf("btree_traveral: ");
	btree_traveral(T, printout_node);
	printf("\n");

	for (i = 0; i < n; i ++) {
		_t = rand() % n;
		printf("btree_delete(T, %d) = %d\n", _t, btree_delete(T, _t));
	}

	printf("btree_traveral: ");
	btree_traveral(T, printout_node);
	printf("\n");

	btree_destory(T);
}
Пример #8
0
/**
* @brief test case for single insert, search and delete method.
*/
void testcase_for_single() {
	TREE* T = btree_create();

	printf("btree_insert(T, 32) = %d\n", btree_insert(T, 32));

	printf("btree_search(T, 32) = %d\n", btree_search(T, 32));

	printf("btree_delete(T, 32) = %d\n", btree_delete(T, 32));

	printf("btree_search(T, 32) = %d\n", btree_search(T, 32));

	printf("btree_traveral: ");
	btree_traveral(T, printout_node);
	printf("\n");

	btree_destory(T);
}
Пример #9
0
int vfs_unmount(struct vnode *node)
{
    ASSERT(V_ISDIR(node->v_flags));

    struct vnode *m_node = get_real_node(node);
    ASSERT(m_node != node);
    ASSERT(m_node->v_count == 1);

    int ret = m_node->v_vfs->vfs_ops->vfs_unmount(m_node->v_vfs);
    kfree(m_node->v_vfs);

    if (!ret)
    {
        btree_delete(node->v_vfs->vfs_mounts, (void*) node->v_inode);
        vput(m_node);
    }

    return ret;
}
Пример #10
0
int main(){
  char command[100];
  char word[100];
  char tango[100];
  tnode *tree = NULL;
  tnode *temp = NULL;

  while(1){
    scanf("%s", command);
    if(strcmp(command, "insert") == 0){
      scanf("%s %s", word, tango);
      temp = btree_insert(word, tango, tree);
      if(tree == NULL){
	tree = temp;
      }
    }else if(strcmp(command, "delete") == 0){
      scanf("%s", word);
      temp = btree_delete(word, tree);
      if(temp == tree->left){
	tree = tree->left;
      }else if(temp == tree->right){
	tree = tree->right;
      }
      
	
    }else if(strcmp(command, "search") == 0){
      scanf("%s", word);
      temp = btree_search(word, tree);
      if(temp == NULL){
	printf("(not found)\n");
      }else{
	printf("%s\n", temp->value);
      }
    }else if(strcmp(command, "quit") == 0){
      break;
    }else{
      printf("ERROR\n");
    }
  }

  btree_destroy(tree);
  return 0;
}
Пример #11
0
int test_btree(string **values, int n){
  int i;
  btree *tree = make_btree();
  uint64_t *hashes = xmalloc(n*sizeof(uint64_t));
  uint64_t *indices = xmalloc(n*sizeof(uint64_t));
  //nfor now I'm just using sequential ints for keys
  DEBUG_PRINTF("Testing btree insertion\n");
  for(i=0;i<n;i++){
    assert(check_tree(tree) >= 0);
    indices[i] = i;
    hashes[i] = fnv_hash(values[i]->mem, values[i]->len);
    btree_insert(tree, hashes[indices[i]], values[i]);
  }
  assert(check_tree(tree) >= 0);
  shuffle_array((void**)indices, n);
  DEBUG_PRINTF("Testing btree lookup\n");
  for(i=0;i<n;i++){
    string *str = btree_lookup(tree, hashes[indices[i]]);
    WARN_ON_ONCE(!string_ptr_eq(str, values[indices[i]]));
  }
  DEBUG_PRINTF("Testing btree deletion\n");  
  for(i=0;i<n;i++){
    uint64_t num_keys = count_num_keys(tree->root);
    if(num_keys != (n-i)){
      DEBUG_PRINTF("Tree has %lu keys, expected %lu\n",
                   num_keys, n-i);
      exit(1);
    }
    assert(check_tree(tree) >= 0);
    if(btree_lookup(tree, hashes[indices[i]])){
      if(!btree_delete(tree, hashes[indices[i]])){
        exit(1);
      }
    } else {
      DEBUG_PRINTF("Couldn't find key %lu\n",indices[i]);
    }
  }
  WARN_ON(check_tree(tree) < 0);
  WARN_ON(tree->root->n_keys != 0);
  return 0;
}
Пример #12
0
int main()
{
  Btree t = (struct tnode *)malloc(sizeof(struct tnode));
  t = btree_empty();
  String s = (char *)malloc(50);
  String a = (char *)malloc(50);
  String b = (char *)malloc(50);
  while (scanf("%49s ", s) != EOF){
    if (strcmp(s, "insert") == 0){
      scanf("%49s ", a);
      scanf("%49s\n", b);
      t = btree_insert(a, b, t);
    }
    else if (strcmp(s, "delete") == 0){
      scanf("%49s\n", a);
      t = btree_delete(a, t);
    }
    else if (strcmp(s, "search") == 0){
      scanf("%49s\n", a);
      if (btree_search(a, t) == NULL)
	printf("(not found)\n");
      else
	printf("%s\n", (btree_search(a, t)));
    }
    else if (strcmp(s, "quit") == 0)
      break;
    else{
      printf("(unknown command)\n");
    }
  }

  free_btree(t);
  free(a);
  free(b);
  free(s);

return 0;

}
Пример #13
0
int main(void)
{
	btree_tree *tmp;
	uint32_t data_idx;
	char data[1024];

	memset(data, 4, 1023);
	memset(data+1023, 0, 1);

	tmp = btree_create("test.mmap", 3, 400, 1024);
	if (!tmp) {
		printf("Couldn't create tree from disk image.\n");
		exit(1);
	}

	/* Testing one node in the root only */
	btree_insert(tmp, 'A', &data_idx); printf("%u\n", data_idx);
	btree_dump(tmp);
	btree_delete(tmp, 'A');
	btree_dump(tmp);


	/* Testing two node in the root only (1) */
	btree_insert(tmp, 'G', &data_idx); printf("%u\n", data_idx);
	btree_insert(tmp, 'Q', &data_idx); printf("%u\n", data_idx);
	btree_dump(tmp);
	btree_delete(tmp, 'G');
	btree_dump(tmp);
	/* - cleanup for next test */
	btree_delete(tmp, 'Q');


	/* Testing two node in the root only (2) */
	btree_insert(tmp, 'G', &data_idx); printf("%u\n", data_idx);
	btree_insert(tmp, 'Q', &data_idx); printf("%u\n", data_idx);
	btree_dump(tmp);
	btree_delete(tmp, 'Q');
	btree_dump(tmp);
	/* - cleanup for next test */
	btree_delete(tmp, 'G');


	/* Testing with full root node */
	btree_insert(tmp, 'E', &data_idx); printf("%u\n", data_idx);
	btree_insert(tmp, 'A', &data_idx); printf("%u\n", data_idx);
	btree_insert(tmp, 'P', &data_idx); printf("%u\n", data_idx);
	btree_insert(tmp, 'F', &data_idx); printf("%u\n", data_idx);
	btree_insert(tmp, 'Q', &data_idx); printf("%u\n", data_idx);
	btree_dump(tmp);
	/* - remove last node */
	btree_delete(tmp, 'Q');
	btree_dump(tmp);
	/* - remove middle node */
	btree_delete(tmp, 'E');
	btree_dump(tmp);
	/* - remove first node */
	btree_delete(tmp, 'A');
	btree_dump(tmp);

	btree_free(tmp);

	return 0;
}
Пример #14
0
static void
update_bmap(
	struct btree_root	*bmap,
	unsigned long		offset,
	xfs_extlen_t		blen,
	void			*new_state)
{
	unsigned long		end = offset + blen;
	int			*cur_state;
	unsigned long		cur_key;
	int			*next_state;
	unsigned long		next_key;
	int			*prev_state;

	cur_state = btree_find(bmap, offset, &cur_key);
	if (!cur_state)
		return;

	if (offset == cur_key) {
		/* if the start is the same as the "item" extent */
		if (cur_state == new_state)
			return;

		/*
		 * Note: this may be NULL if we are updating the map for
		 * the superblock.
		 */
		prev_state = btree_peek_prev(bmap, NULL);

		next_state = btree_peek_next(bmap, &next_key);
		if (next_key > end) {
			/* different end */
			if (new_state == prev_state) {
				/* #1: prev has same state, move offset up */
				btree_update_key(bmap, offset, end);
				return;
			}

			/* #4: insert new extent after, update current value */
			btree_update_value(bmap, offset, new_state);
			btree_insert(bmap, end, cur_state);
			return;
		}

		/* same end (and same start) */
		if (new_state == next_state) {
			/* next has same state */
			if (new_state == prev_state) {
				/* #3: merge prev & next */
				btree_delete(bmap, offset);
				btree_delete(bmap, end);
				return;
			}

			/* #8: merge next */
			btree_update_value(bmap, offset, new_state);
			btree_delete(bmap, end);
			return;
		}

		/* same start, same end, next has different state */
		if (new_state == prev_state) {
			/* #5: prev has same state */
			btree_delete(bmap, offset);
			return;
		}

		/* #6: update value only */
		btree_update_value(bmap, offset, new_state);
		return;
	}

	/* different start, offset is in the middle of "cur" */
	prev_state = btree_peek_prev(bmap, NULL);
	ASSERT(prev_state != NULL);
	if (prev_state == new_state)
		return;

	if (end == cur_key) {
		/* end is at the same point as the current extent */
		if (new_state == cur_state) {
			/* #7: move next extent down */
			btree_update_key(bmap, end, offset);
			return;
		}

		/* #9: different start, same end, add new extent */
		btree_insert(bmap, offset, new_state);
		return;
	}

	/* #2: insert an extent into the middle of another extent */
	btree_insert(bmap, offset, new_state);
	btree_insert(bmap, end, prev_state);
}
/*
 * Display the results of searching for an item before and after removing it from the tree.
 * Parameters: binary tree, item to remove
 */
void test_remove(struct btree_tree *tree, int item) {
	  printf("Before remove item %i - Search resulted in %i.\n", item, btree_search(tree, item));
	  btree_delete(tree, item);
	  printf("After remove item %i - Search resulted in %i.\n\n", item, btree_search(tree, item));
}
Пример #16
0
int main(int argc,char *argv[])
{ 
  int i;
  struct btree_root *bt;
  struct btree_node *bn;
  
  ptr_t key[] = 
    {
      1,2,3,4,30,20,10,30,24,20,3,4,39
    };
  
  
  if(!(bt = init_root(compare,2)))
    return -1;
  printf("key len:%d\n",LEN(key));
  
  for(i=0;i<LEN(key);i++)
  {
    printf("-------begin(insert:%d)---------\n",key[i]);
    
  if(btree_insert(bt,key[i])<0) 
  {
    fprintf(stderr,"insert into btree error!\n");
    goto _destroy;
  }
  //  btree_travel(bt);
  printf("--------end(insert:%d)------\n",key[i]);
  
  }
  
  btree_travel(bt);
  /*
  if(btree_delete(bt,100)<0)
  {
    fprintf(stderr,"delete node key:%lu error\n",100UL);
    goto _destroy;
  }
  else fprintf(stderr,"delete node key:%lu successful\n",100UL);
  */
  for(i=0;i<LEN(key);i++)
  {
    printf("-----------begin(delete:%d,i:%d)----------\n",key[i],i);
    //if(btree_full_delete(bt,NULL,key[i])<0)
    btree_delete(bt,NULL,key[i]);
    /*
    {
      fprintf(stderr,"delete node key:%u error\n",key[i]);
      //goto _destroy;
    }
    else 
    {
      fprintf(stderr,"delete node key:%u succesful\n",key[i]);
    }
    */
    printf("--------travel------\n");
    btree_travel(bt);
    
    printf("-----------end(delete:%d)------------\n",key[i]);
  }
  
  /*
  if(btree_search(bt,100,&bn)<0)
  {
    fprintf(stderr,"cann't search the key:%ld\n",100L);
    goto _destroy;
  }
  else
  fprintf(stderr,"node length:%d,key:%lu\n",bn->n,bn->key[0]);
  */
  btree_travel(bt);
  
 _destroy:
  destroy_root(bt);
  return 0;
}
Пример #17
0
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;
}