Skip to content

Commit 0094af0

Browse files
tengu-altjoao-r-reis
authored andcommitted
Deprecate Session.ExecuteBatch() and move "execute batch" methods to Batch type
Session.ExecuteBatch() was deprecated and similar methods(MapExecuteBatchCAS,ExecuteBatchCAS) were deprecated and moved to Batch type patch by Oleksandr Luzhniy; reviewed by João Reis, Bohdan Siryk, for CASSGO-57
1 parent d32a392 commit 0094af0

File tree

8 files changed

+54
-34
lines changed

8 files changed

+54
-34
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5252

5353
- Refactoring hostpool package test and Expose HostInfo creation (CASSGO-59)
5454

55+
- Move "execute batch" methods to Batch type (CASSGO-57)
56+
5557
### Fixed
5658
- Cassandra version unmarshal fix (CASSGO-49)
5759

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, TOTIMESTAMP(NOW()))")
485485
insertBatch.Query("INSERT INTO cas_table (title, revid, last_modified) VALUES ('_foo', 3e4ad2f1-73a4-11e5-9381-29463d90c3f0, TOTIMESTAMP(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 = TOTIMESTAMP(NOW()) WHERE title='_foo' AND revid=2c3af400-73a4-11e5-9381-29463d90c3f0 IF last_modified=TOTIMESTAMP(NOW());")
492492
failBatch.Query("UPDATE cas_table SET last_modified = TOTIMESTAMP(NOW()) WHERE title='_foo' AND revid=3e4ad2f1-73a4-11e5-9381-29463d90c3f0 IF last_modified=TOTIMESTAMP(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 = TOTIMESTAMP(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 = TOTIMESTAMP(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
}
@@ -752,7 +752,7 @@ func TestBatch(t *testing.T) {
752752
batch.Query(`INSERT INTO batch_table (id) VALUES (?)`, i)
753753
}
754754

