Commit 0a26e549 authored by Brian Tiger Chow's avatar Brian Tiger Chow

provide simple wrapper methods for AllKeysRange

@jbenet @whyrusleeping

was the 1<<16 intentional? replaced the raw methods with wrappers.
parent 64d89ae7
...@@ -32,8 +32,11 @@ type Blockstore interface { ...@@ -32,8 +32,11 @@ type Blockstore interface {
Get(u.Key) (*blocks.Block, error) Get(u.Key) (*blocks.Block, error)
Put(*blocks.Block) error Put(*blocks.Block) error
AllKeys(ctx context.Context, offset int, limit int) ([]u.Key, error) AllKeys(ctx context.Context) ([]u.Key, error)
AllKeysChan(ctx context.Context, offset int, limit int) (<-chan u.Key, error) AllKeysChan(ctx context.Context) (<-chan u.Key, error)
AllKeysRange(ctx context.Context, offset int, limit int) ([]u.Key, error)
AllKeysRangeChan(ctx context.Context, offset int, limit int) (<-chan u.Key, error)
} }
func NewBlockstore(d ds.ThreadSafeDatastore) Blockstore { func NewBlockstore(d ds.ThreadSafeDatastore) Blockstore {
...@@ -83,14 +86,22 @@ func (s *blockstore) DeleteBlock(k u.Key) error { ...@@ -83,14 +86,22 @@ func (s *blockstore) DeleteBlock(k u.Key) error {
return s.datastore.Delete(k.DsKey()) return s.datastore.Delete(k.DsKey())
} }
// AllKeys runs a query for keys from the blockstore. func (bs *blockstore) AllKeys(ctx context.Context) ([]u.Key, error) {
return bs.AllKeysRange(ctx, 0, 0)
}
func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan u.Key, error) {
return bs.AllKeysRangeChan(ctx, 0, 0)
}
// AllKeysRange runs a query for keys from the blockstore.
// 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?
// if offset and limit are 0, they are ignored. // if offset and limit are 0, they are ignored.
// //
// AllKeys respects context // AllKeysRange respects context
func (bs *blockstore) AllKeys(ctx context.Context, offset int, limit int) ([]u.Key, error) { func (bs *blockstore) AllKeysRange(ctx context.Context, offset int, limit int) ([]u.Key, error) {
ch, err := bs.AllKeysChan(ctx, offset, limit) ch, err := bs.AllKeysRangeChan(ctx, offset, limit)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -102,12 +113,12 @@ func (bs *blockstore) AllKeys(ctx context.Context, offset int, limit int) ([]u.K ...@@ -102,12 +113,12 @@ func (bs *blockstore) AllKeys(ctx context.Context, offset int, limit int) ([]u.K
return keys, nil return keys, nil
} }
// AllKeys runs a query for keys from the blockstore. // AllKeysRangeChan runs a query for keys from the blockstore.
// 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?
// if offset and limit are 0, they are ignored. // if offset and limit are 0, they are ignored.
// //
// AllKeys respects context // AllKeysRangeChan respects context
func (bs *blockstore) AllKeysChan(ctx context.Context, offset int, limit int) (<-chan u.Key, error) { func (bs *blockstore) AllKeysRangeChan(ctx context.Context, offset int, limit int) (<-chan u.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, Offset: offset, Limit: limit} q := dsq.Query{KeysOnly: true, Offset: offset, Limit: limit}
......
...@@ -67,7 +67,7 @@ func TestAllKeysSimple(t *testing.T) { ...@@ -67,7 +67,7 @@ func TestAllKeysSimple(t *testing.T) {
bs, keys := newBlockStoreWithKeys(t, nil, 100) bs, keys := newBlockStoreWithKeys(t, nil, 100)
ctx := context.Background() ctx := context.Background()
keys2, err := bs.AllKeys(ctx, 0, 0) keys2, err := bs.AllKeys(ctx)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
...@@ -83,7 +83,7 @@ func TestAllKeysOffsetAndLimit(t *testing.T) { ...@@ -83,7 +83,7 @@ func TestAllKeysOffsetAndLimit(t *testing.T) {
bs, _ := newBlockStoreWithKeys(t, nil, N) bs, _ := newBlockStoreWithKeys(t, nil, N)
ctx := context.Background() ctx := context.Background()
keys3, err := bs.AllKeys(ctx, N/3, N/3) keys3, err := bs.AllKeysRange(ctx, N/3, N/3)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
...@@ -107,7 +107,7 @@ func TestAllKeysRespectsContext(t *testing.T) { ...@@ -107,7 +107,7 @@ func TestAllKeysRespectsContext(t *testing.T) {
getKeys := func(ctx context.Context) { getKeys := func(ctx context.Context) {
started <- struct{}{} started <- struct{}{}
_, err := bs.AllKeys(ctx, 0, 0) // once without cancelling _, err := bs.AllKeys(ctx) // once without cancelling
if err != nil { if err != nil {
errors <- err errors <- err
} }
......
...@@ -46,10 +46,18 @@ func (w *writecache) Put(b *blocks.Block) error { ...@@ -46,10 +46,18 @@ func (w *writecache) Put(b *blocks.Block) error {
return w.blockstore.Put(b) return w.blockstore.Put(b)
} }
func (w *writecache) AllKeys(ctx context.Context, offset int, limit int) ([]u.Key, error) { func (w *writecache) AllKeys(ctx context.Context) ([]u.Key, error) {
return w.blockstore.AllKeys(ctx, offset, limit) return w.blockstore.AllKeysRange(ctx, 0, 0)
} }
func (w *writecache) AllKeysChan(ctx context.Context, offset int, limit int) (<-chan u.Key, error) { func (w *writecache) AllKeysChan(ctx context.Context) (<-chan u.Key, error) {
return w.blockstore.AllKeysChan(ctx, offset, limit) return w.blockstore.AllKeysRangeChan(ctx, 0, 0)
}
func (w *writecache) AllKeysRange(ctx context.Context, offset int, limit int) ([]u.Key, error) {
return w.blockstore.AllKeysRange(ctx, offset, limit)
}
func (w *writecache) AllKeysRangeChan(ctx context.Context, offset int, limit int) (<-chan u.Key, error) {
return w.blockstore.AllKeysRangeChan(ctx, offset, limit)
} }
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