chronograf/bolt/layouts.go

129 lines
3.0 KiB
Go
Raw Normal View History

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
)
// Ensure LayoutsStore implements chronograf.LayoutsStore.
var _ chronograf.LayoutsStore = &LayoutsStore{}
2016-10-06 04:26:39 +00:00
// LayoutsBucket is the bolt bucket layouts are stored in
var LayoutsBucket = []byte("Layout")
2016-10-06 04:26:39 +00:00
// LayoutsStore is the bolt implementation to store layouts
type LayoutsStore struct {
client *Client
IDs chronograf.ID
2016-10-06 04:26:39 +00:00
}
func (s *LayoutsStore) Migrate(ctx context.Context) error {
return nil
}
2016-10-06 04:26:39 +00:00
// All returns all known layouts
func (s *LayoutsStore) All(ctx context.Context) ([]chronograf.Layout, error) {
2016-10-20 14:38:23 +00:00
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(LayoutsBucket).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 LayoutsStore.
func (s *LayoutsStore) 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(LayoutsBucket)
id, err := s.IDs.Generate()
2016-10-06 04:26:39 +00:00
if err != nil {
return err
}
src.ID = id
2016-10-06 04:26:39 +00:00
if v, err := internal.MarshalLayout(src); err != nil {
return err
} 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 LayoutsStore
func (s *LayoutsStore) 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 {
if err := tx.Bucket(LayoutsBucket).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.
func (s *LayoutsStore) Get(ctx context.Context, id string) (chronograf.Layout, error) {
2016-10-20 14:38:23 +00:00
var src chronograf.Layout
2016-10-06 04:26:39 +00:00
if err := s.client.db.View(func(tx *bolt.Tx) error {
if v := tx.Bucket(LayoutsBucket).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
func (s *LayoutsStore) 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(LayoutsBucket)
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
} 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
}