Skip to content

Commit

Permalink
GODRIVER-2726 Merge the gridfs into the mongo package (#1497)
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvasquez authored Jan 18, 2024
1 parent df800a9 commit 8acba08
Show file tree
Hide file tree
Showing 13 changed files with 214 additions and 274 deletions.
5 changes: 2 additions & 3 deletions internal/integration/crud_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"go.mongodb.org/mongo-driver/internal/integration/unified"
"go.mongodb.org/mongo-driver/internal/integtest"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/gridfs"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readconcern"
"go.mongodb.org/mongo-driver/mongo/readpref"
Expand Down Expand Up @@ -1216,7 +1215,7 @@ func executeEstimatedDocumentCount(mt *mtest.T, sess mongo.Session, args bson.Ra
return mt.Coll.EstimatedDocumentCount(context.Background())
}

func executeGridFSDownload(mt *mtest.T, bucket *gridfs.Bucket, args bson.Raw) (int64, error) {
func executeGridFSDownload(mt *mtest.T, bucket *mongo.GridFSBucket, args bson.Raw) (int64, error) {
mt.Helper()

var fileID primitive.ObjectID
Expand All @@ -1236,7 +1235,7 @@ func executeGridFSDownload(mt *mtest.T, bucket *gridfs.Bucket, args bson.Raw) (i
return bucket.DownloadToStream(context.Background(), fileID, new(bytes.Buffer))
}

func executeGridFSDownloadByName(mt *mtest.T, bucket *gridfs.Bucket, args bson.Raw) (int64, error) {
func executeGridFSDownloadByName(mt *mtest.T, bucket *mongo.GridFSBucket, args bson.Raw) (int64, error) {
mt.Helper()

var file string
Expand Down
32 changes: 16 additions & 16 deletions internal/integration/gridfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"go.mongodb.org/mongo-driver/internal/integration/mtest"
"go.mongodb.org/mongo-driver/internal/israce"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/gridfs"
"go.mongodb.org/mongo-driver/mongo/options"
)

Expand Down Expand Up @@ -73,7 +72,7 @@ func TestGridFS(x *testing.T) {

for _, tc := range testcases {
mt.Run(tc.name, func(mt *mtest.T) {
bucket, err := gridfs.NewBucket(mt.DB, options.GridFSBucket().SetChunkSizeBytes(chunkSize))
bucket, err := mt.DB.GridFSBucket(options.GridFSBucket().SetChunkSizeBytes(chunkSize))
assert.Nil(mt, err, "NewBucket error: %v", err)

ustream, err := bucket.OpenUploadStream(context.Background(), "foo")
Expand Down Expand Up @@ -108,7 +107,7 @@ func TestGridFS(x *testing.T) {

mt.Run("index creation", func(mt *mtest.T) {
// Unit tests showing that UploadFromStream creates indexes on the chunks and files collections.
bucket, err := gridfs.NewBucket(mt.DB)
bucket, err := mt.DB.GridFSBucket()
assert.Nil(mt, err, "NewBucket error: %v", err)

byteData := []byte("Hello, world!")
Expand Down Expand Up @@ -187,7 +186,7 @@ func TestGridFS(x *testing.T) {

mt.ClearEvents()

bucket, err := gridfs.NewBucket(mt.DB)
bucket, err := mt.DB.GridFSBucket()
assert.Nil(mt, err, "NewBucket error: %v", err)
defer func() {
_ = bucket.Drop(context.Background())
Expand Down Expand Up @@ -234,7 +233,7 @@ func TestGridFS(x *testing.T) {

mt.ClearEvents()
var fileContent []byte
bucket, err := gridfs.NewBucket(mt.DB)
bucket, err := mt.DB.GridFSBucket()
assert.Nil(mt, err, "NewBucket error: %v", err)
defer func() {
_ = bucket.Drop(context.Background())
Expand Down Expand Up @@ -282,7 +281,7 @@ func TestGridFS(x *testing.T) {
for _, tc := range testCases {
mt.Run(tc.name, func(mt *mtest.T) {
// Create a new GridFS bucket.
bucket, err := gridfs.NewBucket(mt.DB)
bucket, err := mt.DB.GridFSBucket()
assert.Nil(mt, err, "NewBucket error: %v", err)
defer func() { _ = bucket.Drop(context.Background()) }()

Expand All @@ -303,10 +302,10 @@ func TestGridFS(x *testing.T) {
assert.Nil(mt, err, "FindOne error: %v", err)
uploadTime := uploadedFileDoc.Lookup("uploadDate").Time().UTC()

expectedFile := &gridfs.File{
expectedFile := &mongo.GridFSFile{
ID: uploadedFileID,
Length: int64(len(fileData)),
ChunkSize: gridfs.DefaultChunkSize,
ChunkSize: mongo.DefaultGridFSChunkSize,
UploadDate: uploadTime,
Name: fileName,
Metadata: rawMetadata,
Expand All @@ -332,7 +331,7 @@ func TestGridFS(x *testing.T) {
// Test that the chunk size for a file download is determined by the chunkSize field in the files
// collection document, not the bucket's chunk size.

bucket, err := gridfs.NewBucket(mt.DB)
bucket, err := mt.DB.GridFSBucket()
assert.Nil(mt, err, "NewBucket error: %v", err)
defer func() { _ = bucket.Drop(context.Background()) }()

Expand Down Expand Up @@ -363,12 +362,13 @@ func TestGridFS(x *testing.T) {
_, err := mt.DB.Collection("fs.files").InsertOne(context.Background(), filesDoc)
assert.Nil(mt, err, "InsertOne error for files collection: %v", err)

bucket, err := gridfs.NewBucket(mt.DB)
bucket, err := mt.DB.GridFSBucket()
assert.Nil(mt, err, "NewBucket error: %v", err)
defer func() { _ = bucket.Drop(context.Background()) }()

_, err = bucket.OpenDownloadStream(context.Background(), oid)
assert.Equal(mt, gridfs.ErrMissingChunkSize, err, "expected error %v, got %v", gridfs.ErrMissingChunkSize, err)
assert.Equal(mt, mongo.ErrMissingGridFSChunkSize, err,
"expected error %v, got %v", mongo.ErrMissingGridFSChunkSize, err)
})
mt.Run("cursor error during read after downloading", func(mt *mtest.T) {
// To simulate a cursor error we upload a file larger than the 16MB default batch size,
Expand All @@ -378,7 +378,7 @@ func TestGridFS(x *testing.T) {
fileName := "read-error-test"
fileData := make([]byte, 17000000)

bucket, err := gridfs.NewBucket(mt.DB)
bucket, err := mt.DB.GridFSBucket()
assert.Nil(mt, err, "NewBucket error: %v", err)
defer func() { _ = bucket.Drop(context.Background()) }()

Expand Down Expand Up @@ -406,7 +406,7 @@ func TestGridFS(x *testing.T) {
fileName := "skip-error-test"
fileData := make([]byte, 17000000)

bucket, err := gridfs.NewBucket(mt.DB)
bucket, err := mt.DB.GridFSBucket()
assert.Nil(mt, err, "NewBucket error: %v", err)
defer func() { _ = bucket.Drop(context.Background()) }()

Expand Down Expand Up @@ -446,7 +446,7 @@ func TestGridFS(x *testing.T) {
if tc.bucketName != "" {
bucketOpts.SetName(tc.bucketName)
}
bucket, err := gridfs.NewBucket(mt.DB, bucketOpts)
bucket, err := mt.DB.GridFSBucket(bucketOpts)
assert.Nil(mt, err, "NewBucket error: %v", err)
defer func() { _ = bucket.Drop(context.Background()) }()

Expand Down Expand Up @@ -491,7 +491,7 @@ func TestGridFS(x *testing.T) {
chunkSize = &temp
}

bucket, err := gridfs.NewBucket(mt.DB, &options.BucketOptions{
bucket, err := mt.DB.GridFSBucket(&options.BucketOptions{
ChunkSizeBytes: chunkSize,
})
assert.Nil(mt, err, "NewBucket error: %v", err)
Expand Down Expand Up @@ -531,7 +531,7 @@ func TestGridFS(x *testing.T) {

// Regression test for a bug introduced in GODRIVER-2346.
mt.Run("Find", func(mt *mtest.T) {
bucket, err := gridfs.NewBucket(mt.DB)
bucket, err := mt.DB.GridFSBucket()
assert.Nil(mt, err, "NewBucket error: %v", err)
// Find the file back.
cursor, err := bucket.Find(context.Background(), bson.D{{"foo", "bar"}})
Expand Down
9 changes: 4 additions & 5 deletions internal/integration/unified/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/gridfs"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)
Expand Down Expand Up @@ -193,7 +192,7 @@ type EntityMap struct {
dbEntites map[string]*mongo.Database
collEntities map[string]*mongo.Collection
sessions map[string]mongo.Session
gridfsBuckets map[string]*gridfs.Bucket
gridfsBuckets map[string]*mongo.GridFSBucket
bsonValues map[string]bson.RawValue
eventListEntities map[string][]bson.Raw
bsonArrayEntities map[string][]bson.Raw // for storing errors and failures from a loop operation
Expand All @@ -220,7 +219,7 @@ func (em *EntityMap) setClosed(val bool) {
func newEntityMap() *EntityMap {
em := &EntityMap{
allEntities: make(map[string]struct{}),
gridfsBuckets: make(map[string]*gridfs.Bucket),
gridfsBuckets: make(map[string]*mongo.GridFSBucket),
bsonValues: make(map[string]bson.RawValue),
cursorEntities: make(map[string]cursor),
clientEntities: make(map[string]*clientEntity),
Expand Down Expand Up @@ -367,7 +366,7 @@ func (em *EntityMap) addEntity(ctx context.Context, entityType string, entityOpt
return nil
}

func (em *EntityMap) gridFSBucket(id string) (*gridfs.Bucket, error) {
func (em *EntityMap) gridFSBucket(id string) (*mongo.GridFSBucket, error) {
bucket, ok := em.gridfsBuckets[id]
if !ok {
return nil, newEntityNotFoundError("gridfs bucket", id)
Expand Down Expand Up @@ -797,7 +796,7 @@ func (em *EntityMap) addGridFSBucketEntity(entityOptions *entityOptions) error {
bucketOpts = entityOptions.GridFSBucketOptions.BucketOptions
}

bucket, err := gridfs.NewBucket(db, bucketOpts)
bucket, err := db.GridFSBucket(bucketOpts)
if err != nil {
return fmt.Errorf("error creating GridFS bucket: %v", err)
}
Expand Down
9 changes: 4 additions & 5 deletions internal/integration/unified_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"go.mongodb.org/mongo-driver/internal/integtest"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/address"
"go.mongodb.org/mongo-driver/mongo/gridfs"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readconcern"
"go.mongodb.org/mongo-driver/mongo/readpref"
Expand Down Expand Up @@ -116,7 +115,7 @@ type testCase struct {

// set in code if the test is a GridFS test
chunkSize int32
bucket *gridfs.Bucket
bucket *mongo.GridFSBucket

// set in code to track test context
testTopology *topology.Topology
Expand Down Expand Up @@ -360,12 +359,12 @@ func createBucket(mt *mtest.T, testFile testFile, testCase *testCase) {
}
chunkSize := testCase.chunkSize
if chunkSize == 0 {
chunkSize = gridfs.DefaultChunkSize
chunkSize = mongo.DefaultGridFSChunkSize
}
bucketOpts.SetChunkSizeBytes(chunkSize)

var err error
testCase.bucket, err = gridfs.NewBucket(mt.DB, bucketOpts)
testCase.bucket, err = mt.DB.GridFSBucket(bucketOpts)
assert.Nil(mt, err, "NewBucket error: %v", err)
}

Expand Down Expand Up @@ -428,7 +427,7 @@ func runOperation(mt *mtest.T, testCase *testCase, op *operation, sess0, sess1 m
return verifyError(op.opError, err)
}

func executeGridFSOperation(mt *mtest.T, bucket *gridfs.Bucket, op *operation) error {
func executeGridFSOperation(mt *mtest.T, bucket *mongo.GridFSBucket, op *operation) error {
// no results for GridFS operations
assert.Nil(mt, op.Result, "unexpected result for GridFS operation")

Expand Down
5 changes: 0 additions & 5 deletions mongo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -860,11 +860,6 @@ func (c *Client) NumberSessionsInProgress() int {
return int(c.sessionPool.CheckedOut())
}

// Timeout returns the timeout set for this client.
func (c *Client) Timeout() *time.Duration {
return c.timeout
}

func (c *Client) createBaseCursorOptions() driver.CursorOptions {
return driver.CursorOptions{
CommandMonitor: c.monitor,
Expand Down
71 changes: 56 additions & 15 deletions mongo/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,21 +544,6 @@ func (db *Database) ListCollectionNames(ctx context.Context, filter interface{},
return names, nil
}

// ReadConcern returns the read concern used to configure the Database object.
func (db *Database) ReadConcern() *readconcern.ReadConcern {
return db.readConcern
}

// ReadPreference returns the read preference used to configure the Database object.
func (db *Database) ReadPreference() *readpref.ReadPref {
return db.readPreference
}

// WriteConcern returns the write concern used to configure the Database object.
func (db *Database) WriteConcern() *writeconcern.WriteConcern {
return db.writeConcern
}

// Watch returns a change stream for all changes to the corresponding database. See
// https://www.mongodb.com/docs/manual/changeStreams/ for more information about change streams.
//
Expand Down Expand Up @@ -982,3 +967,59 @@ func (db *Database) executeCreateOperation(ctx context.Context, op *operation.Cr

return replaceErrors(op.Execute(ctx))
}

// GridFSBucket is used to construct a GridFS bucket which can be used as a
// container for files.
func (db *Database) GridFSBucket(opts ...*options.BucketOptions) (*GridFSBucket, error) {
b := &GridFSBucket{
name: "fs",
chunkSize: DefaultGridFSChunkSize,
db: db,
}

bo := options.GridFSBucket()
for _, opt := range opts {
if opt == nil {
continue
}
if opt.Name != nil {
bo.Name = opt.Name
}
if opt.ChunkSizeBytes != nil {
bo.ChunkSizeBytes = opt.ChunkSizeBytes
}
if opt.WriteConcern != nil {
bo.WriteConcern = opt.WriteConcern
}
if opt.ReadConcern != nil {
bo.ReadConcern = opt.ReadConcern
}
if opt.ReadPreference != nil {
bo.ReadPreference = opt.ReadPreference
}
}
if bo.Name != nil {
b.name = *bo.Name
}
if bo.ChunkSizeBytes != nil {
b.chunkSize = *bo.ChunkSizeBytes
}
if bo.WriteConcern != nil {
b.wc = bo.WriteConcern
}
if bo.ReadConcern != nil {
b.rc = bo.ReadConcern
}
if bo.ReadPreference != nil {
b.rp = bo.ReadPreference
}

var collOpts = options.Collection().SetWriteConcern(b.wc).SetReadConcern(b.rc).SetReadPreference(b.rp)

b.chunksColl = db.Collection(b.name+".chunks", collOpts)
b.filesColl = db.Collection(b.name+".files", collOpts)
b.readBuf = make([]byte, b.chunkSize)
b.writeBuf = make([]byte, b.chunkSize)

return b, nil
}
37 changes: 0 additions & 37 deletions mongo/gridfs/doc.go

This file was deleted.

Loading

0 comments on commit 8acba08

Please sign in to comment.