chronograf/bolt/client.go

85 lines
1.8 KiB
Go
Raw Normal View History

2016-09-28 19:32:58 +00:00
package bolt
import (
"time"
"github.com/boltdb/bolt"
"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 {
Path string
db *bolt.DB
Now func() time.Time
LayoutIDs mrfusion.ID
2016-09-28 19:32:58 +00:00
ExplorationStore *ExplorationStore
SourcesStore *SourcesStore
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}
c.SourcesStore = &SourcesStore{client: c}
c.ServersStore = &ServersStore{client: c}
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.
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
}
// Always create Sources bucket.
if _, err := tx.CreateBucketIfNotExists(SourcesBucket); err != nil {
return err
}
// 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-28 19:32:58 +00:00
return nil
}); err != nil {
return err
}
// 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
}