Skip to content

Commit 1ebbc6f

Browse files
authored
chore: refactor mon/monc (#5073)
1 parent b41b1b0 commit 1ebbc6f

File tree

9 files changed

+172
-162
lines changed

9 files changed

+172
-162
lines changed

core/stores/mon/bulkinserter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:generate mockgen -package mon -destination collection_inserter_mock.go -source bulkinserter.go collectionInserter
1+
//go:generate mockgen -package mon -destination collectioninserter_mock.go -source bulkinserter.go collectionInserter
22
package mon
33

44
import (

core/stores/mon/collection.go

Lines changed: 83 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,6 @@ func newCollection(collection *mongo.Collection, brk breaker.Breaker) Collection
137137
}
138138
}
139139

140-
func newTestCollection(collection monCollection, brk breaker.Breaker) *decoratedCollection {
141-
return &decoratedCollection{
142-
Collection: collection,
143-
name: "test",
144-
brk: brk,
145-
}
146-
}
147-
148140
func (c *decoratedCollection) Aggregate(ctx context.Context, pipeline any,
149141
opts ...options.Lister[options.AggregateOptions]) (cur *mongo.Cursor, err error) {
150142
ctx, span := startSpan(ctx, aggregate)
@@ -185,6 +177,10 @@ func (c *decoratedCollection) BulkWrite(ctx context.Context, models []mongo.Writ
185177
return
186178
}
187179

180+
func (c *decoratedCollection) Clone(opts ...options.Lister[options.CollectionOptions]) *mongo.Collection {
181+
return c.Collection.Clone(opts...)
182+
}
183+
188184
func (c *decoratedCollection) CountDocuments(ctx context.Context, filter any,
189185
opts ...options.Lister[options.CountOptions]) (count int64, err error) {
190186
ctx, span := startSpan(ctx, countDocuments)
@@ -205,6 +201,10 @@ func (c *decoratedCollection) CountDocuments(ctx context.Context, filter any,
205201
return
206202
}
207203

204+
func (c *decoratedCollection) Database() *mongo.Database {
205+
return c.Collection.Database()
206+
}
207+
208208
func (c *decoratedCollection) DeleteMany(ctx context.Context, filter any,
209209
opts ...options.Lister[options.DeleteManyOptions]) (res *mongo.DeleteResult, err error) {
210210
ctx, span := startSpan(ctx, deleteMany)
@@ -266,6 +266,10 @@ func (c *decoratedCollection) Distinct(ctx context.Context, fieldName string, fi
266266
return
267267
}
268268

269+
func (c *decoratedCollection) Drop(ctx context.Context, opts ...options.Lister[options.DropCollectionOptions]) error {
270+
return c.Collection.Drop(ctx, opts...)
271+
}
272+
269273
func (c *decoratedCollection) EstimatedDocumentCount(ctx context.Context,
270274
opts ...options.Lister[options.EstimatedDocumentCountOptions]) (val int64, err error) {
271275
ctx, span := startSpan(ctx, estimatedDocumentCount)
@@ -391,6 +395,10 @@ func (c *decoratedCollection) FindOneAndUpdate(ctx context.Context, filter, upda
391395
return
392396
}
393397

398+
func (c *decoratedCollection) Indexes() mongo.IndexView {
399+
return c.Collection.Indexes()
400+
}
401+
394402
func (c *decoratedCollection) InsertMany(ctx context.Context, documents []any,
395403
opts ...options.Lister[options.InsertManyOptions]) (res *mongo.InsertManyResult, err error) {
396404
ctx, span := startSpan(ctx, insertMany)
@@ -511,22 +519,6 @@ func (c *decoratedCollection) UpdateOne(ctx context.Context, filter, update any,
511519
return
512520
}
513521

514-
func (c *decoratedCollection) Clone(opts ...options.Lister[options.CollectionOptions]) *mongo.Collection {
515-
return c.Collection.Clone(opts...)
516-
}
517-
518-
func (c *decoratedCollection) Database() *mongo.Database {
519-
return c.Collection.Database()
520-
}
521-
522-
func (c *decoratedCollection) Drop(ctx context.Context, opts ...options.Lister[options.DropCollectionOptions]) error {
523-
return c.Collection.Drop(ctx, opts...)
524-
}
525-
526-
func (c *decoratedCollection) Indexes() mongo.IndexView {
527-
return c.Collection.Indexes()
528-
}
529-
530522
func (c *decoratedCollection) Watch(ctx context.Context, pipeline any, opts ...options.Lister[options.ChangeStreamOptions]) (
531523
*mongo.ChangeStream, error) {
532524
return c.Collection.Watch(ctx, pipeline, opts...)
@@ -578,72 +570,70 @@ func isDupKeyError(err error) bool {
578570
return e.HasErrorCode(duplicateKeyCode)
579571
}
580572

581-
type (
582-
// monCollection defines a MongoDB collection, used for unit test
583-
monCollection interface {
584-
// Aggregate executes an aggregation pipeline.
585-
Aggregate(ctx context.Context, pipeline any, opts ...options.Lister[options.AggregateOptions]) (
586-
*mongo.Cursor, error)
587-
// BulkWrite performs a bulk write operation.
588-
BulkWrite(ctx context.Context, models []mongo.WriteModel, opts ...options.Lister[options.BulkWriteOptions]) (
589-
*mongo.BulkWriteResult, error)
590-
// Clone creates a copy of this collection with the same settings.
591-
Clone(opts ...options.Lister[options.CollectionOptions]) *mongo.Collection
592-
// CountDocuments returns the number of documents in the collection that match the filter.
593-
CountDocuments(ctx context.Context, filter any, opts ...options.Lister[options.CountOptions]) (int64, error)
594-
// Database returns the database that this collection is a part of.
595-
Database() *mongo.Database
596-
// DeleteMany deletes documents from the collection that match the filter.
597-
DeleteMany(ctx context.Context, filter any, opts ...options.Lister[options.DeleteManyOptions]) (
598-
*mongo.DeleteResult, error)
599-
// DeleteOne deletes at most one document from the collection that matches the filter.
600-
DeleteOne(ctx context.Context, filter any, opts ...options.Lister[options.DeleteOneOptions]) (
601-
*mongo.DeleteResult, error)
602-
// Distinct returns a list of distinct values for the given key across the collection.
603-
Distinct(ctx context.Context, fieldName string, filter any,
604-
opts ...options.Lister[options.DistinctOptions]) *mongo.DistinctResult
605-
// Drop drops this collection from database.
606-
Drop(ctx context.Context, opts ...options.Lister[options.DropCollectionOptions]) error
607-
// EstimatedDocumentCount returns an estimate of the count of documents in a collection
608-
// using collection metadata.
609-
EstimatedDocumentCount(ctx context.Context, opts ...options.Lister[options.EstimatedDocumentCountOptions]) (int64, error)
610-
// Find finds the documents matching the provided filter.
611-
Find(ctx context.Context, filter any, opts ...options.Lister[options.FindOptions]) (*mongo.Cursor, error)
612-
// FindOne returns up to one document that matches the provided filter.
613-
FindOne(ctx context.Context, filter any, opts ...options.Lister[options.FindOneOptions]) *mongo.SingleResult
614-
// FindOneAndDelete returns at most one document that matches the filter. If the filter
615-
// matches multiple documents, only the first document is deleted.
616-
FindOneAndDelete(ctx context.Context, filter any, opts ...options.Lister[options.FindOneAndDeleteOptions]) *mongo.SingleResult
617-
// FindOneAndReplace returns at most one document that matches the filter. If the filter
618-
// matches multiple documents, FindOneAndReplace returns the first document in the
619-
// collection that matches the filter.
620-
FindOneAndReplace(ctx context.Context, filter, replacement any,
621-
opts ...options.Lister[options.FindOneAndReplaceOptions]) *mongo.SingleResult
622-
// FindOneAndUpdate returns at most one document that matches the filter. If the filter
623-
// matches multiple documents, FindOneAndUpdate returns the first document in the
624-
// collection that matches the filter.
625-
FindOneAndUpdate(ctx context.Context, filter, update any,
626-
opts ...options.Lister[options.FindOneAndUpdateOptions]) *mongo.SingleResult
627-
// Indexes returns the index view for this collection.
628-
Indexes() mongo.IndexView
629-
// InsertMany inserts the provided documents.
630-
InsertMany(ctx context.Context, documents interface{}, opts ...options.Lister[options.InsertManyOptions]) (*mongo.InsertManyResult, error)
631-
// InsertOne inserts the provided document.
632-
InsertOne(ctx context.Context, document any, opts ...options.Lister[options.InsertOneOptions]) (*mongo.InsertOneResult, error)
633-
// ReplaceOne replaces at most one document that matches the filter.
634-
ReplaceOne(ctx context.Context, filter, replacement any,
635-
opts ...options.Lister[options.ReplaceOptions]) (*mongo.UpdateResult, error)
636-
// UpdateByID updates a single document matching the provided filter.
637-
UpdateByID(ctx context.Context, id, update any,
638-
opts ...options.Lister[options.UpdateOneOptions]) (*mongo.UpdateResult, error)
639-
// UpdateMany updates the provided documents.
640-
UpdateMany(ctx context.Context, filter, update any,
641-
opts ...options.Lister[options.UpdateManyOptions]) (*mongo.UpdateResult, error)
642-
// UpdateOne updates a single document matching the provided filter.
643-
UpdateOne(ctx context.Context, filter, update any,
644-
opts ...options.Lister[options.UpdateOneOptions]) (*mongo.UpdateResult, error)
645-
// Watch returns a change stream cursor used to receive notifications of changes to the collection.
646-
Watch(ctx context.Context, pipeline any, opts ...options.Lister[options.ChangeStreamOptions]) (
647-
*mongo.ChangeStream, error)
648-
}
649-
)
573+
// monCollection defines a MongoDB collection, used for unit test
574+
type monCollection interface {
575+
// Aggregate executes an aggregation pipeline.
576+
Aggregate(ctx context.Context, pipeline any, opts ...options.Lister[options.AggregateOptions]) (
577+
*mongo.Cursor, error)
578+
// BulkWrite performs a bulk write operation.
579+
BulkWrite(ctx context.Context, models []mongo.WriteModel, opts ...options.Lister[options.BulkWriteOptions]) (
580+
*mongo.BulkWriteResult, error)
581+
// Clone creates a copy of this collection with the same settings.
582+
Clone(opts ...options.Lister[options.CollectionOptions]) *mongo.Collection
583+
// CountDocuments returns the number of documents in the collection that match the filter.
584+
CountDocuments(ctx context.Context, filter any, opts ...options.Lister[options.CountOptions]) (int64, error)
585+
// Database returns the database that this collection is a part of.
586+
Database() *mongo.Database
587+
// DeleteMany deletes documents from the collection that match the filter.
588+
DeleteMany(ctx context.Context, filter any, opts ...options.Lister[options.DeleteManyOptions]) (
589+
*mongo.DeleteResult, error)
590+
// DeleteOne deletes at most one document from the collection that matches the filter.
591+
DeleteOne(ctx context.Context, filter any, opts ...options.Lister[options.DeleteOneOptions]) (
592+
*mongo.DeleteResult, error)
593+
// Distinct returns a list of distinct values for the given key across the collection.
594+
Distinct(ctx context.Context, fieldName string, filter any,
595+
opts ...options.Lister[options.DistinctOptions]) *mongo.DistinctResult
596+
// Drop drops this collection from database.
597+
Drop(ctx context.Context, opts ...options.Lister[options.DropCollectionOptions]) error
598+
// EstimatedDocumentCount returns an estimate of the count of documents in a collection
599+
// using collection metadata.
600+
EstimatedDocumentCount(ctx context.Context, opts ...options.Lister[options.EstimatedDocumentCountOptions]) (int64, error)
601+
// Find finds the documents matching the provided filter.
602+
Find(ctx context.Context, filter any, opts ...options.Lister[options.FindOptions]) (*mongo.Cursor, error)
603+
// FindOne returns up to one document that matches the provided filter.
604+
FindOne(ctx context.Context, filter any, opts ...options.Lister[options.FindOneOptions]) *mongo.SingleResult
605+
// FindOneAndDelete returns at most one document that matches the filter. If the filter
606+
// matches multiple documents, only the first document is deleted.
607+
FindOneAndDelete(ctx context.Context, filter any, opts ...options.Lister[options.FindOneAndDeleteOptions]) *mongo.SingleResult
608+
// FindOneAndReplace returns at most one document that matches the filter. If the filter
609+
// matches multiple documents, FindOneAndReplace returns the first document in the
610+
// collection that matches the filter.
611+
FindOneAndReplace(ctx context.Context, filter, replacement any,
612+
opts ...options.Lister[options.FindOneAndReplaceOptions]) *mongo.SingleResult
613+
// FindOneAndUpdate returns at most one document that matches the filter. If the filter
614+
// matches multiple documents, FindOneAndUpdate returns the first document in the
615+
// collection that matches the filter.
616+
FindOneAndUpdate(ctx context.Context, filter, update any,
617+
opts ...options.Lister[options.FindOneAndUpdateOptions]) *mongo.SingleResult
618+
// Indexes returns the index view for this collection.
619+
Indexes() mongo.IndexView
620+
// InsertMany inserts the provided documents.
621+
InsertMany(ctx context.Context, documents interface{}, opts ...options.Lister[options.InsertManyOptions]) (*mongo.InsertManyResult, error)
622+
// InsertOne inserts the provided document.
623+
InsertOne(ctx context.Context, document any, opts ...options.Lister[options.InsertOneOptions]) (*mongo.InsertOneResult, error)
624+
// ReplaceOne replaces at most one document that matches the filter.
625+
ReplaceOne(ctx context.Context, filter, replacement any,
626+
opts ...options.Lister[options.ReplaceOptions]) (*mongo.UpdateResult, error)
627+
// UpdateByID updates a single document matching the provided filter.
628+
UpdateByID(ctx context.Context, id, update any,
629+
opts ...options.Lister[options.UpdateOneOptions]) (*mongo.UpdateResult, error)
630+
// UpdateMany updates the provided documents.
631+
UpdateMany(ctx context.Context, filter, update any,
632+
opts ...options.Lister[options.UpdateManyOptions]) (*mongo.UpdateResult, error)
633+
// UpdateOne updates a single document matching the provided filter.
634+
UpdateOne(ctx context.Context, filter, update any,
635+
opts ...options.Lister[options.UpdateOneOptions]) (*mongo.UpdateResult, error)
636+
// Watch returns a change stream cursor used to receive notifications of changes to the collection.
637+
Watch(ctx context.Context, pipeline any, opts ...options.Lister[options.ChangeStreamOptions]) (
638+
*mongo.ChangeStream, error)
639+
}

core/stores/mon/collection_test.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/zeromicro/go-zero/core/timex"
1313
"go.mongodb.org/mongo-driver/v2/bson"
1414
"go.mongodb.org/mongo-driver/v2/mongo"
15-
mopt "go.mongodb.org/mongo-driver/v2/mongo/options"
15+
"go.mongodb.org/mongo-driver/v2/mongo/options"
1616
"go.uber.org/mock/gomock"
1717
)
1818

@@ -75,7 +75,7 @@ func TestCollection_Aggregate(t *testing.T) {
7575
mockCollection := NewMockmonCollection(ctrl)
7676
mockCollection.EXPECT().Aggregate(gomock.Any(), gomock.Any(), gomock.Any()).Return(&mongo.Cursor{}, nil)
7777
c := newTestCollection(mockCollection, breaker.GetBreaker("localhost"))
78-
_, err := c.Aggregate(context.Background(), []interface{}{}, mopt.Aggregate())
78+
_, err := c.Aggregate(context.Background(), []interface{}{}, options.Aggregate())
7979
assert.Nil(t, err)
8080
}
8181

@@ -156,10 +156,10 @@ func TestCollection_Find(t *testing.T) {
156156
mockCollection.EXPECT().Find(gomock.Any(), gomock.Any(), gomock.Any()).Return(&mongo.Cursor{}, nil)
157157
c := newTestCollection(mockCollection, breaker.GetBreaker("localhost"))
158158
filter := bson.D{{Key: "x", Value: 1}}
159-
_, err := c.Find(context.Background(), filter, mopt.Find())
159+
_, err := c.Find(context.Background(), filter, options.Find())
160160
assert.Nil(t, err)
161161
c.brk = new(dropBreaker)
162-
_, err = c.Find(context.Background(), filter, mopt.Find())
162+
_, err = c.Find(context.Background(), filter, options.Find())
163163
assert.Equal(t, errDummy, err)
164164
}
165165

@@ -185,9 +185,9 @@ func TestCollection_FindOneAndDelete(t *testing.T) {
185185
c := newTestCollection(mockCollection, breaker.GetBreaker("localhost"))
186186
filter := bson.D{}
187187
mockCollection.EXPECT().FindOneAndDelete(gomock.Any(), gomock.Any(), gomock.Any()).Return(&mongo.SingleResult{})
188-
_, err := c.FindOneAndDelete(context.Background(), filter, mopt.FindOneAndDelete())
188+
_, err := c.FindOneAndDelete(context.Background(), filter, options.FindOneAndDelete())
189189
assert.Equal(t, mongo.ErrNoDocuments, err)
190-
_, err = c.FindOneAndDelete(context.Background(), filter, mopt.FindOneAndDelete())
190+
_, err = c.FindOneAndDelete(context.Background(), filter, options.FindOneAndDelete())
191191
assert.Equal(t, mongo.ErrNoDocuments, err)
192192
c.brk = new(dropBreaker)
193193
_, err = c.FindOneAndDelete(context.Background(), bson.D{{Key: "foo", Value: "bar"}})
@@ -202,7 +202,7 @@ func TestCollection_FindOneAndReplace(t *testing.T) {
202202
c := newTestCollection(mockCollection, breaker.GetBreaker("localhost"))
203203
filter := bson.D{{Key: "x", Value: 1}}
204204
replacement := bson.D{{Key: "x", Value: 2}}
205-
opts := mopt.FindOneAndReplace().SetUpsert(true)
205+
opts := options.FindOneAndReplace().SetUpsert(true)
206206
mockCollection.EXPECT().FindOneAndReplace(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(&mongo.SingleResult{})
207207
_, err := c.FindOneAndReplace(context.Background(), filter, replacement, opts)
208208
assert.Equal(t, mongo.ErrNoDocuments, err)
@@ -222,7 +222,7 @@ func TestCollection_FindOneAndUpdate(t *testing.T) {
222222
filter := bson.D{{Key: "x", Value: 1}}
223223
update := bson.D{{Key: "$x", Value: 2}}
224224
mockCollection.EXPECT().FindOneAndUpdate(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(&mongo.SingleResult{})
225-
opts := mopt.FindOneAndUpdate().SetUpsert(true)
225+
opts := options.FindOneAndUpdate().SetUpsert(true)
226226
_, err := c.FindOneAndUpdate(context.Background(), filter, update, opts)
227227
assert.Equal(t, mongo.ErrNoDocuments, err)
228228
_, err = c.FindOneAndUpdate(context.Background(), filter, update, opts)
@@ -487,6 +487,14 @@ func TestIsDupKeyError(t *testing.T) {
487487
}
488488
}
489489

490+
func newTestCollection(collection monCollection, brk breaker.Breaker) *decoratedCollection {
491+
return &decoratedCollection{
492+
Collection: collection,
493+
name: "test",
494+
brk: brk,
495+
}
496+
}
497+
490498
type mockPromise struct {
491499
accepted bool
492500
reason string

core/stores/mon/collection_inserter_mock.go renamed to core/stores/mon/collectioninserter_mock.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)