gosql

package module
v4.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 27, 2021 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrShardPicker          = errors.New("pick shard error")
	ErrShardPickerOverLimit = errors.New("pick shard over limit")
)
View Source
var (
	DefaultConfigSet = wire.NewSet(
		defaultYamlConfigSet,
		ProvideConfig,
	)
)
View Source
var (
	GetBufferPool = CreatePool(func() interface{} {
		var b bytes.Buffer
		return &b
	})
)
View Source
var (
	GetStringsBuilderPool = CreatePool(func() interface{} {
		var b bytes.Buffer
		return &b
	})
)

Functions

func AcquireBuffer

func AcquireBuffer() *bytes.Buffer

func AcquireStringsBuilder

func AcquireStringsBuilder() *strings.Builder

func CreatePool

func CreatePool(f func() interface{}) func() *sync.Pool

func NewDB

func NewDB(driver string, url string, c ConnConfig) *sql.DB

func ProvideDynamicRe

func ProvideDynamicRe(conf *Config) (map[string]*sql.DB, error)

func ProvideYamlConfigFile

func ProvideYamlConfigFile(path string) (*os.File, func(), error)

func ReleaseBuffer

func ReleaseBuffer(b *bytes.Buffer)

func ReleaseStringsBuilder

func ReleaseStringsBuilder(b *strings.Builder)

Types

type AnnuallyDbSelector

type AnnuallyDbSelector struct {
}

func ProvideAnnuallyDbSelector

func ProvideAnnuallyDbSelector() *AnnuallyDbSelector

func (*AnnuallyDbSelector) Select

func (se *AnnuallyDbSelector) Select(di int, dbname string, params ...interface{}) *bytes.Buffer

type AnnuallyTableSelector

type AnnuallyTableSelector struct {
	// contains filtered or unexported fields
}

func ProvideAnnuallyTableSelector

func ProvideAnnuallyTableSelector(conf *Config, dp Partition) *AnnuallyTableSelector

func (*AnnuallyTableSelector) Pick

func (s *AnnuallyTableSelector) Pick(table string, node int, params ...interface{}) *bytes.Buffer

func (*AnnuallyTableSelector) Select

func (s *AnnuallyTableSelector) Select(table string, key []byte, params ...interface{}) (int, int, *bytes.Buffer)

func (*AnnuallyTableSelector) Total

func (s *AnnuallyTableSelector) Total(table string) int

type Cluster

type Cluster interface {
	Write(table string, key []byte, handler DBHandler, params ...interface{})
	Read(table string, key []byte, handler DBHandler, params ...interface{})
	GetShardsTotal() int
	TableSelector() TableSelector
}

type Config

type Config struct {
	ShardingConfig ShardingConfig
	ShardsConfig   []ShardConfig
}

func ProvideConfig

func ProvideConfig(config *YamlConfig) (*Config, error)

func (Config) GetShardingConfig

func (c Config) GetShardingConfig() ShardingConfig

func (Config) GetShardsConfig

func (c Config) GetShardsConfig() []ShardConfig

type ConnConfig

type ConnConfig struct {
	MaxLifeTime  int
	MaxIdleConns int
	MaxOpenConns int
}

func (ConnConfig) GetMaxIdleConns

func (c ConnConfig) GetMaxIdleConns() int

func (ConnConfig) GetMaxLifeTime

func (c ConnConfig) GetMaxLifeTime() time.Duration

func (ConnConfig) GetMaxOpenConns

func (c ConnConfig) GetMaxOpenConns() int

type DBHandler

type DBHandler func(*sql.DB, int, *bytes.Buffer) error

type DailyDbSelector

type DailyDbSelector struct {
}

func ProvideDailyDbSelector

func ProvideDailyDbSelector() *DailyDbSelector

func (*DailyDbSelector) Select

func (se *DailyDbSelector) Select(di int, dbname string, params ...interface{}) *bytes.Buffer

type DailyTableSelector

type DailyTableSelector struct {
	// contains filtered or unexported fields
}

func ProvideDailyTableSelector

func ProvideDailyTableSelector(conf *Config, dp Partition) *DailyTableSelector

func (*DailyTableSelector) Pick

func (s *DailyTableSelector) Pick(table string, node int, params ...interface{}) *bytes.Buffer

func (*DailyTableSelector) Select

func (s *DailyTableSelector) Select(table string, key []byte, params ...interface{}) (int, int, *bytes.Buffer)

func (*DailyTableSelector) Total

func (s *DailyTableSelector) Total(table string) int

type DbConfig

type DbConfig struct {
	Driver   string
	Dbname   string
	Protocol string
	Host     string
	User     string
	Password string
	Params   map[string]string
	Conn     ConnConfig
}

func (DbConfig) GetConn

func (c DbConfig) GetConn() ConnConfig

func (DbConfig) GetDbname

