BTRFS: Added binary search for item slot

Signed-off-by: Adrien Destugues <pulkomandy@pulkomandy.tk>
This commit is contained in:
hyche 2017-06-18 22:44:08 +07:00 committed by Adrien Destugues
parent bd2dab1c73
commit 91d7f850cf
2 changed files with 35 additions and 0 deletions

View File

@ -91,6 +91,39 @@ BNode::SetToWritable(off_t block, int32 transactionId, bool empty)
}
int32
BNode::SearchSlot(const btrfs_key& key, int* slot) const
{
//binary search for item slot in a node
int entrySize = sizeof(btrfs_entry);
if (Level() != 0) {
// internal node
entrySize = sizeof(btrfs_index);
}
int low = 0;
int high = ItemCount();
int mid;
int base = sizeof(btrfs_header);
const btrfs_key* other;
while (low < high) {
mid = (low + high) / 2;
other = (const btrfs_key*)((uint8*)fNode + base + mid * entrySize);
int comp = key.Compare(*other);
if (comp < 0)
high = mid;
else if (comp > 0)
low = mid + 1;
else {
*slot = mid;
return B_OK;
}
}
*slot = low;
return B_ENTRY_NOT_FOUND;
}
//-pragma mark

View File

@ -79,6 +79,8 @@ public:
off_t BlockNumber() const { return fBlockNumber; }
bool IsWritable() const { return fWritable; }
int32 SearchSlot(const btrfs_key& key, int* slot) const;
private:
BNode(const BNode&);
BNode& operator=(const BNode&);