2017-04-28 23:28:25 +00:00
|
|
|
package storm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/asdine/storm/codec"
|
|
|
|
"github.com/asdine/storm/codec/json"
|
2018-01-03 19:29:07 +00:00
|
|
|
"github.com/coreos/bbolt"
|
2017-04-28 23:28:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
dbinfo = "__storm_db"
|
|
|
|
metadataBucket = "__storm_metadata"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Defaults to json
|
|
|
|
var defaultCodec = json.Codec
|
|
|
|
|
|
|
|
// Open opens a database at the given path with optional Storm options.
|
2018-01-03 19:29:07 +00:00
|
|
|
func Open(path string, stormOptions ...func(*Options) error) (*DB, error) {
|
2017-04-28 23:28:25 +00:00
|
|
|
var err error
|
|
|
|
|
2018-01-03 19:29:07 +00:00
|
|
|
var opts Options
|
2017-04-28 23:28:25 +00:00
|
|
|
for _, option := range stormOptions {
|
2018-01-03 19:29:07 +00:00
|
|
|
if err = option(&opts); err != nil {
|
2017-04-28 23:28:25 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-03 19:29:07 +00:00
|
|
|
s := DB{
|
|
|
|
Bolt: opts.bolt,
|
|
|
|
}
|
|
|
|
|
|
|
|
n := node{
|
|
|
|
s: &s,
|
|
|
|
codec: opts.codec,
|
|
|
|
batchMode: opts.batchMode,
|
|
|
|
rootBucket: opts.rootBucket,
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.codec == nil {
|
|
|
|
n.codec = defaultCodec
|
2017-04-28 23:28:25 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 19:29:07 +00:00
|
|
|
if opts.boltMode == 0 {
|
|
|
|
opts.boltMode = 0600
|
2017-04-28 23:28:25 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 19:29:07 +00:00
|
|
|
if opts.boltOptions == nil {
|
|
|
|
opts.boltOptions = &bolt.Options{Timeout: 1 * time.Second}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Node = &n
|
2017-04-28 23:28:25 +00:00
|
|
|
|
|
|
|
// skip if UseDB option is used
|
|
|
|
if s.Bolt == nil {
|
2018-01-03 19:29:07 +00:00
|
|
|
s.Bolt, err = bolt.Open(path, opts.boltMode, opts.boltOptions)
|
2017-04-28 23:28:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-03 19:29:07 +00:00
|
|
|
}
|
2017-04-28 23:28:25 +00:00
|
|
|
|
2018-01-03 19:29:07 +00:00
|
|
|
err = s.checkVersion()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-04-28 23:28:25 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 19:29:07 +00:00
|
|
|
return &s, nil
|
2017-04-28 23:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DB is the wrapper around BoltDB. It contains an instance of BoltDB and uses it to perform all the
|
|
|
|
// needed operations
|
|
|
|
type DB struct {
|
2018-01-03 19:29:07 +00:00
|
|
|
// The root node that points to the root bucket.
|
|
|
|
Node
|
2017-04-28 23:28:25 +00:00
|
|
|
|
|
|
|
// Bolt is still easily accessible
|
|
|
|
Bolt *bolt.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the database
|
|
|
|
func (s *DB) Close() error {
|
|
|
|
return s.Bolt.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) checkVersion() error {
|
|
|
|
var v string
|
|
|
|
err := s.Get(dbinfo, "version", &v)
|
|
|
|
if err != nil && err != ErrNotFound {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-03 19:29:07 +00:00
|
|
|
// for now, we only set the current version if it doesn't exist.
|
|
|
|
// v1 and v2 database files are compatible.
|
|
|
|
if v == "" {
|
2017-04-28 23:28:25 +00:00
|
|
|
return s.Set(dbinfo, "version", Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// toBytes turns an interface into a slice of bytes
|
|
|
|
func toBytes(key interface{}, codec codec.MarshalUnmarshaler) ([]byte, error) {
|
|
|
|
if key == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
switch t := key.(type) {
|
|
|
|
case []byte:
|
|
|
|
return t, nil
|
|
|
|
case string:
|
|
|
|
return []byte(t), nil
|
|
|
|
case int:
|
|
|
|
return numbertob(int64(t))
|
|
|
|
case uint:
|
|
|
|
return numbertob(uint64(t))
|
|
|
|
case int8, int16, int32, int64, uint8, uint16, uint32, uint64:
|
|
|
|
return numbertob(t)
|
|
|
|
default:
|
|
|
|
return codec.Marshal(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func numbertob(v interface{}) ([]byte, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err := binary.Write(&buf, binary.BigEndian, v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func numberfromb(raw []byte) (int64, error) {
|
|
|
|
r := bytes.NewReader(raw)
|
|
|
|
var to int64
|
|
|
|
err := binary.Read(r, binary.BigEndian, &to)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return to, nil
|
|
|
|
}
|