Documentation ¶
Index ¶
- Variables
- func GetMockTankNewFunc(mt *MockTank) func() Tank
- func MockCombineCondition(c *Condition) interface{}
- type ChangeInfo
- type Condition
- func (c *Condition) AddOp(t ConditionType, key string, value interface{}) (r *Condition)
- func (c *Condition) And(s *Condition) *Condition
- func (c *Condition) Combine(leafFunc func(*Condition) interface{}, ...) interface{}
- func (c *Condition) Not() *Condition
- func (c *Condition) Or(s *Condition) *Condition
- type ConditionPair
- type ConditionType
- type DBInfo
- type Event
- type EventType
- type GetNewTank
- type M
- type MockTank
- func (mt *MockTank) Close()
- func (mt *MockTank) Count() Tank
- func (mt *MockTank) Databases() Tank
- func (mt *MockTank) Distinct(key string) Tank
- func (mt *MockTank) Filter(cond *Condition, args ...interface{}) Tank
- func (mt *MockTank) From(name string) Tank
- func (mt *MockTank) GetChangeInfo() *ChangeInfo
- func (mt *MockTank) GetError() error
- func (mt *MockTank) GetLen() int
- func (mt *MockTank) GetTableV() Tank
- func (mt *MockTank) GetValue() []interface{}
- func (mt *MockTank) Index(key ...string) Tank
- func (mt *MockTank) Insert(data ...M) Tank
- func (mt *MockTank) Limit(n int) Tank
- func (mt *MockTank) Offset(n int) Tank
- func (mt *MockTank) OrderBy(key ...string) Tank
- func (mt *MockTank) Query(args ...interface{}) Tank
- func (mt *MockTank) Remove(args ...interface{}) Tank
- func (mt *MockTank) RemoveAll(args ...interface{}) Tank
- func (mt *MockTank) Select(key ...string) Tank
- func (mt *MockTank) SetTableV(data interface{}) Tank
- func (mt *MockTank) Tables() Tank
- func (mt *MockTank) Update(data M, args ...interface{}) Tank
- func (mt *MockTank) UpdateAll(data M, args ...interface{}) Tank
- func (mt *MockTank) Upsert(data M, args ...interface{}) Tank
- func (mt *MockTank) Using(name string) Tank
- func (mt *MockTank) Watch(opts *WatchOptions) (chan *Event, context.CancelFunc)
- type OperationType
- type Tank
- type WatchOptions
Constants ¶
This section is empty.
Variables ¶
var ( EventWatchBreak = &Event{Type: Brk, Value: nil} EventWatchBreakBytes, _ = json.Marshal(EventWatchBreak) )
var ( // BaseCondition is a True node of condition chain // and can be called like: operator.BaseCondition.AddOp(cType, key, value) // then the func will return a new Condition which contains a condition chain // with a BaseCondition head. // BaseCondition -> Condition1 -> Condition2 // -> Condition3 // -> Condition4 // See the Condition.AddOp() for details // ATTENTION: BaseCondition is out of condition tree, except the tree is empty(then will be always true). BaseCondition = NewCondition(Tr, M(nil)) )
Functions ¶
func GetMockTankNewFunc ¶
implements type GetNewTank, return a mock function which will return the provided mock tank
func MockCombineCondition ¶
func MockCombineCondition(c *Condition) interface{}
return the result of Condition Combine with mockLeafFunc and mockBranchFunc
Types ¶
type ChangeInfo ¶
type Condition ¶
type Condition struct { Type ConditionType Value interface{} // contains filtered or unexported fields }
Condition describe the conditions of database operations It is a chain-tree structure: basically a tree but every node is a chain. There is two types of Condition according to Condition.Value:
- leaf node: whose value is operator.M, the actual condition
- branch node: whose value contains 2 types:
- Condition: "Not" operation build it, has 1 child node
- ConditionPair: "And/Or" operation build it, has 2 child node
Let Cm = leaf node, Cc = branch node(Condition), Cp = branch node(Condition Pair) See the following struct:
--------------------- | Cm1 -> Cp1 -> Cm2 | ----------|---------- |(and/or) _________|___________ | |
-------------- --------------------- | Cm3 -> Cm4 | | Cc1 -> Cm5 -> Cc2 | -------------- ---|-------------|---
|(not) |(not) ------- -------------- | Cm6 | | Cm7 -> Cm8 | ------- --------------
Every "block" contains 1 "chain". Every "chain" contains some condition "node" Every "node": contains 3 types:
- Cm(leaf node): actual condition
- Cp(branch node): and/or operation, link to 2 "block"
- Cc(branch node): not operation, link to 1 "block"
The condition making is just the process of tree building
func NewCondition ¶
func NewCondition(t ConditionType, v interface{}) (r *Condition)
func (*Condition) AddOp ¶
func (c *Condition) AddOp(t ConditionType, key string, value interface{}) (r *Condition)
return a new Condition linked to the old one, just like append the chain with a new node
func (*Condition) And ¶
return a new Condition which is made up of two Condition with AND if either of then is BaseCondition then return another
func (*Condition) Combine ¶
func (c *Condition) Combine(leafFunc func(*Condition) interface{}, combineFunc func(ConditionType, []interface{}) interface{}) interface{}
Combine provide a common process that combine the Condition tree. Any driver implements the Tank can combine the Condition tree by providing two functions:
- leafFunc: handle the leaf node whose value-type is M and it's the actual condition
- combineFunc: handle the branch node whose value-type is Condition or ConditionPair
Combine should be called before doing any filter-need operation like query, update, remove. And it should be cached by driver to prevent processing every time.
type ConditionPair ¶
type ConditionType ¶
type ConditionType string
const ( Tr ConditionType = "tr" Eq ConditionType = "eq" Ne ConditionType = "ne" Lt ConditionType = "lt" Lte ConditionType = "lte" Gt ConditionType = "gt" Gte ConditionType = "gte" In ConditionType = "in" Nin ConditionType = "nin" Con ConditionType = "contain" Ext ConditionType = "exists" Not ConditionType = "not" And ConditionType = "and" Or ConditionType = "or" )
type GetNewTank ¶
type GetNewTank func() Tank
the method type for getting Tank by providing config name
type MockTank ¶
type MockTank struct { Value []interface{} Length int ChangeInfo *ChangeInfo Err error }
func (*MockTank) GetChangeInfo ¶
func (mt *MockTank) GetChangeInfo() *ChangeInfo
func (*MockTank) Watch ¶
func (mt *MockTank) Watch(opts *WatchOptions) (chan *Event, context.CancelFunc)
type OperationType ¶
type OperationType string
const ( None OperationType = "none" Query OperationType = "query" Insert OperationType = "insert" Upsert OperationType = "upsert" Update OperationType = "update" UpdateAll OperationType = "updateAll" Remove OperationType = "remove" RemoveAll OperationType = "removeAll" Count OperationType = "count" Tables OperationType = "tables" Databases OperationType = "databases" SetTableV OperationType = "setTableV" GetTableV OperationType = "getTableV" Tail OperationType = "tail" )
type Tank ¶
type Tank interface { // Close the connections Close() // Get the operation result value GetValue() []interface{} // Get the value length or count num GetLen() int // Get the changeInfo of update/remove GetChangeInfo() *ChangeInfo // Get the error if existed GetError() error // List databases Databases() Tank // Switch to database, in zk it will be the first layer of tree Using(name string) Tank // List tables Tables() Tank // Set a value to table, like a key-value option // In tree-like database such as zookeeper, it can be use to set value to a provided path, // in others it should be ignored SetTableV(data interface{}) Tank // Get the value of table, set by SetTableV() // in other databases which not support SetTable(), it should be ignored GetTableV() Tank // From tables, in mongodb it will be the collection, in zk it will be the From(name string) Tank // Set distinct key Distinct(key string) Tank // Make the returned value order by key1, key2, key3... and will be reversed if -key1 is given OrderBy(key ...string) Tank // Set select key Select(key ...string) Tank // Set offset Offset(n int) Tank // Set limit Limit(n int) Tank // Set unique index key Index(key ...string) Tank // Add filter by *Condition, multi-liner-filter will be combine with "AND" Filter(cond *Condition, args ...interface{}) Tank // Do the count query Count() Tank // Do the query according to the filter chain before Query(args ...interface{}) Tank // Do the insert with data Insert(data ...M) Tank // Do the update or insert with data according to the filter chain before Upsert(data M, args ...interface{}) Tank // Do the update with data according to the filter chain before, update the first one Update(data M, args ...interface{}) Tank // Do the update and update all matched thing UpdateAll(data M, args ...interface{}) Tank // Do the remove according to the filter chain before, remove the first one Remove(args ...interface{}) Tank // Do the remove and remove all matched thing RemoveAll(args ...interface{}) Tank // Watch table then return a chan Event. Watch(opts *WatchOptions) (chan *Event, context.CancelFunc) }
Tank defined a basic operating unit. It can be called by making a chain of Tank. Every block of the chain should be a new struct which is cloned from last one.
Tank1(filter1)----------> Tank2(filter2)-----------------------------> Tank3(operating) conf1(filter1)----------> conf2(cloned from conf1 and add filter2)---> conf3(cloned from conf2)
So that it can be easy to extend
type WatchOptions ¶
type WatchOptions struct { // Only watch the node itself, including children added, children removed and node value change. // Will not receive existing children's event. SelfOnly bool `json:"selfOnly"` // Max time of events will received. Watch will be ended after the last event. 0 for infinity. MaxEvents uint `json:"maxEvents"` // The max waiting time of each event. Watch will be ended after timeout. 0 for no limit. Timeout time.Duration `json:"timeout"` // The value-change event will be checked if it's different from last status. If not then this event // will be ignored. And it will not trigger timeout reset. MustDiff string `json:"mustDiff"` }