Skip to content

Commit 35c009a

Browse files
committed
Deprecate Session.ExecuteBatch() and move "execute batch" methods to Batch type
1 parent 65e2caf commit 35c009a

File tree

7 files changed

+81
-32
lines changed

7 files changed

+81
-32
lines changed

cassandra_test.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ func TestCAS(t *testing.T) {
457457

458458
successBatch := session.Batch(LoggedBatch)
459459
successBatch.Query("INSERT INTO cas_table (title, revid, last_modified) VALUES (?, ?, ?) IF NOT EXISTS", title, revid, modified)
460-
if applied, _, err := session.ExecuteBatchCAS(successBatch, &titleCAS, &revidCAS, &modifiedCAS); err != nil {
460+
if applied, _, err := successBatch.ExecCAS(&titleCAS, &revidCAS, &modifiedCAS); err != nil {
461461
t.Fatal("insert:", err)
462462
} else if !applied {
463463
t.Fatalf("insert should have been applied: title=%v revID=%v modified=%v", titleCAS, revidCAS, modifiedCAS)
@@ -466,15 +466,15 @@ func TestCAS(t *testing.T) {
466466
successBatch = session.Batch(LoggedBatch)
467467
successBatch.Query("INSERT INTO cas_table (title, revid, last_modified) VALUES (?, ?, ?) IF NOT EXISTS", title+"_foo", revid, modified)
468468
casMap := make(map[string]interface{})
469-
if applied, _, err := session.MapExecuteBatchCAS(successBatch, casMap); err != nil {
469+
if applied, _, err := successBatch.MapExecCAS(casMap); err != nil {
470470
t.Fatal("insert:", err)
471471
} else if !applied {
472472
t.Fatal("insert should have been applied")
473473
}
474474

475475
failBatch := session.Batch(LoggedBatch)
476476
failBatch.Query("INSERT INTO cas_table (title, revid, last_modified) VALUES (?, ?, ?) IF NOT EXISTS", title, revid, modified)
477-
if applied, _, err := session.ExecuteBatchCAS(successBatch, &titleCAS, &revidCAS, &modifiedCAS); err != nil {
477+
if applied, _, err := successBatch.ExecCAS(&titleCAS, &revidCAS, &modifiedCAS); err != nil {
478478
t.Fatal("insert:", err)
479479
} else if applied {
480480
t.Fatalf("insert should have not been applied: title=%v revID=%v modified=%v", titleCAS, revidCAS, modifiedCAS)
@@ -483,14 +483,14 @@ func TestCAS(t *testing.T) {
483483
insertBatch := session.Batch(LoggedBatch)
484484
insertBatch.Query("INSERT INTO cas_table (title, revid, last_modified) VALUES ('_foo', 2c3af400-73a4-11e5-9381-29463d90c3f0, DATEOF(NOW()))")
485485
insertBatch.Query("INSERT INTO cas_table (title, revid, last_modified) VALUES ('_foo', 3e4ad2f1-73a4-11e5-9381-29463d90c3f0, DATEOF(NOW()))")
486-
if err := session.ExecuteBatch(insertBatch); err != nil {
486+
if err := insertBatch.Exec(); err != nil {
487487
t.Fatal("insert:", err)
488488
}
489489

490490
failBatch = session.Batch(LoggedBatch)
491491
failBatch.Query("UPDATE cas_table SET last_modified = DATEOF(NOW()) WHERE title='_foo' AND revid=2c3af400-73a4-11e5-9381-29463d90c3f0 IF last_modified=DATEOF(NOW());")
492492
failBatch.Query("UPDATE cas_table SET last_modified = DATEOF(NOW()) WHERE title='_foo' AND revid=3e4ad2f1-73a4-11e5-9381-29463d90c3f0 IF last_modified=DATEOF(NOW());")
493-
if applied, iter, err := session.ExecuteBatchCAS(failBatch, &titleCAS, &revidCAS, &modifiedCAS); err != nil {
493+
if applied, iter, err := failBatch.ExecCAS(&titleCAS, &revidCAS, &modifiedCAS); err != nil {
494494
t.Fatal("insert:", err)
495495
} else if applied {
496496
t.Fatalf("insert should have not been applied: title=%v revID=%v modified=%v", titleCAS, revidCAS, modifiedCAS)
@@ -521,20 +521,20 @@ func TestCAS(t *testing.T) {
521521
notCASBatch := session.Batch(LoggedBatch)
522522
notCASBatch.Query("INSERT INTO cas_table (title, revid, last_modified) VALUES (?, ?, ?)", title+"_baz", revid, modified)
523523
casMap = make(map[string]interface{})
524-
if _, _, err := session.MapExecuteBatchCAS(notCASBatch, casMap); err != ErrNotFound {
524+
if _, _, err := notCASBatch.MapExecCAS(casMap); err != ErrNotFound {
525525
t.Fatal("insert should have returned not found:", err)
526526
}
527527

528528
notCASBatch = session.Batch(LoggedBatch)
529529
notCASBatch.Query("INSERT INTO cas_table (title, revid, last_modified) VALUES (?, ?, ?)", title+"_baz", revid, modified)
530530
casMap = make(map[string]interface{})
531-
if _, _, err := session.ExecuteBatchCAS(notCASBatch, &revidCAS); err != ErrNotFound {
531+
if _, _, err := notCASBatch.ExecCAS(&revidCAS); err != ErrNotFound {
532532
t.Fatal("insert should have returned not found:", err)
533533
}
534534

535535
failBatch = session.Batch(LoggedBatch)
536536
failBatch.Query("UPDATE cas_table SET last_modified = DATEOF(NOW()) WHERE title='_foo' AND revid=3e4ad2f1-73a4-11e5-9381-29463d90c3f0 IF last_modified = ?", modified)
537-
if _, _, err := session.ExecuteBatchCAS(failBatch, new(bool)); err == nil {
537+
if _, _, err := failBatch.ExecCAS(new(bool)); err == nil {
538538
t.Fatal("update should have errored")
539539
}
540540
// make sure MapScanCAS does not panic when MapScan fails
@@ -550,7 +550,7 @@ func TestCAS(t *testing.T) {
550550
failBatch.Query("UPDATE cas_table SET last_modified = DATEOF(NOW()) WHERE title='_foo' AND revid=3e4ad2f1-73a4-11e5-9381-29463d90c3f0 IF last_modified = ?", modified)
551551
casMap = make(map[string]interface{})
552552
casMap["last_modified"] = false
553-
if _, _, err := session.MapExecuteBatchCAS(failBatch, casMap); err == nil {
553+
if _, _, err := failBatch.MapExecCAS(casMap); err == nil {
554554
t.Fatal("update should have errored")
555555
}
556556
}
@@ -666,7 +666,7 @@ func TestBatch(t *testing.T) {
666666
batch.Query(`INSERT INTO batch_table (id) VALUES (?)`, i)
667667
}
668668

669-
if err := session.ExecuteBatch(batch); err != nil {
669+
if err := batch.Exec(); err != nil {
670670
t.Fatal("execute batch:", err)
671671
}
672672

@@ -702,7 +702,7 @@ func TestUnpreparedBatch(t *testing.T) {
702702
batch.Query(`UPDATE batch_unprepared SET c = c + 1 WHERE id = 1`)
703703
}
704704

705-
if err := session.ExecuteBatch(batch); err != nil {
705+
if err := batch.Exec(); err != nil {
706706
t.Fatal("execute batch:", err)
707707
}
708708

@@ -738,8 +738,8 @@ func TestBatchLimit(t *testing.T) {
738738
for i := 0; i < 65537; i++ {
739739
batch.Query(`INSERT INTO batch_table2 (id) VALUES (?)`, i)
740740
}
741-
if err := session.ExecuteBatch(batch); err != ErrTooManyStmts {
742-
t.Fatal("gocql attempted to execute a batch larger than the support limit of statements.")
741+
if err := batch.Exec(); err != ErrTooManyStmts {
742+
t.Fatalf("gocql attempted to execute a batch larger than the support limit of statements: expected %v, got %v", ErrTooManyStmts, err)
743743
}
744744

745745
}
@@ -790,7 +790,7 @@ func TestTooManyQueryArgs(t *testing.T) {
790790

791791
batch := session.Batch(UnloggedBatch)
792792
batch.Query("INSERT INTO too_many_query_args (id, value) VALUES (?, ?)", 1, 2, 3)
793-
err = session.ExecuteBatch(batch)
793+
err = batch.Exec()
794794

795795
if err == nil {
796796
t.Fatal("'`INSERT INTO too_many_query_args (id, value) VALUES (?, ?)`, 1, 2, 3' should return an error")
@@ -822,7 +822,7 @@ func TestNotEnoughQueryArgs(t *testing.T) {
822822

823823
batch := session.Batch(UnloggedBatch)
824824
batch.Query("INSERT INTO not_enough_query_args (id, cluster, value) VALUES (?, ?, ?)", 1, 2)
825-
err = session.ExecuteBatch(batch)
825+
err = batch.Exec()
826826

827827
if err == nil {
828828
t.Fatal("'`INSERT INTO not_enough_query_args (id, cluster, value) VALUES (?, ?, ?)`, 1, 2' should return an error")
@@ -1448,7 +1448,7 @@ func TestBatchQueryInfo(t *testing.T) {
14481448
batch := session.Batch(LoggedBatch)
14491449
batch.Bind("INSERT INTO batch_query_info (id, cluster, value) VALUES (?, ?,?)", write)
14501450

1451-
if err := session.ExecuteBatch(batch); err != nil {
1451+
if err := batch.Exec(); err != nil {
14521452
t.Fatalf("batch insert into batch_query_info failed, err '%v'", err)
14531453
}
14541454

@@ -1961,7 +1961,7 @@ func TestBatchStats(t *testing.T) {
19611961
b.Query("INSERT INTO batchStats (id) VALUES (?)", 1)
19621962
b.Query("INSERT INTO batchStats (id) VALUES (?)", 2)
19631963

1964-
if err := session.ExecuteBatch(b); err != nil {
1964+
if err := b.Exec(); err != nil {
19651965
t.Fatalf("query failed. %v", err)
19661966
} else {
19671967
if b.Attempts() < 1 {
@@ -2018,7 +2018,7 @@ func TestBatchObserve(t *testing.T) {
20182018
batch.Query(fmt.Sprintf(`INSERT INTO batch_observe_table (id,other) VALUES (?,%d)`, i), i)
20192019
}
20202020

2021-
if err := session.ExecuteBatch(batch); err != nil {
2021+
if err := batch.Exec(); err != nil {
20222022
t.Fatal("execute batch:", err)
20232023
}
20242024
if observedBatch == nil {
@@ -3315,7 +3315,7 @@ func TestUnsetColBatch(t *testing.T) {
33153315
b.Query("INSERT INTO gocql_test.batchUnsetInsert(id, my_int, my_text) VALUES (?,?,?)", 1, UnsetValue, "")
33163316
b.Query("INSERT INTO gocql_test.batchUnsetInsert(id, my_int, my_text) VALUES (?,?,?)", 2, 2, UnsetValue)
33173317

3318-
if err := session.ExecuteBatch(b); err != nil {
3318+
if err := b.Exec(); err != nil {
33193319
t.Fatalf("query failed. %v", err)
33203320
} else {
33213321
if b.Attempts() < 1 {

doc.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@
311311
//
312312
// The CQL protocol supports sending batches of DML statements (INSERT/UPDATE/DELETE) and so does gocql.
313313
// Use Session.Batch to create a new batch and then fill-in details of individual queries.
314-
// Then execute the batch with Session.ExecuteBatch.
314+
// Then execute the batch with Batch.Exec.
315315
//
316316
// Logged batches ensure atomicity, either all or none of the operations in the batch will succeed, but they have
317317
// overhead to ensure this property.
@@ -329,8 +329,8 @@
329329
// It is also possible to pass entire BEGIN BATCH .. APPLY BATCH statement to Query.Exec.
330330
// There are differences how those are executed.
331331
// BEGIN BATCH statement passed to Query.Exec is prepared as a whole in a single statement.
332-
// Session.ExecuteBatch prepares individual statements in the batch.
333-
// If you have variable-length batches using the same statement, using Session.ExecuteBatch is more efficient.
332+
// Batch.Exec prepares individual statements in the batch.
333+
// If you have variable-length batches using the same statement, using Batch.Exec is more efficient.
334334
//
335335
// See Example_batch for an example.
336336
//
@@ -340,9 +340,9 @@
340340
// INSERT/UPDATE .. IF statement) and reading its result. See example for Query.MapScanCAS.
341341
//
342342
// Multiple-statement lightweight transactions can be executed as a logged batch that contains at least one conditional
343-
// statement. All the conditions must return true for the batch to be applied. You can use Session.ExecuteBatchCAS and
344-
// Session.MapExecuteBatchCAS when executing the batch to learn about the result of the LWT. See example for
345-
// Session.MapExecuteBatchCAS.
343+
// statement. All the conditions must return true for the batch to be applied. You can use Batch.ExecCAS and
344+
// Batch.MapExecCAS when executing the batch to learn about the result of the LWT. See example for
345+
// Batch.MapExecCAS.
346346
//
347347
// # Retries and speculative execution
348348
//

example_batch_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func Example_batch() {
6161
Idempotent: true,
6262
})
6363

64-
err = session.ExecuteBatch(b)
64+
err = b.Exec()
6565
if err != nil {
6666
log.Fatal(err)
6767
}

example_lwt_batch_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func ExampleSession_MapExecuteBatchCAS() {
7272
Args: []interface{}{"B", "pk1", "ck2", ck2Version},
7373
})
7474
m := make(map[string]interface{})
75-
applied, iter, err := session.MapExecuteBatchCAS(b.WithContext(ctx), m)
75+
applied, iter, err := b.WithContext(ctx).MapExecCAS(m)
7676
if err != nil {
7777
log.Fatal(err)
7878
}

integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func TestCustomPayloadMessages(t *testing.T) {
221221
b := session.Batch(LoggedBatch)
222222
b.CustomPayload = customPayload
223223
b.Query("INSERT INTO testCustomPayloadMessages(id,value) VALUES(1, 1)")
224-
if err := session.ExecuteBatch(b); err != nil {
224+
if err := b.Exec(); err != nil {
225225
t.Fatalf("query failed. %v", err)
226226
}
227227
}

session.go

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -759,13 +759,15 @@ func (s *Session) executeBatch(batch *Batch) *Iter {
759759
return iter
760760
}
761761

762+
// Deprecated: use Batch.Exec instead.
762763
// ExecuteBatch executes a batch operation and returns nil if successful
763764
// otherwise an error is returned describing the failure.
764765
func (s *Session) ExecuteBatch(batch *Batch) error {
765766
iter := s.executeBatch(batch)
766767
return iter.Close()
767768
}
768769

770+
// Deprecated: use Batch.ExecCAS instead
769771
// ExecuteBatchCAS executes a batch operation and returns true if successful and
770772
// an iterator (to scan additional rows if more than one conditional statement)
771773
// was sent.
@@ -788,6 +790,29 @@ func (s *Session) ExecuteBatchCAS(batch *Batch, dest ...interface{}) (applied bo
788790
return applied, iter, iter.err
789791
}
790792

793+
// ExecCAS executes a batch operation and returns true if successful and
794+
// an iterator (to scan additional rows if more than one conditional statement)
795+
// was sent.
796+
// Further scans on the interator must also remember to include
797+
// the applied boolean as the first argument to *Iter.Scan
798+
func (b *Batch) ExecCAS(dest ...interface{}) (applied bool, iter *Iter, err error) {
799+
iter = b.session.executeBatch(b)
800+
if err := iter.checkErrAndNotFound(); err != nil {
801+
iter.Close()
802+
return false, nil, err
803+
}
804+
805+
if len(iter.Columns()) > 1 {
806+
dest = append([]interface{}{&applied}, dest...)
807+
iter.Scan(dest...)
808+
} else {
809+
iter.Scan(&applied)
810+
}
811+
812+
return applied, iter, iter.err
813+
}
814+
815+
// Deprecated: use Batch.MapExecCAS instead
791816
// MapExecuteBatchCAS executes a batch operation much like ExecuteBatchCAS,
792817
// however it accepts a map rather than a list of arguments for the initial
793818
// scan.
@@ -813,6 +838,31 @@ func (s *Session) MapExecuteBatchCAS(batch *Batch, dest map[string]interface{})
813838
return applied, iter, iter.err
814839
}
815840

841+
// MapExecCAS executes a batch operation much like ExecuteBatchCAS,
842+
// however it accepts a map rather than a list of arguments for the initial
843+
// scan.
844+
func (b *Batch) MapExecCAS(dest map[string]interface{}) (applied bool, iter *Iter, err error) {
845+
iter = b.session.executeBatch(b)
846+
if err := iter.checkErrAndNotFound(); err != nil {
847+
iter.Close()
848+
return false, nil, err
849+
}
850+
iter.MapScan(dest)
851+
if iter.err != nil {
852+
return false, iter, iter.err
853+
}
854+
// check if [applied] was returned, otherwise it might not be CAS
855+
if _, ok := dest["[applied]"]; ok {
856+
applied = dest["[applied]"].(bool)
857+
delete(dest, "[applied]")
858+
}
859+
860+
// we usually close here, but instead of closing, just returin an error
861+
// if MapScan failed. Although Close just returns err, using Close
862+
// here might be confusing as we are not actually closing the iter
863+
return applied, iter, iter.err
864+
}
865+
816866
type hostMetrics struct {
817867
// Attempts is count of how many times this query has been attempted for this host.
818868
// An attempt is either a retry or fetching next page of results.
@@ -1766,9 +1816,8 @@ type Batch struct {
17661816
routingInfo *queryRoutingInfo
17671817
}
17681818

1819+
// Deprecated: use Session.Batch instead
17691820
// NewBatch creates a new batch operation using defaults defined in the cluster
1770-
//
1771-
// Deprecated: use session.Batch instead
17721821
func (s *Session) NewBatch(typ BatchType) *Batch {
17731822
return s.Batch(typ)
17741823
}

session_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func TestSessionAPI(t *testing.T) {
9898

9999
testBatch := s.Batch(LoggedBatch)
100100
testBatch.Query("test")
101-
err := s.ExecuteBatch(testBatch)
101+
err := testBatch.Exec()
102102

103103
if err != ErrNoConnections {
104104
t.Fatalf("expected session.ExecuteBatch to return '%v', got '%v'", ErrNoConnections, err)
@@ -111,7 +111,7 @@ func TestSessionAPI(t *testing.T) {
111111
//Should just return cleanly
112112
s.Close()
113113

114-
err = s.ExecuteBatch(testBatch)
114+
err = testBatch.Exec()
115115
if err != ErrSessionClosed {
116116
t.Fatalf("expected session.ExecuteBatch to return '%v', got '%v'", ErrSessionClosed, err)
117117
}

0 commit comments

Comments
 (0)