diff --git a/src/sys/ubixfsv2/btree.cpp b/src/sys/ubixfsv2/btree.cpp index a2e4185..0285599 100644 --- a/src/sys/ubixfsv2/btree.cpp +++ b/src/sys/ubixfsv2/btree.cpp @@ -16,7 +16,7 @@ root->leafNode = true; root->childCount[1] = 1; -cout << "---Creating " << inode->name << "@" << inode << endl; +// cout << "---Creating " << inode->name << "@" << inode << endl; strncpy(root->keys[0], inode->name, B_MAX_NAME_LENGTH); // insert pointer to data page to the right of the data root->childHead[1] = (void *)inode; @@ -36,12 +36,13 @@ unsigned int curSlot = 0; if (bnode == NULL || inode == NULL) return false; - + tmpInode = Find(inode->name); + if (tmpInode != NULL) cout << tmpInode->name << " " << inode->name << endl; /* * Find the leaf node the inode goes into */ assert(bnode->used); - cout << "---Inserting " << inode->name << "@" << inode << endl; +// cout << "---Inserting " << inode->name << "@" << inode << endl; while (bnode != NULL && !bnode->leafNode) { if (strcmp(inode->name, bnode->keys[0]) < 0) bnode = (bNode *)bnode->childHead[0]; @@ -65,11 +66,6 @@ if (bnode->leafNode != true) cout << "leafnode!=true" << endl; assert(inode); -cout << inode->name << " ~~ " << bnode->keys[0] << " == "; -cout << strcmp(inode->name, bnode->keys[0]) << endl; -cout << inode->name << " ~~ " << bnode->keys[bnode->used-1] << " == "; -cout << strcmp(inode->name, bnode->keys[bnode->used]) << endl; - if (strcmp(inode->name, bnode->keys[curSlot = 0]) < 0) tmpInode = (ubixfsInode *)bnode->childHead[curSlot]; else @@ -151,7 +147,7 @@ // Info(); if (curSlot != bnode->used) { int shift = bnode->used - curSlot +1; -cout << "shift: " << shift << endl; +// cout << "shift: " << shift << endl; memmove(&bnode->childHead[curSlot+1], &bnode->childHead[curSlot], sizeof(bnode->childHead[0]) * shift); @@ -187,11 +183,11 @@ bnode->childTail[curSlot] = (void *)tmpInode->prev; bnode->childCount[curSlot] = (B_MAX_CHILD_COUNT+1) >> 1; bnode->childCount[curSlot+1] -= bnode->childCount[curSlot]; -cout << "childCount[" << curSlot << "] : " << bnode->childCount[curSlot] << endl; -cout << "childCount[" << curSlot+1 << "] : " << bnode->childCount[curSlot+1] << endl; +// cout << "childCount[" << curSlot << "] : " << bnode->childCount[curSlot] << endl; +// cout << "childCount[" << curSlot+1 << "] : " << bnode->childCount[curSlot+1] << endl; bnode->present[curSlot] = true; if (++bnode->used == B_MAX_KEYS) splitNode(bnode); -cout << "--- After split ---" << endl; +// cout << "--- After split ---" << endl; // Info(); } // if leaf is full @@ -200,7 +196,7 @@ void bTree::splitNode(bNode * oldNode) { -cout << "---in splitNode()---" << endl; +// cout << "---in splitNode()---" << endl; assert(oldNode); if (oldNode == NULL) return; if (oldNode->used != B_MAX_KEYS) return; @@ -211,7 +207,7 @@ unsigned int splitLoc = (B_MAX_KEYS+1) >> 1; unsigned int shift = B_MAX_KEYS - splitLoc; -cout << "splitLoc: " << splitLoc << " shift: " << shift << endl; +// cout << "splitLoc: " << splitLoc << " shift: " << shift << endl; newNode->used = shift; newNode->parent = oldNode->parent; newNode->leafNode = oldNode->leafNode; @@ -264,26 +260,75 @@ root->present[0] = root->present[1] = true; root->childCount[0] = oldNode->used; root->childCount[1] = newNode->used; -cout << "---root node begin---" << endl; - Info(); -cout << "---root node end---" << endl; +//cout << "---root node begin---" << endl; +// Info(); +//cout << "---root node end---" << endl; } else { - if (++oldNode->parent->used == B_MAX_KEYS) splitNode(oldNode->parent); + insertNode(oldNode->parent, newNode->keys[0], newNode, NULL); + if (oldNode->parent->used == B_MAX_KEYS) splitNode(oldNode->parent); } // else -cout << "---oldNode begin---" << endl; - Info(oldNode); -cout << "---oldNode end---" << endl; - -cout << "---newNode---" << endl; - Info(newNode); +// cout << "---oldNode begin---" << endl; +// Info(oldNode); +// cout << "---oldNode end---" << endl; +// cout << "---newNode---" << endl; +// Info(newNode); return; } // bTree::splitNode void -bTree::insertNode(bNode * node, const char * key, void * data) { - if (node == NULL || key == NULL || data == NULL) return; +bTree::insertNode(bNode * node, const char * key, + void * headPtr, void * tailPtr) { unsigned int curSlot = 0; - + if (node == NULL || key == NULL) return; + + if (strcmp(key, node->keys[node->used-1]) >= 0){ + curSlot = node->used; + memset(node->keys[curSlot], 0, B_MAX_NAME_LENGTH); + strncpy(node->keys[curSlot], key, B_MAX_NAME_LENGTH); + node->childHead[curSlot+1] = headPtr; + node->childTail[curSlot+1] = tailPtr; + node->present[curSlot+1] = true; +// node->childCount[node->used] = ?; + + } else { + + for (curSlot = 0; curSlot <= node->used; curSlot++) { + if (strcmp(key, node->keys[curSlot]) < 0) break; + } // for + + /* + * note that there is one more item for everything but keys + * So, make the shift count +1 and just subtract it from the key shift + * later + */ + int shift = node->used - curSlot +1; + + memmove(&node->childHead[curSlot+1], + &node->childHead[curSlot], + sizeof(node->childHead[0]) * shift); + memmove(&node->childTail[curSlot+1], + &node->childTail[curSlot], + sizeof(node->childTail[0]) * shift); + memmove(&node->present[curSlot+1], + &node->present[curSlot], + sizeof(node->present[0]) * shift); + memmove(&node->childCount[curSlot+1], + &node->childCount[curSlot], + sizeof(node->childCount[0]) * shift); + + memmove(&node->keys[curSlot+1], + &node->keys[curSlot], + sizeof(node->keys[0]) * (shift-1)); + + memset(node->keys[curSlot], 0, B_MAX_NAME_LENGTH); + strncpy(node->keys[curSlot], key, B_MAX_NAME_LENGTH); + node->childHead[curSlot+1] = headPtr; + node->childTail[curSlot+1] = tailPtr; + node->present[curSlot+1] = true; +// node->childCount[node->used] = ?; + } // else + ++node->used; + return; } // bTree::insertNode bNode * @@ -374,19 +419,23 @@ bTree::inodeSearch(ubixfsInode * inode, const char * key) { if (inode == NULL || key == NULL) return NULL; int result = strcmp(inode->name, key); +cout << "inode->name: " << inode->name << " key: " << key << " 1result: " << result << endl; if (result == 0) return inode; if (result < 0) { inode = inode->next; - while (inode != NULL && (result = strcmp(inode->name, key) >= 0)) { + while (inode != NULL && ((result = strcmp(key, inode->name)) <= 0)) { +cout << "inode->name: " << inode->name << " key: " << key << " 2result: " << result << endl; inode = inode->next; } // while } else { inode = inode->prev; - while (inode != NULL && (result = strcmp(inode->name, key) <= 0)) { + while (inode != NULL && ((result = strcmp(key, inode->name)) >= 0)) { +cout << "inode->name: " << inode->name << " key: " << key << " 3result: " << result << endl; inode = inode->prev; } // while } // else + return (result == 0 ? inode : NULL); } // bTree::inodeSearch @@ -402,7 +451,7 @@ else return inodeSearch((ubixfsInode *)bnode->childHead[1], key); -cout << "key: " << key << " keys[0]: " << bnode->keys[0] << " result: " << strcmp(key, bnode->keys[0]) << endl; +// cout << "key: " << key << " keys[0]: " << bnode->keys[0] << " result: " << strcmp(key, bnode->keys[0]) << endl; if (strcmp(key, bnode->keys[0]) < 0) return treeSearch((bNode *)bnode->childHead[0], key); diff --git a/src/sys/ubixfsv2/btree.h b/src/sys/ubixfsv2/btree.h index 1e487dd..2348991 100644 --- a/src/sys/ubixfsv2/btree.h +++ b/src/sys/ubixfsv2/btree.h @@ -31,7 +31,7 @@ ubixfsInode * inodeSearch(ubixfsInode *, const char *); void splitNode(bNode *); bNode * allocEmptyNode(void); - void insertNode(bNode *, const char *, void *); + void insertNode(bNode *, const char *, void *, void *); public: bTree(ubixfsInode *); bTree(void) : root(NULL) { }; diff --git a/src/sys/ubixfsv2/main.cpp b/src/sys/ubixfsv2/main.cpp index 57265c2..bb34d81 100644 --- a/src/sys/ubixfsv2/main.cpp +++ b/src/sys/ubixfsv2/main.cpp @@ -13,16 +13,19 @@ int i = 0; ubixfsInode * inode = (ubixfsInode *)malloc(sizeof(ubixfsInode)); memset(inode, 0, sizeof(ubixfsInode)); - strcpy(inode -> name, "h"); + strcpy(inode -> name, "temp123"); bTree * tree = new bTree(inode); - for (i = 0; i < 100; i++) { + for (i = 0; i < 2; i++) { + // if (i%1000 == 0) + cout << "-_- i = "<name[k] = (char)((random() % 26)+'a'); } // for k - tree->Insert(inode); + if (!tree->Insert(inode)) cout << "Insert() failed" << endl; } // for i #if 0 inode = (ubixfsInode *)malloc(sizeof(ubixfsInode)); @@ -86,15 +89,21 @@ cout << "---Inserting " << inode->name << "---" << endl; tree->Insert(inode); #endif + i = 0; - ubixfsInode * tmpInode = tree->GetFirstNode(); + ubixfsInode * tmpInode = tree->Find("temp123"); + if (tmpInode != NULL) cout << "found: " << tmpInode->name << endl; +#if 0 + tmpInode = tree->GetFirstNode(); if (tmpInode == NULL) cout << "GetFirstNode() returns null" << endl; while (tmpInode != NULL) { cout << "node[" << i++ << "]: " << tmpInode->name << endl; tmpInode = tmpInode->next; } // while +#endif 0 cout << sizeof(struct bNode) << endl; +// tree->Info(); free(inode); return 0; }