idstore.go 2.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
package blockstore

import (
	"context"

	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 {
13 14
	bs     Blockstore
	viewer Viewer
15 16
}

17 18 19
var _ Blockstore = (*idstore)(nil)
var _ Viewer = (*idstore)(nil)

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

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

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

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

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

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
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)
}

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

80
func (b *idstore) Get(k cid.Cid) (blocks.Block, error) {
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
	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)
}

112
func (b *idstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
113 114
	return b.bs.AllKeysChan(ctx)
}