755-
if err := session.ExecuteBatch(batch); err != nil {
755+
if err := batch.Exec(); err != nil {
756756
t.Fatal("execute batch:", err)
757757
}
758758

@@ -788,7 +788,7 @@ func TestUnpreparedBatch(t *testing.T) {
788788
batch.Query(`UPDATE batch_unprepared SET c = c + 1 WHERE id = 1`)
789789
}
790790

791-
if err := session.ExecuteBatch(batch); err != nil {
791+
if err := batch.Exec(); err != nil {
792792
t.Fatal("execute batch:", err)
793793
}
794794

@@ -824,8 +824,8 @@ func TestBatchLimit(t *testing.T) {
824824
for i := 0; i < 65537; i++ {
825825
batch.Query(`INSERT INTO batch_table2 (id) VALUES (?)`, i)
826826
}
827-
if err := session.ExecuteBatch(batch); err != ErrTooManyStmts {
828-
t.Fatal("gocql attempted to execute a batch larger than the support limit of statements.")
827+
if err := batch.Exec(); err != ErrTooManyStmts {
828+
t.Fatalf("gocql attempted to execute a batch larger than the support limit of statements: expected %v, got %v", ErrTooManyStmts, err)
829829
}
830830

831831
}
@@ -876,7 +876,7 @@ func TestTooManyQueryArgs(t *testing.T) {
876876

877877
batch := session.Batch(UnloggedBatch)
878878
batch.Query("INSERT INTO too_many_query_args (id, value) VALUES (?, ?)", 1, 2, 3)
879-
err = session.ExecuteBatch(batch)
879+
err = batch.Exec()
880880

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

909909
batch := session.Batch(UnloggedBatch)
910910
batch.Query("INSERT INTO not_enough_query_args (id, cluster, value) VALUES (?, ?, ?)", 1, 2)
911-
err = session.ExecuteBatch(batch)
911+
err = batch.Exec()
912912

913913
if err == nil {
914914
t.Fatal("'`INSERT INTO not_enough_query_args (id, cluster, value) VALUES (?, ?, ?)`, 1, 2' should return an error")
@@ -1534,7 +1534,7 @@ func TestBatchQueryInfo(t *testing.T) {
15341534
batch := session.Batch(LoggedBatch)
15351535
batch.Bind("INSERT INTO batch_query_info (id, cluster, value) VALUES (?, ?,?)", write)
15361536

1537-
if err := session.ExecuteBatch(batch); err != nil {
1537+
if err := batch.Exec(); err != nil {
15381538
t.Fatalf("batch insert into batch_query_info failed, err '%v'", err)
15391539
}
15401540

@@ -2047,7 +2047,7 @@ func TestBatchStats(t *testing.T) {
20472047
b.Query("INSERT INTO batchStats (id) VALUES (?)", 1)
20482048
b.Query("INSERT INTO batchStats (id) VALUES (?)", 2)
20492049

2050-
if err := session.ExecuteBatch(b); err != nil {
2050+
if err := b.Exec(); err != nil {
20512051
t.Fatalf("query failed. %v", err)
20522052
} else {
20532053
if b.Attempts() < 1 {
@@ -2104,7 +2104,7 @@ func TestBatchObserve(t *testing.T) {
21042104
batch.Query(fmt.Sprintf(`INSERT INTO batch_observe_table (id,other) VALUES (?,%d)`, i), i)
21052105
}
21062106

2107-
if err := session.ExecuteBatch(batch); err != nil {
2107+
if err := batch.Exec(); err != nil {
21082108
t.Fatal("execute batch:", err)
21092109
}
21102110
if observedBatch == nil {
@@ -3408,7 +3408,7 @@ func TestUnsetColBatch(t *testing.T) {
34083408
b.Query("INSERT INTO gocql_test.batchUnsetInsert(id, my_int, my_text) VALUES (?,?,?)", 1, UnsetValue, "")
34093409
b.Query("INSERT INTO gocql_test.batchUnsetInsert(id, my_int, my_text) VALUES (?,?,?)", 2, 2, UnsetValue)
34103410

3411-
if err := session.ExecuteBatch(b); err != nil {
3411+
if err := b.Exec(); err != nil {
34123412
t.Fatalf("query failed. %v", err)
34133413
} else {
34143414
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: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -775,20 +775,31 @@ func (s *Session) executeBatch(batch *Batch) *Iter {
775775
return iter
776776
}
777777

778+
// Deprecated: use Batch.Exec instead.
778779
// ExecuteBatch executes a batch operation and returns nil if successful
779780
// otherwise an error is returned describing the failure.
780781
func (s *Session) ExecuteBatch(batch *Batch) error {
781782
iter := s.executeBatch(batch)
782783
return iter.Close()
783784
}
784785

786+
// Deprecated: use Batch.ExecCAS instead
785787
// ExecuteBatchCAS executes a batch operation and returns true if successful and
786788
// an iterator (to scan additional rows if more than one conditional statement)
787789
// was sent.
788790
// Further scans on the interator must also remember to include
789791
// the applied boolean as the first argument to *Iter.Scan
790792
func (s *Session) ExecuteBatchCAS(batch *Batch, dest ...interface{}) (applied bool, iter *Iter, err error) {
791-
iter = s.executeBatch(batch)
793+
return batch.ExecCAS(dest...)
794+
}
795+
796+
// ExecCAS executes a batch operation and returns true if successful and
797+
// an iterator (to scan additional rows if more than one conditional statement)
798+
// was sent.
799+
// Further scans on the interator must also remember to include
800+
// the applied boolean as the first argument to *Iter.Scan
801+
func (b *Batch) ExecCAS(dest ...interface{}) (applied bool, iter *Iter, err error) {
802+
iter = b.session.executeBatch(b)
792803
if err := iter.checkErrAndNotFound(); err != nil {
793804
iter.Close()
794805
return false, nil, err
@@ -804,11 +815,19 @@ func (s *Session) ExecuteBatchCAS(batch *Batch, dest ...interface{}) (applied bo
804815
return applied, iter, iter.err
805816
}
806817

818+
// Deprecated: use Batch.MapExecCAS instead
807819
// MapExecuteBatchCAS executes a batch operation much like ExecuteBatchCAS,
808820
// however it accepts a map rather than a list of arguments for the initial
809821
// scan.
810822
func (s *Session) MapExecuteBatchCAS(batch *Batch, dest map[string]interface{}) (applied bool, iter *Iter, err error) {
811-
iter = s.executeBatch(batch)
823+
return batch.MapExecCAS(dest)
824+
}
825+
826+
// MapExecCAS executes a batch operation much like ExecuteBatchCAS,
827+
// however it accepts a map rather than a list of arguments for the initial
828+
// scan.
829+
func (b *Batch) MapExecCAS(dest map[string]interface{}) (applied bool, iter *Iter, err error) {
830+
iter = b.session.executeBatch(b)
812831
if err := iter.checkErrAndNotFound(); err != nil {
813832
iter.Close()
814833
return false, nil, err
@@ -1828,9 +1847,8 @@ type Batch struct {
18281847
routingInfo *queryRoutingInfo
18291848
}
18301849

1850+
// Deprecated: use Session.Batch instead
18311851
// NewBatch creates a new batch operation using defaults defined in the cluster
1832-
//
1833-
// Deprecated: use session.Batch instead
18341852
func (s *Session) NewBatch(typ BatchType) *Batch {
18351853
return s.Batch(typ)
18361854
}

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)