storage

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2020 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Unused = 0

	ErrorHandlerDepth = 4

	InvalidRank  = -1
	InvalidScore = math.MinInt64

	MinScore = math.MinInt64
	MaxScore = math.MaxInt64
)
View Source
const NullCacheProxy = nullCacheProxy(0)
View Source
const NullDatabaseProxy = nullDatabaseProxy(0)

Variables

View Source
var (
	ErrNotFound            = errors.New("not found")
	ErrFieldNotFound       = errors.New("field not found")
	ErrUnexpectedLength    = errors.New("unexpected length")
	ErrViewRefFieldMissing = errors.New("view ref field missing")
	ErrTableNotFoundInView = errors.New("table not found in view")
	ErrTypeAssert          = errors.New("type assert failed")
	ErrNotImplemented      = errors.New("not implemented")
)

Functions

func ContainsField

func ContainsField(fields []string, field string) bool

func JoinField

func JoinField(key, originField string) string

func JoinIndexKey

func JoinIndexKey(engineName string, index Index) string

func JoinKey

func JoinKey(engineName, originKey string) string

Types

type CacheProxy

type CacheProxy interface {
	NewSession() CacheProxySession
}

type CacheProxySession

type CacheProxySession interface {
	Tx
	HDel(table string, fields ...string) (int64, error)
	HExists(table, field string) (bool, error)
	HIncrBy(table, field string, incr int64) (int64, error)
	HMGet(table string, fields ...string) ([]interface{}, error)
	HMSet(table string, fields map[string]string) (string, error)
	Delete(keys ...string) (int64, error)
	ZAdd(key string, members ...redis.Z) (int64, error)
	ZRem(key string, members ...interface{}) (int64, error)
	ZRank(key, member string) (int64, error)
	ZScore(key, member string) (int64, error)
	// range APIs
	ZRange(key string, start, stop int64) (RangeResult, error)
	ZRangeWithScores(key string, start, stop int64) (RangeResult, error)
	ZRangeByScore(key string, opt redis.ZRangeBy) (RangeResult, error)
	ZRangeByLex(key string, opt redis.ZRangeBy) (RangeLexResult, error)
	ZRangeByScoreWithScores(key string, opt redis.ZRangeBy) (RangeResult, error)
	ZRevRange(key string, start, stop int64) (RangeResult, error)
	ZRevRangeWithScores(key string, start, stop int64) (RangeResult, error)
	ZRevRangeByScore(key string, opt redis.ZRangeBy) (RangeResult, error)
	ZRevRangeByLex(key string, opt redis.ZRangeBy) (RangeLexResult, error)
	ZRevRangeByScoreWithScores(key string, opt redis.ZRangeBy) (RangeResult, error)
}

type DatabaseProxy

type DatabaseProxy interface {
	NewSession() DatabaseProxySession
}

type DatabaseProxySession

type DatabaseProxySession interface {
	Tx
	Insert(table Table) (int64, error)
	Update(table Table, fields ...string) (int64, error)
	Remove(tableName, keyName string, keys ...interface{}) (int64, error)
	Get(table Table, fields ...string) (bool, error)
}

type Engine

type Engine interface {
	Repository
	// Cache returns CacheProxy
	Cache() CacheProxy
	// Database returns DatabaseProxy
	Database() DatabaseProxy
	// SetErrorHandler sets handler for handling error
	SetErrorHandler(eh ErrorHandler)
	// AddIndex adds an index, should panic if repeated index name on a same table
	AddIndex(index Index)
	// NewSession new a session
	NewSession() Session
}

func NewEngine

func NewEngine(name string, database DatabaseProxy, cache CacheProxy) Engine

NewEngine creates an engine which named name. Parameter database MUST be not nil, but cache can be nil.

type ErrorHandler

type ErrorHandler func(action string, err error) error

ErrorHandler handles error

type Field

type Field string

Field implements FieldList which atmost contains one value

func (Field) Field

func (f Field) Field(i int) string

func (Field) Len

func (f Field) Len() int

type FieldGetter

type FieldGetter interface {
	GetField(field string) (interface{}, bool)
}

FieldGetter get value by field

type FieldList

type FieldList interface {
	Len() int
	Field(int) string
}

FieldList holds n fields

type FieldSetter

type FieldSetter interface {
	SetField(field, value string) error
}

FieldGetter set value by field

type FieldSlice

type FieldSlice []string

FieldSlice implements FieldList

func (FieldSlice) Field

func (fs FieldSlice) Field(i int) string

func (FieldSlice) Len

func (fs FieldSlice) Len() int

type GetOption

type GetOption func(*getOptions)

GetOption represents options for Get/Find/FindView operations

func WithSyncFromDatabase

func WithSyncFromDatabase() GetOption

WithSyncFromDatabase returns a GetOption which would get data from database if data not found in cache

type Index

type Index interface {
	Name() string
	TableMeta() TableMeta
	Update(s Session, table ReadonlyTable, key interface{}, updatedFields []string) error
	Remove(s Session, keys ...interface{}) error
}

Index represents a sorted set for field of table

type Int16Keys

type Int16Keys []int16

func (Int16Keys) Key

func (keys Int16Keys) Key(i int) interface{}

func (Int16Keys) Len

func (keys Int16Keys) Len() int

type Int32Keys

type Int32Keys []int32

func (Int32Keys) Key

func (keys Int32Keys) Key(i int) interface{}

func (Int32Keys) Len

func (keys Int32Keys) Len() int

type Int64Keys

type Int64Keys []int64

func ToInt64Keys

func ToInt64Keys(keys KeyList, filter func(int64) bool) Int64Keys

ToInt64Keys convert KeyList to an Int64Keys which satify filter all keys would be contained if filter is nil

func (Int64Keys) Key

func (keys Int64Keys) Key(i int) interface{}

func (Int64Keys) Len

func (keys Int64Keys) Len() int

type Int8Keys

type Int8Keys []int8

func (Int8Keys) Key

func (keys Int8Keys) Key(i int) interface{}

func (Int8Keys) Len

func (keys Int8Keys) Len() int

type IntKeys

type IntKeys []int

func (IntKeys) Key

func (keys IntKeys) Key(i int) interface{}

func (IntKeys) Len

func (keys IntKeys) Len() int

type InterfaceKeys

type InterfaceKeys []interface{}

func (InterfaceKeys) Key

func (keys InterfaceKeys) Key(i int) interface{}

func (InterfaceKeys) Len

func (keys InterfaceKeys) Len() int

type KeyList

type KeyList interface {
	Len() int
	Key(int) interface{}
}

KeyList holds n keys

type RangeLexResult

type RangeLexResult interface {
	KeyList
}

RangeLexResult represents result of range by lex

type RangeOption

type RangeOption func(*rangeOptions)

RangeOption represents options for IndexRange operations

func RangeCount

func RangeCount(count int64) RangeOption

func RangeOffset

func RangeOffset(offset int64) RangeOption

func RangeRev

func RangeRev() RangeOption

func RangeWithScores

func RangeWithScores() RangeOption

type RangeResult

type RangeResult interface {
	KeyList
	Score(i int) int64
}

RangeLexResult represents result of range by rank,score etc.

type ReadonlyTable

type ReadonlyTable interface {
	TableMeta() TableMeta
	Key() interface{}
	FieldGetter
}

ReadonlyTable represents a read-only table

type Record

type Record interface {
	Key() string
	Member() interface{}
	Unixstamp() int64
}

Record represents a sorted set which score is unixstamp

func NewRecord

func NewRecord(key string, member interface{}, unixstamp int64) Record

type Repository

