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

Commit c0ab1b2

Browse files
pmenonapavlo
authored andcommitted
ART index wrapper
1 parent 2694033 commit c0ab1b2

File tree

10 files changed

+607
-148
lines changed

10 files changed

+607
-148
lines changed

src/common/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "common/thread_pool.h"
2323
#include "concurrency/transaction_manager_factory.h"
2424
#include "gc/gc_manager_factory.h"
25+
#include "index/index.h"
2526
#include "settings/settings_manager.h"
2627
#include "threadpool/mono_queue_pool.h"
2728

src/gc/transaction_level_gc_manager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "concurrency/epoch_manager_factory.h"
1919
#include "concurrency/transaction_manager_factory.h"
2020
#include "settings/settings_manager.h"
21+
#include "index/index.h"
2122
#include "storage/database.h"
2223
#include "storage/tile_group.h"
2324
#include "storage/tuple.h"

src/include/index/art_index.h

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// art_index.h
6+
//
7+
// Identification: src/include/index/art_index.h
8+
//
9+
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#pragma once
14+
15+
#include "adaptive_radix_tree/Tree.h"
16+
#include "index/index.h"
17+
18+
namespace peloton {
19+
namespace index {
20+
21+
//===----------------------------------------------------------------------===//
22+
//
23+
// An Adaptive Radix Tree (ART) based index.
24+
//
25+
//===----------------------------------------------------------------------===//
26+
class ArtIndex : public Index {
27+
friend class IndexFactory;
28+
29+
public:
30+
explicit ArtIndex(IndexMetadata *metadata);
31+
32+
// Forward declare iterator class for later
33+
class Iterator;
34+
35+
/// Insert the given key-value pair into the index
36+
bool InsertEntry(const storage::Tuple *key, ItemPointer *value) override;
37+
38+
/// Delete the given key-value pair from the index
39+
bool DeleteEntry(const storage::Tuple *key, ItemPointer *value) override;
40+
41+
/// Insert the given key-value pair into the index, but only if the predicate
42+
/// returns false for every instance of the given key in the index
43+
bool CondInsertEntry(const storage::Tuple *key, ItemPointer *value,
44+
std::function<bool(const void *)> predicate) override;
45+
46+
/// Scan the range of keys between [start,end] inclusive, placing results in
47+
/// the provided result vector
48+
void ScanRange(const storage::Tuple *start, const storage::Tuple *end,
49+
std::vector<ItemPointer *> &result);
50+
51+
/// Perform a scan, using the predicate, and place the results into the
52+
/// provided result vector
53+
void Scan(const std::vector<type::Value> &values,
54+
const std::vector<oid_t> &key_column_ids,
55+
const std::vector<ExpressionType> &expr_types,
56+
ScanDirectionType scan_direction,
57+
std::vector<ItemPointer *> &result,
58+
const ConjunctionScanPredicate *scan_predicate) override;
59+
60+
/// Perform a limited scan
61+
void ScanLimit(const std::vector<type::Value> &values,
62+
const std::vector<oid_t> &key_column_ids,
63+
const std::vector<ExpressionType> &expr_types,
64+
ScanDirectionType scan_direction,
65+
std::vector<ItemPointer *> &result,
66+
const ConjunctionScanPredicate *scan_predicate, uint64_t limit,
67+
uint64_t offset) override;
68+
69+
/// Scan the entire index, placing results in the provided result vector
70+
void ScanAllKeys(std::vector<ItemPointer *> &result) override;
71+
72+
/// Scan all values for the given key, placing results in the provided vector
73+
void ScanKey(const storage::Tuple *key,
74+
std::vector<ItemPointer *> &result) override;
75+
76+
/// Return the index type
77+
std::string GetTypeName() const override {
78+
return IndexTypeToString(GetIndexMethodType());
79+
}
80+
81+
// TODO(pmenon): Implement me
82+
size_t GetMemoryFootprint() override { return 0; }
83+
84+
/// Does the index need GC
85+
// TODO(pmenon): Implement me
86+
bool NeedGC() override { return false; }
87+
88+
/// Perform any necessary GC on the index
89+
// TODO(pmenon): Implement me
90+
void PerformGC() override {}
91+
92+
/// These two functions are for testing so we can inject custom key loading
93+
/// functions without bringing up a full-blown database/table.
94+
void SetLoadKeyFunc(art::Tree::LoadKeyFunction load_func, void *ctx);
95+
96+
/// Construct an ART key from a Peloton key
97+
void ConstructArtKey(const AbstractTuple &tuple, art::Key &key) const {
98+
key_constructor_.ConstructKey(tuple, key);
99+
}
100+
101+
private:
102+
void ScanRange(const art::Key &start, const art::Key &end,
103+
std::vector<ItemPointer *> &result);
104+
105+
//===--------------------------------------------------------------------===//
106+
//
107+
// Helper class to construct an art::Key from a Peloton key.
108+
//
109+
//===--------------------------------------------------------------------===//
110+
class KeyConstructor {
111+
public:
112+
explicit KeyConstructor(const catalog::Schema &key_schema)
113+
: key_schema_(key_schema) {}
114+
115+
/// Given an input Peloton key, construct an equivalent art::Key
116+
void ConstructKey(const AbstractTuple &input_key, art::Key &tree_key) const;
117+
118+
/// Generate the minimum and maximum key
119+
void ConstructMinMaxKey(art::Key &min_key, art::Key &max_key) const;
120+
121+
private:
122+
template <typename NativeType>
123+
static NativeType FlipSign(NativeType val);
124+
125+
// Write the provided integral data type into the buffer
126+
template <typename NativeType>
127+
static void WriteValue(uint8_t *data, NativeType val);
128+
129+
// Write a specified number of ASCII characters into the output buffer
130+
static void WriteAsciiString(uint8_t *data, const char *val, uint32_t len);
131+
132+
private:
133+
// The index's key schema
134+
const catalog::Schema &key_schema_;
135+
};
136+
137+
private:
138+
// ART
139+
art::Tree container_;
140+
141+
// Key constructor
142+
KeyConstructor key_constructor_;
143+
};
144+
145+
} // namespace index
146+
} // namespace peloton

