blockstore.go 3.73 KB
Newer Older
1 2 3 4 5 6 7
// package blockstore implements a thin wrapper over a datastore, giving a
// clean interface for Getting and Putting block objects.
package blockstore

import (
	"errors"

8 9 10 11 12 13
	ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
	dsns "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/namespace"
	dsq "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/query"
	mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
	context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
	blocks "github.com/ipfs/go-ipfs/blocks"
14
	key "github.com/ipfs/go-ipfs/blocks/key"
15
	eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog"
16 17
)

18 19
var log = eventlog.Logger("blockstore")

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
20
// BlockPrefix namespaces blockstore datastores
21
var BlockPrefix = ds.NewKey("blocks")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
22

23 24
var ValueTypeMismatch = errors.New("The retrieved value is not a Block")

25 26
var ErrNotFound = errors.New("blockstore: block not found")

Brian Tiger Chow's avatar
Brian Tiger Chow committed
27
// Blockstore wraps a ThreadSafeDatastore
28
type Blockstore interface {
29 30 31
	DeleteBlock(key.Key) error
	Has(key.Key) (bool, error)
	Get(key.Key) (*blocks.Block, error)
32
	Put(*blocks.Block) error
33

34
	AllKeysChan(ctx context.Context) (<-chan key.Key, error)
35 36 37
}

func NewBlockstore(d ds.ThreadSafeDatastore) Blockstore {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38
	dd := dsns.Wrap(d, BlockPrefix)
39
	return &blockstore{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40
		datastore: dd,
41 42 43 44
	}
}

type blockstore struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
45 46 47
	datastore ds.Datastore
	// cant be ThreadSafeDatastore cause namespace.Datastore doesnt support it.
	// we do check it on `NewBlockstore` though.
48 49
}

50
func (bs *blockstore) Get(k key.Key) (*blocks.Block, error) {
51
	maybeData, err := bs.datastore.Get(k.DsKey())
52 53 54
	if err == ds.ErrNotFound {
		return nil, ErrNotFound
	}
55 56 57 58 59 60 61 62 63 64 65 66
	if err != nil {
		return nil, err
	}
	bdata, ok := maybeData.([]byte)
	if !ok {
		return nil, ValueTypeMismatch
	}

	return blocks.NewBlockWithHash(bdata, mh.Multihash(k))
}

func (bs *blockstore) Put(block *blocks.Block) error {
67
	k := block.Key().DsKey()
68 69

	// Has is cheaper than Put, so see if we already have it
70
	exists, err := bs.datastore.Has(k)
71
	if err == nil && exists {
72 73 74
		return nil // already stored.
	}
	return bs.datastore.Put(k, block.Data)
75
}
76

77
func (bs *blockstore) Has(k key.Key) (bool, error) {
78 79 80
	return bs.datastore.Has(k.DsKey())
}

81
func (s *blockstore) DeleteBlock(k key.Key) error {
82 83
	return s.datastore.Delete(k.DsKey())
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
84

85
// AllKeysChan runs a query for keys from the blockstore.
86 87
// this is very simplistic, in the future, take dsq.Query as a param?
//
88
// AllKeysChan respects context
89
func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan key.Key, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90 91

	// KeysOnly, because that would be _a lot_ of data.
92
	q := dsq.Query{KeysOnly: true}
93 94
	// datastore/namespace does *NOT* fix up Query.Prefix
	q.Prefix = BlockPrefix.String()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
95 96 97 98 99
	res, err := bs.datastore.Query(q)
	if err != nil {
		return nil, err
	}

100
	// this function is here to compartmentalize
101
	get := func() (k key.Key, ok bool) {
102 103 104 105 106 107 108 109 110 111 112 113
		select {
		case <-ctx.Done():
			return k, false
		case e, more := <-res.Next():
			if !more {
				return k, false
			}
			if e.Error != nil {
				log.Debug("blockstore.AllKeysChan got err:", e.Error)
				return k, false
			}

114 115
			// need to convert to key.Key using key.KeyFromDsKey.
			k = key.KeyFromDsKey(ds.NewKey(e.Key))
116
			log.Debug("blockstore: query got key", k)
117 118 119 120 121 122 123

			// key must be a multihash. else ignore it.
			_, err := mh.Cast([]byte(k))
			if err != nil {
				return "", true
			}

124 125
			return k, true
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
126
	}
127

128
	output := make(chan key.Key)
129 130 131 132 133 134 135 136 137 138 139
	go func() {
		defer func() {
			res.Process().Close() // ensure exit (signals early exit, too)
			close(output)
		}()

		for {
			k, ok := get()
			if !ok {
				return
			}
140 141 142
			if k == "" {
				continue
			}
143 144 145 146 147 148 149 150 151 152

			select {
			case <-ctx.Done():
				return
			case output <- k:
			}
		}
	}()

	return output, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
153
}