From dce3cb2a9e6b01044af0edd7b1b5bfbfddddcc04 Mon Sep 17 00:00:00 2001 From: Dwight Date: Mon, 25 Feb 2008 18:22:10 -0500 Subject: [PATCH] unindex fix --- db/btree.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++++------ db/btree.h | 2 +- db/db.cpp | 2 +- db/pdfile.cpp | 1 + 4 files changed, 67 insertions(+), 10 deletions(-) diff --git a/db/btree.cpp b/db/btree.cpp index 031580facb6..08019142ab6 100644 --- a/db/btree.cpp +++ b/db/btree.cpp @@ -217,16 +217,18 @@ void BtreeBucket::findLargestKey(const DiskLoc& thisLoc, DiskLoc& largestLoc, in DiskLoc loc = thisLoc; while( 1 ) { BtreeBucket *b = loc.btree(); +// b->dump(); if( !b->nextChild.isNull() ) { loc = b->nextChild; continue; } + + assert(b->n>0); + largestLoc = loc; + largestKey = b->n-1; + break; } - - assert(n>0); - largestLoc = loc; - largestKey = n-1; } /* pos: for existing keys k0...kn-1. @@ -341,6 +343,10 @@ int qqq = 0; bool BtreeBucket::unindex(const DiskLoc& thisLoc, const char *ns, JSObj& key, const DiskLoc& recordLoc ) { // cout << "unindex " << key.toString() << endl; + if( qqq ) { + fullValidate(thisLoc); + } + BtreeCursor c(thisLoc, key, 1, true); while( c.ok() ) { @@ -356,6 +362,10 @@ bool BtreeBucket::unindex(const DiskLoc& thisLoc, const char *ns, JSObj& key, co c.advance(); } + if( qqq ) { + fullValidate(thisLoc); + } + return false; } @@ -534,10 +544,15 @@ void BtreeBucket::insertHere(DiskLoc thisLoc, const char *ns, int keypos, // add our new key, there is room now { - if( keypos < mid ) { + +//dump(); + + if( keypos <= mid ) { +// if( keypos < mid ) { if( split_debug ) cout << " keypos=0); @@ -578,6 +593,7 @@ DiskLoc BtreeBucket::advance(const DiskLoc& thisLoc, int& keyOfs, int direction, int ko = keyOfs + direction; DiskLoc nextDown = childForPos(ko+adj); if( !nextDown.isNull() ) { +// nextDown.btree()->dump();//TEMP: while( 1 ) { keyOfs = direction>0 ? 0 : nextDown.btree()->n - 1; DiskLoc loc= nextDown.btree()->childForPos(keyOfs + adj); @@ -618,6 +634,7 @@ DiskLoc BtreeBucket::advance(const DiskLoc& thisLoc, int& keyOfs, int direction, DiskLoc BtreeBucket::locate(const DiskLoc& thisLoc, JSObj& key, int& pos, bool& found, int direction) { int p; found = find(key, p); +// dump(); if( found ) { /* todo: slow? this can be avoided for indexes that don't allow dup keys. add support for that. */ { @@ -663,7 +680,7 @@ int BtreeBucket::_insert(DiskLoc thisLoc, const char *ns, DiskLoc recordLoc, return 2; } assert( key.objsize() > 0 ); - +//dump(); int pos; bool found = find(key, pos); if( insert_debug ) { @@ -713,10 +730,41 @@ void BtreeBucket::dump() { cout << " right:" << hex << nextChild.getOfs() << dec << endl; } +JSObj *music = 0; + +void tempMusic(DiskLoc thisLoc) +{ + BtreeCursor c(thisLoc, *music, 1, true); + while( c.ok() ) { + KeyNode kn = c.currKeyNode(); + if( !kn.key.woEqual(*music) ) + break; + if( kn.recordLoc.getOfs() == 0x4c8d7c0 ) { + cout << "*** found it" << endl; + // c.bucket.btree()->dump(); + return; + } + c.advance(); + } + + cout << "*** NOT FOUND" << endl; +} + int BtreeBucket::insert(DiskLoc thisLoc, const char *ns, DiskLoc recordLoc, JSObj& key, bool dupsAllowed, IndexDetails& idx, bool toplevel) { if( toplevel ) { +/* if( key.toString() == "{ _searchIndex: \"music\" }" ) { + if( music == 0 ) { + music = new JSObj(key.copy()); + cout << music->toString() << endl; + } + tempMusic(thisLoc); + if( recordLoc.getOfs() == 0x4c8d7c0 ) { + cout << "About to add it!" << endl; + } + } +*/ ++ninserts; if( /*ninserts > 127250 || */ninserts % 1000 == 0 ) { cout << "ninserts: " << ninserts << endl; @@ -727,14 +775,14 @@ int BtreeBucket::insert(DiskLoc thisLoc, const char *ns, DiskLoc recordLoc, //bt_fv = 1; //idx.head.btree()->fullValidate(idx.head); } - if( 0 && ninserts >= 127287 ) { +/* if( 0 && ninserts >= 127287 ) { cout << "---------------------------------------------------------------- " << ninserts << endl; cout << "insert() top level " << key.toString() << ' ' << recordLoc.toString() << endl; bt_fv = 1; split_debug = 1; insert_debug = 1; idx.head.btree()->fullValidate(idx.head); - } + }*/ } bool chk = false; @@ -742,6 +790,14 @@ int BtreeBucket::insert(DiskLoc thisLoc, const char *ns, DiskLoc recordLoc, int x = _insert(thisLoc, ns, recordLoc, key, dupsAllowed, DiskLoc(), DiskLoc(), idx); assertValid(); +/* if( toplevel ) { + if( recordLoc.getOfs() == 0x4c8d7c0 ) { + if( key.toString() == "{ _searchIndex: \"music\" }" ) { + tempMusic(thisLoc); + } + } + } +*/ return x; } diff --git a/db/btree.h b/db/btree.h index d7d8bdcb7ed..94d3f041faf 100644 --- a/db/btree.h +++ b/db/btree.h @@ -140,7 +140,7 @@ private: JSObj& key, bool dupsAllowed, DiskLoc lChild, DiskLoc rChild, IndexDetails&); bool find(JSObj& key, int& pos); - void findLargestKey(const DiskLoc& thisLoc, DiskLoc& largestLoc, int& largestKey); + static void findLargestKey(const DiskLoc& thisLoc, DiskLoc& largestLoc, int& largestKey); }; class BtreeCursor : public Cursor { diff --git a/db/db.cpp b/db/db.cpp index b1000f02945..74ccf127d2b 100644 --- a/db/db.cpp +++ b/db/db.cpp @@ -272,7 +272,7 @@ public: }; void listen(int port) { - cout << "db version: 24feb08.2 embedded objects indexable; catch query asserts; dup keys; _alloc" << endl; + cout << "db version: 25feb08.1 unindex fixes" << endl; pdfileInit(); testTheDb(); cout << curTimeMillis() % 10000 << " waiting for connections...\n" << endl; diff --git a/db/pdfile.cpp b/db/pdfile.cpp index c5120e6d126..5a5777589b8 100644 --- a/db/pdfile.cpp +++ b/db/pdfile.cpp @@ -414,6 +414,7 @@ void _unindexRecord(const char *ns, IndexDetails& id, JSObj& obj, const DiskLoc& cout << " obj:" << obj.toString() << '\n'; cout << " key:" << j.toString() << '\n'; cout << " dl:" << dl.toString() << endl; + //id.head.btree()->unindex(id.head, ns, j, dl); } } }