taco-db  0.1.0
Table.h
Go to the documentation of this file.
1 // storage/Table.h
2 #pragma once
3 
4 #include "tdb.h"
5 
6 #include <unordered_set>
7 
8 #include "catalog/TableDesc.h"
10 #include "storage/FileManager.h"
11 #include "storage/Record.h"
12 
13 namespace taco {
14 
15 class File;
16 
33 class Table {
34 public:
35  class Iterator;
36 
54  static void Initialize(const TableDesc *tabdesc);
55 
73  static std::unique_ptr<Table> Create(
74  std::shared_ptr<const TableDesc> tabdesc);
75 
76 private:
77  Table(std::shared_ptr<const TableDesc> tabdesc,
78  std::unique_ptr<File> file)
79  : m_tabdesc(std::move(tabdesc)),
80  m_file(std::move(file)),
82  }
83 
84 public:
85 
89  ~Table();
90 
94  const TableDesc*
95  GetTableDesc() const {
96  return m_tabdesc.get();
97  }
98 
112  void InsertRecord(Record& rec);
113 
124  void EraseRecord(RecordId rid);
125 
142  void UpdateRecord(RecordId rid, Record &rec);
143 
148 
154 
155 private:
156 
160  std::shared_ptr<const TableDesc> m_tabdesc;
161 
165  std::unique_ptr<File> m_file;
166 
172 
173 public:
174 
185  class Iterator {
186  public:
191  m_table(nullptr) {}
192 
193  private:
198  Iterator(Table* tbl, RecordId rid);
199 
200  public:
206  try {
207  if (GetTable()) {
208  EndScan();
209  }
210  } catch (const TDBError &e) {
211  // Logs a warning about errors in the end scan call, which is
212  // most likely because BufferManager::UnpinPage() throws some
213  // error caused by some bug in the Iterator. There's no
214  // sensible thing we could do here to mitigate the issue. If
215  // the page is still pinned, the buffer manager will throw a
216  // fatal error when the Database destroys it, which will
217  // provide greater details for debugging than calling
218  // std::terminate() here.
219  LOG(kWarning, "unable to destruct the iterator due to an error "
220  "in Table::Iterator::EndScan(): \n%s", e.GetMessage());
221  }
222  }
223 
227  Iterator(Iterator&& it) = default;
228 
235  Iterator &operator=(Iterator &&it) = default;
236 
240  Iterator(const Iterator& it) = delete;
241 
245  Iterator &operator=(const Iterator &it) = delete;
246 
251  constexpr Table*
252  GetTable() const {
253  return m_table;
254  }
255 
260  constexpr const Record&
262  return m_cur_record;
263  }
264 
268  RecordId
270  return m_cur_record.GetRecordId();
271  }
272 
276  bool
277  IsAtValidRecord() const {
278  return m_cur_record.IsValid();
279  }
280 
293  bool Next();
294 
308  void EndScan();
309 
310  private:
313 
319 
324 
326 
327  friend class Table;
328  };
329 
330  friend class Iterator;
331 };
332 
333 } // namespace taco
taco::Table::Iterator
friend class Iterator
Definition: Table.h:330
taco::Table
Table implements a heap file over the virtual file provided by the FileManager.
Definition: Table.h:33
taco::Table::StartScan
Iterator StartScan()
Starts a scan of the table from the beginning.
Definition: Table.cpp:205
taco::Table::InsertRecord
void InsertRecord(Record &rec)
Inserts the record into table.
Definition: Table.cpp:45
taco::TableDesc
Definition: TableDesc.h:11
taco::Table::m_insertion_pid
PageNumber m_insertion_pid
The last known page possibly with empty space.
Definition: Table.h:171
taco::TDBError
Definition: logging.h:30
taco::INVALID_PID
constexpr PageNumber INVALID_PID
The invalid page number.
Definition: tdb_base.h:236
taco::Table::Iterator::m_table
Table * m_table
The table that the iterator is iterating on.
Definition: Table.h:312
taco
Definition: datum.h:28
taco::Table::Iterator::m_cur_record
Record m_cur_record
The current record the iterator is on if the most recent Next() call returns true.
Definition: Table.h:318
taco::Table::StartScanFrom
Iterator StartScanFrom(RecordId rid)
Starts a scan of the table so that the first call to Iterator::Next() will return the first record wi...
Definition: Table.cpp:211
taco::Table::Table
Table(std::shared_ptr< const TableDesc > tabdesc, std::unique_ptr< File > file)
Definition: Table.h:77
taco::Table::GetTableDesc
const TableDesc * GetTableDesc() const
Returns the table descriptor of this table.
Definition: Table.h:95
taco::Table::m_file
std::unique_ptr< File > m_file
An open file that stores the heap file for this table.
Definition: Table.h:165
taco::kWarning
constexpr LogSeverity kWarning
Definition: logging.h:20
taco::Table::Iterator::GetTable
constexpr Table * GetTable() const
Returns the table.
Definition: Table.h:252
taco::RecordId
The record ID of a record on a page is a pair of ‘(PageNumber, SlotId)’.
Definition: Record.h:17
taco::Table::Iterator
The Iterator interface for scanning the heap file.
Definition: Table.h:185
taco::Table::Iterator::m_pinned_bufid
ScopedBufferId m_pinned_bufid
A pin on the page where the current record is located.
Definition: Table.h:323
TableDesc.h
taco::Table::Iterator::Iterator
Iterator()
Constructs an invalid iterator.
Definition: Table.h:190
tdb.h
taco::Table::Iterator::GetCurrentRecord
constexpr const Record & GetCurrentRecord() const
Returns the current record, which is only valid when a previous Next() call returns true.
Definition: Table.h:261
taco::Table::~Table
~Table()
Destructor.
Definition: Table.cpp:40
taco::Table::Iterator::EndScan
void EndScan()
Ends the scan, which may perform operations that may throw errors such as releasing page pins.
Definition: Table.cpp:307
taco::Table::UpdateRecord
void UpdateRecord(RecordId rid, Record &rec)
Updates the record specified by the record ID.
Definition: Table.cpp:167
taco::Table::Iterator::Next
bool Next()
Moves the iterator to the next record in the file.
Definition: Table.cpp:226
taco::Record::GetRecordId
RecordId & GetRecordId()
Definition: Record.h:131
taco::Record::IsValid
bool IsValid() const
Definition: Record.h:126
taco::ResourceGuard< BufferId, BufferUnpin, BufferId, INVALID_BUFID >
taco::Record
Definition: Record.h:87
LOG
#define LOG(level,...)
LOG(LogSeverity level, const char *fmt, ...)
Definition: logging.h:116
taco::Table::Iterator::IsAtValidRecord
bool IsAtValidRecord() const
Returns GetCurrentRecord().IsValid().
Definition: Table.h:277
std
Definition: Record.h:148
taco::Table::m_tabdesc
std::shared_ptr< const TableDesc > m_tabdesc
The descriptor of the table.
Definition: Table.h:160
taco::Table::Iterator::m_lastpg_pid
PageNumber m_lastpg_pid
Definition: Table.h:325
taco::Table::EraseRecord
void EraseRecord(RecordId rid)
Erases the record specified by the record ID.
Definition: Table.cpp:132
taco::Table::Create
static std::unique_ptr< Table > Create(std::shared_ptr< const TableDesc > tabdesc)
Constructs a table object on a heap file that was previously initialized by Table::Initialize().
Definition: Table.cpp:34
BufferManager.h
FileManager.h
taco::Table::Iterator::operator=
Iterator & operator=(Iterator &&it)=default
We can use the default move assignment only if EndScan does not do anything other than unpin the page...
Record.h
taco::TDBError::GetMessage
const std::string & GetMessage() const
Definition: logging.h:42
taco::Table::Iterator::~Iterator
~Iterator()
Destructs an iterator.
Definition: Table.h:205
taco::Table::Iterator::GetCurrentRecordId
RecordId GetCurrentRecordId() const
Returns the record ID of the current record.
Definition: Table.h:269
taco::Table::Initialize
static void Initialize(const TableDesc *tabdesc)
Inititalizes a table on a newly created virtual file.
Definition: Table.cpp:10
taco::PageNumber
uint32_t PageNumber
Definition: tdb_base.h:214