-
Notifications
You must be signed in to change notification settings - Fork 919
GODRIVER-2962 Merge insert operation into mongo package. #2264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| // Copyright (C) MongoDB, Inc. 2019-present. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
| // not use this file except in compliance with the License. You may obtain | ||
| // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| package mongo | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "go.mongodb.org/mongo-driver/v2/bson" | ||
| "go.mongodb.org/mongo-driver/v2/event" | ||
| "go.mongodb.org/mongo-driver/v2/internal/driverutil" | ||
| "go.mongodb.org/mongo-driver/v2/internal/logger" | ||
| "go.mongodb.org/mongo-driver/v2/mongo/writeconcern" | ||
| "go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore" | ||
| "go.mongodb.org/mongo-driver/v2/x/mongo/driver" | ||
| "go.mongodb.org/mongo-driver/v2/x/mongo/driver/description" | ||
| "go.mongodb.org/mongo-driver/v2/x/mongo/driver/session" | ||
| ) | ||
|
|
||
| // insert performs an insert operation. | ||
| type insert struct { | ||
| authenticator driver.Authenticator | ||
| bypassDocumentValidation *bool | ||
| comment bsoncore.Value | ||
| documents []bsoncore.Document | ||
| ordered *bool | ||
| session *session.Client | ||
| clock *session.ClusterClock | ||
| collection string | ||
| monitor *event.CommandMonitor | ||
| crypt driver.Crypt | ||
| database string | ||
| deployment driver.Deployment | ||
| selector description.ServerSelector | ||
| writeConcern *writeconcern.WriteConcern | ||
| retry *driver.RetryMode | ||
| result insertResult | ||
| serverAPI *driver.ServerAPIOptions | ||
| timeout *time.Duration | ||
| rawData *bool | ||
| additionalCmd bson.D | ||
| logger *logger.Logger | ||
| } | ||
|
|
||
| // insertResult represents an insert result returned by the server. | ||
| type insertResult struct { | ||
| // Number of documents successfully inserted. | ||
| N int64 | ||
| } | ||
|
|
||
| func buildInsertResult(response bsoncore.Document) (insertResult, error) { | ||
| elements, err := response.Elements() | ||
| if err != nil { | ||
| return insertResult{}, err | ||
| } | ||
| ir := insertResult{} | ||
| for _, element := range elements { | ||
| if element.Key() == "n" { | ||
| var ok bool | ||
| ir.N, ok = element.Value().AsInt64OK() | ||
| if !ok { | ||
| return ir, fmt.Errorf("response field 'n' is type int32 or int64, but received BSON type %s", element.Value().Type) | ||
| } | ||
| } | ||
| } | ||
| return ir, nil | ||
| } | ||
|
|
||
| // Result returns the result of executing this operation. | ||
| func (i *insert) Result() insertResult { return i.result } | ||
|
|
||
| func (i *insert) processResponse(_ context.Context, resp bsoncore.Document, _ driver.ResponseInfo) error { | ||
| ir, err := buildInsertResult(resp) | ||
| i.result.N += ir.N | ||
| return err | ||
| } | ||
|
|
||
| // Execute runs this operations and returns an error if the operation did not execute successfully. | ||
| func (i *insert) Execute(ctx context.Context) error { | ||
| if i.deployment == nil { | ||
| return errors.New("the Insert operation must have a Deployment set before Execute can be called") | ||
| } | ||
| batches := &driver.Batches{ | ||
| Identifier: "documents", | ||
| Documents: i.documents, | ||
| Ordered: i.ordered, | ||
| } | ||
|
|
||
| return driver.Operation{ | ||
| CommandFn: i.command, | ||
| ProcessResponseFn: i.processResponse, | ||
| Batches: batches, | ||
| RetryMode: i.retry, | ||
| Type: driver.Write, | ||
| Client: i.session, | ||
| Clock: i.clock, | ||
| CommandMonitor: i.monitor, | ||
| Crypt: i.crypt, | ||
| Database: i.database, | ||
| Deployment: i.deployment, | ||
| Selector: i.selector, | ||
| WriteConcern: i.writeConcern, | ||
| ServerAPI: i.serverAPI, | ||
| Timeout: i.timeout, | ||
| Logger: i.logger, | ||
| Name: driverutil.InsertOp, | ||
| Authenticator: i.authenticator, | ||
| }.Execute(ctx) | ||
|
|
||
| } | ||
|
|
||
| func (i *insert) command(dst []byte, desc description.SelectedServer) ([]byte, error) { | ||
| dst = bsoncore.AppendStringElement(dst, "insert", i.collection) | ||
| if i.bypassDocumentValidation != nil && (desc.WireVersion != nil && | ||
| driverutil.VersionRangeIncludes(*desc.WireVersion, 4)) { | ||
|
|
||
| dst = bsoncore.AppendBooleanElement(dst, "bypassDocumentValidation", *i.bypassDocumentValidation) | ||
| } | ||
| if i.comment.Type != bsoncore.Type(0) { | ||
| dst = bsoncore.AppendValueElement(dst, "comment", i.comment) | ||
| } | ||
| if i.ordered != nil { | ||
| dst = bsoncore.AppendBooleanElement(dst, "ordered", *i.ordered) | ||
| } | ||
| // Set rawData for 8.2+ servers. | ||
| if i.rawData != nil && desc.WireVersion != nil && driverutil.VersionRangeIncludes(*desc.WireVersion, 27) { | ||
| dst = bsoncore.AppendBooleanElement(dst, "rawData", *i.rawData) | ||
| } | ||
| if len(i.additionalCmd) > 0 { | ||
| doc, err := bson.Marshal(i.additionalCmd) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| dst = append(dst, doc[4:len(doc)-1]...) | ||
| } | ||
| return dst, nil | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -17,6 +17,8 @@ import ( | |||||||||||||||
| "go.mongodb.org/mongo-driver/v2/internal/integtest" | ||||||||||||||||
| "go.mongodb.org/mongo-driver/v2/internal/require" | ||||||||||||||||
| "go.mongodb.org/mongo-driver/v2/internal/serverselector" | ||||||||||||||||
| "go.mongodb.org/mongo-driver/v2/mongo" | ||||||||||||||||
| "go.mongodb.org/mongo-driver/v2/mongo/options" | ||||||||||||||||
| "go.mongodb.org/mongo-driver/v2/mongo/writeconcern" | ||||||||||||||||
| "go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore" | ||||||||||||||||
| "go.mongodb.org/mongo-driver/v2/x/mongo/driver" | ||||||||||||||||
|
|
@@ -145,12 +147,20 @@ func autoInsertDocs(t *testing.T, writeConcern *writeconcern.WriteConcern, docs | |||||||||||||||
|
|
||||||||||||||||
| // insertDocs inserts the docs into the test cluster. | ||||||||||||||||
| func insertDocs(t *testing.T, dbname, colname string, writeConcern *writeconcern.WriteConcern, docs ...bsoncore.Document) { | ||||||||||||||||
| err := operation.NewInsert(docs...). | ||||||||||||||||
| Collection(colname). | ||||||||||||||||
| Database(dbname). | ||||||||||||||||
| Deployment(integtest.Topology(t)). | ||||||||||||||||
| ServerSelector(&serverselector.Write{}). | ||||||||||||||||
| WriteConcern(writeConcern). | ||||||||||||||||
| Execute(context.Background()) | ||||||||||||||||
| t.Helper() | ||||||||||||||||
|
|
||||||||||||||||
| // The initial call to integtest.Topology drops the database used by the | ||||||||||||||||
| // tests, so we have to call it here first to prevent the existing test code | ||||||||||||||||
| // from dropping the database after we've inserted data. | ||||||||||||||||
| integtest.Topology(t) | ||||||||||||||||
|
|
||||||||||||||||
| client, err := mongo.Connect(options.Client().ApplyURI(connectionString.Original).SetWriteConcern(writeConcern)) | ||||||||||||||||
| require.NoError(t, err) | ||||||||||||||||
| defer func() { | ||||||||||||||||
| _ = client.Disconnect(context.Background()) | ||||||||||||||||
| }() | ||||||||||||||||
|
|
||||||||||||||||
| coll := client.Database(dbname).Collection(colname) | ||||||||||||||||
| _, err = coll.InsertMany(context.Background(), docs) | ||||||||||||||||
|
||||||||||||||||
| _, err = coll.InsertMany(context.Background(), docs) | |
| // Convert []bsoncore.Document to []interface{} for InsertMany | |
| docsInterface := make([]interface{}, len(docs)) | |
| for i, doc := range docs { | |
| docsInterface[i] = doc | |
| } | |
| _, err = coll.InsertMany(context.Background(), docsInterface) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The mongo.Connect call does not specify a context parameter. While this may work with the current API, it's better practice to pass context.Background() or another appropriate context to ensure proper cancellation and timeout behavior.