This repository has been archived by the owner on Jul 21, 2021. It is now read-only.
/
carbon_fds.go
63 lines (54 loc) 路 1.63 KB
/
carbon_fds.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
package carbon
import (
"os"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"github.com/ipld/go-car/util"
carbs "github.com/willscott/carbs"
)
// carbonFD is a carbon implementation based on having two file handles opened, one appending to the file, and the other
// seeking to read items as needed. This implementation is preferable for a write-heavy workload.
type carbonFD struct {
path string
writeHandle *poswriter
carbs.Carbs
idx *insertionIndex
}
var _ (Carbon) = (*carbonFD)(nil)
func (c *carbonFD) DeleteBlock(cid.Cid) error {
return errUnsupported
}
// Put puts a given block to the underlying datastore
func (c *carbonFD) Put(b blocks.Block) error {
return c.PutMany([]blocks.Block{b})
}
// PutMany puts a slice of blocks at the same time using batching
// capabilities of the underlying datastore whenever possible.
func (c *carbonFD) PutMany(b []blocks.Block) error {
for _, bl := range b {
n := c.writeHandle.at
if err := util.LdWrite(c.writeHandle, bl.Cid().Bytes(), bl.RawData()); err != nil {
return err
}
c.idx.items.InsertNoReplace(mkRecordFromCid(bl.Cid(), n))
}
return nil
}
// Finish serializes the carbon index so that it can be later used as a carbs read-only blockstore
func (c *carbonFD) Finish() error {
fi, err := c.idx.Flatten()
if err != nil {
return err
}
fd, ok := c.writeHandle.Writer.(*os.File)
if ok {
if err := fd.Close(); err != nil {
return err
}
}
return carbs.Save(fi, c.path)
}
// Checkpoint serializes the carbon index so that the partially written blockstore can be resumed.
func (c *carbonFD) Checkpoint() error {
return carbs.Save(c.idx, c.path)
}