2016-10-06 04:26:39 +00:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
2016-10-25 15:20:06 +00:00
|
|
|
"context"
|
|
|
|
|
2016-10-06 04:26:39 +00:00
|
|
|
"github.com/boltdb/bolt"
|
2016-10-20 14:38:23 +00:00
|
|
|
"github.com/influxdata/chronograf"
|
|
|
|
"github.com/influxdata/chronograf/bolt/internal"
|
2016-10-06 04:26:39 +00:00
|
|
|
)
|
|
|
|
|
2016-10-20 14:38:23 +00:00
|
|
|
// Ensure LayoutStore implements chronograf.LayoutStore.
|
|
|
|
var _ chronograf.LayoutStore = &LayoutStore{}
|
2016-10-06 04:26:39 +00:00
|
|
|
|
2017-03-06 16:11:52 +00:00
|
|
|
// LayoutBucket is the bolt bucket layouts are stored in
|
2016-10-06 04:26:39 +00:00
|
|
|
var LayoutBucket = []byte("Layout")
|
|
|
|
|
2017-03-06 16:11:52 +00:00
|
|
|
// LayoutStore is the bolt implementation to store layouts
|
2016-10-06 04:26:39 +00:00
|
|
|
type LayoutStore struct {
|
|
|
|
client *Client
|
2016-10-20 14:38:23 +00:00
|
|
|
IDs chronograf.ID
|
2016-10-06 04:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// All returns all known layouts
|
2016-10-20 14:38:23 +00:00
|
|
|
func (s *LayoutStore) All(ctx context.Context) ([]chronograf.Layout, error) {
|
|
|
|
var srcs []chronograf.Layout
|
2016-10-06 04:26:39 +00:00
|
|
|
if err := s.client.db.View(func(tx *bolt.Tx) error {
|
|
|
|
if err := tx.Bucket(LayoutBucket).ForEach(func(k, v []byte) error {
|
2016-10-20 14:38:23 +00:00
|
|
|
var src chronograf.Layout
|
2016-10-06 04:26:39 +00:00
|
|
|
if err := internal.UnmarshalLayout(v, &src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
srcs = append(srcs, src)
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return srcs, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add creates a new Layout in the LayoutStore.
|
2016-10-20 14:38:23 +00:00
|
|
|
func (s *LayoutStore) Add(ctx context.Context, src chronograf.Layout) (chronograf.Layout, error) {
|
2016-10-06 04:26:39 +00:00
|
|
|
if err := s.client.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
b := tx.Bucket(LayoutBucket)
|
2016-10-10 22:00:27 +00:00
|
|
|
id, err := s.IDs.Generate()
|
2016-10-06 04:26:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-10 22:00:27 +00:00
|
|
|
src.ID = id
|
2016-10-06 04:26:39 +00:00
|
|
|
if v, err := internal.MarshalLayout(src); err != nil {
|
|
|
|
return err
|
2016-10-10 22:00:27 +00:00
|
|
|
} else if err := b.Put([]byte(src.ID), v); err != nil {
|
2016-10-06 04:26:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2016-10-20 14:38:23 +00:00
|
|
|
return chronograf.Layout{}, err
|
2016-10-06 04:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return src, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes the Layout from the LayoutStore
|
2016-10-20 14:38:23 +00:00
|
|
|
func (s *LayoutStore) Delete(ctx context.Context, src chronograf.Layout) error {
|
2016-10-11 00:34:05 +00:00
|
|
|
_, err := s.Get(ctx, src.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-06 04:26:39 +00:00
|
|
|
if err := s.client.db.Update(func(tx *bolt.Tx) error {
|
2016-10-10 22:00:27 +00:00
|
|
|
if err := tx.Bucket(LayoutBucket).Delete([]byte(src.ID)); err != nil {
|
2016-10-06 04:26:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns a Layout if the id exists.
|
2016-10-20 14:38:23 +00:00
|
|
|
func (s *LayoutStore) Get(ctx context.Context, id string) (chronograf.Layout, error) {
|
|
|
|
var src chronograf.Layout
|
2016-10-06 04:26:39 +00:00
|
|
|
if err := s.client.db.View(func(tx *bolt.Tx) error {
|
2016-10-10 22:00:27 +00:00
|
|
|
if v := tx.Bucket(LayoutBucket).Get([]byte(id)); v == nil {
|
2016-10-20 14:38:23 +00:00
|
|
|
return chronograf.ErrLayoutNotFound
|
2016-10-06 04:26:39 +00:00
|
|
|
} else if err := internal.UnmarshalLayout(v, &src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2016-10-20 14:38:23 +00:00
|
|
|
return chronograf.Layout{}, err
|
2016-10-06 04:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return src, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update a Layout
|
2016-10-20 14:38:23 +00:00
|
|
|
func (s *LayoutStore) Update(ctx context.Context, src chronograf.Layout) error {
|
2016-10-06 04:26:39 +00:00
|
|
|
if err := s.client.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
// Get an existing layout with the same ID.
|
|
|
|
b := tx.Bucket(LayoutBucket)
|
2016-10-10 22:00:27 +00:00
|
|
|
if v := b.Get([]byte(src.ID)); v == nil {
|
2016-10-20 14:38:23 +00:00
|
|
|
return chronograf.ErrLayoutNotFound
|
2016-10-06 04:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if v, err := internal.MarshalLayout(src); err != nil {
|
|
|
|
return err
|
2016-10-10 22:00:27 +00:00
|
|
|
} else if err := b.Put([]byte(src.ID), v); err != nil {
|
2016-10-06 04:26:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|