influxdb/bolt/exploration.go

129 lines
3.4 KiB
Go
Raw Normal View History

2016-09-28 19:32:58 +00:00
package bolt
import (
2016-10-25 15:20:06 +00:00
"context"
2016-09-28 19:32:58 +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-09-28 19:32:58 +00:00
)
2016-10-20 14:38:23 +00:00
// Ensure ExplorationStore implements chronograf.ExplorationStore.
var _ chronograf.ExplorationStore = &ExplorationStore{}
2016-09-28 19:32:58 +00:00
var ExplorationBucket = []byte("Explorations")
type ExplorationStore struct {
client *Client
}
// Search the ExplorationStore for all explorations owned by userID.
2016-10-20 14:38:23 +00:00
func (s *ExplorationStore) Query(ctx context.Context, uid chronograf.UserID) ([]*chronograf.Exploration, error) {
var explorations []*chronograf.Exploration
2016-09-28 19:32:58 +00:00
if err := s.client.db.View(func(tx *bolt.Tx) error {
if err := tx.Bucket(ExplorationBucket).ForEach(func(k, v []byte) error {
2016-10-20 14:38:23 +00:00
var e chronograf.Exploration
2016-09-28 19:32:58 +00:00
if err := internal.UnmarshalExploration(v, &e); err != nil {
return err
} else if e.UserID != uid {
return nil
}
explorations = append(explorations, &e)
return nil
}); err != nil {
return err
}
return nil
}); err != nil {
return nil, err
}
return explorations, nil
}
// Create a new Exploration in the ExplorationStore.
2016-10-20 14:38:23 +00:00
func (s *ExplorationStore) Add(ctx context.Context, e *chronograf.Exploration) (*chronograf.Exploration, error) {
2016-09-28 19:32:58 +00:00
if err := s.client.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(ExplorationBucket)
seq, err := b.NextSequence()
if err != nil {
return err
}
2016-10-20 14:38:23 +00:00
e.ID = chronograf.ExplorationID(seq)
2016-10-25 15:20:06 +00:00
e.CreatedAt = s.client.Now().UTC()
e.UpdatedAt = e.CreatedAt
2016-09-28 19:32:58 +00:00
if v, err := internal.MarshalExploration(e); err != nil {
return err
} else if err := b.Put(itob(int(e.ID)), v); err != nil {
return err
}
return nil
}); err != nil {
return nil, err
2016-09-28 19:32:58 +00:00
}
return e, nil
2016-09-28 19:32:58 +00:00
}
// Delete the exploration from the ExplorationStore
2016-10-20 14:38:23 +00:00
func (s *ExplorationStore) Delete(ctx context.Context, e *chronograf.Exploration) error {
2016-09-28 19:32:58 +00:00
if err := s.client.db.Update(func(tx *bolt.Tx) error {
if err := tx.Bucket(ExplorationBucket).Delete(itob(int(e.ID))); err != nil {
return err
}
return nil
}); err != nil {
return err
}
return nil
}
// Retrieve an exploration for an id exists.
2016-10-20 14:38:23 +00:00
func (s *ExplorationStore) Get(ctx context.Context, id chronograf.ExplorationID) (*chronograf.Exploration, error) {
var e chronograf.Exploration
2016-09-28 19:32:58 +00:00
if err := s.client.db.View(func(tx *bolt.Tx) error {
if v := tx.Bucket(ExplorationBucket).Get(itob(int(id))); v == nil {
2016-10-20 14:38:23 +00:00
return chronograf.ErrExplorationNotFound
2016-09-28 19:32:58 +00:00
} else if err := internal.UnmarshalExploration(v, &e); err != nil {
return err
}
return nil
}); err != nil {
return nil, err
}
return &e, nil
}
// Update an exploration; will also update the `UpdatedAt` time.
2016-10-20 14:38:23 +00:00
func (s *ExplorationStore) Update(ctx context.Context, e *chronograf.Exploration) error {
2016-09-28 19:32:58 +00:00
if err := s.client.db.Update(func(tx *bolt.Tx) error {
// Retreive an existing exploration with the same exploration ID.
2016-10-20 14:38:23 +00:00
var ee chronograf.Exploration
2016-09-28 19:32:58 +00:00
b := tx.Bucket(ExplorationBucket)
if v := b.Get(itob(int(e.ID))); v == nil {
2016-10-20 14:38:23 +00:00
return chronograf.ErrExplorationNotFound
2016-09-28 19:32:58 +00:00
} else if err := internal.UnmarshalExploration(v, &ee); err != nil {
return err
}
ee.Name = e.Name
ee.UserID = e.UserID
ee.Data = e.Data
2016-10-25 15:20:06 +00:00
ee.UpdatedAt = s.client.Now().UTC()
2016-09-28 19:32:58 +00:00
if v, err := internal.MarshalExploration(&ee); err != nil {
return err
} else if err := b.Put(itob(int(ee.ID)), v); err != nil {
return err
}
return nil
}); err != nil {
return err
}
return nil
}