-
Notifications
You must be signed in to change notification settings - Fork 1
/
storethehash.go
170 lines (141 loc) · 4.51 KB
/
storethehash.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
package storethehash
import (
"context"
"time"
store "github.com/hannahhoward/go-storethehash/store"
cidprimary "github.com/hannahhoward/go-storethehash/store/primary/cid"
"github.com/hannahhoward/go-storethehash/store/types"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
bstore "github.com/ipfs/go-ipfs-blockstore"
)
type errorType string
func (e errorType) Error() string {
return string(e)
}
// ErrNotSupported indicates and error that is not supported because this store is append only
const ErrNotSupported = errorType("Operation not supported")
// HashedBlockstore is a blockstore that uses a simple hash table and two files to write
type HashedBlockstore struct {
store *store.Store
}
const defaultIndexSizeBits = uint8(24)
const defaultBurstRate = 4 * 1024 * 1024
const defaultSyncInterval = time.Second
type configOptions struct {
indexSizeBits uint8
syncInterval time.Duration
burstRate types.Work
}
type Option func(*configOptions)
func IndexBitSize(indexBitSize uint8) Option {
return func(co *configOptions) {
co.indexSizeBits = indexBitSize
}
}
func SyncInterval(syncInterval time.Duration) Option {
return func(co *configOptions) {
co.syncInterval = syncInterval
}
}
func BurstRate(burstRate uint64) Option {
return func(co *configOptions) {
co.burstRate = types.Work(burstRate)
}
}
// OpenHashedBlockstore opens a HashedBlockstore with the default index size
func OpenHashedBlockstore(indexPath string, dataPath string, options ...Option) (*HashedBlockstore, error) {
co := configOptions{
indexSizeBits: defaultIndexSizeBits,
syncInterval: defaultSyncInterval,
burstRate: defaultBurstRate,
}
for _, option := range options {
option(&co)
}
primary, err := cidprimary.OpenCIDPrimary(dataPath)
if err != nil {
return nil, err
}
store, err := store.OpenStore(indexPath, primary, co.indexSizeBits, co.syncInterval, co.burstRate)
if err != nil {
return nil, err
}
return &HashedBlockstore{store}, nil
}
// DeleteBlock is not supported for this store
func (bs *HashedBlockstore) DeleteBlock(_ cid.Cid) error {
return ErrNotSupported
}
// Has indicates if a block is present in a block store
func (bs *HashedBlockstore) Has(c cid.Cid) (bool, error) {
return bs.store.Has(c.Bytes())
}
// Get returns a block
func (bs *HashedBlockstore) Get(c cid.Cid) (blocks.Block, error) {
value, found, err := bs.store.Get(c.Bytes())
if err != nil {
return nil, err
}
if !found {
return nil, bstore.ErrNotFound
}
return blocks.NewBlockWithCid(value, c)
}
// GetSize returns the CIDs mapped BlockSize
func (bs *HashedBlockstore) GetSize(c cid.Cid) (int, error) {
// unoptimized implementation for now
size, found, err := bs.store.GetSize(c.Bytes())
if err != nil {
return 0, err
}
if !found {
return 0, bstore.ErrNotFound
}
return int(size), nil
}
// Put puts a given block to the underlying datastore
func (bs *HashedBlockstore) Put(blk blocks.Block) error {
err := bs.store.Put(blk.Cid().Bytes(), blk.RawData())
// suppress key exist error because this is not expected behavior for a blockstore
if err == types.ErrKeyExists {
return nil
}
return err
}
// PutMany puts a slice of blocks at the same time using batching
// capabilities of the underlying datastore whenever possible.
func (bs *HashedBlockstore) PutMany(blks []blocks.Block) error {
for _, blk := range blks {
err := bs.store.Put(blk.Cid().Bytes(), blk.RawData())
// suppress key exist error because this is not expected behavior for a blockstore
if err != nil && err != types.ErrKeyExists {
return err
}
}
return nil
}
// AllKeysChan returns a channel from which
// the CIDs in the Blockstore can be read. It should respect
// the given context, closing the channel if it becomes Done.
func (bs *HashedBlockstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
return nil, ErrNotSupported
}
// HashOnRead specifies if every read block should be
// rehashed to make sure it matches its CID.
func (bs *HashedBlockstore) HashOnRead(enabled bool) {
}
func (bs *HashedBlockstore) Start() {
bs.store.Start()
}
func (bs *HashedBlockstore) Close() {
bs.store.Close()
}
var _ bstore.Blockstore = &HashedBlockstore{}
// ErrOutOfBounds indicates the bucket index was greater than the number of bucks
const ErrOutOfBounds = types.ErrOutOfBounds
// ErrIndexTooLarge indicates the maximum supported bucket size is 32-bits
const ErrIndexTooLarge = types.ErrIndexTooLarge
const ErrKeyTooShort = types.ErrKeyTooShort
const ErrKeyExists = types.ErrKeyExists
type ErrIndexWrongBitSize = types.ErrIndexWrongBitSize