func (c DbConfig) GetDbname() string

func (DbConfig) GetDriver

func (c DbConfig) GetDriver() string

func (DbConfig) GetHost

func (c DbConfig) GetHost() string

func (DbConfig) GetParams

func (c DbConfig) GetParams() map[string]string

func (DbConfig) GetPassword

func (c DbConfig) GetPassword() string

func (DbConfig) GetProtocol

func (c DbConfig) GetProtocol() string

func (DbConfig) GetUrl

func (c DbConfig) GetUrl(dbname string) string

func (DbConfig) GetUser

func (c DbConfig) GetUser() string

type DbExecutor

type DbExecutor func(*sql.DB, DBHandler, int, *bytes.Buffer)
var DefaultDBExecutor DbExecutor = func(db *sql.DB, handler DBHandler, node int, table *bytes.Buffer) {
	_ = handler(db, node, table)
}

type DbSelector

type DbSelector interface {
	Select(int, string, ...interface{}) *bytes.Buffer
}

type Decoder

type Decoder interface {
	Decode(interface{}) error
}

type DynamicCluster

type DynamicCluster struct {
	Re       map[string]*sql.DB
	Config   *Config
	Sharding Sharding
	Selector DbSelector
	Executor DbExecutor
	Lb       Replication
	// contains filtered or unexported fields
}

func (*DynamicCluster) GetShardsTotal

func (s *DynamicCluster) GetShardsTotal() int

func (*DynamicCluster) Read

func (s *DynamicCluster) Read(table string, key []byte, handler DBHandler, params ...interface{})

func (*DynamicCluster) TableSelector

func (s *DynamicCluster) TableSelector() TableSelector

func (*DynamicCluster) Write

func (s *DynamicCluster) Write(table string, key []byte, handler DBHandler, params ...interface{})

type MonthlyDbSelector

type MonthlyDbSelector struct {
}

func ProvideMonthlyDbSelector

func ProvideMonthlyDbSelector() *MonthlyDbSelector

func (*MonthlyDbSelector) Select

func (se *MonthlyDbSelector) Select(di int, dbname string, params ...interface{}) *bytes.Buffer

type MonthlyTableSelector

type MonthlyTableSelector struct {
	// contains filtered or unexported fields
}

func ProvideMonthlyTableSelector

func ProvideMonthlyTableSelector(conf *Config, dp Partition) *MonthlyTableSelector

func (*MonthlyTableSelector) Pick

func (s *MonthlyTableSelector) Pick(table string, node int, params ...interface{}) *bytes.Buffer

func (*MonthlyTableSelector) Select

func (s *MonthlyTableSelector) Select(table string, key []byte, params ...interface{}) (int, int, *bytes.Buffer)

func (*MonthlyTableSelector) Total

func (s *MonthlyTableSelector) Total(table string) int

type Partition

type Partition interface {
	Partition([]byte, int) int
}

type Replication

type Replication interface {
	Replicate(int) int
}

type RoundRobinSelector

type RoundRobinSelector = distributed.RoundRobinSelector

func ProvideRoundRobinSelector

func ProvideRoundRobinSelector() *RoundRobinSelector

type Shard

type Shard interface {
	GetMaster() *sql.DB
	GetReplica() *sql.DB
	GetReplicaTotal() int
}

func ProvideStaticShards

func ProvideStaticShards(conf *Config, sharding Sharding, lb Replication) ([]Shard, error)

type ShardConfig

type ShardConfig struct {
	MasterConfig   DbConfig
	ReplicasConfig []DbConfig
}

func (ShardConfig) GetMasterConfig

func (c ShardConfig) GetMasterConfig() DbConfig

func (ShardConfig) GetReplicasConfig

func (c ShardConfig) GetReplicasConfig() []DbConfig

type Sharding

type Sharding interface {
	Select(string, []byte, ...interface{}) (int, int, *bytes.Buffer)
	Allocation(int, string, ...interface{}) string
	GetDbname() string
	TableSelector() TableSelector
}

type ShardingConfig

type ShardingConfig struct {
	Dbname string
	Total  int
	Type   string
	Table  []ShardingTableConfig
}

func (ShardingConfig) GetDbname

func (s ShardingConfig) GetDbname() string

func (ShardingConfig) GetTableConfig

func (s ShardingConfig) GetTableConfig() []ShardingTableConfig

func (ShardingConfig) GetTotal

func (s ShardingConfig) GetTotal() int

func (ShardingConfig) GetType

func (s ShardingConfig) GetType() string

type ShardingTableConfig

type ShardingTableConfig struct {
	Name  string
	Total int
}

func (*ShardingTableConfig) GetTableName

func (s *ShardingTableConfig) GetTableName() string

func (*ShardingTableConfig) GetTotal

func (s *ShardingTableConfig) GetTotal() int

type StandardSharding

type StandardSharding struct {
	// contains filtered or unexported fields
}

