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

Commit f6edb95

Browse files
committed
Clean up of the index utility code. This is to help make it easier for the students to debug their project in the class.
(1) Added GetInfo methods for TupleKey and GenericKey. (2) Added IndexUtil::GetInfo(ItemPointer) (3) Added examples of debug output in BwTreeIndex::InsertEntry + DeleteEntry I'm dedicating this commit to @jarulraj. Keep it greasy while you are on tour, son.
1 parent 2b64904 commit f6edb95

File tree

10 files changed

+231
-86
lines changed

10 files changed

+231
-86
lines changed

src/include/index/bwtree.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//===----------------------------------------------------------------------===//
22
//
3-
// PelotonDB
3+
// Peloton
44
//
55
// bwtree.h
66
//
7-
// Identification: src/index/bwtree.h
7+
// Identification: src/include/index/bwtree.h
88
//
9-
// Copyright (c) 2015, Carnegie Mellon University Database Group
9+
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
1010
//
1111
//===----------------------------------------------------------------------===//
1212
//

src/include/index/bwtree_index.h

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
//
55
// bwtree_index.h
66
//
7-
// Identification: src/backend/index/bwtree_index.h
7+
// Identification: src/include/index/bwtree_index.h
88
//
9-
// Copyright (c) 2015-16, Carnegie Mellon University Database Group
9+
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
1010
//
1111
//===----------------------------------------------------------------------===//
1212

@@ -67,20 +67,20 @@ class BWTreeIndex : public Index {
6767

6868
~BWTreeIndex();
6969

70-
bool InsertEntry(const storage::Tuple *key, ItemPointer *value);
70+
bool InsertEntry(const storage::Tuple *key, ItemPointer *value) override;
7171

72-
bool DeleteEntry(const storage::Tuple *key, ItemPointer *value);
72+
bool DeleteEntry(const storage::Tuple *key, ItemPointer *value) override;
7373

7474
bool CondInsertEntry(const storage::Tuple *key,
7575
ItemPointer *value,
76-
std::function<bool(const void *)> predicate);
76+
std::function<bool(const void *)> predicate) override;
7777

7878
void Scan(const std::vector<type::Value> &values,
7979
const std::vector<oid_t> &key_column_ids,
8080
const std::vector<ExpressionType> &expr_types,
8181
ScanDirectionType scan_direction,
8282
std::vector<ValueType> &result,
83-
const ConjunctionScanPredicate *csp_p);
83+
const ConjunctionScanPredicate *csp_p) override;
8484

8585
void ScanLimit(const std::vector<type::Value> &values,
8686
const std::vector<oid_t> &key_column_ids,
@@ -89,23 +89,24 @@ class BWTreeIndex : public Index {
8989
std::vector<ValueType> &result,
9090
const ConjunctionScanPredicate *csp_p,
9191
uint64_t limit,
92-
uint64_t offset);
92+
uint64_t offset) override;
9393

94-
void ScanAllKeys(std::vector<ValueType> &result);
94+
void ScanAllKeys(std::vector<ValueType> &result) override;
9595

9696
void ScanKey(const storage::Tuple *key,
97-
std::vector<ValueType> &result);
97+
std::vector<ValueType> &result) override;
9898

99-
std::string GetTypeName() const;
99+
std::string GetTypeName() const override;
100100

101101
// TODO: Implement this
102-
size_t GetMemoryFootprint() { return 0; }
102+
size_t GetMemoryFootprint() override { return 0; }
103103

