int insert_nonfull(BTreeNode* x,char* key,struct DB* db) { unsigned long i = x->nKeys; if(x->leaf) { while(i >= 0 && keys_compare(x,key,i) < 0) { keys_copy(x,i+1,i); i--; } keys_copy(x,i+1,key); x->nKeys++; return disk_write_node(db,x->page,x); } else { while(i >= 0 && keys_compare(x,key,i) < 0) i--; i = i + 1; BTreeNode c_i; disk_read_node(db,x->chld[i],&c_i); if(c_i.nKeys == (2*BTREE_KEY_CNT-1)) { split_child(x,i,db); if(keys_compare(x,key,i) > 0)i++; } insert_nonfull(&c_i,key,db); } }
int insert_key(BTreeNode* head,char* key,struct DB* db) { BTreeNode* root = head; if(root->nKeys == (2*BTREE_KEY_CNT-1)) { BTreeNode s; s.page = db->db_all->db_alloc(); *head = s; s.leaf = 0; s.nKeys = 0; s.keys[0] = root->page; split_child(&s,1,db); insert_nonfull(&s,key,db); } else { insert_nonfull(root,key,db); } }
void insert_nonfull(Btree_node *x, DBT *key, DBT *value) { int i = x->n - 1; if (x->is_leaf) { while (i >= 0 && memcmp(key, x->keys + x->offset_key[i], min(key->size, x->offset_key[i + 1] - x->offset_key[i] == -1))) { x->offset_key[i + 1] = x->offset_key[i] + key->size; x->offset_data[i + 1] = x->offset_data[i] + value->size; i--; } if (i + 2 != x->n - 1) { memmove(x->keys + x->offset_key[i + 2], x->keys + x->offset_key[i + 1], x->offset_key[x->n + 1] - x->offset_key[i + 2]); memmove(x->data + x->offset_data[i + 2], x->data + x->offset_data[i + 1], x->offset_data[x->n + 1] - x->offset_data[i + 2]); } x->n++; write_to_file(x); } else { while (i >= 0 && memcmp(key, x->keys + x->offset_key[i], min(key->size, x->offset_key[i + 1] - x->offset_key[i])) == -1) { i--; } i++; Btree_node *n = malloc(sizeof(*n)); n->Id = x->childs_id[i]; n->tree = x->tree; read_from_file(n); if (n->n == 2 * t - 1) { split_child(x, n, i); if(memcmp(key, x->keys + x->offset_key[i], min(key->size, x->offset_key[i + 1] - x->offset_key[i])) == 1) i++; } insert_nonfull(n, key, value); } }
void CmiNode::delete_child(CmiNode* cNode) { split_child(cNode); CmiNode::deleteNode(cNode); }