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 
24  CCLookupTableEntry(const CCLookupTableEntry&) = delete;
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:
105  CatCacheBase();
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 
254  Oid FindOperator(OpType optype, Oid oparg0typid, Oid oparg1typid);
255 
269  Oid FindCast(Oid castoprtypid, Oid castrettypid, bool must_be_implicit);
270 
275  inline std::shared_ptr<const SysTable_Aggregation>
277  auto entry =
281  aggid);
282  if (!entry) {
283  return nullptr;
284  }
285  return static_pointer_cast<const SysTable_Aggregation>(
286  entry->m_systable_struct);
287  }
288 
300  Oid FindAggregationByNameAndOprType(absl::string_view aggname,
301  Oid aggoprtypid);
302 
323  Oid aggoprtypid);
324 
333  Oid systabid,
334  Oid idxid_hint,
335  FieldId oid_colid,
336  Oid oid);
337 
346  Oid systabid,
347  Oid idxid_hint,
348  FieldId name_colid,
349  absl::string_view name);
350 
351  template<bool expect_unique, size_t NPreds, bool no_cache>
353  using EntryPtr = typename std::conditional<
354  no_cache,
355  std::unique_ptr<CCLookupTableEntry>,
357  using RetType = typename std::conditional<
358  expect_unique,
359  EntryPtr,
360  std::vector<EntryPtr>>::type;
361 
362  template<class RHS0, class ...RemRHS>
363  inline static
364  typename std::enable_if<std::is_same<RHS0, RHS0>::value
365  && expect_unique, RetType>::type
367  Oid systabid,
368  Oid idxid_hint,
369  const std::array<FieldId, NPreds> &fieldid,
370  const std::array<Oid, NPreds> &eq_funcid,
371  RHS0&& rhs0,
372  RemRHS&& ...remrhs) {
373  return Impl(_this, systabid, idxid_hint, 0, fieldid, eq_funcid,
374  std::forward<RHS0>(rhs0), std::forward<RemRHS>(remrhs)...);
375  }
376 
377  template<class RHS0, class ...RemRHS>
378  inline static
379  typename std::enable_if<std::is_same<RHS0, RHS0>::value
380  && !expect_unique, RetType>::type
382  Oid systabid,
383  Oid idxid_hint,
384  size_t expect_n,
385  const std::array<FieldId, NPreds> &fieldid,
386  const std::array<Oid, NPreds> &eq_funcid,
387  RHS0&& rhs0,
388  RemRHS&& ...remrhs) {
389  return Impl(_this, systabid, idxid_hint, expect_n, fieldid,
390  eq_funcid, std::forward<RHS0>(rhs0),
391  std::forward<RemRHS>(remrhs)...);
392  }
393 
394  private:
395  template<class ...RHS>
396  static RetType
397  Impl(CatCacheBase *_this,
398  Oid systabid,
399  Oid idxid_hint,
400  size_t expect_n,
401  const std::array<FieldId, NPreds> &fieldid,
402  const std::array<Oid, NPreds> &eq_funcid,
403  RHS&& ...rhs);
404  };
405 
406  template<bool, size_t, bool> friend struct SearchForCatalogEntry;
407 
411  Oid AddTable(absl::string_view tabname,
412  std::vector<Oid> coltypid,
413  std::vector<uint64_t> coltypparam,
414  std::vector<std::string> field_names,
415  std::vector<bool> colisnullable,
416  std::vector<bool> colisarray,
417  FileId tabfid);
418 
428  Oid AddIndex(absl::string_view idxname,
429  Oid idxtabid,
430  IdxType idxtyp,
431  bool idxunique,
432  std::vector<FieldId> idxcoltabcolids,
433  FileId idxfid,
434  std::vector<Oid> idxcolltfuncids,
435  std::vector<Oid> idxcoleqfuncids);
436 private:
441  void CreateDBMeta();
442 
447  absl::flat_hash_map<Oid, FileId> LoadInitFile(
448  const std::string &init_datafile,
449  BootstrapCatCache *catcache);
450 
455  void FinishInitCatalog(const absl::flat_hash_map<Oid, FileId> &tabid2fid,
456  BootstrapCatCache *catcache);
457 
463  void LoadMinCache(BootstrapCatCache *catcache);
464 
474  void CheckIndexes(bool init);
475 
476  void BuildIndex(bool init, Oid idxid);
477 
483  Oid AllocateOid();
484 
491  template<bool no_cache>
492  typename std::conditional<no_cache,
493  std::unique_ptr<CCLookupTableEntry>,
494  CCLookupTableEntry*>::type
495  GetOrCreateCachedEntry(Oid systabid,
496  RecordId recid,
497  const Schema *schema,
498  const char *buf);
499 
504  void InsertCatalogEntries(Oid systabid,
505  const std::vector<std::vector<Datum>> &data);
506 
509  //absl::flat_hash_map<Oid, CCLookupTableEntry> m_oid_lookup_table;
510  absl::flat_hash_map<RecordId, CCLookupTableEntry> m_recid_lookup_table;
511  absl::flat_hash_map<Oid, std::shared_ptr<TableDesc>> m_table_desc;
512  absl::flat_hash_map<Oid, std::shared_ptr<IndexDesc>> m_index_desc;
513 };
514 
515 } // namespace taco
516 
517 #endif // CATALOG_CATCACHEBASE_H
taco::initoids::TAB_Type
constexpr const Oid TAB_Type
Definition: initoids.h:19
taco::SysTable_Function
SysTable_Function is an in-memory cached record in the system catalog SysTable_Function.
Definition: Function.h:16
taco::CatCacheInternalAccess::CreateSysTableStruct
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
taco::OpType
uint8_t OpType
The operator type, see expr/optypes.h.
Definition: tdb_base.h:288
taco::IdxType
uint8_t IdxType
The index type, see index/idxtyps.h.
Definition: tdb_base.h:285
taco::initoids::IDX_Table_tabid
constexpr const Oid IDX_Table_tabid
Definition: initoids.h:36
taco::CatCacheBase::AddIndex
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.
taco::CatCacheBase::FindOperator
Oid FindOperator(OpType optype, Oid oparg0typid, Oid oparg1typid)
Returns the operator function id of the one with the specific operand types and operator type.
taco::CatCacheBase::IsInitialized
constexpr bool IsInitialized() const
Returns if the catalog cache has been initialized.
Definition: CatCacheBase.h:122
taco::SysTable_Table::tabid_colid
static constexpr FieldId tabid_colid()
Definition: Table.h:121
taco::CatCacheBase::m_initialized
bool m_initialized
Definition: CatCacheBase.h:507
taco::CCLookupTableEntry::operator=
CCLookupTableEntry & operator=(const CCLookupTableEntry &)=delete
taco::CatCacheInternalAccess::CopySysTableStruct
static T * CopySysTableStruct(const T *s)
Definition: CatCacheBase.h:67
taco::SysTable_Function::funcname_colid
static constexpr FieldId funcname_colid()
Definition: Function.h:114
taco::initoids::IDX_Table_tabname
constexpr const Oid IDX_Table_tabname
Definition: initoids.h:37
taco::CatCacheBase::m_use_index
bool m_use_index
Definition: CatCacheBase.h:508
taco::CatCacheBase::GetTypeName
std::string GetTypeName(Oid typid)
Returns the type name of the specified type if it exists.
Definition: CatCacheBase.h:175
taco::CatCacheBase::SearchForCatalogEntry::Call
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:381
taco
Definition: datum.h:28
taco::initoids::IDX_Index_idxid
constexpr const Oid IDX_Index_idxid
Definition: initoids.h:20
taco::CatCacheInternalAccess::ConstructSysTableStruct
static T * ConstructSysTableStruct(Args &&...args)
Constructs a SysTable_xxx struct from c++ values by invoking its constructor.
Definition: CatCacheBase.h:78
taco::initoids::IDX_Type_typid
constexpr const Oid IDX_Type_typid
Definition: initoids.h:38
taco::CatCacheBase::AllocateOid
Oid AllocateOid()
Allocates an object ID.
taco::CatCacheBase::FindFunction
std::shared_ptr< const SysTable_Function > FindFunction(Oid funcid)
Definition: CatCacheBase.h:184
taco::SysTable_Function::funcid_colid
static constexpr FieldId funcid_colid()
Definition: Function.h:99
taco::SysTable_Index
SysTable_Index is an in-memory cached record in the system catalog SysTable_Index.
Definition: Index.h:16
taco::CatCacheBase::AddTable
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.
taco::initoids::TAB_Index
constexpr const Oid TAB_Index
Definition: initoids.h:13
taco::CatCacheBase::FindIndexDesc
std::shared_ptr< const IndexDesc > FindIndexDesc(Oid idxid)
taco::CatCacheBase::SearchForCatalogEntry
Definition: CatCacheBase.h:352
taco::CatCacheBase::SearchForCatalogEntry
friend struct SearchForCatalogEntry
Definition: CatCacheBase.h:406
taco::CCLookupTableEntry::CCLookupTableEntry
CCLookupTableEntry(RecordId recid, std::shared_ptr< void > systable_struct)
Definition: CatCacheBase.h:20
taco::CatCacheBase::m_recid_lookup_table
absl::flat_hash_map< RecordId, CCLookupTableEntry > m_recid_lookup_table
Definition: CatCacheBase.h:510
taco::initoids::TAB_Function
constexpr const Oid TAB_Function
Definition: initoids.h:16
taco::CatCacheBase::SearchForCatalogEntry::Impl
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)
taco::FieldId
int16_t FieldId
Definition: tdb_base.h:213
taco::CatCacheBase::FinishInitCatalog
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.
taco::CatCacheInternalAccess
Some internal functions of catalog cache implementations.
Definition: CatCacheBase.h:38
taco::CatCacheBase::FindTableDesc
std::shared_ptr< const TableDesc > FindTableDesc(Oid tabid)
taco::SysTable_Index::idxname_colid
static constexpr FieldId idxname_colid()
Definition: Index.h:162
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
TableDesc.h
taco::CatCacheBase::CatCacheBase
CatCacheBase()
taco::CatCacheBase::BuildIndex
void BuildIndex(bool init, Oid idxid)
tdb.h
taco::initoids::IDX_Aggregation_aggid
constexpr const Oid IDX_Aggregation_aggid
Definition: initoids.h:22
taco::CatCacheBase::SearchForCatalogEntry::EntryPtr
typename std::conditional< no_cache, std::unique_ptr< CCLookupTableEntry >, CCLookupTableEntry * >::type EntryPtr
Definition: CatCacheBase.h:356
taco::initoids::TAB_Aggregation
constexpr const Oid TAB_Aggregation
Definition: initoids.h:18
taco::CatCacheBase::InsertCatalogEntries
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.
systables.h
taco::SysTable_Table::tabname_colid
static constexpr FieldId tabname_colid()
Definition: Table.h:146
taco::CatCacheBase::FindType
std::shared_ptr< const SysTable_Type > FindType(Oid typid)
Definition: CatCacheBase.h:157
taco::SysTable_Type::typid_colid
static constexpr FieldId typid_colid()
Definition: Type.h:154
taco::initoids::IDX_Function_funcid
constexpr const Oid IDX_Function_funcid
Definition: initoids.h:29
taco::CatCacheBase::CheckIndexes
void CheckIndexes(bool init)
Checks if any of the catalog indexes need to be initialized and/or rebuilt.
taco::CCLookupTableEntry::m_systable_struct
std::shared_ptr< void > m_systable_struct
Definition: CatCacheBase.h:30
taco::CatCacheBase::m_index_desc
absl::flat_hash_map< Oid, std::shared_ptr< IndexDesc > > m_index_desc
Definition: CatCacheBase.h:512
taco::FileId
uint32_t FileId
The file ID.
Definition: tdb_base.h:222
taco::CatCacheBase::FindIndexByName
Oid FindIndexByName(absl::string_view idxname)
Definition: CatCacheBase.h:229
taco::SysTable_Table
SysTable_Table is an in-memory cached record in the system catalog SysTable_Table.
Definition: Table.h:16
taco::CatCacheBase::InitializeFromInitData
void InitializeFromInitData(const std::string &init_datafile)
Creates the catalog from an init data file and initializes the catalog cache.
taco::BootstrapCatCache
BootstrapCatCache stores hard-coded data needed to bootstrap the entire database catalog.
Definition: BootstrapCatCache.h:21
taco::CatCacheBase::FindAggregation
std::shared_ptr< const SysTable_Aggregation > FindAggregation(Oid aggid)
Returns the aggregation systable struct for the aggregation with ID aggid.
Definition: CatCacheBase.h:276
taco::CatCacheBase::SearchForCatalogEntryByOid
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.
taco::CatCacheBase::m_table_desc
absl::flat_hash_map< Oid, std::shared_ptr< TableDesc > > m_table_desc
Definition: CatCacheBase.h:511
taco::AggType
uint8_t AggType
The aggregation type, see catalog/aggtyp.h.
Definition: tdb_base.h:296
taco::CatCacheBase::FindFunctionByName
Oid FindFunctionByName(absl::string_view funcname)
Definition: CatCacheBase.h:198
taco::Oid
uint32_t Oid
Definition: tdb_base.h:211
taco::CatCacheBase::SearchForCatalogEntryByName
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.
taco::CatCacheBase::FindTableByName
Oid FindTableByName(absl::string_view tabname)
Definition: CatCacheBase.h:141
taco::CCLookupTableEntry::m_recid
RecordId m_recid
Definition: CatCacheBase.h:29
std
Definition: Record.h:148
taco::CatCacheBase::FindAggregationByTidAndOprType
Oid FindAggregationByTidAndOprType(AggType aggtyp, Oid aggoprtypid)
Returns the aggregation ID for the aggregation with the given type and operand type.
taco::CatCacheBase::FindIndex
std::shared_ptr< const SysTable_Index > FindIndex(Oid idxid)
Definition: CatCacheBase.h:215
IndexDesc.h
taco::CatCacheBase::FindFunctionArgs
std::shared_ptr< const SysTable_FunctionArgs > FindFunctionArgs(Oid funcid, int16_t funcargid)
taco::CatCacheBase::FindCast
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.
taco::InvalidOid
constexpr Oid InvalidOid
Definition: tdb_base.h:225
taco::CatCacheBase::SearchForCatalogEntry::RetType
typename std::conditional< expect_unique, EntryPtr, std::vector< EntryPtr > >::type RetType
Definition: CatCacheBase.h:360
taco::CatCacheBase::FindAggregationByNameAndOprType
Oid FindAggregationByNameAndOprType(absl::string_view aggname, Oid aggoprtypid)
Returns the aggregation ID for the aggregation with the given name and aggregation operand type.
taco::CatCacheBase::LoadMinCache
void LoadMinCache(BootstrapCatCache *catcache)
Loads the minimum set of the required catalog entries for initializing the cache.
taco::initoids::TAB_Table
constexpr const Oid TAB_Table
Definition: initoids.h:12
taco::SysTable_Index::idxid_colid
static constexpr FieldId idxid_colid()
Definition: Index.h:132
taco::CatCacheBase::LoadInitFile
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.
taco::CatCacheInternalAccess::CreateSysTableStruct
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
taco::initoids::IDX_Function_funcname
constexpr const Oid IDX_Function_funcname
Definition: initoids.h:30
taco::CatCacheBase::GetOrCreateCachedEntry
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.
taco::SysTable_Aggregation::aggid_colid
static constexpr FieldId aggid_colid()
Definition: Aggregation.h:143
taco::CCLookupTableEntry
This is an internal data structure of the catalog cache for storing an in-memory catalog table entry.
Definition: CatCacheBase.h:19
Record.h
taco::CatCacheBase::FindAllIndexesOfTable
std::vector< Oid > FindAllIndexesOfTable(Oid idxtabid)
taco::CatCacheBase
CatCacheBase implements the common routines and public interfaces for accessing and modifying the sys...
Definition: CatCacheBase.h:103
taco::CatCacheBase::SearchForCatalogEntry::Call
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:366
taco::CatCacheBase::InitializeFromExistingData
void InitializeFromExistingData()
Initializes the catalog cache from the existing data.
taco::CatCacheBase::CreateDBMeta
void CreateDBMeta()
Creates and initializes the database meta file.
taco::CatCacheBase::FindTable
std::shared_ptr< const SysTable_Table > FindTable(Oid tabid)
Definition: CatCacheBase.h:127
taco::CatCacheInternalAccess::GetDatumVector
std::vector< Datum > GetDatumVector(const T &systable_struct)
Definition: CatCacheBase.h:84