db

package
v0.0.0-...-562cd84 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2020 License: Apache-2.0 Imports: 8 Imported by: 0

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

View Source
const (
	DefaultRedisUNIXSocket  string = "/var/run/redis/redis.sock"
	DefaultRedisLocalTCPEP  string = "localhost:6379"
	DefaultRedisRemoteTCPEP string = "127.0.0.1:6379"
)
View Source
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 NewDB

func NewDB(opt Options) (*DB, error)

NewDB is the factory method to create new DB's.

func SubscribeDB

func SubscribeDB(opt Options, skeys []*SKey, handler HFunc) (*DB, error)

SubscribeDB is the factory method to create a subscription to the DB. The returned instance can only be used for Subscription.

func (*DB) AbortTx

func (d *DB) AbortTx() error

AbortTx method is used by infra to abort a check-and-set Transaction.

func (*DB) AppendWatchTx

func (d *DB) AppendWatchTx(w []WatchKeys, tss []*TableSpec) error

func (*DB) CommitTx

func (d *DB) CommitTx() error

CommitTx method is used by infra to commit a check-and-set Transaction.

func (*DB) CreateEntry

func (d *DB) CreateEntry(ts *TableSpec, key Key, value Value) error

CreateEntry creates an entry(row) in the table.

func (*DB) DeleteDB

func (d *DB) DeleteDB() error

DeleteDB is the gentle way to close the DB connection.

func (*DB) DeleteEntry

func (d *DB) DeleteEntry(ts *TableSpec, key Key) error

DeleteEntry deletes an entry(row) in the table.

func (*DB) DeleteEntryFields

func (d *DB) DeleteEntryFields(ts *TableSpec, key Key, value Value) error

DeleteEntryFields deletes some fields/columns in an entry(row) in the table.

func (*DB) DeleteKeys

func (d *DB) DeleteKeys(ts *TableSpec, key Key) error

DeleteKeys deletes all entry/row keys matching a pattern.

func (*DB) DeleteMapAll

func (d *DB) DeleteMapAll(ts *TableSpec) error

For Testing only. Do Not Use!!! There is no transaction support on these.

func (*DB) DeleteTable

func (d *DB) DeleteTable(ts *TableSpec) error

DeleteTable deletes the entire table.

func (*DB) GetEntry

func (d *DB) GetEntry(ts *TableSpec, key Key) (Value, error)

GetEntry retrieves an entry(row) from the table.

func (*DB) GetKeys

func (d *DB) GetKeys(ts *TableSpec) ([]Key, error)

GetKeys retrieves all entry/row keys.

func (*DB) GetKeysByPattern

func (d *DB) GetKeysByPattern(ts *TableSpec, pattern string) ([]Key, error)

GetKeysByPattern retrieves all entry/row keysi matching with the given pattern.

func (*DB) GetKeysPattern

func (d *DB) GetKeysPattern(ts *TableSpec, pat Key) ([]Key, error)

func (*DB) GetMap

func (d *DB) GetMap(ts *TableSpec, mapKey string) (string, error)

func (*DB) GetMapAll

func (d *DB) GetMapAll(ts *TableSpec) (Value, error)

func (*DB) GetTable

func (d *DB) GetTable(ts *TableSpec) (Table, error)

GetTable gets the entire table.

func (*DB) ModEntry

func (d *DB) ModEntry(ts *TableSpec, key Key, value Value) error

ModEntry modifies an entry(row) in the table.

func (*DB) Publish

func (d *DB) Publish(channel string, message interface{}) error

func (*DB) SetEntry

func (d *DB) SetEntry(ts *TableSpec, key Key, value Value) error

SetEntry sets an entry(row) in the table.

func (*DB) SetMap

func (d *DB) SetMap(ts *TableSpec, mapKey string, mapValue string) error

For Testing only. Do Not Use!!! ============================== There is no transaction support on these.

func (*DB) StartTx

func (d *DB) StartTx(w []WatchKeys, tss []*TableSpec) error

StartTx method is used by infra to start a check-and-set Transaction.

func (DB) String

func (d DB) String() string

func (*DB) UnsubscribeDB

func (d *DB) UnsubscribeDB() error

UnsubscribeDB is used to close a DB subscription

type DBNum

type DBNum int

DBNum type indicates the type of DB (Eg: ConfigDB, ApplDB, ...).

const (
	ApplDB        DBNum = iota // 0
	AsicDB                     // 1
	CountersDB                 // 2
	LogLevelDB                 // 3
	ConfigDB                   // 4
	FlexCounterDB              // 5
	StateDB                    // 6
	SnmpDB                     // 7
	ErrorDB                    // 8
	UserDB                     // 9
	// All DBs added above this line, please ----
	MaxDB //  The Number of DBs
)

func (DBNum) String

func (dbNo DBNum) String() string

type HFunc

type HFunc func(*DB, *SKey, *Key, SEvent) error

HFunc gives the name of the table, and other per-table customizations.

type Key

type Key struct {
	Comp []string
}

Key gives the key components. (Eg: { Comp : [] string { "acl1", "rule1" } } ).

func (*Key) Get

func (k *Key) Get(index int) string

Get returns the key component at given index

func (*Key) Len

func (k *Key) Len() int

Len returns number of components in the Key

func (Key) String

func (k Key) String() string

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.

func (Options) String

func (o Options) String() string

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
}

func (*Table) GetEntry

func (t *Table) GetEntry(key Key) (Value, error)

GetEntry method retrieves an entry/row from a previously read table.

func (*Table) GetKeys

func (t *Table) GetKeys() ([]Key, error)

GetKeys method retrieves all entry/row keys from a previously read table.

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

func Tables2TableSpecs(tables []string) []*TableSpec

Convenience function to make TableSpecs from strings. This only works on Tables having key components without TableSeparator as part of the key.

type Value

type Value struct {
	Field map[string]string
}

Value gives the fields as a map. (Eg: { Field: map[string]string { "type" : "l3v6", "ports" : "eth0" } } ).

func (*Value) Get

func (v *Value) Get(name string) string

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

func (v *Value) GetInt(name string) (int, error)

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

func (v *Value) GetList(name string) []string

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.

func (*Value) Has

func (v *Value) Has(name string) bool

Has function checks if a field exists.

func (*Value) IsPopulated

func (v *Value) IsPopulated() bool

func (*Value) Remove

func (v *Value) Remove(name string)

Remove function removes a field from this Value.

func (*Value) Set

func (v *Value) Set(name, value string)

Set function sets a string value for a field.

func (*Value) SetInt

func (v *Value) SetInt(name string, value int)

SetInt sets an integer value for a field.

func (*Value) SetList

func (v *Value) SetList(name string, items []string)

SetList function sets an list value to a field. Field name and value are formatted as per swsssdk conventions: - A "@" suffix is appended to key name - Field value is the comma separated string of list items

func (Value) String

func (v Value) String() string

type WatchKeys

type WatchKeys struct {
	Ts  *TableSpec
	Key *Key
}

WatchKeys is array of (TableSpec, Key) tuples to be watched in a Transaction.

func (WatchKeys) String

func (w WatchKeys) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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