forked from naqvijafar91/cuteDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
page_store_in_file.go
58 lines (50 loc) · 1.12 KB
/
page_store_in_file.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
package cutedb
import (
"fmt"
"os"
)
type blockFileStore struct {
file *os.File
}
func newFileStore(file *os.File) blockStore {
return &blockFileStore{file: file}
}
func (s *blockFileStore) getBlockFromDisk(index int64) ([]byte, error) {
offset := index * blockSize
_, err := s.file.Seek(offset, 0)
if err != nil {
return nil, err
}
blockBuffer := make([]byte, blockSize)
_, err = s.file.Read(blockBuffer)
if err != nil {
return nil, err
}
return blockBuffer, nil
}
func (s *blockFileStore) writeBlockToDisk(block *diskBlock) error {
fmt.Printf("writeBlockToDisk: %d\n", block.id)
seekOffset := blockSize * block.id
blockBuffer := block.getBufferFromBlock()
_, err := s.file.Seek(int64(seekOffset), 0)
if err != nil {
return err
}
_, err = s.file.Write(blockBuffer)
if err != nil {
return err
}
return nil
}
func (s *blockFileStore) getLatestBlockID() (int64, error) {
fi, err := s.file.Stat()
if err != nil {
return -1, err
}
length := fi.Size()
if length == 0 {
return -1, nil
}
// Calculate page number required to be fetched from disk
return (int64(fi.Size()) / int64(blockSize)) - 1, nil
}