Skip to content

Commit 63d46a3

Browse files
authored
GODRIVER-2416 Completely remove the 'x/bsonx' package. (#1122)
1 parent 2f4c9b7 commit 63d46a3

24 files changed

+40
-5371
lines changed

benchmark/bson.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"path/filepath"
1313

1414
"go.mongodb.org/mongo-driver/bson"
15-
"go.mongodb.org/mongo-driver/x/bsonx"
1615
)
1716

1817
const (
@@ -25,12 +24,12 @@ const (
2524

2625
// utility functions for the bson benchmarks
2726

28-
func loadSourceDocument(pathParts ...string) (bsonx.Doc, error) {
27+
func loadSourceDocument(pathParts ...string) (bson.D, error) {
2928
data, err := ioutil.ReadFile(filepath.Join(pathParts...))
3029
if err != nil {
3130
return nil, err
3231
}
33-
doc := bsonx.Doc{}
32+
var doc bson.D
3433
err = bson.UnmarshalExtJSON(data, true, &doc)
3534
if err != nil {
3635
return nil, err
@@ -48,7 +47,7 @@ func loadSourceRaw(pathParts ...string) (bson.Raw, error) {
4847
if err != nil {
4948
return nil, err
5049
}
51-
raw, err := doc.MarshalBSON()
50+
raw, err := bson.Marshal(doc)
5251
if err != nil {
5352
return nil, err
5453
}

benchmark/bson_document.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"context"
1111
"errors"
1212

13-
"go.mongodb.org/mongo-driver/x/bsonx"
13+
"go.mongodb.org/mongo-driver/bson"
1414
)
1515

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

2424
for i := 0; i < iters; i++ {
25-
out, err := doc.MarshalBSON()
25+
out, err := bson.Marshal(doc)
2626
if err != nil {
2727
return err
2828
}
@@ -40,15 +40,16 @@ func bsonDocumentDecodingLazy(tm TimerManager, iters int, source string) error {
4040
return err
4141
}
4242

43-
raw, err := doc.MarshalBSON()
43+
raw, err := bson.Marshal(doc)
4444
if err != nil {
4545
return err
4646
}
4747

4848
tm.ResetTimer()
4949

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

68-
raw, err := doc.MarshalBSON()
69+
raw, err := bson.Marshal(doc)
6970
if err != nil {
7071
return err
7172
}
7273

7374
tm.ResetTimer()
7475

7576
for i := 0; i < iters; i++ {
76-
out, err := bsonx.ReadDoc(raw)
77+
var out bson.D
78+
err := bson.Unmarshal(raw, &out)
7779
if err != nil {
7880
return err
7981
}

benchmark/multi.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"context"
1111
"errors"
1212

13-
"go.mongodb.org/mongo-driver/x/bsonx"
13+
"go.mongodb.org/mongo-driver/bson"
1414
)
1515

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

4747
tm.ResetTimer()
4848

49-
cursor, err := coll.Find(ctx, bsonx.Doc{})
49+
cursor, err := coll.Find(ctx, bson.D{})
5050
if err != nil {
5151
return err
5252
}
@@ -103,7 +103,7 @@ func multiInsertCase(ctx context.Context, tm TimerManager, iters int, data strin
103103
return err
104104
}
105105

106-
err = db.RunCommand(ctx, bsonx.Doc{{"create", bsonx.String("corpus")}}).Err()
106+
err = db.RunCommand(ctx, bson.D{{"create", "corpus"}}).Err()
107107
if err != nil {
108108
return err
109109
}

benchmark/single.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010
"context"
1111
"errors"
1212

13+
"go.mongodb.org/mongo-driver/bson"
1314
"go.mongodb.org/mongo-driver/internal"
1415
"go.mongodb.org/mongo-driver/internal/testutil"
1516
"go.mongodb.org/mongo-driver/mongo"
1617
"go.mongodb.org/mongo-driver/mongo/options"
17-
"go.mongodb.org/mongo-driver/x/bsonx"
1818
)
1919

2020
const (
@@ -51,17 +51,17 @@ func SingleRunCommand(ctx context.Context, tm TimerManager, iters int) error {
5151
}
5252
defer db.Client().Disconnect(ctx)
5353

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

5656
tm.ResetTimer()
5757
for i := 0; i < iters; i++ {
58-
var doc bsonx.Doc
58+
var doc bson.D
5959
err := db.RunCommand(ctx, cmd).Decode(&doc)
6060
if err != nil {
6161
return err
6262
}
6363
// read the document and then throw it away to prevent
64-
out, err := doc.MarshalBSON()
64+
out, err := bson.Marshal(doc)
6565
if err != nil {
6666
return err
6767
}
@@ -93,23 +93,27 @@ func SingleFindOneByID(ctx context.Context, tm TimerManager, iters int) error {
9393
return err
9494
}
9595
coll := db.Collection("corpus")
96+
9697
for i := 0; i < iters; i++ {
97-
id := int32(i)
98-
res, err := coll.InsertOne(ctx, doc.Set("_id", bsonx.Int32(id)))
98+
idDoc := make(bson.D, 0, len(doc)+1)
99+
idDoc = append(idDoc, bson.E{"_id", i})
100+
idDoc = append(idDoc, doc...)
101+
res, err := coll.InsertOne(ctx, idDoc)
99102
if err != nil {
100103
return err
101104
}
102105
if res.InsertedID == nil {
103-
return errors.New("insert failed")
106+
return errors.New("no inserted ID returned")
104107
}
105108
}
106109

107110
tm.ResetTimer()
108111

109112
for i := 0; i < iters; i++ {
110-
res := coll.FindOne(ctx, bsonx.Doc{{"_id", bsonx.Int32(int32(i))}})
111-
if res == nil {
112-
return errors.New("find one query produced nil result")
113+
var res bson.D
114+
err := coll.FindOne(ctx, bson.D{{"_id", i}}).Decode(&res)
115+
if err != nil {
116+
return err
113117
}
114118
}
115119

@@ -142,7 +146,7 @@ func singleInsertCase(ctx context.Context, tm TimerManager, iters int, data stri
142146
return err
143147
}
144148

145-
err = db.RunCommand(ctx, bsonx.Doc{{"create", bsonx.String("corpus")}}).Err()
149+
err = db.RunCommand(ctx, bson.D{{"create", "corpus"}}).Err()
146150
if err != nil {
147151
return err
148152
}
@@ -155,9 +159,6 @@ func singleInsertCase(ctx context.Context, tm TimerManager, iters int, data stri
155159
if _, err = coll.InsertOne(ctx, doc); err != nil {
156160
return err
157161
}
158-
159-
// TODO: should be remove after resolving GODRIVER-468
160-
_ = doc.Delete("_id")
161162
}
162163

163164
tm.StopTimer()

mongo/cursor.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
"go.mongodb.org/mongo-driver/bson"
1717
"go.mongodb.org/mongo-driver/bson/bsoncodec"
18-
"go.mongodb.org/mongo-driver/x/bsonx"
1918
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
2019
"go.mongodb.org/mongo-driver/x/mongo/driver"
2120
"go.mongodb.org/mongo-driver/x/mongo/driver/session"
@@ -83,8 +82,6 @@ func NewCursorFromDocuments(documents []interface{}, err error, registry *bsonco
8382
switch t := doc.(type) {
8483
case nil:
8584
return nil, ErrNilDocument
86-
case bsonx.Doc:
87-
doc = t.Copy()
8885
case []byte:
8986
// Slight optimization so we'll just use MarshalBSON and not go through the codec machinery.
9087
doc = bson.Raw(t)

mongo/integration/collection_test.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
2121
"go.mongodb.org/mongo-driver/mongo/options"
2222
"go.mongodb.org/mongo-driver/mongo/writeconcern"
23-
"go.mongodb.org/mongo-driver/x/bsonx"
2423
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
2524
)
2625

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

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

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

498495
testCases := []struct {
499496
name string
@@ -502,7 +499,6 @@ func TestCollection(t *testing.T) {
502499
{"bsoncore Document", bsoncore.Document(docBytes)},
503500
{"bson Raw", bson.Raw(docBytes)},
504501
{"bson D", doc},
505-
{"bsonx Document", xDoc},
506502
{"byte slice", docBytes},
507503
}
508504
for _, tc := range testCases {
@@ -707,8 +703,8 @@ func TestCollection(t *testing.T) {
707703
assert.NotNil(mt, res.UpsertedID, "expected upserted ID, got nil")
708704
})
709705
mt.Run("write error", func(mt *mtest.T) {
710-
filter := bsonx.Doc{{"_id", bsonx.String("foo")}}
711-
replacement := bsonx.Doc{{"_id", bsonx.Double(3.14159)}}
706+
filter := bson.D{{"_id", "foo"}}
707+
replacement := bson.D{{"_id", 3.14159}}
712708
_, err := mt.Coll.InsertOne(context.Background(), filter)
713709
assert.Nil(mt, err, "InsertOne error: %v", err)
714710

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

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

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

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

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

@@ -1884,7 +1880,7 @@ func testAggregateWithOptions(mt *mtest.T, createIndex bool, opts *options.Aggre
18841880
if createIndex {
18851881
indexView := mt.Coll.Indexes()
18861882
_, err := indexView.CreateOne(context.Background(), mongo.IndexModel{
1887-
Keys: bsonx.Doc{{"x", bsonx.Int32(1)}},
1883+
Keys: bson.D{{"x", 1}},
18881884
})
18891885
assert.Nil(mt, err, "CreateOne error: %v", err)
18901886
}

mongo/mongo.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"strings"
1717

1818
"go.mongodb.org/mongo-driver/mongo/options"
19-
"go.mongodb.org/mongo-driver/x/bsonx"
2019
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
2120

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

0 commit comments

Comments
 (0)