Skip to content

Commit

Permalink
GODRIVER-2348 Resolve merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvasquez committed May 9, 2024
2 parents 3b40fa4 + 0c2ae5d commit 1620f92
Show file tree
Hide file tree
Showing 37 changed files with 404 additions and 199 deletions.
18 changes: 11 additions & 7 deletions .evergreen/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1958,6 +1958,10 @@ axes:
- id: version
display_name: MongoDB Version
values:
- id: "8.0"
display_name: "8.0"
variables:
VERSION: "8.0"
- id: "7.0"
display_name: "7.0"
variables:
Expand Down Expand Up @@ -2394,7 +2398,7 @@ buildvariants:

- matrix_name: "tests-42-plus-zlib-zstd-support"
tags: ["pullrequest"]
matrix_spec: { version: ["4.2", "4.4", "5.0", "6.0", "7.0"], os-ssl-40: ["windows-64-go-1-20", "rhel87-64-go-1-20"] }
matrix_spec: { version: ["4.2", "4.4", "5.0", "6.0", "7.0", "8.0"], os-ssl-40: ["windows-64-go-1-20", "rhel87-64-go-1-20"] }
display_name: "${version} ${os-ssl-40}"
tasks:
- name: ".test !.enterprise-auth !.snappy"
Expand All @@ -2412,28 +2416,28 @@ buildvariants:
- name: ".test .enterprise-auth"

- matrix_name: "aws-auth-test"
matrix_spec: { version: ["4.4", "5.0", "6.0", "7.0", "latest"], os-aws-auth: "*" }
matrix_spec: { version: ["4.4", "5.0", "6.0", "7.0", "8.0", "latest"], os-aws-auth: "*" }
display_name: "MONGODB-AWS Auth ${version} ${os-aws-auth}"
tasks:
- name: "aws-auth-test"

- matrix_name: "ocsp-test"
matrix_spec: { version: ["4.4", "5.0", "6.0", "7.0", "latest"], ocsp-rhel-87: ["rhel87-go-1-20"] }
matrix_spec: { version: ["4.4", "5.0", "6.0", "7.0", "8.0", "latest"], ocsp-rhel-87: ["rhel87-go-1-20"] }
display_name: "OCSP ${version} ${ocsp-rhel-87}"
batchtime: 20160 # Use a batchtime of 14 days as suggested by the OCSP test README
tasks:
- name: ".ocsp"

- matrix_name: "ocsp-test-windows"
matrix_spec: { version: ["4.4", "5.0", "6.0", "7.0", "latest"], os-ssl-40: ["windows-64-go-1-20"] }
matrix_spec: { version: ["4.4", "5.0", "6.0", "7.0", "8.0", "latest"], os-ssl-40: ["windows-64-go-1-20"] }
display_name: "OCSP ${version} ${os-ssl-40}"
batchtime: 20160 # Use a batchtime of 14 days as suggested by the OCSP test README
tasks:
# Windows MongoDB servers do not staple OCSP responses and only support RSA.
- name: ".ocsp-rsa !.ocsp-staple"

- matrix_name: "ocsp-test-macos"
matrix_spec: { version: ["4.4", "5.0", "6.0", "7.0", "latest"], os-ssl-40: ["macos11-go-1-20"] }
matrix_spec: { version: ["4.4", "5.0", "6.0", "7.0", "8.0", "latest"], os-ssl-40: ["macos11-go-1-20"] }
display_name: "OCSP ${version} ${os-ssl-40}"
batchtime: 20160 # Use a batchtime of 14 days as suggested by the OCSP test README
tasks:
Expand All @@ -2449,7 +2453,7 @@ buildvariants:

- matrix_name: "versioned-api-test"
tags: ["pullrequest"]
matrix_spec: { version: ["5.0", "6.0", "7.0"], os-ssl-40: ["windows-64-go-1-20", "rhel87-64-go-1-20"] }
matrix_spec: { version: ["5.0", "6.0", "7.0", "8.0"], os-ssl-40: ["windows-64-go-1-20", "rhel87-64-go-1-20"] }
display_name: "API Version ${version} ${os-ssl-40}"
tasks:
- name: ".versioned-api"
Expand All @@ -2468,7 +2472,7 @@ buildvariants:

