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 
51 public:
67  static void InitializePage(char *pagebuf, FieldOffset usr_data_sz = 0);
68 
76  VarlenDataPage(char *pagebuf);
77 
84  char* GetUserData() const;
85 
89  SlotId
90  GetMinSlotId() const {
91  return MinSlotId;
92  }
93 
104  SlotId GetMaxSlotId() const;
105 
113  SlotId
115  for (SlotId sid = GetMinSlotId(); sid <= GetMaxSlotId(); ++sid) {
116  if (IsOccupied(sid)) {
117  return sid;
118  }
119  }
120  return INVALID_SID;
121  }
122 
131  SlotId
133  return GetMaxSlotId();
134  }
135 
141  bool IsOccupied(SlotId sid) const;
142 
146  SlotId GetRecordCount() const;
147 
163  char *GetRecordBuffer(SlotId sid, FieldOffset *p_len) const;
164 
169  Record
170  GetRecord(SlotId sid) const {
171  Record rec;
172  rec.GetData() = GetRecordBuffer(sid, &rec.GetLength());
173  rec.GetRecordId().sid = sid;
174  return rec;
175  }
176 
188  bool InsertRecord(Record &rec);
189 
197  bool EraseRecord(SlotId sid);
198 
220  bool UpdateRecord(SlotId sid, Record &rec);
221 
238  bool InsertRecordAt(SlotId sid, Record &rec);
239 
251  void RemoveSlot(SlotId sid);
252 
276  void ShiftSlots(SlotId n, bool truncate);
277 
294  static FieldOffset ComputeFreeSpace(FieldOffset usr_data_sz,
295  SlotId num_recs,
296  FieldOffset total_reclen);
297 private:
305  inline void
306  CheckSID(SlotId sid) const {
307  if (sid < GetMinSlotId() || sid > GetMaxSlotId()) {
308  LOG(kError, "sid is out of range, got " SLOTID_FORMAT " but "
309  "expecting in [" SLOTID_FORMAT ", " SLOTID_FORMAT "]",
310  sid, GetMinSlotId(), GetMaxSlotId());
311  }
312  }
313 
314 
320  SlotData*
321  GetSlotArray() const {
322  return (SlotData*)(m_pagebuf + PAGE_SIZE);
323  }
324 
332  void PutRecordIntoSlot(SlotId sid, Record &rec);
333 
334  void CompactSpace();
335 
336  char *m_pagebuf;
337 };
338 
339 } // namespace taco
340 
341 #endif // STORAGE_VARLENDATAPAGE_H
Definition: Record.h:87
FieldOffset & GetLength()
Definition: Record.h:110
const char *& GetData()
Definition: Record.h:100
RecordId & GetRecordId()
Definition: Record.h:131
VarlenDataPage implements a buffered heap page that supports inserting, deleting, updating and random...
Definition: VarlenDataPage.h:50
SlotId GetMinOccupiedSlotId() const
Returns the slot ID of the first occupied slot on this page.
Definition: VarlenDataPage.h:114
bool InsertRecord(Record &rec)
Inserts the record into the page.
Definition: VarlenDataPage.cpp:143
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
void CheckSID(SlotId sid) const
Checks if sid is in the range of [GetMinSlotId(), GetMaxSlotId()].
Definition: VarlenDataPage.h:306
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
char * GetUserData() const
Returns the pointer to the user data area.
Definition: VarlenDataPage.cpp:104
bool IsOccupied(SlotId sid) const
Returns whether the slot sid is currently occupied.
Definition: VarlenDataPage.cpp:135
char * m_pagebuf
Definition: VarlenDataPage.h:336
bool InsertRecordAt(SlotId sid, Record &rec)
Inserts the record at a given sid in the range of [GetMinSlotId(), GetMaxSlotId() + 1].
Definition: VarlenDataPage.cpp:386
VarlenDataPage(char *pagebuf)
Constructs a VarlenDataPage object on a buffered page in pagebuf that was previously initialized by V...
Definition: VarlenDataPage.cpp:99
SlotId GetMaxSlotId() const
Returns the largest occupied slot ID sid currently on this page if there's any.
Definition: VarlenDataPage.cpp:109
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
void CompactSpace()
Definition: VarlenDataPage.cpp:345
SlotId GetMinSlotId() const
Returns the smallest valid slot ID.
Definition: VarlenDataPage.h:90
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:170
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
SlotId GetRecordCount() const
Returns the number of the occupied slots on this page.
Definition: VarlenDataPage.cpp:114
SlotData * GetSlotArray() const
Returns the beginning of the slot array, which grows backwards and its valid range is [-1....
Definition: VarlenDataPage.h:321
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
SlotId GetMaxOccupiedSlotId() const
Returns the slot of the last occupied slot on this page.
Definition: VarlenDataPage.h:132
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
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
bool EraseRecord(SlotId sid)
Erases a record from the page specified by the slot ID.
Definition: VarlenDataPage.cpp:222
#define LOG(level,...)
LOG(LogSeverity level, const char *fmt, ...)
Definition: logging.h:116
Definition: datum.h:28
constexpr LogSeverity kError
Definition: logging.h:21
uint16_t SlotId
Definition: tdb_base.h:222
constexpr SlotId INVALID_SID
The invalid slot ID.
Definition: tdb_base.h:254
int16_t FieldOffset
Definition: tdb_base.h:211
constexpr SlotId MinSlotId
The minimum valid slot ID.
Definition: tdb_base.h:259
SlotId sid
Definition: Record.h:23
Describes a slot.
Definition: VarlenDataPage.cpp:67
constexpr const size_t PAGE_SIZE
Definition: tdb_base.h:159
#define SLOTID_FORMAT
Definition: tdb_base.h:273