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

import (
	"errors"

	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9 10
	dsns "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/namespace"
	dsq "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/query"
11
	mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
12
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
13
	blocks "github.com/jbenet/go-ipfs/blocks"
14
	eventlog "github.com/jbenet/go-ipfs/thirdparty/eventlog"
15 16 17
	u "github.com/jbenet/go-ipfs/util"
)

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 35
	AllKeys(ctx context.Context) ([]u.Key, error)
	AllKeysChan(ctx context.Context) (<-chan u.Key, error)
36 37 38
}

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

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

func (bs *blockstore) Get(k u.Key) (*blocks.Block, error) {
	maybeData, err := bs.datastore.Get(k.DsKey())
53 54 55
	if err == ds.ErrNotFound {
		return nil, ErrNotFound
	}
56 57 58 59 60 61 62 63 64 65 66 67
	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 {
68 69 70 71 72 73 74
	// 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)
75
}
76 77 78 79 80 81 82 83

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
84

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

91
	ch, err := bs.AllKeysChan(ctx)
92 93 94 95
	if err != nil {
		return nil, err
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
96
	var keys []u.Key
97 98 99 100 101 102
	for k := range ch {
		keys = append(keys, k)
	}
	return keys, nil
}

103
// AllKeysChan runs a query for keys from the blockstore.
104 105
// this is very simplistic, in the future, take dsq.Query as a param?
//
106 107
// AllKeysChan respects context
func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan u.Key, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
108 109

	// KeysOnly, because that would be _a lot_ of data.
110
	q := dsq.Query{KeysOnly: true}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
111 112 113 114 115
	res, err := bs.datastore.Query(q)
	if err != nil {
		return nil, err
	}

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	// 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))
132
			log.Debug("blockstore: query got key", k)
133 134 135 136 137 138 139

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

140 141
			return k, true
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
142
	}
143 144 145 146 147 148 149 150 151 152 153 154 155

	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
			}
156 157 158
			if k == "" {
				continue
			}
159 160 161 162 163 164 165 166 167 168

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

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