teocdbcli

package
v0.0.0-...-2e4d9bc Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2021 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package teocdbcli is the Teonet key-value databaseb service client package.

This package provides types and functions to Send requests and get responce from Teonet key-value database.

Index

Examples

Constants

View Source
const (
	CmdBinary   = 129 // Binary set, get, delete or get list binary {key,value} to/from key-value database
	CmdSet      = 130 // Set (insert or update) text or json {key,value} to key-value database
	CmdGet      = 131 // Get key and send answer with value in text or json format from key-value database
	CmdList     = 132 // List get not completed key and send answer with array of keys in text or json format from key-value database
	CmdDelete   = 138 // Delete key
	CmdFunc     = 139 // Process plugin command(function)
	CmdGetID    = 140 // Get new numeric ID by name
	CmdSetID    = 141 // Set new value of numeric ID by name
	CmdDeleteID = 142 // Delete numeric ID by name
	CmdListBody = 143 // ListBody get not completed key and send answer with array of keys data in json format from key-value database
	CmdSetQueue = 144 // Add value to named queue by key (name of queue)
	CmdGetQueue = 145 // Get first value from named queue by key (name of queue)
)

Key value database commands.

View Source
const (
	CheckUser = 133 // Check and register user
)

Users registrar commands

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyList

type KeyList struct {
	// contains filtered or unexported fields
}

KeyList is strings array of keys

func (*KeyList) Append

func (k *KeyList) Append(keys ...string)

Append one key or range of keys to KeyList keys slice

func (*KeyList) Keys

func (k *KeyList) Keys() []string

Keys return keys string slice

func (*KeyList) Len

func (k *KeyList) Len() int

Len return length of keys array

func (*KeyList) MarshalBinary

func (k *KeyList) MarshalBinary() (data []byte, err error)

MarshalBinary marshal Keylist (string slice) to byte slice with \0x00 separator

func (*KeyList) MarshalJSON

func (k *KeyList) MarshalJSON() (data []byte, err error)

MarshalJSON returns the JSON encoding

func (*KeyList) String

func (k *KeyList) String() string

String return keys in string divided by end of line

func (*KeyList) UnmarshalBinary

func (k *KeyList) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary unmarshal byte slice with \0x00 separator to Keylist (string slice)

type KeyValue

type KeyValue struct {
	Cmd           byte   // Command
	ID            uint32 // Packet id
	Key           string // Key
	Value         []byte // Value
	RequestInJSON bool   // Request packet format
	Err           string // Database error
}

KeyValue is key value packet data (text or binary) used in requests and responce teonet commands.

func (*KeyValue) Empty

func (kv *KeyValue) Empty()

Empty clears KeyValue values to it default values.

func (*KeyValue) MarshalBinary

func (kv *KeyValue) MarshalBinary() (data []byte, err error)

MarshalBinary encodes KeyValue receiver data into binary buffer and returns it in byte slice.

func (*KeyValue) MarshalText

func (kv *KeyValue) MarshalText() (data []byte, err error)

MarshalText encodes KeyValue receiver data into text buffer and returns it in byte slice. Response format for text requests: {key,id,value}

func (*KeyValue) UnmarshalBinary

func (kv *KeyValue) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary decode binary buffer into KeyValue receiver data.

func (*KeyValue) UnmarshalText

func (kv *KeyValue) UnmarshalText(text []byte) (err error)

UnmarshalText decode text or json buffer into KeyValue receiver data structure. Parameters avalable in text request:

{key} {key,id} {key,id,} {key,value} {key,id,value}

Parameters avalable in text request by commands: CmdSet:

{key} {key,value} {key,id,value}

CmdGet:

{key} {key,id}

CmdList:

{key} {key,id}

type Plugin

type Plugin struct {
	ID            uint32   // Packet id
	Name          string   // Plugin name
	Func          string   // Function name
	Params        []string // Function parameters
	RequestInJSON bool     // Request packet format
}

Plugin is plugins function and parameters used in requests and responce teonet commands.

func (*Plugin) Empty

func (p *Plugin) Empty()

Empty clears KeyValue values to it default values.

func (Plugin) MarshalBinary

func (p Plugin) MarshalBinary() (data []byte, err error)

