taco-db  0.1.0
CatCacheBase.h
Go to the documentation of this file.
1 #ifndef CATALOG_CATCACHEBASE_H
2 #define CATALOG_CATCACHEBASE_H
3 
4 #include "tdb.h"
5 
6 #include <absl/container/flat_hash_map.h>
7 
8 #include "catalog/TableDesc.h"
9 #include "catalog/IndexDesc.h"
10 #include "catalog/systables.h"
11 #include "storage/Record.h"
12 
13 namespace taco {
14 
20  CCLookupTableEntry(RecordId recid, std::shared_ptr<void> systable_struct):
21  m_recid(recid),
22  m_systable_struct(std::move(systable_struct)) {}
23 
28 
30  std::shared_ptr<void> m_systable_struct;
31 };
32 
39 protected:
40 
50  static std::shared_ptr<void> CreateSysTableStruct(
51  Oid tabid, const std::vector<Datum> &data);
52 
59  template<class T>
60  static T*
61  CreateSysTableStruct(const std::vector<Datum> &data) {
62  return T::Create(data);
63  }
64 
65  template<class T>
66  static T*
67  CopySysTableStruct(const T *s) {
68  T *copy = new T(*s);
69  return copy;
70  }
71 
76  template<class T, class ...Args>
77  static T*
78  ConstructSysTableStruct(Args&& ...args) {
79  return new T(std::forward<Args>(args)...);
80  }
81 
82  template<class T>
83  std::vector<Datum>
84  GetDatumVector(const T& systable_struct) {
85  return systable_struct->GetDatumVector();
86  }
87 };
88 
102 template<class CatCacheCls>
104 public:
106 
111 
116  void InitializeFromInitData(const std::string &init_datafile);
117 
121  constexpr
122  bool IsInitialized() const {
123  return m_initialized;
124  }
125 
126  inline std::shared_ptr<const SysTable_Table>
127  FindTable(Oid tabid) {
128  auto entry =
132  tabid);
133  if (!entry) {
134  return nullptr;
135  }
136  return static_pointer_cast<const SysTable_Table>(
137  entry->m_systable_struct);
138  }
139 
140  inline Oid
141  FindTableByName(absl::string_view tabname) {
142  auto entry =
146  tabname);
147  if (!entry) {
148  return InvalidOid;
149  }
150  return ((const SysTable_Table*)(entry->m_systable_struct.get()))
151  ->tabid();
152  }
153 
154  std::shared_ptr<const TableDesc> FindTableDesc(Oid tabid);
155 
156  inline std::shared_ptr<const SysTable_Type>
157  FindType(Oid typid) {
158  auto entry =
162  typid);
163  if (!entry) {
164  return nullptr;
165  }
166  return static_pointer_cast<const SysTable_Type>(
167  entry->m_systable_struct);
168  }
169 
174  inline std::string
175  GetTypeName(Oid typid) {
176  auto typ = FindType(typid);
177  if (!typ.get()) {
178  return "INVALID_TYPE";
179  }
180  return typ->typname();
181  }
182 
183  inline std::shared_ptr<const SysTable_Function>
184  FindFunction(Oid funcid) {
185  auto entry =
189  funcid);
190  if (!entry) {
191  return nullptr;
192  }
193  return static_pointer_cast<const SysTable_Function>(
194  entry->m_systable_struct);
195  }
196 
197  inline Oid
198  FindFunctionByName(absl::string_view funcname) {
199  auto entry =
203  funcname);
204  if (!entry) {
205  return InvalidOid;
206  }
207  return ((const SysTable_Function*)(entry->m_systable_struct.get()))
208  ->funcid();
209  }
210 
211  std::shared_ptr<const SysTable_FunctionArgs> FindFunctionArgs(
212  Oid funcid, int16_t funcargid);
213 
214  inline std::shared_ptr<const SysTable_Index>
215  FindIndex(Oid idxid) {
216  auto entry =
220  idxid);
221  if (!entry) {
222  return nullptr;
223  }
224  return static_pointer_cast<const SysTable_Index>(
225  entry->m_systable_struct);
226  }
227 
228  inline Oid
229  FindIndexByName(absl::string_view idxname) {
230  auto entry =
232  InvalidOid,
234  idxname);
235  if (!entry) {
236  return InvalidOid;
237  }
238  return ((const SysTable_Index*)(entry->m_systable_struct.get()))
239  ->idxid();
240  }
241 
242  std::vector<Oid> FindAllIndexesOfTable(Oid idxtabid);
243 
244  std::shared_ptr<const IndexDesc> FindIndexDesc(Oid idxid);
245 
253  Oid FindOperator(OpType optype, Oid oparg0typid, Oid oparg1typid);
254 
268  Oid FindCast(Oid castoprtypid, Oid castrettypid, bool must_be_implicit);
269 
274  inline std::shared_ptr<const SysTable_Aggregation>
276  auto entry =
280  aggid);
281  if (!entry) {
282  return nullptr;
283  }
284  return static_pointer_cast<const SysTable_Aggregation>(
285  entry->m_systable_struct);
286  }
287 
299  Oid FindAggregationByNameAndOprType(absl::string_view aggname,
300  Oid aggoprtypid);
301 
322  Oid aggoprtypid);
323 
332  Oid systabid,
333  Oid idxid_hint,
334  FieldId oid_colid,
335  Oid oid);
336 
345  Oid systabid,
346  Oid idxid_hint,
347  FieldId name_colid,
348  absl::string_view name);
349 
350  template<bool expect_unique, size_t NPreds, bool no_cache>
352  using EntryPtr = typename std::conditional<
353  no_cache,
354  std::unique_ptr<CCLookupTableEntry>,
355  CCLookupTableEntry*>::type;
356  using RetType = typename std::conditional<
357  expect_unique,
358  EntryPtr,
359  std::vector<EntryPtr>>::type;
360 
361  template<class RHS0, class ...RemRHS>
362  inline static
363  typename std::enable_if<std::is_same<RHS0, RHS0>::value
364  && expect_unique, RetType>::type
366  Oid systabid,
367  Oid idxid_hint,
368  const std::array<FieldId, NPreds> &fieldid,
369  const std::array<Oid, NPreds> &eq_funcid,
370  RHS0&& rhs0,
371  RemRHS&& ...remrhs) {
372  return Impl(_this, systabid, idxid_hint, 0, fieldid, eq_funcid,
373  std::forward<RHS0>(rhs0), std::forward<RemRHS>(remrhs)...);
374  }
375 
376  template<class RHS0, class ...RemRHS>
377  inline static
378  typename std::enable_if<std::is_same<RHS0, RHS0>::value
379  && !expect_unique, RetType>::type
381  Oid systabid,
382  Oid idxid_hint,
383  size_t expect_n,
384  const std::array<FieldId, NPreds> &fieldid,
385  const std::array<Oid, NPreds> &eq_funcid,
386  RHS0&& rhs0,
387  RemRHS&& ...remrhs) {
388  return Impl(_this, systabid, idxid_hint, expect_n, fieldid,
389  eq_funcid, std::forward<RHS0>(rhs0),
390  std::forward<RemRHS>(remrhs)...);
391  }
392 
393  private:
394  template<class ...RHS>
395  static RetType
397  Oid systabid,
398  Oid idxid_hint,
399  size_t expect_n,
400  const std::array<FieldId, NPreds> &fieldid,
401  const std::array<Oid, NPreds> &eq_funcid,
402  RHS&& ...rhs);
403  };
404 
405  template<bool, size_t, bool> friend struct SearchForCatalogEntry;
406 
410  Oid AddTable(absl::string_view tabname,
411  std::vector<Oid> coltypid,
412  std::vector<uint64_t> coltypparam,
413  std::vector<std::string> field_names,
414  std::vector<bool> colisnullable,
415  std::vector<bool> colisarray,
416  FileId tabfid);
417 
427  Oid AddIndex(absl::string_view idxname,
428  Oid idxtabid,
429  IdxType idxtyp,
430  bool idxunique,
431  std::vector<FieldId> idxcoltabcolids,
432  FileId idxfid,
433  std::vector<Oid> idxcolltfuncids,
434  std::vector<Oid> idxcoleqfuncids);
435 private:
440  void CreateDBMeta();
441 
446  absl::flat_hash_map<Oid, FileId> LoadInitFile(
447  const std::string &init_datafile,
448  BootstrapCatCache *catcache);
449 
454  void FinishInitCatalog(const absl::flat_hash_map<Oid, FileId> &tabid2fid,
455  BootstrapCatCache *catcache);
456 
463 
473  void CheckIndexes(bool init);
474 
475  void BuildIndex(bool init, Oid idxid);
476 
483 
490  template<bool no_cache>
491  typename std::conditional<no_cache,
492  std::unique_ptr<CCLookupTableEntry>,
493  CCLookupTableEntry*>::type
495  RecordId recid,
496  const Schema *schema,
497  const char *buf);
498 
503  void InsertCatalogEntries(Oid systabid,
504  const std::vector<std::vector<Datum>> &data);
505 
508  //absl::flat_hash_map<Oid, CCLookupTableEntry> m_oid_lookup_table;
509  absl::flat_hash_map<RecordId, CCLookupTableEntry> m_recid_lookup_table;
510  absl::flat_hash_map<Oid, std::shared_ptr<TableDesc>> m_table_desc;
511  absl::flat_hash_map<Oid, std::shared_ptr<IndexDesc>> m_index_desc;
512 };
513 
514 } // namespace taco
515 
516 #endif // CATALOG_CATCACHEBASE_H
BootstrapCatCache stores hard-coded data needed to bootstrap the entire database catalog.
Definition: BootstrapCatCache.h:21
CatCacheBase implements the common routines and public interfaces for accessing and modifying the sys...
Definition: CatCacheBase.h:103
absl::flat_hash_map< Oid, std::shared_ptr< TableDesc > > m_table_desc
Definition: CatCacheBase.h:510
Oid FindAggregationByTidAndOprType(AggType aggtyp, Oid aggoprtypid)
Returns the aggregation ID for the aggregation with the given type and operand type.
std::shared_ptr< const IndexDesc > FindIndexDesc(Oid idxid)
Oid FindCast(Oid castoprtypid, Oid castrettypid, bool must_be_implicit)
Returns the cast function id of the one with the specific operand type and return type.
std::shared_ptr< const SysTable_Index > FindIndex(Oid idxid)
Definition: CatCacheBase.h:215
void InsertCatalogEntries(Oid systabid, const std::vector< std::vector< Datum >> &data)
Inserts the new catalog entries in data into the systable systabid and also update all its indexes.
std::shared_ptr< const SysTable_FunctionArgs > FindFunctionArgs(Oid funcid, int16_t funcargid)
bool m_use_index
Definition: CatCacheBase.h:507
CCLookupTableEntry * SearchForCatalogEntryByName(Oid systabid, Oid idxid_hint, FieldId name_colid, absl::string_view name)
Searches for the systable entry with its name column at name_colid equals to name.
std::vector< Oid > FindAllIndexesOfTable(Oid idxtabid)
Oid AllocateOid()
Allocates an object ID.
Oid FindOperator(OpType optype, Oid oparg0typid, Oid oparg1typid)
Returns the operator function id of the one with the specific operand types and operator type.
void InitializeFromInitData(const std::string &init_datafile)
Creates the catalog from an init data file and initializes the catalog cache.
absl::flat_hash_map< Oid, std::shared_ptr< IndexDesc > > m_index_desc
Definition: CatCacheBase.h:511
std::shared_ptr< const SysTable_Function > FindFunction(Oid funcid)
Definition: CatCacheBase.h:184
Oid FindIndexByName(absl::string_view idxname)
Definition: CatCacheBase.h:229
std::conditional< no_cache, std::unique_ptr< CCLookupTableEntry >, CCLookupTableEntry * >::type GetOrCreateCachedEntry(Oid systabid, RecordId recid, const Schema *schema, const char *buf)
Returns the catalog entry in the systable by its table id and record id.
void FinishInitCatalog(const absl::flat_hash_map< Oid, FileId > &tabid2fid, BootstrapCatCache *catcache)
Finishes the initialization of the catalog and updates the DB meta file if necessary.
Oid AddTable(absl::string_view tabname, std::vector< Oid > coltypid, std::vector< uint64_t > coltypparam, std::vector< std::string > field_names, std::vector< bool > colisnullable, std::vector< bool > colisarray, FileId tabfid)
Adds a table into the catalog.
bool m_initialized
Definition: CatCacheBase.h:506
void InitializeFromExistingData()
Initializes the catalog cache from the existing data.
void CreateDBMeta()
Creates and initializes the database meta file.
void LoadMinCache(BootstrapCatCache *catcache)
Loads the minimum set of the required catalog entries for initializing the cache.
std::shared_ptr< const SysTable_Aggregation > FindAggregation(Oid aggid)
Returns the aggregation systable struct for the aggregation with ID aggid.
Definition: CatCacheBase.h:275
absl::flat_hash_map< Oid, FileId > LoadInitFile(const std::string &init_datafile, BootstrapCatCache *catcache)
Loads the init catalog file and writes them into the catalog files.
std::shared_ptr< const SysTable_Type > FindType(Oid typid)
Definition: CatCacheBase.h:157
Oid FindAggregationByNameAndOprType(absl::string_view aggname, Oid aggoprtypid)
Returns the aggregation ID for the aggregation with the given name and aggregation operand type.
std::shared_ptr< const SysTable_Table > FindTable(Oid tabid)
Definition: CatCacheBase.h:127
CCLookupTableEntry * SearchForCatalogEntryByOid(Oid systabid, Oid idxid_hint, FieldId oid_colid, Oid oid)
Searches for the systable entry with ID recid in the given systable systabid.
absl::flat_hash_map< RecordId, CCLookupTableEntry > m_recid_lookup_table
Definition: CatCacheBase.h:509
constexpr bool IsInitialized() const
Returns if the catalog cache has been initialized.
Definition: CatCacheBase.h:122
void CheckIndexes(bool init)
Checks if any of the catalog indexes need to be initialized and/or rebuilt.
Oid AddIndex(absl::string_view idxname, Oid idxtabid, IdxType idxtyp, bool idxunique, std::vector< FieldId > idxcoltabcolids, FileId idxfid, std::vector< Oid > idxcolltfuncids, std::vector< Oid > idxcoleqfuncids)
Adds an index into the catalog.
std::shared_ptr< const TableDesc > FindTableDesc(Oid tabid)
Oid FindTableByName(absl::string_view tabname)
Definition: CatCacheBase.h:141
void BuildIndex(bool init, Oid idxid)
Oid FindFunctionByName(absl::string_view funcname)
Definition: CatCacheBase.h:198
std::string GetTypeName(Oid typid)
Returns the type name of the specified type if it exists.
Definition: CatCacheBase.h:175
Some internal functions of catalog cache implementations.
Definition: CatCacheBase.h:38
static std::shared_ptr< void > CreateSysTableStruct(Oid tabid, const std::vector< Datum > &data)
Creates a SysTable_xxx struct for a row stored in a record payout in the specified table.
Definition: CatCacheBase_gen.cpp:8
static T * CopySysTableStruct(const T *s)
Definition: CatCacheBase.h:67
static T * ConstructSysTableStruct(Args &&...args)
Constructs a SysTable_xxx struct from c++ values by invoking its constructor.
Definition: CatCacheBase.h:78
static T * CreateSysTableStruct(const std::vector< Datum > &data)
Creates a SysTable_xxx struct from a vector of data by invoking its Create() function.
Definition: CatCacheBase.h:61
std::vector< Datum > GetDatumVector(const T &systable_struct)
Definition: CatCacheBase.h:84
A Schema object stores the information for accessing an ordered set of typed fields either from a dis...
Definition: Schema.h:39
static constexpr FieldId aggid_colid()
Definition: Aggregation.h:143
SysTable_Function is an in-memory cached record in the system catalog SysTable_Function.
Definition: Function.h:16
static constexpr FieldId funcid_colid()
Definition: Function.h:99
static constexpr FieldId funcname_colid()
Definition: Function.h:114
SysTable_Index is an in-memory cached record in the system catalog SysTable_Index.
Definition: Index.h:16
static constexpr FieldId idxname_colid()
Definition: Index.h:162
static constexpr FieldId idxid_colid()
Definition: Index.h:132
SysTable_Table is an in-memory cached record in the system catalog SysTable_Table.
Definition: Table.h:16
static constexpr FieldId tabid_colid()
Definition: Table.h:121
static constexpr FieldId tabname_colid()
Definition: Table.h:146
static constexpr FieldId typid_colid()
Definition: Type.h:154
Definition: Record.h:148
constexpr const Oid IDX_Index_idxid
Definition: initoids.h:20
constexpr const Oid IDX_Function_funcid
Definition: initoids.h:29
constexpr const Oid IDX_Type_typid
Definition: initoids.h:38
constexpr const Oid TAB_Aggregation
Definition: initoids.h:18
constexpr const Oid IDX_Function_funcname
Definition: initoids.h:30
constexpr const Oid IDX_Table_tabname
Definition: initoids.h:37
constexpr const Oid TAB_Function
Definition: initoids.h:16
constexpr const Oid TAB_Table
Definition: initoids.h:12
constexpr const Oid TAB_Index
Definition: initoids.h:13
constexpr const Oid TAB_Type
Definition: initoids.h:19
constexpr const Oid IDX_Aggregation_aggid
Definition: initoids.h:22
constexpr const Oid IDX_Table_tabid
Definition: initoids.h:36
Definition: datum.h:28
constexpr Oid InvalidOid
Definition: tdb_base.h:224
uint8_t OpType
The operator type, see expr/optypes.h.
Definition: tdb_base.h:287
uint32_t Oid
Definition: tdb_base.h:210
uint8_t IdxType
The index type, see index/idxtyps.h.
Definition: tdb_base.h:284
int16_t FieldId
Definition: tdb_base.h:212
uint32_t FileId
The file ID.
Definition: tdb_base.h:221
uint8_t AggType
The aggregation type, see catalog/aggtyp.h.
Definition: tdb_base.h:295
This is an internal data structure of the catalog cache for storing an in-memory catalog table entry.
Definition: CatCacheBase.h:19
CCLookupTableEntry(RecordId recid, std::shared_ptr< void > systable_struct)
Definition: CatCacheBase.h:20
CCLookupTableEntry(CCLookupTableEntry &&e)=default
std::shared_ptr< void > m_systable_struct
Definition: CatCacheBase.h:30
CCLookupTableEntry & operator=(const CCLookupTableEntry &)=delete
RecordId m_recid
Definition: CatCacheBase.h:29
CCLookupTableEntry(const CCLookupTableEntry &)=delete
CCLookupTableEntry & operator=(CCLookupTableEntry &&e)=default
Definition: CatCacheBase.h:351
static std::enable_if< std::is_same< RHS0, RHS0 >::value &&expect_unique, RetType >::type Call(CatCacheBase *_this, Oid systabid, Oid idxid_hint, const std::array< FieldId, NPreds > &fieldid, const std::array< Oid, NPreds > &eq_funcid, RHS0 &&rhs0, RemRHS &&...remrhs)
Definition: CatCacheBase.h:365
typename std::conditional< expect_unique, EntryPtr, std::vector< EntryPtr > >::type RetType
Definition: CatCacheBase.h:359
static std::enable_if< std::is_same< RHS0, RHS0 >::value &&!expect_unique, RetType >::type Call(CatCacheBase *_this, Oid systabid, Oid idxid_hint, size_t expect_n, const std::array< FieldId, NPreds > &fieldid, const std::array< Oid, NPreds > &eq_funcid, RHS0 &&rhs0, RemRHS &&...remrhs)
Definition: CatCacheBase.h:380
static RetType Impl(CatCacheBase *_this, Oid systabid, Oid idxid_hint, size_t expect_n, const std::array< FieldId, NPreds > &fieldid, const std::array< Oid, NPreds > &eq_funcid, RHS &&...rhs)
typename std::conditional< no_cache, std::unique_ptr< CCLookupTableEntry >, CCLookupTableEntry * >::type EntryPtr
Definition: CatCacheBase.h:355
The record ID of a record on a page is a pair of ‘(PageNumber, SlotId)’.
Definition: Record.h:17
This header file includes all internal struct representation of system catalog records.