database

package
v0.0.0-...-3c0cf66 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2016 License: Apache-2.0 Imports: 8 Imported by: 0

README

database

database is a package that provides a Generic DB Abstraction layer. It establishes the 2 main interfaces for defininf a new database provider and a new table definition.

DB Providers

In order to add a new database provider, you will be required to implement the following interface:

// DbProvider represents a persistent database provider
type DbProvider interface {
    // Initializes the Database
    DbInit(dbDir, dbFile string) error
    // Closes the database
    DbClose() error
    // Creates the tables if the tables do not already exist in the database
    DbTablesInit(tables []string) error
    // Populates the in-memory table from the database
    DbTableRebuild(table DbTable) error
    // Adds the key/value pair to the table
    DbAdd(table string, key string, value interface{}) error
    //Deletes the key/value pair from the table
    DbDelete(table string, key string) error
    //Retrives the value corresponding to the key from the table
    DbGet(table string, key string, dbTable DbTable) (interface{}, error)
    //Retrieves all values from a table
    DbGetAll(table string, dbTable DbTable) ([]interface{}, error)
}
Current supported DB Providers
  • BoltDB (key/value database)

DB Table

There is a generic table interface that needs to be implemented in order to define a generic table structure.

// DbTable defines basic table operations
type DbTable interface {
    // Creates the backing map
    NewTable()
    // Name of the table as stored in the database
    Name() string
    // Allocates and returns a single value in the table
    NewElement() interface{}
    // Add an value to the in memory table
    Add(k string, v interface{}) error
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BoltDB

type BoltDB struct {
	Name string
	DB   *bolt.DB
}

BoltDB database structure

func NewBoltDBProvider

func NewBoltDBProvider() *BoltDB

NewBoltDBProvider returns a bolt based database that conforms to the DBProvider interface

func (*BoltDB) DbAdd

func (db *BoltDB) DbAdd(table string, key string, value interface{}) (err error)

DbAdd adds a new element to table in Bolt database

func (*BoltDB) DbClose

func (db *BoltDB) DbClose() error

DbClose closes Bolt database

func (*BoltDB) DbDelete

func (db *BoltDB) DbDelete(table string, key string) (err error)

DbDelete deletes an element from table in Bolt database

func (*BoltDB) DbGet

func (db *BoltDB) DbGet(table string, key string, dbTable DbTable) (interface{}, error)

DbGet obtains value by key from table

func (*BoltDB) DbGetAll

func (db *BoltDB) DbGetAll(table string, dbTable DbTable) (elements []interface{}, err error)

DbGetAll gets all elements from specific table

func (*BoltDB) DbInit

func (db *BoltDB) DbInit(dbDir, dbFile string) error

DbInit initialize Bolt database

func (*BoltDB) DbTableRebuild

func (db *BoltDB) DbTableRebuild(table DbTable) error

DbTableRebuild builds bolt table into memory

func (*BoltDB) DbTablesInit

func (db *BoltDB) DbTablesInit(tables []string) (err error)

DbTablesInit initializes list of tables in Bolt

type DbProvider

type DbProvider interface {
	// Initializes the Database
	DbInit(dbDir, dbFile string) error
	// Closes the database
	DbClose() error
	// Creates the tables if the tables do not already exist in the database
	DbTablesInit(tables []string) error
	// Populates the in-memory table from the database
	DbTableRebuild(table DbTable) error
	// Adds the key/value pair to the table
	DbAdd(table string, key string, value interface{}) error
	//Deletes the key/value pair from the table
	DbDelete(table string, key string) error
	//Retrives the value corresponding to the key from the table
	DbGet(table string, key string, dbTable DbTable) (interface{}, error)
	//Retrieves all values from a table
	DbGetAll(table string, dbTable DbTable) ([]interface{}, error)
}

DbProvider represents a persistent database provider

type DbTable

type DbTable interface {
	// Creates the backing map
	NewTable()
	// Name of the table as stored in the database
	Name() string
	// Allocates and returns a single value in the table
	NewElement() interface{}
	// Add an value to the in memory table
	Add(k string, v interface{}) error
}

DbTable defines basic table operations

Jump to

Keyboard shortcuts

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