taco-db  0.1.0
VarlenDataPage.h
Go to the documentation of this file.
1 #ifndef STORAGE_VARLENDATAPAGE_H
2 #define STORAGE_VARLENDATAPAGE_H
3 
4 #include "tdb.h"
5 
6 #include "storage/Record.h"
7 
8 namespace taco {
9 
10 struct SlotData;
11 
12 static_assert(MinSlotId == 1);
13 
41 public:
57  static void InitializePage(char *pagebuf, FieldOffset usr_data_sz = 0);
58 
66  VarlenDataPage(char *pagebuf);
67 
74  char* GetUserData() const;
75 
79  constexpr SlotId
80  GetMinSlotId() const {
81  return MinSlotId;
82  }
83 
94  SlotId GetMaxSlotId() const;
95 
103  SlotId
105  for (SlotId sid = GetMinSlotId(); sid <= GetMaxSlotId(); ++sid) {
106  if (IsOccupied(sid)) {
107  return sid;
108  }
109  }
110  return INVALID_SID;
111  }
112 
121  SlotId
123  return GetMaxSlotId();
124  }
125 
131  bool IsOccupied(SlotId sid) const;
132 
136  SlotId GetRecordCount() const;
137 
153  char *GetRecordBuffer(SlotId sid, FieldOffset *p_len) const;
154 
159  Record
160  GetRecord(SlotId sid) const {
161  Record rec;
162  rec.GetData() = GetRecordBuffer(sid, &rec.GetLength());
163  rec.GetRecordId().sid = sid;
164  return rec;
165  }
166 
178  bool InsertRecord(Record &rec);
179 
187  bool EraseRecord(SlotId sid);
188 
210  bool UpdateRecord(SlotId sid, Record &rec);
211 
228  bool InsertRecordAt(SlotId sid, Record &rec);
229 
241  void RemoveSlot(SlotId sid);
242 
260  void ShiftSlots(SlotId n, bool truncate);
261 
278  static FieldOffset ComputeFreeSpace(FieldOffset usr_data_sz,
279  SlotId num_recs,
280  FieldOffset total_reclen);
281 private:
289  inline void
290  CheckSID(SlotId sid) const {
291  if (sid < GetMinSlotId() || sid > GetMaxSlotId()) {
292  LOG(kError, "sid is out of range, got " SLOTID_FORMAT " but "
293  "expecting in [" SLOTID_FORMAT ", " SLOTID_FORMAT "]",
294  sid, GetMinSlotId(), GetMaxSlotId());
295  }
296  }
297 
298 
304  constexpr SlotData*
305  GetSlotArray() const {
306  return (SlotData*)(m_pagebuf + PAGE_SIZE);
307  }
308 
316  void PutRecordIntoSlot(SlotId sid, Record &rec);
317 
318  void CompactSpace();
319 
320  char *m_pagebuf;
321 };
322 
323 } // namespace taco
324 
325 #endif // STORAGE_VARLENDATAPAGE_H
taco::VarlenDataPage::UpdateRecord
bool UpdateRecord(SlotId sid, Record &rec)
Updates the record at the specified slot in place to a new one rec on this page if there's enough spa...
Definition: VarlenDataPage.cpp:249
taco::FieldOffset
int16_t FieldOffset
Definition: tdb_base.h:212
taco::INVALID_SID
constexpr SlotId INVALID_SID
The invalid slot ID.
Definition: tdb_base.h:255
taco::VarlenDataPage
VarlenDataPage implements a buffered heap page that supports inserting, deleting, updating and random...
Definition: VarlenDataPage.h:40
PAGE_SIZE
constexpr const size_t PAGE_SIZE
Definition: tdb_base.h:159
taco::VarlenDataPage::ComputeFreeSpace
static FieldOffset ComputeFreeSpace(FieldOffset usr_data_sz, SlotId num_recs, FieldOffset total_reclen)
Computes the size of the free space on the page if there are num_recs records inserted to an empty Va...
Definition: VarlenDataPage.cpp:526
taco::VarlenDataPage::VarlenDataPage
VarlenDataPage(char *pagebuf)
Constructs a VarlenDataPage object on a buffered page in pagebuf that was previously initialized by V...
Definition: VarlenDataPage.cpp:99
taco::MinSlotId
constexpr SlotId MinSlotId
The minimum valid slot ID.
Definition: tdb_base.h:260
taco::VarlenDataPage::InitializePage
static void InitializePage(char *pagebuf, FieldOffset usr_data_sz=0)
Initializes a new (virtual) File page buffered in pagebuf as an empty variable-length data page.
Definition: VarlenDataPage.cpp:81
taco
Definition: datum.h:28
taco::VarlenDataPage::GetRecord
Record GetRecord(SlotId sid) const
Returns the record in the slot sid as a Record object, with its GetRecordId().sid set to sid and GetR...
Definition: VarlenDataPage.h:160
taco::SlotId
uint16_t SlotId
Definition: tdb_base.h:223
taco::kError
constexpr LogSeverity kError
Definition: logging.h:21
taco::VarlenDataPage::InsertRecordAt
bool InsertRecordAt(SlotId sid, Record &rec)
Inserts the record at a given sid in the range of [GetMinSlotId(), GetMaxSlotId() + 1].
Definition: VarlenDataPage.cpp:386
taco::VarlenDataPage::CheckSID
void CheckSID(SlotId sid) const
Checks if sid is in the range of [GetMinSlotId(), GetMaxSlotId()].
Definition: VarlenDataPage.h:290
taco::VarlenDataPage::GetRecordCount
SlotId GetRecordCount() const
Returns the number of the occupied slots on this page.
Definition: VarlenDataPage.cpp:114
taco::Record::GetLength
FieldOffset & GetLength()
Definition: Record.h:110
taco::VarlenDataPage::IsOccupied
bool IsOccupied(SlotId sid) const
Returns whether the slot sid is currently occupied.
Definition: VarlenDataPage.cpp:135
SLOTID_FORMAT
#define SLOTID_FORMAT
Definition: tdb_base.h:274
taco::RecordId::sid
SlotId sid
Definition: Record.h:23
taco::Record::GetData
const char *& GetData()
Definition: Record.h:100
tdb.h
taco::VarlenDataPage::GetRecordBuffer
char * GetRecordBuffer(SlotId sid, FieldOffset *p_len) const
Returns the record buffer of the slot sid and sets *p_len to the length of the record if p_len is not...
Definition: VarlenDataPage.cpp:119
taco::VarlenDataPage::GetMinOccupiedSlotId
SlotId GetMinOccupiedSlotId() const
Returns the slot ID of the first occupied slot on this page.
Definition: VarlenDataPage.h:104
taco::Record::GetRecordId
RecordId & GetRecordId()
Definition: Record.h:131
taco::VarlenDataPage::GetMaxOccupiedSlotId
SlotId GetMaxOccupiedSlotId() const
Returns the slot of the last occupied slot on this page.
Definition: VarlenDataPage.h:122
taco::VarlenDataPage::GetMinSlotId
constexpr SlotId GetMinSlotId() const
Returns the smallest valid slot ID.
Definition: VarlenDataPage.h:80
taco::SlotData
Describes a slot.
Definition: VarlenDataPage.cpp:67
taco::Record
Definition: Record.h:87
LOG
#define LOG(level,...)
LOG(LogSeverity level, const char *fmt, ...)
Definition: logging.h:116
taco::VarlenDataPage::m_pagebuf
char * m_pagebuf
Definition: VarlenDataPage.h:320
taco::VarlenDataPage::EraseRecord
bool EraseRecord(SlotId sid)
Erases a record from the page specified by the slot ID.
Definition: VarlenDataPage.cpp:222
taco::VarlenDataPage::GetSlotArray
constexpr SlotData * GetSlotArray() const
Returns the beginning of the slot array, which grows backwards and its valid range is [-1....
Definition: VarlenDataPage.h:305
taco::VarlenDataPage::PutRecordIntoSlot
void PutRecordIntoSlot(SlotId sid, Record &rec)
Copies a record into an unoccupied space at the beginning of the free space and stores the offset and...
Definition: VarlenDataPage.cpp:207
taco::VarlenDataPage::InsertRecord
bool InsertRecord(Record &rec)
Inserts the record into the page.
Definition: VarlenDataPage.cpp:143
taco::VarlenDataPage::GetMaxSlotId
SlotId GetMaxSlotId() const
Returns the largest occupied slot ID sid currently on this page if there's any.
Definition: VarlenDataPage.cpp:109
taco::VarlenDataPage::CompactSpace
void CompactSpace()
Definition: VarlenDataPage.cpp:345
taco::VarlenDataPage::RemoveSlot
void RemoveSlot(SlotId sid)
Erases a record at the given sid in the range of [GetMinSlotId(), GetMaxSlotId()] if it exists and mo...
Definition: VarlenDataPage.cpp:439
taco::VarlenDataPage::ShiftSlots
void ShiftSlots(SlotId n, bool truncate)
Shifts all the (occupied or unoccupied) slots to the right by n slots to truncate existing slots or t...
Definition: VarlenDataPage.cpp:467
Record.h
taco::VarlenDataPage::GetUserData
char * GetUserData() const
Returns the pointer to the user data area.
Definition: VarlenDataPage.cpp:104