Beispiel #1
0
void print_tree(struct p_tree *t, int mode)
{
	if( !is_leaf_node(t) )
	{
		if( mode == TREE_PRINT ) printf("(");
		if( !is_leaf_node(t) ) print_tree(t->left, mode);
		if( mode == TREE_PRINT ) printf(",");
		if( !is_leaf_node(t) ) print_tree(t->right, mode);
		if( mode == TREE_PRINT ) printf(")");
	}
//	else printf("%s", t->name);
	else {
		if( mode == TREE_PRINT ) {
			if( t->sp_code != -1 ) printf("%s%d", t->name, t->sp_code);
			else printf("%s", t->name);
		}
		else if( mode == INT_PRINT ) {
			if( t->sp_code != -1 ) {
				if( t->od == 0 ) printf("s %s%d + %d %d\n", t->name, t->sp_code, t->reg.lower, (t->reg.upper)-1); 
				else if( t->od == 1 ) printf("s %s%d - %d %d\n", t->name, t->sp_code, t->reg.lower, (t->reg.upper)-1); 
			}
			else {
				if( t->od == 0 ) printf("s %s + %d %d\n", t->name, t->reg.lower, (t->reg.upper)-1); 
				else if( t->od == 1 ) printf("s %s - %d %d\n", t->name, t->reg.lower, (t->reg.upper)-1); 
			}
		}
	}
}
static int _search_by_key (reiserfs_filsys_t fs, struct key * key, struct path * path)
{
    struct buffer_head * bh;
    unsigned long block = SB_ROOT_BLOCK (fs);
    struct path_element * curr;
    int retval;
    
    path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET;
    while (1) {
	curr = PATH_OFFSET_PELEMENT (path, ++ path->path_length);
	bh = curr->pe_buffer = bread (fs->s_dev, block, fs->s_blocksize);
        if (bh == 0) {
	    path->path_length --;
	    pathrelse (path);
	    return ITEM_NOT_FOUND;
	}
	retval = _bin_search (key, B_N_PKEY (bh, 0), B_NR_ITEMS (bh),
			      is_leaf_node (bh) ? IH_SIZE : KEY_SIZE, &(curr->pe_position), comp_keys);
	if (retval == ITEM_FOUND) {
	    /* key found, return if this is leaf level */
	    if (is_leaf_node (bh)) {
		path->pos_in_item = 0;
		return ITEM_FOUND;
	    }
	    curr->pe_position ++;
	} else {
	    /* key not found in the node */
	    if (is_leaf_node (bh))
		return ITEM_NOT_FOUND;
	}
	block = B_N_CHILD_NUM (bh, curr->pe_position);
    }
    printf ("search_by_key: you can not get here\n");
    return ITEM_NOT_FOUND;
}
Beispiel #3
0
static void reiserfsck_check_tree (int dev, int block, int size, check_function_t comp_func)
{
    struct buffer_head * bh;
    int what_node;
    
    bh = bread (dev, block, size);
    if (bh == 0)
        reiserfs_panic("reiserfsck_check_tree: unable to read %lu block on device 0x%x\n",
                block, dev);

    if (!B_IS_IN_TREE (bh)) {
	reiserfs_panic (0, "reiserfsck_check_tree: buffer (%b %z) not in tree", bh, bh);
    }
    
    what_node = who_is_this (bh->b_data, bh->b_size);
    if (what_node != THE_LEAF && what_node != THE_INTERNAL)
	die ("Not formatted node");

    if (!is_block_used (bh->b_blocknr))
	die ("Not marked as used");

    if (is_leaf_node (bh) && is_leaf_bad_xx (bh))
	die ("Bad leaf");

    if (is_internal_node(bh) && is_internal_bad (bh))
	die ("bad internal");
    
    if (is_internal_node (bh)) {
	int i;
	struct disk_child * dc;
	
	dc = B_N_CHILD (bh, 0);
	for (i = 0; i <= B_NR_ITEMS (bh); i ++, dc ++) {
	    reiserfsck_check_tree (dev, dc_block_number(dc), size, comp_func);
	    g_dkey = B_N_PDELIM_KEY (bh, i);
	}
    } else if (is_leaf_node (bh)) {
	g_right = bh;
	if (g_left != 0 && g_dkey != 0) {
	    comp_func ();
	    brelse (g_left);
	}
	g_left = g_right;
	return;
    } else {
	reiserfs_panic ("reiserfsck_check_tree: block %lu has bad block type (%b)",
			bh->b_blocknr, bh);
    }
    brelse (bh);
}
Beispiel #4
0
void delete_one_sp(struct p_tree *t, int sp)
{
	struct p_tree *left, *right;
	struct p_tree *parent;	

	left = t->left;
	right = t->right;
	parent = t->parent;

	if( (left != NULL) && (is_leaf_node(left)) && left->sp_code == sp ) {
		if( (right != NULL) && (is_leaf_node(right)) && right->sp_code == sp ) {
			if( t->left->ch_sp != NULL ) free(t->left->ch_sp);
			free(t->left);
			if( t->right->ch_sp != NULL ) free(t->right->ch_sp);
			free(t->right);
			left = NULL;
			right = NULL;
		}
		else {
			if( t->left->ch_sp != NULL ) free(t->left->ch_sp);
			free(t->left);
			if( t->right != NULL ) {
				assign_node(t, t->right, DEALLOC);
				left = t->left;
				right = t->right;
			}
		}
	}
	else if( (right != NULL) && (is_leaf_node(right)) && right->sp_code == sp ) {
		if( t->right->ch_sp != NULL ) free(t->right->ch_sp);
		free(t->right);
		if( t->left != NULL ) {
			assign_node(t, t->left, DEALLOC);
			left = t->left;
			right = t->right;
		}
	}

	if( (left != NULL) && !is_leaf_node(left) ) delete_one_sp(left, sp);
	if( (right != NULL) && !is_leaf_node(right) ) delete_one_sp(right, sp);

	if( t->d_mode != LEAF && left == NULL && right == NULL ) {
		if( parent->left == t ) assign_node(parent, parent->right, DEALLOC);
		else if( parent->right == t) assign_node(parent, parent->left, DEALLOC);
		if( t->num_csp > 0 ) free(t->ch_sp);
		free(t);
	}
}
Beispiel #5
0
int is_leaf_bad (struct buffer_head * bh)
{
    int i;
    struct item_head * ih;
    int bad = 0;

    assert (is_leaf_node (bh));

    for (i = 0, ih = B_N_PITEM_HEAD (bh,  0); i < B_NR_ITEMS (bh); i ++, ih ++) {
	if (is_bad_item (bh, ih, B_I_PITEM (bh, ih))) {
	    fsck_log ("is_leaf_bad: block %lu: %d-th item (%H) is bad\n",
		      bh->b_blocknr, i, ih);
	    bad = 1;
	    continue;
	}

	if (i && bad_pair (fs, bh, i)) {
	    fsck_log ("is_leaf_bad: block %luL %d-th item (%H) and "
		      "the next one (%H) are in wrong order\n",
		     bh->b_blocknr, i - 1, ih - 1, ih);
	    bad = 1;
	}
    }

    return bad;
}
Beispiel #6
0
void write_bk_gname(struct p_tree *gt, struct g_list *genes)
{
	if( gt->left != NULL ) write_bk_gname(gt->left, genes);
	if( gt->right != NULL ) write_bk_gname(gt->right, genes);

	if( is_leaf_node(gt) ) {
		strcpy(gt->name, genes[gt->gid].gname);
	}
}
Beispiel #7
0
static void reiserfsck_check_cached_tree (int dev, int block, int size)
{
    struct buffer_head * bh;
    int what_node;

    bh =  find_buffer(dev, block, size);
    if (bh == 0)
	return;
    if (!buffer_uptodate (bh)) {
	die ("reiserfsck_check_cached_tree: found notuptodate buffer");
    }

    bh->b_count ++;
    
    if (!B_IS_IN_TREE (bh)) {
	die ("reiserfsck_check_cached_tree: buffer (%b %z) not in tree", bh, bh);
    }

    what_node = who_is_this (bh->b_data, bh->b_size);
    if ((what_node != THE_LEAF && what_node != THE_INTERNAL) ||
	!is_block_used (bh->b_blocknr) ||
	(is_leaf_node (bh) && is_leaf_bad (bh)) ||
	(is_internal_node(bh) && is_internal_bad (bh)))
	die ("reiserfsck_check_cached_tree: bad node in the tree");
    if (is_internal_node (bh)) {
	int i;
	struct disk_child * dc;
	
	dc = B_N_CHILD (bh, 0);
	for (i = 0; i <= B_NR_ITEMS (bh); i ++, dc ++) {
	    reiserfsck_check_cached_tree (dev, dc_block_number(dc), size);
	    g_dkey = B_N_PDELIM_KEY (bh, i);
	}
    } else if (is_leaf_node (bh)) {
	brelse (bh);
	return;
    } else {
	reiserfs_panic ("reiserfsck_check_cached_tree: block %lu has bad block type (%b)",
			bh->b_blocknr, bh);
    }
    brelse (bh);
}
Beispiel #8
0
/* only directory item can be fatally bad */
static int is_leaf_bad_xx (struct buffer_head * bh)
{
    int i;
    struct item_head * ih;

    if (!is_leaf_node (bh))
	return 0;
    for (i = 0, ih = B_N_PITEM_HEAD (bh,  0); i < B_NR_ITEMS (bh); i ++, ih ++)
	if (is_bad_item (bh, ih, B_I_PITEM (bh, ih))) {
	    return 1;
	}
    return 0;
}
Beispiel #9
0
int get_leaf_node(struct p_tree *sp, int dir)
{
	struct p_tree *temp;

	temp = sp;
	while( (!is_leaf_node(temp)) && (temp != NULL) ) {
		if( dir == RIGHT ) temp = temp->right;
		else if( dir == LEFT ) temp = temp->left; 
	}
	
	if( temp == NULL ) return(-1);		
	else return(temp->sp_code);
}
Beispiel #10
0
void mapping_tree(struct p_tree *gt, struct p_tree *sp)
{
	if( gt->left != NULL && gt->d_mode != DONE ) mapping_tree(gt->left, sp);
	if( gt->right != NULL && gt->d_mode != DONE ) mapping_tree(gt->right, sp);

	if( gt->d_mode == DONE ) {}
	else if( is_leaf_node(gt) == true ) {
		gt->d_mode = LEAF;
		gt->visited = false;
	}
	else { 
		gt->d_mode = assign_dmode(gt, sp);
		gt->visited = false;
	}
}
Beispiel #11
0
void get_deepest_sp(struct p_tree *sp, int *sp1, int *sp2, int *out_sp, int *cur_max) // sp1 is a left node and sp2 is a right node
{
	struct p_tree *temp;
	struct p_tree *par;

	if( sp->left != NULL ) get_deepest_sp(sp->left, sp1, sp2, out_sp, cur_max);
	if( sp->right != NULL ) get_deepest_sp(sp->right, sp1, sp2, out_sp, cur_max);

	if( is_leaf_node(sp) == false ) {
		if( is_leaf_node(sp->left) && is_leaf_node(sp->right) ) {
			if( (*cur_max) < sp->depth ) {
				*cur_max = sp->depth;
				temp = sp->left;
				*sp1 = temp->sp_code;
				temp = sp->right;
				*sp2 = temp->sp_code;	
				par = sp->parent;
				temp = par->left;
				if( temp->nid == sp->nid ) *out_sp = get_leaf_node(par->right, LEFT);
				else *out_sp = get_leaf_node(par->left, RIGHT);
			}
		}
	}
}
Beispiel #12
0
void print_tree(struct p_tree *t, int mode)
{
	int i = 0;
	if( !is_leaf_node(t) )
	{
		if( mode == TREE_PRINT ) printf("(");
		if( !is_leaf_node(t) ) print_tree(t->left, mode);
		if( mode == TREE_PRINT ) printf(",");
		if( !is_leaf_node(t) ) print_tree(t->right, mode);
		if( mode == TREE_PRINT ) {
			printf(")[%d:", t->nid);
			for(i = 0; i < t->num_csp; i++) {
				printf("%d", t->ch_sp[i]); 
				if( i < (t->num_csp-1) ) printf(",");
				else printf("]");
			}
		}
	}
//	else printf("%s", t->name);
	else {
		if( mode == TREE_PRINT ) {
			if( t->sp_code != -1 ) printf("%d", t->sp_code);
			else printf("%s", t->name);
		}
		else if( mode == INT_PRINT ) {
			if( t->sp_code != -1 ) {
				if( t->od == 0 ) printf("s %s%d + %d %d\n", t->name, t->sp_code, t->reg.lower, (t->reg.upper)-1); 
				else if( t->od == 1 ) printf("s %s%d - %d %d\n", t->name, t->sp_code, t->reg.lower, (t->reg.upper)-1); 
			}
			else {
				if( t->od == 0 ) printf("s %s + %d %d\n", t->name, t->reg.lower, (t->reg.upper)-1); 
				else if( t->od == 1 ) printf("s %s - %d %d\n", t->name, t->reg.lower, (t->reg.upper)-1); 
			}
		}
	}
}
Beispiel #13
0
void assign_sp_id(struct p_tree *t, struct sp_list *list, int depth, int num_sp)
{
	struct p_tree *temp = NULL;
	int count;
	int i;

	if( t->left != NULL ) assign_sp_id(t->left, list, depth+1, num_sp);
	if( t->right != NULL ) assign_sp_id(t->right, list, depth+1, num_sp);

	if( is_leaf_node(t) || is_done_node(t) ) {
			t->depth = depth;
			t->sp_code = get_id_in_list(t->name, list, num_sp);				
			t->num_csp = 1;
			t->ch_sp = (int *) ckalloc(sizeof(int));
			t->ch_sp[0] = t->sp_code; // the initialization of a species list
//			if( debug_mode == true)	printf("%d: %s %d\n", t->nid, t->name, t->sp_code);
	}
	else
	{
		t->depth = depth;
		temp = t->left;
		count = 0;
		if(  ( (t->left->num_csp) + (t->right->num_csp) ) < num_sp ) {
			t->ch_sp = (int *) ckalloc(((t->left->num_csp) + (t->right->num_csp)) * sizeof(int));
		}
		else {
			t->ch_sp = (int *) ckalloc(num_sp * sizeof(int));
		}

		for( i = 0; i < temp->num_csp; i++ ) {
			t->ch_sp[count] = temp->ch_sp[i];
			count++;
		}
		temp = t->right;
		for( i = 0; i < temp->num_csp; i++ ) {
			if( is_in_list(temp->ch_sp[i], t->ch_sp, count) == false ) {
				t->ch_sp[count] = temp->ch_sp[i];	
				count++;
			}
		}
		t->num_csp = count;
	}
}
void dump_graph_dot_format(GraphHandle graph, const char* filepath) {
  FILE* dot_file = fopen(filepath, "w");
  LOG_INFO("Dumping graph to %s", filepath);
  ASSERT(dot_file, "Failed to open file");

  fprintf(dot_file, "digraph {\n");
  fprintf(dot_file, "  node  [ nodesep=1.5 ];\n");
  fprintf(dot_file, "  graph [ overlap=false; bgcolor=\"grey\" ];\n");
  fprintf(dot_file, "  edge  [ weight=0.5 ];\n");

  for(uint32_t i=0; i<graph.vertex_count; ++i) {
    Node* node = (Node*)(graph.root + i);
    LOG_TRACE("Printing %u : %s", i, node->name);

    if (i == 0 || i == graph.vertex_count-1)
      fprintf_node_dot_format(dot_file, node, "cyan");
    else if (is_leaf_node(node))
      fprintf_node_dot_format(dot_file, node, "gold");
    else if (is_leaf_node_ignore_back(node))
      fprintf_node_dot_format(dot_file, node, "orange");
    else
      fprintf_node_dot_format(dot_file, node, NULL);

    for(uint32_t slot=0; slot < SLOT_COUNT; ++slot) {
      Node* child = follow_edge(node->slots[slot]);
      if (child)
        if (is_backwards(node->slots[slot]))
          fprintf_edge_dot_format(dot_file, node, child, "red");
        else if (get_flags_on_edge(node->slots[slot], SET_TRAVERSE_FLAG))
          fprintf_edge_dot_format(dot_file, node, child, "blue");
        else if (get_flags_on_edge(node->slots[slot], SET_VISIT_FLAG))
          fprintf_edge_dot_format(dot_file, node, child, "green4");
        else
          fprintf_edge_dot_format(dot_file, node, child, NULL);
      else if (is_backwards(node->slots[slot]))
        fprintf_edge_dot_format(dot_file, node, NULL, "red");
    }
  }

  fprintf(dot_file, "}\n");
  fclose(dot_file);
}
Beispiel #15
0
void assign_gid(struct p_tree *t, struct g_list *genes, int num_genes)
{
	int i;

	if( t->left != NULL ) assign_gid(t->left, genes, num_genes);
	if( t->right != NULL ) assign_gid(t->right, genes, num_genes);

	if( is_leaf_node(t) == true ) {
		i = 0;
		while( (i < num_genes) && (strcmp(t->name, genes[i].gname) != 0) ) i++;

		if( i >= num_genes ) {
			printf("%s is not in the gene list\n", t->name);
			exit(EXIT_FAILURE);
		}
		else t->gid = genes[i].gid;	
	}
	else {
		t->gid = -1;
	}
}
Beispiel #16
0
void extract_gtree_3sp(struct p_tree *t, int sp1, int sp2, int out_sp, FILE *f)
{
	if( (!is_leaf_node(t)) && ( is_3sp_in_list(sp1, sp2, out_sp, t->left) && is_3sp_in_list(sp1, sp2, out_sp, t->right) ) )
	{
		fprintf(f, "(");
		if( !is_leaf_node(t) ) extract_gtree_3sp(t->left, sp1, sp2, out_sp, f);
		fprintf(f, ",");
		if( !is_leaf_node(t) ) extract_gtree_3sp(t->right, sp1, sp2, out_sp, f);
		fprintf(f, ")");
	}
	else if( (!is_leaf_node(t)) && ( is_3sp_in_list(sp1, sp2, out_sp, t->left) ) )
	{
		extract_gtree_3sp(t->left, sp1, sp2, out_sp, f);
	}
	else if( (!is_leaf_node(t)) && ( is_3sp_in_list(sp1, sp2, out_sp, t->right) ) )
	{
		extract_gtree_3sp(t->right, sp1, sp2, out_sp, f);
	}
	else if( is_leaf_node(t) ) fprintf(f, "%s", t->name);
	else {}
}
Beispiel #17
0
void *ttree_delete_at_cursor(TtreeCursor *cursor) {
  Ttree *ttree = cursor->ttree;
  TtreeNode *tnode, *n;
  void *ret;

  TTREE_ASSERT(cursor->ttree != NULL);
  TTREE_ASSERT(cursor->state == CURSOR_OPENED);
  tnode = cursor->tnode;
  ret = ttree_key2item(ttree, tnode->keys[cursor->idx]);
  decrease_tnode_window(ttree, tnode, &cursor->idx);
  cursor->state = CURSOR_CLOSED;
  if (UNLIKELY(cursor->idx > tnode->max_idx)) {
    cursor->idx = tnode->max_idx;
  }

  /*
   * If after a key was removed, T*-tree node contains more than
   * minimum allowed number of items, the proccess is completed.
   */
  if (tnode_num_keys(tnode) > min_tnode_entries(ttree)) {
    return ret;
  }
  if (is_internal_node(tnode)) {
    int idx;

    /*
     * If it is an internal node, we have to recover number
     * of items from it by moving one item from its successor.
     */
    n = tnode->successor;
    idx = tnode->max_idx + 1;
    increase_tnode_window(ttree, tnode, &idx);
    tnode->keys[idx] = n->keys[n->min_idx++];
    if (UNLIKELY(cursor->idx > tnode->max_idx)) {
      cursor->idx = tnode->max_idx;
    }
    if (!tnode_is_empty(n) && is_leaf_node(n)) {
      return ret;
    }

    /*
     * If we're here, then successor is either a half-leaf
     * or an empty leaf.
     */
    tnode = n;
  }
  if (!is_leaf_node(tnode)) {
    int items, diff;

    n = tnode->left ? tnode->left : tnode->right;
    items = tnode_num_keys(n);

    /*
     * If half-leaf can not be merged with a leaf,
     * the proccess is completed.
     */
    if (items > (ttree->keys_per_tnode - tnode_num_keys(tnode))) {
      return ret;
    }

    if (tnode_get_side(n) == TNODE_RIGHT) {
      /*
       * Merge current node with its right leaf. Items from the leaf
       * are placed after the maximum item in a node.
       */
      diff = (ttree->keys_per_tnode - tnode->max_idx - items) - 1;
      if (diff < 0) {
        memcpy(tnode->keys + tnode->min_idx + diff, tnode->keys + tnode->min_idx,
            sizeof(void *) * tnode_num_keys(tnode));
        tnode->min_idx += diff;
        tnode->max_idx += diff;
        if (cursor->tnode == tnode) {
          cursor->idx += diff;
        }
      }
      memcpy(tnode->keys + tnode->max_idx + 1, n->keys + n->min_idx, sizeof(void *) * items);
      tnode->max_idx += items;
    }
    else {
      /*
       * Merge current node with its left leaf. Items the leaf
       * are placed before the minimum item in a node.
       */
      diff = tnode->min_idx - items;
      if (diff < 0) {
        register int i;

        for (i = tnode->max_idx; i >= tnode->min_idx; i--) {
          tnode->keys[i - diff] = tnode->keys[i];
        }

        tnode->min_idx -= diff;
        tnode->max_idx -= diff;
        if (cursor->tnode == tnode) {
          cursor->idx -= diff;
        }
      }

      memcpy(tnode->keys + tnode->min_idx - items, n->keys + n->min_idx, sizeof(void *) * items);
      tnode->min_idx -= items;
    }

    n->min_idx = 1;
    n->max_idx = 0;
    tnode = n;
  }
  if (!tnode_is_empty(tnode)) {
    return ret;
  }

  /* if we're here, then current node will be removed from the tree. */
  n = tnode->parent;
  if (!n) {
    ttree->root = NULL;
    free(tnode);
    return ret;
  }

  n->sides[tnode_get_side(tnode)] = NULL;
  fixup_after_deletion(ttree, tnode, NULL );
  free(tnode);
  return ret;
}
Beispiel #18
0
void page_node::insert(bt_key* key, RID rid, bt_key* itr) {
	if (is_leaf_node()) insert_to_leaf(key, rid, itr);
	else insert_to_index(key, rid, itr);
}
Beispiel #19
0
/* This function fills up the path from the root to the leaf as it
   descends the tree looking for the key.  It uses reiserfs_bread to
   try to find buffers in the cache given their block number.  If it
   does not find them in the cache it reads them from disk.  For each
   node search_by_key finds using reiserfs_bread it then uses
   bin_search to look through that node.  bin_search will find the
   position of the block_number of the next node if it is looking
   through an internal node.  If it is looking through a leaf node
   bin_search will find the position of the item which has key either
   equal to given key, or which is the maximal key less than the given
   key.  search_by_key returns a path that must be checked for the
   correctness of the top of the path but need not be checked for the
   correctness of the bottom of the path */