- matrix_name: "load-balancer-test"
tags: ["pullrequest"]
matrix_spec: { version: ["5.0", "6.0", "7.0"], os-ssl-40: ["rhel87-64-go-1-20"] }
matrix_spec: { version: ["5.0", "6.0", "7.0", "8.0"], os-ssl-40: ["rhel87-64-go-1-20"] }
display_name: "Load Balancer Support ${version} ${os-ssl-40}"
tasks:
- name: ".load-balancer"
Expand Down
2 changes: 2 additions & 0 deletions internal/integration/change_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func TestChangeStream_ReplicaSet(t *testing.T) {
// cause an event to occur so the resume token is updated
generateEvents(mt, 1)
assert.True(mt, cs.Next(context.Background()), "expected next to return true, got false")
assert.Equal(mt, 0, cs.RemainingBatchLength())
firstToken := cs.ResumeToken()

// cause an event on a different collection than the one being watched so the server's PBRT is updated
Expand Down Expand Up @@ -373,6 +374,7 @@ func TestChangeStream_ReplicaSet(t *testing.T) {

// Iterate over one event to get resume token
assert.True(mt, cs.Next(context.Background()), "expected Next to return true, got false")
assert.Equal(mt, numEvents-1, cs.RemainingBatchLength())
token := cs.ResumeToken()
closeStream(cs)

Expand Down
25 changes: 15 additions & 10 deletions internal/integration/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -872,24 +872,29 @@ func TestCollection(t *testing.T) {
}
})
mt.RunOpts("distinct", noClientOpts, func(mt *mtest.T) {
all := []interface{}{int32(1), int32(2), int32(3), int32(4), int32(5)}
last3 := []interface{}{int32(3), int32(4), int32(5)}
all := []int32{1, 2, 3, 4, 5}

testCases := []struct {
name string
filter bson.D
opts *options.DistinctOptions
expected []interface{}
name string
filter bson.D
opts *options.DistinctOptions
want []int32
}{
{"no options", bson.D{}, nil, all},
{"filter", bson.D{{"x", bson.D{{"$gt", 2}}}}, nil, last3},
{"filter", bson.D{{"x", bson.D{{"$gt", 2}}}}, nil, all[2:]},
{"options", bson.D{}, options.Distinct().SetComment("1"), all},
}
for _, tc := range testCases {
mt.Run(tc.name, func(mt *mtest.T) {
initCollection(mt, mt.Coll)
res, err := mt.Coll.Distinct(context.Background(), "x", tc.filter, tc.opts)
assert.Nil(mt, err, "Distinct error: %v", err)
assert.Equal(mt, tc.expected, res, "expected result %v, got %v", tc.expected, res)
res := mt.Coll.Distinct(context.Background(), "x", tc.filter, tc.opts)
assert.Nil(mt, res.Err(), "Distinct error: %v", res.Err())

var got []int32
err := res.Decode(&got)
assert.NoError(t, err)

assert.EqualValues(mt, tc.want, got, "expected result %v, got %v", tc.want, got)
})
}
})
Expand Down
59 changes: 39 additions & 20 deletions internal/integration/crud_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ func executeListIndexes(mt *mtest.T, sess *mongo.Session, args bson.Raw) (*mongo
return mt.Coll.Indexes().List(context.Background())
}

