gosql

package module
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2021 License: MIT Imports: 13 Imported by: 0

README

gosql

sql lib for go

Documentation

Index

Constants

This section is empty.

Variables

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
	})
)
View Source
var (
	NopDbPartitionError = errors.New("Empty DB Partition Config ")
)

Functions

func AcquireBuffer

func AcquireBuffer() *bytes.Buffer

func AcquireStringsBuilder

func AcquireStringsBuilder() *strings.Builder

func CreatePool

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

func DividesGenerate

func DividesGenerate(prefix string, num int) string

func NewDB

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

func ReleaseBuffer

func ReleaseBuffer(b *bytes.Buffer)

func ReleaseStringsBuilder

func ReleaseStringsBuilder(b *strings.Builder)

Types

type Cluster

type Cluster struct {
	DP     DbPartitioner
	Shards []*Shard
}

func NewCluster

func NewCluster(conf Configer, exec func(*sql.DB, DBHandler, TablePartition)) (*Cluster, error)

func (*Cluster) Map

func (p *Cluster) Map(f func(*Shard))

func (*Cluster) Pick

func (p *Cluster) Pick() *Shard

func (*Cluster) Read

func (p *Cluster) Read(handler DBHandler)

func (*Cluster) Select

func (p *Cluster) Select(key []byte) *Shard

func (*Cluster) Write

func (p *Cluster) Write(handler DBHandler)

type ClusterConfig

type ClusterConfig struct {
	ReplicationConfig    ReplicationConfig
	DbPartitionConfig    DbPartitionConfig
	TablePartitionConfig TablePartitionConfig
}

func NewClusterConfig

func NewClusterConfig(config *YamlClusterConfig) *ClusterConfig

func (*ClusterConfig) GetDbPartitionConfig

func (c *ClusterConfig) GetDbPartitionConfig() *DbPartitionConfig

func (*ClusterConfig) GetReplicationConfig

func (c *ClusterConfig) GetReplicationConfig() *ReplicationConfig

func (*ClusterConfig) GetTablePartitionConfig

func (c *ClusterConfig) GetTablePartitionConfig() *TablePartitionConfig

type ClusterConfiger

type ClusterConfiger interface {
	GetReplicationConfig() *ReplicationConfig
	GetDbPartitionConfig() *DbPartitionConfig
	GetTablePartitionConfig() *TablePartitionConfig
}

type Config

type Config struct {
	Cluster *ClusterConfig
	Shards  []*ShardConfig
}

func NewConfig

func NewConfig(conf *YamlConfig) *Config

func (*Config) GetClusterConfig

func (c *Config) GetClusterConfig() *ClusterConfig

func (*Config) GetShardsConfig

func (c *Config) GetShardsConfig() []*ShardConfig

type Configer

type Configer interface {
	GetClusterConfig() *ClusterConfig
	GetShardsConfig() []*ShardConfig
}

type ConnConfiger

type ConnConfiger interface {
	GetMaxLifetime() time.Duration
	GetMaxIdleConns() int
	GetMaxOpenConns() int
}

type DBHandler

type DBHandler func(*sql.DB, TablePartition) error

type DbConfiger

type DbConfiger interface {
	GetDriver() string
	GetDbname() string
	SetDbname(string)
	ConnConfiger
}

type DbPartition

type DbPartition struct {
	Total     int
	Partition Partition
}

func (*DbPartition) Divide

func (p *DbPartition) Divide(prefix string, num int) string

func (*DbPartition) Select

func (p *DbPartition) Select(key []byte) int

type DbPartitionConfig

type DbPartitionConfig struct {
	YamlDbPartitionConfig
}

func NewDbPartitionConfig

func NewDbPartitionConfig(p *YamlPartitionConfig) *DbPartitionConfig

func (*DbPartitionConfig) GetTotal

func (d *DbPartitionConfig) GetTotal() int

func (*DbPartitionConfig) GetType