src/include/index/index.h

Lines changed: 35 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ class AbstractTuple;
3232

3333
namespace catalog {
3434
class Schema;
35-
}
35+
} // namespace catalog
3636

3737
namespace storage {
3838
class Tuple;
39-
}
39+
} // namespace storage
4040

4141
namespace index {
4242

@@ -69,78 +69,56 @@ class IndexMetadata : public Printable {
6969

7070
const std::string &GetName() const { return name_; }
7171

72-
inline oid_t GetOid() { return index_oid; }
72+
oid_t GetOid() { return index_oid; }
7373

74-
inline oid_t GetTableOid() { return table_oid; }
74+
oid_t GetTableOid() { return table_oid; }
7575

76-
inline oid_t GetDatabaseOid() { return database_oid; }
76+
oid_t GetDatabaseOid() { return database_oid; }
7777

78-
inline IndexType GetIndexType() { return index_type_; }
78+
IndexType GetIndexType() { return index_type_; }
7979

8080
IndexConstraintType GetIndexConstraintType() {
8181
return index_constraint_type_;
8282
}
8383

84-
/*
85-
* Returns a schema object pointer that represents
86-
* the schema of indexed columns, from leading column
87-
* to the least important columns
88-
*/
89-
inline const catalog::Schema *GetKeySchema() const { return key_schema; }
84+
// Returns a schema object pointer that represents the schema of indexed
85+
// columns, from leading column to the least important columns.
86+
const catalog::Schema *GetKeySchema() const { return key_schema; }
9087

91-
/*
92-
* Returns a schema object pointer that represents
93-
* the schema of the underlying table
94-
*/
95-
inline const catalog::Schema *GetTupleSchema() const { return tuple_schema; }
88+
// Returns a schema object that represents the schema of the underlying table
89+
const catalog::Schema *GetTupleSchema() const { return tuple_schema; }
9690

9791
// Return the number of columns inside index key (not in tuple key)
98-
//
99-
// Note that this must be defined inside the cpp source file
100-
// because it uses the member of catalog::Schema which is not known here
10192
oid_t GetColumnCount() const;
10293

10394
bool HasUniqueKeys() const { return unique_keys; }
10495

105-
/*
106-
* GetKeyAttrs() - Returns the mapping relation between indexed columns
107-
* and base table columns
108-
*
109-
* The return value is a const vector reference
110-
*
111-
* The entry whose value is j on index i means the i-th column in the
112-
* key is mapped to the j-th column in the base table tuple
113-
*/
114-
inline const std::vector<oid_t> &GetKeyAttrs() const { return key_attrs; }
96+
// Returns the mapping relation between indexed columns and base table columns
97+
// The entry whose value is j on index i means the i-th column in the key is
98+
// mapped to the j-th column in the base table tuple
99+
const std::vector<oid_t> &GetKeyAttrs() const { return key_attrs; }
115100

116-
/*
117-
* GetTupleToIndexMapping() - Returns the mapping relation between tuple key
118-
* column and index key columns
119-
*/
120-
inline const std::vector<oid_t> &GetTupleToIndexMapping() const {
101+
// Returns the mapping relation between tuple key column and index key columns
102+
const std::vector<oid_t> &GetTupleToIndexMapping() const {
121103
return tuple_attrs;
122104
}
123105

124-
inline double GetUtility() const { return utility_ratio; }
106+
double GetUtility() const { return utility_ratio; }
125107

126-
inline void SetUtility(double p_utility_ratio) {
127-
utility_ratio = p_utility_ratio;
128-
}
108+
void SetUtility(double p_utility_ratio) { utility_ratio = p_utility_ratio; }
129109

130-
inline bool GetVisibility() const { return (visible_); }
110+
bool GetVisibility() const { return (visible_); }
131111

132-
inline void SetVisibility(bool visibile) { visible_ = visibile; }
112+
void SetVisibility(bool visible) { visible_ = visible; }
133113

134-
/*
135-
* GetInfo() - Get a string representation for debugging
136-
*/
137-
const std::string GetInfo() const;
114+
// Get a string representation for debugging
115+
const std::string GetInfo() const override;
138116

139117
//===--------------------------------------------------------------------===//
140118
// STATIC HELPERS
141119
//===--------------------------------------------------------------------===//
142120

143-
static inline void SetDefaultVisibleFlag(bool flag) {
121+
static void SetDefaultVisibleFlag(bool flag) {
144122
LOG_DEBUG("Set IndexMetadata visible flag to '%s'",
145123
(flag ? "true" : "false"));
146124
index_default_visibility = flag;
@@ -336,9 +314,9 @@ class Index : public Printable {
336314
virtual void ScanKey(const storage::Tuple *key,
337315
std::vector<ItemPointer *> &result) = 0;
338316

339-
///////////////////////////////////////////////////////////////////
340-
// Garbage Collection
341-
///////////////////////////////////////////////////////////////////
317+
//////////////////////////////////////////////////////////////////////////////
318+
/// Garbage Collection
319+
//////////////////////////////////////////////////////////////////////////////
342320

343321
// This gives a hint on whether GC is needed on the index
344322
// For those that do not need GC this always return false
@@ -348,17 +326,9 @@ class Index : public Printable {
348326
// For those that do not need GC this should return immediately
349327
virtual void PerformGC() = 0;
350328

351-
// The following two are used to perform GC in a
352-
// fast manner
353-
// Because if we register for epoch for every operation then
354-
// essentially we are synchronizing on each operation which does
355-
// not scale at all
356-
// virtual void *JoinEpoch() = 0;
357-
// virtual void LeaveEpoch(void *) = 0;
358-
359-
//===--------------------------------------------------------------------===//
360-
// STATS
361-
//===--------------------------------------------------------------------===//
329+
//////////////////////////////////////////////////////////////////////////////
330+
/// Stats
331+
//////////////////////////////////////////////////////////////////////////////
362332

363333
void IncreaseNumberOfTuplesBy(const size_t amount);
364334

@@ -372,9 +342,9 @@ class Index : public Printable {
372342

373343
void ResetDirty();
374344

375-
//===--------------------------------------------------------------------===//
376-
// Utilities
377-
//===--------------------------------------------------------------------===//
345+
//////////////////////////////////////////////////////////////////////////////
346+
/// Utilities
347+
//////////////////////////////////////////////////////////////////////////////
378348

379349
virtual std::string GetTypeName() const = 0;
380350

@@ -393,6 +363,8 @@ class Index : public Printable {
393363
return metadata->GetKeySchema();
394364
}
395365

366+
IndexType GetIndexMethodType() const { return metadata->GetIndexType(); }
367+
396368
IndexConstraintType GetIndexType() const {
397369
return metadata->GetIndexConstraintType();
398370
}
@@ -420,8 +392,6 @@ class Index : public Printable {
420392

421393
virtual void IncrementIndexedTileGroupOffset() {
422394
indexed_tile_group_offset++;
423-
424-
return;
425395
}
426396

427397
protected:

src/include/storage/data_table.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
#include "common/item_pointer.h"
2222
#include "common/platform.h"
2323
#include "common/container/lock_free_array.h"
24-
#include "executor/executor_context.h"
25-
#include "index/index.h"
2624
#include "storage/abstract_table.h"
2725
#include "storage/indirection_array.h"
2826
#include "trigger/trigger.h"
@@ -37,23 +35,23 @@ namespace peloton {
3735

3836
namespace brain {
3937
class Sample;
40-
}
38+
} // namespace brain
4139

4240
namespace catalog {
4341
class ForeignKey;
44-
}
42+
} // namespace catalog
4543

4644
namespace index {
4745
class Index;
48-
}
46+
} // namespace index
4947

5048
namespace logging {
5149
class LogManager;
52-
}
50+
} // namespace logging
5351

5452
namespace concurrency {
5553
class TransactionContext;
56-
}
54+
} // namespace concurrency
5755

5856
namespace storage {
5957

0 commit comments

Comments
 (0)