bool MojDbIsamQuery::match() { // compare against lower key when descending, upper otherwise bool desc = m_plan->desc(); const ByteVec& key = m_iter->key(!desc).byteVec(); if (key.empty()) return true; // test for >= when descending, < otherwise int comp = compareKey(key); return (comp >= 0) == desc; }
// Search for a given key in the ordered binary tree t. Value* search(Table t, Key key) { if (t == 0) return 0; int cmp = compareKey(key, t->key); if (cmp == -1) return search(t->left, key); if (cmp == 1) return search(t->right, key); // Now the only possibility that is left is cmp == 0, // that is key == t->key. return &(t->val); }
template<class R, class N, class K, int n> N* TreeInRoot<R, N, K, n>::find(K key) const { N* node = base(); while(node) { int cmp = compareKey(*node, key); if(cmp == 0) break; // Found the node, return it if(cmp < 0) { node = static_cast<Node*>(node)->left_; } else { node = static_cast<Node*>(node)->right_; } } return node; }
int segmentFix(Segment* segment,const Block* block,const Slice* key) { uint32_t low,mid,high; int result = -1; Slice midkey; initSlice(&midkey,20); low = 0; high = block->restartNum-1; while(low < high){ mid = low + high; mid = mid >> 1; printf("slow=%u,high=%u,mid=%u\n",low,high,mid); getBlockEntryKey(block->data_+block->restart_[mid],&midkey); result = compareKey(midkey,*key); if(result == 1){ high = mid; }else if(result == 2){ low = mid+1; mid = low; }else{ high = mid; low = mid; } } if(mid == 0){ printf("Waring: the first key of the block is bigger than key.\n"); } if(mid != 0){ mid--; } if(mid == (block->restartNum-1)){ segment->start_ = block->restart_[mid]; segment->size_ = block->restart_offset - block->restart_[mid]; }else{ segment->start_ = block->restart_[mid]; segment->size_ = block->restart_[mid+1] - block->restart_[mid]; } printf("Fixed finished!\nmid = %u,start_ = %u,size_ = %u\n",mid,segment->start_,segment->size_); midkey.size_ = 0; getBlockEntryKey(block->data_+segment->start_,&midkey); showKey(&midkey); printf("FIXFIX\n"); freeSlice(&midkey); return 1; }
/* 成功则返回在segment中的偏移量,失败则返回1,应为offset不可能等于1 */ uint32_t scanSegment(const Block* block,const Segment* segment,DBhandle* dbhandle) { Slice lastKey; BlockEntry blockEntry; size_t tmp; size_t result; uint32_t offset = 0; unsigned char* b = block->data_ + segment->start_; printf("scanSegment:start_ = %u\n",segment->start_); initSlice(&lastKey,20); lastKey.size_ = 0; initBlockEntry(&blockEntry); while(offset < segment->size_){ tmp = decodeBlockEntry(&blockEntry,b+offset,&lastKey); //showBlockEntry(&blockEntry); printf("keysize = %zd,valuesize = %zd\n",blockEntry.key_.size_,blockEntry.value_.size_); //break; //showBlockEntry(&blockEntry); //showIndexBlockEntry(&blockEntry); result = compareKey(blockEntry.key_,dbhandle->key_); if(0 == result){ cpySlice(&dbhandle->value_,&blockEntry.value_); freeSlice(&lastKey); freeBlockEntry(&blockEntry); return offset; }else if(1 == result){ printf("Waring:Not Found!\n"); freeSlice(&lastKey); freeBlockEntry(&blockEntry); return 1; } offset += tmp; } freeSlice(&lastKey); freeBlockEntry(&blockEntry); return 1; }
// A constructor for list nodes. Table insert(Table t, Key key, Value val) { if (t == 0) { BinTree nodePtr = malloc( sizeof(struct BinNode) ); nodePtr->key = key; nodePtr->val = val; nodePtr->left = 0; nodePtr->right = 0; return nodePtr; } int cmp = compareKey(key, t->key); if (cmp == -1) { t->left = insert(t->left, key, val); } else if (cmp == 1) { t->right = insert(t->right, key, val); } else { t->val = val; } assert( ordered(t) ); return t; }
MojErr MojDbIsamQuery::seek(bool& foundOut) { MojAssert(m_state == StateSeek); // if descending, seek to upper bound, lower otherwise bool desc = m_plan->desc(); const ByteVec& key = m_iter->key(desc).byteVec(); m_state = StateNext; // if the last key returned while iterating over the previous range is >= // our first key, we can use our current position instead of seeking if (m_keySize > 0) { if ((compareKey(key) < 0) == desc) { foundOut = true; return MojErrNone; } } MojErr err = seekImpl(key, desc, foundOut); MojErrCheck(err); return MojErrNone; }
inline int compareNode(const BasicPtree & other) const{ return (compareKey(other) && compareData(other)); }
return &(t->val); } BinNodePtr findRightSucc(Table t) { if (t->left == 0) return t; return findRightSucc(t->left); } // Delete a given key from the table t. We assume that the key appears // in t. Table delete(Table t, Key key) { assert(t != 0); int cmp = compareKey(key, t->key); if (cmp == -1) t->left = delete(t->left, key); if (cmp == 1) t->right = delete(t->right, key); if (cmp != 0) return t; // If we ever get here, we must have key == t->key. if (t->left == 0 && t->right == 0) { free(t); return 0; } if (t->left == 0) { Table res = t->right; free(t); return res;