kvbase

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2020 License: GPL-3.0 Imports: 13 Imported by: 0

README

kvbase

build report coverage documentation license version

A simple abstraction library for key value stores.

Currently supported stores:

Getting Started

Installing

To start using kvbase, install Go and run go get:

$ go get github.com/Wolveix/kvbase/..

This will retrieve the library. You can interact with each supported store in the exact same way, simply swap out the New initialisation.


Opening a database

All databases are stored within a Backend interface, and have the following functions available:

  • Count(bucket string) (int, error)
  • Create(bucket string, key string, model interface{}) error
  • Delete(bucket string, key string) error
  • Drop(bucket string) error
  • Get(bucket string, model interface{}) (*map[string]interface{}, error)
  • Read(bucket string, key string, model interface{}) error
  • Update(bucket string, key string, model interface{}) error

To open a database instance, call the package with New followed by the type. E.g: kvbase.NewBadgerDB("data", false):

package main

import (
	"log"

	"github.com/Wolveix/kvbase"
)

func main() {
    db, err := kvbase.NewBadgerDB("data", false)
    if err != nil {
        log.Fatal(err)
    }
}

These functions expect a source to be specified. Some drivers utilize a file, others utilize a folder. Not all backends require the boolean value after the source (this value enables in-memory mode, disabling persistent database storage).


Counting entries within a bucket

The Count() function expects a bucket (as a string),:

counter, err := db.Count("users")
if err != nil {
    log.Fatal(err)
}

fmt.Print(counter) //This will output 1.
Creating an entry

The Create() function expects a bucket (as a string), a key (as a string) and a struct containing your data (as an interface{}):

type User struct {
	Password string
	Username string
}

user := User{
    "Password123",
    "JohnSmith123",
}

if err := db.Create("users", user.Username, &user); err != nil {
    log.Fatal(err)
}

If the key already exists, this will fail.

Deleting an entry

The Delete() function expects a bucket (as a string), a key (as a string):

if err := db.Delete("users", "JohnSmith01"); err != nil {
    log.Fatal(err)
}
Dropping a bucket

The Drop() function expects a bucket (as a string):

if err := db.Drop("users"); err != nil {
    log.Fatal(err)
}
Getting all entries

The Get() function expects a bucket (as a string), and a struct to unmarshal your data into (as an interface{}):

results, err := db.Get("users", User{})
if err != nil {
    log.Fatal(err)
}

s, _ := json.MarshalIndent(results, "", "\t")
fmt.Print(string(s))

results will now contain a *map[string]interface{} object. Note that the object doesn't support indexing, so results["JohnSmith01"] won't work; however, you can loop through the map to find specific keys.

Reading an entry

The Read() function expects a bucket (as a string), a key (as a string) and a struct to unmarshal your data into (as an interface{}):

user := User{}

if err := db.Read("users", "JohnSmith01", &user); err != nil {
    log.Fatal(err)
}

fmt.Print(user.Password) //This will output Password123
Updating an entry

The Update() function expects a bucket (as a string), a key (as a string) and a struct containing your data (as an interface{}):

user := User{
    "Password456",
    "JohnSmith123",
}

if err := db.Update("users", user.Username, &user); err != nil {
    log.Fatal(err)
}

If the key doesn't already exist, this will fail.


Credits

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backend

type Backend interface {
	Count(bucket string) (int, error)
	Create(bucket string, key string, model interface{}) error
	Delete(bucket string, key string) error
	Drop(bucket string) error
	Get(bucket string, model interface{}) (*map[string]interface{}, error)
	Read(bucket string, key string, model interface{}) error
	Update(bucket string, key string, model interface{}) error
}

Backend implements all common backend functions

func NewBadgerDB

func NewBadgerDB(source string, memory bool) (Backend, error)

NewBadgerDB initialises a new database using the BadgerDB driver

func NewBboltDB

func NewBboltDB(source string) (Backend, error)

NewBboltDB initialises a new database using the BboltDB driver

func NewBitcaskDB

func NewBitcaskDB(source string) (Backend, error)

NewBitcaskDB initialises a new database using the BitcaskDB driver

func NewBoltDB

func NewBoltDB(source string) (Backend, error)

NewBoltDB initialises a new database using the BoltDB driver

func NewDiskvDB

func NewDiskvDB(source string) (Backend, error)

NewDiskvDB initialises a new database using the DiskvDB driver

func NewGoCache

func NewGoCache(source string, memory bool) (Backend, error)

NewGoCache initialises a new database using the GoCache driver

func NewLevelDB

func NewLevelDB(source string) (Backend, error)