MarshalBinary encodes Plugin receiver data into binary buffer and returns it in byte slice.

func (*Plugin) UnmarshalBinary

func (p *Plugin) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary decode binary buffer into Plugin receiver data.

type TeoConnector

type TeoConnector interface {
	SendTo(peer string, cmd byte, data []byte) (int, error)
	//SendAnswer(pac *teonet.Packet, cmd byte, data []byte) (int, error)
	SendAnswer(pac interface{}, cmd byte, data []byte) (int, error)

	// WaitFrom wait receiving data from peer. The third function parameter is
	// timeout. It may be omitted or contain timeout time of time. Duration type.
	// If timeout parameter is omitted than default timeout value sets to 2 second.
	WaitFrom(from string, cmd byte, ii ...interface{}) <-chan *struct {
		Data []byte
		Err  error
	}
}

TeoConnector is teonet connector interface. It may be servers (*Teonet) or clients (*TeoLNull) connector and must conain SendTo, SendAnswer and WaitFrom methods.

type TeocdbCli

type TeocdbCli struct {
	// contains filtered or unexported fields
}

TeocdbCli is teocdbcli packet receiver.

func New

func New(con TeoConnector, peer ...string) *TeocdbCli

New creates new teocdbcli object. The con parameter is Teonet connection. The peer parameter is teonet teocdb peer name, it sets to 'teo-cdb' by default if parameter omitted. Returned TeocdbCli object used to send requests to the teonet cdb key-value database.

func (*TeocdbCli) Send

func (cdb *TeocdbCli) Send(cmd byte, key string, value ...[]byte) (data []byte, err error)

Send is clients low level api function to send binary command and exequte it in teonet key-value database.

This function sends CmdBinary command (129) to the teocdb teonet service which applay it in teonet key-value database, wait and return answer.

The 'cmd' function parameter may be set to: CmdSet, CmdGet or CmdList:

CmdSet  (130) - Set  <key,value> insert or update key-value to the key-value database
CmdGet  (131) - Get  <key> gets key and return answer with value from the key-value database
CmdList (132) - List <key> gets not completed key and return answer with array of keys from the key-value database

The 'key' parameter should contain key (for Set and Get functions) or beginning of key (for List function).

The 'value' parameter sets for Set command and should be omitted for other commands.

This function returns data slice. For the CmdGet there is any binary or text data which was set with Set command. For the CmdList ther is KeyList data structur it shoul be encoded with the Keylist UnmarshalBinary(data) function:

var keylist KeyList
keylist.UnmarshalBinary(data)
Example (CmdGet)

Send CmdGet (get value) command - read value from teo-cdb by key example.

fmt.Printf("Get value, key: %s\n", key)
data, err := cdb.Send(CmdGet, key)
if err != nil {
	// do something
	fmt.Println("Got answer data: Hello world!")
	return
}
fmt.Printf("Got answer data: %s\n", string(data))
Output:

Get value, key: test.key.919
Got answer data: Hello world!
Example (CmdList)

Send CmdList command - read list of keys from teo-cdb by key example.

listKey := "test.key."
fmt.Printf("Get list, key: %s\n", listKey)
data, err := cdb.Send(CmdList, listKey)
if err != nil {
	// do something
	fmt.Println("Got answer keys: {[test.key.919 test.key.111 test.key.1]}")
	return
}

var keylist KeyList
keylist.UnmarshalBinary(data)
fmt.Printf("Got answer keys: %v\n", keylist)
Output:

Get list, key: test.key.
Got answer keys: {[test.key.919 test.key.111 test.key.1]}
Example (CmdSet)

Send CmdSet (set value) command - save key and value in teo-cdb example.

fmt.Printf("Set key: %s, data: %s\n", key, value)
data, err := cdb.Send(CmdSet, key, []byte(value))
if err != nil {
	// do something
}
fmt.Printf("Got answer data: %v\n", data)
Output:

Set key: test.key.919, data: Hello world!
Got answer data: []

Notes

Directories

Path Synopsis
Package conf is the Teonet config reader based on teocdb key-value databaseb service client package.
Package conf is the Teonet config reader based on teocdb key-value databaseb service client package.

Jump to

Keyboard shortcuts

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