shard

package
v2.1.3 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultShardFunc = func(_ context.Context, shardKey string) ShardID {
	shardID, err := strconv.Atoi(shardKey)
	if err != nil {

		return math.MaxInt32
	}

	return ShardID(shardID)
}

DefaultShardFunc default function for determining shard number by shardKey.

Functions

This section is empty.

Types

type DB

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

DB sharded database. Contains information about all shards. Each shard is a separate database. This is a service object that is used in bucket.DB for managing connections to shards.

func New

func New(shardInfo []*ShardInfo, shardFunc ShardFunc, opts ...Option) *DB

New creates a sharded database.

func NewFromDSN

func NewFromDSN(dsn []DSNInfo, shardFunc ShardFunc, opts ...Option) *DB

NewFromDSN creates a sharded database by creating PxDB based on DSN.

func (*DB) Begin

func (s *DB) Begin(ctx context.Context, shardKey string,
	f func(context.Context) error, opts ...txmgr.Option,
) error

Begin starts a function in a transaction for the specified shardKey.

func (*DB) Connection

func (s *DB) Connection(ctx context.Context, shardKey string, opt ...conn.ConnectionOption) conn.IConnection

Connection returns IConnection interface implementation for the specified shardKey.

func (*DB) GetFunc

func (s *DB) GetFunc() ShardFunc

GetFunc returns a function to get shard id by sharding key.

func (*DB) GetShards

func (s *DB) GetShards() []ShardID

GetShards returns information about shards.

func (*DB) GetTxManager

func (s *DB) GetTxManager(shardID ShardID) txmgr.ITransactionManager

GetTxManager returns transaction manager for the shard.

func (*DB) Info

func (s *DB) Info() bootstrap.Info

Info returns information about the shard.

func (*DB) RunFunc

func (s *DB) RunFunc(ctx context.Context,
	f func(ctx context.Context, shardID ShardID, con conn.IConnection) error,
	runParallel int,
) error

RunFunc executes a function for all shards. The order of shards is not defined. runParallel specifies the number of goroutines to use for parallel execution. If runParallel is 0, the function will be executed in the sequential way.

func (*DB) Start

func (s *DB) Start(ctx context.Context) error

Start launches the service.

func (*DB) Stop

func (s *DB) Stop(ctx context.Context) error

Stop stops the service.

func (*DB) WithoutTransaction

func (s *DB) WithoutTransaction(ctx context.Context, shardKey string) context.Context

WithoutTransaction returns context without transaction for the specified shardKey.

type DSNInfo

type DSNInfo struct {
	ShardID ShardID
	DSN     string
	Options []db.Option
}

DSNInfo information about PxDB with DSN.

type Option

type Option func(*DB)

Option option for DB.

func WithLogger

func WithLogger(logger ctxlog.ILogger) Option

WithLogger sets the logger.

func WithName

func WithName(name string) Option

WithName sets the name of the sharded DB.

func WithRestartPolicy

func WithRestartPolicy(restartPolicy []backoff.RetryOption) Option

WithRestartPolicy sets the restart policy.

type ShardFunc

type ShardFunc func(ctx context.Context, shardKey string) ShardID

ShardFunc function to get shard by key.

type ShardID

type ShardID uint

ShardID shard identifier.

func (ShardID) String

func (s ShardID) String() string

String converts ShardID to string.

type ShardInfo

type ShardInfo struct {
	ShardID    ShardID
	Connector  db.IStartStopConnector
	TxBeginner txmgr.ITransactionBeginner
	TxInformer txmgr.ITransactionInformer
	// contains filtered or unexported fields
}

ShardInfo information about a shard.

func NewInfoPxDB

func NewInfoPxDB(
	shardID ShardID,
	pgdb *db.PxDB,
	t telemetry.ITelemetry,
) *ShardInfo

NewInfoPxDB helper function, that creates shard information based on db.PxDB. telemetry is optional.

Jump to

Keyboard shortcuts

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