This repository has been archived by the owner on Jul 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfsbs.go
309 lines (255 loc) · 5.71 KB
/
fsbs.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
package sbs
import (
"fmt"
"os"
"path/filepath"
"github.com/ipfs/go-sbs/consts"
pb "github.com/ipfs/go-sbs/pb"
"github.com/boltdb/bolt"
proto "github.com/gogo/protobuf/proto"
mmap "github.com/gxed/mmap-go"
)
var ErrNotFound = fmt.Errorf("not found")
var (
bucketOffset = []byte("offsets")
)
type Sbs struct {
Mem []byte
mmfi *os.File
mm mmap.MMap
index *bolt.DB
alloc *AllocatorBlock
curAlloc *AllocatorBlock
}
func Open(path string) (*Sbs, error) {
datapath := filepath.Join(path, "data")
indexpath := filepath.Join(path, "index")
db, err := bolt.Open(indexpath, 0600, nil)
if err != nil {
return nil, err
}
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists(bucketOffset)
return err
})
if err != nil {
return nil, err
}
fi, err := os.OpenFile(datapath, os.O_RDWR, 0300)
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
fi, err = os.Create(datapath)
if err != nil {
return nil, err
}
err = fi.Truncate(int64(consts.BlockSize * consts.BlocksPerAllocator))
if err != nil {
return nil, err
}
}
mm, err := mmap.Map(fi, mmap.RDWR, 0)
if err != nil {
return nil, err
}
alloc, err := LoadAllocator(mm[:consts.BlockSize])
if err != nil {
return nil, err
}
return &Sbs{
mmfi: fi,
mm: mm,
index: db,
alloc: alloc,
curAlloc: alloc,
}, nil
}
func (sbs *Sbs) Close() error {
if err := sbs.index.Close(); err != nil {
return err
}
if err := sbs.mm.Unmap(); err != nil {
return err
}
return nil
}
func (sbs *Sbs) nextAllocator() error {
currEnd := sbs.curAlloc.Offset + consts.BlocksPerAllocator
newEnd := currEnd + consts.BlocksPerAllocator
if uint64(len(sbs.mm)) < newEnd*consts.BlockSize {
err := sbs.expand()
if err != nil {
return err
}
}
nalloc, err := LoadAllocator(sbs.mm[currEnd*consts.BlockSize : newEnd*consts.BlockSize])
if err != nil {
return err
}
nalloc.Offset = currEnd
sbs.curAlloc = nalloc
_ = sbs.mm[newEnd*consts.BlockSize-1] // range check
return nil
}
func (sbs *Sbs) expand() error {
currEnd := sbs.curAlloc.Offset + consts.BlocksPerAllocator
newEnd := int64(currEnd + consts.BlocksPerAllocator)
err := sbs.mmfi.Truncate(newEnd * consts.BlockSize)
if err != nil {
return err
}
err = sbs.mm.Unmap()
if err != nil {
return err
}
nmm, err := mmap.Map(sbs.mmfi, mmap.RDWR, 0)
if err != nil {
return err
}
sbs.mm = nmm
return nil
}
func blocksNeeded(length uint64) uint64 {
nblks := length / consts.BlockSize
if length%consts.BlockSize != 0 {
nblks++
}
return nblks
}
func (sbs *Sbs) allocateN(nblks uint64) ([]uint64, error) {
blks := make([]uint64, 0, nblks)
for uint64(len(blks)) != nblks {
mblks, err := sbs.curAlloc.Allocate(nblks - uint64(len(blks)))
switch err {
case ErrAllocatorFull:
err = sbs.nextAllocator()
if err != nil {
return nil, err
}
fallthrough
case nil:
blks = append(blks, mblks...)
default:
return nil, err
}
}
return blks, nil
}
func (sbs *Sbs) copyToStorage(val []byte, blks []uint64) {
for i, blk := range blks {
l := consts.BlockSize
beg := i * consts.BlockSize
if bufleft := len(val) - beg; bufleft < l {
l = bufleft
}
//fmt.Printf("trying to write: %d, blocklen: %d", blk, len(sbs.mm)/consts.BlockSize)
blkoff := blk * consts.BlockSize
copy(sbs.mm[blkoff:blkoff+uint64(l)], val[beg:beg+l])
}
}
func createRecord(val []byte, blks []uint64) ([]byte, error) {
t := pb.Record_Indirect
rec := &pb.Record{
Blocks: blks,
Size_: proto.Uint64(uint64(len(val))),
Type: &t,
}
return proto.Marshal(rec)
}
func (sbs *Sbs) Put(k []byte, val []byte) error {
nblks := blocksNeeded(uint64(len(val)))
blks, err := sbs.allocateN(nblks)
if err != nil {
return err
}
data, err := createRecord(val, blks)
if err != nil {
return err
}
sbs.copyToStorage(val, blks)
err = sbs.index.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketOffset)
return b.Put(k, data)
})
return err
}
func (sbs *Sbs) getPB(k []byte) (*pb.Record, error) {
var prec pb.Record
err := sbs.index.View(func(tx *bolt.Tx) error {
rec := tx.Bucket(bucketOffset).Get(k)
if len(rec) == 0 {
return ErrNotFound
}
err := proto.Unmarshal(rec, &prec)
return err
})
return &prec, err
}
func (sbs *Sbs) Has(k []byte) (bool, error) {
has := false
err := sbs.index.View(func(tx *bolt.Tx) error {
rec := tx.Bucket(bucketOffset).Get(k)
if len(rec) != 0 {
has = true
}
return nil
})
return has, err
}
func (sbs *Sbs) read(prec *pb.Record, out []byte) {
var beg uint64
for _, blk := range prec.GetBlocks() {
l := uint64(consts.BlockSize)
if lsize := uint64(len(out)) - beg; lsize < l {
l = lsize
}
blkoff := blk * consts.BlockSize
copy(out[beg:beg+l], sbs.mm[blkoff:blkoff+l])
beg += l
}
}
func (sbs *Sbs) Get(k []byte) ([]byte, error) {
prec, err := sbs.getPB(k)
if err != nil {
return nil, err
}
out := make([]byte, prec.GetSize_())
sbs.read(prec, out)
return out, nil
}
func (sbs *Sbs) Delete(k []byte) error {
var prec pb.Record
err := sbs.index.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketOffset)
rec := b.Get(k)
if len(rec) == 0 {
return ErrNotFound
}
err := b.Delete(k)
if err != nil {
return err
}
return proto.Unmarshal(rec, &prec)
})
if err != nil {
return err
}
tofree := make(map[uint64][]uint64)
for _, blk := range prec.GetBlocks() {
wa := blk / consts.BlocksPerAllocator
wi := blk % consts.BlocksPerAllocator
tofree[wa] = append(tofree[wa], wi)
}
for wa, list := range tofree {
beg := wa * consts.BlockSize * consts.BlocksPerAllocator
alloc, err := LoadAllocator(sbs.mm[beg : beg+consts.BlockSize])
if err != nil {
return err
}
if err := alloc.Free(list); err != nil {
return err
}
}
return nil
}