bbolt

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2024 License: Apache-2.0 Imports: 5 Imported by: 1

Documentation

Overview

Package bbolt provides a Hord database driver for BoltDB.

BoltDB is an embedded key-value database that persists data on disk. To use this driver, import it as follows:

import (
    "github.com/madflojo/hord"
    "github.com/madflojo/hord/bbolt"
)

Connecting to the Database

Use the Dial() function to create a new client for interacting with BoltDB.

var db hord.Database
db, err := bbolt.Dial(bbolt.Config{})
if err != nil {
    // Handle connection error
}

Initialize database

Hord provides a Setup() function for preparing a database. This function is safe to execute after every Dial().

err := db.Setup()
if err != nil {
    // Handle setup error
}

Database Operations

Hord provides a simple abstraction for working with BoltDB, with easy-to-use methods such as Get() and Set() to read and write values.

// Connect to the BoltDB database
db, err := bbolt.Dial(bbolt.Config{})
if err != nil {
    // Handle connection error
}

err := db.Setup()
if err != nil {
    // Handle setup error
}

// Set a value
err = db.Set("key", []byte("value"))
if err != nil {
    // Handle error
}

// Retrieve a value
value, err := db.Get("key")
if err != nil {
    // Handle error
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Bucketname specifies the bucket to store and retrieve data from.
	Bucketname string

	// Filename specifies the file path of the bbolt file.
	Filename string

	// Permissions specifies the file permissions for the bbolt database file.
	Permissions os.FileMode

	// Timeout specifies the timeout duration for opening obtaining a file lock on the database file.
	// Default value is 5 Seconds, a value of 0 is invalid.
	Timeout time.Duration
}

Config represents the configuration for the bbolt database.

type Database

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

Database is an bbolt implementation of the hord.Database interface.

func Dial

func Dial(cfg Config) (*Database, error)

Dial initializes and returns a new bbolt database instance.

func (*Database) Close

func (db *Database) Close()

Close closes the bbolt database connection and clears all stored data.

func (*Database) Delete

func (db *Database) Delete(key string) error

Delete removes data from the bbolt database based on the provided key. It returns an error if the key is invalid.

func (*Database) Get

func (db *Database) Get(key string) ([]byte, error)

Get retrieves data from the bbolt database based on the provided key. It returns the data associated with the key or an error if the key is invalid or the data does not exist.

func (*Database) HealthCheck

func (db *Database) HealthCheck() error

HealthCheck performs a health check on the bbolt database.

func (*Database) Keys

func (db *Database) Keys() ([]string, error)

Keys retrieves a list of keys stored in the bbolt database.

func (*Database) Set

func (db *Database) Set(key string, data []byte) error

Set inserts or updates data in the bbolt database based on the provided key. It returns an error if the key or data is invalid.

func (*Database) Setup

func (db *Database) Setup() error

Setup initializes the database by creating the necessary bucket if it doesn't exist. Returns an error if the database is not connected or if there is an error creating the bucket.

Jump to

Keyboard shortcuts

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