router

package
v0.15.5 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2018 License: MIT, MIT Imports: 15 Imported by: 3

Documentation

Index

Constants

View Source
const (
	// unset - no type for this KeyspaceId
	KIT_UNSET = KeyspaceIdType("")

	// uint64 - a uint64 value is used
	// this is represented as 'unsigned bigint' in mysql
	KIT_UINT64 = KeyspaceIdType("uint64")

	// bytes - a string of bytes is used
	// this is represented as 'varbinary' in mysql
	KIT_BYTES = KeyspaceIdType("bytes")
)
View Source
const (
	MinNumKey = math.MinInt64
	MaxNumKey = math.MaxInt64
)
View Source
const (
	EID_NODE = iota
	VALUE_NODE
	LIST_NODE
	OTHER_NODE
)

Variables

View Source
var (
	DefaultRuleType = "default"
	HashRuleType    = "hash"
	RangeRuleType   = "range"
)
View Source
var AllKeyspaceIdTypes = []KeyspaceIdType{
	KIT_UNSET,
	KIT_UINT64,
	KIT_BYTES,
}
View Source
var MaxKey = KeyspaceId("")

MaxKey is bigger than all KeyspaceId (by convention).

View Source
var MinKey = KeyspaceId("")

MinKey is smaller than all KeyspaceId (the value really is).

Functions

func EncodeValue

func EncodeValue(value interface{}) string

func GetShardList

func GetShardList(sql string, r *Router, bindVars map[string]interface{}) (nodes []string, err error)

Limitation:

where, eg, key name is id:

where id = 1
where id in (1, 2, 3)
where id > 1
where id >= 1
where id < 1
where id <= 1
where id between 1 and 10
where id >= 1 and id < 10

func GetShardListIndex

func GetShardListIndex(sql string, r *Router, bindVars map[string]interface{}) (nodes []int, err error)

func GetStmtShardList

func GetStmtShardList(stmt ast.Statement, r *Router, bindVars map[string]interface{}) (nodes []string, err error)

func GetStmtShardListIndex

func GetStmtShardListIndex(stmt ast.Statement, r *Router, bindVars map[string]interface{}) (nodes []int, err error)

func HashValue

func HashValue(value interface{}) uint64

func IsKeyspaceIdTypeInList

func IsKeyspaceIdTypeInList(typ KeyspaceIdType, types []KeyspaceIdType) bool

IsKeyspaceIdTypeInList returns true if the given type is in the list. Use it with AllKeyspaceIdTypes for instance.

func KeyRangesIntersect

func KeyRangesIntersect(first, second KeyRange) bool

KeyRangesIntersect returns true if some Keyspace values exist in both ranges.

See: http://stackoverflow.com/questions/4879315/what-is-a-tidy-algorithm-to-find-overlapping-intervals two segments defined as (a,b) and (c,d) (with a<b and c<d): intersects = (b > c) && (a < d) overlap = min(b, d) - max(c, a)

func NumValue

func NumValue(value interface{}) int64

Types

type DefaultShard

type DefaultShard struct {
}

func (*DefaultShard) FindForKey

func (s *DefaultShard) FindForKey(key interface{}) int

type HashShard

type HashShard struct {
	ShardNum int
}

func (*HashShard) FindForKey

func (s *HashShard) FindForKey(key interface{}) int

type HexKeyspaceId

type HexKeyspaceId string

HexKeyspaceId is the hex represention of a KeyspaceId.

func (HexKeyspaceId) Unhex

func (hkid HexKeyspaceId) Unhex() (KeyspaceId, error)

Unhex converts a HexKeyspaceId into a KeyspaceId (hex decoding).

type KeyError

type KeyError string

func NewKeyError

func NewKeyError(format string, args ...interface{}) KeyError

func (KeyError) Error

func (ke KeyError) Error() string

type KeyRange

type KeyRange struct {
	Start KeyspaceId
	End   KeyspaceId
}

