Skip to content

Commit 841d0f7

Browse files
authored
Do not reuse datastore Batch (#23)
Reusing a batch is not guaranteed to be safe for all datastores.
1 parent ca375fb commit 841d0f7

File tree

3 files changed

+7
-23
lines changed

3 files changed

+7
-23
lines changed

dsqueue.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -411,25 +411,18 @@ func (q *DSQueue) clearDatastore(ctx context.Context) (int, error) {
411411
return 0, fmt.Errorf("cannot create datastore batch: %w", err)
412412
}
413413

414-
var rmCount, writeCount int
414+
var rmCount int
415415
for result := range results.Next() {
416416
if ctx.Err() != nil {
417417
return 0, ctx.Err()
418418
}
419-
if writeCount >= DefaultBufferSize {
420-
writeCount = 0
421-
if err = batch.Commit(ctx); err != nil {
422-
return 0, fmt.Errorf("cannot commit datastore updates: %w", err)
423-
}
424-
}
425419
if result.Error != nil {
426420
return 0, fmt.Errorf("cannot read query result from datastore: %w", result.Error)
427421
}
428422
if err = batch.Delete(ctx, datastore.NewKey(result.Key)); err != nil {
429423
return 0, fmt.Errorf("cannot delete key from datastore: %w", err)
430424
}
431425
rmCount++
432-
writeCount++
433426
}
434427

435428
if err = batch.Commit(ctx); err != nil {
@@ -509,7 +502,6 @@ func (q *DSQueue) readDatastore(ctx context.Context, n int, items [][]byte) ([][
509502
if err != nil {
510503
return nil, fmt.Errorf("cannot create datastore batch: %w", err)
511504
}
512-
var delCount int
513505

514506
for result := range results.Next() {
515507
if ctx.Err() != nil {
@@ -522,14 +514,6 @@ func (q *DSQueue) readDatastore(ctx context.Context, n int, items [][]byte) ([][
522514
if err = batch.Delete(ctx, datastore.NewKey(result.Key)); err != nil {
523515
return nil, fmt.Errorf("error deleting queue item: %w", err)
524516
}
525-
delCount++
526-
527-
if delCount >= DefaultBufferSize {
528-
delCount = 0
529-
if err = batch.Commit(ctx); err != nil {
530-
return nil, fmt.Errorf("cannot commit datastore updates: %w", err)
531-
}
532-
}
533517

534518
parts := strings.SplitN(strings.TrimPrefix(result.Key, "/"), "/", 2)
535519
if len(parts) != 2 {

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ require (
66
github.com/gammazero/deque v1.1.0
77
github.com/hashicorp/golang-lru/v2 v2.0.7
88
github.com/ipfs/go-cid v0.5.0
9-
github.com/ipfs/go-datastore v0.8.4
10-
github.com/ipfs/go-log/v2 v2.8.1
9+
github.com/ipfs/go-datastore v0.9.0
10+
github.com/ipfs/go-log/v2 v2.8.2
1111
github.com/ipfs/go-test v0.2.3
1212
)
1313

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ github.com/ipfs/go-block-format v0.2.2 h1:uecCTgRwDIXyZPgYspaLXoMiMmxQpSx2aq34eN
1818
github.com/ipfs/go-block-format v0.2.2/go.mod h1:vmuefuWU6b+9kIU0vZJgpiJt1yicQz9baHXE8qR+KB8=
1919
github.com/ipfs/go-cid v0.5.0 h1:goEKKhaGm0ul11IHA7I6p1GmKz8kEYniqFopaB5Otwg=
2020
github.com/ipfs/go-cid v0.5.0/go.mod h1:0L7vmeNXpQpUS9vt+yEARkJ8rOg43DF3iPgn4GIN0mk=
21-
github.com/ipfs/go-datastore v0.8.4 h1:vXEsd76T3KIOSKXizjhmS3ICGMl+oOSjpLSxE3v8/Wc=
22-
github.com/ipfs/go-datastore v0.8.4/go.mod h1:uT77w/XEGrvJWwHgdrMr8bqCN6ZTW9gzmi+3uK+ouHg=
21+
github.com/ipfs/go-datastore v0.9.0 h1:WocriPOayqalEsueHv6SdD4nPVl4rYMfYGLD4bqCZ+w=
22+
github.com/ipfs/go-datastore v0.9.0/go.mod h1:uT77w/XEGrvJWwHgdrMr8bqCN6ZTW9gzmi+3uK+ouHg=
2323
github.com/ipfs/go-detect-race v0.0.1 h1:qX/xay2W3E4Q1U7d9lNs1sU9nvguX0a7319XbyQ6cOk=
2424
github.com/ipfs/go-detect-race v0.0.1/go.mod h1:8BNT7shDZPo99Q74BpGMK+4D8Mn4j46UU0LZ723meps=
25-
github.com/ipfs/go-log/v2 v2.8.1 h1:Y/X36z7ASoLJaYIJAL4xITXgwf7RVeqb1+/25aq/Xk0=
26-
github.com/ipfs/go-log/v2 v2.8.1/go.mod h1:NyhTBcZmh2Y55eWVjOeKf8M7e4pnJYM3yDZNxQBWEEY=
25+
github.com/ipfs/go-log/v2 v2.8.2 h1:nVG4nNHUwwI/sTs9Bi5iE8sXFQwXs3AjkkuWhg7+Y2I=
26+
github.com/ipfs/go-log/v2 v2.8.2/go.mod h1:UhIYAwMV7Nb4ZmihUxfIRM2Istw/y9cAk3xaK+4Zs2c=
2727
github.com/ipfs/go-test v0.2.3 h1:Z/jXNAReQFtCYyn7bsv/ZqUwS6E7iIcSpJ2CuzCvnrc=
2828
github.com/ipfs/go-test v0.2.3/go.mod h1:QW8vSKkwYvWFwIZQLGQXdkt9Ud76eQXRQ9Ao2H+cA1o=
2929
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=

0 commit comments

Comments
 (0)