taco-db  0.1.0
Record.h
Go to the documentation of this file.
1 // storage/Record.h
2 #pragma once
3 
4 #include "tdb.h"
5 
6 #include <absl/strings/str_format.h>
7 #include <absl/strings/string_view.h>
8 #include <iostream>
9 
10 namespace taco {
11 
12 class Schema;
13 
17 struct RecordId {
21 
24 
26  uint16_t reserved;
27 
28  void
30  pid = INVALID_PID;
31  sid = INVALID_SID;
32  }
33 
34  constexpr bool
35  IsValid() const {
36  return pid != INVALID_PID && sid != INVALID_SID;
37  }
38 
39  std::string
40  ToString() const {
41  return absl::StrFormat("(" PAGENUMBER_FORMAT ", " SLOTID_FORMAT ")",
42  pid, sid);
43  }
44 };
45 
46 // The size of RecordId should be currently exactly 8 bytes.
47 static_assert(sizeof(RecordId) == 8);
48 
49 constexpr inline bool
50 operator==(const RecordId &rid1, const RecordId &rid2) {
51  return rid1.pid == rid2.pid && rid1.sid == rid2.sid;
52 }
53 
54 constexpr inline bool
55 operator!=(const RecordId &rid1, const RecordId &rid2) {
56  return rid1.pid != rid2.pid || rid1.sid != rid2.sid;
57 }
58 
59 constexpr inline bool
60 operator<(const RecordId &rid1, const RecordId &rid2) {
61  return rid1.pid < rid2.pid || (rid1.pid == rid2.pid && rid1.sid < rid2.sid);
62 }
63 
64 constexpr inline bool
65 operator<=(const RecordId &rid1, const RecordId &rid2) {
66  return rid1.pid < rid2.pid || (rid1.pid == rid2.pid &&
67  rid1.sid <= rid2.sid);
68 }
69 
70 constexpr inline bool
71 operator>(const RecordId &rid1, const RecordId &rid2) {
72  return rid1.pid > rid2.pid || (rid1.pid == rid2.pid && rid1.sid > rid2.sid);
73 }
74 
75 constexpr inline bool
76 operator>=(const RecordId &rid1, const RecordId &rid2) {
77  return rid1.pid > rid2.pid || (rid1.pid == rid2.pid &&
78  rid1.sid >= rid2.sid);
79 }
80 
81 inline std::ostream&
82 operator<<(std::ostream &out, const RecordId &rid) {
83  out << rid.ToString();
84  return out;
85 }
86 
87 class Record {
88 public:
89  Record():
90  m_buffer(nullptr), m_buflen(0) {}
91  Record(const char* buf, FieldOffset buflen):
92  m_buffer(buf), m_buflen(buflen) {}
94  m_buffer(buf.data()), m_buflen(buf.size()) {}
95 
96  Record(const Record& other) = default;
97  Record& operator=(const Record& other) = default;
98 
99  const char*&
101  return m_buffer;
102  }
103 
104  const char*
105  GetData() const {
106  return m_buffer;
107  }
108 
109  FieldOffset&
111  return m_buflen;
112  }
113 
115  GetLength() const {
116  return m_buflen;
117  }
118 
119  void
120  Clear() {
121  m_buffer = nullptr;
122  m_rid.SetInvalid();
123  }
124 
125  bool
126  IsValid() const {
127  return m_buffer != NULL;
128  }
129 
130  RecordId&
132  return m_rid;
133  }
134 
135  const RecordId&
136  GetRecordId() const {
137  return m_rid;
138  }
139 
140 private:
141  const char* m_buffer;
144 };
145 
146 } // namespace taco
147 
148 namespace std {
149  template<> struct hash<taco::RecordId> {
150  size_t
151  operator()(const taco::RecordId& x) const {
152  return std::hash<uint64_t>()(((uint64_t)x.pid << 32) + x.sid);
153  }
154  };
155 }
taco::RecordId::RecordId
RecordId(PageNumber pid, SlotId sid)
Definition: Record.h:20
taco::Record::Record
Record()
Definition: Record.h:89
taco::Record::Clear
void Clear()
Definition: Record.h:120
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::INVALID_PID
constexpr PageNumber INVALID_PID
The invalid page number.
Definition: tdb_base.h:236
taco::RecordId::reserved
uint16_t reserved
2-byte padding for alignment, but it's currently unused.
Definition: Record.h:26
taco
Definition: datum.h:28
std::hash< taco::RecordId >::operator()
size_t operator()(const taco::RecordId &x) const
Definition: Record.h:151
taco::Record::GetLength
FieldOffset GetLength() const
Definition: Record.h:115
taco::operator>
constexpr bool operator>(const RecordId &rid1, const RecordId &rid2)
Definition: Record.h:71
taco::SlotId
uint16_t SlotId
Definition: tdb_base.h:223
taco::operator<
constexpr bool operator<(const RecordId &rid1, const RecordId &rid2)
Definition: Record.h:60
taco::RecordId::IsValid
constexpr bool IsValid() const
Definition: Record.h:35
taco::Record::GetLength
FieldOffset & GetLength()
Definition: Record.h:110
taco::operator<<
std::ostream & operator<<(std::ostream &out, const RecordId &rid)
Definition: Record.h:82
taco::RecordId
The record ID of a record on a page is a pair of ‘(PageNumber, SlotId)’.
Definition: Record.h:17
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::Record::GetRecordId
const RecordId & GetRecordId() const
Definition: Record.h:136
taco::RecordId::RecordId
RecordId(PageNumber pid)
Definition: Record.h:19
taco::Record::GetRecordId
RecordId & GetRecordId()
Definition: Record.h:131
taco::Record::IsValid
bool IsValid() const
Definition: Record.h:126
taco::Record::m_rid
RecordId m_rid
Definition: Record.h:143
maxaligned_char_buf
std::vector< char, AlignedAllocImpl::aligned_allocator< 8, char > > maxaligned_char_buf
Definition: tdb_base.h:155
PAGENUMBER_FORMAT
#define PAGENUMBER_FORMAT
Definition: tdb_base.h:273
taco::Record::m_buflen
FieldOffset m_buflen
Definition: Record.h:142
taco::RecordId::SetInvalid
void SetInvalid()
Definition: Record.h:29
taco::operator!=
constexpr bool operator!=(const RecordId &rid1, const RecordId &rid2)
Definition: Record.h:55
taco::operator>=
constexpr bool operator>=(const RecordId &rid1, const RecordId &rid2)
Definition: Record.h:76
taco::Record
Definition: Record.h:87
taco::operator<=
constexpr bool operator<=(const RecordId &rid1, const RecordId &rid2)
Definition: Record.h:65
taco::RecordId::RecordId
RecordId()
Definition: Record.h:18
std
Definition: Record.h:148
taco::Record::Record
Record(const char *buf, FieldOffset buflen)
Definition: Record.h:91
taco::Record::operator=
Record & operator=(const Record &other)=default
taco::Record::m_buffer
const char * m_buffer
Definition: Record.h:141
taco::Record::GetData
const char * GetData() const
Definition: Record.h:105
taco::RecordId::pid
PageNumber pid
Definition: Record.h:22
taco::RecordId::ToString
std::string ToString() const
Definition: Record.h:40
taco::operator==
constexpr bool operator==(const RecordId &rid1, const RecordId &rid2)
Definition: Record.h:50
taco::Record::Record
Record(const maxaligned_char_buf &buf)
Definition: Record.h:93
taco::PageNumber
uint32_t PageNumber
Definition: tdb_base.h:214