From 7b71cc1cae1914a5729a2615c9ef20681cb95f5c Mon Sep 17 00:00:00 2001 From: Qingyang Hu Date: Thu, 26 Sep 2024 18:10:20 -0400 Subject: [PATCH] WIP --- mongo/client_bulk_write.go | 19 +++++++++---------- mongo/client_bulk_write_models.go | 6 ++++++ mongo/errors.go | 2 +- .../unified/client_operation_execution.go | 5 ++++- mongo/results.go | 2 +- x/mongo/driver/operation/command.go | 1 + 6 files changed, 22 insertions(+), 13 deletions(-) diff --git a/mongo/client_bulk_write.go b/mongo/client_bulk_write.go index ba79069b42..f4b9466704 100644 --- a/mongo/client_bulk_write.go +++ b/mongo/client_bulk_write.go @@ -46,15 +46,14 @@ func (bw *clientBulkWrite) execute(ctx context.Context) error { getNsIndex := func(namespace string) int { if v, ok := nsMap[namespace]; ok { return v - } else { - nsIdx := len(nsList) - nsMap[namespace] = nsIdx - nsList = append(nsList, namespace) - return nsIdx } + nsIdx := len(nsList) + nsMap[namespace] = nsIdx + nsList = append(nsList, namespace) + return nsIdx } resMap := make([]interface{}, len(bw.models)) - insIdMap := make(map[int]interface{}) + insIDMap := make(map[int]interface{}) for i, v := range bw.models { var doc bsoncore.Document var err error @@ -71,7 +70,7 @@ func (bw *clientBulkWrite) execute(ctx context.Context) error { if err != nil { break } - insIdMap[i] = id + insIDMap[i] = id case *ClientUpdateOneModel: nsIdx = getNsIndex(model.Namespace) if bw.result.UpdateResults == nil { @@ -203,7 +202,7 @@ func (bw *clientBulkWrite) execute(ctx context.Context) error { return err } case map[int64]ClientInsertResult: - if err = appendInsertResult(cur, res, insIdMap); err != nil { + if err = appendInsertResult(cur, res, insIDMap); err != nil { return err } case map[int64]ClientUpdateResult: @@ -413,7 +412,7 @@ func appendUpdateResult(cur bson.Raw, m map[int64]ClientUpdateResult) error { N int32 NModified int32 Upserted struct { - Id interface{} `bson:"_id"` + ID interface{} `bson:"_id"` } } if err := bson.Unmarshal(cur, &res); err != nil { @@ -422,7 +421,7 @@ func appendUpdateResult(cur bson.Raw, m map[int64]ClientUpdateResult) error { m[int64(res.Idx)] = ClientUpdateResult{ MatchedCount: int64(res.N), ModifiedCount: int64(res.NModified), - UpsertedID: res.Upserted.Id, + UpsertedID: res.Upserted.ID, } return nil } diff --git a/mongo/client_bulk_write_models.go b/mongo/client_bulk_write_models.go index c14e37451f..e942a7af4c 100644 --- a/mongo/client_bulk_write_models.go +++ b/mongo/client_bulk_write_models.go @@ -15,6 +15,7 @@ type ClientWriteModels struct { models []interface{} } +// AppendInsertOne appends ClientInsertOneModels. func (m *ClientWriteModels) AppendInsertOne(models ...*ClientInsertOneModel) *ClientWriteModels { if m == nil { m = &ClientWriteModels{} @@ -25,6 +26,7 @@ func (m *ClientWriteModels) AppendInsertOne(models ...*ClientInsertOneModel) *Cl return m } +// AppendUpdateOne appends ClientUpdateOneModels. func (m *ClientWriteModels) AppendUpdateOne(models ...*ClientUpdateOneModel) *ClientWriteModels { if m == nil { m = &ClientWriteModels{} @@ -35,6 +37,7 @@ func (m *ClientWriteModels) AppendUpdateOne(models ...*ClientUpdateOneModel) *Cl return m } +// AppendUpdateMany appends ClientUpdateManyModels. func (m *ClientWriteModels) AppendUpdateMany(models ...*ClientUpdateManyModel) *ClientWriteModels { if m == nil { m = &ClientWriteModels{} @@ -45,6 +48,7 @@ func (m *ClientWriteModels) AppendUpdateMany(models ...*ClientUpdateManyModel) * return m } +// AppendReplaceOne appends ClientReplaceOneModels. func (m *ClientWriteModels) AppendReplaceOne(models ...*ClientReplaceOneModel) *ClientWriteModels { if m == nil { m = &ClientWriteModels{} @@ -55,6 +59,7 @@ func (m *ClientWriteModels) AppendReplaceOne(models ...*ClientReplaceOneModel) * return m } +// AppendDeleteOne appends ClientDeleteOneModels. func (m *ClientWriteModels) AppendDeleteOne(models ...*ClientDeleteOneModel) *ClientWriteModels { if m == nil { m = &ClientWriteModels{} @@ -65,6 +70,7 @@ func (m *ClientWriteModels) AppendDeleteOne(models ...*ClientDeleteOneModel) *Cl return m } +// AppendDeleteMany appends ClientDeleteManyModels. func (m *ClientWriteModels) AppendDeleteMany(models ...*ClientDeleteManyModel) *ClientWriteModels { if m == nil { m = &ClientWriteModels{} diff --git a/mongo/errors.go b/mongo/errors.go index e20af03e95..b4de6224b3 100644 --- a/mongo/errors.go +++ b/mongo/errors.go @@ -638,7 +638,7 @@ func (bwe ClientBulkWriteException) Error() string { if len(bwe.WriteErrors) > 0 { errs := make([]error, 0, len(bwe.WriteErrors)) for _, v := range bwe.WriteErrors { - errs = append(errs, &v) + errs = append(errs, v) } causes = append(causes, "write errors: "+joinBatchErrors(errs)) } diff --git a/mongo/integration/unified/client_operation_execution.go b/mongo/integration/unified/client_operation_execution.go index 0a4c381638..80fc1374b4 100644 --- a/mongo/integration/unified/client_operation_execution.go +++ b/mongo/integration/unified/client_operation_execution.go @@ -212,7 +212,10 @@ func executeClientBulkWrite(ctx context.Context, operation *operation) (*operati opts.SetVerboseResults(val.Boolean()) case "writeConcern": var wc writeConcern - bson.Unmarshal(val.Value, &wc) + err := bson.Unmarshal(val.Value, &wc) + if err != nil { + return nil, err + } c, err := wc.toWriteConcernOption() if err != nil { return nil, err diff --git a/mongo/results.go b/mongo/results.go index 0088e734fc..58da18e7fb 100644 --- a/mongo/results.go +++ b/mongo/results.go @@ -14,7 +14,7 @@ import ( "go.mongodb.org/mongo-driver/x/mongo/driver/operation" ) -// BulkWriteResult is the result type returned by a client-level BulkWrite operation. +// ClientBulkWriteResult is the result type returned by a client-level BulkWrite operation. type ClientBulkWriteResult struct { // The number of documents inserted. InsertedCount int64 diff --git a/x/mongo/driver/operation/command.go b/x/mongo/driver/operation/command.go index 6936d91719..443120d130 100644 --- a/x/mongo/driver/operation/command.go +++ b/x/mongo/driver/operation/command.go @@ -54,6 +54,7 @@ func NewCommand(command bsoncore.Document) *Command { } } +// NewCommandFn constructs and returns a new Command. func NewCommandFn(commandFn func([]byte, description.SelectedServer) ([]byte, error)) *Command { return &Command{ commandFn: commandFn,