@@ -135,9 +135,10 @@ func (c *Collection) Actions() *ActionList {
135
135
// document; a Get after the write will see the new value if the service is strongly
136
136
// consistent, but may see the old value if the service is eventually consistent.
137
137
type ActionList struct {
138
- coll * Collection
139
- actions []* Action
140
- beforeDo func (asFunc func (interface {}) bool ) error
138
+ coll * Collection
139
+ actions []* Action
140
+ atomicWrites bool
141
+ beforeDo func (asFunc func (interface {}) bool ) error
141
142
}
142
143
143
144
// An Action is a read or write on a single document.
@@ -171,11 +172,7 @@ func (l *ActionList) add(a *Action) *ActionList {
171
172
// Except for setting the revision field and possibly setting the key fields, the doc
172
173
// argument is not modified.
173
174
func (l * ActionList ) Create (doc Document ) * ActionList {
174
- return l .add (& Action {kind : driver .Create , doc : doc })
175
- }
176
-
177
- func (l * ActionList ) CreateTx (doc Document ) * ActionList {
178
- return l .add (& Action {kind : driver .Create , doc : doc , transaction : true })
175
+ return l .add (& Action {kind : driver .Create , doc : doc , transaction : l .atomicWrites })
179
176
}
180
177
181
178
// Replace adds an action that replaces a document to the given ActionList, and
@@ -187,11 +184,7 @@ func (l *ActionList) CreateTx(doc Document) *ActionList {
187
184
// See the Revisions section of the package documentation for how revisions are
188
185
// handled.
189
186
func (l * ActionList ) Replace (doc Document ) * ActionList {
190
- return l .add (& Action {kind : driver .Replace , doc : doc })
191
- }
192
-
193
- func (l * ActionList ) ReplaceTx (doc Document ) * ActionList {
194
- return l .add (& Action {kind : driver .Replace , doc : doc , transaction : true })
187
+ return l .add (& Action {kind : driver .Replace , doc : doc , transaction : l .atomicWrites })
195
188
}
196
189
197
190
// Put adds an action that adds or replaces a document to the given ActionList, and returns the ActionList.
@@ -204,11 +197,7 @@ func (l *ActionList) ReplaceTx(doc Document) *ActionList {
204
197
// See the Revisions section of the package documentation for how revisions are
205
198
// handled.
206
199
func (l * ActionList ) Put (doc Document ) * ActionList {
207
- return l .add (& Action {kind : driver .Put , doc : doc })
208
- }
209
-
210
- func (l * ActionList ) PutTx (doc Document ) * ActionList {
211
- return l .add (& Action {kind : driver .Put , doc : doc , transaction : true })
200
+ return l .add (& Action {kind : driver .Put , doc : doc , transaction : l .atomicWrites })
212
201
}
213
202
214
203
// Delete adds an action that deletes a document to the given ActionList, and returns
@@ -223,11 +212,7 @@ func (l *ActionList) Delete(doc Document) *ActionList {
223
212
// semantics of an action list are to stop at first error, then we might abort a
224
213
// list of Deletes just because one of the docs was not present, and that seems
225
214
// wrong, or at least something you'd want to turn off.
226
- return l .add (& Action {kind : driver .Delete , doc : doc })
227
- }
228
-
229
- func (l * ActionList ) DeleteTx (doc Document ) * ActionList {
230
- return l .add (& Action {kind : driver .Delete , doc : doc , transaction : true })
215
+ return l .add (& Action {kind : driver .Delete , doc : doc , transaction : l .atomicWrites })
231
216
}
232
217
233
218
// Get adds an action that retrieves a document to the given ActionList, and
@@ -268,19 +253,11 @@ func (l *ActionList) Get(doc Document, fps ...FieldPath) *ActionList {
268
253
// Update does not modify its doc argument, except to set the new revision. To obtain
269
254
// the updated document, call Get after calling Update.
270
255
func (l * ActionList ) Update (doc Document , mods Mods ) * ActionList {
271
- return l .add (& Action {
272
- kind : driver .Update ,
273
- doc : doc ,
274
- mods : mods ,
275
- })
276
- }
277
-
278
- func (l * ActionList ) UpdateTx (doc Document , mods Mods ) * ActionList {
279
256
return l .add (& Action {
280
257
kind : driver .Update ,
281
258
doc : doc ,
282
259
mods : mods ,
283
- transaction : true ,
260
+ transaction : l . atomicWrites ,
284
261
})
285
262
}
286
263
@@ -456,7 +433,7 @@ func (c *Collection) toDriverAction(a *Action) (*driver.Action, error) {
456
433
// A Put with a revision field is equivalent to a Replace.
457
434
kind = driver .Replace
458
435
}
459
- d := & driver.Action {Kind : kind , Doc : ddoc , Key : key , Transaction : a .transaction }
436
+ d := & driver.Action {Kind : kind , Doc : ddoc , Key : key , InAtomicWrite : a .transaction }
460
437
if a .fieldpaths != nil {
461
438
d .FieldPaths , err = parseFieldPaths (a .fieldpaths )
462
439
if err != nil {
@@ -560,6 +537,12 @@ func (l *ActionList) String() string {
560
537
return "[" + strings .Join (as , ", " ) + "]"
561
538
}
562
539
540
+ // AtomicWrites causes all following writes in the list to execute atomically.
541
+ func (l * ActionList ) AtomicWrites () * ActionList {
542
+ l .atomicWrites = true
543
+ return l
544
+ }
545
+
563
546
func (a * Action ) String () string {
564
547
buf := & strings.Builder {}
565
548
fmt .Fprintf (buf , "%s(%v" , a .kind , a .doc )
0 commit comments