Skip to content

Commit 48838bf

Browse files
committed
cleanup
1 parent ac20fb7 commit 48838bf

File tree

7 files changed

+312
-227
lines changed

7 files changed

+312
-227
lines changed

mongo/client.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,8 @@ func (c *Client) BulkWrite(ctx context.Context, models *ClientWriteModels,
886886
}
887887
wc = bwo.WriteConcern
888888
}
889-
if !writeconcern.AckWrite(wc) {
889+
acknowledged := writeconcern.AckWrite(wc)
890+
if !acknowledged {
890891
if bwo.Ordered == nil || *bwo.Ordered {
891892
return nil, errors.New("cannot request unacknowledged write concern and ordered writes")
892893
}
@@ -912,13 +913,17 @@ func (c *Client) BulkWrite(ctx context.Context, models *ClientWriteModels,
912913
}
913914
if bwo.VerboseResults == nil || !(*bwo.VerboseResults) {
914915
op.errorsOnly = true
915-
} else if !writeconcern.AckWrite(wc) {
916+
} else if !acknowledged {
916917
return nil, errors.New("cannot request unacknowledged write concern and verbose results")
917918
}
918919
if err = op.execute(ctx); err != nil {
919920
return nil, replaceErrors(err)
920921
}
921-
return &op.result, nil
922+
var results *ClientBulkWriteResult
923+
if acknowledged {
924+
results = &op.result
925+
}
926+
return results, nil
922927
}
923928

924929
// newLogger will use the LoggerOptions to create an internal logger and publish

mongo/client_bulk_write.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const (
2929
database = "admin"
3030
)
3131

32-
// bulkWrite performs a bulkwrite operation
3332
type clientBulkWrite struct {
3433
models []clientWriteModel
3534
errorsOnly bool

mongo/client_bulk_write_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright (C) MongoDB, Inc. 2024-present.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
// not use this file except in compliance with the License. You may obtain
5+
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
7+
package mongo
8+
9+
import (
10+
"testing"
11+
12+
"go.mongodb.org/mongo-driver/bson"
13+
"go.mongodb.org/mongo-driver/internal/assert"
14+
"go.mongodb.org/mongo-driver/internal/require"
15+
)
16+
17+
func TestBatches(t *testing.T) {
18+
t.Run("test Addvancing", func(t *testing.T) {
19+
batches := &modelBatches{
20+
models: make([]clientWriteModel, 2),
21+
}
22+
batches.AdvanceBatches(3)
23+
size := batches.Size()
24+
assert.Equal(t, 0, size, "expected: %d, got: %d", 1, size)
25+
})
26+
t.Run("test appendBatches", func(t *testing.T) {
27+
client, err := NewClient()
28+
require.NoError(t, err, "NewClient error: %v", err)
29+
batches := &modelBatches{
30+
client: client,
31+
models: []clientWriteModel{
32+
{"ns0", nil},
33+
{"ns1", &ClientInsertOneModel{
34+
Document: bson.D{{"foo", 42}},
35+
}},
36+
{"ns2", &ClientReplaceOneModel{
37+
Filter: bson.D{{"foo", "bar"}},
38+
Replacement: bson.D{{"foo", "baz"}},
39+
}},
40+
{"ns1", &ClientDeleteOneModel{
41+
Filter: bson.D{{"qux", "quux"}},
42+
}},
43+
},
44+
offset: 1,
45+
result: &ClientBulkWriteResult{},
46+
}
47+
var n int
48+
n, _, err = batches.AppendBatchSequence(nil, 4, 16_000, 16_000)
49+
require.NoError(t, err, "AppendBatchSequence error: %v", err)
50+
assert.Equal(t, 3, n, "expected %d appendings, got: %d", 3, n)
51+
52+
_ = batches.cursorHandlers[0](&cursorInfo{Ok: true, Idx: 0}, nil)
53+
_ = batches.cursorHandlers[1](&cursorInfo{Ok: true, Idx: 1}, nil)
54+
_ = batches.cursorHandlers[2](&cursorInfo{Ok: true, Idx: 2}, nil)
55+
56+
ins, ok := batches.result.InsertResults[1]
57+
assert.True(t, ok, "expected an insert results")
58+
assert.NotNil(t, ins.InsertedID, "expected an ID")
59+
60+
_, ok = batches.result.UpdateResults[2]
61+
assert.True(t, ok, "expected an insert results")
62+
63+
_, ok = batches.result.DeleteResults[3]
64+
assert.True(t, ok, "expected an insert results")
65+
})
66+
}

0 commit comments

Comments
 (0)