Commit 67f1e932 authored by Jeromy's avatar Jeromy

move util.Key into its own package under blocks

parent eb50103b
......@@ -11,8 +11,8 @@ import (
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"
key "github.com/ipfs/go-ipfs/blocks/key"
eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog"
u "github.com/ipfs/go-ipfs/util"
)
var log = eventlog.Logger("blockstore")
......@@ -26,12 +26,12 @@ var ErrNotFound = errors.New("blockstore: block not found")
// Blockstore wraps a ThreadSafeDatastore
type Blockstore interface {
DeleteBlock(u.Key) error
Has(u.Key) (bool, error)
Get(u.Key) (*blocks.Block, error)
DeleteBlock(key.Key) error
Has(key.Key) (bool, error)
Get(key.Key) (*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 {
......@@ -47,7 +47,7 @@ type blockstore struct {
// 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())
if err == ds.ErrNotFound {
return nil, ErrNotFound
......@@ -74,11 +74,11 @@ func (bs *blockstore) Put(block *blocks.Block) error {
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())
}
func (s *blockstore) DeleteBlock(k u.Key) error {
func (s *blockstore) DeleteBlock(k key.Key) error {
return s.datastore.Delete(k.DsKey())
}
......@@ -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?
//
// 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.
q := dsq.Query{KeysOnly: true}
......@@ -98,7 +98,7 @@ func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan u.Key, error) {
}
// this function is here to compartmentalize
get := func() (k u.Key, ok bool) {
get := func() (k key.Key, ok bool) {
select {
case <-ctx.Done():
return k, false
......@@ -111,8 +111,8 @@ func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan u.Key, error) {
return k, false
}
// need to convert to u.Key using u.KeyFromDsKey.
k = u.KeyFromDsKey(ds.NewKey(e.Key))
// need to convert to key.Key using key.KeyFromDsKey.
k = key.KeyFromDsKey(ds.NewKey(e.Key))
log.Debug("blockstore: query got key", k)
// key must be a multihash. else ignore it.
......@@ -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() {
defer func() {
res.Process().Close() // ensure exit (signals early exit, too)
......
......@@ -11,14 +11,14 @@ import (
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
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
func TestGetWhenKeyNotPresent(t *testing.T) {
bs := NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
_, err := bs.Get(u.Key("not present"))
_, err := bs.Get(key.Key("not present"))
if err != nil {
t.Log("As expected, block is not present")
......@@ -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 {
d = ds.NewMapDatastore()
}
bs := NewBlockstore(ds_sync.MutexWrap(d))
keys := make([]u.Key, N)
keys := make([]key.Key, N)
for i := 0; i < N; i++ {
block := blocks.NewBlock([]byte(fmt.Sprintf("some data %d", i)))
err := bs.Put(block)
......@@ -63,8 +63,8 @@ func newBlockStoreWithKeys(t *testing.T, d ds.Datastore, N int) (Blockstore, []u
return bs, keys
}
func collect(ch <-chan u.Key) []u.Key {
var keys []u.Key
func collect(ch <-chan key.Key) []key.Key {
var keys []key.Key
for k := range ch {
keys = append(keys, k)
}
......@@ -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) {
t.Errorf("expect and actual differ: %d != %d", len(expect), len(actual))
......
......@@ -4,7 +4,7 @@ import (
"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"
"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).
......@@ -21,19 +21,19 @@ type writecache struct {
blockstore Blockstore
}
func (w *writecache) DeleteBlock(k u.Key) error {
func (w *writecache) DeleteBlock(k key.Key) error {
w.cache.Remove(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 {
return true, nil
}
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)
}
......@@ -45,6 +45,6 @@ func (w *writecache) Put(b *blocks.Block) error {
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)
}
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