-
Notifications
You must be signed in to change notification settings - Fork 6
/
records.go
211 lines (169 loc) · 4.95 KB
/
records.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package fbptree
import (
"fmt"
"math"
)
const maxRecordSize = math.MaxUint32
// records is an abstraction over the pages that
// allows to gather pages into the records of the variable size.
type records struct {
pager *pager
}
// newRecords instantiates new instance of the records.
func newRecords(pager *pager) *records {
return &records{pager}
}
// new instantiates new record and returns its identifier or error.
func (r *records) new() (uint32, error) {
newPageId, err := r.pager.new()
if err != nil {
return 0, fmt.Errorf("failed to instantiate the first block page: %w", err)
}
return newPageId, nil
}
// write writes record and accepts variable data length, in case if data
// length is larger than page size, it will require more pages and update them.
func (r *records) write(recordId uint32, data []byte) error {
recordSize := len(data)
if recordSize >= maxRecordSize {
return fmt.Errorf("the record size must be less than %d", maxRecordSize)
}
pageData, err := r.pager.read(recordId)
if err != nil {
return fmt.Errorf("failed to read the initial record page %d: %w", recordId, err)
}
nextId := nextRecordId(pageData)
freeNextPage := true
writeSize := recordSize
if recordSize > (len(pageData) - 16) {
freeNextPage = false
writeSize = len(pageData) - 16
}
written := writeSize
if freeNextPage {
clearNextRecordId(pageData)
}
copy(pageData[8:16], encodeUint32(uint32(recordSize)))
copy(pageData[16:], data[0:writeSize])
var newPageId uint32
if nextId == 0 && written < recordSize {
newPageId, err = r.pager.new()
if err != nil {
return fmt.Errorf("failed to initialize new page: %w", err)
}
setNextRecordId(pageData, newPageId)
}
if err := r.pager.write(recordId, pageData); err != nil {
return fmt.Errorf("failed to write the page data for page %d: %w", recordId, err)
}
for nextId != 0 {
pageId := nextId
pageData, err := r.pager.read(pageId)
if err != nil {
return fmt.Errorf("failed to read page %d: %w", nextId, err)
}
nextId = nextRecordId(pageData)
if freeNextPage {
if err := r.pager.free(pageId); err != nil {
return fmt.Errorf("failed to free page %d: %w", pageId, err)
}
continue
}
if written < recordSize {
toWrite := recordSize - written
if toWrite > (len(pageData) - 8) {
toWrite = len(pageData) - 8
}
copy(pageData[8:], data[written:written+toWrite])
written += toWrite
}
freeNextPage = written >= recordSize
if freeNextPage {
clearNextRecordId(pageData)
}
if nextId == 0 && written < recordSize {
newPageId, err = r.pager.new()
if err != nil {
return fmt.Errorf("failed to initialize new page: %w", err)
}
setNextRecordId(pageData, newPageId)
}
if err := r.pager.write(pageId, pageData); err != nil {
return fmt.Errorf("failed to write page %d: %w", pageId, err)
}
}
for written < recordSize {
pageId := newPageId
pageData := make([]byte, r.pager.pageSize)
toWrite := recordSize - written
if toWrite > (len(pageData) - 8) {
toWrite = len(pageData) - 8
}
copy(pageData[8:], data[written:written+toWrite])
written += toWrite
if written < recordSize {
newPageId, err = r.pager.new()
if err != nil {
return fmt.Errorf("failed to initialize new page: %w", err)
}
setNextRecordId(pageData, newPageId)
}
if err := r.pager.write(pageId, pageData); err != nil {
return fmt.Errorf("failed to write page %d: %w", newPageId, err)
}
}
return nil
}
func reset(data []byte) {
for i := 0; i < len(data); i++ {
data[i] = 0
}
}
// Free frees all pages used by the record.
func (r *records) free(recordId uint32) error {
nextId := recordId
for nextId != 0 {
pageId := nextId
data, err := r.pager.read(pageId)
if err != nil {
return fmt.Errorf("failed to read record page %d: %w", pageId, err)
}
nextId = nextRecordId(data)
err = r.pager.free(pageId)
if err != nil {
return fmt.Errorf("failed to free page %d: %w", pageId, err)
}
}
return nil
}
// read reads all the data in the record pages and returns it. It is not aligned
// to the page size.
func (r *records) read(recordId uint32) ([]byte, error) {
data, err := r.pager.read(recordId)
if err != nil {
return nil, fmt.Errorf("failed to read initial record page: %w", err)
}
recordData := make([]byte, recordSize(data))
copy(recordData, data[16:])
for nextId, pageCount := nextRecordId(data), 1; nextId != 0; nextId, pageCount = nextRecordId(data), pageCount+1 {
data, err = r.pager.read(nextId)
if err != nil {
return nil, fmt.Errorf("failed to read page %d: %w", nextId, err)
}
from := pageCount*(int(r.pager.pageSize)-8) - 8
copy(recordData[from:], data[8:])
}
return recordData, nil
}
func setNextRecordId(pageData []byte, nextId uint32) {
copy(pageData[0:8], encodeUint32(nextId))
}
func clearNextRecordId(pageData []byte) {
reset(pageData[0:8])
}
func recordSize(pageData []byte) uint32 {
return decodeUint32(pageData[8:16])
}
func nextRecordId(pageData []byte) uint32 {
return decodeUint32(pageData[0:8])
}