Documentation
¶
Overview ¶
Package db implements a wrapper over the go-redis/redis.
There may be an attempt to mimic sonic-py-swsssdk to ease porting of code written in python using that SDK to Go Language.
Example:
Initialization:
d, _ := db.NewDB(db.Options { DBNo : db.ConfigDB, InitIndicator : "CONFIG_DB_INITIALIZED", TableNameSeparator: "|", KeySeparator : "|", })
Close:
d.DeleteDB()
No-Transaction SetEntry
tsa := db.TableSpec { Name: "ACL_TABLE" } tsr := db.TableSpec { Name: "ACL_RULE" }
ca := make([]string, 1, 1)
ca[0] = "MyACL1_ACL_IPV4" akey := db.Key { Comp: ca} avalue := db.Value {map[string]string {"ports":"eth0","type":"mirror" }}
d.SetEntry(&tsa, akey, avalue)
GetEntry
avalue, _ := d.GetEntry(&tsa, akey)
GetKeys
keys, _ := d.GetKeys(&tsa);
GetKeysPattern
keys, _ := d.GetKeys(&tsa, akeyPattern);
No-Transaction DeleteEntry
d.DeleteEntry(&tsa, akey)
GetTable
ta, _ := d.GetTable(&tsa)
No-Transaction DeleteTable
d.DeleteTable(&ts)
Transaction
rkey := db.Key { Comp: []string { "MyACL2_ACL_IPV4", "RULE_1" }} rvalue := db.Value { Field: map[string]string { "priority" : "0", "packet_action" : "eth1", }, }
d.StartTx([]db.WatchKeys { {Ts: &tsr, Key: &rkey} }, []*db.TableSpec { &tsa, &tsr })
d.SetEntry( &tsa, akey, avalue) d.SetEntry( &tsr, rkey, rvalue)
e := d.CommitTx()
Transaction Abort
d.StartTx([]db.WatchKeys {}, []*db.TableSpec { &tsa, &tsr }) d.DeleteEntry( &tsa, rkey) d.AbortTx()
Package db implements a wrapper over the go-redis/redis.
Package db implements a wrapper over the go-redis/redis.
Index ¶
- Constants
- type DB
- func (d *DB) AbortTx() error
- func (d *DB) AppendWatchTx(w []WatchKeys, tss []*TableSpec) error
- func (d *DB) CommitTx() error
- func (d *DB) CreateEntry(ts *TableSpec, key Key, value Value) error
- func (d *DB) DeleteDB() error
- func (d *DB) DeleteEntry(ts *TableSpec, key Key) error
- func (d *DB) DeleteEntryFields(ts *TableSpec, key Key, value Value) error
- func (d *DB) DeleteKeys(ts *TableSpec, key Key) error
- func (d *DB) DeleteMapAll(ts *TableSpec) error
- func (d *DB) DeleteTable(ts *TableSpec) error
- func (d *DB) GetEntry(ts *TableSpec, key Key) (Value, error)
- func (d *DB) GetKeys(ts *TableSpec) ([]Key, error)
- func (d *DB) GetKeysByPattern(ts *TableSpec, pattern string) ([]Key, error)
- func (d *DB) GetKeysPattern(ts *TableSpec, pat Key) ([]Key, error)
- func (d *DB) GetMap(ts *TableSpec, mapKey string) (string, error)
- func (d *DB) GetMapAll(ts *TableSpec) (Value, error)
- func (d *DB) GetTable(ts *TableSpec) (Table, error)
- func (d *DB) ModEntry(ts *TableSpec, key Key, value Value) error
- func (d *DB) Publish(channel string, message interface{}) error
- func (d *DB) SetEntry(ts *TableSpec, key Key, value Value) error
- func (d *DB) SetMap(ts *TableSpec, mapKey string, mapValue string) error
- func (d *DB) StartTx(w []WatchKeys, tss []*TableSpec) error
- func (d DB) String() string
- func (d *DB) UnsubscribeDB() error
- type DBNum
- type HFunc
- type Key
- type Options
- type SEvent
- type SKey
- type Table
- type TableSpec
- type Value
- func (v *Value) Get(name string) string
- func (v *Value) GetInt(name string) (int, error)
- func (v *Value) GetList(name string) []string
- func (v *Value) Has(name string) bool
- func (v *Value) IsPopulated() bool
- func (v *Value) Remove(name string)
- func (v *Value) Set(name, value string)
- func (v *Value) SetInt(name string, value int)
- func (v *Value) SetList(name string, items []string)
- func (v Value) String() string
- type WatchKeys
Constants ¶
const ( DefaultRedisUNIXSocket string = "/var/run/redis/redis.sock" DefaultRedisLocalTCPEP string = "localhost:6379" DefaultRedisRemoteTCPEP string = "127.0.0.1:6379" )
const (
InitialTxPipelineSize int = 100
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DB ¶
type DB struct { Opts *Options // contains filtered or unexported fields }
DB is the main type.
func SubscribeDB ¶
SubscribeDB is the factory method to create a subscription to the DB. The returned instance can only be used for Subscription.
func (*DB) CreateEntry ¶
CreateEntry creates an entry(row) in the table.
func (*DB) DeleteEntry ¶
DeleteEntry deletes an entry(row) in the table.
func (*DB) DeleteEntryFields ¶
DeleteEntryFields deletes some fields/columns in an entry(row) in the table.
func (*DB) DeleteKeys ¶
DeleteKeys deletes all entry/row keys matching a pattern.
func (*DB) DeleteMapAll ¶
For Testing only. Do Not Use!!! There is no transaction support on these.
func (*DB) DeleteTable ¶
DeleteTable deletes the entire table.
func (*DB) GetKeysByPattern ¶
GetKeysByPattern retrieves all entry/row keysi matching with the given pattern.
func (*DB) SetMap ¶
For Testing only. Do Not Use!!! ============================== There is no transaction support on these.
func (*DB) UnsubscribeDB ¶
UnsubscribeDB is used to close a DB subscription
type Key ¶
type Key struct {
Comp []string
}
Key gives the key components. (Eg: { Comp : [] string { "acl1", "rule1" } } ).
type Options ¶
type Options struct { DBNo DBNum InitIndicator string TableNameSeparator string KeySeparator string IsWriteDisabled bool //Indicated if write is allowed DisableCVLCheck bool }
Options gives parameters for opening the redis client.
type SEvent ¶
type SEvent int
const ( SEventNone SEvent = iota // No Op SEventHSet // HSET, HMSET, and its variants SEventHDel // HDEL, also SEventDel generated, if HASH is becomes empty SEventDel // DEL, & also if key gets deleted (empty HASH, expire,..) SEventOther // Some other command not covered above. // The below two are always sent regardless of SEMap. SEventClose // Close requested due to Unsubscribe() called. SEventErr // Error condition. Call Unsubscribe, after return. )
type SKey ¶
type SKey struct { Ts *TableSpec Key *Key SEMap map[SEvent]bool // nil map indicates subscribe to all }
SKey is (TableSpec, Key, []SEvent) 3-tuples to be watched in a Transaction.
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
type TableSpec ¶
type TableSpec struct { Name string // https://github.com/project-arlo/sonic-mgmt-framework/issues/29 // CompCt tells how many components in the key. Only the last component // can have TableSeparator as part of the key. Otherwise, we cannot // tell where the key component begins. CompCt int }
TableSpec gives the name of the table, and other per-table customizations. (Eg: { Name: ACL_TABLE" }).
func Tables2TableSpecs ¶
Convenience function to make TableSpecs from strings. This only works on Tables having key components without TableSeparator as part of the key.
type Value ¶
Value gives the fields as a map. (Eg: { Field: map[string]string { "type" : "l3v6", "ports" : "eth0" } } ).
func (*Value) Get ¶
Get returns the value of a field. Returns empty string if the field does not exists. Use Has() function to check existance of field.
func (*Value) GetInt ¶
GetInt returns value of a field as int. Returns 0 if the field does not exists. Returns an error if the field value is not a number.
func (*Value) GetList ¶
GetList returns the value of a an array field. A "@" suffix is automatically appended to the field name if not present (as per swsssdk convention). Field value is split by comma and resulting slice is returned. Empty slice is returned if field not exists.