operator

package
v1.19.1 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	EventWatchBreak         = &Event{Type: Brk, Value: nil}
	EventWatchBreakBytes, _ = json.Marshal(EventWatchBreak)
)
View Source
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

func GetMockTankNewFunc(mt *MockTank) func() Tank

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 ChangeInfo struct {
	Updated int
	Removed int
	Matched int
}

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

func (c *Condition) And(s *Condition) *Condition

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.

func (*Condition) Not

func (c *Condition) Not() *Condition

return a new Condition which is made up of itself with NOT if it is BaseCondition then return itself Not(BaseCondition) -> BaseCondition

func (*Condition) Or

func (c *Condition) Or(s *Condition) *Condition

return a new Condition which is made up of two Condition with OR if either of then is BaseCondition then return another

type ConditionPair

type ConditionPair struct {
	First  *Condition
	Second *Condition
}

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 DBInfo

type DBInfo struct {
	Addr           []string
	ConnectTimeout time.Duration
	Username       string
	Password       string

	Database     string
	Mode         int
	ListenerName string
	MaxOpenConn  int
	MaxIdleConn  int
}

type Event

type Event struct {
	Type  EventType `json:"type"`
	Value M         `json:"value"`
}

type EventType

type EventType int32
const (
	Nop EventType = iota
	Add
	Del
	Chg
	SChg
	Brk EventType = -1
)

func (EventType) String

func (et EventType) String() string

type GetNewTank

type GetNewTank func() Tank

the method type for getting Tank by providing config name

type M

type M map[string]interface{}

func (M) Update

func (m M) Update(key string, value interface{}) M

type MockTank

type MockTank struct {
	Value      []interface{}
	Length     int
	ChangeInfo *ChangeInfo
	Err        error
}

func (*MockTank) Close

func (mt *MockTank) Close()

func (*MockTank) Count

func (mt *MockTank) Count() Tank

func (*MockTank) Databases

func (mt *MockTank) Databases() Tank

func (*MockTank) Distinct

func (mt *MockTank) Distinct(key string) Tank

func (*MockTank) Filter

func (mt *MockTank) Filter(cond *Condition, args ...interface{}) Tank

func (*MockTank) From

func (mt *MockTank) From(name string) Tank

func (*MockTank) GetChangeInfo

func (mt *MockTank) GetChangeInfo() *ChangeInfo

func (*MockTank) GetError

func (mt *MockTank) GetError() error

func (*MockTank) GetLen

func (mt *MockTank) GetLen() int

func (*MockTank) GetTableV

func (mt *MockTank) GetTableV() Tank

func (*MockTank) GetValue

func (mt *MockTank) GetValue() []interface{}

func (*MockTank) Index

func (mt *MockTank) Index(key ...string) Tank

func (*MockTank) Insert

func (mt *MockTank) Insert(data ...M) Tank

func (*MockTank) Limit

func (mt *MockTank) Limit(n int) Tank

func (*MockTank) Offset

func (mt *MockTank) Offset(n int) Tank

func (*MockTank) OrderBy

func (mt *MockTank) OrderBy(key ...string) Tank

func (*MockTank) Query

func (mt *MockTank) Query(args ...interface{}) Tank

func (*MockTank) Remove

func (mt *MockTank) Remove(args ...interface{}) Tank

func (*MockTank) RemoveAll

func (mt *MockTank) RemoveAll(args ...interface{}) Tank

func (*MockTank) Select

func (mt *MockTank) Select(key ...string) Tank

func (*MockTank) SetTableV

func (mt *MockTank) SetTableV(data interface{}) Tank

func (*MockTank) Tables

func (mt *MockTank) Tables() Tank

func (*MockTank) Update

func (mt *MockTank) Update(data M, args ...interface{}) Tank

func (*MockTank) UpdateAll

func (mt *MockTank) UpdateAll(data M, args ...interface{}) Tank

func (*MockTank) Upsert

func (mt *MockTank) Upsert(data M, args ...interface{}) Tank

func (*MockTank) Using

func (mt *MockTank) Using(name string) Tank

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"`
}

Jump to

Keyboard shortcuts

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