lokaldb

package module
v0.0.0-...-dfd7f09 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2023 License: MIT Imports: 4 Imported by: 0

README

lokaldb

lokaldb is a wrapper around bbolt key-value database to manage messaging data in a local database for the Go programming language.

Its primary purpose is to persist messages if sending messages to queues like NATS fails.

This is an initial version.

Structures

LokalDB

LokalDB is a wrapper around bbolt key-value database to manage messaging data in a local database.

type LokalDB struct {
    ldb      *bolt.DB
    FileName string
}
ChunkData

ChunkData represents the key-value chunks of data to be used as a result for slices of key-value data.

type ChunkData struct {
    Key   string
    Value []byte
}

Functions

Open (file string) (*LokalDB, error)

Opens a local database file. It creates the file if it does not exist.

// Open a new lokaldb instance
db, err := lokaldb.Open(`test.db`)
if err != nil {
    log.Fatalf("%e", err)
}
Store(bucket string, key string, data []byte) error

Inserts data in the local database. It will update records containing the same key with the current value.

// Store a George in the key `name`
err = db.Store(`default`, `name`, []byte(`George`))
if err != nil {
    log.Println(err.Error())
}
StoreOnce(bucket string, data []ChunkData) error

Inserts data in the local database in one go. It will update records containing the same key with the current value.


kv := []ChunkData {
    ChunkData {
        Key: `beatle1`,
        Value: `John`,
    },
     ChunkData {
        Key: `beatle2`,
        Value: `Paul`,
    },
     ChunkData {
        Key: `beatle3`,
        Value: `George`,
    },
     ChunkData {
        Key: `beatle4`,
        Value: `Ringo`,
    },
}

err = db.StoreOnce(`default`, kv)
if err != nil {
    log.Fatalf("%e", err)
}
Fetch(bucket string, key string) (data []byte, err error)

Gets a single record from the local database with the provided key. If the record does not exist, it will return nil.

b, err = db.Fetch(`default`, `beatle1`)
Delete(bucket string, key string) error

Removes a single record in the database that matches the provided key.

err = db.Delete(`default`, key)
if err != nil {
    log.Fatalf("%e", err)
}
DeleteOnce(bucket string, key []string) error

Remove records in the local database in one go from a supplied provided key.

ks := []string{
    `beatle1`,
    `beatle2`,
    `beatle3`,
    `beatle4`,
}
err = db.DeleteOnce(`default`, ks)
if err != nil {
    log.Fatalf("%e", err)
}
FetchChunkUp(bucket string, max int, offset int) ([]ChunkData, error)

Gets a chunk of data starting from the bottom to top limited by max.

// Get 10 records from bottom to top, no offset
kv1, err = db.FetchChunkUp(`default`, 10, 0)
FetchChunkDown(bucket string, max int, offset int) ([]ChunkData, error)

Gets a chunk of data starting from the top to bottom limited by max.

// Get 10 records from top to bottom, no offset
kv1, err = db.FetchChunkDown(`default`, 10, 0)
FetchDelete(bucket string, key string) ([]byte, error)

Gets the record with the provided key and deletes it.

bf, err = db.FetchDelete(`default`, `nuisance`)
if err != nil {
    log.Fatalf("%e", err)
}
SliceUp(bucket string) (data []byte, err error)

Fetches and deletes a record from bottom to top.

b, err = db.SliceUp(`default`)
if err != nil {
    log.Fatalf("%e", err)
}
SliceDown(bucket string) (data []byte, err error)

Fetches and deletes a record from top to bottom.

b, err = db.SliceDown(`default`)
if err != nil {
    log.Fatalf("%e", err)
}
CutChunkUp(bucket string, max int) ([]ChunkData, error)

Gets a chunk of data starting from bottom to top in descending order and removes them.

b, err = db.CutChunkUp(`default`, 50)
if err != nil {
    log.Fatalf("%e", err)
}
CutChunkDown(bucket string, max int) ([]ChunkData, error)

