idstore.go 2.36 KB
Newer Older
1 2 3 4
package blockstore

import (
	"context"
5
	"io"
6 7

	mh "github.com/multiformats/go-multihash"
8 9
	blocks "gitlab.dms3.io/dms3/go-block-format"
	cid "gitlab.dms3.io/dms3/go-cid"
10 11 12 13
)

// idstore wraps a BlockStore to add support for identity hashes
type idstore struct {
14 15
	bs     Blockstore
	viewer Viewer
16 17
}

18 19
var _ Blockstore = (*idstore)(nil)
var _ Viewer = (*idstore)(nil)
20
var _ io.Closer = (*idstore)(nil)
21

Kevin Atkinson's avatar
Kevin Atkinson committed
22
func NewIdStore(bs Blockstore) Blockstore {
23 24 25 26 27
	ids := &idstore{bs: bs}
	if v, ok := bs.(Viewer); ok {
		ids.viewer = v
	}
	return ids
28 29
}

30
func extractContents(k cid.Cid) (bool, []byte) {
Steven Allen's avatar
Steven Allen committed
31 32 33 34 35
	// Pre-check by calling Prefix(), this much faster than extracting the hash.
	if k.Prefix().MhType != mh.IDENTITY {
		return false, nil
	}

36 37 38 39 40 41 42
	dmh, err := mh.Decode(k.Hash())
	if err != nil || dmh.Code != mh.ID {
		return false, nil
	}
	return true, dmh.Digest
}

43
func (b *idstore) DeleteBlock(k cid.Cid) error {
44 45 46 47 48 49 50
	isId, _ := extractContents(k)
	if isId {
		return nil
	}
	return b.bs.DeleteBlock(k)
}

51
func (b *idstore) Has(k cid.Cid) (bool, error) {
52 53 54 55 56 57 58
	isId, _ := extractContents(k)
	if isId {
		return true, nil
	}
	return b.bs.Has(k)
}

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
func (b *idstore) View(k cid.Cid, callback func([]byte) error) error {
	if b.viewer == nil {
		blk, err := b.Get(k)
		if err != nil {
			return err
		}
		return callback(blk.RawData())
	}
	isId, bdata := extractContents(k)
	if isId {
		return callback(bdata)
	}
	return b.viewer.View(k, callback)
}

74
func (b *idstore) GetSize(k cid.Cid) (int, error) {
75 76 77 78 79 80 81
	isId, bdata := extractContents(k)
	if isId {
		return len(bdata), nil
	}
	return b.bs.GetSize(k)
}

82
func (b *idstore) Get(k cid.Cid) (blocks.Block, error) {
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
	isId, bdata := extractContents(k)
	if isId {
		return blocks.NewBlockWithCid(bdata, k)
	}
	return b.bs.Get(k)
}

func (b *idstore) Put(bl blocks.Block) error {
	isId, _ := extractContents(bl.Cid())
	if isId {
		return nil
	}
	return b.bs.Put(bl)
}

func (b *idstore) PutMany(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(toPut)
}

func (b *idstore) HashOnRead(enabled bool) {
	b.bs.HashOnRead(enabled)
}

114
func (b *idstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
115 116
	return b.bs.AllKeysChan(ctx)
}
117 118 119 120 121 122 123

func (b *idstore) Close() error {
	if c, ok := b.bs.(io.Closer); ok {
		return c.Close()
	}
	return nil
}