Skip to content

Commit

Permalink
feat: introduce valid check into iterator (#110)
Browse files Browse the repository at this point in the history
Signed-off-by: Jian Zhang <[email protected]>
  • Loading branch information
zz-jason authored Aug 13, 2024
1 parent 2cc6019 commit 495265f
Show file tree
Hide file tree
Showing 14 changed files with 324 additions and 192 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ QualifierOrder: ['inline', 'static', 'const', 'volatile', 'type']
IncludeCategories:
- Regex: '^".*.hpp'
Priority: 2
- Regex: '^<leanstore/*'
- Regex: '^("|<)leanstore/*'
Priority: 2
- Regex: '^("|<)(benchmark)/'
Priority: 3
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/shared/RocksDBAdapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ struct RocksDBAdapter : public Adapter<Record> {
Fold(folded_key, Record::id) + Record::foldKey(folded_key + sizeof(SEP), key);
// -------------------------------------------------------------------------------------
rocksdb::Iterator* it = map.db->NewIterator(map.ro);
for (it->SeekForNext(RSlice(folded_key, folded_key_len));
for (it->SeekToFirstGreaterEqual(RSlice(folded_key, folded_key_len));
it->Valid() && getId(it->Key()) == Record::id; it->Next()) {
typename Record::Key s_key;
Record::unfoldKey(reinterpret_cast<const uint8_t*>(it->Key().data() + sizeof(SEP)), s_key);
Expand All @@ -196,7 +196,7 @@ struct RocksDBAdapter : public Adapter<Record> {
Fold(folded_key, Record::id) + Record::foldKey(folded_key + sizeof(SEP), key);
// -------------------------------------------------------------------------------------
rocksdb::Iterator* it = map.db->NewIterator(map.ro);
for (it->SeekForPrev(RSlice(folded_key, folded_key_len));
for (it->SeekToLastLessEqual(RSlice(folded_key, folded_key_len));
it->Valid() && getId(it->Key()) == Record::id; it->Prev()) {
typename Record::Key s_key;
Record::unfoldKey(reinterpret_cast<const uint8_t*>(it->Key().data() + sizeof(SEP)), s_key);
Expand Down
12 changes: 4 additions & 8 deletions examples/c/BasicKvExample.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,11 @@ int main() {
return -1;
}

uint8_t succeed = BasicKvIterSeekForFirst(iterHandle, 0);
while (succeed) {
for (BasicKvIterSeekToFirst(iterHandle, 0); BasicKvIterValid(iterHandle);
BasicKvIterNext(iterHandle, 0)) {
StringSlice key = BasicKvIterKey(iterHandle);
StringSlice val = BasicKvIterVal(iterHandle);
printf("%.*s, %.*s\n", (int)key.mSize, key.mData, (int)val.mSize, val.mData);

succeed = BasicKvIterNext(iterHandle, 0);
}

// destroy the iterator
Expand All @@ -88,13 +86,11 @@ int main() {
return -1;
}

uint8_t succeed = BasicKvIterSeekForLast(iterHandle, 0);
while (succeed) {
for (BasicKvIterSeekToLast(iterHandle, 0); BasicKvIterValid(iterHandle);
BasicKvIterPrev(iterHandle, 0)) {
StringSlice key = BasicKvIterKey(iterHandle);
StringSlice val = BasicKvIterVal(iterHandle);
printf("%.*s, %.*s\n", (int)key.mSize, key.mData, (int)val.mSize, val.mData);

succeed = BasicKvIterPrev(iterHandle, 0);
}

// destroy the iterator
Expand Down
35 changes: 16 additions & 19 deletions include/leanstore/btree/core/Iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,54 +15,51 @@ class Iterator {
// Interfaces for exact key seeking
//------------------------------------------------------------------------------------------------

//! Seek for the exact key in the tree
//! @return true if the key is found, false otherwise
virtual bool SeekExact(Slice key) = 0;
//! Seek to the position of the key which = the given key
virtual void SeekToEqual(Slice key) = 0;

//------------------------------------------------------------------------------------------------
// Interfaces for ascending iteration
//------------------------------------------------------------------------------------------------

//! Seek for the first key in the tree
//! @return true if the key is found, false otherwise
virtual bool SeekForFirst() = 0;
//! Seek to the position of the first key
virtual void SeekToFirst() = 0;

//! Seek for the first key in the tree which >= the given key
//! @return true if the key is found, false otherwise
virtual bool SeekForNext(Slice key) = 0;
//! Seek to the position of the first key which >= the given key
virtual void SeekToFirstGreaterEqual(Slice key) = 0;

//! Whether a next key exists in the tree
//! @return true if the next key exists, false otherwise
virtual bool HasNext() = 0;

//! Iterate to the next key in the tree
//! @return true if the next key exists, false otherwise
virtual bool Next() = 0;
virtual void Next() = 0;

//------------------------------------------------------------------------------------------------
// Interfaces for descending iteration
//------------------------------------------------------------------------------------------------

//! Seek for the last key in the tree
//! @return true if the key is found, false otherwise
virtual bool SeekForLast() = 0;
//! Seek to the position of the last key
virtual void SeekToLast() = 0;

//! Seek for the last key in the tree which <= the given key
//! @return true if the key is found, false otherwise
virtual bool SeekForPrev(Slice key) = 0;
//! Seek to the position of the last key which <= the given key
virtual void SeekToLastLessEqual(Slice key) = 0;

//! Whether a previous key exists in the tree
//! @return true if the previous key exists, false otherwise
virtual bool HasPrev() = 0;

//! Iterate to the previous key in the tree
//! @return true if the previous key exists, false otherwise
virtual bool Prev() = 0;
virtual void Prev() = 0;

//------------------------------------------------------------------------------------------------
// Interfaces for accessing the current iterator position
//------------------------------------------------------------------------------------------------

//! Whether the iterator is valid
//! @return true if the iterator is pointing to a valid key-value pair, false otherwise
virtual bool Valid() = 0;

//! Get the key of the current iterator position, the key is read-only
virtual Slice Key() = 0;

Expand Down
6 changes: 3 additions & 3 deletions include/leanstore/btree/core/PessimisticExclusiveIterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class PessimisticExclusiveIterator : public PessimisticIterator {
}

virtual OpCode SeekToInsert(Slice key) {
seekForTargetPageOnDemand(key);
seekToTargetPageOnDemand(key);

bool isEqual = false;
mSlotId = mGuardedLeaf->LowerBound<false>(key, &isEqual);
Expand Down Expand Up @@ -116,8 +116,8 @@ class PessimisticExclusiveIterator : public PessimisticIterator {
AssembleKey();
Slice key = this->Key();
SplitForKey(key);
auto succeed [[maybe_unused]] = SeekExact(key);
LS_DCHECK(succeed);
SeekToEqual(key);
LS_DCHECK(Valid());
}
LS_DCHECK(mSlotId != -1);
mGuardedLeaf->ExtendPayload(mSlotId, targetSize);
Expand Down
Loading

0 comments on commit 495265f

Please sign in to comment.