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  Table*
252  GetTable() const {
253  return m_table;
254  }
255 
260  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
Definition: Record.h:87
bool IsValid() const
Definition: Record.h:126
RecordId & GetRecordId()
Definition: Record.h:131
Definition: logging.h:30
const std::string & GetMessage() const
Definition: logging.h:42
Definition: TableDesc.h:11
The Iterator interface for scanning the heap file.
Definition: Table.h:185
Iterator & operator=(Iterator &&it)=default
We can use the default move assignment only if EndScan does not do anything other than unpin the page...
bool Next()
Moves the iterator to the next record in the file.
Definition: Table.cpp:220
RecordId GetCurrentRecordId() const
Returns the record ID of the current record.
Definition: Table.h:269
void EndScan()
Ends the scan, which may perform operations that may throw errors such as releasing page pins.
Definition: Table.cpp:297
~Iterator()
Destructs an iterator.
Definition: Table.h:205
Record m_cur_record
The current record the iterator is on if the most recent Next() call returns true.
Definition: Table.h:318
Table * GetTable() const
Returns the table.
Definition: Table.h:252
Iterator(const Iterator &it)=delete
Deleted copy constructor.
bool IsAtValidRecord() const
Returns GetCurrentRecord().IsValid().
Definition: Table.h:277
Iterator(Iterator &&it)=default
Default move constructor.
const Record & GetCurrentRecord() const
Returns the current record, which is only valid when a previous Next() call returns true.
Definition: Table.h:261
Iterator & operator=(const Iterator &it)=delete
Deleted copy assignment.
Iterator()
Constructs an invalid iterator.
Definition: Table.h:190
ScopedBufferId m_pinned_bufid
A pin on the page where the current record is located.
Definition: Table.h:323
PageNumber m_lastpg_pid
Definition: Table.h:325
Table * m_table
The table that the iterator is iterating on.
Definition: Table.h:312
Table implements a heap file over the virtual file provided by the FileManager.
Definition: Table.h:33
PageNumber m_insertion_pid
The last known page possibly with empty space.
Definition: Table.h:171
~Table()
Destructor.
Definition: Table.cpp:38
void UpdateRecord(RecordId rid, Record &rec)
Updates the record specified by the record ID.
Definition: Table.cpp:155
void InsertRecord(Record &rec)
Inserts the record into table.
Definition: Table.cpp:43
Table(std::shared_ptr< const TableDesc > tabdesc, std::unique_ptr< File > file)
Definition: Table.h:77
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:32
static void Initialize(const TableDesc *tabdesc)
Inititalizes a table on a newly created virtual file.
Definition: Table.cpp:10
std::unique_ptr< File > m_file
An open file that stores the heap file for this table.
Definition: Table.h:165
std::shared_ptr< const TableDesc > m_tabdesc
The descriptor of the table.
Definition: Table.h:160
friend class Iterator
Definition: Table.h:330
Iterator StartScan()
Starts a scan of the table from the beginning.
Definition: Table.cpp:199
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:205
void EraseRecord(RecordId rid)
Erases the record specified by the record ID.
Definition: Table.cpp:121
const TableDesc * GetTableDesc() const
Returns the table descriptor of this table.
Definition: Table.h:95
#define LOG(level,...)
LOG(LogSeverity level, const char *fmt, ...)
Definition: logging.h:116
Definition: Record.h:148
Definition: datum.h:28
uint32_t PageNumber
Definition: tdb_base.h:213
constexpr LogSeverity kWarning
Definition: logging.h:20
constexpr PageNumber INVALID_PID
The invalid page number.
Definition: tdb_base.h:235
The record ID of a record on a page is a pair of ‘(PageNumber, SlotId)’.
Definition: Record.h:17