KeyRange is an interval of KeyspaceId values. It contains Start, but excludes End. In other words, it is: [Start, End[

func KeyRangesOverlap

func KeyRangesOverlap(first, second KeyRange) (KeyRange, error)

KeyRangesOverlap returns the overlap between two KeyRanges. They need to overlap, otherwise an error is returned.

func ParseKeyRangeParts

func ParseKeyRangeParts(start, end string) (KeyRange, error)

Parse a start and end hex values and build a KeyRange

func ParseShardingSpec

func ParseShardingSpec(spec string) ([]KeyRange, error)

ParseShardingSpec parses a string that describes a sharding specification. a-b-c-d will be parsed as a-b, b-c, c-d. The empty string may serve both as the start and end of the keyspace: -a-b- will be parsed as start-a, a-b, b-end.

func (KeyRange) Contains

func (kr KeyRange) Contains(i KeyspaceId) bool

func (KeyRange) IsPartial

func (kr KeyRange) IsPartial() bool

Returns true if the KeyRange does not cover the entire space.

func (KeyRange) MapKey

func (kr KeyRange) MapKey() string

func (KeyRange) String

func (kr KeyRange) String() string

type KeyRangeShard

type KeyRangeShard struct {
	Shards []KeyRange
}

func (*KeyRangeShard) EqualStart

func (s *KeyRangeShard) EqualStart(key interface{}, index int) bool

func (*KeyRangeShard) EqualStop

func (s *KeyRangeShard) EqualStop(key interface{}, index int) bool

func (*KeyRangeShard) FindForKey

func (s *KeyRangeShard) FindForKey(key interface{}) int

type KeyspaceId

type KeyspaceId string

KeyspaceId is the type we base sharding on.

func (KeyspaceId) Hex

func (kid KeyspaceId) Hex() HexKeyspaceId

Hex prints a KeyspaceId in capital hex.

type KeyspaceIdType

type KeyspaceIdType string

KeyspaceIdType represents the type of the KeyspaceId. Usually we don't care, but some parts of the code will need that info.

type NumKeyRange

type NumKeyRange struct {
	Start int64
	End   int64
}

func ParseNumShardingSpec

func ParseNumShardingSpec(spec string) ([]NumKeyRange, error)

ParseShardingSpec parses a string that describes a sharding specification. a-b-c-d will be parsed as a-b, b-c, c-d. The empty string may serve both as the start and end of the keyspace: -a-b- will be parsed as start-a, a-b, b-end.

func (NumKeyRange) Contains

func (kr NumKeyRange) Contains(i int64) bool

func (NumKeyRange) MapKey

func (kr NumKeyRange) MapKey() string

func (NumKeyRange) String

func (kr NumKeyRange) String() string

type NumRangeShard

type NumRangeShard struct {
	Shards []NumKeyRange
}

func (*NumRangeShard) EqualStart

func (s *NumRangeShard) EqualStart(key interface{}, index int) bool

func (*NumRangeShard) EqualStop

func (s *NumRangeShard) EqualStop(key interface{}, index int) bool

func (*NumRangeShard) FindForKey

func (s *NumRangeShard) FindForKey(key interface{}) int

type RangeShard

type RangeShard interface {
	Shard
	EqualStart(key interface{}, index int) bool
	EqualStop(key interface{}, index int) bool
}

type Router

type Router struct {
	DB          string
	Rules       map[string]*Rule //key is <table name>
	DefaultRule *Rule
	// contains filtered or unexported fields
}

func NewRouter

func NewRouter(conf *models.Config, schema *schema.Schema) (*Router, error)

func (*Router) GetRule

func (r *Router) GetRule(table string) *Rule

type RoutingPlan

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

type Rule

type Rule struct {
	DB    string
	Table string
	Key   string
	Type  string
	Nodes []string
	Shard Shard
}

func NewDefaultRule

func NewDefaultRule(db string, node string) *Rule

func (*Rule) FindNode

func (r *Rule) FindNode(key interface{}) string

func (*Rule) FindNodeIndex

func (r *Rule) FindNodeIndex(key interface{}) int

func (*Rule) String

func (r *Rule) String() string

type RuleConfig

type RuleConfig struct {
	models.ShardConfig
}

func (*RuleConfig) ParseRule

func (c *RuleConfig) ParseRule(db string) (*Rule, error)

type Shard

type Shard interface {
	FindForKey(key interface{}) int
}

type Uint64Key

type Uint64Key uint64

Uint64Key is a uint64 that can be converted into a KeyspaceId.

func (Uint64Key) KeyspaceId

func (i Uint64Key) KeyspaceId() KeyspaceId

KeyspaceId returns the KeyspaceId associated with a Uint64Key.

func (Uint64Key) String

func (i Uint64Key) String() string

Jump to

Keyboard shortcuts

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