Skip to content

GODRIVER-1953 Remove bsonx from production code paths. #893

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

Merged
merged 3 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/documentation_examples/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import (
"time"

"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/examples/documentation_examples"
"go.mongodb.org/mongo-driver/internal/testutil"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/description"
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx"
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
"go.mongodb.org/mongo-driver/x/mongo/driver/topology"
)
Expand Down Expand Up @@ -174,7 +174,7 @@ func TestCausalConsistencyExamples(t *testing.T) {
func getServerVersion(ctx context.Context, client *mongo.Client) (string, error) {
serverStatus, err := client.Database("admin").RunCommand(
ctx,
bsonx.Doc{{"serverStatus", bsonx.Int32(1)}},
bson.D{{"serverStatus", 1}},
).DecodeBytes()
if err != nil {
return "", err
Expand Down
4 changes: 2 additions & 2 deletions mongo/client_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
"testing"

"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/internal/testutil"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx"
)

func TestClientOptions_CustomDialer(t *testing.T) {
Expand All @@ -27,7 +27,7 @@ func TestClientOptions_CustomDialer(t *testing.T) {
require.NoError(t, err)
err = client.Connect(context.Background())
require.NoError(t, err)
_, err = client.ListDatabases(context.Background(), bsonx.Doc{})
_, err = client.ListDatabases(context.Background(), bson.D{})
require.NoError(t, err)
got := atomic.LoadInt32(&td.called)
if got < 1 {
Expand Down
13 changes: 3 additions & 10 deletions mongo/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"go.mongodb.org/mongo-driver/mongo/readconcern"
"go.mongodb.org/mongo-driver/mongo/readpref"
"go.mongodb.org/mongo-driver/mongo/writeconcern"
"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/operation"
Expand Down Expand Up @@ -439,19 +438,13 @@ func (db *Database) ListCollectionNames(ctx context.Context, filter interface{},

names := make([]string, 0)
for res.Next(ctx) {
next := &bsonx.Doc{}
err = res.Decode(next)
elem, err := res.Current.LookupErr("name")
if err != nil {
return nil, err
}

elem, err := next.LookupErr("name")
if err != nil {
return nil, err
}

if elem.Type() != bson.TypeString {
return nil, fmt.Errorf("incorrect type for 'name'. got %v. want %v", elem.Type(), bson.TypeString)
if elem.Type != bson.TypeString {
return nil, fmt.Errorf("incorrect type for 'name'. got %v. want %v", elem.Type, bson.TypeString)
}

elemName := elem.StringValue()
Expand Down
71 changes: 16 additions & 55 deletions mongo/gridfs/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"go.mongodb.org/mongo-driver/mongo/readconcern"
"go.mongodb.org/mongo-driver/mongo/readpref"
"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 @@ -59,7 +58,7 @@ type Bucket struct {
// Upload contains options to upload a file to a bucket.
type Upload struct {
chunkSize int32
metadata bsonx.Doc
metadata bson.D
}

// NewBucket creates a GridFS bucket.
Expand Down Expand Up @@ -186,12 +185,8 @@ func (b *Bucket) UploadFromStreamWithID(fileID interface{}, filename string, sou

// OpenDownloadStream creates a stream from which the contents of the file can be read.
func (b *Bucket) OpenDownloadStream(fileID interface{}) (*DownloadStream, error) {
id, err := convertFileID(fileID)
if err != nil {
return nil, err
}
return b.openDownloadStream(bsonx.Doc{
{"_id", id},
return b.openDownloadStream(bson.D{
{"_id", fileID},
})
}

Expand Down Expand Up @@ -224,9 +219,9 @@ func (b *Bucket) OpenDownloadStreamByName(filename string, opts ...*options.Name
numSkip = (-1 * numSkip) - 1
}

findOpts := options.Find().SetSkip(int64(numSkip)).SetSort(bsonx.Doc{{"uploadDate", bsonx.Int32(sortOrder)}})
findOpts := options.Find().SetSkip(int64(numSkip)).SetSort(bson.D{{"uploadDate", sortOrder}})

return b.openDownloadStream(bsonx.Doc{{"filename", bsonx.String(filename)}}, findOpts)
return b.openDownloadStream(bson.D{{"filename", filename}}, findOpts)
}

// DownloadToStreamByName downloads the file with the given name to the given io.Writer.
Expand Down Expand Up @@ -254,11 +249,7 @@ func (b *Bucket) Delete(fileID interface{}) error {
defer cancel()
}

id, err := convertFileID(fileID)
if err != nil {
return err
}
res, err := b.filesColl.DeleteOne(ctx, bsonx.Doc{{"_id", id}})
res, err := b.filesColl.DeleteOne(ctx, bson.D{{"_id", fileID}})
if err == nil && res.DeletedCount == 0 {
err = ErrFileNotFound
}
Expand Down Expand Up @@ -317,13 +308,9 @@ func (b *Bucket) Rename(fileID interface{}, newFilename string) error {
defer cancel()
}

id, err := convertFileID(fileID)
if err != nil {
return err
}
res, err := b.filesColl.UpdateOne(ctx,
bsonx.Doc{{"_id", id}},
bsonx.Doc{{"$set", bsonx.Document(bsonx.Doc{{"filename", bsonx.String(newFilename)}})}},
bson.D{{"_id", fileID}},
bson.D{{"$set", bson.D{{"filename", newFilename}}}},
)
if err != nil {
return err
Expand Down Expand Up @@ -426,11 +413,7 @@ func (b *Bucket) downloadToStream(ds *DownloadStream, stream io.Writer) (int64,
}

func (b *Bucket) deleteChunks(ctx context.Context, fileID interface{}) error {
id, err := convertFileID(fileID)
if err != nil {
return err
}
_, err = b.chunksColl.DeleteMany(ctx, bsonx.Doc{{"files_id", id}})
_, err := b.chunksColl.DeleteMany(ctx, bson.D{{"files_id", fileID}})
return err
}

Expand All @@ -449,13 +432,9 @@ func (b *Bucket) findFile(ctx context.Context, filter interface{}, opts ...*opti
}

func (b *Bucket) findChunks(ctx context.Context, fileID interface{}) (*mongo.Cursor, error) {
id, err := convertFileID(fileID)
if err != nil {
return nil, err
}
chunksCursor, err := b.chunksColl.Find(ctx,
bsonx.Doc{{"files_id", id}},
options.Find().SetSort(bsonx.Doc{{"n", bsonx.Int32(1)}})) // sort by chunk index
bson.D{{"files_id", fileID}},
options.Find().SetSort(bson.D{{"n", 1}})) // sort by chunk index
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -550,7 +529,7 @@ func (b *Bucket) createIndexes(ctx context.Context) error {
return err
}

docRes := cloned.FindOne(ctx, bsonx.Doc{}, options.FindOne().SetProjection(bsonx.Doc{{"_id", bsonx.Int32(1)}}))
docRes := cloned.FindOne(ctx, bson.D{}, options.FindOne().SetProjection(bson.D{{"_id", 1}}))

_, err = docRes.DecodeBytes()
if err != mongo.ErrNoDocuments {
Expand Down Expand Up @@ -617,31 +596,13 @@ func (b *Bucket) parseUploadOptions(opts ...*options.UploadOptions) (*Upload, er
if err != nil {
return nil, err
}
doc, err := bsonx.ReadDoc(raw)
if err != nil {
return nil, err
var doc bson.D
unMarErr := bson.UnmarshalWithRegistry(uo.Registry, raw, &doc)
if unMarErr != nil {
return nil, unMarErr
}
upload.metadata = doc
}

return upload, nil
}

type _convertFileID struct {
ID interface{} `bson:"_id"`
}

func convertFileID(fileID interface{}) (bsonx.Val, error) {
id := _convertFileID{
ID: fileID,
}

b, err := bson.Marshal(id)
if err != nil {
return bsonx.Val{}, err
}
val := bsoncore.Document(b).Lookup("_id")
var res bsonx.Val
err = res.UnmarshalBSONValue(val.Type, val.Data)
return res, err
}
44 changes: 16 additions & 28 deletions mongo/gridfs/upload_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (

"math"

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

// UploadBufferSize is the size in bytes of one stream batch. Chunks will be written to the db after the sum of chunk
Expand Down Expand Up @@ -137,11 +137,7 @@ func (us *UploadStream) Abort() error {
defer cancel()
}

id, err := convertFileID(us.FileID)
if err != nil {
return err
}
_, err = us.chunksColl.DeleteMany(ctx, bsonx.Doc{{"files_id", id}})
_, err := us.chunksColl.DeleteMany(ctx, bson.D{{"files_id", us.FileID}})
if err != nil {
return err
}
Expand All @@ -163,10 +159,6 @@ func (us *UploadStream) uploadChunks(ctx context.Context, uploadPartial bool) er

docs := make([]interface{}, numChunks)

id, err := convertFileID(us.FileID)
if err != nil {
return err
}
begChunkIndex := us.chunkIndex
for i := 0; i < us.bufferIndex; i += int(us.chunkSize) {
endIndex := i + int(us.chunkSize)
Expand All @@ -178,17 +170,17 @@ func (us *UploadStream) uploadChunks(ctx context.Context, uploadPartial bool) er
endIndex = us.bufferIndex
}
chunkData := us.buffer[i:endIndex]
docs[us.chunkIndex-begChunkIndex] = bsonx.Doc{
{"_id", bsonx.ObjectID(primitive.NewObjectID())},
{"files_id", id},
{"n", bsonx.Int32(int32(us.chunkIndex))},
{"data", bsonx.Binary(0x00, chunkData)},
docs[us.chunkIndex-begChunkIndex] = bson.D{
{"_id", primitive.NewObjectID()},
{"files_id", us.FileID},
{"n", int32(us.chunkIndex)},
{"data", primitive.Binary{Subtype: 0x00, Data: chunkData}},
}
us.chunkIndex++
us.fileLen += int64(len(chunkData))
}

_, err = us.chunksColl.InsertMany(ctx, docs)
_, err := us.chunksColl.InsertMany(ctx, docs)
if err != nil {
return err
}
Expand All @@ -203,23 +195,19 @@ func (us *UploadStream) uploadChunks(ctx context.Context, uploadPartial bool) er
}

func (us *UploadStream) createFilesCollDoc(ctx context.Context) error {
id, err := convertFileID(us.FileID)
if err != nil {
return err
}
doc := bsonx.Doc{
{"_id", id},
{"length", bsonx.Int64(us.fileLen)},
{"chunkSize", bsonx.Int32(us.chunkSize)},
{"uploadDate", bsonx.DateTime(time.Now().UnixNano() / int64(time.Millisecond))},
{"filename", bsonx.String(us.filename)},
doc := bson.D{
{"_id", us.FileID},
{"length", us.fileLen},
{"chunkSize", us.chunkSize},
{"uploadDate", primitive.DateTime(time.Now().UnixNano() / int64(time.Millisecond))},
{"filename", us.filename},
}

if us.metadata != nil {
doc = append(doc, bsonx.Elem{"metadata", bsonx.Document(us.metadata)})
doc = append(doc, bson.E{"metadata", us.metadata})
}

_, err = us.filesColl.InsertOne(ctx, doc)
_, err := us.filesColl.InsertOne(ctx, doc)
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions mongo/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
"go.mongodb.org/mongo-driver/internal/testutil/assert"
"go.mongodb.org/mongo-driver/mongo/readconcern"
"go.mongodb.org/mongo-driver/mongo/readpref"
"go.mongodb.org/mongo-driver/mongo/writeconcern"
"go.mongodb.org/mongo-driver/x/bsonx"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)

func TestMain(m *testing.M) {
Expand All @@ -32,8 +33,8 @@ func TestMain(m *testing.M) {
assert.RegisterOpts(reflect.TypeOf(&readconcern.ReadConcern{}), cmp.AllowUnexported(readconcern.ReadConcern{}))
assert.RegisterOpts(reflect.TypeOf(&writeconcern.WriteConcern{}), cmp.AllowUnexported(writeconcern.WriteConcern{}))
assert.RegisterOpts(reflect.TypeOf(&readpref.ReadPref{}), cmp.AllowUnexported(readpref.ReadPref{}))
assert.RegisterOpts(reflect.TypeOf(bsonx.Doc{}), cmp.AllowUnexported(bsonx.Elem{}, bsonx.Val{}))
assert.RegisterOpts(reflect.TypeOf(bsonx.Arr{}), cmp.AllowUnexported(bsonx.Val{}))
assert.RegisterOpts(reflect.TypeOf(bson.D{}), cmp.AllowUnexported(bson.E{}, bsoncore.Value{}))
assert.RegisterOpts(reflect.TypeOf(bson.A{}), cmp.AllowUnexported(bsoncore.Value{}))

os.Exit(m.Run())
}
11 changes: 0 additions & 11 deletions mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,6 @@ func transformAndEnsureID(registry *bsoncodec.Registry, val interface{}) (bsonco
return doc, id, nil
}

func transformDocument(registry *bsoncodec.Registry, val interface{}) (bsonx.Doc, error) {
if doc, ok := val.(bsonx.Doc); ok {
return doc.Copy(), nil
}
b, err := transformBsoncoreDocument(registry, val, true, "document")
if err != nil {
return nil, err
}
return bsonx.ReadDoc(b)
}

func transformBsoncoreDocument(registry *bsoncodec.Registry, val interface{}, mapAllowed bool, paramName string) (bsoncore.Document, error) {
if registry == nil {
registry = bson.DefaultRegistry
Expand Down
Loading