feat: replace NewBetchInserter function name (#2769)

master
MarkJoyMa 2 years ago committed by GitHub
parent 0c786ca849
commit 0defb7522f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -25,25 +25,15 @@ type (
} }
) )
// Deprecated. Use NewBatchInserter instead. // NewBulkInserter returns a BulkInserter.
func NewBulkInserter(coll *mongo.Collection, interval ...time.Duration) *BulkInserter { func NewBulkInserter(coll Collection, interval ...time.Duration) (*BulkInserter, error) {
return newBulkInserter(coll, interval...)
}
// NewBatchInserter returns a BulkInserter.
func NewBatchInserter(coll Collection, interval ...time.Duration) (*BulkInserter, error) {
cloneColl, err := coll.Clone() cloneColl, err := coll.Clone()
if err != nil { if err != nil {
return nil, err return nil, err
} }
return newBulkInserter(cloneColl, interval...), nil
}
// newBulkInserter returns a BulkInserter.
func newBulkInserter(coll *mongo.Collection, interval ...time.Duration) *BulkInserter {
inserter := &dbInserter{ inserter := &dbInserter{
collection: coll, collection: cloneColl,
} }
duration := flushInterval duration := flushInterval
@ -54,7 +44,7 @@ func newBulkInserter(coll *mongo.Collection, interval ...time.Duration) *BulkIns
return &BulkInserter{ return &BulkInserter{
executor: executors.NewPeriodicalExecutor(duration, inserter), executor: executors.NewPeriodicalExecutor(duration, inserter),
inserter: inserter, inserter: inserter,
} }, nil
} }
// Flush flushes the inserter, writes all pending records. // Flush flushes the inserter, writes all pending records.

@ -15,24 +15,7 @@ func TestBulkInserter(t *testing.T) {
mt.Run("test", func(mt *mtest.T) { mt.Run("test", func(mt *mtest.T) {
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "ok", Value: 1}}...)) mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "ok", Value: 1}}...))
bulk := NewBulkInserter(mt.Coll) bulk, err := NewBulkInserter(createModel(mt).Collection)
bulk.SetResultHandler(func(result *mongo.InsertManyResult, err error) {
assert.Nil(t, err)
assert.Equal(t, 2, len(result.InsertedIDs))
})
bulk.Insert(bson.D{{Key: "foo", Value: "bar"}})
bulk.Insert(bson.D{{Key: "foo", Value: "baz"}})
bulk.Flush()
})
}
func TestBatchInserter(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("test", func(mt *mtest.T) {
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "ok", Value: 1}}...))
bulk, err := NewBatchInserter(createModel(mt).Collection)
assert.Equal(t, err, nil) assert.Equal(t, err, nil)
bulk.SetResultHandler(func(result *mongo.InsertManyResult, err error) { bulk.SetResultHandler(func(result *mongo.InsertManyResult, err error) {
assert.Nil(t, err) assert.Nil(t, err)

Loading…
Cancel
Save