NewLevelDB initialises a new database using the LevelDB driver

type BadgerBackend

type BadgerBackend struct {
	Backend
	Connection *badger.DB
	Memory     bool
	Source     string
}

BadgerBackend acts as a wrapper around a Backend interface

func (*BadgerBackend) Count

func (database *BadgerBackend) Count(bucket string) (int, error)

Count returns the total number of records inside of the provided bucket

func (*BadgerBackend) Create

func (database *BadgerBackend) Create(bucket string, key string, model interface{}) error

Create inserts a record into the backend

func (*BadgerBackend) Delete

func (database *BadgerBackend) Delete(bucket string, key string) error

Delete removes a record from the backend

func (*BadgerBackend) Drop

func (database *BadgerBackend) Drop(bucket string) error

Drop deletes a bucket (and all of its contents) from the backend

func (*BadgerBackend) Get

func (database *BadgerBackend) Get(bucket string, model interface{}) (*map[string]interface{}, error)

Get returns all records inside of the provided bucket

func (*BadgerBackend) Read

func (database *BadgerBackend) Read(bucket string, key string, model interface{}) error

Read returns a single struct from the provided bucket, using the provided key

func (*BadgerBackend) Update

func (database *BadgerBackend) Update(bucket string, key string, model interface{}) error

Update modifies an existing record from the backend, inside of the provided bucket, using the provided key

type BboltBackend

type BboltBackend struct {
	Backend
	Connection *bbolt.DB
	Source     string
}

BboltBackend acts as a wrapper around a Backend interface

func (*BboltBackend) Count

func (database *BboltBackend) Count(bucket string) (int, error)

Count returns the total number of records inside of the provided bucket

func (*BboltBackend) Create

func (database *BboltBackend) Create(bucket string, key string, model interface{}) error

Create inserts a record into the backend

func (*BboltBackend) Delete

func (database *BboltBackend) Delete(bucket string, key string) error

Delete removes a record from the backend

func (*BboltBackend) Drop

func (database *BboltBackend) Drop(bucket string) error

Drop deletes a bucket (and all of its contents) from the backend

func (*BboltBackend) Get

func (database *BboltBackend) Get(bucket string, model interface{}) (*map[string]interface{}, error)

Get returns all records inside of the provided bucket

func (*BboltBackend) Read

func (database *BboltBackend) Read(bucket string, key string, model interface{}) error

Read returns a single struct from the provided bucket, using the provided key

func (*BboltBackend) Update

func (database *BboltBackend) Update(bucket string, key string, model interface{}) error

Update modifies an existing record from the backend, inside of the provided bucket, using the provided key

type BitcaskBackend

type BitcaskBackend struct {
	Backend
	Connection *bitcask.Bitcask
	Source     string
}

BitcaskBackend acts as a wrapper around a Backend interface

func (*BitcaskBackend) Count

func (database *BitcaskBackend) Count(bucket string) (int, error)

Count returns the total number of records inside of the provided bucket

func (*BitcaskBackend) Create

func (database *BitcaskBackend) Create(bucket string, key string, model interface{}) error

Create inserts a record into the backend

func (*BitcaskBackend) Delete

func (database *BitcaskBackend) Delete(bucket string, key string) error

Delete removes a record from the backend

func (*BitcaskBackend) Drop

func (database *BitcaskBackend) Drop(bucket string) error

Drop deletes a bucket (and all of its contents) from the backend

func (*BitcaskBackend) Get

func (database *BitcaskBackend) Get(bucket string, model interface{}) (*map[string]interface{}, error)

Get returns all records inside of the provided bucket

func (*BitcaskBackend) Read

func (database *BitcaskBackend) Read(bucket string, key string, model interface{}) error

Read returns a single struct from the provided bucket, using the provided key

func (*BitcaskBackend) Update

func (database *BitcaskBackend) Update(bucket string, key string, model interface{}) error

Update modifies an existing record from the backend, inside of the provided bucket, using the provided key

type BoltBackend

type BoltBackend struct {
	Backend
	Connection *bolt.DB
	Source     string
}

BoltBackend acts as a wrapper around a Backend interface

func (*BoltBackend) Count

func (database *BoltBackend) Count(bucket string) (int, error)

Count returns the total number of records inside of the provided bucket

func (*BoltBackend) Create

func (database *BoltBackend) Create(bucket string, key string, model interface{}) error

Create inserts a record into the backend

func (*BoltBackend) Delete

func (database *BoltBackend) Delete(bucket string, key string) error

Delete removes a record from the backend

func (*BoltBackend) Drop

