/
store.go
220 lines (170 loc) · 4.49 KB
/
store.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
package internal
import (
"errors"
"github.com/spacemeshos/poet-ref/shared"
"math"
"os"
)
type WriteData struct {
id shared.Identifier
l shared.Label
}
type WriteChan chan *WriteData
type IKvStore interface {
Read(id Identifier) (shared.Label, error)
Write(id Identifier, l shared.Label)
IsLabelInStore(id Identifier) (bool, error)
Reset() error
Delete() error
Size() uint64
Finalize() error // finalize writing w/o closing the file
Close() error // finalize and close
//GetWriteChan() WriteChan
}
type KVFileStore struct {
fileName string
file *os.File
n uint // 9 <= n < 64
f BinaryStringFactory
bw *Writer
c uint64 // num of labels written to store in this session
sz uint64 // num of bytes written
}
const buffSizeBytes = 1024 * 1024 * 1024
// Create a new prover with commitment X and 1 <= n < 64
// n specifies the leafs height from the root and the number of bits in leaf ids
func NewKvFileStore(fileName string, n uint) (IKvStore, error) {
res := &KVFileStore{
fileName: fileName,
n: n,
f: NewSMBinaryStringFactory(),
}
err := res.init()
return res, err
}
func (d *KVFileStore) init() error {
f, err := os.OpenFile(d.fileName, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
return err
}
d.file = f
// todo: compare pref w/o buffers
d.bw = NewWriterSize(f, buffSizeBytes)
return nil
}
func (d *KVFileStore) Write(id Identifier, l shared.Label) {
// update stats
d.c += 1
d.sz += uint64(len(l))
_, err := d.bw.Write(l)
if err != nil {
panic(err)
}
}
// Removes all data from the file
func (d *KVFileStore) Reset() error {
err := d.bw.Flush()
if err != nil {
return err
}
d.c = 0
d.sz = 0
return d.file.Truncate(0)
}
func (d *KVFileStore) Finalize() error {
// flush buffered ata to the underling writer
err := d.bw.Flush()
log.Tracef("Finalizing store. Wrote %d leaves. Total bytes: %d", d.c, d.sz)
return err
}
func (d *KVFileStore) Close() error {
err := d.Finalize()
if err != nil {
return err
}
return d.file.Close()
}
func (d *KVFileStore) Delete() error {
return os.Remove(d.fileName)
}
func (d *KVFileStore) Size() uint64 {
stats, err := d.file.Stat()
if err != nil {
println(err)
}
return uint64(stats.Size()) + uint64(d.bw.Buffered())
}
// Returns true iff node's label is in the store
func (d *KVFileStore) IsLabelInStore(id Identifier) (bool, error) {
idx, err := d.calcFileIndex(id)
if err != nil {
return false, err
}
stats, err := d.file.Stat()
if err != nil {
return false, err
}
if d.bw.Buffered() > 0 && idx < (d.c*shared.WB) {
// label is in file or in the buffer
return true, nil
}
fileSize := uint64(stats.Size())
return idx < fileSize, nil
}
// Read label value from the store
// Returns the label of node id or error if it is not in the store
func (d *KVFileStore) Read(id Identifier) (shared.Label, error) {
label := make(shared.Label, shared.WB)
// total # of labels written - # of buffered labels == idx of label at buff start
// say 4 labels were written, and Buffered() is 64 bytes. 2 last labels
// are in buffer and the index of the label at buff start is 2.
// idAtBuffStart := d.c - uint64(d.bw.Buffered()/shared.WB)
// label file index
idx, err := d.calcFileIndex(id)
if err != nil {
return label, err
}
n, err := d.file.ReadAt(label, int64(idx))
if err != nil {
return label, err
}
if n == 0 {
return label, errors.New("label for id is not in store")
}
return label, nil
}
// Returns the store file offset for the data of a node identified by id
func (d *KVFileStore) calcFileIndex(id Identifier) (uint64, error) {
s := d.subtreeSize(len(id))
s1, err := d.leftSiblingsSubtreeSize(id)
if err != nil {
return 0, err
}
idx := s + s1 - 1
offset := idx * shared.WB
//fmt.Printf("Node id %s. Index: %d. Offset: %d\n", id, idx, offset)
return offset, nil
}
// Returns the size of the subtree rooted at a node at tree depth
func (d *KVFileStore) subtreeSize(depth int) uint64 {
// Subtree height for a node at depth d
h := d.n - uint(depth)
return uint64(math.Pow(2, float64(h+1)) - 1)
}
// Returns the size of the subtrees rooted at left siblings on the path
// from node id to the root node
func (d *KVFileStore) leftSiblingsSubtreeSize(id Identifier) (uint64, error) {
bs, err := d.f.NewBinaryString(string(id))
if err != nil {
return 0, err
}
siblings, err := bs.GetBNSiblings(true)
if err != nil {
return 0, err
}
var res uint64
for _, s := range siblings {
res += d.subtreeSize(int(s.GetDigitsCount()))
}
return res, nil
}