Skip to content

Commit

Permalink
Swapped back RWLock in btree index after fixing a bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jarulraj committed May 13, 2016
1 parent 6f21eaa commit 486eab2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
38 changes: 24 additions & 14 deletions src/backend/index/btree_index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ BTreeIndex<KeyType, ValueType, KeyComparator,
// we should not rely on shared_ptr to reclaim memory.
// this is because the underlying index can split or merge leaf nodes,
// which invokes data data copy and deletes.
// as the underlying index is unaware of shared_ptr,
// as the underlying index is unaware of shared_ptr,
// memory allocated should be managed carefully by programmers.
for (auto entry = container.begin(); entry != container.end(); ++entry) {
delete entry->second;
Expand All @@ -54,11 +54,12 @@ bool BTreeIndex<KeyType, ValueType, KeyComparator,
new ItemPointer(location));

{
std::lock_guard<std::mutex> lock(index_lock);
index_lock.WriteLock();

// Insert the key, val pair
container.insert(entry);

index_lock.Unlock();
}

return true;
Expand All @@ -73,7 +74,7 @@ bool BTreeIndex<KeyType, ValueType, KeyComparator,
index_key.SetFromKey(key);

{
std::lock_guard<std::mutex> lock(index_lock);
index_lock.WriteLock();

// Delete the < key, location > pair
bool try_again = true;
Expand All @@ -99,6 +100,7 @@ bool BTreeIndex<KeyType, ValueType, KeyComparator,
}
}

index_lock.Unlock();
}

return true;
Expand All @@ -115,16 +117,17 @@ bool BTreeIndex<KeyType, ValueType, KeyComparator,
index_key.SetFromKey(key);

{
std::lock_guard<std::mutex> lock(index_lock);
index_lock.WriteLock();

// find the <key, location> pair
auto entries = container.equal_range(index_key);
for (auto entry = entries.first; entry != entries.second; ++entry) {

ItemPointer item_pointer = *(entry->second);

if (predicate(item_pointer)) {
// this key is already visible or dirty in the index
index_lock.Unlock();
return false;
}
}
Expand All @@ -133,6 +136,7 @@ bool BTreeIndex<KeyType, ValueType, KeyComparator,
container.insert(std::pair<KeyType, ValueType>(
index_key, new ItemPointer(location)));

index_lock.Unlock();
}

return true;
Expand Down Expand Up @@ -165,7 +169,7 @@ void BTreeIndex<KeyType, ValueType, KeyComparator, KeyEqualityChecker>::Scan(
LOG_TRACE("Special case : %d ", special_case);

{
std::lock_guard<std::mutex> lock(index_lock);
index_lock.ReadLock();

auto scan_begin_itr = container.begin();
std::unique_ptr<storage::Tuple> start_key;
Expand Down Expand Up @@ -201,7 +205,7 @@ void BTreeIndex<KeyType, ValueType, KeyComparator, KeyEqualityChecker>::Scan(
// "expression types"
// For instance, "5" EXPR_GREATER_THAN "2" is true
if (Compare(tuple, key_column_ids, expr_types, values) == true) {

ItemPointer item_pointer = *(scan_itr->second);

result.push_back(item_pointer);
Expand All @@ -221,6 +225,7 @@ void BTreeIndex<KeyType, ValueType, KeyComparator, KeyEqualityChecker>::Scan(
break;
}

index_lock.Unlock();
}
}

Expand All @@ -230,19 +235,20 @@ void
BTreeIndex<KeyType, ValueType, KeyComparator, KeyEqualityChecker>::ScanAllKeys(
std::vector<ItemPointer> &result) {
{
std::lock_guard<std::mutex> lock(index_lock);
index_lock.ReadLock();

auto itr = container.begin();

// scan all entries
while (itr != container.end()) {

ItemPointer item_pointer = *(itr->second);

result.push_back(std::move(item_pointer));
itr++;
}

index_lock.Unlock();
}
}

Expand All @@ -254,17 +260,18 @@ void BTreeIndex<KeyType, ValueType, KeyComparator, KeyEqualityChecker>::ScanKey(
index_key.SetFromKey(key);

{
std::lock_guard<std::mutex> lock(index_lock);
index_lock.ReadLock();

// find the <key, location> pair
auto entries = container.equal_range(index_key);
for (auto entry = entries.first; entry != entries.second; ++entry) {

ItemPointer item_pointer = *(entry->second);

result.push_back(item_pointer);
}

index_lock.Unlock();
}

}
Expand Down Expand Up @@ -299,7 +306,7 @@ void BTreeIndex<KeyType, ValueType, KeyComparator, KeyEqualityChecker>::Scan(
LOG_TRACE("Special case : %d ", special_case);

{
std::lock_guard<std::mutex> lock(index_lock);
index_lock.ReadLock();

auto scan_begin_itr = container.begin();
std::unique_ptr<storage::Tuple> start_key;
Expand Down Expand Up @@ -353,6 +360,7 @@ void BTreeIndex<KeyType, ValueType, KeyComparator, KeyEqualityChecker>::Scan(
break;
}

index_lock.Unlock();
}
}

Expand All @@ -362,7 +370,7 @@ void
BTreeIndex<KeyType, ValueType, KeyComparator, KeyEqualityChecker>::ScanAllKeys(
std::vector<ItemPointer *> &result) {
{
std::lock_guard<std::mutex> lock(index_lock);
index_lock.ReadLock();

auto itr = container.begin();

Expand All @@ -373,6 +381,7 @@ BTreeIndex<KeyType, ValueType, KeyComparator, KeyEqualityChecker>::ScanAllKeys(
itr++;
}

index_lock.Unlock();
}

}
Expand All @@ -388,14 +397,15 @@ void BTreeIndex<KeyType, ValueType, KeyComparator, KeyEqualityChecker>::ScanKey(
index_key.SetFromKey(key);

{
std::lock_guard<std::mutex> lock(index_lock);
index_lock.ReadLock();

// find the <key, location> pair
auto entries = container.equal_range(index_key);
for (auto entry = entries.first; entry != entries.second; ++entry) {
result.push_back(entry->second);
}

index_lock.Unlock();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/backend/index/btree_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class BTreeIndex : public Index {
KeyComparator comparator;

// synch helper
std::mutex index_lock;
RWLock index_lock;
};

} // End index namespace
Expand Down

0 comments on commit 486eab2

Please sign in to comment.