github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/internal/router/scylla/router.go (about) 1 package scyllaRouter 2 3 import ( 4 "github.com/ronaksoft/rony/pools/querypool" 5 "github.com/ronaksoft/rony/tools" 6 "github.com/scylladb/gocqlx/v2" 7 "github.com/scylladb/gocqlx/v2/table" 8 ) 9 10 type Router struct { 11 s gocqlx.Session 12 qp map[string]*querypool.QueryPool 13 t *table.Table 14 v map[string]*table.Table 15 } 16 17 func New(s gocqlx.Session) *Router { 18 r := &Router{ 19 s: s, 20 t: table.New(table.Metadata{ 21 Name: "entities", 22 Columns: []string{"entity_id", "replica_set", "created_on", "edited_on"}, 23 PartKey: []string{"entity_id"}, 24 SortKey: []string{}, 25 }), 26 } 27 r.qp = map[string]*querypool.QueryPool{ 28 "insertIF": querypool.New(func() *gocqlx.Queryx { 29 return r.t.InsertBuilder().Unique().Query(s) 30 }), 31 "insert": querypool.New(func() *gocqlx.Queryx { 32 return r.t.InsertBuilder().Query(s) 33 }), 34 "update": querypool.New(func() *gocqlx.Queryx { 35 return r.t.UpdateBuilder().Set("replica_set", "edited_on").Query(s) 36 }), 37 "delete": querypool.New(func() *gocqlx.Queryx { 38 return r.t.DeleteBuilder().Query(s) 39 }), 40 "get": querypool.New(func() *gocqlx.Queryx { 41 return r.t.GetQuery(s, "replica_set", "created_on", "edited_on") 42 }), 43 } 44 45 return r 46 } 47 48 func (r *Router) Set(entityID string, replicaSet uint64, replace bool) error { 49 var q *gocqlx.Queryx 50 if replace { 51 q = r.qp["insertIF"].GetQuery() 52 defer r.qp["insertIF"].Put(q) 53 } else { 54 q = r.qp["insert"].GetQuery() 55 defer r.qp["insert"].Put(q) 56 } 57 58 q.Bind(entityID, replicaSet, tools.TimeUnix(), tools.TimeUnix()) 59 60 return q.Exec() 61 } 62 63 func (r *Router) Get(entityID string) (replicaSet uint64, err error) { 64 q := r.qp["get"].GetQuery() 65 defer r.qp["get"].Put(q) 66 67 q.Bind(entityID) 68 69 err = q.Scan(&replicaSet, nil, nil) 70 71 return 72 }