Rename store Namespace / Prefix options to Database and Table (#1492)

* Rename Namespace to DB, Rename Prefix to table, Remove Suffix Option

* Rename options

* Rename options

* Add store_table option

* Table per service, not Database per service
This commit is contained in:
Jake Sanders 2020-04-06 16:45:55 +01:00 committed by GitHub
parent 3a378eb7d6
commit 3324d140c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 69 additions and 95 deletions

View File

@ -225,9 +225,14 @@ var (
Usage: "Comma-separated list of store addresses", Usage: "Comma-separated list of store addresses",
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "store_namespace", Name: "store_database",
EnvVars: []string{"MICRO_STORE_NAMESPACE"}, EnvVars: []string{"MICRO_STORE_DATABASE"},
Usage: "Namespace for store data", Usage: "Database option for the underlying store",
},
&cli.StringFlag{
Name: "store_table",
EnvVars: []string{"MICRO_STORE_Table"},
Usage: "Table option for the underlying store",
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "transport", Name: "transport",
@ -622,9 +627,15 @@ func (c *cmd) Before(ctx *cli.Context) error {
} }
} }
if len(ctx.String("store_namespace")) > 0 { if len(ctx.String("store_database")) > 0 {
if err := (*c.opts.Store).Init(store.Namespace(ctx.String("store_namespace"))); err != nil { if err := (*c.opts.Store).Init(store.Database(ctx.String("store_database"))); err != nil {
logger.Fatalf("Error configuring store: %v", err) logger.Fatalf("Error configuring store database option: %v", err)
}
}
if len(ctx.String("store_table")) > 0 {
if err := (*c.opts.Store).Init(store.Table(ctx.String("store_table"))); err != nil {
logger.Fatalf("Error configuring store table option: %v", err)
} }
} }

2
go.mod
View File

@ -60,5 +60,3 @@ require (
gopkg.in/src-d/go-git.v4 v4.13.1 gopkg.in/src-d/go-git.v4 v4.13.1
gopkg.in/telegram-bot-api.v4 v4.6.4 gopkg.in/telegram-bot-api.v4 v4.6.4
) )
replace github.com/coreos/bbolt => go.etcd.io/bbolt v1.3.4

View File

@ -106,11 +106,11 @@ func (s *service) Init(opts ...Option) {
logger.Fatal(err) logger.Fatal(err)
} }
// If the store has no namespace set, fallback to the // If the store has no Table set, fallback to the
// services name // services name
if len(store.DefaultStore.Options().Namespace) == 0 { if len(store.DefaultStore.Options().Table) == 0 {
name := s.opts.Cmd.App().Name name := s.opts.Cmd.App().Name
store.DefaultStore.Init(store.Namespace(name)) store.DefaultStore.Init(store.Table(name))
} }
// TODO: replace Cmd.Init with config.Load // TODO: replace Cmd.Init with config.Load

View File

