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.
func NewBulkInserter(coll *mongo.Collection, interval ...time.Duration) *BulkInserter {
return newBulkInserter(coll, interval...)
}
// NewBatchInserter returns a BulkInserter.
func NewBatchInserter(coll Collection, interval ...time.Duration) (*BulkInserter, error) {
// NewBulkInserter returns a BulkInserter.
func NewBulkInserter(coll Collection, interval ...time.Duration) (*BulkInserter, error) {
cloneColl, err := coll.Clone()
if err != nil {
return nil, err
}
return newBulkInserter(cloneColl, interval...), nil
}
// newBulkInserter returns a BulkInserter.
func newBulkInserter(coll *mongo.Collection, interval ...time.Duration) *BulkInserter {
inserter := &dbInserter{
collection: coll,
collection: cloneColl,
}
duration := flushInterval
@ -54,7 +44,7 @@ func newBulkInserter(coll *mongo.Collection, interval ...time.Duration) *BulkIns
return &BulkInserter{
executor: executors.NewPeriodicalExecutor(duration, inserter),
inserter: inserter,
}
}, nil
}
// 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.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "ok", Value: 1}}...))
bulk := NewBulkInserter(mt.Coll)
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)
bulk, err := NewBulkInserter(createModel(mt).Collection)
assert.Equal(t, err, nil)
bulk.SetResultHandler(func(result *mongo.InsertManyResult, err error) {
assert.Nil(t, err)

Loading…
Cancel
Save