-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagers.go
664 lines (478 loc) · 13.6 KB
/
pagers.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
package gokvdb
import (
"os"
"fmt"
"sync"
"path/filepath"
//"strings"
)
const (
PGTYPE_FREELIST byte = 1
PGTYPE_PAYLOAD byte = 2
HEADER_SIZE int = 512
STREAM_PAGER_HEADER_SIZE int = 256
PAYLOAD_HEADER_SIZE int = 16
PAYLOAD_PAGE_HEADER_SIZE int = 16
)
type IStream interface {
Write(data []byte)
Read(count int) ([]byte, error)
Seek(offset int64)
Sync()
Close()
ToString() string
}
type IPager interface {
ReadPage(pid uint32, count int) ([]byte, error)
WritePage(pid uint32, data []byte)
CreatePageId() uint32
FreePageId(pid uint32)
WritePayloadData(pid uint32, data []byte)
ReadPayloadData(pid uint32) ([]byte, error)
Save() []byte
GetPageSize() int
ToString() string
}
type FileStream struct {
path string
file *os.File
rwlock sync.Mutex
}
type BaseStreamPager struct {
stream IStream
meta *StreamPagerMeta
isChanged bool
}
type StreamPagerMeta struct {
pageSize uint32
lastPageId uint32
freelistPageId uint32
}
type StreamPager struct {
basePager *BaseStreamPager
freelist *FreePageList
payloadFactory *PayloadPageFactory
}
func (s *FileStream) ToString() string {
return fmt.Sprintf("<FileStream path=%v", s.path)
}
func (s *FileStream) Write(data []byte) {
s.rwlock.Lock()
defer s.rwlock.Unlock()
s.file.Write(data)
}
func (s *FileStream) Read(count int) ([]byte, error) {
s.rwlock.Lock()
defer s.rwlock.Unlock()
data := make([]byte, count)
_, err := s.file.Read(data)
if err != nil {
return nil, err
}
//fmt.Println("Read", rtn)
return data, err
}
func (s *FileStream) Seek(offset int64) {
s.rwlock.Lock()
defer s.rwlock.Unlock()
s.file.Seek(offset, os.SEEK_SET)
}
func (s *FileStream) Sync() {
s.rwlock.Lock()
defer s.rwlock.Unlock()
s.file.Sync()
}
func (s *FileStream) Close() {
s.rwlock.Lock()
defer s.rwlock.Unlock()
if s.file != nil {
s.file.Sync()
s.file.Close()
s.file = nil
}
}
func OpenFileStream(path string) (IStream, error) {
fullpath, _ := filepath.Abs(path)
_CheckCreateDirpath(fullpath)
f, err := os.OpenFile(fullpath, os.O_RDWR | os.O_CREATE, 0666)
if err != nil {
return nil, err
}
stream := new(FileStream)
stream.path = path
stream.file = f
return stream, nil
}
func (meta StreamPagerMeta) ToString() string {
return fmt.Sprintf("<StreamPagerMeta pageSize=%v lastPageId=%v freelistPageId=%v>", meta.pageSize, meta.lastPageId, meta.freelistPageId)
}
func ReadOrNewStreamPagerMeta(pageSize uint32, data []byte) *StreamPagerMeta {
//fmt.Println("_ReadOrNewStreamRawPagerMeta", pageSize, data)
meta := new(StreamPagerMeta)
if data != nil && len(data) > 8 {
rd := NewDataStreamFromBuffer(data)
meta.pageSize = rd.ReadUInt32()
meta.lastPageId = rd.ReadUInt32()
meta.freelistPageId = rd.ReadUInt32()
} else {
meta.pageSize = pageSize
meta.lastPageId = 0
meta.freelistPageId = 0
}
if meta.pageSize == 0 {
meta.pageSize = pageSize
}
//fmt.Printf("_ReadOrNewStreamRawPagerMeta pageSize=%v lastPageId=%v\n", meta.pageSize, meta.lastPageId)
return meta
}
func NewStreamPager(stream IStream, meta *StreamPagerMeta) IPager {
pager := new(StreamPager)
basePager := new(BaseStreamPager)
basePager.stream = stream
basePager.meta = meta
freelist := _NewFreePageList(basePager, meta.freelistPageId, false)
pager.basePager = basePager
pager.freelist = freelist
pager.payloadFactory = NewPayloadPageFactory(pager)
basePager.meta.freelistPageId = freelist.rootPageId
return pager
}
func (p *StreamPager) WritePayloadData(pid uint32, data []byte) {
p.payloadFactory.WritePayloadData(pid, data)
}
func (p *StreamPager) ReadPayloadData(pid uint32) ([]byte, error) {
return p.payloadFactory.ReadPayloadData(pid)
}
func (p *StreamPager) GetPageSize() int {
return int(p.basePager.meta.pageSize)
}
func (p *StreamPager) ToString() string {
return fmt.Sprintf("<StreamPager meta=%v>", p.basePager.meta.ToString())
}
func (p *StreamPager) Save() []byte {
p.freelist.Save()
if p.basePager.isChanged {
w := NewDataStreamFromBuffer(make([]byte, STREAM_PAGER_HEADER_SIZE))
w.WriteUInt32(p.basePager.meta.pageSize)
w.WriteUInt32(p.basePager.meta.lastPageId)
w.WriteUInt32(p.basePager.meta.freelistPageId)
//p.stream.Seek(0)
//p.stream.Write(w.ToBytes())
p.basePager.isChanged = false
//fmt.Println(">>>>>>>>>>>>>>>>> StreamRawPager SAVE", "p.meta.lastPageId", p.meta.lastPageId)
return w.ToBytes()
}
return nil
}
func (p *StreamPager) ReadPage(pid uint32, count int) ([]byte, error) {
return p.basePager.ReadPage(pid, count)
}
func (p *StreamPager) WritePage(pid uint32, data []byte) {
p.basePager.WritePage(pid, data)
}
func (p *StreamPager) CreatePageId() uint32 {
freeId, ok := p.freelist.Pop()
if ok {
return freeId
}
return p.basePager.CreatePageId()
}
func (p *StreamPager) FreePageId(pid uint32) {
//fmt.Println(p.ToString(), "FreePageId", pid)
if pid < 1 {
fmt.Printf("cant free pageId=%v\n", pid)
os.Exit(1)
}
empty := make([]byte, PAYLOAD_PAGE_HEADER_SIZE)
p.basePager.WritePage(pid, empty)
p.freelist.Put(pid)
}
func (p *BaseStreamPager) GetPageSize() int {
return int(p.meta.pageSize)
}
func (p *BaseStreamPager) CalcPageOffset(pid uint32) int64 {
return int64(pid * p.meta.pageSize)
}
func (p *BaseStreamPager) ReadPage(pid uint32, count int) ([]byte, error) {
if count == 0 || count > int(p.meta.pageSize) {
count = int(p.meta.pageSize)
}
seek2 := p.CalcPageOffset(pid)
p.stream.Seek(seek2)
data, err := p.stream.Read(count)
//fmt.Printf("ReadPage pid=%v pageSize=%v count=%v seek=%v dataLen=%v\n", pid, p.meta.pageSize, count, seek2, len(data))
return data, err
}
func (p *BaseStreamPager) WritePage(pid uint32, data []byte) {
if len(data) > int(p.meta.pageSize) {
fmt.Printf("page size needs <= %v\n", p.meta.pageSize)
os.Exit(1)
}
pageData := make([]byte, p.meta.pageSize)
copy(pageData, data)
seek2 := p.CalcPageOffset(pid)
p.stream.Seek(seek2)
p.stream.Write(pageData)
//fmt.Printf("WritePage pid=%v seek=%v dataLen=%v\n", pid, seek2, len(data))
}
func (p *BaseStreamPager) CreatePageId() uint32 {
pid := p.meta.lastPageId + 1
p.meta.lastPageId = pid
p.isChanged = true
//fmt.Println("======================StreamRawPager CreatePageId", pid, "meta", p.meta.lastPageId )
return pid
}
func (p *BaseStreamPager) FreePageId(pid uint32) {
}
func (p *BaseStreamPager) ToString() string {
return fmt.Sprintf("<BaseStreamPager lastPageId=%v>", p.meta.lastPageId)
}
func (p *BaseStreamPager) Save() []byte {
fmt.Println("NO Implemented Save")
os.Exit(1)
return nil
}
func (p *BaseStreamPager) WritePayloadData(pid uint32, data []byte) {
fmt.Println("NO Implemented WritePayloadData")
os.Exit(1)
}
func (p *BaseStreamPager) ReadPayloadData(pid uint32) ([]byte, error) {
fmt.Println("NO Implemented ReadPayloadData")
os.Exit(1)
return nil, DBError{message: "NO Implemented ReadPayloadData"}
}
type PayloadPageFactory struct {
pager IPager
isDebug bool
}
func NewPayloadPageFactory(pager IPager) *PayloadPageFactory {
factory := new(PayloadPageFactory)
factory.pager = pager
return factory
}
type PayloadPageWriter struct {
pageIds []uint32
pageIdIndex int
pager IPager
factory *PayloadPageFactory
}
type PayloadPageHeader struct {
//pgType uint8
pageIndex int
contentLen uint32
hasNextPage bool
nextPageId uint32
}
func _ReadPayloadPageHeader(rd *DataStream) PayloadPageHeader {
pageIndex := int(rd.ReadUInt16())
contentLen := rd.ReadUInt32()
hasNextPage := rd.ReadBool()
nextPageId := rd.ReadUInt32()
hdr := PayloadPageHeader{ pageIndex: pageIndex, contentLen: contentLen, hasNextPage: hasNextPage, nextPageId: nextPageId}
return hdr
}
func (w *PayloadPageWriter) CalcPageIds(pid uint32) {
//fmt.Println("CalcPageIds pid", pid)
var pageIds []uint32
var curPageId uint32
var nextPageId uint32
nextPageId = pid
//fmt.Println("PayloadPageWriter CalcPageIds")
for {
//fmt.Println("PayloadPageWriter CalcPageIds", nextPageId)
if curPageId == nextPageId {
fmt.Printf("ERROR curPageId=%v nextPageId=%v\n", curPageId, nextPageId)
os.Exit(1)
break
}
curPageId = nextPageId
if curPageId < 1 {
fmt.Printf("ERROR payloadPageId=%v\n", curPageId)
os.Exit(1)
}
pageIds = append(pageIds, curPageId)
headerBytes, err := w.pager.ReadPage(curPageId, PAYLOAD_PAGE_HEADER_SIZE)
if w.factory.isDebug {
fmt.Printf("[CalcPageIds] rootId=%v curPageId=%v err=%v headerBytes=%v\n", pid, curPageId, err, headerBytes)
}
//
if err == nil && len(headerBytes) == PAYLOAD_PAGE_HEADER_SIZE {
hdrR := NewDataStreamFromBuffer(headerBytes)
pgType := hdrR.ReadUInt8()
/*
if w.factory.isDebug {
fmt.Println("pid", curPageId, "pgType", pgType)
}
*/
if pgType != PGTYPE_PAYLOAD {
break
}
hdr := _ReadPayloadPageHeader(hdrR)
nextPageId = hdr.nextPageId
//fmt.Println(">>>>>>>> pageHeaderNextPageId =..", pageHeaderNextPageId)
if hdr.hasNextPage {
continue
}
}
break
}
w.pageIdIndex = 0
w.pageIds = pageIds
}
func (w *PayloadPageWriter) GetOrCreateNextWritePageId() uint32 {
if w.pageIdIndex < len(w.pageIds) {
pid := w.pageIds[w.pageIdIndex]
w.pageIdIndex += 1
return pid
}
return w.pager.CreatePageId()
}
func (w *PayloadPageWriter) FreePageIds() {
for {
if !(w.pageIdIndex < len(w.pageIds)) {
break
}
pid := w.pageIds[w.pageIdIndex]
//fmt.Println("PayloadPageWriter FreePageIds", pid)
if pid == 0 {
fmt.Println("FreePageIds cant == 0")
os.Exit(1)
}
w.pager.FreePageId(pid)
w.pageIdIndex += 1
}
}
func (w *PayloadPageWriter) Write(pid uint32, data []byte) {
w.CalcPageIds(pid)
ds := NewDataStream()
ds.WriteUInt32(uint32(len(data)))
ds.Seek(PAYLOAD_HEADER_SIZE)
ds.Write(data)
//
writeData := ds.ToBytes()
var pageIndex uint16
var curPageId uint32
var nextPageId uint32
var iStart int
var iEnd int
nextPageId = pid
iStart = 0
loopCount := 0
pageSize := w.pager.GetPageSize()
pageContentSize := pageSize - PAYLOAD_PAGE_HEADER_SIZE
curPageId = 0
nextPageId = w.GetOrCreateNextWritePageId()
writeDataLen := len(writeData)
pageIndex = 0
for {
if !(iStart < writeDataLen) {
break
}
loopCount += 1
curPageId = nextPageId
hasNextPage := false
iEnd = iStart + pageContentSize
if iEnd > writeDataLen {
iEnd = writeDataLen
}
if iEnd < writeDataLen {
hasNextPage = true
}
if hasNextPage {
nextPageId = w.GetOrCreateNextWritePageId()
} else {
nextPageId = 0
//fmt.Println("!hasNextPage nextPageId = 0")
}
if hasNextPage && nextPageId < 1 {
fmt.Println("Error nextPageId", nextPageId)
os.Exit(1)
}
pageContentData := writeData[iStart:iEnd]
pageContentDataLen := uint32(len(pageContentData))
if w.factory.isDebug {
fmt.Println("[PayloadPageWriter] pageIndex", pageIndex, "pageContentDataLen", pageContentDataLen, "hasNextPage", hasNextPage, "curPageId", curPageId, "nextPageId", nextPageId)
}
pageW := NewDataStream()
pageW.WriteUInt8(PGTYPE_PAYLOAD)
pageW.WriteUInt16(pageIndex)
pageW.WriteUInt32(pageContentDataLen)
pageW.WriteBool(hasNextPage)
pageW.WriteUInt32(nextPageId)
pageW.Seek(PAYLOAD_PAGE_HEADER_SIZE)
pageW.Write(pageContentData)
//pageBuf.Seek(PAGE_HEADER_SIZE)
pageData := pageW.ToBytes()
if len(pageData) > pageSize {
pageData = pageData[:pageSize]
}
testRd := NewDataStreamFromBuffer(pageData)
testRd.ReadUInt8()
testRd.ReadUInt16()
testContentLen := testRd.ReadUInt32()
w.pager.WritePage(curPageId, pageData)
//fmt.Println("SAVE pageData", "loopCount", loopCount, "pid=", curPageId, "len", len(pageData), "testContentLen", testContentLen, len(pageContentData))
if len(pageContentData) != int(testContentLen) {
fmt.Println("TEST CONTENT LEN ERROR!", pageContentDataLen, testContentLen, int(testContentLen))
os.Exit(1)
}
iStart += pageContentSize
pageIndex += 1
}
w.FreePageIds()
}
func (f *PayloadPageFactory) WritePayloadData(pid uint32, data []byte) {
if pid < 1 {
fmt.Println("WritePayloadData ERROR pid=", pid)
os.Exit(1)
}
w := new(PayloadPageWriter)
w.pager = f.pager
w.factory = f
w.Write(pid, data)
}
func (f *PayloadPageFactory) ReadPayloadData(pid uint32) ([]byte, error) {
//fmt.Println("_LoadPayloadPage", pid)
var allBytes []byte
curPageId := pid
pageCount := 0
for {
/*
if curPageId == 0 {
break
}*/
pageCount += 1
//fmt.Println("ReadPayloadData", "rootPid", pid, "pageCount", pageCount, "curPageId", curPageId)
pageData, err := f.pager.ReadPage(curPageId, 0)
if err != nil {
return nil, err
}
rd := NewDataStreamFromBuffer(pageData)
pgType := rd.ReadUInt8()
header := _ReadPayloadPageHeader(rd)
if pgType != PGTYPE_PAYLOAD {
fmt.Println("ERROR PayloadPage pgType=", pgType)
os.Exit(1)
}
//fmt.Printf("Read PayloadPage rootPid=%v pageIndex=%v pageDataLen=%v hasNextPage=%v curPageId=%v nextPageId=%v\n", pid, header.pageIndex, header.contentLen, header.hasNextPage, curPageId, header.nextPageId )
rd.Seek(PAYLOAD_PAGE_HEADER_SIZE)
contentBytes := rd.Read(int(header.contentLen))
//fmt.Println("_LoadPayloadPage", "pid=", nextPageId, "contentBytes=", len(contentBytes))
allBytes = append(allBytes, contentBytes...)
if !header.hasNextPage {
break
}
curPageId = header.nextPageId
}
if len(allBytes) < PAYLOAD_HEADER_SIZE {
return nil, DBError{message: fmt.Sprintf("pid=%v bytes=%v not enough!", pid, len(allBytes))}
}
buf := NewDataStreamFromBuffer(allBytes)
payloadDataLenRequired := buf.ReadUInt32()
//fmt.Println("pid=", pid, "payloadDataLenRequired", payloadDataLenRequired)
allBytes = allBytes[PAYLOAD_HEADER_SIZE:]
if uint32(len(allBytes)) != payloadDataLenRequired {
return nil, DBError{message: fmt.Sprintf("bytes length needs %v payload data is %v!", payloadDataLenRequired, len(allBytes))}
}
return allBytes, nil
}