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",
},
&cli.StringFlag{
Name: "store_namespace",
EnvVars: []string{"MICRO_STORE_NAMESPACE"},
Usage: "Namespace for store data",
Name: "store_database",
EnvVars: []string{"MICRO_STORE_DATABASE"},
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{
Name: "transport",
@ -622,9 +627,15 @@ func (c *cmd) Before(ctx *cli.Context) error {
}
}
if len(ctx.String("store_namespace")) > 0 {
if err := (*c.opts.Store).Init(store.Namespace(ctx.String("store_namespace"))); err != nil {
logger.Fatalf("Error configuring store: %v", err)
if len(ctx.String("store_database")) > 0 {
if err := (*c.opts.Store).Init(store.Database(ctx.String("store_database"))); err != nil {
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/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)
}
// If the store has no namespace set, fallback to the
// If the store has no Table set, fallback to the
// services name
if len(store.DefaultStore.Options().Namespace) == 0 {
if len(store.DefaultStore.Options().Table) == 0 {
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

View File

@ -10,7 +10,7 @@ import (
)
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()
assert := assert.New(t)

View File

@ -105,8 +105,8 @@ func (w *workersKV) Init(opts ...store.Option) error {
for _, o := range opts {
o(&w.options)
}
if len(w.options.Namespace) > 0 {
w.namespace = w.options.Namespace
if len(w.options.Database) > 0 {
w.namespace = w.options.Database
}
ttl := w.options.Context.Value("STORE_CACHE_TTL")
if ttl != nil {
@ -388,7 +388,7 @@ func NewStore(opts ...store.Option) store.Store {
}
if len(namespace) == 0 {
namespace = options.Namespace
namespace = options.Database
}
// 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
func Namespace(ns string) store.Option {
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"}
}
namespace := s.options.Namespace
namespace := s.options.Database
if len(namespace) == 0 {
namespace = DefaultNamespace
}
prefix := s.options.Prefix
prefix := s.options.Table
if len(prefix) == 0 {
prefix = DefaultPrefix
}

View File

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

View File

@ -60,11 +60,11 @@ func (e *etcdStore) init() error {
}
e.client = client
ns := ""
if len(e.options.Prefix) > 0 {
ns = e.options.Prefix
if len(e.options.Table) > 0 {
ns = e.options.Table
}
if len(e.options.Namespace) > 0 {
ns = e.options.Namespace + "/" + ns
if len(e.options.Database) > 0 {
ns = e.options.Database + "/" + ns
}
if len(ns) > 0 {
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) {
if len(m.options.Suffix) > 0 {
k = k + m.options.Suffix
if len(m.options.Table) > 0 {
k = m.options.Table + "/" + k
}
if len(m.options.Prefix) > 0 {
k = m.options.Prefix + "/" + k
}
if len(m.options.Namespace) > 0 {
k = m.options.Namespace + "/" + k
if len(m.options.Database) > 0 {
k = m.options.Database + "/" + k
}
var storedRecord *internalRecord
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) {
key := r.Key
if len(m.options.Suffix) > 0 {
key = key + m.options.Suffix
if len(m.options.Table) > 0 {
key = m.options.Table + "/" + key
}
if len(m.options.Prefix) > 0 {
key = m.options.Prefix + "/" + key
}
if len(m.options.Namespace) > 0 {
key = m.options.Namespace + "/" + key
if len(m.options.Database) > 0 {
key = m.options.Database + "/" + key
}
// 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) {
if len(m.options.Suffix) > 0 {
key = key + m.options.Suffix
if len(m.options.Table) > 0 {
key = m.options.Table + "/" + key
}
if len(m.options.Prefix) > 0 {
key = m.options.Prefix + "/" + key
}
if len(m.options.Namespace) > 0 {
key = m.options.Namespace + "/" + key
if len(m.options.Database) > 0 {
key = m.options.Database + "/" + key
}
m.store.Delete(key)
}
@ -226,14 +217,11 @@ func (m *memoryStore) list(limit, offset uint) []string {
allKeys := make([]string, len(allItems))
i := 0
for k := range allItems {
if len(m.options.Suffix) > 0 {
k = strings.TrimSuffix(k, m.options.Suffix)
if len(m.options.Database) > 0 {
k = strings.TrimPrefix(k, m.options.Database+"/")
}
if len(m.options.Namespace) > 0 {
k = strings.TrimPrefix(k, m.options.Namespace+"/")
}
if len(m.options.Prefix) > 0 {
k = strings.TrimPrefix(k, m.options.Prefix+"/")
if len(m.options.Table) > 0 {
k = strings.TrimPrefix(k, m.options.Table+"/")
}
allKeys[i] = k
i++

View File

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

View File

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

View File

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

View File

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