Skip to content

Commit

Permalink
refactor addConsumedCapacity, add a lil test
Browse files Browse the repository at this point in the history
  • Loading branch information
guregu committed Jul 16, 2024
1 parent 58c2308 commit 578fe40
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 37 deletions.
4 changes: 2 additions & 2 deletions batchget.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,8 @@ redo:
return false
}
if itr.bg.cc != nil {
for _, cc := range itr.output.ConsumedCapacity {
addConsumedCapacity(itr.bg.cc, &cc)
for i := range itr.output.ConsumedCapacity {
itr.bg.cc.add(&itr.output.ConsumedCapacity[i])
}
}

Expand Down
4 changes: 2 additions & 2 deletions batchwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ func (bw *BatchWrite) Run(ctx context.Context) (wrote int, err error) {
return wrote, err
}
if bw.cc != nil {
for _, cc := range res.ConsumedCapacity {
addConsumedCapacity(bw.cc, &cc)
for i := range res.ConsumedCapacity {
bw.cc.add(&res.ConsumedCapacity[i])
}
}

Expand Down
4 changes: 2 additions & 2 deletions delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ func (d *Delete) run(ctx context.Context) (*dynamodb.DeleteItemOutput, error) {
d.cc.incRequests()
return err
})
if d.cc != nil && output != nil {
addConsumedCapacity(d.cc, output.ConsumedCapacity)
if output != nil {
d.cc.add(output.ConsumedCapacity)
}
return output, err
}
Expand Down
4 changes: 2 additions & 2 deletions put.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func (p *Put) run(ctx context.Context) (output *dynamodb.PutItemOutput, err erro
p.cc.incRequests()
return err
})
if p.cc != nil && output != nil {
addConsumedCapacity(p.cc, output.ConsumedCapacity)
if output != nil {
p.cc.add(output.ConsumedCapacity)
}
return
}
Expand Down
16 changes: 4 additions & 12 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,7 @@ func (q *Query) One(ctx context.Context, out interface{}) error {
if err != nil {
return err
}
if q.cc != nil {
addConsumedCapacity(q.cc, res.ConsumedCapacity)
}
q.cc.add(res.ConsumedCapacity)

return unmarshalItem(res.Item, out)
}
Expand Down Expand Up @@ -266,9 +264,7 @@ func (q *Query) One(ctx context.Context, out interface{}) error {
if err != nil {
return err
}
if q.cc != nil {
addConsumedCapacity(q.cc, res.ConsumedCapacity)
}
q.cc.add(res.ConsumedCapacity)

return unmarshalItem(res.Items[0], out)
}
Expand Down Expand Up @@ -304,9 +300,7 @@ func (q *Query) Count(ctx context.Context) (int, error) {
if err != nil {
return 0, err
}
if q.cc != nil {
addConsumedCapacity(q.cc, res.ConsumedCapacity)
}
q.cc.add(res.ConsumedCapacity)

q.startKey = res.LastEvaluatedKey
if res.LastEvaluatedKey == nil ||
Expand Down Expand Up @@ -402,9 +396,7 @@ func (itr *queryIter) Next(ctx context.Context, out interface{}) bool {
if itr.err != nil {
return false
}
if itr.query.cc != nil {
addConsumedCapacity(itr.query.cc, itr.output.ConsumedCapacity)
}
itr.query.cc.add(itr.output.ConsumedCapacity)
if len(itr.output.LastEvaluatedKey) > len(itr.exLEK) {
itr.exLEK = itr.output.LastEvaluatedKey
}
Expand Down
9 changes: 2 additions & 7 deletions scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,7 @@ func (s *Scan) Count(ctx context.Context) (int, error) {

count += int(out.Count)
scanned += out.ScannedCount

if s.cc != nil {
addConsumedCapacity(s.cc, out.ConsumedCapacity)
}
s.cc.add(out.ConsumedCapacity)

if out.LastEvaluatedKey == nil ||
(s.limit > 0 && count >= s.limit) ||
Expand Down Expand Up @@ -407,9 +404,7 @@ redo:
if itr.err != nil {
return false
}
if itr.scan.cc != nil {
addConsumedCapacity(itr.scan.cc, itr.output.ConsumedCapacity)
}
itr.scan.cc.add(itr.output.ConsumedCapacity)
if len(itr.output.LastEvaluatedKey) > len(itr.exLEK) {
itr.exLEK = itr.output.LastEvaluatedKey
}
Expand Down
2 changes: 1 addition & 1 deletion table.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ type ConsumedCapacity struct {
Requests int
}

func addConsumedCapacity(cc *ConsumedCapacity, raw *types.ConsumedCapacity) {
func (cc *ConsumedCapacity) add(raw *types.ConsumedCapacity) {
if cc == nil || raw == nil {
return
}
Expand Down
9 changes: 9 additions & 0 deletions table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ func TestAddConsumedCapacity(t *testing.T) {
if !reflect.DeepEqual(cc, expected) {
t.Error("bad ConsumedCapacity:", cc, "≠", expected)
}

t.Run("request count", func(t *testing.T) {
const expectedReqs = 2
cc.incRequests()
cc.incRequests()
if cc.Requests != expectedReqs {
t.Error("bad Requests count:", cc.Requests, "≠", expectedReqs)
}
})
}

func normalizeDesc(desc *Description) {
Expand Down
14 changes: 7 additions & 7 deletions tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func (tx *GetTx) Run(ctx context.Context) error {
resp, err = tx.db.client.TransactGetItems(ctx, input)
tx.cc.incRequests()
if tx.cc != nil && resp != nil {
for _, cc := range resp.ConsumedCapacity {
addConsumedCapacity(tx.cc, &cc)
for i := range resp.ConsumedCapacity {
tx.cc.add(&resp.ConsumedCapacity[i])
}
}
return err
Expand Down Expand Up @@ -113,8 +113,8 @@ func (tx *GetTx) All(ctx context.Context, out interface{}) error {
resp, err = tx.db.client.TransactGetItems(ctx, input)
tx.cc.incRequests()
if tx.cc != nil && resp != nil {
for _, cc := range resp.ConsumedCapacity {
addConsumedCapacity(tx.cc, &cc)
for i := range resp.ConsumedCapacity {
tx.cc.add(&resp.ConsumedCapacity[i])
}
}
return err
Expand Down Expand Up @@ -259,9 +259,9 @@ func (tx *WriteTx) Run(ctx context.Context) error {
err = tx.db.retry(ctx, func() error {
out, err := tx.db.client.TransactWriteItems(ctx, input)
tx.cc.incRequests()
if tx.cc != nil && out != nil {
for _, cc := range out.ConsumedCapacity {
addConsumedCapacity(tx.cc, &cc)
if out != nil {
for i := range out.ConsumedCapacity {
tx.cc.add(&out.ConsumedCapacity[i])
}
}
return err
Expand Down
4 changes: 2 additions & 2 deletions update.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,8 @@ func (u *Update) run(ctx context.Context) (*dynamodb.UpdateItemOutput, error) {
u.cc.incRequests()
return err
})
if u.cc != nil && output != nil {
addConsumedCapacity(u.cc, output.ConsumedCapacity)
if output != nil {
u.cc.add(output.ConsumedCapacity)
}
return output, err
}
Expand Down

0 comments on commit 578fe40

Please sign in to comment.