backend/pkg/library/cassandra/option.go

145 lines
3.3 KiB
Go
Raw Normal View History

2025-11-17 09:31:58 +00:00
package cassandra
import (
"time"
"github.com/scylladb/gocqlx/v3/qb"
"github.com/gocql/gocql"
)
// Option 是設定選項的函數型別
type Option func(*cassandraConf)
func WithPort(port int) Option {
return func(c *cassandraConf) {
c.Port = port
}
}
func WithKeyspace(keyspace string) Option {
return func(c *cassandraConf) {
c.Keyspace = keyspace
}
}
func WithAuth(username, password string) Option {
return func(c *cassandraConf) {
c.Username = username
c.Password = password
c.UseAuth = true
}
}
// WithConsistency is used to set the consistency level, default is Quorum
func WithConsistency(consistency gocql.Consistency) Option {
return func(c *cassandraConf) {
c.Consistency = consistency
}
}
// WithConnectTimeoutSec is used to set the connect timeout, default is 10 seconds
func WithConnectTimeoutSec(timeout int) Option {
return func(c *cassandraConf) {
if timeout <= 0 {
timeout = defaultTimeoutSec
}
c.ConnectTimeoutSec = timeout
}
}
// WithNumConns is used to set the number of connections to each node, default is 10
func WithNumConns(numConns int) Option {
return func(c *cassandraConf) {
if numConns <= 0 {
numConns = defaultNumConns
}
c.NumConns = numConns
}
}
// WithMaxRetries is used to set the maximum retries, default is 3
func WithMaxRetries(maxRetries int) Option {
return func(c *cassandraConf) {
if maxRetries <= 0 {
maxRetries = defaultMaxRetries
}
c.MaxRetries = maxRetries
}
}
// WithRetryMinInterval is used to set the minimum retry interval, default is 1 second
func WithRetryMinInterval(duration time.Duration) Option {
return func(c *cassandraConf) {
if duration <= 0 {
duration = defaultRetryMinInterval
}
c.RetryMinInterval = duration
}
}
// WithRetryMaxInterval is used to set the maximum retry interval, default is 30 seconds
func WithRetryMaxInterval(duration time.Duration) Option {
return func(c *cassandraConf) {
if duration <= 0 {
duration = defaultRetryMaxInterval
}
c.RetryMaxInterval = duration
}
}
// WithReconnectInitialInterval is used to set the initial reconnect interval, default is 1 second
func WithReconnectInitialInterval(duration time.Duration) Option {
return func(c *cassandraConf) {
if duration <= 0 {
duration = defaultReconnectInitialInterval
}
c.ReconnectInitialInterval = duration
}
}
// WithReconnectMaxInterval is used to set the maximum reconnect interval, default is 60 seconds
func WithReconnectMaxInterval(duration time.Duration) Option {
return func(c *cassandraConf) {
if duration <= 0 {
duration = defaultReconnectMaxInterval
}
c.ReconnectMaxInterval = duration
}
}
// WithCQLVersion is used to set the CQL version, default is 3.0.0
func WithCQLVersion(version string) Option {
return func(c *cassandraConf) {
if version == "" {
version = defaultCqlVersion
}
c.CQLVersion = version
}
}
// ===============================================================
// QueryOption defines a function that modifies a query builder
type QueryOption func(*qb.SelectBuilder, qb.M)
// WithWhere adds WHERE clauses to the query
func WithWhere(where []qb.Cmp, args map[string]any) QueryOption {
return func(b *qb.SelectBuilder, bind qb.M) {
if len(where) > 0 {
b.Where(where...)
for k, v := range args {
bind[k] = v
}
}
}
}