2018-09-14 14:25:14 +00:00
|
|
|
package inmem
|
|
|
|
|
|
|
|
import (
|
2019-02-19 23:47:19 +00:00
|
|
|
"context"
|
2018-09-14 14:25:14 +00:00
|
|
|
"sync"
|
2018-10-16 13:52:52 +00:00
|
|
|
"time"
|
2018-09-14 14:25:14 +00:00
|
|
|
|
2019-01-08 00:37:16 +00:00
|
|
|
platform "github.com/influxdata/influxdb"
|
|
|
|
"github.com/influxdata/influxdb/rand"
|
|
|
|
"github.com/influxdata/influxdb/snowflake"
|
2018-09-14 14:25:14 +00:00
|
|
|
)
|
|
|
|
|
2018-11-07 18:55:52 +00:00
|
|
|
// OpPrefix is the op prefix.
|
|
|
|
const OpPrefix = "inmem/"
|
|
|
|
|
2018-09-14 14:25:14 +00:00
|
|
|
// Service implements various top level services.
|
|
|
|
type Service struct {
|
|
|
|
authorizationKV sync.Map
|
|
|
|
organizationKV sync.Map
|
|
|
|
bucketKV sync.Map
|
|
|
|
userKV sync.Map
|
|
|
|
dashboardKV sync.Map
|
|
|
|
viewKV sync.Map
|
2019-02-14 20:32:54 +00:00
|
|
|
variableKV sync.Map
|
2018-09-14 14:25:14 +00:00
|
|
|
dbrpMappingKV sync.Map
|
|
|
|
userResourceMappingKV sync.Map
|
2018-12-03 16:07:08 +00:00
|
|
|
labelKV sync.Map
|
2019-01-18 19:03:36 +00:00
|
|
|
labelMappingKV sync.Map
|
2018-09-07 15:45:28 +00:00
|
|
|
scraperTargetKV sync.Map
|
2018-10-15 19:17:01 +00:00
|
|
|
telegrafConfigKV sync.Map
|
2018-10-03 14:51:14 +00:00
|
|
|
onboardingKV sync.Map
|
|
|
|
basicAuthKV sync.Map
|
2019-02-19 23:47:19 +00:00
|
|
|
sessionKV sync.Map
|
|
|
|
sourceKV sync.Map
|
2018-09-14 14:25:14 +00:00
|
|
|
|
|
|
|
TokenGenerator platform.TokenGenerator
|
|
|
|
IDGenerator platform.IDGenerator
|
2018-10-16 13:52:52 +00:00
|
|
|
time func() time.Time
|
2018-09-14 14:25:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewService creates an instance of a Service.
|
|
|
|
func NewService() *Service {
|
2019-02-19 23:47:19 +00:00
|
|
|
s := &Service{
|
2018-09-14 14:25:14 +00:00
|
|
|
TokenGenerator: rand.NewTokenGenerator(64),
|
|
|
|
IDGenerator: snowflake.NewIDGenerator(),
|
2018-10-16 13:52:52 +00:00
|
|
|
time: time.Now,
|
2018-09-14 14:25:14 +00:00
|
|
|
}
|
2019-02-19 23:47:19 +00:00
|
|
|
s.initializeSources(context.TODO())
|
|
|
|
return s
|
2018-09-14 14:25:14 +00:00
|
|
|
}
|
2018-10-16 13:52:52 +00:00
|
|
|
|
|
|
|
// WithTime sets the function for computing the current time. Used for updating meta data
|
|
|
|
// about objects stored. Should only be used in tests for mocking.
|
|
|
|
func (s *Service) WithTime(fn func() time.Time) {
|
|
|
|
s.time = fn
|
|
|
|
}
|
2019-02-19 23:47:19 +00:00
|
|
|
|
|
|
|
// Flush removes all data from the in-memory store
|
|
|
|
func (s *Service) Flush() {
|
|
|
|
s.flush(&s.authorizationKV)
|
|
|
|
s.flush(&s.organizationKV)
|
|
|
|
s.flush(&s.bucketKV)
|
|
|
|
s.flush(&s.userKV)
|
|
|
|
s.flush(&s.dashboardKV)
|
|
|
|
s.flush(&s.viewKV)
|
|
|
|
s.flush(&s.variableKV)
|
|
|
|
s.flush(&s.dbrpMappingKV)
|
|
|
|
s.flush(&s.userResourceMappingKV)
|
|
|
|
s.flush(&s.labelKV)
|
|
|
|
s.flush(&s.labelMappingKV)
|
|
|
|
s.flush(&s.scraperTargetKV)
|
|
|
|
s.flush(&s.telegrafConfigKV)
|
|
|
|
s.flush(&s.onboardingKV)
|
|
|
|
s.flush(&s.basicAuthKV)
|
|
|
|
s.flush(&s.sessionKV)
|
|
|
|
s.flush(&s.sourceKV)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) flush(m *sync.Map) {
|
|
|
|
keys := []interface{}{}
|
|
|
|
f := func(key, value interface{}) bool {
|
|
|
|
keys = append(keys, key)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Range(f)
|
|
|
|
|
|
|
|
for _, k := range keys {
|
|
|
|
m.Delete(k)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|