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 
111  virtual bool DeleteKey(const IndexKey *key, RecordId &rid) = 0;
112 
122  bool DeleteRecord(Record& rec, const Schema *tabschema = nullptr);
123 
147  virtual std::unique_ptr<Iterator> StartScan(const IndexKey *lower,
148  bool lower_isstrict,
149  const IndexKey *upper,
150  bool upper_isstrict) = 0;
151 
152 protected:
153  Index(std::shared_ptr<const IndexDesc> idxdesc);
154 
155 private:
163  void PrepareKey(std::vector<Datum> &data,
164  const char *recbuf,
165  const Schema *tabschema);
166 
167  std::shared_ptr<const IndexDesc> m_idxdesc;
168 
175 public:
181  class Iterator {
182  public:
183  Iterator() = default;
184  Iterator(const Iterator&) = delete;
185  Iterator& operator=(const Iterator&) = delete;
186 
190  Iterator(Iterator&& it) = default;
191 
195  Iterator &operator=(Iterator &&it) = default;
196 
197  virtual ~Iterator() = default;
198 
202  constexpr const Index*
203  GetIndex() const {
204  return m_index;
205  }
206 
211  virtual bool Next() = 0;
212 
216  virtual bool IsAtValidItem() = 0;
217 
224  virtual const Record &GetCurrentItem() = 0;
225 
231 
235  virtual void EndScan() = 0;
236 
237  protected:
238  Iterator(Index *index):
239  m_index(index) {}
240 
246  template<class T>
247  constexpr T*
248  GetIndexAs() const {
249  return (T*) m_index;
250  }
251 
252  private:
254  };
255 };
256 
257 } // namespace taco
258 
259 #endif // INDEX_INDEX_H
BulkLoadIterator is an interface for providing (key, RecordId) pairs for index bulk loading.
Definition: BulkLoadIterator.h:17
Definition: IndexDesc.h:11
A forward-iterator for the items in the index.
Definition: Index.h:181
virtual bool Next()=0
Moves the iterator to the next indexed item if any.
Iterator & operator=(Iterator &&it)=default
Default move assignment.
virtual const Record & GetCurrentItem()=0
Returns the current indexed item the iterator is currently at, where the GetData() and GetLength() pa...
constexpr T * GetIndexAs() const
Static casts the m_index pointer to T* type.
Definition: Index.h:248
virtual bool IsAtValidItem()=0
Returns whether the iterator is currently at a valid indexed item.
virtual ~Iterator()=default
Index * m_index
Definition: Index.h:253
Iterator(const Iterator &)=delete
Iterator(Iterator &&it)=default
Default move constructor.
virtual void EndScan()=0
Ends the index scan.
Iterator & operator=(const Iterator &)=delete
constexpr const Index * GetIndex() const
Returns the underlying index.
Definition: Index.h:203
Iterator(Index *index)
Definition: Index.h:238
virtual RecordId GetCurrentRecordId()=0
Returns the record id of the indexed item the iterator is currently at.
An interface class for index implementations.
Definition: Index.h:19
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.
unique_malloced_ptr m_key
A buffer enough for holding an Indexkey for the number of fields in the key schema.
Definition: Index.h:174
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
void PrepareKey(std::vector< Datum > &data, const char *recbuf, const Schema *tabschema)
Extract the keys from recbuf into m_key.
Definition: Index.cpp:68
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
virtual bool InsertKey(const IndexKey *key, RecordId rid)=0
Inserts the (key, rid) pair into the index.
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
virtual bool DeleteKey(const IndexKey *key, RecordId &rid)=0
Deletes an arbitrary data entry with matching key if rid is invalid.
static void Initialize(const IndexDesc *idxdesc)
Initializes an index described by an index descriptor.
Definition: Index.cpp:25
const Schema * GetKeySchema() const
Returns the key schema of this index.
Definition: Index.h:62
const IndexDesc * GetIndexDesc() const
Returns the index descriptor of this index.
Definition: Index.h:54
bool DeleteRecord(Record &rec, const Schema *tabschema=nullptr)
Deletes the (key, rid) extracted from the table record from the index.
Definition: Index.cpp:61
std::shared_ptr< const IndexDesc > m_idxdesc
Definition: Index.h:167
Index(std::shared_ptr< const IndexDesc > idxdesc)
Definition: Index.cpp:10
virtual ~Index()
Index object destructor.
Definition: Index.cpp:14
Definition: Record.h:87
A Schema object stores the information for accessing an ordered set of typed fields either from a dis...
Definition: Schema.h:39
Definition: datum.h:28
An IndexKey stores references to a few data (Datum objects) that comprise a key tuple to in an index.
Definition: IndexKey.h:35
The record ID of a record on a page is a pair of ‘(PageNumber, SlotId)’.
Definition: Record.h:17
std::unique_ptr< void, AlignedAllocImpl::FreeMem > unique_malloced_ptr
Definition: tdb_base.h:94