Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit 4267752

Browse files
author
Ziqi Wang
committed
Fixing non-unique key insert problem
1 parent 23e1005 commit 4267752

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

src/include/index/bwtree.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2986,7 +2986,8 @@ class BwTree : public BwTreeBase {
29862986
* If both are nullptr then we just traverse and do not do anything
29872987
*/
29882988
const KeyValuePair *Traverse(Context *context_p, const ValueType *value_p,
2989-
std::pair<int, bool> *index_pair_p) {
2989+
std::pair<int, bool> *index_pair_p,
2990+
bool unique_key=false) {
29902991
// For value collection it always returns nullptr
29912992
const KeyValuePair *found_pair_p = nullptr;
29922993

@@ -3072,7 +3073,7 @@ class BwTree : public BwTreeBase {
30723073
} else {
30733074
// If a value is given then use this value to Traverse down leaf
30743075
// page to find whether the value exists or not
3075-
found_pair_p = NavigateLeafNode(context_p, *value_p, index_pair_p);
3076+
found_pair_p = NavigateLeafNode(context_p, *value_p, index_pair_p, unique_key);
30763077
}
30773078

30783079
if (context_p->abort_flag == true) {
@@ -4172,7 +4173,8 @@ class BwTree : public BwTreeBase {
41724173
*/
41734174
const KeyValuePair *NavigateLeafNode(Context *context_p,
41744175
const ValueType &search_value,
4175-
std::pair<int, bool> *index_pair_p) {
4176+
std::pair<int, bool> *index_pair_p,
4177+
bool unique_key=false) {
41764178
// This will go to the right sibling until we have seen
41774179
// a node whose range match the search key
41784180
NavigateSiblingChain(context_p);
@@ -4217,7 +4219,7 @@ class BwTree : public BwTreeBase {
42174219
// We do not need to check any delete set here, since if the
42184220
// value has been deleted earlier then this function would
42194221
// already have returned
4220-
if (ValueCmpEqual(scan_start_it->second, search_value)) {
4222+
if (unique_key == true || ValueCmpEqual(scan_start_it->second, search_value)) {
42214223
// Since only Delete() will use this piece of information
42224224
// we set exist flag to false to indicate that the value
42234225
// has been invalidated
@@ -4245,7 +4247,7 @@ class BwTree : public BwTreeBase {
42454247
static_cast<const LeafInsertNode *>(node_p);
42464248

42474249
if (KeyCmpEqual(search_key, insert_node_p->item.first)) {
4248-
if (ValueCmpEqual(insert_node_p->item.second, search_value)) {
4250+
if (unique_key == true || ValueCmpEqual(insert_node_p->item.second, search_value)) {
42494251
// Only Delete() will use this
42504252
// We just simply inherit from the first node
42514253
*index_pair_p = insert_node_p->GetIndexPair();
@@ -4264,7 +4266,7 @@ class BwTree : public BwTreeBase {
42644266

42654267
// If the value was deleted then return false
42664268
if (KeyCmpEqual(search_key, delete_node_p->item.first)) {
4267-
if (ValueCmpEqual(delete_node_p->item.second, search_value)) {
4269+
if (unique_key == true || ValueCmpEqual(delete_node_p->item.second, search_value)) {
42684270
// Only Insert() will use this
42694271
// We just simply inherit from the first node
42704272
*index_pair_p = delete_node_p->GetIndexPair();
@@ -6993,8 +6995,12 @@ class BwTree : public BwTreeBase {
69936995
*
69946996
* This function returns false if value already exists
69956997
* If CAS fails this function retries until it succeeds
6998+
*
6999+
* Note that this function also takes a unique_key argument, to indicate whether
7000+
* we allow the same key with different values. For a primary key index this
7001+
* should be set true. By default we allow non-unique key
69967002
*/
6997-
bool Insert(const KeyType &key, const ValueType &value) {
7003+
bool Insert(const KeyType &key, const ValueType &value, bool unique_key=false) {
69987004
LOG_TRACE("Insert called");
69997005

70007006
#ifdef BWTREE_DEBUG
@@ -7011,7 +7017,7 @@ class BwTree : public BwTreeBase {
70117017
// Also if the key previously exists in the delta chain
70127018
// then return the position of the node using next_key_p
70137019
// if there is none then return nullptr
7014-
const KeyValuePair *item_p = Traverse(&context, &value, &index_pair);
7020+
const KeyValuePair *item_p = Traverse(&context, &value, &index_pair, unique_key);
70157021

70167022
// If the key-value pair already exists then return false
70177023
if (item_p != nullptr) {

src/index/bwtree_index.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,20 @@ bool BWTREE_INDEX_TYPE::InsertEntry(const storage::Tuple *key,
5353
KeyType index_key;
5454
index_key.SetFromKey(key);
5555

56-
bool ret = container.Insert(index_key, value);
56+
bool ret;
57+
if(HasUniqueKeys() == true) {
58+
ret = container.Insert(index_key, value, true);
59+
} else {
60+
ret = container.Insert(index_key, value, false);
61+
}
5762

58-
if (static_cast<StatsType>(settings::SettingsManager::GetInt(
59-
settings::SettingId::stats_mode)) != StatsType::INVALID) {
63+
if (static_cast<StatsType>(settings::SettingsManager::GetInt(settings::SettingId::stats_mode)) != StatsType::INVALID) {
6064
stats::BackendStatsContext::GetInstance()->IncrementIndexInserts(metadata);
6165
}
6266

63-
LOG_TRACE("InsertEntry(key=%s, val=%s) [%s]",
64-
index_key.GetInfo().c_str(),
67+
// NOTE: If I use index_key.GetInfo() here, I always get an empty key?
68+
LOG_DEBUG("InsertEntry(key=%s, val=%s) [%s]",
69+
key->GetInfo().c_str(),
6570
IndexUtil::GetInfo(value).c_str(),
6671
(ret ? "SUCCESS" : "FAIL"));
6772

@@ -85,14 +90,13 @@ bool BWTREE_INDEX_TYPE::DeleteEntry(const storage::Tuple *key,
8590
// it is unnecessary for us to allocate memory
8691
bool ret = container.Delete(index_key, value);
8792

88-
if (static_cast<StatsType>(settings::SettingsManager::GetInt(
89-
settings::SettingId::stats_mode)) != StatsType::INVALID) {
93+
if (static_cast<StatsType>(settings::SettingsManager::GetInt(settings::SettingId::stats_mode)) != StatsType::INVALID) {
9094
stats::BackendStatsContext::GetInstance()->IncrementIndexDeletes(
9195
delete_count, metadata);
9296
}
9397

94-
LOG_TRACE("DeleteEntry(key=%s, val=%s) [%s]",
95-
index_key.GetInfo().c_str(),
98+
LOG_DEBUG("DeleteEntry(key=%s, val=%s) [%s]",
99+
key->GetInfo().c_str(),
96100
IndexUtil::GetInfo(value).c_str(),
97101
(ret ? "SUCCESS" : "FAIL"));
98102

@@ -122,8 +126,7 @@ bool BWTREE_INDEX_TYPE::CondInsertEntry(
122126
assert(ret == false);
123127
}
124128

125-
if (static_cast<StatsType>(settings::SettingsManager::GetInt(
126-
settings::SettingId::stats_mode)) != StatsType::INVALID) {
129+
if (static_cast<StatsType>(settings::SettingsManager::GetInt(settings::SettingId::stats_mode)) != StatsType::INVALID) {
127130
stats::BackendStatsContext::GetInstance()->IncrementIndexInserts(metadata);
128131
}
129132

@@ -197,8 +200,7 @@ void BWTREE_INDEX_TYPE::Scan(
197200
}
198201
} // if is full scan
199202

200-
if (static_cast<StatsType>(settings::SettingsManager::GetInt(
201-
settings::SettingId::stats_mode)) != StatsType::INVALID) {
203+
if (static_cast<StatsType>(settings::SettingsManager::GetInt(settings::SettingId::stats_mode)) != StatsType::INVALID) {
202204
stats::BackendStatsContext::GetInstance()->IncrementIndexReads(
203205
result.size(), metadata);
204206
}
@@ -222,6 +224,7 @@ void BWTREE_INDEX_TYPE::ScanLimit(
222224
const std::vector<ExpressionType> &expr_list,
223225
ScanDirectionType scan_direction, std::vector<ValueType> &result,
224226
const ConjunctionScanPredicate *csp_p, uint64_t limit, uint64_t offset) {
227+
225228
// Only work with limit == 1 and offset == 0
226229
// Because that gets translated to "min"
227230
// But still since we could not access tuples in the table
@@ -243,6 +246,7 @@ void BWTREE_INDEX_TYPE::ScanLimit(
243246
auto scan_itr = container.Begin(index_low_key);
244247
if ((scan_itr.IsEnd() == false) &&
245248
(container.KeyCmpLessEqual(scan_itr->first, index_high_key))) {
249+
246250
result.push_back(scan_itr->second);
247251
}
248252
} else {
@@ -263,8 +267,7 @@ void BWTREE_INDEX_TYPE::ScanAllKeys(std::vector<ValueType> &result) {
263267
it++;
264268
}
265269

266-
if (static_cast<StatsType>(settings::SettingsManager::GetInt(
267-
settings::SettingId::stats_mode)) != StatsType::INVALID) {
270+
if (static_cast<StatsType>(settings::SettingsManager::GetInt(settings::SettingId::stats_mode)) != StatsType::INVALID) {
268271
stats::BackendStatsContext::GetInstance()->IncrementIndexReads(
269272
result.size(), metadata);
270273
}
@@ -280,8 +283,7 @@ void BWTREE_INDEX_TYPE::ScanKey(const storage::Tuple *key,
280283
// This function in BwTree fills a given vector
281284
container.GetValue(index_key, result);
282285

283-
if (static_cast<StatsType>(settings::SettingsManager::GetInt(
284-
settings::SettingId::stats_mode)) != StatsType::INVALID) {
286+
if (static_cast<StatsType>(settings::SettingsManager::GetInt(settings::SettingId::stats_mode)) != StatsType::INVALID) {
285287
stats::BackendStatsContext::GetInstance()->IncrementIndexReads(
286288
result.size(), metadata);
287289
}

0 commit comments

Comments
 (0)