forked from scylladb/gocqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
713 lines (600 loc) · 17.7 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
// Copyright (C) 2017 ScyllaDB
// Use of this source code is governed by a ALv2-style
// license that can be found in the LICENSE file.
// +build all integration
package gocqlx_test
import (
"fmt"
"math"
"testing"
"time"
"github.com/gocql/gocql"
"github.com/scylladb/gocqlx/v2"
"github.com/scylladb/gocqlx/v2/gocqlxtest"
"github.com/scylladb/gocqlx/v2/qb"
"github.com/scylladb/gocqlx/v2/table"
"golang.org/x/sync/errgroup"
)
// Running examples locally:
// make run-scylla
// make run-examples
func TestExample(t *testing.T) {
cluster := gocqlxtest.CreateCluster()
session, err := gocqlx.WrapSession(cluster.CreateSession())
if err != nil {
t.Fatal("create session:", err)
}
defer session.Close()
session.ExecStmt(`DROP KEYSPACE examples`)
basicCreateAndPopulateKeyspace(t, session)
basicReadScyllaVersion(t, session)
datatypesBlob(t, session)
datatypesUserDefinedType(t, session)
datatypesUserDefinedTypeWrapper(t, session)
datatypesJson(t, session)
pagingForwardPaging(t, session)
pagingEfficientFullTableScan(t, session)
lwtLock(t, session)
}
// This example shows how to use query builders and table models to build
// queries. It uses "BindStruct" function for parameter binding and "Select"
// function for loading data to a slice.
func basicCreateAndPopulateKeyspace(t *testing.T, session gocqlx.Session) {
err := session.ExecStmt(`CREATE KEYSPACE IF NOT EXISTS examples WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}`)
if err != nil {
t.Fatal("create keyspace:", err)
}
type Song struct {
ID gocql.UUID
Title string
Album string
Artist string
Tags []string
Data []byte
}
type PlaylistItem struct {
ID gocql.UUID
Title string
Album string
Artist string
SongID gocql.UUID
}
err = session.ExecStmt(`CREATE TABLE IF NOT EXISTS examples.songs (
id uuid PRIMARY KEY,
title text,
album text,
artist text,
tags set<text>,
data blob)`)
if err != nil {
t.Fatal("create table:", err)
}
err = session.ExecStmt(`CREATE TABLE IF NOT EXISTS examples.playlists (
id uuid,
title text,
album text,
artist text,
song_id uuid,
PRIMARY KEY (id, title, album, artist))`)
if err != nil {
t.Fatal("create table:", err)
}
playlistMetadata := table.Metadata{
Name: "examples.playlists",
Columns: []string{"id", "title", "album", "artist", "song_id"},
PartKey: []string{"id"},
SortKey: []string{"title", "album", "artist", "song_id"},
}
playlistTable := table.New(playlistMetadata)
// Insert song using query builder.
insertSong := qb.Insert("examples.songs").
Columns("id", "title", "album", "artist", "tags", "data").Query(session)
insertSong.BindStruct(Song{
ID: mustParseUUID("756716f7-2e54-4715-9f00-91dcbea6cf50"),
Title: "La Petite Tonkinoise",
Album: "Bye Bye Blackbird",
Artist: "Joséphine Baker",
Tags: []string{"jazz", "2013"},
Data: []byte("music"),
})
if err := insertSong.ExecRelease(); err != nil {
t.Fatal("ExecRelease() failed:", err)
}
// Insert playlist using table model.
insertPlaylist := playlistTable.InsertQuery(session)
insertPlaylist.BindStruct(PlaylistItem{
ID: mustParseUUID("2cc9ccb7-6221-4ccb-8387-f22b6a1b354d"),
Title: "La Petite Tonkinoise",
Album: "Bye Bye Blackbird",
Artist: "Joséphine Baker",
SongID: mustParseUUID("756716f7-2e54-4715-9f00-91dcbea6cf50"),
})
if err := insertPlaylist.ExecRelease(); err != nil {
t.Fatal("ExecRelease() failed:", err)
}
// Query and displays data.
queryPlaylist := playlistTable.SelectQuery(session)
queryPlaylist.BindStruct(&PlaylistItem{
ID: mustParseUUID("2cc9ccb7-6221-4ccb-8387-f22b6a1b354d"),
})
var items []*PlaylistItem
if err := queryPlaylist.Select(&items); err != nil {
t.Fatal("Select() failed:", err)
}
for _, i := range items {
t.Logf("%+v", *i)
}
}
// This example shows how to load a single value using "Get" function.
// Get can also work with UDTs and types that implement gocql marshalling functions.
func basicReadScyllaVersion(t *testing.T, session gocqlx.Session) {
var releaseVersion string
err := session.Query("SELECT release_version FROM system.local", nil).Get(&releaseVersion)
if err != nil {
t.Fatal("Get() failed:", err)
}
t.Logf("Scylla version is: %s", releaseVersion)
}
// This examples shows how to bind data from a map using "BindMap" function,
// override field name mapping using the "db" tags, and use "Unsafe" function
// to handle situations where driver returns more coluns that we are ready to
// consume.
func datatypesBlob(t *testing.T, session gocqlx.Session) {
err := session.ExecStmt(`CREATE KEYSPACE IF NOT EXISTS examples WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}`)
if err != nil {
t.Fatal("create keyspace:", err)
}
err = session.ExecStmt(`CREATE TABLE IF NOT EXISTS examples.blobs(k int PRIMARY KEY, b blob, m map<text, blob>)`)
if err != nil {
t.Fatal("create table:", err)
}
// One way to get a byte buffer is to allocate it and fill it yourself:
var buf [16]byte
for i := range buf {
buf[i] = 0xff
}
insert := qb.Insert("examples.blobs").Columns("k", "b", "m").Query(session)
insert.BindMap(qb.M{
"k": 1,
"b": buf[:],
"m": map[string][]byte{"test": buf[:]},
})
if err := insert.ExecRelease(); err != nil {
t.Fatal("ExecRelease() failed:", err)
}
row := &struct {
Buffer []byte `db:"b"`
Mapping map[string][]byte `db:"m"`
}{}
q := qb.Select("examples.blobs").Where(qb.EqLit("k", "1")).Query(session)
// Unsafe is used here to override validation error that check if all
// requested columns are consumed `failed: missing destination name "k" in struct` error
if err := q.Iter().Unsafe().Get(row); err != nil {
t.Fatal("Get() failed:", err)
}
t.Logf("%+v", row.Buffer)
t.Logf("%+v", row.Mapping)
}
type Coordinates struct {
gocqlx.UDT
X int
Y int
}
// This example shows how to add User Defined Type marshalling capabilities by
// adding a single line - embedding gocqlx.UDT.
func datatypesUserDefinedType(t *testing.T, session gocqlx.Session) {
err := session.ExecStmt(`CREATE KEYSPACE IF NOT EXISTS examples WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}`)
if err != nil {
t.Fatal("create keyspace:", err)
}
err = session.ExecStmt(`CREATE TYPE IF NOT EXISTS examples.coordinates(x int, y int)`)
if err != nil {
t.Fatal("create type:", err)
}
err = session.ExecStmt(`CREATE TABLE IF NOT EXISTS examples.udts(k int PRIMARY KEY, c coordinates)`)
if err != nil {
t.Fatal("create table:", err)
}
coordinates1 := Coordinates{X: 12, Y: 34}
coordinates2 := Coordinates{X: 56, Y: 78}
insert := qb.Insert("examples.udts").Columns("k", "c").Query(session)
insert.BindMap(qb.M{
"k": 1,
"c": coordinates1,
})
if err := insert.Exec(); err != nil {
t.Fatal("Exec() failed:", err)
}
insert.BindMap(qb.M{
"k": 2,
"c": coordinates2,
})
if err := insert.Exec(); err != nil {
t.Fatal("Exec() failed:", err)
}
var coordinates []Coordinates
q := qb.Select("examples.udts").Columns("c").Query(session)
if err := q.Select(&coordinates); err != nil {
t.Fatal("Select() failed:", err)
}
for _, c := range coordinates {
t.Logf("%+v", c)
}
}
type coordinates struct {
X int
Y int
}
// This example shows how to add User Defined Type marshalling capabilities to
// types that we cannot modify, like library or transfer objects, without
// rewriting them in runtime.
func datatypesUserDefinedTypeWrapper(t *testing.T, session gocqlx.Session) {
err := session.ExecStmt(`CREATE KEYSPACE IF NOT EXISTS examples WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}`)
if err != nil {
t.Fatal("create keyspace:", err)
}
err = session.ExecStmt(`CREATE TYPE IF NOT EXISTS examples.coordinates(x int, y int)`)
if err != nil {
t.Fatal("create type:", err)
}
err = session.ExecStmt(`CREATE TABLE IF NOT EXISTS examples.udts_wrapper(k int PRIMARY KEY, c coordinates)`)
if err != nil {
t.Fatal("create table:", err)
}
// Embed coordinates within CoordinatesUDT
c1 := &coordinates{X: 12, Y: 34}
c2 := &coordinates{X: 56, Y: 78}
type CoordinatesUDT struct {
gocqlx.UDT
*coordinates
}
coordinates1 := CoordinatesUDT{coordinates: c1}
coordinates2 := CoordinatesUDT{coordinates: c2}
insert := qb.Insert("examples.udts_wrapper").Columns("k", "c").Query(session)
insert.BindMap(qb.M{
"k": 1,
"c": coordinates1,
})
if err := insert.Exec(); err != nil {
t.Fatal("Exec() failed:", err)
}
insert.BindMap(qb.M{
"k": 2,
"c": coordinates2,
})
if err := insert.Exec(); err != nil {
t.Fatal("Exec() failed:", err)
}
var coordinates []Coordinates
q := qb.Select("examples.udts_wrapper").Columns("c").Query(session)
if err := q.Select(&coordinates); err != nil {
t.Fatal("Select() failed:", err)
}
for _, c := range coordinates {
t.Logf("%+v", c)
}
}
// This example shows how to use query builder to work with
func datatypesJson(t *testing.T, session gocqlx.Session) {
err := session.ExecStmt(`CREATE KEYSPACE IF NOT EXISTS examples WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}`)
if err != nil {
t.Fatal("create keyspace:", err)
}
err = session.ExecStmt(`CREATE TABLE IF NOT EXISTS examples.querybuilder_json(id int PRIMARY KEY, name text, specs map<text, text>)`)
if err != nil {
t.Fatal("create table:", err)
}
insert := qb.Insert("examples.querybuilder_json").Json().Query(session)
insert.Bind(`{ "id": 1, "name": "Mouse", "specs": { "color": "silver" } }`)
if err := insert.Exec(); err != nil {
t.Fatal("Exec() failed:", err)
}
insert.Bind(`{ "id": 2, "name": "Keyboard", "specs": { "layout": "qwerty" } }`)
if err := insert.Exec(); err != nil {
t.Fatal("Exec() failed:", err)
}
// fromJson lets you provide individual columns as JSON:
insertFromJson := qb.Insert("examples.querybuilder_json").
Columns("id", "name").
FuncColumn("specs", qb.Fn("fromJson", "json")).
Query(session)
insertFromJson.BindMap(qb.M{
"id": 3,
"name": "Screen",
"json": `{ "size": "24-inch" }`,
})
if err := insertFromJson.Exec(); err != nil {
t.Fatal("Exec() failed:", err)
}
// Reading the whole row as a JSON object:
q := qb.Select("examples.querybuilder_json").
Json().
Where(qb.EqLit("id", "1")).
Query(session)
var jsonString string
if err := q.Get(&jsonString); err != nil {
t.Fatal("Get() failed:", err)
}
t.Logf("Entry #1 as JSON: %s", jsonString)
// Extracting a particular column as JSON:
q = qb.Select("examples.querybuilder_json").
Columns("id", "toJson(specs) AS json_specs").
Where(qb.EqLit("id", "2")).
Query(session)
row := &struct {
ID int
JsonSpecs string
}{}
if err := q.Get(row); err != nil {
t.Fatal("Get() failed:", err)
}
t.Logf("Entry #%d's specs as JSON: %s", row.ID, row.JsonSpecs)
}
type Video struct {
UserID int
UserName string
Added time.Time
VideoID int
Title string
}
func pagingFillTable(t *testing.T, insert *gocqlx.Queryx) {
t.Helper()
// 3 users
for i := 0; i < 3; i++ {
// 49 videos each
for j := 0; j < 49; j++ {
insert.BindStruct(Video{
UserID: i,
UserName: fmt.Sprint("user ", i),
Added: time.Unix(int64(j)*100, 0),
VideoID: i*100 + j,
Title: fmt.Sprint("video ", i*100+j),
})
if err := insert.Exec(); err != nil {
t.Fatal("Exec() failed:", err)
}
}
}
}
// This example shows how to use stateful paging and how "Select" function
// can be used to fetch single page only.
func pagingForwardPaging(t *testing.T, session gocqlx.Session) {
err := session.ExecStmt(`CREATE KEYSPACE IF NOT EXISTS examples WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}`)
if err != nil {
t.Fatal("create keyspace:", err)
}
err = session.ExecStmt(`CREATE TABLE IF NOT EXISTS examples.paging_forward_paging(
user_id int,
user_name text,
added timestamp,
video_id int,
title text,
PRIMARY KEY (user_id, added, video_id)
) WITH CLUSTERING ORDER BY (added DESC, video_id ASC)`)
if err != nil {
t.Fatal("create table:", err)
}
videoMetadata := table.Metadata{
Name: "examples.paging_forward_paging",
Columns: []string{"user_id", "user_name", "added", "video_id", "title"},
PartKey: []string{"user_id"},
SortKey: []string{"added", "video_id"},
}
videoTable := table.New(videoMetadata)
pagingFillTable(t, videoTable.InsertQuery(session))
// Query and displays data. Iterate over videos of user "1" 10 entries per request.
const itemsPerPage = 10
getUserVideos := func(userID int, page []byte) (userVideos []Video, nextPage []byte, err error) {
q := videoTable.SelectQuery(session).Bind(userID)
defer q.Release()
q.PageState(page)
q.PageSize(itemsPerPage)
iter := q.Iter()
return userVideos, iter.PageState(), iter.Select(&userVideos)
}
var (
userVideos []Video
nextPage []byte
)
for i := 1; ; i++ {
userVideos, nextPage, err = getUserVideos(1, nextPage)
if err != nil {
t.Fatalf("oad page %d: %s", i, err)
}
t.Logf("Page %d:", i)
for _, v := range userVideos {
t.Logf("%+v", v)
}
if len(nextPage) == 0 {
break
}
}
}
// This example shows how to efficiently process all rows in a table using
// the "token" function. It implements idea from blog post [1]:
// As a bonus we use "CompileNamedQueryString" to get named parameters out of
// CQL query placeholders like in Python or Java driver.
//
// [1] https://www.scylladb.com/2017/02/13/efficient-full-table-scans-with-scylla-1-6/.
func pagingEfficientFullTableScan(t *testing.T, session gocqlx.Session) {
err := session.ExecStmt(`CREATE KEYSPACE IF NOT EXISTS examples WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}`)
if err != nil {
t.Fatal("create keyspace:", err)
}
err = session.ExecStmt(`CREATE TABLE IF NOT EXISTS examples.paging_efficient_full_table_scan(
user_id int,
user_name text,
added timestamp,
video_id int,
title text,
PRIMARY KEY (user_id, added, video_id)
) WITH CLUSTERING ORDER BY (added DESC, video_id ASC)`)
if err != nil {
t.Fatal("create table:", err)
}
videoMetadata := table.Metadata{
Name: "examples.paging_efficient_full_table_scan",
Columns: []string{"user_id", "user_name", "added", "video_id", "title"},
PartKey: []string{"user_id"},
SortKey: []string{"added", "video_id"},
}
videoTable := table.New(videoMetadata)
pagingFillTable(t, videoTable.InsertQuery(session))
// Calculate optimal number of workers for the cluster:
var (
nodesInCluster = 1
coresInNode = 1
smudgeFactor = 3
)
workers := nodesInCluster * coresInNode * smudgeFactor
t.Logf("Workers %d", workers)
type tokenRange struct {
Start int64
End int64
}
buf := make(chan tokenRange)
// sequencer pushes token ranges to buf
sequencer := func() error {
span := int64(math.MaxInt64 / (50 * workers))
tr := tokenRange{math.MinInt64, math.MinInt64 + span}
for tr.End > tr.Start {
buf <- tr
tr.Start = tr.End
tr.End += span
}
tr.End = math.MaxInt64
buf <- tr
close(buf)
return nil
}
// worker queries a token ranges generated by sequencer
worker := func() error {
const cql = `SELECT * FROM examples.paging_efficient_full_table_scan WHERE
token(user_id) >= :start AND
token(user_id) < :end`
stmt, names, err := gocqlx.CompileNamedQueryString(cql)
if err != nil {
return err
}
q := session.Query(stmt, names)
defer q.Release()
var v Video
for {
tr, ok := <-buf
if !ok {
break
}
iter := q.BindStruct(tr).Iter()
for iter.StructScan(&v) {
t.Logf("%+v:", v)
}
if err := iter.Close(); err != nil {
return err
}
}
return nil
}
// Query and displays data.
var wg errgroup.Group
wg.Go(sequencer)
for i := 0; i < workers; i++ {
wg.Go(worker)
}
if err := wg.Wait(); err != nil {
t.Fatal(err)
}
}
// This example shows how to use Lightweight Transactions (LWT) aka.
// Compare-And-Set (CAS) functions.
// See: https://docs.scylladb.com/using-scylla/lwt/ for more details.
func lwtLock(t *testing.T, session gocqlx.Session) {
err := session.ExecStmt(`CREATE KEYSPACE IF NOT EXISTS examples WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}`)
if err != nil {
t.Fatal("create keyspace:", err)
}
type Lock struct {
Name string
Owner string
TTL int64
}
err = session.ExecStmt(`CREATE TABLE examples.lock (name text PRIMARY KEY, owner text)`)
if err != nil {
t.Fatal("create table:", err)
}
extend := func(lock Lock) bool {
q := qb.Update("examples.lock").
Set("owner").
Where(qb.Eq("name")).
If(qb.Eq("owner")).
TTLNamed("ttl").
Query(session).
SerialConsistency(gocql.Serial).
BindStruct(lock)
applied, err := q.ExecCASRelease()
if err != nil {
t.Fatal("ExecCASRelease() failed:", err)
}
return applied
}
acquire := func(lock Lock) (applied bool) {
var prev Lock
defer func() {
t.Logf("Acquire %+v applied %v owner %+v)", lock, applied, prev)
}()
q := qb.Insert("examples.lock").
Columns("name", "owner").
TTLNamed("ttl").
Unique().
Query(session).
SerialConsistency(gocql.Serial).
BindStruct(lock)
applied, err = q.GetCASRelease(&prev)
if err != nil {
t.Fatal("GetCASRelease() failed:", err)
}
if applied {
return true
}
if prev.Owner == lock.Owner {
return extend(lock)
}
return false
}
const (
resource = "acme"
ttl = time.Second
)
l1 := Lock{
Name: resource,
Owner: "1",
TTL: qb.TTL(ttl),
}
l2 := Lock{
Name: resource,
Owner: "2",
TTL: qb.TTL(ttl),
}
if !acquire(l1) {
t.Fatal("l1 failed to acquire lock")
}
if acquire(l2) {
t.Fatal("unexpectedly l2 acquired lock")
}
if !acquire(l1) {
t.Fatal("l1 failed to extend lock")
}
time.Sleep(time.Second)
if !acquire(l2) {
t.Fatal("l2 failed to acquire lock")
}
if acquire(l1) {
t.Fatal("unexpectedly l1 acquired lock")
}
}
func mustParseUUID(s string) gocql.UUID {
u, err := gocql.ParseUUID(s)
if err != nil {
panic(err)
}
return u
}