Gets a chunk of data starting from top to bottom in ascending order and removes them.

b, err = db.CutChunkDown(`default`, 50)
if err != nil {
    log.Fatalf("%e", err)
}
Count(bucket string) (int, error)

Count records in the bucket

Close() error

Close the local database

Examples

Please see more examples of using lokaldb in your projects on the lokaldb_test.go file.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package lokaldb is a wrapper around bbolt key-value database to manage messaging data in a local database Its primary purpose is to be used persist messages if sending queues like NATS fails.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrLocalDatabaseNotYetOpened = errors.New(`local database not yet opened`)
	ErrCorruptedInternalBucket   = errors.New(`empty or corrupted internal bucket`)
	ErrBucketDoesNotExist        = errors.New(`bucket does not exist`)
	ErrNoKeysSet                 = errors.New(`no keys set`)
)

Errors

Functions

This section is empty.

Types

type ChunkData

type ChunkData struct {
	Key   string
	Value []byte
}

ChunkData represents the key-value chunks of data to be used as a result for slices of key-value data

type LokalDB

type LokalDB struct {
	FileName string
	// contains filtered or unexported fields
}

LokalDB is a wrapper around bbolt key-value database to manage messaging data in a local database

func Open

func Open(file string) (*LokalDB, error)

Open opens a local database file. It creates the file if it does not exist.

func (*LokalDB) Close

func (db *LokalDB) Close() error

Close the local database

func (*LokalDB) Count

func (db *LokalDB) Count(bucket string) (int, error)

Count records in the bucket

func (*LokalDB) CutChunkDown

func (db *LokalDB) CutChunkDown(bucket string, max int) ([]ChunkData, error)

CutChunkDown gets a chunk of data starting from top to bottom in ascending order and removes them.

func (*LokalDB) CutChunkUp

func (db *LokalDB) CutChunkUp(bucket string, max int) ([]ChunkData, error)

CutChunkUp gets a chunk of data starting from bottom to top in descending order and removes them.

func (*LokalDB) Delete

func (db *LokalDB) Delete(bucket string, key string) error

Delete a single record in the database that matches the provided key.

func (*LokalDB) DeleteOnce

func (db *LokalDB) DeleteOnce(bucket string, key []string) error

DeleteOnce remove records in the local database in one go from a supplied provided key.

func (*LokalDB) Fetch

func (db *LokalDB) Fetch(bucket string, key string) (data []byte, err error)

Fetch gets a single record from the local database with the provided key. If the record does not exist, it will return nil.

func (*LokalDB) FetchChunkDown

func (db *LokalDB) FetchChunkDown(bucket string, max int, offset int) ([]ChunkData, error)

FetchChunkDown gets a chunk of data starting from the top to bottom limited by max.

func (*LokalDB) FetchChunkUp

func (db *LokalDB) FetchChunkUp(bucket string, max int, offset int) ([]ChunkData, error)

FetchChunkUp gets a chunk of data starting from the bottom to top limited by max.

func (*LokalDB) FetchDelete

func (db *LokalDB) FetchDelete(bucket string, key string) ([]byte, error)

FetchDelete gets the record with the provided key and deletes it.

func (*LokalDB) SliceDown

func (db *LokalDB) SliceDown(bucket string) (data []byte, err error)

SliceDown fetches and deletes a record from top to bottom.

func (*LokalDB) SliceUp

func (db *LokalDB) SliceUp(bucket string) (data []byte, err error)

SliceUp fetches and deletes a record from bottom to top.

func (*LokalDB) Store

func (db *LokalDB) Store(bucket string, key string, data []byte) error

Store inserts data in the local database. It will update records containing the same key with the current value.

func (*LokalDB) StoreOnce

func (db *LokalDB) StoreOnce(bucket string, data []ChunkData) error

StoreOnce inserts data in the local database in one go. It will update records containing the same key with the current value.

Jump to

Keyboard shortcuts

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