func ProvideStandardSharding

func ProvideStandardSharding(conf *Config, selector TableSelector) *StandardSharding

func (*StandardSharding) Allocation

func (s *StandardSharding) Allocation(i int, dbname string, params ...interface{}) string

func (*StandardSharding) GetDbname

func (s *StandardSharding) GetDbname() string

func (*StandardSharding) Select

func (s *StandardSharding) Select(table string, key []byte, params ...interface{}) (int, int, *bytes.Buffer)

func (*StandardSharding) TableSelector

func (s *StandardSharding) TableSelector() TableSelector

type StandardTableSelector

type StandardTableSelector struct {
	// contains filtered or unexported fields
}

func ProvideStandardTableSelector

func ProvideStandardTableSelector(conf *Config, dp Partition) *StandardTableSelector

func (*StandardTableSelector) Pick

func (s *StandardTableSelector) Pick(table string, node int, params ...interface{}) *bytes.Buffer

func (*StandardTableSelector) Select

func (s *StandardTableSelector) Select(table string, key []byte, params ...interface{}) (int, int, *bytes.Buffer)

func (*StandardTableSelector) Total

func (s *StandardTableSelector) Total(table string) int

type StaticCluster

type StaticCluster struct {
	Sharding Sharding
	Shards   []Shard
	Config   *Config
	Executor DbExecutor
}

func (*StaticCluster) GetShardsTotal

func (s *StaticCluster) GetShardsTotal() int

func (*StaticCluster) Read

func (s *StaticCluster) Read(table string, key []byte, handler DBHandler, params ...interface{})

func (*StaticCluster) TableSelector

func (s *StaticCluster) TableSelector() TableSelector

func (*StaticCluster) Write

func (s *StaticCluster) Write(table string, key []byte, handler DBHandler, params ...interface{})

type StaticShard

type StaticShard struct {
	Master  *sql.DB
	Replica []*sql.DB
	// contains filtered or unexported fields
}

func NewShard

func NewShard(conf ShardConfig, dbname string, lb Replication) *StaticShard

func (*StaticShard) GetMaster

func (s *StaticShard) GetMaster() *sql.DB

func (*StaticShard) GetReplica

func (s *StaticShard) GetReplica() *sql.DB

func (*StaticShard) GetReplicaTotal

func (s *StaticShard) GetReplicaTotal() int

type TableSelector

type TableSelector interface {
	Select(string, []byte, ...interface{}) (int, int, *bytes.Buffer)
	Pick(string, int, ...interface{}) *bytes.Buffer
	Total(string) int
}

type YamlClusterConfig

type YamlClusterConfig struct {
	YamlDBConfig `yaml:",inline"`
	Sharding     YamlShardingConfig `yaml:"sharding"`
}

type YamlConfig

type YamlConfig struct {
	Cluster YamlClusterConfig   `yaml:"cluster"`
	Shards  []*YamlShardsConfig `yaml:"shards"`
}

func ProvideYamlConfig

func ProvideYamlConfig(dec Decoder) (*YamlConfig, error)

type YamlConnConfig

type YamlConnConfig struct {
	MaxLifetime  int `yaml:"max_life_time"`
	MaxIdleConns int `yaml:"max_idle_conns"`
	MaxOpenConns int `yaml:"max_open_conns"`
}

type YamlDBConfig

type YamlDBConfig struct {
	Driver string         `yaml:"driver"`
	Dbname string         `yaml:"dbname"`
	Type   string         `yaml:"type"`
	Conn   YamlConnConfig `yaml:"conn"`
}

type YamlDecoder

type YamlDecoder = yaml.Decoder

func ProvideYamlDecoder

func ProvideYamlDecoder(rd io.Reader) *YamlDecoder

type YamlMasterConfig

type YamlMasterConfig struct {
	Host           string `yaml:"host"`
	YamlNodeConfig `yaml:",inline"`
}

type YamlNodeConfig

type YamlNodeConfig struct {
	User     string            `yaml:"user"`
	Password string            `yaml:"password"`
	Protocol string            `yaml:"protocol"`
	Params   map[string]string `yaml:"params"`
}

type YamlReplicasConfig

type YamlReplicasConfig struct {
	Hosts          []string `yaml:"hosts"`
	YamlNodeConfig `yaml:",inline"`
}

type YamlShardingConfig

type YamlShardingConfig struct {
	DbTotal int                        `yaml:"total"`
	Table   []*YamlTableShardingConfig `yaml:"table"`
}

type YamlShardsConfig

type YamlShardsConfig struct {
	Master   YamlMasterConfig   `yaml:"master"`
	Replicas YamlReplicasConfig `yaml:"replicas"`
}

type YamlTableShardingConfig

type YamlTableShardingConfig struct {
	Name  string `yaml:"name"`
	Total int    `yaml:"total"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL