datastore

package
v0.11.3-pre4 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2017 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package datastore provides data store functionality. The data store is kept in memory, but optionally the data store may be saved to a file to provide a perisistent data store. This uses go-cache (https://github.com/pmylund/go-cache) for storing the data.

The methods that set, get, and delete key/value pairs also take a `keyType` argument that specifies what kind of object it is.

Index

Constants

View Source
const MySQLTimeFormat = "2006-01-02 15:04:05"

Format to use for dates and times for MySQL.

Variables

View Source
var Dbh *sql.DB

Dbh is the database handle, shared around.

Functions

func CheckForOne

func CheckForOne(dbhandle Dbhandle, kind string, name string) (int32, error)

CheckForOne object of the given type identified by the given name. For this function to work, the underlying table MUST have its primary text identifier be called "name".

func ChkNilArray

func ChkNilArray(obj interface{})

ChkNilArray examines an object, searching for empty slices. When restoring an object from either the in-memory data store after it has been saved to disk, or loading an object from the database with gob encoded data structures, empty slices are encoded as "null" when they're sent out as JSON to the client. This makes the client very unhappy, so those empty slices need to be recreated again. Annoying, but it's how it goes.

func ConnectDB

func ConnectDB(dbEngine string, params interface{}) (*sql.DB, error)

ConnectDB connects to a database with the database name and a map of connection options. Currently supports MySQL and PostgreSQL.

func DecodeBlob

func DecodeBlob(data []byte, obj interface{}) error

DecodeBlob decodes the data encoded with EncodeBlob that was stored in the database so it can be loaded back into a goiardi object. The 'obj' in the arguments *must* be the address of the object receiving the blob of data (e.g. datastore.DecodeBlob(data, &obj).

func EncodeBlob

func EncodeBlob(obj interface{}) ([]byte, error)

EncodeBlob encodes a slice or map of goiardi object data to save in the database. Pass the object to be encoded in like datastore.EncodeBlob(&foo.Thing).

func EncodeToJSON

func EncodeToJSON(obj interface{}) (string, error)

EncodeToJSON encodes an object to a JSON string.

func WalkMapForNil

func WalkMapForNil(r interface{}) interface{}

WalkMapForNil walks through the given map, searching for nil slices to create. This does not handle all possible cases, but it *does* handle the cases found with the chef objects in goiardi.

Types

type DataStore

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

DataStore is the main data store struct, holding the key/value store and list of objects.

func New

func New() *DataStore

New creates a new data store instance, or returns an already created one.

func (*DataStore) AllNodeStatuses added in v0.8.0

func (ds *DataStore) AllNodeStatuses(nodeName string) ([]interface{}, error)

AllNodeStatuses returns a list of all statuses known for the given node from the in-memory data store.

func (*DataStore) Delete

func (ds *DataStore) Delete(keyType string, key string)

Delete a value from the data store.

func (*DataStore) DeleteLogInfo

func (ds *DataStore) DeleteLogInfo(id int) error

DeleteLogInfo deletes a logged event from the data store.

func (*DataStore) DeleteNodeStatus added in v0.8.0

func (ds *DataStore) DeleteNodeStatus(nodeName string) error

DeleteNodeStatus deletes all status reports for a node from the in-memory data store.

func (*DataStore) Get

func (ds *DataStore) Get(keyType string, key string) (interface{}, bool)

Get a value of the given type associated with the given key, if it exists.

func (*DataStore) GetList

func (ds *DataStore) GetList(keyType string) []string

GetList returns a list of all objects of the given type.

func (*DataStore) GetLogInfo

func (ds *DataStore) GetLogInfo(id int) (interface{}, error)

GetLogInfo gets a loginfo by id.

func (*DataStore) GetLogInfoList

func (ds *DataStore) GetLogInfoList() map[int]interface{}

GetLogInfoList gets all the log infos currently stored.

func (*DataStore) LatestNodeStatus added in v0.8.0

func (ds *DataStore) LatestNodeStatus(nodeName string) (interface{}, error)

LatestNodeStatus returns the latest status for a node from the in-memory data store.

func (*DataStore) Load

func (ds *DataStore) Load(dsFile string) error

Load the frozen data store from disk.

func (*DataStore) PurgeLogInfoBefore

func (ds *DataStore) PurgeLogInfoBefore(id int) (int64, error)

PurgeLogInfoBefore purges all the logged events with an id less than the one given from the data store.

func (*DataStore) Save

func (ds *DataStore) Save(dsFile string) error

Save freezes and saves the data store to disk.

func (*DataStore) Set

func (ds *DataStore) Set(keyType string, key string, val interface{})

Set a value of the given type with the provided key.

func (*DataStore) SetLogInfo

func (ds *DataStore) SetLogInfo(obj interface{}, logID ...int) error

SetLogInfo sets a loginfo in the data store. Unlike most of these objects, log infos are stored and retrieved by id, since they have no useful names.

func (*DataStore) SetNodeStatus added in v0.8.0

func (ds *DataStore) SetNodeStatus(nodeName string, obj interface{}, nsID ...int) error

SetNodeStatus updates a node's status using the in-memory data store.

type Dbhandle

type Dbhandle interface {
	Prepare(query string) (*sql.Stmt, error)
	QueryRow(query string, args ...interface{}) *sql.Row
	Query(query string, args ...interface{}) (*sql.Rows, error)
	Exec(query string, args ...interface{}) (sql.Result, error)
}

Dbhandle is an interface for db handle types that can execute queries.

type ResRow

type ResRow interface {
	Scan(dest ...interface{}) error
}

ResRow is an interface for rows returned by Query, or a single row returned by QueryRow. Used for passing in a db handle or a transaction to a function.

Jump to

Keyboard shortcuts

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