forked from ipfs/go-ipfs-blockstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
idstore.go
123 lines (106 loc) · 2.54 KB
/
idstore.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
package blockstore
import (
"context"
"io"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
mh "github.com/multiformats/go-multihash"
)
// idstore wraps a BlockStore to add support for identity hashes
type idstore struct {
bs Blockstore
viewer Viewer
}
var _ Blockstore = (*idstore)(nil)
var _ Viewer = (*idstore)(nil)
var _ io.Closer = (*idstore)(nil)
func NewIdStore(bs Blockstore) Blockstore {
ids := &idstore{bs: bs}
if v, ok := bs.(Viewer); ok {
ids.viewer = v
}
return ids
}
func extractContents(k cid.Cid) (bool, []byte) {
// Pre-check by calling Prefix(), this much faster than extracting the hash.
if k.Prefix().MhType != mh.IDENTITY {
return false, nil
}
dmh, err := mh.Decode(k.Hash())
if err != nil || dmh.Code != mh.IDENTITY {
return false, nil
}
return true, dmh.Digest
}
func (b *idstore) DeleteBlock(ctx context.Context, k cid.Cid) error {
isId, _ := extractContents(k)
if isId {
return nil
}
return b.bs.DeleteBlock(ctx, k)
}
func (b *idstore) Has(ctx context.Context, k cid.Cid) (bool, error) {
isId, _ := extractContents(k)
if isId {
return true, nil
}
return b.bs.Has(ctx, k)
}
func (b *idstore) View(ctx context.Context, k cid.Cid, callback func([]byte) error) error {
if b.viewer == nil {
blk, err := b.Get(ctx, k)
if err != nil {
return err
}
return callback(blk.RawData())
}
isId, bdata := extractContents(k)
if isId {
return callback(bdata)
}
return b.viewer.View(ctx, k, callback)
}
func (b *idstore) GetSize(ctx context.Context, k cid.Cid) (int, error) {
isId, bdata := extractContents(k)
if isId {
return len(bdata), nil
}
return b.bs.GetSize(ctx, k)
}
func (b *idstore) Get(ctx context.Context, k cid.Cid) (blocks.Block, error) {
isId, bdata := extractContents(k)
if isId {
return blocks.NewBlockWithCid(bdata, k)
}
return b.bs.Get(ctx, k)
}
func (b *idstore) Put(ctx context.Context, bl blocks.Block) error {
isId, _ := extractContents(bl.Cid())
if isId {
return nil
}
return b.bs.Put(ctx, bl)
}
func (b *idstore) PutMany(ctx context.Context, bs []blocks.Block) error {
toPut := make([]blocks.Block, 0, len(bs))
for _, bl := range bs {
isId, _ := extractContents(bl.Cid())
if isId {
continue
}
toPut = append(toPut, bl)
}
return b.bs.PutMany(ctx, toPut)
}
func (b *idstore) HashOnRead(enabled bool) {
b.bs.HashOnRead(enabled)
}
func (b *idstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
return b.bs.AllKeysChan(ctx)
}
func (b *idstore) Close() error {
if c, ok := b.bs.(io.Closer); ok {
return c.Close()
}
return nil
}