func (database *BoltBackend) Drop(bucket string) error

Drop deletes a bucket (and all of its contents) from the backend

func (*BoltBackend) Get

func (database *BoltBackend) Get(bucket string, model interface{}) (*map[string]interface{}, error)

Get returns all records inside of the provided bucket

func (*BoltBackend) Read

func (database *BoltBackend) Read(bucket string, key string, model interface{}) error

Read returns a single struct from the provided bucket, using the provided key

func (*BoltBackend) Update

func (database *BoltBackend) Update(bucket string, key string, model interface{}) error

Update modifies an existing record from the backend, inside of the provided bucket, using the provided key

type DiskvBackend

type DiskvBackend struct {
	Backend
	Connection *diskv.Diskv
	Source     string
}

DiskvBackend acts as a wrapper around a Backend interface

func (*DiskvBackend) Count

func (database *DiskvBackend) Count(bucket string) (int, error)

Count returns the total number of records inside of the provided bucket

func (*DiskvBackend) Create

func (database *DiskvBackend) Create(bucket string, key string, model interface{}) error

Create inserts a record into the backend

func (*DiskvBackend) Delete

func (database *DiskvBackend) Delete(bucket string, key string) error

Delete removes a record from the backend

func (*DiskvBackend) Drop

func (database *DiskvBackend) Drop(bucket string) error

Drop deletes a bucket (and all of its contents) from the backend

func (*DiskvBackend) Get

func (database *DiskvBackend) Get(bucket string, model interface{}) (*map[string]interface{}, error)

Get returns all records inside of the provided bucket

func (*DiskvBackend) Read

func (database *DiskvBackend) Read(bucket string, key string, model interface{}) error

Read returns a single struct from the provided bucket, using the provided key

func (*DiskvBackend) Update

func (database *DiskvBackend) Update(bucket string, key string, model interface{}) error

Update modifies an existing record from the backend, inside of the provided bucket, using the provided key

type GoCacheBackend

type GoCacheBackend struct {
	Backend
	Connection *cache.Cache
	Memory     bool
	Mux        sync.RWMutex
	Source     string
}

GoCacheBackend acts as a wrapper around a Backend interface

func (*GoCacheBackend) Count

func (database *GoCacheBackend) Count(bucket string) (int, error)

Count returns the total number of records inside of the provided bucket

func (*GoCacheBackend) Create

func (database *GoCacheBackend) Create(bucket string, key string, model interface{}) error

Create inserts a record into the backend

func (*GoCacheBackend) Delete

func (database *GoCacheBackend) Delete(bucket string, key string) error

Delete removes a record from the backend

func (*GoCacheBackend) Drop

func (database *GoCacheBackend) Drop(bucket string) error

Drop deletes a bucket (and all of its contents) from the backend

func (*GoCacheBackend) Get

func (database *GoCacheBackend) Get(bucket string, model interface{}) (*map[string]interface{}, error)

Get returns all records inside of the provided bucket

func (*GoCacheBackend) Read

func (database *GoCacheBackend) Read(bucket string, key string, model interface{}) error

Read returns a single struct from the provided bucket, using the provided key

func (*GoCacheBackend) Update

func (database *GoCacheBackend) Update(bucket string, key string, model interface{}) error

Update modifies an existing record from the backend, inside of the provided bucket, using the provided key

type LevelBackend

type LevelBackend struct {
	Backend
	Connection *leveldb.DB
	Source     string
}

LevelBackend acts as a wrapper around a Backend interface

func (*LevelBackend) Count

func (database *LevelBackend) Count(bucket string) (int, error)

Count returns the total number of records inside of the provided bucket

func (*LevelBackend) Create

func (database *LevelBackend) Create(bucket string, key string, model interface{}) error

Create inserts a record into the backend

func (*LevelBackend) Delete

func (database *LevelBackend) Delete(bucket string, key string) error

Delete removes a record from the backend

func (*LevelBackend) Drop

func (database *LevelBackend) Drop(bucket string) error

Drop deletes a bucket (and all of its contents) from the backend

func (*LevelBackend) Get

func (database *LevelBackend) Get(bucket string, model interface{}) (*map[string]interface{}, error)

Get returns all records inside of the provided bucket

func (*LevelBackend) Read

func (database *LevelBackend) Read(bucket string, key string, model interface{}) error

Read returns a single struct from the provided bucket, using the provided key

func (*LevelBackend) Update

func (database *LevelBackend) Update(bucket string, key string, model interface{}) error

Update modifies an existing record from the backend, inside of the provided bucket, using the provided key

Jump to

Keyboard shortcuts

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