func executeDistinct(mt *mtest.T, sess *mongo.Session, args bson.Raw) ([]interface{}, error) {
func executeDistinct(mt *mtest.T, sess *mongo.Session, args bson.Raw) (bson.RawArray, error) {
mt.Helper()

var fieldName string
Expand All @@ -623,16 +623,22 @@ func executeDistinct(mt *mtest.T, sess *mongo.Session, args bson.Raw) ([]interfa
}
}

var res *mongo.DistinctResult
if sess != nil {
var res []interface{}
err := mongo.WithSession(context.Background(), sess, func(sc context.Context) error {
var derr error
res, derr = mt.Coll.Distinct(sc, fieldName, filter, opts)
return derr
err := mongo.WithSession(context.Background(), sess, func(ctx context.Context) error {
res = mt.Coll.Distinct(ctx, fieldName, filter, opts)

return res.Err()
})
return res, err

if err != nil {
return nil, err
}
} else {
res = mt.Coll.Distinct(context.Background(), fieldName, filter, opts)
}
return mt.Coll.Distinct(context.Background(), fieldName, filter, opts)

return res.Raw()
}

func executeFindOneAndDelete(mt *mtest.T, sess *mongo.Session, args bson.Raw) *mongo.SingleResult {
Expand Down Expand Up @@ -1525,25 +1531,34 @@ func verifyDeleteResult(mt *mtest.T, res *mongo.DeleteResult, result interface{}
"deleted count mismatch; expected %v, got %v", expected.DeletedCount, res.DeletedCount)
}

func verifyDistinctResult(mt *mtest.T, actualResult []interface{}, expectedResult interface{}) {
func verifyDistinctResult(
mt *mtest.T,
got bson.RawArray,
want interface{},
) {
mt.Helper()

if expectedResult == nil {
if got == nil {
return
}

for i, expected := range expectedResult.(bson.A) {
actual := actualResult[i]
iExpected := getIntFromInterface(expected)
iActual := getIntFromInterface(actual)
assert.NotNil(mt, want, "expected want to be non-nil")

if iExpected != nil {
assert.NotNil(mt, iActual, "expected nil but got %v", iActual)
assert.Equal(mt, *iExpected, *iActual, "expected value %v but got %v", *iExpected, *iActual)
continue
arr, ok := want.(bson.A)
assert.True(mt, ok, "expected want to be a BSON array")

for i, iwant := range arr {
gotRawValue := got.Index(uint(i))

iwantType, iwantBytes, err := bson.MarshalValue(iwant)
assert.NoError(mt, err)

wantRawValue := bson.RawValue{
Type: iwantType,
Value: iwantBytes,
}

assert.Equal(mt, expected, actual, "expected value %v but got %v", expected, actual)
assert.EqualValues(mt, wantRawValue, gotRawValue, "expected value %v but got %v", wantRawValue, gotRawValue)
}
}

Expand Down Expand Up @@ -1632,7 +1647,11 @@ func verifyCursorResult(mt *mtest.T, cur *mongo.Cursor, result interface{}) {
}
}

func verifySingleResult(mt *mtest.T, actualResult *mongo.SingleResult, expectedResult interface{}) {
func verifySingleResult(
mt *mtest.T,
actualResult *mongo.SingleResult,
expectedResult interface{},
) {
mt.Helper()

if expectedResult == nil {
Expand Down
2 changes: 2 additions & 0 deletions internal/integration/sessions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,8 @@ func extractReturnError(returnValues []reflect.Value) error {
return converted
case *mongo.SingleResult:
return converted.Err()
case *mongo.DistinctResult:
return converted.Err()
default:
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/integration/unified/admin_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func performDistinctWorkaround(ctx context.Context) error {
commandFn := func(ctx context.Context, client *mongo.Client) error {
for _, coll := range entities(ctx).collections() {
newColl := client.Database(coll.Database().Name()).Collection(coll.Name())
_, err := newColl.Distinct(ctx, "x", bson.D{})
err := newColl.Distinct(ctx, "x", bson.D{}).Err()
if err != nil {
ns := fmt.Sprintf("%s.%s", coll.Database().Name(), coll.Name())
return fmt.Errorf("error running distinct for collection %q: %w", ns, err)
Expand Down
12 changes: 7 additions & 5 deletions internal/integration/unified/collection_operation_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,15 +539,17 @@ func executeDistinct(ctx context.Context, operation *operation) (*operationResul
return nil, newMissingArgumentError("filter")
}

res, err := coll.Distinct(ctx, fieldName, filter, opts)
if err != nil {
res := coll.Distinct(ctx, fieldName, filter, opts)
if err := res.Err(); err != nil {
return newErrorResult(err), nil
}
_, rawRes, err := bson.MarshalValue(res)

arr, err := res.Raw()
if err != nil {
return nil, fmt.Errorf("error converting Distinct result to raw BSON: %w", err)
return newErrorResult(err), nil
}
return newValueResult(bson.TypeArray, rawRes, nil), nil

return newValueResult(bson.TypeArray, arr, nil), nil
}

func executeDropIndex(ctx context.Context, operation *operation) (*operationResult, error) {
Expand Down
4 changes: 2 additions & 2 deletions internal/integration/unified_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ func runSpecTestCase(mt *mtest.T, test *testCase, testFile testFile) {
if mtest.ClusterTopologyKind() == mtest.Sharded && test.Description == "distinct" {
err := runCommandOnAllServers(func(mongosClient *mongo.Client) error {
coll := mongosClient.Database(mt.DB.Name()).Collection(mt.Coll.Name())
_, err := coll.Distinct(context.Background(), "x", bson.D{})
return err

return coll.Distinct(context.Background(), "x", bson.D{}).Err()
})
assert.Nil(mt, err, "error running distinct against all mongoses: %v", err)
}
Expand Down
6 changes: 6 additions & 0 deletions mongo/change_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,12 @@ func (cs *ChangeStream) ID() int64 {
return cs.cursor.ID()
}

// RemainingBatchLength returns the number of documents left in the current batch. If this returns zero, the subsequent
// call to Next or TryNext will do a network request to fetch the next batch.
func (cs *ChangeStream) RemainingBatchLength() int {
return len(cs.batch)
}

// SetBatchSize sets the number of documents to fetch from the database with
// each iteration of the ChangeStream's "Next" or "TryNext" method. This setting
// only affects subsequent document batches fetched from the database.
Expand Down
2 changes: 1 addition & 1 deletion mongo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func newClient(opts ...*options.ClientOptions) (*Client, error) {
}
// Timeout
if to := clientOpt.Timeout; to != nil && *to < 0 {
return nil, fmt.Errorf(`invalid value %q for "Timeout": value must be positive"`, *to)
return nil, fmt.Errorf(`invalid value %q for "Timeout": value must be positive`, *to)
}
client.timeout = clientOpt.Timeout
client.httpClient = clientOpt.HTTPClient
Expand Down
2 changes: 1 addition & 1 deletion mongo/client_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func ExampleConnect_stableAPI() {
coll := serverAPIStrictClient.Database("db").Collection("coll")
// Fails with error: (APIStrictError) Provided apiStrict:true, but the
// command distinct is not in API Version 1
_, err = coll.Distinct(context.TODO(), "distinct", bson.D{})
err = coll.Distinct(context.TODO(), "distinct", bson.D{}).Err()
log.Println(err)

// ServerAPIOptions can be declared with a DeprecationErrors option.
Expand Down
2 changes: 1 addition & 1 deletion mongo/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ func TestClient(t *testing.T) {
copts := options.Client().SetTimeout(-1 * time.Second)
_, err := Connect(copts)

errmsg := `invalid value for "timeout": "-1s"`
errmsg := `invalid value "-1s" for "Timeout": value must be positive`
assert.Equal(t, errmsg, err.Error(), "expected error %v, got %v", errmsg, err.Error())
})
}
36 changes: 15 additions & 21 deletions mongo/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -1288,16 +1288,20 @@ func (coll *Collection) EstimatedDocumentCount(ctx context.Context,
// The opts parameter can be used to specify options for the operation (see the options.DistinctOptions documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/distinct/.
func (coll *Collection) Distinct(ctx context.Context, fieldName string, filter interface{},
opts ...*options.DistinctOptions) ([]interface{}, error) {
func (coll *Collection) Distinct(
ctx context.Context,
fieldName string,
filter interface{},
opts ...*options.DistinctOptions,
) *DistinctResult {

if ctx == nil {
ctx = context.Background()
}

f, err := marshal(filter, coll.bsonOpts, coll.registry)
if err != nil {
return nil, err
return &DistinctResult{err: err}
}

sess := sessionFromContext(ctx)
Expand All @@ -1309,7 +1313,7 @@ func (coll *Collection) Distinct(ctx context.Context, fieldName string, filter i

err = coll.client.validSession(sess)
if err != nil {
return nil, err
return &DistinctResult{err: err}
}

rc := coll.readConcern
Expand Down Expand Up @@ -1344,7 +1348,7 @@ func (coll *Collection) Distinct(ctx context.Context, fieldName string, filter i
if option.Comment != nil {
comment, err := marshalValue(option.Comment, coll.bsonOpts, coll.registry)
if err != nil {
return nil, err
return &DistinctResult{err: err}
}
op.Comment(comment)
}
Expand All @@ -1356,30 +1360,20 @@ func (coll *Collection) Distinct(ctx context.Context, fieldName string, filter i

err = op.Execute(ctx)
if err != nil {
return nil, replaceErrors(err)
return &DistinctResult{err: replaceErrors(err)}
}

arr, ok := op.Result().Values.ArrayOK()
if !ok {
return nil, fmt.Errorf("response field 'values' is type array, but received BSON type %s", op.Result().Values.Type)
}
err := fmt.Errorf("response field 'values' is type array, but received BSON type %s", op.Result().Values.Type)

values, err := arr.Values()
if err != nil {
return nil, err
return &DistinctResult{err: err}
}

retArray := make([]interface{}, len(values))

for i, val := range values {
raw := bson.RawValue{Type: bson.Type(val.Type), Value: val.Data}
err = raw.Unmarshal(&retArray[i])
if err != nil {
return nil, err
}
return &DistinctResult{
reg: coll.registry,
arr: bson.RawArray(arr),
}

return retArray, replaceErrors(err)
}

// mergeFindOptions combines the given FindOptions instances into a single FindOptions in a last-property-wins fashion.
Expand Down

0 comments on commit 1620f92

Please sign in to comment.