influxdb/shard.go

85 lines
1.9 KiB
Go
Raw Normal View History

2014-11-04 04:15:58 +00:00
package influxdb
import (
"errors"
2014-11-04 04:15:58 +00:00
"fmt"
"time"
"github.com/boltdb/bolt"
)
2014-11-06 06:03:35 +00:00
// Shard represents the physical storage for a given time range.
type Shard struct {
ID uint64 `json:"id,omitempty"`
2014-11-06 06:03:35 +00:00
StartTime time.Time `json:"startTime,omitempty"`
EndTime time.Time `json:"endTime,omitempty"`
2014-11-04 04:15:58 +00:00
store *bolt.DB
}
// newShard returns a new initialized Shard instance.
func newShard() *Shard { return &Shard{} }
2014-11-06 06:03:35 +00:00
// Duration returns the duration between the shard's start and end time.
func (s *Shard) Duration() time.Duration { return s.EndTime.Sub(s.StartTime) }
// open initializes and opens the shard's store.
func (s *Shard) open(path string) error {
// Return an error if the shard is already open.
if s.store != nil {
return errors.New("shard already open")
}
// Open store on shard.
store, err := bolt.Open(path, 0600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
return err
}
s.store = store
// Initialize store.
if err := s.init(); err != nil {
_ = s.close()
return fmt.Errorf("init: %s", err)
}
return nil
}
// init creates top-level buckets in the datastore.
func (s *Shard) init() error {
return s.store.Update(func(tx *bolt.Tx) error {
_, _ = tx.CreateBucketIfNotExists([]byte("values"))
return nil
})
}
// close shuts down the shard's store.
func (s *Shard) close() error {
return s.store.Close()
}
2014-11-04 04:15:58 +00:00
// write writes series data to a shard.
2014-11-18 00:23:21 +00:00
func (s *Shard) writeSeries(name string, tags map[string]string, value interface{}) error {
2014-11-04 04:15:58 +00:00
return s.store.Update(func(tx *bolt.Tx) error {
2014-11-17 22:54:35 +00:00
// TODO
2014-11-04 04:15:58 +00:00
return nil
})
}
2014-11-06 06:03:35 +00:00
func (s *Shard) deleteSeries(name string) error {
2014-11-04 04:15:58 +00:00
panic("not yet implemented") // TODO
}
// Shards represents a list of shards.
type Shards []*Shard
// IDs returns a slice of all shard ids.
func (p Shards) IDs() []uint64 {
ids := make([]uint64, len(p))
for i, s := range p {
ids[i] = s.ID
}
return ids
}