Skip to content

Commit

Permalink
make batch Merge variadic
Browse files Browse the repository at this point in the history
  • Loading branch information
guregu committed Feb 12, 2024
1 parent 137ce45 commit c2e6f02
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,10 @@ docker compose -f '.github/docker-compose.yml' up -d

# Run the tests with a fresh table
# The tables will be created automatically
# The '%' in the table name will be replaced the current timestamp
DYNAMO_TEST_ENDPOINT='http://localhost:8880' \
DYNAMO_TEST_REGION='local' \
DYNAMO_TEST_TABLE='TestDB-%' \ # the % will be replaced the current timestamp
DYNAMO_TEST_TABLE='TestDB-%' \
AWS_ACCESS_KEY_ID='dummy' \
AWS_SECRET_ACCESS_KEY='dummy' \
AWS_REGION='local' \
Expand Down
8 changes: 2 additions & 6 deletions batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ func TestBatchGetWrite(t *testing.T) {
batches = append(batches, b)
}
batch1 := batches[0]
for _, b := range batches[1:] {
batch1.Merge(b)
}
batch1.Merge(batches[1:]...)
var wcc ConsumedCapacity
wrote, err := batch1.ConsumedCapacity(&wcc).Run()
if wrote != totalBatchSize {
Expand All @@ -64,9 +62,7 @@ func TestBatchGetWrite(t *testing.T) {

var cc ConsumedCapacity
get1 := gets[0].ConsumedCapacity(&cc)
for _, b := range gets[1:] {
get1.Merge(b)
}
get1.Merge(gets[1:]...)

var results []widget
err = get1.All(&results)
Expand Down
26 changes: 14 additions & 12 deletions batchget.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,21 @@ func (bg *BatchGet) projectionFor(table string) []string {
}

// Merge copies operations and settings from src to this batch get.
func (bg *BatchGet) Merge(src *BatchGet) *BatchGet {
bg.reqs = append(bg.reqs, src.reqs...)
bg.consistent = bg.consistent || src.consistent
this := bg.batch.table.Name()
for table, proj := range src.projections {
if this == table {
continue
func (bg *BatchGet) Merge(srcs ...*BatchGet) *BatchGet {
for _, src := range srcs {
bg.reqs = append(bg.reqs, src.reqs...)
bg.consistent = bg.consistent || src.consistent
this := bg.batch.table.Name()
for table, proj := range src.projections {
if this == table {
continue
}
bg.mergeProjection(table, proj)
}
bg.mergeProjection(table, proj)
}
if len(src.projection) > 0 {
if that := src.batch.table.Name(); that != this {
bg.mergeProjection(that, src.projection)
if len(src.projection) > 0 {
if that := src.batch.table.Name(); that != this {
bg.mergeProjection(that, src.projection)
}
}
}
return bg
Expand Down
6 changes: 4 additions & 2 deletions batchwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ func (bw *BatchWrite) deleteIn(table Table, hashKey, rangeKey string, keys ...Ke
}

// Merge copies operations from src to this batch.
func (bw *BatchWrite) Merge(src *BatchWrite) *BatchWrite {
bw.ops = append(bw.ops, src.ops...)
func (bw *BatchWrite) Merge(srcs ...*BatchWrite) *BatchWrite {
for _, src := range srcs {
bw.ops = append(bw.ops, src.ops...)
}
return bw
}

Expand Down

0 comments on commit c2e6f02

Please sign in to comment.