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 }
Definition: Record.h:87
FieldOffset m_buflen
Definition: Record.h:142
const char * m_buffer
Definition: Record.h:141
void Clear()
Definition: Record.h:120
bool IsValid() const
Definition: Record.h:126
FieldOffset & GetLength()
Definition: Record.h:110
const char * GetData() const
Definition: Record.h:105
const char *& GetData()
Definition: Record.h:100
RecordId m_rid
Definition: Record.h:143
const RecordId & GetRecordId() const
Definition: Record.h:136
Record & operator=(const Record &other)=default
RecordId & GetRecordId()
Definition: Record.h:131
Record(const Record &other)=default
Record(const maxaligned_char_buf &buf)
Definition: Record.h:93
FieldOffset GetLength() const
Definition: Record.h:115
Record(const char *buf, FieldOffset buflen)
Definition: Record.h:91
Record()
Definition: Record.h:89
Definition: Record.h:148
Definition: datum.h:28
constexpr bool operator==(const RecordId &rid1, const RecordId &rid2)
Definition: Record.h:50
std::ostream & operator<<(std::ostream &out, const RecordId &rid)
Definition: Record.h:82
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 bool operator!=(const RecordId &rid1, const RecordId &rid2)
Definition: Record.h:55
constexpr bool operator<=(const RecordId &rid1, const RecordId &rid2)
Definition: Record.h:65
uint32_t PageNumber
Definition: tdb_base.h:213
constexpr bool operator>(const RecordId &rid1, const RecordId &rid2)
Definition: Record.h:71
constexpr bool operator<(const RecordId &rid1, const RecordId &rid2)
Definition: Record.h:60
constexpr bool operator>=(const RecordId &rid1, const RecordId &rid2)
Definition: Record.h:76
constexpr PageNumber INVALID_PID
The invalid page number.
Definition: tdb_base.h:235
size_t operator()(const taco::RecordId &x) const
Definition: Record.h:151
The record ID of a record on a page is a pair of ‘(PageNumber, SlotId)’.
Definition: Record.h:17
SlotId sid
Definition: Record.h:23
RecordId(PageNumber pid)
Definition: Record.h:19
PageNumber pid
Definition: Record.h:22
constexpr bool IsValid() const
Definition: Record.h:35
RecordId()
Definition: Record.h:18
void SetInvalid()
Definition: Record.h:29
RecordId(PageNumber pid, SlotId sid)
Definition: Record.h:20
std::string ToString() const
Definition: Record.h:40
uint16_t reserved
2-byte padding for alignment, but it's currently unused.
Definition: Record.h:26
std::vector< char, AlignedAllocImpl::aligned_allocator< 8, char > > maxaligned_char_buf
Definition: tdb_base.h:155
#define PAGENUMBER_FORMAT
Definition: tdb_base.h:272
#define SLOTID_FORMAT
Definition: tdb_base.h:273