func (d *DbPartitionConfig) GetType() string

type DbPartitionConfiger

type DbPartitionConfiger interface {
	GetTotal() int
	GetType() string
}

type DbPartitioner

type DbPartitioner interface {
	Select(key []byte) int
	Divide(prefix string, num int) string
}

func NewDbPartition

func NewDbPartition(conf DbPartitionConfiger) (DbPartitioner, error)

type Decoder

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

type MasterNode

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

func NewMasterNode

func NewMasterNode(driver string, c MasterNodeConfiger) *MasterNode

func (*MasterNode) GetDB

func (m *MasterNode) GetDB() *sql.DB

type MasterNodeConfiger

type MasterNodeConfiger interface {
	GetMasterUrl() string
	DbConfiger
}

type NopDbPartition

type NopDbPartition struct {
}

func (*NopDbPartition) Divide

func (p *NopDbPartition) Divide(prefix string, num int) string

func (*NopDbPartition) Select

func (p *NopDbPartition) Select([]byte) int

type NopReplication

type NopReplication struct {
}

func (*NopReplication) Replicate

func (lb *NopReplication) Replicate(int) int

type NopTablePartition

type NopTablePartition struct {
}

func (*NopTablePartition) Select

func (p *NopTablePartition) Select(key []byte, prefix string) string

type Option

type Option interface {
	// contains filtered or unexported methods
}

func AddExecutor

func AddExecutor(f func(*sql.DB, DBHandler, TablePartition)) Option

func AddMaster

func AddMaster(conf ShardConfiger) Option

func AddReplica

func AddReplica(conf ShardConfiger, opts ...ReplicaNodeOption) Option

func AddTablePartition

func AddTablePartition(tp TablePartition) Option

type Partition

type Partition = distributed.Partition

type PreloadTablePartition

type PreloadTablePartition struct {
	Partition Partition
	// contains filtered or unexported fields
}

func (*PreloadTablePartition) Select

func (p *PreloadTablePartition) Select(key []byte, prefix string) string

type ReplicaNode

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

func NewReplicasNode

func NewReplicasNode(driver string, c ReplicaNodeConfiger, opts ...ReplicaNodeOption) *ReplicaNode

func (*ReplicaNode) GetDB

func (r *ReplicaNode) GetDB() *sql.DB

func (*ReplicaNode) SetReplication

func (r *ReplicaNode) SetReplication(lb Replication)

func (*ReplicaNode) WithOptions

func (r *ReplicaNode) WithOptions(opts ...ReplicaNodeOption) *ReplicaNode

type ReplicaNodeConfiger

type ReplicaNodeConfiger interface {
	GetReplicaUrls() []string
	DbConfiger
}

type ReplicaNodeOption

type ReplicaNodeOption interface {
	// contains filtered or unexported methods
}

func AddReplication

func AddReplication(lb Replication) ReplicaNodeOption

type Replication

type Replication = distributed.Replication

func NewReplication

func NewReplication(conf ReplicationConfiger) Replication

type ReplicationConfig

type ReplicationConfig struct {
	YamlReplicationConfig
}

func NewReplicationConfig

func NewReplicationConfig(conf *YamlReplicationConfig) *ReplicationConfig

func (*ReplicationConfig) GetType

func (r *ReplicationConfig) GetType() string

type ReplicationConfiger

type ReplicationConfiger interface {
	GetType() string
}

type Shard

type Shard struct {
	Master  *MasterNode
	Replica *ReplicaNode

	Executor func(*sql.DB, DBHandler, TablePartition)
	// contains filtered or unexported fields
}

func NewShard

func NewShard(conf *ShardConfig, options ...Option) *Shard

func (*Shard) Read

func (s *Shard) Read(handler DBHandler)

func (*Shard) WithOptions

func (s *Shard) WithOptions(opts ...Option) *Shard

func (*Shard) Write

