From be41444a9e98ed7224168358cff572fdf9b24ed0 Mon Sep 17 00:00:00 2001 From: Brian Tiger Chow <brian.holderchow@gmail.com> Date: Fri, 23 Jan 2015 22:03:05 -0800 Subject: [PATCH] provide simple wrapper methods for AllKeysRange @jbenet @whyrusleeping was the 1<<16 intentional? replaced the raw methods with wrappers. --- blocks/blockstore/blockstore.go | 29 +++++++++++++++++++--------- blocks/blockstore/blockstore_test.go | 6 +++--- blocks/blockstore/write_cache.go | 16 +++++++++++---- core/commands/refs.go | 2 +- core/corerepo/gc.go | 2 +- exchange/reprovide/reprovide.go | 2 +- 6 files changed, 38 insertions(+), 19 deletions(-) diff --git a/blocks/blockstore/blockstore.go b/blocks/blockstore/blockstore.go index 3c98b0735..70a705884 100644 --- a/blocks/blockstore/blockstore.go +++ b/blocks/blockstore/blockstore.go @@ -32,8 +32,11 @@ type Blockstore interface { Get(u.Key) (*blocks.Block, error) Put(*blocks.Block) error - AllKeys(ctx context.Context, offset int, limit int) ([]u.Key, error) - AllKeysChan(ctx context.Context, offset int, limit int) (<-chan u.Key, error) + AllKeys(ctx context.Context) ([]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 { @@ -83,14 +86,22 @@ func (s *blockstore) DeleteBlock(k u.Key) error { 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? // if offset and limit are 0, they are ignored. // -// AllKeys respects context -func (bs *blockstore) AllKeys(ctx context.Context, offset int, limit int) ([]u.Key, error) { +// AllKeysRange respects context +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 { return nil, err } @@ -102,12 +113,12 @@ func (bs *blockstore) AllKeys(ctx context.Context, offset int, limit int) ([]u.K 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? // if offset and limit are 0, they are ignored. // -// AllKeys respects context -func (bs *blockstore) AllKeysChan(ctx context.Context, offset int, limit int) (<-chan u.Key, error) { +// AllKeysRangeChan respects context +func (bs *blockstore) AllKeysRangeChan(ctx context.Context, offset int, limit int) (<-chan u.Key, error) { // KeysOnly, because that would be _a lot_ of data. q := dsq.Query{KeysOnly: true, Offset: offset, Limit: limit} diff --git a/blocks/blockstore/blockstore_test.go b/blocks/blockstore/blockstore_test.go index 44f5964e8..2280f78f8 100644 --- a/blocks/blockstore/blockstore_test.go +++ b/blocks/blockstore/blockstore_test.go @@ -67,7 +67,7 @@ func TestAllKeysSimple(t *testing.T) { bs, keys := newBlockStoreWithKeys(t, nil, 100) ctx := context.Background() - keys2, err := bs.AllKeys(ctx, 0, 0) + keys2, err := bs.AllKeys(ctx) if err != nil { t.Fatal(err) } @@ -83,7 +83,7 @@ func TestAllKeysOffsetAndLimit(t *testing.T) { bs, _ := newBlockStoreWithKeys(t, nil, N) ctx := context.Background() - keys3, err := bs.AllKeys(ctx, N/3, N/3) + keys3, err := bs.AllKeysRange(ctx, N/3, N/3) if err != nil { t.Fatal(err) } @@ -107,7 +107,7 @@ func TestAllKeysRespectsContext(t *testing.T) { getKeys := func(ctx context.Context) { started <- struct{}{} - _, err := bs.AllKeys(ctx, 0, 0) // once without cancelling + _, err := bs.AllKeys(ctx) // once without cancelling if err != nil { errors <- err } diff --git a/blocks/blockstore/write_cache.go b/blocks/blockstore/write_cache.go index 377ce629d..487899597 100644 --- a/blocks/blockstore/write_cache.go +++ b/blocks/blockstore/write_cache.go @@ -46,10 +46,18 @@ func (w *writecache) Put(b *blocks.Block) error { return w.blockstore.Put(b) } -func (w *writecache) AllKeys(ctx context.Context, offset int, limit int) ([]u.Key, error) { - return w.blockstore.AllKeys(ctx, offset, limit) +func (w *writecache) AllKeys(ctx context.Context) ([]u.Key, error) { + return w.blockstore.AllKeysRange(ctx, 0, 0) } -func (w *writecache) AllKeysChan(ctx context.Context, offset int, limit int) (<-chan u.Key, error) { - return w.blockstore.AllKeysChan(ctx, offset, limit) +func (w *writecache) AllKeysChan(ctx context.Context) (<-chan u.Key, error) { + 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) } diff --git a/core/commands/refs.go b/core/commands/refs.go index 36f5db026..d599dc6de 100644 --- a/core/commands/refs.go +++ b/core/commands/refs.go @@ -137,7 +137,7 @@ Displays the hashes of all local objects. } // todo: make async - allKeys, err := n.Blockstore.AllKeysChan(ctx, 0, 0) + allKeys, err := n.Blockstore.AllKeysChan(ctx) if err != nil { res.SetError(err, cmds.ErrNormal) return diff --git a/core/corerepo/gc.go b/core/corerepo/gc.go index 1ba047c41..14a2ce810 100644 --- a/core/corerepo/gc.go +++ b/core/corerepo/gc.go @@ -16,7 +16,7 @@ type KeyRemoved struct { func GarbageCollectBlockstore(n *core.IpfsNode, ctx context.Context) (<-chan *KeyRemoved, error) { - keychan, err := n.Blockstore.AllKeysChan(ctx, 0, 1<<16) + keychan, err := n.Blockstore.AllKeysChan(ctx) if err != nil { return nil, err } diff --git a/exchange/reprovide/reprovide.go b/exchange/reprovide/reprovide.go index 1534b4ec4..bd4c84fad 100644 --- a/exchange/reprovide/reprovide.go +++ b/exchange/reprovide/reprovide.go @@ -49,7 +49,7 @@ func (rp *Reprovider) ProvideEvery(ctx context.Context, tick time.Duration) { } func (rp *Reprovider) Reprovide(ctx context.Context) error { - keychan, err := rp.bstore.AllKeysChan(ctx, 0, 1<<16) + keychan, err := rp.bstore.AllKeysChan(ctx) if err != nil { return debugerror.Errorf("Failed to get key chan from blockstore: %s", err) } -- GitLab