type Repository interface {
	// Name returns database name
	Name() string
	// Insert inserts new records
	Insert(tables ...Table) error
	// Update updates specific fields of record
	Update(table Table, fields ...string) error
	// UpdateByIndexScore updates specific fields of records which satify index score range
	UpdateByIndexScore(table Table, index Index, minScore, maxScore int64, fields ...string) (RangeResult, error)
	// Find gets records all fields by keys and stores loaded data to container
	Find(keys KeyList, container TableListContainer, opts ...GetOption) error
	// FindFields gets records specific fields by keys and stores loaded data to container
	FindFields(keys KeyList, container TableListContainer, fields ...string) error
	FindByIndexScore(index Index, minScore, maxScore int64, container TableListContainer, opts ...GetOption) error
	FindFieldsByIndexScore(index Index, minScore, maxScore int64, container TableListContainer, fields ...string) error
	// Get gets one record all fields
	Get(table Table, opts ...GetOption) (bool, error)
	// GetFields gets one record specific fields
	GetFields(table Table, fields ...string) (bool, error)
	// Remove removes one record
	Remove(table ReadonlyTable) error
	// RemoveRecords removes records by keys
	RemoveRecords(meta TableMeta, keys ...interface{}) error
	// Clear removes all records of table
	Clear(table string) error
	// FindView loads view by keys and stores loaded data to container
	FindView(view View, keys KeyList, container TableListContainer, opts ...GetOption) error
	// IndexRank gets rank of table key in index, returns InvalidRank if key not found
	IndexRank(index Index, key interface{}) (int64, error)
	// IndexScore gets score of table key in index, returns InvalidScore if key not found
	IndexScore(index Index, key interface{}) (int64, error)
	// IndexRange ranges index by rank
	IndexRange(index Index, start, stop int64, opts ...RangeOption) (RangeResult, error)
	// IndexRangeByScore ranges index by score
	IndexRangeByScore(index Index, min, max int64, opts ...RangeOption) (RangeResult, error)
	// IndexRangeByLex ranges index by lexicographical
	IndexRangeByLex(index Index, min, max string, opts ...RangeOption) (RangeLexResult, error)
	// AddRecord adds a record with timestamp
	AddRecord(key string, member interface{}, unixstamp int64) error
	GetRecordsByTime(key string, startUnixstamp, endUnixstamp int64) (RangeResult, error)
	GetRecordsByPage(key string, pageSize int, startRank int64) (RangeResult, error)
}

Repository represents APIs for operating database and/or cache

type Session

type Session interface {
	Tx
	Repository
	Cache() CacheProxySession
	Database() DatabaseProxySession
}

type StringKeys

type StringKeys []string

func ToStringKeys

func ToStringKeys(keys KeyList, filter func(string) bool) StringKeys

ToInt64Keys convert KeyList to an StringKeys which satify filter all keys would be contained if filter is nil

func (StringKeys) Key

func (keys StringKeys) Key(i int) interface{}

func (StringKeys) Len

func (keys StringKeys) Len() int

type Table

type Table interface {
	ReadonlyTable
	SetKey(string) error
	FieldSetter
}

Table represents a table in sql database, and hash table in nosql database

type TableListContainer

type TableListContainer interface {
	TableMeta() TableMeta
	Len() int
	New(tableName string, index int, key string) (Table, error)
}

TableListContainer is a linear container holds and creates Table

type TableMeta

type TableMeta interface {
	// Name returns name of table
	Name() string
	// Key returns name of key field
	Key() string
	// Fields returns names of all fields except key field
	Fields() []string
}

TableMeta holds table meta information

type Tx

type Tx interface {
	Begin() error
	Commit() error
	Rollback() error
	Close()
}

type Uint16Keys

type Uint16Keys []uint16

func (Uint16Keys) Key

func (keys Uint16Keys) Key(i int) interface{}

func (Uint16Keys) Len

func (keys Uint16Keys) Len() int

type Uint32Keys

type Uint32Keys []uint32

func (Uint32Keys) Key

func (keys Uint32Keys) Key(i int) interface{}

func (Uint32Keys) Len

func (keys Uint32Keys) Len() int

type Uint64Keys

type Uint64Keys []uint64

func (Uint64Keys) Key

func (keys Uint64Keys) Key(i int) interface{}

func (Uint64Keys) Len

func (keys Uint64Keys) Len() int

type Uint8Keys

type Uint8Keys []uint8

func (Uint8Keys) Key

func (keys Uint8Keys) Key(i int) interface{}

func (Uint8Keys) Len

func (keys Uint8Keys) Len() int

type UintKeys

type UintKeys []uint

func (UintKeys) Key

func (keys UintKeys) Key(i int) interface{}

func (UintKeys) Len

func (keys UintKeys) Len() int

type View

type View interface {
	TableMeta() TableMeta
	Fields() FieldList
	Refs() map[string]View
}

View represents a view references a table

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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