blockstore.go 4.14 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
	logging "github.com/ipfs/go-ipfs/vendor/QmTBXYb6y2ZcJmoXVKk3pf9rzSEjbCg7tQaJW7RSuH14nv/go-log"
16 17
)

Jeromy's avatar
Jeromy committed
18
var log = logging.Logger("blockstore")
19

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
	PutMany([]*blocks.Block) error
34

35
	AllKeysChan(ctx context.Context) (<-chan key.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 {
Jeromy's avatar
Jeromy committed
46
	datastore ds.Batching
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
47 48
	// cant be ThreadSafeDatastore cause namespace.Datastore doesnt support it.
	// we do check it on `NewBlockstore` though.
49 50
}

51
func (bs *blockstore) Get(k key.Key) (*blocks.Block, error) {
52
	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
	k := block.Key().DsKey()
69 70

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

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
func (bs *blockstore) PutMany(blocks []*blocks.Block) error {
	t, err := bs.datastore.Batch()
	if err != nil {
		return err
	}
	for _, b := range blocks {
		k := b.Key().DsKey()
		exists, err := bs.datastore.Has(k)
		if err == nil && exists {
			continue
		}

		err = t.Put(k, b.Data)
		if err != nil {
			return err
		}
	}
	return t.Commit()
}

98
func (bs *blockstore) Has(k key.Key) (bool, error) {
99 100 101
	return bs.datastore.Has(k.DsKey())
}

102
func (s *blockstore) DeleteBlock(k key.Key) error {
103 104
	return s.datastore.Delete(k.DsKey())
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
105

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

	// KeysOnly, because that would be _a lot_ of data.
113
	q := dsq.Query{KeysOnly: true}
114 115
	// datastore/namespace does *NOT* fix up Query.Prefix
	q.Prefix = BlockPrefix.String()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
116 117 118 119 120
	res, err := bs.datastore.Query(q)
	if err != nil {
		return nil, err
	}

121
	// this function is here to compartmentalize
122
	get := func() (k key.Key, ok bool) {
123 124 125 126 127 128 129 130 131 132 133 134
		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
			}

135 136
			// need to convert to key.Key using key.KeyFromDsKey.
			k = key.KeyFromDsKey(ds.NewKey(e.Key))
137
			log.Debug("blockstore: query got key", k)
138 139 140 141 142 143 144

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

145 146
			return k, true
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
147
	}
148

149
	output := make(chan key.Key)
150 151 152 153 154 155 156 157 158 159 160
	go func() {
		defer func() {
			res.Process().Close() // ensure exit (signals early exit, too)
			close(output)
		}()

		for {
			k, ok := get()
			if !ok {
				return
			}
161 162 163
			if k == "" {
				continue
			}
164 165 166 167 168 169 170 171 172 173

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

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