Skip to content

Commit

Permalink
GODRIVER-2416 Completely remove the 'x/bsonx' package. (mongodb#1122)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewdale authored Nov 16, 2022
1 parent 2f4c9b7 commit 63d46a3
Show file tree
Hide file tree
Showing 24 changed files with 40 additions and 5,371 deletions.
7 changes: 3 additions & 4 deletions benchmark/bson.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"path/filepath"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/x/bsonx"
)

const (
Expand All @@ -25,12 +24,12 @@ const (

// utility functions for the bson benchmarks

func loadSourceDocument(pathParts ...string) (bsonx.Doc, error) {
func loadSourceDocument(pathParts ...string) (bson.D, error) {
data, err := ioutil.ReadFile(filepath.Join(pathParts...))
if err != nil {
return nil, err
}
doc := bsonx.Doc{}
var doc bson.D
err = bson.UnmarshalExtJSON(data, true, &doc)
if err != nil {
return nil, err
Expand All @@ -48,7 +47,7 @@ func loadSourceRaw(pathParts ...string) (bson.Raw, error) {
if err != nil {
return nil, err
}
raw, err := doc.MarshalBSON()
raw, err := bson.Marshal(doc)
if err != nil {
return nil, err
}
Expand Down
14 changes: 8 additions & 6 deletions benchmark/bson_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"context"
"errors"

"go.mongodb.org/mongo-driver/x/bsonx"
"go.mongodb.org/mongo-driver/bson"
)

func bsonDocumentEncoding(tm TimerManager, iters int, source string) error {
Expand All @@ -22,7 +22,7 @@ func bsonDocumentEncoding(tm TimerManager, iters int, source string) error {
tm.ResetTimer()

for i := 0; i < iters; i++ {
out, err := doc.MarshalBSON()
out, err := bson.Marshal(doc)
if err != nil {
return err
}
Expand All @@ -40,15 +40,16 @@ func bsonDocumentDecodingLazy(tm TimerManager, iters int, source string) error {
return err
}

raw, err := doc.MarshalBSON()
raw, err := bson.Marshal(doc)
if err != nil {
return err
}

tm.ResetTimer()

for i := 0; i < iters; i++ {
out, err := bsonx.ReadDoc(raw)
var out bson.D
err := bson.Unmarshal(raw, &out)
if err != nil {
return err
}
Expand All @@ -65,15 +66,16 @@ func bsonDocumentDecoding(tm TimerManager, iters, numKeys int, source string) er
return err
}

raw, err := doc.MarshalBSON()
raw, err := bson.Marshal(doc)
if err != nil {
return err
}

tm.ResetTimer()

for i := 0; i < iters; i++ {
out, err := bsonx.ReadDoc(raw)
var out bson.D
err := bson.Unmarshal(raw, &out)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions benchmark/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"context"
"errors"

"go.mongodb.org/mongo-driver/x/bsonx"
"go.mongodb.org/mongo-driver/bson"
)

func MultiFindMany(ctx context.Context, tm TimerManager, iters int) error {
Expand Down Expand Up @@ -46,7 +46,7 @@ func MultiFindMany(ctx context.Context, tm TimerManager, iters int) error {

tm.ResetTimer()

cursor, err := coll.Find(ctx, bsonx.Doc{})
cursor, err := coll.Find(ctx, bson.D{})
if err != nil {
return err
}
Expand Down Expand Up @@ -103,7 +103,7 @@ func multiInsertCase(ctx context.Context, tm TimerManager, iters int, data strin
return err
}

err = db.RunCommand(ctx, bsonx.Doc{{"create", bsonx.String("corpus")}}).Err()
err = db.RunCommand(ctx, bson.D{{"create", "corpus"}}).Err()
if err != nil {
return err
}
Expand Down
29 changes: 15 additions & 14 deletions benchmark/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
"context"
"errors"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/internal"
"go.mongodb.org/mongo-driver/internal/testutil"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx"
)

const (
Expand Down Expand Up @@ -51,17 +51,17 @@ func SingleRunCommand(ctx context.Context, tm TimerManager, iters int) error {
}
defer db.Client().Disconnect(ctx)

cmd := bsonx.Doc{{internal.LegacyHelloLowercase, bsonx.Boolean(true)}}
cmd := bson.D{{internal.LegacyHelloLowercase, true}}

tm.ResetTimer()
for i := 0; i < iters; i++ {
var doc bsonx.Doc
var doc bson.D
err := db.RunCommand(ctx, cmd).Decode(&doc)
if err != nil {
return err
}
// read the document and then throw it away to prevent
out, err := doc.MarshalBSON()
out, err := bson.Marshal(doc)
if err != nil {
return err
}
Expand Down Expand Up @@ -93,23 +93,27 @@ func SingleFindOneByID(ctx context.Context, tm TimerManager, iters int) error {
return err
}
coll := db.Collection("corpus")

for i := 0; i < iters; i++ {
id := int32(i)
res, err := coll.InsertOne(ctx, doc.Set("_id", bsonx.Int32(id)))
idDoc := make(bson.D, 0, len(doc)+1)
idDoc = append(idDoc, bson.E{"_id", i})
idDoc = append(idDoc, doc...)
res, err := coll.InsertOne(ctx, idDoc)
if err != nil {
return err
}
if res.InsertedID == nil {
return errors.New("insert failed")
return errors.New("no inserted ID returned")
}
}

tm.ResetTimer()

for i := 0; i < iters; i++ {
res := coll.FindOne(ctx, bsonx.Doc{{"_id", bsonx.Int32(int32(i))}})
if res == nil {
return errors.New("find one query produced nil result")
var res bson.D
err := coll.FindOne(ctx, bson.D{{"_id", i}}).Decode(&res)
if err != nil {
return err
}
}

Expand Down Expand Up @@ -142,7 +146,7 @@ func singleInsertCase(ctx context.Context, tm TimerManager, iters int, data stri
return err
}

err = db.RunCommand(ctx, bsonx.Doc{{"create", bsonx.String("corpus")}}).Err()
err = db.RunCommand(ctx, bson.D{{"create", "corpus"}}).Err()
if err != nil {
return err
}
Expand All @@ -155,9 +159,6 @@ func singleInsertCase(ctx context.Context, tm TimerManager, iters int, data stri
if _, err = coll.InsertOne(ctx, doc); err != nil {
return err
}

// TODO: should be remove after resolving GODRIVER-468
_ = doc.Delete("_id")
}

tm.StopTimer()
Expand Down
3 changes: 0 additions & 3 deletions mongo/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
"go.mongodb.org/mongo-driver/x/bsonx"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
"go.mongodb.org/mongo-driver/x/mongo/driver"
"go.mongodb.org/mongo-driver/x/mongo/driver/session"
Expand Down Expand Up @@ -83,8 +82,6 @@ func NewCursorFromDocuments(documents []interface{}, err error, registry *bsonco
switch t := doc.(type) {
case nil:
return nil, ErrNilDocument
case bsonx.Doc:
doc = t.Copy()
case []byte:
// Slight optimization so we'll just use MarshalBSON and not go through the codec machinery.
doc = bson.Raw(t)
Expand Down
24 changes: 10 additions & 14 deletions mongo/integration/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/writeconcern"
"go.mongodb.org/mongo-driver/x/bsonx"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)

Expand Down Expand Up @@ -337,7 +336,7 @@ func TestCollection(t *testing.T) {
initCollection(mt, mt.Coll)
indexView := mt.Coll.Indexes()
_, err := indexView.CreateOne(context.Background(), mongo.IndexModel{
Keys: bsonx.Doc{{"x", bsonx.Int32(1)}},
Keys: bson.D{{"x", 1}},
})
assert.Nil(mt, err, "CreateOne error: %v", err)

Expand Down Expand Up @@ -406,7 +405,7 @@ func TestCollection(t *testing.T) {
initCollection(mt, mt.Coll)
indexView := mt.Coll.Indexes()
_, err := indexView.CreateOne(context.Background(), mongo.IndexModel{
Keys: bsonx.Doc{{"x", bsonx.Int32(1)}},
Keys: bson.D{{"x", 1}},
})
assert.Nil(mt, err, "index CreateOne error: %v", err)

Expand Down Expand Up @@ -492,8 +491,6 @@ func TestCollection(t *testing.T) {
doc := bson.D{{"$set", bson.D{{"x", 2}}}}
docBytes, err := bson.Marshal(doc)
assert.Nil(mt, err, "Marshal error: %v", err)
xUpdate := bsonx.Doc{{"x", bsonx.Int32(2)}}
xDoc := bsonx.Doc{{"$set", bsonx.Document(xUpdate)}}

testCases := []struct {
name string
Expand All @@ -502,7 +499,6 @@ func TestCollection(t *testing.T) {
{"bsoncore Document", bsoncore.Document(docBytes)},
{"bson Raw", bson.Raw(docBytes)},
{"bson D", doc},
{"bsonx Document", xDoc},
{"byte slice", docBytes},
}
for _, tc := range testCases {
Expand Down Expand Up @@ -707,8 +703,8 @@ func TestCollection(t *testing.T) {
assert.NotNil(mt, res.UpsertedID, "expected upserted ID, got nil")
})
mt.Run("write error", func(mt *mtest.T) {
filter := bsonx.Doc{{"_id", bsonx.String("foo")}}
replacement := bsonx.Doc{{"_id", bsonx.Double(3.14159)}}
filter := bson.D{{"_id", "foo"}}
replacement := bson.D{{"_id", 3.14159}}
_, err := mt.Coll.InsertOne(context.Background(), filter)
assert.Nil(mt, err, "InsertOne error: %v", err)

Expand Down Expand Up @@ -841,7 +837,7 @@ func TestCollection(t *testing.T) {
initCollection(mt, mt.Coll)
indexView := mt.Coll.Indexes()
_, err := indexView.CreateOne(context.Background(), mongo.IndexModel{
Keys: bsonx.Doc{{"x", bsonx.Int32(1)}},
Keys: bson.D{{"x", 1}},
})
assert.Nil(mt, err, "CreateOne error: %v", err)

Expand Down Expand Up @@ -1211,7 +1207,7 @@ func TestCollection(t *testing.T) {
initCollection(mt, mt.Coll)
indexView := mt.Coll.Indexes()
_, err := indexView.CreateOne(context.Background(), mongo.IndexModel{
Keys: bsonx.Doc{{"x", bsonx.Int32(1)}},
Keys: bson.D{{"x", 1}},
})
assert.Nil(mt, err, "CreateOne error: %v", err)

Expand Down Expand Up @@ -1271,7 +1267,7 @@ func TestCollection(t *testing.T) {
initCollection(mt, mt.Coll)
indexView := mt.Coll.Indexes()
_, err := indexView.CreateOne(context.Background(), mongo.IndexModel{
Keys: bsonx.Doc{{"x", bsonx.Int32(1)}},
Keys: bson.D{{"x", 1}},
})
assert.Nil(mt, err, "CreateOne error: %v", err)

Expand Down Expand Up @@ -1347,7 +1343,7 @@ func TestCollection(t *testing.T) {
initCollection(mt, mt.Coll)
indexView := mt.Coll.Indexes()
_, err := indexView.CreateOne(context.Background(), mongo.IndexModel{
Keys: bsonx.Doc{{"x", bsonx.Int32(1)}},
Keys: bson.D{{"x", 1}},
})
assert.Nil(mt, err, "CreateOne error: %v", err)

Expand Down Expand Up @@ -1429,7 +1425,7 @@ func TestCollection(t *testing.T) {
initCollection(mt, mt.Coll)
indexView := mt.Coll.Indexes()
_, err := indexView.CreateOne(context.Background(), mongo.IndexModel{
Keys: bsonx.Doc{{"x", bsonx.Int32(1)}},
Keys: bson.D{{"x", 1}},
})
assert.Nil(mt, err, "CreateOne error: %v", err)

Expand Down Expand Up @@ -1884,7 +1880,7 @@ func testAggregateWithOptions(mt *mtest.T, createIndex bool, opts *options.Aggre
if createIndex {
indexView := mt.Coll.Indexes()
_, err := indexView.CreateOne(context.Background(), mongo.IndexModel{
Keys: bsonx.Doc{{"x", bsonx.Int32(1)}},
Keys: bson.D{{"x", 1}},
})
assert.Nil(mt, err, "CreateOne error: %v", err)
}
Expand Down
5 changes: 1 addition & 4 deletions mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"strings"

"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"

"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -80,8 +79,6 @@ func transformAndEnsureID(registry *bsoncodec.Registry, val interface{}) (bsonco
switch tt := val.(type) {
case nil:
return nil, nil, ErrNilDocument
case bsonx.Doc:
val = tt.Copy()
case []byte:
// Slight optimization so we'll just use MarshalBSON and not go through the codec machinery.
val = bson.Raw(tt)
Expand Down Expand Up @@ -264,7 +261,7 @@ func transformUpdateValue(registry *bsoncodec.Registry, update interface{}, doll
switch t := update.(type) {
case nil:
return u, ErrNilDocument
case primitive.D, bsonx.Doc:
case primitive.D:
u.Type = bsontype.EmbeddedDocument
u.Data, err = transformBsoncoreDocument(registry, update, true, "update")
if err != nil {
Expand Down
Loading

0 comments on commit 63d46a3

Please sign in to comment.