void BTree::addToNode(BTreeNode *node, BTreeElement *child) { if (!node->addChild(child->getMinId(), child)) { //__android_log_print(ANDROID_LOG_INFO, "Jello", "Node is full"); BTreeNode *newNode = new BTreeNode(this, nodeCapacity); int oldMinId = node->getMinId(); node->split(newNode); BTreeNode *addTo; if (node->getMinId() == -1 || child->getMinId() > node->getMinId()) addTo = node; else addTo = newNode; if (!addTo->addChild(child->getMinId(), child)) { // //__android_log_print(ANDROID_LOG_INFO, "Jello", "Node add FAILED!"); return; } if (node->getParent() == NULL) { // //__android_log_print(ANDROID_LOG_INFO, "Jello", "This node's parent is null"); BTreeNode *parent = new BTreeNode(this, nodeCapacity); parent->addChild(node->getMinId(), node); root = parent; } addToNode(node->getParent(), newNode); } //__android_log_print(ANDROID_LOG_INFO, "Jello", "Added new node to parent"); }
void test_save_open_node() { BTreeFS fs("test_btree_fs.dat", 4); BTreeNode node = fs.allocNode(true); node.setSentinel(rand()); node.setKeysNum(4); node.addChild(23, rand()); node.addChild(48, rand()); node.addChild(236, rand()); node.addChild(326, rand()); fs.saveNode(node); BTreeNode saved_node = fs.openNode(node.ref()); assert(node == saved_node); }
void BTree::add(int id, RecordInfo *record) { //__android_log_print(ANDROID_LOG_INFO, "Jello", "== BTree add id: %d", id); if (root == NULL) { //__android_log_print(ANDROID_LOG_INFO, "Jello", "new leaf as root: %d", leafCapacity); root = new BTreeLeaf(this, leafCapacity); } BTreeElement *e = root; while (e->type == BTreeElement::ELEMENT_NODE) { e = ((BTreeNode*)e)->getSubNodeFor(id); if (e == NULL) return; } BTreeLeaf *leaf = (BTreeLeaf*)e; if (!leaf->add(id, record)) { //__android_log_print(ANDROID_LOG_INFO, "Jello", "Not enough space in leaf"); BTreeLeaf *newLeaf = new BTreeLeaf(this, leafCapacity); int oldMinId = leaf->getMinId(); //__android_log_print(ANDROID_LOG_INFO, "Jello", "Splitting"); leaf->split(newLeaf); BTreeLeaf *addTo; if (leaf->getMinId() == -1 || id > leaf->getMinId()) addTo = leaf; else addTo = newLeaf; if (!addTo->add(id, record)) { //__android_log_print(ANDROID_LOG_INFO, "Jello", "Leaf add FAILED!"); return; } if (leaf->getParent() == NULL) { //__android_log_print(ANDROID_LOG_INFO, "Jello", "This leaf's parent is NULL, creating new node"); BTreeNode *node = new BTreeNode(this, nodeCapacity); node->addChild(leaf->getMinId(), leaf); root = node; } //__android_log_print(ANDROID_LOG_INFO, "Jello", "Adding new node to parent"); addToNode(leaf->getParent(), newLeaf); } }
void BTree::update(int id, RecordInfo *record) { //__android_log_print(ANDROID_LOG_INFO, "Jello", "Update: %d", id); BTreeElement *e = root; if (e == NULL) return; while (e->type == BTreeElement::ELEMENT_NODE) { e = ((BTreeNode*)e)->getSubNodeFor(id); if (e == NULL) return; } BTreeLeaf *leaf = (BTreeLeaf*)e; if (!leaf->update(id, record)) { //__android_log_print(ANDROID_LOG_INFO, "Jello", "update failed, splitting node"); BTreeLeaf *newLeaf = new BTreeLeaf(this, leafCapacity); int oldMinId = leaf->getMinId(); leaf->split(newLeaf); if (id < leaf->getMinId()) { if (!newLeaf->update(id, record)) { //__android_log_print(ANDROID_LOG_INFO, "Jello", "Leaf add FAILED!"); return; } } else { if (!leaf->update(id, record)) { //__android_log_print(ANDROID_LOG_INFO, "Jello", "Leaf add FAILED!"); return; } } if (leaf->getParent() == NULL) { BTreeNode *node = new BTreeNode(this, nodeCapacity); node->addChild(leaf->getMinId(), leaf); } addToNode(leaf->getParent(), newLeaf); } else mergeNode(leaf); }