104-
bool NeedGC() {
104+
bool NeedGC() override {
105105
return container.NeedGarbageCollection();
106106
}
107107

108-
void PerformGC() {
108+
void PerformGC() override {
109+
LOG_INFO("Bw-Tree Garbage Collection!");
109110
container.PerformGarbageCollection();
110111

111112
return;

src/include/index/compact_ints_key.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
// Identification: src/include/index/compact_ints_key.h
88
//
9-
// Copyright (c) 2015-17, Carnegie Mellon University Database Group
9+
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
1010
//
1111
//===----------------------------------------------------------------------===//
1212

@@ -320,16 +320,23 @@ class CompactIntsKey {
320320
}
321321

322322
public:
323-
/*
324-
* GetInfo() - Prints the content of this key
323+
324+
/**
325+
* Prints the content of this key.
326+
*
327+
* IMPORTANT: This class should <b>not</b> override Printable
328+
* because that adds an extra 8 bytes and it makes all the math
329+
* above for doing fast comparisons fail.
330+
*
331+
* @return
325332
*/
326-
std::string GetInfo() const {
333+
const std::string GetInfo() const {
327334
std::ostringstream os;
328335
os << "CompactIntsKey<" << KeySize << "> - " << key_size_byte << " bytes"
329336
<< std::endl;
330337

331338
// This is the current offset we are on printing the key
332-
int offset = 0;
339+
size_t offset = 0;
333340
while (offset < key_size_byte) {
334341
constexpr int byte_per_line = 16;
335342
os << StringUtil::Format("0x%.8X ", offset);

src/include/index/generic_key.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
//
77
// Identification: src/include/index/generic_key.h
88
//
9-
// Copyright (c) 2015-16, Carnegie Mellon University Database Group
9+
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
1010
//
1111
//===----------------------------------------------------------------------===//
1212

1313
#pragma once
1414

15+
#include <sstream>
16+
1517
#include "type/type_util.h"
1618

1719
namespace peloton {
@@ -52,6 +54,24 @@ class GenericKey {
5254
return (data_ptr);
5355
}
5456

57+
/**
58+
* Generate a human-readable version of this GenericKey.
59+
* This method is slow (lots of memory copying) so you
60+
* don't want to execute this on the critical path of
61+
* anything real in the system.
62+
*
63+
* IMPORTANT: The output is based on the original tuple
64+
* schema and not the key schema. So you may end up seeing
65+
* attributes values that are not actually used in the index.
66+
*
67+
* @return
68+
*/
69+
const std::string GetInfo() const {
70+
storage::Tuple tuple(schema, data);
71+
return (tuple.GetInfo());
72+
}
73+
74+
5575
// actual location of data, extends past the end.
5676
char data[KeySize];
5777

src/include/index/index.h

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
// Identification: src/include/index/index.h
88
//
9-
// Copyright (c) 2015-16, Carnegie Mellon University Database Group
9+
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
1010
//
1111
//===----------------------------------------------------------------------===//
1212

@@ -255,13 +255,41 @@ class Index : public Printable {
255255
// Index Scan
256256
///////////////////////////////////////////////////////////////////
257257

258+
/**
259+
* Scans a range inside the index and returns all elements
260+
* in the result vector. The type of scan to perform is
261+
* specified by the given scan predicate (csp_p).
262+
* Whether to perform a forward or backward scan is
263+
* determined by the given scan_direction.
264+
*
265+
* @param value_list
266+
* @param tuple_column_id_list
267+
* @param expr_list
268+
* @param scan_direction
269+
* @param result
270+
* @param csp_p
271+
*/
258272
virtual void Scan(const std::vector<type::Value> &value_list,
259273
const std::vector<oid_t> &tuple_column_id_list,
260274
const std::vector<ExpressionType> &expr_list,
261275
ScanDirectionType scan_direction,
262276
std::vector<ItemPointer *> &result,
263277
const ConjunctionScanPredicate *csp_p) = 0;
264278

279+
/**
280+
* The ScanLimit function has the same behavior as the Scan function
281+
* except that the scan stops after it reads offset + limit elements.
282+
* Therefore, maximum of limit elements are returned in the result vector.
283+
*
284+
* @param value_list
285+
* @param tuple_column_id_list
286+
* @param expr_list
287+
* @param scan_direction
288+
* @param result
289+
* @param csp_p
290+
* @param limit
291+
* @param offset
292+
*/
265293
virtual void ScanLimit(const std::vector<type::Value> &value_list,
266294
const std::vector<oid_t> &tuple_column_id_list,
267295
const std::vector<ExpressionType> &expr_list,
@@ -270,17 +298,41 @@ class Index : public Printable {
270298
const ConjunctionScanPredicate *csp_p, uint64_t limit,
271299
uint64_t offset) = 0;
272300

273-
// This is the version used to test scan
274-
// Since it does scan planning everytime, it is slow, and should
275-
// only be used for correctness testing
301+
/**
302+
* This is the version used to test the basic scan operation.
303+
* Since it does a scan planning every time it is invoked, it will
304+
* be slower than the regular scan.
305+
* This should <b>only</b> be used for correctness testing.
306+
*
307+
* @param value_list
308+
* @param tuple_column_id_list
309+
* @param expr_list
310+
* @param scan_direction
311+
* @param result
312+
*/
276313
virtual void ScanTest(const std::vector<type::Value> &value_list,
277314
const std::vector<oid_t> &tuple_column_id_list,
278315
const std::vector<ExpressionType> &expr_list,
279316
const ScanDirectionType &scan_direction,
280317
std::vector<ItemPointer *> &result);
281318

319+
/**
320+
* Scan all of the keys in the index and store their values
321+
* in the result vector.
322+
*
323+
* @param result
324+
*/
282325
virtual void ScanAllKeys(std::vector<ItemPointer *> &result) = 0;
283326

327+
/**
328+
* Finds all of the values in the index for the given key and
329+
* return them in the result vector.
330+
* If the index is not configured to store duplicate keys,
331+
* then at most one value will be returned.
332+
*
333+
* @param key
334+
* @param result
335+
*/
284336
virtual void ScanKey(const storage::Tuple *key,
285337
std::vector<ItemPointer *> &result) = 0;
286338

@@ -341,8 +393,6 @@ class Index : public Printable {
341393
return metadata->GetKeySchema();
342394
}
343395

344-
IndexType GetIndexMethodType() { return metadata->GetIndexType(); }
345-
346396
IndexConstraintType GetIndexType() const {
347397
return metadata->GetIndexConstraintType();
348398
}

src/include/index/index_util.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
//
77
// Identification: src/include/index/index_util.h
88
//
9-
// Copyright (c) 2015-16, Carnegie Mellon University Database Group
9+
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
1413
#pragma once
1514

1615
#include <map>
@@ -76,12 +75,22 @@ class IndexUtil {
7675
std::vector<std::pair<oid_t, oid_t>> &value_index_list);
7776

7877
/**
79-
*
78+
* Generate a string with a list of the values in the index.
79+
* Note that this is will not print out the keys!.
8080
* @param index
8181
* @return
8282
*/
8383
static std::string Debug(Index *index);
8484

85+
/**
86+
* Return a string representation of an ItemPointer.
87+
* This is here because we don't want to add extra methods
88+
* to our ItemPointer since that takes up memory.
89+
* @param ptr
90+
* @return
91+
*/
92+
static const std::string GetInfo(const ItemPointer *ptr);
93+
8594
};
8695

8796

src/include/index/tuple_key.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
//===----------------------------------------------------------------------===//
32
//
43
// Peloton
@@ -7,7 +6,7 @@
76
//
87
// Identification: src/include/index/tuple_key.h
98
//
10-
// Copyright (c) 2015-16, Carnegie Mellon University Database Group
9+
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
1110
//
1211
//===----------------------------------------------------------------------===//
1312

@@ -115,6 +114,20 @@ class TupleKey {
115114
else
116115
return column_indices[indexColumn];
117116
}
117+
118+
/**
119+
* Prints the content of this key.
120+
*
121+
* IMPORTANT: This class should <b>not</b> override Printable
122+
* because that adds an extra 8 bytes and it makes all the math
123+
* above for doing fast comparisons fail.
124+
*
125+
* @return
126+
*/
127+
const std::string GetInfo() const {
128+
storage::Tuple tuple(key_tuple_schema, key_tuple);
129+
return (tuple.GetInfo());
130+
}
118131
};
119132

120133
/*

src/index/bwtree_index.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@
44
//
55
// bwtree_index.cpp
66
//
7-
// Identification: src/backend/index/bwtree_index.cpp
7+
// Identification: src/index/bwtree_index.cpp
88
//
9-
// Copyright (c) 2015-16, Carnegie Mellon University Database Group
9+
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
1010
//
1111
//===----------------------------------------------------------------------===//
12+
1213
#include "index/bwtree_index.h"
1314

14-
#include "common/logger.h"
1515
#include "index/index_key.h"
1616
#include "index/scan_optimizer.h"
1717
#include "statistics/stats_aggregator.h"
18-
#include "storage/tuple.h"
1918
#include "settings/settings_manager.h"
2019

2120
namespace peloton {
@@ -60,6 +59,11 @@ bool BWTREE_INDEX_TYPE::InsertEntry(const storage::Tuple *key,
6059
stats::BackendStatsContext::GetInstance()->IncrementIndexInserts(metadata);
6160
}
6261

62+
LOG_TRACE("InsertEntry(key=%s, val=%s) [%s]",
63+
index_key.GetInfo().c_str(),
64+
IndexUtil::GetInfo(value).c_str(),
65+
(ret ? "SUCCESS" : "FAIL"));
66+
6367
return ret;
6468
}
6569

@@ -73,6 +77,7 @@ bool BWTREE_INDEX_TYPE::DeleteEntry(const storage::Tuple *key,
7377
ItemPointer *value) {
7478
KeyType index_key;
7579
index_key.SetFromKey(key);
80+
7681
size_t delete_count = 0;
7782

7883
// In Delete() since we just use the value for comparison (i.e. read-only)
@@ -83,6 +88,12 @@ bool BWTREE_INDEX_TYPE::DeleteEntry(const storage::Tuple *key,
8388
stats::BackendStatsContext::GetInstance()->IncrementIndexDeletes(
8489
delete_count, metadata);
8590
}
91+
92+
LOG_TRACE("DeleteEntry(key=%s, val=%s) [%s]",
93+
index_key.GetInfo().c_str(),
94+
IndexUtil::GetInfo(value).c_str(),
95+
(ret ? "SUCCESS" : "FAIL"));
96+
8697
return ret;
8798
}
8899

0 commit comments

Comments
 (0)