2016-09-28 19:32:58 +00:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/boltdb/bolt"
|
2016-10-10 22:00:27 +00:00
|
|
|
"github.com/influxdata/mrfusion"
|
|
|
|
"github.com/influxdata/mrfusion/uuid"
|
2016-09-28 19:32:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Client is a client for the boltDB data store.
|
|
|
|
type Client struct {
|
2016-10-10 22:00:27 +00:00
|
|
|
Path string
|
|
|
|
db *bolt.DB
|
|
|
|
Now func() time.Time
|
|
|
|
LayoutIDs mrfusion.ID
|
2016-09-28 19:32:58 +00:00
|
|
|
|
|
|
|
ExplorationStore *ExplorationStore
|
2016-09-30 20:39:27 +00:00
|
|
|
SourcesStore *SourcesStore
|
2016-09-30 23:46:28 +00:00
|
|
|
ServersStore *ServersStore
|
2016-10-06 04:26:39 +00:00
|
|
|
LayoutStore *LayoutStore
|
2016-09-28 19:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient() *Client {
|
|
|
|
c := &Client{Now: time.Now}
|
|
|
|
c.ExplorationStore = &ExplorationStore{client: c}
|
2016-09-30 20:39:27 +00:00
|
|
|
c.SourcesStore = &SourcesStore{client: c}
|
2016-09-30 23:46:28 +00:00
|
|
|
c.ServersStore = &ServersStore{client: c}
|
2016-10-10 22:00:27 +00:00
|
|
|
c.LayoutStore = &LayoutStore{
|
|
|
|
client: c,
|
|
|
|
IDs: &uuid.V4{},
|
|
|
|
}
|
2016-09-28 19:32:58 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open and initialize boltDB. Initial buckets are created if they do not exist.
|
|
|
|
func (c *Client) Open() error {
|
|
|
|
// Open database file.
|
2016-10-10 15:45:35 +00:00
|
|
|
db, err := bolt.Open(c.Path, 0600, &bolt.Options{Timeout: 1 * time.Second})
|
2016-09-28 19:32:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.db = db
|
|
|
|
|
|
|
|
if err := c.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
// Always create explorations bucket.
|
|
|
|
if _, err := tx.CreateBucketIfNotExists(ExplorationBucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-30 20:39:27 +00:00
|
|
|
// Always create Sources bucket.
|
|
|
|
if _, err := tx.CreateBucketIfNotExists(SourcesBucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-30 23:46:28 +00:00
|
|
|
// Always create Servers bucket.
|
|
|
|
if _, err := tx.CreateBucketIfNotExists(ServersBucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-06 04:26:39 +00:00
|
|
|
// Always create Layouts bucket.
|
|
|
|
if _, err := tx.CreateBucketIfNotExists(LayoutBucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-30 20:39:27 +00:00
|
|
|
|
2016-09-28 19:32:58 +00:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-10 22:00:27 +00:00
|
|
|
// TODO: Ask @gunnar about these
|
|
|
|
/*
|
|
|
|
c.ExplorationStore = &ExplorationStore{client: c}
|
|
|
|
c.SourcesStore = &SourcesStore{client: c}
|
|
|
|
c.ServersStore = &ServersStore{client: c}
|
|
|
|
c.LayoutStore = &LayoutStore{client: c}
|
|
|
|
*/
|
2016-09-28 19:32:58 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Close() error {
|
|
|
|
if c.db != nil {
|
|
|
|
return c.db.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|