blockstore.go 4.08 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
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
9
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10 11
	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"
12
	mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
13

14 15
	blocks "github.com/jbenet/go-ipfs/blocks"
	u "github.com/jbenet/go-ipfs/util"
16
	eventlog "github.com/jbenet/go-ipfs/util/eventlog"
17 18
)

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

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

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

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

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

	AllKeys(ctx context.Context, offset int, limit int) ([]u.Key, error)
	AllKeysChan(ctx context.Context, offset int, limit int) (<-chan u.Key, error)
37 38 39
}

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

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

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

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
85 86 87 88

// AllKeys runs a query for keys from the blockstore.
// this is very simplistic, in the future, take dsq.Query as a param?
// if offset and limit are 0, they are ignored.
89 90 91 92 93 94 95 96 97
//
// AllKeys respects context
func (bs *blockstore) AllKeys(ctx context.Context, offset int, limit int) ([]u.Key, error) {

	ch, err := bs.AllKeysChan(ctx, offset, limit)
	if err != nil {
		return nil, err
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
98
	var keys []u.Key
99 100 101 102 103 104 105 106 107 108 109 110
	for k := range ch {
		keys = append(keys, k)
	}
	return keys, nil
}

// AllKeys runs a query for keys from the blockstore.
// this is very simplistic, in the future, take dsq.Query as a param?
// if offset and limit are 0, they are ignored.
//
// AllKeys respects context
func (bs *blockstore) AllKeysChan(ctx context.Context, offset int, limit int) (<-chan u.Key, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
111 112 113 114 115 116 117 118

	// KeysOnly, because that would be _a lot_ of data.
	q := dsq.Query{KeysOnly: true, Offset: offset, Limit: limit}
	res, err := bs.datastore.Query(q)
	if err != nil {
		return nil, err
	}

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	// 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))
135
			log.Debug("blockstore: query got key", k)
136 137
			return k, true
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
138
	}
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

	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
			}

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

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