@@ -6,6 +6,7 @@ package mongo
66import (
77 "bytes"
88 "context"
9+ "errors"
910 "fmt"
1011 "time"
1112
@@ -38,18 +39,18 @@ func (db *DB) Open(uri string, opt ...database.Option) error {
3839
3940 clientOptions := options .Client ().ApplyURI (uri )
4041 if rs := clientOptions .ReplicaSet ; * rs == "" {
41- return fmt . Errorf ("replica set name is required to enable transactions" )
42+ return errors . New ("replica set name is required to enable transactions" )
4243 }
4344
4445 ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
4546 defer cancel ()
4647
4748 client , err := mongo .Connect (ctx , clientOptions )
4849 if err != nil {
49- return fmt .Errorf ("failed to invalid options %v: %w" , clientOptions , err )
50+ return fmt .Errorf ("failed creating client to invalid options %v: %w" , clientOptions , err )
5051 }
5152
52- if err = client .Ping (context .Background (), nil ); err != nil {
53+ if err : = client .Ping (context .Background (), nil ); err != nil {
5354 return fmt .Errorf ("failed connecting to MongoDB: %w" , err )
5455 }
5556
@@ -61,13 +62,13 @@ func (db *DB) Open(uri string, opt ...database.Option) error {
6162
6263func (db * DB ) Close () error {
6364 if err := db .db .Client ().Disconnect (context .Background ()); err != nil {
64- return fmt .Errorf ("failed disconnecting mongo to : %w" , err )
65+ return fmt .Errorf ("failed disconnecting from MongoDB : %w" , err )
6566 }
6667
6768 return nil
6869}
6970
70- // CreateTable creates a collection or an embedded collection if it does not exists .
71+ // CreateTable creates a collection or an embedded collection if it does not exist .
7172func (db * DB ) CreateTable (collection []byte ) error {
7273 return db .createTable (context .Background (), collection )
7374}
@@ -78,12 +79,12 @@ func (db *DB) DeleteTable(collection []byte) error {
7879 return db .deleteTable (context .Background (), collection )
7980}
8081
81- // Get returns the value stored in the given bucked and key.
82+ // Get returns the value stored in the given collection and key.
8283func (db * DB ) Get (collection , key []byte ) (ret []byte , err error ) {
8384 return db .get (context .Background (), collection , key )
8485}
8586
86- // Set stores the given value on collection and key.
87+ // Set stores the given value in collection and key.
8788func (db * DB ) Set (collection , key , value []byte ) error {
8889 return db .set (context .Background (), collection , key , value )
8990}
@@ -93,6 +94,7 @@ func (db *DB) Del(collection, key []byte) error {
9394 return db .del (context .Background (), collection , key )
9495}
9596
97+ // List returns the full list of entries in a collection.
9698func (db * DB ) List (collection []byte ) ([]* database.Entry , error ) {
9799 return db .list (context .Background (), collection )
98100}
@@ -111,19 +113,19 @@ func (db *DB) CmpAndSwap(collection, key, oldValue, newValue []byte) ([]byte, bo
111113
112114 val , swapped := []byte {}, false
113115 err = mongo .WithSession (context .Background (), session , func (ctx mongo.SessionContext ) error {
114- if err = session .StartTransaction (txnOptions ); err != nil {
115- return fmt .Errorf ("failed to pending transaction: %w" , err )
116+ if err : = session .StartTransaction (txnOptions ); err != nil {
117+ return fmt .Errorf ("failed starting transaction to pending transaction: %w" , err )
116118 }
117119
118120 val , swapped , err = db .cmpAndSwap (ctx , collection , key , oldValue , newValue )
119121 if err != nil {
120- if err = session .AbortTransaction (ctx ); err != nil {
122+ if err : = session .AbortTransaction (ctx ); err != nil {
121123 return fmt .Errorf ("failed to execute CmpAndSwap transaction on %s/%s and failed to rollback transaction: %w" , collection , key , err )
122124 }
123125 return fmt .Errorf ("failed aborting transaction: %w" , err )
124126 }
125127
126- if err = session .CommitTransaction (ctx ); err != nil {
128+ if err : = session .CommitTransaction (ctx ); err != nil {
127129 return fmt .Errorf ("failed committing transaction: %w" , err )
128130 }
129131 return nil
@@ -144,16 +146,15 @@ func (db *DB) Update(tx *database.Tx) error {
144146 defer session .EndSession (context .Background ())
145147
146148 err = mongo .WithSession (context .Background (), session , func (ctx mongo.SessionContext ) error {
147- if err = session .StartTransaction (txnOptions ); err != nil {
148- return fmt .Errorf ("failed to pending transaction: %w" , err )
149+ if err : = session .StartTransaction (txnOptions ); err != nil {
150+ return fmt .Errorf ("failed starting transaction to pending transaction: %w" , err )
149151 }
150152
151- err = db .executeTransactions (ctx , tx , session )
152- if err != nil {
153+ if err := db .executeTransactions (ctx , tx , session ); err != nil {
153154 return err
154155 }
155156
156- if err = session .CommitTransaction (ctx ); err != nil {
157+ if err : = session .CommitTransaction (ctx ); err != nil {
157158 return fmt .Errorf ("failed committing transaction: %w" , err )
158159 }
159160 return nil
@@ -177,8 +178,7 @@ func (db *DB) createTable(ctx context.Context, collection []byte) error {
177178 Options : options .Index ().SetUnique (true ),
178179 }
179180
180- _ , err := db .db .Collection (string (collection )).Indexes ().CreateOne (ctx , index )
181- if err != nil {
181+ if _ , err := db .db .Collection (string (collection )).Indexes ().CreateOne (ctx , index ); err != nil {
182182 return fmt .Errorf ("failed creating collection %q: %w" , collection , err )
183183 }
184184
@@ -191,7 +191,7 @@ func (db *DB) deleteTable(ctx context.Context, collection []byte) error {
191191 }
192192
193193 if err := db .db .Collection (string (collection )).Drop (ctx ); err != nil {
194- return fmt .Errorf ("failed dropping collection %q: %w" , collection , err )
194+ return fmt .Errorf ("failed deleting collection %q: %w" , collection , err )
195195 }
196196 return nil
197197}
@@ -217,14 +217,12 @@ func (db *DB) set(ctx context.Context, collection, key, value []byte) error {
217217 update := createUpdate (value )
218218 opts := options .Update ().SetUpsert (true )
219219
220- _ , err := db .db .Collection (string (collection )).UpdateOne (ctx , filter , update , opts )
221- if err != nil {
220+ if _ , err := db .db .Collection (string (collection )).UpdateOne (ctx , filter , update , opts ); err != nil {
222221 return fmt .Errorf ("failed setting value %s/%s: %w" , collection , key , err )
223222 }
224223 return nil
225224}
226225
227- // List returns the full list of entries in a collection.
228226func (db * DB ) list (ctx context.Context , collection []byte ) ([]* database.Entry , error ) {
229227 if ! collectionExists (ctx , db .db , string (collection )) {
230228 return nil , fmt .Errorf ("failed finding collection %q: %w" , collection , database .ErrNotFound )
@@ -235,11 +233,11 @@ func (db *DB) list(ctx context.Context, collection []byte) ([]*database.Entry, e
235233
236234 cursor , err := db .db .Collection (string (collection )).Find (ctx , filter )
237235 if err != nil {
238- return nil , fmt .Errorf ("failed listing values of collection %s to : %w" , collection , err )
236+ return nil , fmt .Errorf ("failed listing values of collection %s: %w" , collection , err )
239237 }
240238 defer cursor .Close (ctx )
241239
242- if err = cursor .Err (); err != nil {
240+ if err : = cursor .Err (); err != nil {
243241 return nil , fmt .Errorf ("failed listing values of %q: %w" , collection , err )
244242 }
245243
@@ -259,14 +257,13 @@ func (db *DB) list(ctx context.Context, collection []byte) ([]*database.Entry, e
259257 })
260258 }
261259
262- if err = cursor .Err (); err != nil {
260+ if err : = cursor .Err (); err != nil {
263261 return nil , fmt .Errorf ("failed listing values of collection %q: %w" , collection , err )
264262 }
265263
266264 return entries , nil
267265}
268266
269- // Del deletes the value stored in the given collection and key.
270267func (db * DB ) del (ctx context.Context , collection , key []byte ) error {
271268 filter := createFilter ("key" , key )
272269
@@ -276,7 +273,7 @@ func (db *DB) del(ctx context.Context, collection, key []byte) error {
276273 }
277274
278275 if mongoRes .DeletedCount == 0 {
279- return fmt .Errorf ("failed to delete: %s/%s: %w" , collection , key , database .ErrNotFound )
276+ return fmt .Errorf ("failed deleting %s/%s: %w" , collection , key , database .ErrNotFound )
280277 }
281278
282279 return nil
@@ -292,8 +289,7 @@ func (db *DB) cmpAndSwap(ctx context.Context, collection, key, target, newValue
292289 return v , false , nil
293290 }
294291
295- err = db .set (ctx , collection , key , newValue )
296- if err != nil {
292+ if err := db .set (ctx , collection , key , newValue ); err != nil {
297293 return nil , false , err
298294 }
299295
@@ -306,28 +302,28 @@ func (db *DB) executeTransactions(ctx mongo.SessionContext, tx *database.Tx, ses
306302 switch op .Cmd {
307303 case database .CreateTable :
308304 if err := db .CreateTable (op .Bucket ); err != nil {
309- return abort (ctx , session , err )
305+ return abort (ctx , session , fmt . Errorf ( "failed creating table %s: %w" , op . Bucket , err ) )
310306 }
311307 case database .DeleteTable :
312308 if err := db .DeleteTable (op .Bucket ); err != nil {
313- return abort (ctx , session , err )
309+ return abort (ctx , session , fmt . Errorf ( "failed deleting table %s: %w" , op . Bucket , err ) )
314310 }
315311 case database .Get :
316312 if op .Result , err = db .get (ctx , op .Bucket , op .Key ); err != nil {
317- return abort (ctx , session , err )
313+ return abort (ctx , session , fmt . Errorf ( "failed getting %s/%s: %w" , op . Bucket , op . Key , err ) )
318314 }
319315 case database .Set :
320316 if err := db .set (ctx , op .Bucket , op .Key , op .Value ); err != nil {
321- return abort (ctx , session , err )
317+ return abort (ctx , session , fmt . Errorf ( "failed setting %s/%s: %w" , op . Bucket , op . Key , err ) )
322318 }
323319 case database .Delete :
324320 if err := db .del (ctx , op .Bucket , op .Key ); err != nil {
325- return abort (ctx , session , err )
321+ return abort (ctx , session , fmt . Errorf ( "failed deleting %s/%s: %w" , op . Bucket , op . Key , err ) )
326322 }
327323 case database .CmpAndSwap :
328324 op .Result , op .Swapped , err = db .cmpAndSwap (ctx , op .Bucket , op .Key , op .CmpValue , op .Value )
329325 if err != nil {
330- return abort (ctx , session , err )
326+ return abort (ctx , session , fmt . Errorf ( "failed load-or-store on %s/%s: %w" , op . Bucket , op . Key , err ) )
331327 }
332328 case database .CmpOrRollback :
333329 return abort (ctx , session , database .ErrOpNotSupported )
@@ -359,7 +355,7 @@ func createUpdate(value []byte) bson.D {
359355func abort (ctx context.Context , session mongo.Session , err error ) error {
360356 abortError := session .AbortTransaction (ctx )
361357 if abortError != nil {
362- return fmt .Errorf ("failed aborting transaction due to %q : %w" , abortError , err )
358+ return fmt .Errorf ("failed aborting transaction due to %w : %w" , abortError , err )
363359 }
364360 return fmt .Errorf ("failed executing transaction: %w" , err )
365361}
0 commit comments