boltdbpool

package module
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2023 License: BSD-3-Clause Imports: 7 Imported by: 2

README

Pool of Bolt DBs

Go PkgGoDev NewReleases

Installation

Run go get resenje.org/boltdbpool from command line.

Documentation

Overview

Package boltdbpool implements a pool container for BoltDB github.com/coreos/bbolt databases. Pool elements called connections keep reference counts for each database to close it when it when the count is 0. Database is reused or opened based on database file path. Closing the database must not be done directly, instead Connection.Close() method should be used. Database is removed form the pool and closed by the goroutine in the background in respect to reference count and delay in time if it is specified.

Example:

package main

import (
    "fmt"
    "time"

    "resenje.org/boltdbpool"
)

func main() {
    pool := boltdbpool.New(&boltdbpool.Options{
        ConnectionExpires: 5 * time.Second,
        ErrorHandler: func(err error) {
            fmt.Printf("error: %v", err)
        },
    })
    defer p.Close()

    ...

    c, err := pool.Get("/tmp/db.bolt")
    if err != nil {
        panic(err)
    }
    defer c.Close()

    ...

    c.DB.Update(func(tx *bolt.TX) error {
        ...
    })
}

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultErrorHandler is the default function that prints errors from the Pool.
	DefaultErrorHandler = func(err error) {
		log.Printf("error: %v", err)
	}
)

Functions

This section is empty.

Types

type Connection

type Connection struct {
	DB *bolt.DB
	// contains filtered or unexported fields
}

Connection encapsulates bolt.DB and keeps reference counter and closing time information.

func (*Connection) Close

func (c *Connection) Close()

Close function on Connection decrements reference counter and closes the database if needed.

type Options

type Options struct {
	// BoltOptions is used on bolt.Open().
	BoltOptions *bolt.Options

	// ConnectionExpires is a duration between the reference count drops to 0 and
	// the time when the database is closed. It is useful to avoid frequent
	// openings of the same database. If the value is 0 (default), no caching is done.
	ConnectionExpires time.Duration

	// ErrorHandler is the function that handles errors.
	ErrorHandler func(error)
}

Options are used when a new pool is created that.

type Pool

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

Pool keeps track of connections.

func New

func New(options *Options) *Pool

New creates new pool with provided options and also starts database closing goroutone and goroutine for errors handling to ErrorHandler.

func (*Pool) Close

func (p *Pool) Close()

Close function closes and removes from the pool all databases. After the execution pool is not usable.

func (*Pool) Get

func (p *Pool) Get(path string) (*Connection, error)

Get returns a connection that contains a database or creates a new connection with newly opened database based on options specified on pool creation.

func (*Pool) Has

func (p *Pool) Has(path string) bool

Has returns true if a database with a file path is in the pool.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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