gosql

package module
v3.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 GetNopDbPartition = createNopDbPartition()
View Source
var GetNopTablePartition = createNopTablePartition()
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, TableSelector)) (*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, table string) *Shard

func (*Cluster) Write

func (p *Cluster) Write(handler DBHandler)

type ClusterConfig

type ClusterConfig struct {
	ReplicationConfig ReplicationConfig
	PartitionConfig   PartitionConfig
}

func NewClusterConfig

func NewClusterConfig(config *YamlClusterConfig) *ClusterConfig

func (*ClusterConfig) GetPartitionConfig

func (c *ClusterConfig) GetPartitionConfig() *PartitionConfig

func (*ClusterConfig) GetReplicationConfig

func (c *ClusterConfig) GetReplicationConfig() *ReplicationConfig

type ClusterConfiger

type ClusterConfiger interface {
	GetReplicationConfig() *ReplicationConfig
	GetPartitionConfig() *PartitionConfig
}

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, TableSelector) error

type DbConfiger

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

type DbPartition

type DbPartition struct {
	Total int
	// contains filtered or unexported fields
}

func (*DbPartition) Partition

func (p *DbPartition) Partition(key []byte, table string) int

func (*DbPartition) Pick

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

func (*DbPartition) Select

func (p *DbPartition) Select(key []byte, table string) string

type DbPartitioner

type DbPartitioner interface {
	Pick(prefix string, index int) string
	Partition([]byte, string) int
	TableSelector
}

func NewPartition

func NewPartition(conf PartitionConfiger) (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) GetTablePartition

func (p *NopDbPartition) GetTablePartition(table string) TablePartition

func (*NopDbPartition) Partition

func (p *NopDbPartition) Partition(bytes []byte, s string) int

func (*NopDbPartition) Pick

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

func (*NopDbPartition) Select

func (p *NopDbPartition) Select(key []byte, table string) string

type NopReplication

type NopReplication struct {
}

func (*NopReplication) Replicate

func (lb *NopReplication) Replicate(int) int

type NopTablePartition

type NopTablePartition struct {
}

func (*NopTablePartition) GetTableChunkTotal

func (p *NopTablePartition) GetTableChunkTotal() int

func (*NopTablePartition) GetTableTotal

func (p *NopTablePartition) GetTableTotal() int

func (*NopTablePartition) Partition

func (p *NopTablePartition) Partition(key []byte) int

func (*NopTablePartition) Select

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

type Option

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

func AddExecutor

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

func AddMaster

func AddMaster(conf ShardConfiger) Option

func AddReplica

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

func AddTablePartition

func AddTablePartition(selector TableSelector) Option

type Partition

type Partition = distributed.Partition

type PartitionConfig

type PartitionConfig struct {
	Type    string
	DbTotal int
	Table   *TablePartitionConfig
}

func NewPartitionConfig

func NewPartitionConfig(p *YamlPartitionConfig) *PartitionConfig

分库

func (*PartitionConfig) GetDbTotal

func (p *PartitionConfig) GetDbTotal() int

func (*PartitionConfig) GetTablePartitionConfig

func (p *PartitionConfig) GetTablePartitionConfig() *TablePartitionConfig

func (*PartitionConfig) GetType

func (p *PartitionConfig) GetType() string

type PartitionConfiger

type PartitionConfiger interface {
	GetDbTotal() int
	GetType() string
	GetTablePartitionConfig() *TablePartitionConfig
}

type PreloadTablePartition

type PreloadTablePartition struct {
	Table           string
	TableTotal      int
	TableChunkTotal int
	// contains filtered or unexported fields
}

func (*PreloadTablePartition) GetTableChunkTotal

func (p *PreloadTablePartition) GetTableChunkTotal() int

func (*PreloadTablePartition) GetTableTotal

func (p *PreloadTablePartition) GetTableTotal() int

func (*PreloadTablePartition) Partition

func (p *PreloadTablePartition) Partition(key []byte) int

func (*PreloadTablePartition) Select

func (p *PreloadTablePartition) Select(key []byte, table 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, TableSelector)
	// 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, table string) string
	Partition(key []byte) int
	GetTableTotal() int
	GetTableChunkTotal() int
}

func NewTablePartition

func NewTablePartition(conf TablePartitionConfiger) TablePartition

type TablePartitionConfig

type TablePartitionConfig struct {
	DbTotal     int
	TableConfig []*TablePartitionNodeConfig
}

func NewTablePartitionConfig

func NewTablePartitionConfig(conf []*YamlTablePartitionConfig, dbTotal int) *TablePartitionConfig

分表

func (*TablePartitionConfig) GetTablePartitionNodeConfig

func (c *TablePartitionConfig) GetTablePartitionNodeConfig() []TablePartitionConfiger

type TablePartitionConfiger

type TablePartitionConfiger interface {
	GetDbTotal() int
	GetTableTotal() int
	GetName() string
}

type TablePartitionNodeConfig

type TablePartitionNodeConfig struct {
	DbTotal int
	Name    string
	Total   int
}

func (*TablePartitionNodeConfig) GetDbTotal

func (t *TablePartitionNodeConfig) GetDbTotal() int

func (*TablePartitionNodeConfig) GetName

func (t *TablePartitionNodeConfig) GetName() string

func (*TablePartitionNodeConfig) GetTableTotal

func (t *TablePartitionNodeConfig) GetTableTotal() int

type TableSelector

type TableSelector interface {
	Select([]byte, string) string
}

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 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 {
	Type    string                      `yaml:"type"`
	DbTotal int                         `yaml:"db_total"`
	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 {
	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