forked from naqvijafar91/cuteDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_service_impl.go
152 lines (130 loc) · 3.47 KB
/
block_service_impl.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
package cutedb
import (
"os"
)
func initblockServiceFileStore() blockService {
path := "./db/test.db"
if _, err := os.Stat(path); os.IsNotExist(err) {
os.Mkdir("./db", os.ModePerm)
}
if _, err := os.Stat(path); err == nil {
// path/to/whatever exists
err := os.Remove(path)
if err != nil {
panic(err)
}
}
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
panic(err)
}
return &blockServiceImpl{blockStore: newFileStore(file)}
}
func initblockServicKvStore() blockService {
return &blockServiceImpl{blockStore: newKvStore()}
}
type blockServiceImpl struct {
blockStore blockStore
}
func (bs *blockServiceImpl) getLatestBlockID() (int64, error) {
return bs.blockStore.getLatestBlockID()
}
//@Todo:Store current root block data somewhere else
func (bs *blockServiceImpl) getRootBlock() (*diskBlock, error) {
/*
1. Check if root block exists
2. If exisits, fetch it, else initialize a new block
*/
if !bs.rootBlockExists() {
// Need to write a new block
return bs.newBlock()
}
return bs.getBlockFromDiskByBlockNumber(0)
}
func (bs *blockServiceImpl) getBlockFromDiskByBlockNumber(index int64) (*diskBlock, error) {
if index < 0 {
panic("Index less than 0 asked")
}
blockBuffer, err := bs.blockStore.getBlockFromDisk(index)
if err != nil {
return nil, err
}
block := bs.getBlockFromBuffer(blockBuffer)
return block, nil
}
func (bs *blockServiceImpl) getBlockFromBuffer(blockBuffer []byte) *diskBlock {
return getBlockFromBuffer(blockBuffer)
}
func (bs *blockServiceImpl) newBlock() (*diskBlock, error) {
latestBlockID, err := bs.getLatestBlockID()
block := &diskBlock{}
if err != nil {
// This means that no file exists
block.id = 0
} else {
block.id = uint64(latestBlockID) + 1
}
block.currentLeafSize = 0
err = bs.writeBlockToDisk(block)
if err != nil {
return nil, err
}
return block, nil
}
func (bs *blockServiceImpl) writeBlockToDisk(block *diskBlock) error {
err := bs.blockStore.writeBlockToDisk(block)
if err != nil {
return err
}
return nil
}
func (bs *blockServiceImpl) getNodeAtBlockID(blockID uint64) (*DiskNode, error) {
block, err := bs.getBlockFromDiskByBlockNumber(int64(blockID))
if err != nil {
return nil, err
}
return bs.convertBlockToDiskNode(block), nil
}
func (bs *blockServiceImpl) convertBlockToDiskNode(block *diskBlock) *DiskNode {
return block.convertBlockToDiskNode(bs)
}
// NewBlockFromNode - Save a new node to disk block
func (bs *blockServiceImpl) saveNewNodeToDisk(n *DiskNode) error {
// Get block id to be assigned to this block
latestBlockID, err := bs.getLatestBlockID()
if err != nil {
return err
}
n.blockID = uint64(latestBlockID) + 1
block := n.convertDiskNodeToBlock()
return bs.writeBlockToDisk(block)
}
func (bs *blockServiceImpl) updateNodeToDisk(n *DiskNode) error {
block := n.convertDiskNodeToBlock()
return bs.writeBlockToDisk(block)
}
func (bs *blockServiceImpl) updateRootNode(n *DiskNode) error {
n.blockID = 0
return bs.updateNodeToDisk(n)
}
func (bs *blockServiceImpl) rootBlockExists() bool {
latestBlockID, err := bs.getLatestBlockID()
// fmt.Println(latestBlockID)
//@Todo:Validate the type of error here
if err != nil {
// Need to write a new block
return false
} else if latestBlockID == -1 {
return false
} else {
return true
}
}
/**
@Todo: Implement a function to :
1. Dynamicaly calculate blockSize
2. Then based on the blocksize, calculate the maxLeafSize
*/
func (bs *blockServiceImpl) getMaxLeafSize() int {
return maxLeafSize
}