2018-04-17 04:50:20 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-04-06 17:32:34 +01:00
|
|
|
package boltdb
|
|
|
|
|
|
|
|
import (
|
2018-04-10 22:46:48 +01:00
|
|
|
"time"
|
2018-04-17 04:50:20 +01:00
|
|
|
|
|
|
|
"github.com/boltdb/bolt"
|
2018-04-06 17:32:34 +01:00
|
|
|
)
|
|
|
|
|
2018-04-10 22:46:48 +01:00
|
|
|
var (
|
|
|
|
defaultTimeout = 1 * time.Second
|
|
|
|
|
2018-04-17 04:50:20 +01:00
|
|
|
ErrDbOpen = Error.New("error boltdb failed to open")
|
|
|
|
ErrInitDb = Error.New("error instantiating boltdb")
|
2018-04-10 22:46:48 +01:00
|
|
|
)
|
2018-04-06 17:32:34 +01:00
|
|
|
|
|
|
|
// Client is the storage interface for the Bolt database
|
|
|
|
type Client struct {
|
2018-04-17 04:50:20 +01:00
|
|
|
db *bolt.DB
|
|
|
|
Path string
|
2018-04-06 17:32:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// New instantiates a new BoltDB client
|
2018-04-10 22:46:48 +01:00
|
|
|
func New(path string) (*Client, error) {
|
|
|
|
db, err := bolt.Open(path, 0600, &bolt.Options{Timeout: defaultTimeout})
|
2018-04-06 17:32:34 +01:00
|
|
|
if err != nil {
|
2018-04-10 22:46:48 +01:00
|
|
|
return nil, ErrDbOpen
|
2018-04-06 17:32:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Client{
|
2018-04-17 04:50:20 +01:00
|
|
|
db: db,
|
|
|
|
Path: path,
|
2018-04-06 17:32:34 +01:00
|
|
|
}, nil
|
|
|
|
}
|
2018-04-10 22:46:48 +01:00
|
|
|
|
|
|
|
func (c *Client) Close() error {
|
|
|
|
return c.db.Close()
|
|
|
|
}
|