@ -10,7 +10,7 @@ import (
) )
func TestCache(t *testing.T) { func TestCache(t *testing.T) {
l0, l1, l2 := memory.NewStore(store.Namespace("l0")), memory.NewStore(store.Prefix("l1")), memory.NewStore(store.Suffix("l2")) l0, l1, l2 := memory.NewStore(store.Database("l0")), memory.NewStore(store.Table("l1")), memory.NewStore()
_, _, _ = l0.Init(), l1.Init(), l2.Init() _, _, _ = l0.Init(), l1.Init(), l2.Init()
assert := assert.New(t) assert := assert.New(t)

View File

@ -105,8 +105,8 @@ func (w *workersKV) Init(opts ...store.Option) error {
for _, o := range opts { for _, o := range opts {
o(&w.options) o(&w.options)
} }
if len(w.options.Namespace) > 0 { if len(w.options.Database) > 0 {
w.namespace = w.options.Namespace w.namespace = w.options.Database
} }
ttl := w.options.Context.Value("STORE_CACHE_TTL") ttl := w.options.Context.Value("STORE_CACHE_TTL")
if ttl != nil { if ttl != nil {
@ -388,7 +388,7 @@ func NewStore(opts ...store.Option) store.Store {
} }
if len(namespace) == 0 { if len(namespace) == 0 {
namespace = options.Namespace namespace = options.Database
} }
// validate options are not blank or log.Fatal // validate options are not blank or log.Fatal

View File

@ -49,7 +49,7 @@ func Account(id string) store.Option {
// Namespace sets the KV namespace // Namespace sets the KV namespace
func Namespace(ns string) store.Option { func Namespace(ns string) store.Option {
return func(o *store.Options) { return func(o *store.Options) {
o.Namespace = ns o.Database = ns
} }
} }

View File

@ -299,12 +299,12 @@ func (s *sqlStore) configure() error {
s.options.Nodes = []string{"localhost:26257"} s.options.Nodes = []string{"localhost:26257"}
} }
namespace := s.options.Namespace namespace := s.options.Database
if len(namespace) == 0 { if len(namespace) == 0 {
namespace = DefaultNamespace namespace = DefaultNamespace
} }
prefix := s.options.Prefix prefix := s.options.Table
if len(prefix) == 0 { if len(prefix) == 0 {
prefix = DefaultPrefix prefix = DefaultPrefix
} }

View File

@ -28,7 +28,7 @@ func TestSQL(t *testing.T) {
db.Close() db.Close()
sqlStore := NewStore( sqlStore := NewStore(
store.Namespace("testsql"), store.Database("testsql"),
store.Nodes(connection), store.Nodes(connection),
) )

View File

@ -60,11 +60,11 @@ func (e *etcdStore) init() error {
} }
e.client = client e.client = client
ns := "" ns := ""
if len(e.options.Prefix) > 0 { if len(e.options.Table) > 0 {
ns = e.options.Prefix ns = e.options.Table
} }
if len(e.options.Namespace) > 0 { if len(e.options.Database) > 0 {
ns = e.options.Namespace + "/" + ns ns = e.options.Database + "/" + ns
} }
if len(ns) > 0 { if len(ns) > 0 {
e.client.KV = namespace.NewKV(e.client.KV, ns) e.client.KV = namespace.NewKV(e.client.KV, ns)

View File

@ -83,14 +83,11 @@ func (m *memoryStore) Read(key string, opts ...store.ReadOption) ([]*store.Recor
} }
func (m *memoryStore) get(k string) (*store.Record, error) { func (m *memoryStore) get(k string) (*store.Record, error) {
if len(m.options.Suffix) > 0 { if len(m.options.Table) > 0 {
k = k + m.options.Suffix k = m.options.Table + "/" + k
} }
if len(m.options.Prefix) > 0 { if len(m.options.Database) > 0 {
k = m.options.Prefix + "/" + k k = m.options.Database + "/" + k
}
if len(m.options.Namespace) > 0 {
k = m.options.Namespace + "/" + k
} }
var storedRecord *internalRecord var storedRecord *internalRecord
r, found := m.store.Get(k) r, found := m.store.Get(k)
@ -142,14 +139,11 @@ func (m *memoryStore) Write(r *store.Record, opts ...store.WriteOption) error {
func (m *memoryStore) set(r *store.Record) { func (m *memoryStore) set(r *store.Record) {
key := r.Key key := r.Key
if len(m.options.Suffix) > 0 { if len(m.options.Table) > 0 {
key = key + m.options.Suffix key = m.options.Table + "/" + key
} }
if len(m.options.Prefix) > 0 { if len(m.options.Database) > 0 {
key = m.options.Prefix + "/" + key key = m.options.Database + "/" + key
}
if len(m.options.Namespace) > 0 {
key = m.options.Namespace + "/" + key
} }
// copy the incoming record and then // copy the incoming record and then
@ -175,14 +169,11 @@ func (m *memoryStore) Delete(key string, opts ...store.DeleteOption) error {
} }
func (m *memoryStore) delete(key string) { func (m *memoryStore) delete(key string) {
if len(m.options.Suffix) > 0 { if len(m.options.Table) > 0 {
key = key + m.options.Suffix key = m.options.Table + "/" + key
} }
if len(m.options.Prefix) > 0 { if len(m.options.Database) > 0 {
key = m.options.Prefix + "/" + key key = m.options.Database + "/" + key
}
if len(m.options.Namespace) > 0 {
key = m.options.Namespace + "/" + key
} }
m.store.Delete(key) m.store.Delete(key)
} }
@ -226,14 +217,11 @@ func (m *memoryStore) list(limit, offset uint) []string {
allKeys := make([]string, len(allItems)) allKeys := make([]string, len(allItems))
i := 0 i := 0
for k := range allItems { for k := range allItems {
if len(m.options.Suffix) > 0 { if len(m.options.Database) > 0 {
k = strings.TrimSuffix(k, m.options.Suffix) k = strings.TrimPrefix(k, m.options.Database+"/")
} }
if len(m.options.Namespace) > 0 { if len(m.options.Table) > 0 {
k = strings.TrimPrefix(k, m.options.Namespace+"/") k = strings.TrimPrefix(k, m.options.Table+"/")
}
if len(m.options.Prefix) > 0 {
k = strings.TrimPrefix(k, m.options.Prefix+"/")
} }
allKeys[i] = k allKeys[i] = k
i++ i++

View File

@ -10,9 +10,9 @@ import (
) )
func TestMemoryReInit(t *testing.T) { func TestMemoryReInit(t *testing.T) {
s := NewStore(store.Prefix("aaa")) s := NewStore(store.Table("aaa"))
s.Init(store.Prefix("")) s.Init(store.Table(""))
if len(s.Options().Prefix) > 0 { if len(s.Options().Table) > 0 {
t.Error("Init didn't reinitialise the store") t.Error("Init didn't reinitialise the store")
} }
} }
@ -25,31 +25,19 @@ func TestMemoryBasic(t *testing.T) {
func TestMemoryPrefix(t *testing.T) { func TestMemoryPrefix(t *testing.T) {
s := NewStore() s := NewStore()
s.Init(store.Prefix("some-prefix")) s.Init(store.Table("some-prefix"))
basictest(s, t)
}
func TestMemorySuffix(t *testing.T) {
s := NewStore()
s.Init(store.Suffix("some-suffix"))
basictest(s, t)
}
func TestMemoryPrefixSuffix(t *testing.T) {
s := NewStore()
s.Init(store.Prefix("some-prefix"), store.Prefix("some-suffix"))
basictest(s, t) basictest(s, t)
} }
func TestMemoryNamespace(t *testing.T) { func TestMemoryNamespace(t *testing.T) {
s := NewStore() s := NewStore()
s.Init(store.Namespace("some-namespace")) s.Init(store.Database("some-namespace"))
basictest(s, t) basictest(s, t)
} }
func TestMemoryNamespacePrefix(t *testing.T) { func TestMemoryNamespacePrefix(t *testing.T) {
s := NewStore() s := NewStore()
s.Init(store.Prefix("some-prefix"), store.Namespace("some-namespace")) s.Init(store.Table("some-prefix"), store.Database("some-namespace"))
basictest(s, t) basictest(s, t)
} }

View File

@ -11,13 +11,10 @@ type Options struct {
// For example, an etcd implementation would contain the nodes of the cluster. // For example, an etcd implementation would contain the nodes of the cluster.
// A SQL implementation could contain one or more connection strings. // A SQL implementation could contain one or more connection strings.
Nodes []string Nodes []string
// Namespace allows multiple isolated stores to be kept in one backend, if supported. // Database allows multiple isolated stores to be kept in one backend, if supported.
// For example multiple tables in a SQL store. Database string
Namespace string // Table is analagous to a table in database backends or a key prefix in KV backends
// Prefix sets a global prefix on all keys Table string
Prefix string
// Suffix sets a global suffix on all keys
Suffix string
// Context should contain all implementation specific options, using context.WithValue. // Context should contain all implementation specific options, using context.WithValue.
Context context.Context Context context.Context
} }
@ -34,25 +31,17 @@ func Nodes(a ...string) Option {
} }
} }
// Namespace allows multiple isolated stores to be kept in one backend, if supported. // Database allows multiple isolated stores to be kept in one backend, if supported.
// For example multiple tables in a SQL store. func Database(db string) Option {
func Namespace(ns string) Option {
return func(o *Options) { return func(o *Options) {
o.Namespace = ns o.Database = db
} }
} }
// Prefix sets a global prefix on all keys // Table is analagous to a table in database backends or a key prefix in KV backends
func Prefix(p string) Option { func Table(t string) Option {
return func(o *Options) { return func(o *Options) {
o.Prefix = p o.Table = t
}
}
// Suffix sets a global suffix on all keys
func Suffix(s string) Option {
return func(o *Options) {
o.Suffix = s
} }
} }

View File

@ -19,7 +19,7 @@ func NewScope(s store.Store, prefix string) Scope {
func (s *Scope) Options() store.Options { func (s *Scope) Options() store.Options {
o := s.Store.Options() o := s.Store.Options()
o.Prefix = s.prefix o.Table = s.prefix
return o return o
} }

View File

@ -36,8 +36,8 @@ func (s *serviceStore) Init(opts ...store.Option) error {
for _, o := range opts { for _, o := range opts {
o(&s.options) o(&s.options)
} }
s.Namespace = s.options.Namespace s.Namespace = s.options.Database
s.Prefix = s.options.Prefix s.Prefix = s.options.Table
s.Nodes = s.options.Nodes s.Nodes = s.options.Nodes
return nil return nil
@ -165,8 +165,8 @@ func NewStore(opts ...store.Option) store.Store {
service := &serviceStore{ service := &serviceStore{
options: options, options: options,
Namespace: options.Namespace, Namespace: options.Database,
Prefix: options.Prefix, Prefix: options.Table,
Nodes: options.Nodes, Nodes: options.Nodes,
Client: pb.NewStoreService("go.micro.store", client.DefaultClient), Client: pb.NewStoreService("go.micro.store", client.DefaultClient),
} }