database

package
v0.2.9 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package database provides a key/value storage. It allows binary serialization and deserialization to a reader/writer of to file

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB struct {
	// Storage stores the key storage mappings
	Storage map[string]interface{}
}

DB represents a database

func (*DB) Delete

func (db *DB) Delete(key string) (deleted bool)

Delete removes a record from the database. It returns true if the key was present and false otherwise

func (*DB) Exists

func (db *DB) Exists(key string) bool

Exists allows checking for the existence of a record in the database

func (*DB) Get

func (db *DB) Get(key string) (value interface{})

Get allows retrieving a previously stored value indexed for 'key'. If the key does not exists, it will return a nil value.

func (*DB) Keys

func (db *DB) Keys() []string

Keys returns a slice containing all the keys stored

func (*DB) Set

func (db *DB) Set(key string, value interface{})

Set allows storing a generic value indexed by the provided key. If an existing record already exists for the same key, it will be overwritten

type FileDB

type FileDB struct {
	DB
	// contains filtered or unexported fields
}

FileDB implements a database backed up by a file

func (*FileDB) Deserialize

func (db *FileDB) Deserialize() error

Deserialize reads the file storage from disk and re-populates the in-memory database

func (*FileDB) Serialize

func (db *FileDB) Serialize() error

Serialize saves the in-memory database records to the on-disk storage file

type Serializable

type Serializable interface {
	// Serialize allows saving the database storage to a file on disk
	Serialize() error
	// DeserializeFromFile allows populating the database from a file on disk
	Deserialize() error
}

Serializable defines the interface that all serializable objects should implement

type Storer

type Storer interface {
	Serializable
	// Get allows retrieving the value associated with the provided key
	// If the key is not present, it returns nil
	Get(key string) interface{}
	// Set allows setting a value identified by the provided key
	Set(key string, value interface{})
	// Delete allows deleting a key.
	// It returns true if the key was present or false otherwise
	Delete(key string) bool
	//Exists returns true if the key exists and false otherwise
	Exists(key string) bool
	// Keys returns the list of key identifiers stored
	Keys() []string
}

Storer defines the interface that all database kinds should implement

func NewFileDatabase

func NewFileDatabase(file string) (Storer, error)

NewFileDatabase returns a file-backed database. If 'file' exists, the function will try to load its contents to populate the database.

Jump to

Keyboard shortcuts

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