taco-db  0.1.0
Index.h
Go to the documentation of this file.
1 #ifndef INDEX_INDEX_H
2 #define INDEX_INDEX_H
3 
4 #include "tdb.h"
5 
7 #include "index/IndexKey.h"
8 #include "index/idxtyps.h"
9 #include "catalog/IndexDesc.h"
10 #include "storage/Record.h"
11 
12 namespace taco {
13 
19 class Index {
20 public:
21  class Iterator;
22 
36  static void Initialize(const IndexDesc *idxdesc);
37 
42  static std::unique_ptr<Index> Create(
43  std::shared_ptr<const IndexDesc> idxdesc);
44 
48  virtual ~Index();
49 
53  const IndexDesc*
54  GetIndexDesc() const {
55  return m_idxdesc.get();
56  }
57 
61  const Schema*
62  GetKeySchema() const {
63  return m_idxdesc->GetKeySchema();
64  }
65 
79  virtual void BulkLoad(BulkLoadIterator &iter);
80 
88  virtual bool InsertKey(const IndexKey *key, RecordId rid) = 0;
89 
99  bool InsertRecord(const Record& rec, const Schema *tabschema = nullptr);
100 
110  virtual bool DeleteKey(const IndexKey *key, RecordId &rid) = 0;
111 
121  bool DeleteRecord(Record& rec, const Schema *tabschema = nullptr);
122 
146  virtual std::unique_ptr<Iterator> StartScan(const IndexKey *lower,
147  bool lower_isstrict,
148  const IndexKey *upper,
149  bool upper_isstrict) = 0;
150 
151 protected:
152  Index(std::shared_ptr<const IndexDesc> idxdesc);
153 
154 private:
162  void PrepareKey(std::vector<Datum> &data,
163  const char *recbuf,
164  const Schema *tabschema);
165 
166  std::shared_ptr<const IndexDesc> m_idxdesc;
167 
174 public:
180  class Iterator {
181  public:
182  Iterator() = default;
183  Iterator(const Iterator&) = delete;
184  Iterator& operator=(const Iterator&) = delete;
185 
189  Iterator(Iterator&& it) = default;
190 
194  Iterator &operator=(Iterator &&it) = default;
195 
196  virtual ~Iterator() = default;
197 
201  constexpr const Index*
202  GetIndex() const {
203  return m_index;
204  }
205 
210  virtual bool Next() = 0;
211 
215  virtual bool IsAtValidItem() = 0;
216 
223  virtual const Record &GetCurrentItem() = 0;
224 
229  virtual RecordId GetCurrentRecordId() = 0;
230 
234  virtual void EndScan() = 0;
235 
236  protected:
237  Iterator(Index *index):
238  m_index(index) {}
239 
245  template<class T>
246  constexpr T*
247  GetIndexAs() const {
248  return (T*) m_index;
249  }
250 
251  private:
253  };
254 };
255 
256 } // namespace taco
257 
258 #endif // INDEX_INDEX_H
taco::Index::Initialize
static void Initialize(const IndexDesc *idxdesc)
Initializes an index described by an index descriptor.
Definition: Index.cpp:25
taco::Index::Iterator::Iterator
Iterator()=default
taco::Index::Iterator::Next
virtual bool Next()=0
Moves the iterator to the next indexed item if any.
taco::Index::Iterator::EndScan
virtual void EndScan()=0
Ends the index scan.
taco::Index::GetKeySchema
const Schema * GetKeySchema() const
Returns the key schema of this index.
Definition: Index.h:62
taco::Index::GetIndexDesc
const IndexDesc * GetIndexDesc() const
Returns the index descriptor of this index.
Definition: Index.h:54
taco::Index::Iterator::GetCurrentRecordId
virtual RecordId GetCurrentRecordId()=0
Returns the record id of the indexed item the iterator is currently at.
taco::Index::Iterator::GetCurrentItem
virtual const Record & GetCurrentItem()=0
Returns the current indexed item the iterator is currently at, where the GetData() and GetLength() pa...
taco
Definition: datum.h:28
unique_malloced_ptr
std::unique_ptr< void, AlignedAllocImpl::FreeMem > unique_malloced_ptr
Definition: tdb_base.h:94
taco::Schema
A Schema object stores the information for accessing an ordered set of typed fields either from a dis...
Definition: Schema.h:39
taco::RecordId
The record ID of a record on a page is a pair of ‘(PageNumber, SlotId)’.
Definition: Record.h:17
taco::Index::Iterator::Iterator
Iterator(Index *index)
Definition: Index.h:237
tdb.h
taco::Index::InsertKey
virtual bool InsertKey(const IndexKey *key, RecordId rid)=0
Inserts the (key, rid) pair into the index.
taco::Index::BulkLoad
virtual void BulkLoad(BulkLoadIterator &iter)
Loads all the (key, record id) pairs provided by the iterator iter into an empty index.
Definition: Index.cpp:17
taco::IndexKey
An IndexKey stores references to a few data (Datum objects) that comprise a key tuple to in an index.
Definition: IndexKey.h:35
taco::Index::DeleteKey
virtual bool DeleteKey(const IndexKey *key, RecordId &rid)=0
Deletes any matching key (if rid is invalid), or the matching (key, rid) pair (if rid is valid) from ...
taco::Index::DeleteRecord
bool DeleteRecord(Record &rec, const Schema *tabschema=nullptr)
Deletes the (key, rid) extracted from the table record from the index.
Definition: Index.cpp:61
taco::Index::Iterator::GetIndex
constexpr const Index * GetIndex() const
Returns the underlying index.
Definition: Index.h:202
taco::BulkLoadIterator
BulkLoadIterator is an interface for providing (key, RecordId) pairs for index bulk loading.
Definition: BulkLoadIterator.h:17
taco::Index::StartScan
virtual std::unique_ptr< Iterator > StartScan(const IndexKey *lower, bool lower_isstrict, const IndexKey *upper, bool upper_isstrict)=0
Returns a forward-iterator for all the indexed items in the specified range defined by the arguments.
taco::Record
Definition: Record.h:87
taco::Index::Iterator::IsAtValidItem
virtual bool IsAtValidItem()=0
Returns whether the iterator is currently at a valid indexed item.
taco::Index::Iterator::operator=
Iterator & operator=(const Iterator &)=delete
taco::Index::Iterator::~Iterator
virtual ~Iterator()=default
taco::Index::~Index
virtual ~Index()
Index object destructor.
Definition: Index.cpp:14
IndexDesc.h
taco::Index::Iterator::m_index
Index * m_index
Definition: Index.h:252
taco::Index::Create
static std::unique_ptr< Index > Create(std::shared_ptr< const IndexDesc > idxdesc)
Creates an index object over an index file described by the index descriptor, which has already been ...
Definition: Index.cpp:40
taco::Index::Iterator
A forward-iterator for the items in the index.
Definition: Index.h:180
taco::Index::InsertRecord
bool InsertRecord(const Record &rec, const Schema *tabschema=nullptr)
Inserts the (key, rid) extracted from the table record into the index.
Definition: Index.cpp:54
taco::IndexDesc
Definition: IndexDesc.h:11
BulkLoadIterator.h
taco::Index::m_key
unique_malloced_ptr m_key
A buffer enough for holding an Indexkey for the number of fields in the key schema.
Definition: Index.h:173
taco::Index::PrepareKey
void PrepareKey(std::vector< Datum > &data, const char *recbuf, const Schema *tabschema)
Extract the keys from recbuf into m_key.
Definition: Index.cpp:68
taco::Index
An interface class for index implementations.
Definition: Index.h:19
Record.h
taco::Index::m_idxdesc
std::shared_ptr< const IndexDesc > m_idxdesc
Definition: Index.h:166
IndexKey.h
taco::Index::Iterator::GetIndexAs
constexpr T * GetIndexAs() const
Static casts the m_index pointer to T* type.
Definition: Index.h:247
taco::Index::Index
Index(std::shared_ptr< const IndexDesc > idxdesc)
Definition: Index.cpp:10
idxtyps.h