int search_by_key(
    struct super_block  * p_s_sb,         /* Super block.                           */
    struct key          * p_s_key,        /* Key to search.                         */
    struct path         * p_s_search_path,/* This structure was allocated and initialized by
					     the calling function. It is filled up by this
					     function.  */
    int                 * p_n_repeat,     /* Whether schedule occured. */
    int                   n_stop_level   /* How far down the tree to search.*/
    ) {
    dev_t                      n_dev           = p_s_sb->s_dev;
    int                         n_repeat,
	n_block_number  = SB_ROOT_BLOCK (p_s_sb),
	expected_level = SB_TREE_HEIGHT (p_s_sb),
	n_block_size    = p_s_sb->s_blocksize;
    struct buffer_head  *       p_s_bh;
    struct path_element *       p_s_last_element;
    int				n_retval;
    int 			right_neighbor_of_leaf_node;

#ifdef CONFIG_REISERFS_CHECK
    int n_repeat_counter = 0;
#endif

    /* As we add each node to a path we increase its count.  This
       means that we must be careful to release all nodes in a path
       before we either discard the path struct or re-use the path
       struct, as we do here. */
    pathrelse (p_s_search_path);

    *p_n_repeat = CARRY_ON;

    /* With each iteration of this loop we search through the items in
       the current node, and calculate the next current node(next path
       element) for the next iteration of this loop.. */
    while ( 1 ) {

#ifdef CONFIG_REISERFS_CHECK
	if ( !(++n_repeat_counter % 50000) )
	    printk ("PAP-5100: search_by_key(pid %u): there were %d searches from the tree_root lokking for key %p\n",
		    current->pid, n_repeat_counter, p_s_key);
#endif

	/* prep path to have another element added to it. */
	p_s_last_element = PATH_OFFSET_PELEMENT(p_s_search_path, ++p_s_search_path->path_length);
	expected_level --;
	n_repeat = CARRY_ON;

	/* Read the next tree node, and set the last element in the
           path to have a pointer to it. */
	if ( ! (p_s_bh = p_s_last_element->pe_buffer =
		reiserfs_bread(n_dev, n_block_number, n_block_size, &n_repeat)) ) {
	    p_s_search_path->path_length --;
	    pathrelse(p_s_search_path);
	    *p_n_repeat |= n_repeat;
	    return IO_ERROR;
	}

	*p_n_repeat |= n_repeat;

	/* It is possible that schedule occured. We must check whether
	   the key to search is still in the tree rooted from the
	   current buffer. If not then repeat search from the root. */
	if ( n_repeat != CARRY_ON && 
	     (!B_IS_IN_TREE (p_s_bh) || (! key_in_buffer(p_s_search_path, p_s_key, p_s_sb))) ) {
	    pathrelse (p_s_search_path);

	    /* Get the root block number so that we can repeat the
               search starting from the root. */
	    n_block_number  = SB_ROOT_BLOCK (p_s_sb);
	    expected_level = SB_TREE_HEIGHT (p_s_sb);
	    right_neighbor_of_leaf_node = 0;

	    /* repeat search from the root */
	    continue;
	}

#ifdef CONFIG_REISERFS_CHECK

	if ( ! key_in_buffer(p_s_search_path, p_s_key, p_s_sb) )
	    reiserfs_panic(p_s_sb, "PAP-5130: search_by_key: key is not in the buffer");
	if ( cur_tb ) {
/*	print_tb (init_mode, init_item_pos, init_pos_in_item, &init_tb, "5140");*/
	    reiserfs_panic(p_s_sb, "PAP-5140: search_by_key: schedule occurred in do_balance!");
	}

#endif

	// make sure, that the node contents look like a nod of
	// certain level
	if (!is_tree_node (p_s_bh, expected_level)) {
	    print_block (stderr, 0, p_s_bh, 3, -1, -1);
	    reiserfs_panic ("vs-5150: search_by_key: expeced level %d", expected_level);
	    pathrelse (p_s_search_path);
	    return IO_ERROR;
	}

	/* ok, we have acquired next formatted node in the tree */
	n_retval = bin_search (p_s_key, B_N_PITEM_HEAD(p_s_bh, 0), B_NR_ITEMS(p_s_bh),
			       is_leaf_node (p_s_bh) ? IH_SIZE : KEY_SIZE, &(p_s_last_element->pe_position));
	if (node_level (p_s_bh) == n_stop_level)
	    return n_retval;

	/* we are not in the stop level */
	if (n_retval == ITEM_FOUND)
	    /* item has been found, so we choose the pointer which is to the right of the found one */
	    p_s_last_element->pe_position++;
	/* if item was not found we choose the position which is to the left of the found item. This
	   requires no code, bin_search did it already.*/


	/* So we have chosen a position in the current node which is an
	   internal node.  Now we calculate child block number by position in the node. */
	n_block_number = B_N_CHILD_NUM(p_s_bh, p_s_last_element->pe_position);
    }
}
Beispiel #20
0
Datei: dary.c Projekt: tkng/dary
dary_status
dary_insert(dary *da, const uchar *key, int value)
{
   int i, n, m;
   n = key[0];
   dprint("key:%s", key);
   dprint("free_head:%d", da->free_head);

   // validate_array(da); 
   for (i = 1; key[i] != '\0';i++) {

      if (is_leaf_node(da, n)) {
         m = assign_base(da, n, key[i]);
      } else {    
         m = da->base[n] + key[i];
      }
      dprint("m = %d (base[%d](%d) + key[%d](%d))", m, n, da->base[n], i, key[i]);
    
      if (m >= da->length) {
         int retcode = extend_array(da, m + 256);
        
         if (retcode != DARY_STATUS_SUCCESS)
            return retcode;
      }
      
      if (is_free_node(da, m)) {
         dprint("%d is free node, insert", m);
         dprint("key[%d] = %d", i, key[i]);
         insert_node_to(da, m, n, key[i]);
      } else if (is_no_conflict(da, m, n, key[i])) {
         dprint("no conflict for key[%d]", i);
      } else {
         /* FIXME: Here should be added a check for which is easy to move. */

         if (should_move_m(da, m, n)) {
            dprint("move m");
            int retcode = move_conflicts(da, m, &n);
            
            if (retcode != DARY_STATUS_SUCCESS) {
               dprint("failed to move conflicted node %d\n",m);
               return retcode;
            }
            insert_node_to(da, m, n, key[i]);
         } else {
            dprint("move n, n=%d, base[n] = %d", n, da->base[n]);
            int retcode = move_conflicts2(da, n, key[i]);
            dprint("current n=%d, base[n] = %d", n, da->base[n]);
            if (retcode != DARY_STATUS_SUCCESS) {
               dprint("failed to move conflicted node %d\n",m);
               return retcode;
            }
            m = da->base[n] + key[i];
            insert_node_to(da, m, n, key[i]);

         }
      }
      //    fprintf(stderr, "\n");
      n = m;
   }
  
   da->values[n] = value;
   //  validate_array(da);
   //  dary_dump(da);
  
   return DARY_STATUS_SUCCESS;
}
Beispiel #21
0
/* recursive function processing all tree nodes */
static unsigned long move_formatted_block(unsigned long block, unsigned long bnd, int h)
{
	struct buffer_head * bh;
	struct item_head *ih;
	unsigned long new_blocknr = 0;
	int node_is_internal = 0;
	int i, j;
	
	bh = bread(fs->s_dev, block, fs->s_blocksize);
	
	if (is_leaf_node (bh)) {
		
		leaf_node_cnt++;
		
		for (i=0; i < B_NR_ITEMS(bh); i++) {
			ih = B_N_PITEM_HEAD(bh, i);
			if (is_indirect_ih(ih)) {
				__u32 * indirect;

				indirect = (__u32 *)B_I_PITEM (bh, ih);
				for (j = 0; j < I_UNFM_NUM(ih); j++) {
					unsigned long  unfm_block;

					if (indirect [j] == 0) /* hole */
						continue;
					unfm_block = move_unformatted_block(le32_to_cpu (indirect [j]), bnd, h + 1);
					if (unfm_block) {
						indirect [j] = cpu_to_le32 (unfm_block);
						mark_buffer_dirty(bh);
					}
				}
			}	
		}
	} else if (is_internal_node (bh)) { /* internal node */
		
		int_node_cnt++;
		node_is_internal = 1;
		
		for (i=0; i <= B_NR_ITEMS(bh); i++) {
			unsigned long moved_block;
			moved_block = move_formatted_block(B_N_CHILD_NUM(bh, i), bnd, h+1);
			if (moved_block) {
				set_child_block_number (bh, i, moved_block);
				mark_buffer_dirty(bh);
			}
		}	
	} else {
		die ("resize_reiserfs: block (%lu) has invalid format\n", block);
	}
	
	if (buffer_dirty(bh)) {
		mark_buffer_uptodate(bh,1);
		bwrite(bh);
	}
	
	brelse(bh);	
	
	new_blocknr = move_generic_block(block, bnd, h);
	if (new_blocknr) {
		if (node_is_internal)
			int_moved_cnt++;
		else
			leaf_moved_cnt++;
	}
	
	return new_blocknr;
}
Beispiel #22
0
int page_node::findHalf(bt_key* key, RID rid,int &flag, bt_key *itr){
	if (is_leaf_node()) return find_leaf_Half(key,rid,flag, itr);
	else return find_index_Half(key, flag, itr);
}