blockstore.go 3.57 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 14 15
	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"
	eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog"
	u "github.com/ipfs/go-ipfs/util"
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("b")
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
	DeleteBlock(u.Key) error
	Has(u.Key) (bool, error)
31 32
	Get(u.Key) (*blocks.Block, error)
	Put(*blocks.Block) error
33

34
	AllKeysChan(ctx context.Context) (<-chan u.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 51
}

func (bs *blockstore) Get(k u.Key) (*blocks.Block, error) {
	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 68 69 70 71 72 73
	// Has is cheaper than
	k := block.Key().DsKey()
	exists, err := bs.datastore.Has(k)
	if err != nil && exists {
		return nil // already stored.
	}
	return bs.datastore.Put(k, block.Data)
74
}
75 76 77 78 79 80 81 82

func (bs *blockstore) Has(k u.Key) (bool, error) {
	return bs.datastore.Has(k.DsKey())
}

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

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

	// KeysOnly, because that would be _a lot_ of data.
91
	q := dsq.Query{KeysOnly: true}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
92 93 94 95 96
	res, err := bs.datastore.Query(q)
	if err != nil {
		return nil, err
	}

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	// this function is here to compartmentalize
	get := func() (k u.Key, ok bool) {
		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
			}

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

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

121 122
			return k, true
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
123
	}
124 125 126 127 128 129 130 131 132 133 134 135 136

	output := make(chan u.Key)
	go func() {
		defer func() {
			res.Process().Close() // ensure exit (signals early exit, too)
			close(output)
		}()

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

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

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