@cryptotaxi247 / kubo / commits / be41444a9

provide simple wrapper methods for AllKeysRange

@jbenet @whyrusleeping was the 1<<16 intentional? replaced the raw methods with wrappers.

Brian Tiger Chow committed Jan 23, 2015 at 22:03 UTC be41444a9e98ed7224168358cff572fdf9b24ed0
6 files changed +38 -19
blocks/blockstore/blockstore.go
+20 -9
@@ -32,8 +32,11 @@ type Blockstore interface {
32 Get(u.Key) (*blocks.Block, error)
33 Put(*blocks.Block) error
34
35 - AllKeys(ctx context.Context, offset int, limit int) ([]u.Key, error)
36 - AllKeysChan(ctx context.Context, offset int, limit int) (<-chan u.Key, error)
35 + AllKeys(ctx context.Context) ([]u.Key, error)
36 + AllKeysChan(ctx context.Context) (<-chan u.Key, error)
37 +
38 + AllKeysRange(ctx context.Context, offset int, limit int) ([]u.Key, error)
39 + AllKeysRangeChan(ctx context.Context, offset int, limit int) (<-chan u.Key, error)
40 }
41
42 func NewBlockstore(d ds.ThreadSafeDatastore) Blockstore {
@@ -83,14 +86,22 @@ func (s *blockstore) DeleteBlock(k u.Key) error {
86 return s.datastore.Delete(k.DsKey())
87 }
88
86 -// AllKeys runs a query for keys from the blockstore.
89 +func (bs *blockstore) AllKeys(ctx context.Context) ([]u.Key, error) {
90 + return bs.AllKeysRange(ctx, 0, 0)
91 +}
92 +
93 +func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan u.Key, error) {
94 + return bs.AllKeysRangeChan(ctx, 0, 0)
95 +}
96 +
97 +// AllKeysRange runs a query for keys from the blockstore.
98 // this is very simplistic, in the future, take dsq.Query as a param?
99 // if offset and limit are 0, they are ignored.
100 //
90 -// AllKeys respects context
91 -func (bs *blockstore) AllKeys(ctx context.Context, offset int, limit int) ([]u.Key, error) {
101 +// AllKeysRange respects context
102 +func (bs *blockstore) AllKeysRange(ctx context.Context, offset int, limit int) ([]u.Key, error) {
103
93 - ch, err := bs.AllKeysChan(ctx, offset, limit)
104 + ch, err := bs.AllKeysRangeChan(ctx, offset, limit)
105 if err != nil {
106 return nil, err
107 }
@@ -102,12 +113,12 @@ func (bs *blockstore) AllKeys(ctx context.Context, offset int, limit int) ([]u.K
113 return keys, nil
114 }
115
105 -// AllKeys runs a query for keys from the blockstore.
116 +// AllKeysRangeChan runs a query for keys from the blockstore.
117 // this is very simplistic, in the future, take dsq.Query as a param?
118 // if offset and limit are 0, they are ignored.
119 //
109 -// AllKeys respects context
110 -func (bs *blockstore) AllKeysChan(ctx context.Context, offset int, limit int) (<-chan u.Key, error) {
120 +// AllKeysRangeChan respects context
121 +func (bs *blockstore) AllKeysRangeChan(ctx context.Context, offset int, limit int) (<-chan u.Key, error) {
122
123 // KeysOnly, because that would be _a lot_ of data.
124 q := dsq.Query{KeysOnly: true, Offset: offset, Limit: limit}
blocks/blockstore/blockstore_test.go
+3 -3
@@ -67,7 +67,7 @@ func TestAllKeysSimple(t *testing.T) {
67 bs, keys := newBlockStoreWithKeys(t, nil, 100)
68
69 ctx := context.Background()
70 - keys2, err := bs.AllKeys(ctx, 0, 0)
70 + keys2, err := bs.AllKeys(ctx)
71 if err != nil {
72 t.Fatal(err)
73 }
@@ -83,7 +83,7 @@ func TestAllKeysOffsetAndLimit(t *testing.T) {
83 bs, _ := newBlockStoreWithKeys(t, nil, N)
84
85 ctx := context.Background()
86 - keys3, err := bs.AllKeys(ctx, N/3, N/3)
86 + keys3, err := bs.AllKeysRange(ctx, N/3, N/3)
87 if err != nil {
88 t.Fatal(err)
89 }
@@ -107,7 +107,7 @@ func TestAllKeysRespectsContext(t *testing.T) {
107
108 getKeys := func(ctx context.Context) {
109 started <- struct{}{}
110 - _, err := bs.AllKeys(ctx, 0, 0) // once without cancelling
110 + _, err := bs.AllKeys(ctx) // once without cancelling
111 if err != nil {
112 errors <- err
113 }
blocks/blockstore/write_cache.go
+12 -4
@@ -46,10 +46,18 @@ func (w *writecache) Put(b *blocks.Block) error {
46 return w.blockstore.Put(b)
47 }
48
49 -func (w *writecache) AllKeys(ctx context.Context, offset int, limit int) ([]u.Key, error) {
50 - return w.blockstore.AllKeys(ctx, offset, limit)
49 +func (w *writecache) AllKeys(ctx context.Context) ([]u.Key, error) {
50 + return w.blockstore.AllKeysRange(ctx, 0, 0)
51 }
52
53 -func (w *writecache) AllKeysChan(ctx context.Context, offset int, limit int) (<-chan u.Key, error) {
54 - return w.blockstore.AllKeysChan(ctx, offset, limit)
53 +func (w *writecache) AllKeysChan(ctx context.Context) (<-chan u.Key, error) {
54 + return w.blockstore.AllKeysRangeChan(ctx, 0, 0)
55 +}
56 +
57 +func (w *writecache) AllKeysRange(ctx context.Context, offset int, limit int) ([]u.Key, error) {
58 + return w.blockstore.AllKeysRange(ctx, offset, limit)
59 +}
60 +
61 +func (w *writecache) AllKeysRangeChan(ctx context.Context, offset int, limit int) (<-chan u.Key, error) {
62 + return w.blockstore.AllKeysRangeChan(ctx, offset, limit)
63 }
core/commands/refs.go
+1 -1
@@ -137,7 +137,7 @@ Displays the hashes of all local objects.
137 }
138
139 // todo: make async
140 - allKeys, err := n.Blockstore.AllKeysChan(ctx, 0, 0)
140 + allKeys, err := n.Blockstore.AllKeysChan(ctx)
141 if err != nil {
142 res.SetError(err, cmds.ErrNormal)
143 return
core/corerepo/gc.go
+1 -1
@@ -16,7 +16,7 @@ type KeyRemoved struct {
16
17 func GarbageCollectBlockstore(n *core.IpfsNode, ctx context.Context) (<-chan *KeyRemoved, error) {
18
19 - keychan, err := n.Blockstore.AllKeysChan(ctx, 0, 1<<16)
19 + keychan, err := n.Blockstore.AllKeysChan(ctx)
20 if err != nil {
21 return nil, err
22 }
exchange/reprovide/reprovide.go
+1 -1
@@ -49,7 +49,7 @@ func (rp *Reprovider) ProvideEvery(ctx context.Context, tick time.Duration) {
49 }
50
51 func (rp *Reprovider) Reprovide(ctx context.Context) error {
52 - keychan, err := rp.bstore.AllKeysChan(ctx, 0, 1<<16)
52 + keychan, err := rp.bstore.AllKeysChan(ctx)
53 if err != nil {
54 return debugerror.Errorf("Failed to get key chan from blockstore: %s", err)
55 }