func (s *Shard) Write(handler DBHandler)

type ShardConfig

type ShardConfig struct {
	YamlDBConfig
	YamlShardConfig
	// contains filtered or unexported fields
}

func NewShardConfig

func NewShardConfig(shard *YamlShardConfig, db *YamlDBConfig, r *YamlReplicationConfig) *ShardConfig

func (*ShardConfig) GetDbname

func (s *ShardConfig) GetDbname() string

func (*ShardConfig) GetDriver

func (s *ShardConfig) GetDriver() string

func (*ShardConfig) GetMasterUrl

func (s *ShardConfig) GetMasterUrl() string

func (*ShardConfig) GetMaxIdleConns

func (s *ShardConfig) GetMaxIdleConns() int

func (*ShardConfig) GetMaxLifetime

func (s *ShardConfig) GetMaxLifetime() time.Duration

func (*ShardConfig) GetMaxOpenConns

func (s *ShardConfig) GetMaxOpenConns() int

func (*ShardConfig) GetReplicaUrls

func (s *ShardConfig) GetReplicaUrls() []string

func (*ShardConfig) SetDbname

func (s *ShardConfig) SetDbname(name string)

type TablePartition

type TablePartition interface {
	Select(key []byte, prefix string) string
}

func NewTablePartition

func NewTablePartition(conf TablePartitionConfiger, start int) TablePartition

type TablePartitionConfig

type TablePartitionConfig struct {
	YamlTablePartitionConfig
}

func NewTablePartitionConfig

func NewTablePartitionConfig(p *YamlPartitionConfig) *TablePartitionConfig

func (*TablePartitionConfig) GetSpecs

func (*TablePartitionConfig) GetType

func (t *TablePartitionConfig) GetType() string

type TablePartitionConfiger

type TablePartitionConfiger interface {
	GetType() string
	GetSpecs() []*TablePartitionSpecsConfig
}

type TablePartitionSpecConfiger

type TablePartitionSpecConfiger interface {
	GetName() string
	GetTotal() int
}

type TablePartitionSpecsConfig

type TablePartitionSpecsConfig = YamlTablePartitionSpecConfig

func (*TablePartitionSpecsConfig) GetName

func (p *TablePartitionSpecsConfig) GetName() string

func (*TablePartitionSpecsConfig) GetTotal

func (p *TablePartitionSpecsConfig) GetTotal() int

type YamlClusterConfig

type YamlClusterConfig struct {
	Replication YamlReplicationConfig `yaml:"replication"`
	Partition   YamlPartitionConfig   `yaml:"partition"`
}

type YamlConfig

type YamlConfig struct {
	Cluster YamlClusterConfig  `yaml:"cluster"`
	Db      YamlDBConfig       `yaml:"db"`
	Shards  []*YamlShardConfig `yaml:"shards"`
}

func ParseYamlConfig

func ParseYamlConfig(d Decoder) (*YamlConfig, error)

func ParseYamlConfigFile

func ParseYamlConfigFile(path string) (*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"`
	Conn   YamlConnConfig `yaml:"conn"`
}

type YamlDbPartitionConfig

type YamlDbPartitionConfig struct {
	Type  string `yaml:"type"`
	Total int    `yaml:"total"`
}

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 YamlPartitionConfig

type YamlPartitionConfig struct {
	Db    YamlDbPartitionConfig    `yaml:"db"`
	Table YamlTablePartitionConfig `yaml:"table"`
}

type YamlReplicasConfig

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

type YamlReplicationConfig

type YamlReplicationConfig struct {
	Type string `yaml:"type"`
}

type YamlShardConfig

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

type YamlTablePartitionConfig

type YamlTablePartitionConfig struct {
	Type  string                       `yaml:"type"`
	Specs []*TablePartitionSpecsConfig `yaml:"specs"`
}

type YamlTablePartitionSpecConfig

type YamlTablePartitionSpecConfig 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