This repository has been archived by the owner on Dec 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpagemanager.go
323 lines (273 loc) · 7.71 KB
/
pagemanager.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package pages
import (
"fmt"
"io"
"math"
"os"
"sync"
"github.com/NebulousLabs/Sia/build"
)
// Identifier is a helper type that can be used to reopen a previously created
// entry
type Identifier int64
// PageManager blabla
type PageManager struct {
// file is the underlying file to which data is written
file *os.File
// freePages contains the pages that can be reused for new data
freePages *recyclingPage
// TODO find a better way to do this
// recyclePages is used to indicate if it is safe to reuse free pages. This
// is used as a workaround to disable page recycling while pages are being
// inserted into freePages
recyclePages bool
// mu is a mutex to lock the PageManager's ressources
mu *sync.Mutex
// entryPages keeps track of all the entryPages
entryPages map[Identifier]*entryPage
}
// allocatePage either returns a free page or allocates a page and adds
// it to the pages map.
func (p *PageManager) allocatePage() (*physicalPage, error) {
// If there are free pages available return one of those
var newPage *physicalPage
if p.recyclePages && p.freePages != nil && p.freePages.availablePages() > 0 {
removedPage, err := p.freePages.freePage()
if err != nil {
return nil, build.ExtendErr("Failed to reuse free page", err)
}
return removedPage, nil
}
// Get the fileOff for the page
fileOff, err := p.file.Seek(0, io.SeekEnd)
if err != nil {
return nil, err
}
// The last page might not have pageSize yet so we might have to adjust the
// offset a bit
if fileOff%pageSize != 0 {
fileOff += (pageSize - fileOff%pageSize)
}
// Don't start before dataOff
if fileOff < dataOff {
fileOff = dataOff
}
// Create the new page and write it to disk
newPage = &physicalPage{
file: p.file,
fileOff: fileOff,
}
// TODO maybe remove this but if we do we have to fix the way we calculate
// the fileOff for new pages
n, err := newPage.file.WriteAt(make([]byte, pageSize, pageSize), newPage.fileOff)
if n != pageSize || err != nil {
return nil, fmt.Errorf("couldn't write new page wrote %v bytes %v", n, err)
}
return newPage, nil
}
// Close closes open handles and frees ressources
func (p PageManager) Close() error {
return p.file.Close()
}
// Create creates a new Entry and returns an identifier for it
func (p *PageManager) Create() (*Entry, Identifier, error) {
p.mu.Lock()
defer p.mu.Unlock()
// Allocate a page for the table
pp, err := p.allocatePage()
if err != nil {
return nil, 0, build.ExtendErr("failed to allocate page for new entryPage", err)
}
// Create the first pageTable
root, err := newPageTable(0, nil, p)
if err != nil {
return nil, 0, build.ExtendErr("Couldn't create new pageTable", err)
}
// Create the entryPage
ep := &entryPage{
&tieredPage{
pp: pp,
pm: p,
root: root,
mu: new(sync.RWMutex),
},
0,
}
// Initialize entryPage
if err := writeTieredPageEntry(pp, 0, 0, ep.pp.fileOff); err != nil {
return nil, 0, err
}
// Create a new entry
newEntry := &Entry{
pm: p,
ep: ep,
}
// Increment the entryPage's counter and add it to the map
id := Identifier(ep.pp.fileOff)
p.entryPages[id] = ep
ep.instanceCounter++
return newEntry, id, nil
}
// loadFreePagesFromDisk loads the offsets of free pages from the first page of
// the file.
func (p *PageManager) loadFreePagesFromDisk() error {
// Create the physicalPage object using the identifier. We don't know
// usedSize yet but for the entryPage we can just set it to pageSize
pp := &physicalPage{
file: p.file,
fileOff: freeOff,
usedSize: pageSize,
}
// Read all the entries from the entryPage and remember the root and usedSize
rootOff := int64(0)
usedSize := int64(0)
height := int64(0)
var err error
for i := 0; i < pageSize/tieredPageEntrySize; i++ {
usedSize, rootOff, err = readEntryPageEntry(pp, int64(i))
if err != nil {
return build.ExtendErr("Failed to read entry", err)
}
// Remember the reached height
height = int64(i)
// Stop if we find a root that isn't full yet
numPages := int64(math.Pow(float64(numPageEntries), float64(i+1)))
if usedSize < numPages*pageSize {
break
}
}
// Create the entryPage object and recover the tree.
ep := &recyclingPage{
&tieredPage{
pp: pp,
usedSize: usedSize,
pm: p,
mu: new(sync.RWMutex),
},
nil,
}
// Recover the tree to get the pages of the entry
if err := ep.recoverTree(rootOff, height); err != nil {
return build.ExtendErr("Failed to recover tree", err)
}
p.freePages = ep
return nil
}
// managedAllocatePage either returns a free page or allocates a page and adds
// it to the pages map.
func (p *PageManager) managedAllocatePage() (*physicalPage, error) {
p.mu.Lock()
defer p.mu.Unlock()
return p.allocatePage()
}
// New creates a PageManager or recovers an existing one
func New(filePath string) (*PageManager, error) {
// Create the page manager object
pm := &PageManager{
mu: new(sync.Mutex),
entryPages: make(map[Identifier]*entryPage),
recyclePages: true,
}
// Try to open the database file
file, err := os.OpenFile(filePath, os.O_RDWR, 0600)
if err == nil {
// There is a file that can be recovered
pm.file = file
// Load the freePages
if err := pm.loadFreePagesFromDisk(); err != nil {
return nil, build.ExtendErr("failed to read free pages", err)
}
return pm, nil
} else if !os.IsNotExist(err) {
// The file exists but cannot be opened
return nil, build.ExtendErr("Failed to open existing database file", err)
}
// If the file doesn't exist create a new one
file, err = os.Create(filePath)
if err != nil {
return nil, build.ExtendErr("Failed to create the database file: %v", err)
}
pm.file = file
// Create the pageEntry for the free pages.
root, err := newPageTable(0, nil, pm)
if err != nil {
return nil, build.ExtendErr("Failed to create pageTable for recycling page", err)
}
rp := &recyclingPage{
&tieredPage{
pm: pm,
root: root,
mu: new(sync.RWMutex),
pp: &physicalPage{
file: pm.file,
fileOff: freeOff,
usedSize: pageSize,
},
},
nil,
}
pm.freePages = rp
return pm, nil
}
// Open loads a previously created entry
func (p *PageManager) Open(id Identifier) (*Entry, error) {
p.mu.Lock()
defer p.mu.Unlock()
// Check if the identifier was opened before
if ep, exists := p.entryPages[id]; exists {
// Increase the instance counter of the entryPage
ep.instanceCounter++
return &Entry{
pm: p,
ep: ep,
}, nil
}
// Create the physicalPage object using the identifier. We don't know
// usedSize yet but for the entryPage we can just set it to pageSize
pp := &physicalPage{
file: p.file,
fileOff: int64(id),
usedSize: pageSize,
}
// Read all the entries from the entryPage and remember the root and usedSize
rootOff := int64(0)
usedSize := int64(0)
height := int64(0)
var err error
for i := 0; i < pageSize/tieredPageEntrySize; i++ {
usedSize, rootOff, err = readEntryPageEntry(pp, int64(i))
if err != nil {
return nil, build.ExtendErr("Failed to read entry", err)
}
// Remember the reached height
height = int64(i)
// Stop if we find a root that isn't full yet
numPages := int64(math.Pow(float64(numPageEntries), float64(i+1)))
if usedSize < numPages*pageSize {
break
}
}
// Create the entryPage object and recover the tree.
ep := &entryPage{
&tieredPage{
pp: pp,
usedSize: usedSize,
pm: p,
mu: new(sync.RWMutex),
},
0,
}
// Recover the tree to get the pages of the entry
if err := ep.recoverTree(rootOff, height); err != nil {
return nil, build.ExtendErr("Failed to recover tree", err)
}
// Create the entry
newEntry := &Entry{
pm: p,
ep: ep,
}
// Increment the entryPage's counter and add it to the map
p.entryPages[id] = ep
ep.instanceCounter++
return newEntry, nil
}