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

import (
	"errors"
Jeromy's avatar
Jeromy committed
7
	"sync"
8

9 10 11 12 13 14
	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"
15
	key "github.com/ipfs/go-ipfs/blocks/key"
Jeromy's avatar
Jeromy committed
16
	logging "github.com/ipfs/go-ipfs/vendor/QmQg1J6vikuXF9oDvm4wpdeAUvvkVEKW1EYDw9HhTMnP2b/go-log"
17 18
)

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21
// BlockPrefix namespaces blockstore datastores
22
var BlockPrefix = ds.NewKey("blocks")
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")

Jeromy's avatar
Jeromy committed
28
// Blockstore wraps a Datastore
29
type Blockstore interface {
30 31 32
	DeleteBlock(key.Key) error
	Has(key.Key) (bool, error)
	Get(key.Key) (*blocks.Block, error)
33
	Put(*blocks.Block) error
34
	PutMany([]*blocks.Block) error
35

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

Jeromy's avatar
Jeromy committed
39 40 41
type GCBlockstore interface {
	Blockstore

42 43 44 45 46 47 48 49 50 51
	// GCLock locks the blockstore for garbage collection. No operations
	// that expect to finish with a pin should ocurr simultaneously.
	// Reading during GC is safe, and requires no lock.
	GCLock() func()

	// PinLock locks the blockstore for sequences of puts expected to finish
	// with a pin (before GC). Multiple put->pin sequences can write through
	// at the same time, but no GC should not happen simulatenously.
	// Reading during Pinning is safe, and requires no lock.
	PinLock() func()
Jeromy's avatar
Jeromy committed
52 53
}

Jeromy's avatar
Jeromy committed
54
func NewBlockstore(d ds.Batching) *blockstore {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
55
	dd := dsns.Wrap(d, BlockPrefix)
56
	return &blockstore{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
57
		datastore: dd,
58 59 60 61
	}
}

type blockstore struct {
Jeromy's avatar
Jeromy committed
62
	datastore ds.Batching
Jeromy's avatar
Jeromy committed
63 64

	lk sync.RWMutex
65 66
}

67
func (bs *blockstore) Get(k key.Key) (*blocks.Block, error) {
68
	maybeData, err := bs.datastore.Get(k.DsKey())
69 70 71
	if err == ds.ErrNotFound {
		return nil, ErrNotFound
	}
72 73 74 75 76 77 78 79 80 81 82 83
	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 {
84
	k := block.Key().DsKey()
85 86

	// Has is cheaper than Put, so see if we already have it
87
	exists, err := bs.datastore.Has(k)
88
	if err == nil && exists {
89 90 91
		return nil // already stored.
	}
	return bs.datastore.Put(k, block.Data)
92
}
93

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
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()
}

114
func (bs *blockstore) Has(k key.Key) (bool, error) {
115 116 117
	return bs.datastore.Has(k.DsKey())
}

118
func (s *blockstore) DeleteBlock(k key.Key) error {
119 120
	return s.datastore.Delete(k.DsKey())
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
121

122
// AllKeysChan runs a query for keys from the blockstore.
123 124
// this is very simplistic, in the future, take dsq.Query as a param?
//
125
// AllKeysChan respects context
126
func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan key.Key, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
127 128

	// KeysOnly, because that would be _a lot_ of data.
129
	q := dsq.Query{KeysOnly: true}
130 131
	// datastore/namespace does *NOT* fix up Query.Prefix
	q.Prefix = BlockPrefix.String()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
132 133 134 135 136
	res, err := bs.datastore.Query(q)
	if err != nil {
		return nil, err
	}

137
	// this function is here to compartmentalize
138
	get := func() (k key.Key, ok bool) {
139 140 141 142 143 144 145 146 147 148 149 150
		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
			}

151 152
			// need to convert to key.Key using key.KeyFromDsKey.
			k = key.KeyFromDsKey(ds.NewKey(e.Key))
153
			log.Debug("blockstore: query got key", k)
154 155 156 157 158 159 160

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

161 162
			return k, true
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
163
	}
164

165
	output := make(chan key.Key)
166 167 168 169 170 171 172 173 174 175 176
	go func() {
		defer func() {
			res.Process().Close() // ensure exit (signals early exit, too)
			close(output)
		}()

		for {
			k, ok := get()
			if !ok {
				return
			}
177 178 179
			if k == "" {
				continue
			}
180 181 182 183 184 185 186 187 188 189

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

	return output, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
190
}
Jeromy's avatar
Jeromy committed
191

192
func (bs *blockstore) GCLock() func() {
Jeromy's avatar
Jeromy committed
193 194 195 196
	bs.lk.Lock()
	return bs.lk.Unlock
}

197
func (bs *blockstore) PinLock() func() {
Jeromy's avatar
Jeromy committed
198 199 200
	bs.lk.RLock()
	return bs.lk.RUnlock
}