Commit 67f1e932 authored by Jeromy's avatar Jeromy

move util.Key into its own package under blocks

parent eb50103b
...@@ -11,8 +11,8 @@ import ( ...@@ -11,8 +11,8 @@ import (
mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash" 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" context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
blocks "github.com/ipfs/go-ipfs/blocks" blocks "github.com/ipfs/go-ipfs/blocks"
key "github.com/ipfs/go-ipfs/blocks/key"
eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog" eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog"
u "github.com/ipfs/go-ipfs/util"
) )
var log = eventlog.Logger("blockstore") var log = eventlog.Logger("blockstore")
...@@ -26,12 +26,12 @@ var ErrNotFound = errors.New("blockstore: block not found") ...@@ -26,12 +26,12 @@ var ErrNotFound = errors.New("blockstore: block not found")
// Blockstore wraps a ThreadSafeDatastore // Blockstore wraps a ThreadSafeDatastore
type Blockstore interface { type Blockstore interface {
DeleteBlock(u.Key) error DeleteBlock(key.Key) error
Has(u.Key) (bool, error) Has(key.Key) (bool, error)
Get(u.Key) (*blocks.Block, error) Get(key.Key) (*blocks.Block, error)
Put(*blocks.Block) error Put(*blocks.Block) error
AllKeysChan(ctx context.Context) (<-chan u.Key, error) AllKeysChan(ctx context.Context) (<-chan key.Key, error)
} }
func NewBlockstore(d ds.ThreadSafeDatastore) Blockstore { func NewBlockstore(d ds.ThreadSafeDatastore) Blockstore {
...@@ -47,7 +47,7 @@ type blockstore struct { ...@@ -47,7 +47,7 @@ type blockstore struct {
// we do check it on `NewBlockstore` though. // we do check it on `NewBlockstore` though.
} }
func (bs *blockstore) Get(k u.Key) (*blocks.Block, error) { func (bs *blockstore) Get(k key.Key) (*blocks.Block, error) {
maybeData, err := bs.datastore.Get(k.DsKey()) maybeData, err := bs.datastore.Get(k.DsKey())
if err == ds.ErrNotFound { if err == ds.ErrNotFound {
return nil, ErrNotFound return nil, ErrNotFound
...@@ -74,11 +74,11 @@ func (bs *blockstore) Put(block *blocks.Block) error { ...@@ -74,11 +74,11 @@ func (bs *blockstore) Put(block *blocks.Block) error {
return bs.datastore.Put(k, block.Data) return bs.datastore.Put(k, block.Data)
} }
func (bs *blockstore) Has(k u.Key) (bool, error) { func (bs *blockstore) Has(k key.Key) (bool, error) {
return bs.datastore.Has(k.DsKey()) return bs.datastore.Has(k.DsKey())
} }
func (s *blockstore) DeleteBlock(k u.Key) error { func (s *blockstore) DeleteBlock(k key.Key) error {
return s.datastore.Delete(k.DsKey()) return s.datastore.Delete(k.DsKey())
} }
...@@ -86,7 +86,7 @@ func (s *blockstore) DeleteBlock(k u.Key) error { ...@@ -86,7 +86,7 @@ func (s *blockstore) DeleteBlock(k u.Key) error {
// this is very simplistic, in the future, take dsq.Query as a param? // this is very simplistic, in the future, take dsq.Query as a param?
// //
// AllKeysChan respects context // AllKeysChan respects context
func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan u.Key, error) { func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan key.Key, error) {
// KeysOnly, because that would be _a lot_ of data. // KeysOnly, because that would be _a lot_ of data.
q := dsq.Query{KeysOnly: true} q := dsq.Query{KeysOnly: true}
...@@ -98,7 +98,7 @@ func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan u.Key, error) { ...@@ -98,7 +98,7 @@ func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan u.Key, error) {
} }
// this function is here to compartmentalize // this function is here to compartmentalize
get := func() (k u.Key, ok bool) { get := func() (k key.Key, ok bool) {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return k, false return k, false
...@@ -111,8 +111,8 @@ func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan u.Key, error) { ...@@ -111,8 +111,8 @@ func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan u.Key, error) {
return k, false return k, false
} }
// need to convert to u.Key using u.KeyFromDsKey. // need to convert to key.Key using key.KeyFromDsKey.
k = u.KeyFromDsKey(ds.NewKey(e.Key)) k = key.KeyFromDsKey(ds.NewKey(e.Key))
log.Debug("blockstore: query got key", k) log.Debug("blockstore: query got key", k)
// key must be a multihash. else ignore it. // key must be a multihash. else ignore it.
...@@ -125,7 +125,7 @@ func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan u.Key, error) { ...@@ -125,7 +125,7 @@ func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan u.Key, error) {
} }
} }
output := make(chan u.Key) output := make(chan key.Key)
go func() { go func() {
defer func() { defer func() {
res.Process().Close() // ensure exit (signals early exit, too) res.Process().Close() // ensure exit (signals early exit, too)
......
...@@ -11,14 +11,14 @@ import ( ...@@ -11,14 +11,14 @@ import (
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
blocks "github.com/ipfs/go-ipfs/blocks" blocks "github.com/ipfs/go-ipfs/blocks"
u "github.com/ipfs/go-ipfs/util" key "github.com/ipfs/go-ipfs/blocks/key"
) )
// TODO(brian): TestGetReturnsNil // TODO(brian): TestGetReturnsNil
func TestGetWhenKeyNotPresent(t *testing.T) { func TestGetWhenKeyNotPresent(t *testing.T) {
bs := NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore())) bs := NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
_, err := bs.Get(u.Key("not present")) _, err := bs.Get(key.Key("not present"))
if err != nil { if err != nil {
t.Log("As expected, block is not present") t.Log("As expected, block is not present")
...@@ -45,13 +45,13 @@ func TestPutThenGetBlock(t *testing.T) { ...@@ -45,13 +45,13 @@ func TestPutThenGetBlock(t *testing.T) {
} }
} }
func newBlockStoreWithKeys(t *testing.T, d ds.Datastore, N int) (Blockstore, []u.Key) { func newBlockStoreWithKeys(t *testing.T, d ds.Datastore, N int) (Blockstore, []key.Key) {
if d == nil { if d == nil {
d = ds.NewMapDatastore() d = ds.NewMapDatastore()
} }
bs := NewBlockstore(ds_sync.MutexWrap(d)) bs := NewBlockstore(ds_sync.MutexWrap(d))
keys := make([]u.Key, N) keys := make([]key.Key, N)
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
block := blocks.NewBlock([]byte(fmt.Sprintf("some data %d", i))) block := blocks.NewBlock([]byte(fmt.Sprintf("some data %d", i)))
err := bs.Put(block) err := bs.Put(block)
...@@ -63,8 +63,8 @@ func newBlockStoreWithKeys(t *testing.T, d ds.Datastore, N int) (Blockstore, []u ...@@ -63,8 +63,8 @@ func newBlockStoreWithKeys(t *testing.T, d ds.Datastore, N int) (Blockstore, []u
return bs, keys return bs, keys
} }
func collect(ch <-chan u.Key) []u.Key { func collect(ch <-chan key.Key) []key.Key {
var keys []u.Key var keys []key.Key
for k := range ch { for k := range ch {
keys = append(keys, k) keys = append(keys, k)
} }
...@@ -219,7 +219,7 @@ func TestValueTypeMismatch(t *testing.T) { ...@@ -219,7 +219,7 @@ func TestValueTypeMismatch(t *testing.T) {
} }
} }
func expectMatches(t *testing.T, expect, actual []u.Key) { func expectMatches(t *testing.T, expect, actual []key.Key) {
if len(expect) != len(actual) { if len(expect) != len(actual) {
t.Errorf("expect and actual differ: %d != %d", len(expect), len(actual)) t.Errorf("expect and actual differ: %d != %d", len(expect), len(actual))
......
...@@ -4,7 +4,7 @@ import ( ...@@ -4,7 +4,7 @@ import (
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/hashicorp/golang-lru" "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/hashicorp/golang-lru"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
"github.com/ipfs/go-ipfs/blocks" "github.com/ipfs/go-ipfs/blocks"
u "github.com/ipfs/go-ipfs/util" key "github.com/ipfs/go-ipfs/blocks/key"
) )
// WriteCached returns a blockstore that caches up to |size| unique writes (bs.Put). // WriteCached returns a blockstore that caches up to |size| unique writes (bs.Put).
...@@ -21,19 +21,19 @@ type writecache struct { ...@@ -21,19 +21,19 @@ type writecache struct {
blockstore Blockstore blockstore Blockstore
} }
func (w *writecache) DeleteBlock(k u.Key) error { func (w *writecache) DeleteBlock(k key.Key) error {
w.cache.Remove(k) w.cache.Remove(k)
return w.blockstore.DeleteBlock(k) return w.blockstore.DeleteBlock(k)
} }
func (w *writecache) Has(k u.Key) (bool, error) { func (w *writecache) Has(k key.Key) (bool, error) {
if _, ok := w.cache.Get(k); ok { if _, ok := w.cache.Get(k); ok {
return true, nil return true, nil
} }
return w.blockstore.Has(k) return w.blockstore.Has(k)
} }
func (w *writecache) Get(k u.Key) (*blocks.Block, error) { func (w *writecache) Get(k key.Key) (*blocks.Block, error) {
return w.blockstore.Get(k) return w.blockstore.Get(k)
} }
...@@ -45,6 +45,6 @@ func (w *writecache) Put(b *blocks.Block) error { ...@@ -45,6 +45,6 @@ func (w *writecache) Put(b *blocks.Block) error {
return w.blockstore.Put(b) return w.blockstore.Put(b)
} }
func (w *writecache) AllKeysChan(ctx context.Context) (<-chan u.Key, error) { func (w *writecache) AllKeysChan(ctx context.Context) (<-chan key.Key, error) {
return w.blockstore.AllKeysChan(ctx) return w.blockstore.AllKeysChan(ctx)
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment