Add opts to service proto (#1517)
* Add opts to service proto * Support database/table opts
This commit is contained in:
@@ -2,12 +2,12 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/micro/go-micro/v2/store"
|
||||
|
||||
"github.com/patrickmn/go-cache"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@@ -15,8 +15,11 @@ import (
|
||||
// NewStore returns a memory store
|
||||
func NewStore(opts ...store.Option) store.Store {
|
||||
s := &memoryStore{
|
||||
options: store.Options{},
|
||||
store: cache.New(cache.NoExpiration, 5*time.Minute),
|
||||
options: store.Options{
|
||||
Database: "micro",
|
||||
Table: "micro",
|
||||
},
|
||||
store: cache.New(cache.NoExpiration, 5*time.Minute),
|
||||
}
|
||||
for _, o := range opts {
|
||||
o(&s.options)
|
||||
@@ -30,6 +33,101 @@ type memoryStore struct {
|
||||
store *cache.Cache
|
||||
}
|
||||
|
||||
type internalRecord struct {
|
||||
key string
|
||||
value []byte
|
||||
expiresAt time.Time
|
||||
}
|
||||
|
||||
func (m *memoryStore) key(prefix, key string) string {
|
||||
return filepath.Join(prefix, key)
|
||||
}
|
||||
|
||||
func (m *memoryStore) prefix(database, table string) string {
|
||||
if len(database) == 0 {
|
||||
database = m.options.Database
|
||||
}
|
||||
if len(table) == 0 {
|
||||
table = m.options.Table
|
||||
}
|
||||
return filepath.Join(database, table)
|
||||
}
|
||||
|
||||
func (m *memoryStore) get(prefix, key string) (*store.Record, error) {
|
||||
key = m.key(prefix, key)
|
||||
|
||||
var storedRecord *internalRecord
|
||||
r, found := m.store.Get(key)
|
||||
if !found {
|
||||
return nil, store.ErrNotFound
|
||||
}
|
||||
|
||||
storedRecord, ok := r.(*internalRecord)
|
||||
if !ok {
|
||||
return nil, errors.New("Retrieved a non *internalRecord from the cache")
|
||||
}
|
||||
|
||||
// Copy the record on the way out
|
||||
newRecord := &store.Record{}
|
||||
newRecord.Key = strings.TrimPrefix(storedRecord.key, prefix+"/")
|
||||
newRecord.Value = make([]byte, len(storedRecord.value))
|
||||
copy(newRecord.Value, storedRecord.value)
|
||||
if !storedRecord.expiresAt.IsZero() {
|
||||
newRecord.Expiry = time.Until(storedRecord.expiresAt)
|
||||
}
|
||||
|
||||
return newRecord, nil
|
||||
}
|
||||
|
||||
func (m *memoryStore) set(prefix string, r *store.Record) {
|
||||
key := m.key(prefix, r.Key)
|
||||
|
||||
// copy the incoming record and then
|
||||
// convert the expiry in to a hard timestamp
|
||||
i := &internalRecord{}
|
||||
i.key = r.Key
|
||||
i.value = make([]byte, len(r.Value))
|
||||
copy(i.value, r.Value)
|
||||
|
||||
if r.Expiry != 0 {
|
||||
i.expiresAt = time.Now().Add(r.Expiry)
|
||||
}
|
||||
|
||||
m.store.Set(key, i, r.Expiry)
|
||||
}
|
||||
|
||||
func (m *memoryStore) delete(prefix, key string) {
|
||||
key = m.key(prefix, key)
|
||||
m.store.Delete(key)
|
||||
}
|
||||
|
||||
func (m *memoryStore) list(prefix string, limit, offset uint) []string {
|
||||
allItems := m.store.Items()
|
||||
allKeys := make([]string, len(allItems))
|
||||
i := 0
|
||||
|
||||
for k := range allItems {
|
||||
if !strings.HasPrefix(k, prefix+"/") {
|
||||
continue
|
||||
}
|
||||
allKeys[i] = strings.TrimPrefix(k, prefix+"/")
|
||||
i++
|
||||
}
|
||||
|
||||
if limit != 0 || offset != 0 {
|
||||
sort.Slice(allKeys, func(i, j int) bool { return allKeys[i] < allKeys[j] })
|
||||
min := func(i, j uint) uint {
|
||||
if i < j {
|
||||
return i
|
||||
}
|
||||
return j
|
||||
}
|
||||
return allKeys[offset:min(limit, uint(len(allKeys)))]
|
||||
}
|
||||
|
||||
return allKeys
|
||||
}
|
||||
|
||||
func (m *memoryStore) Close() error {
|
||||
m.store.Flush()
|
||||
return nil
|
||||
@@ -53,31 +151,33 @@ func (m *memoryStore) Read(key string, opts ...store.ReadOption) ([]*store.Recor
|
||||
o(&readOpts)
|
||||
}
|
||||
|
||||
prefix := m.prefix(readOpts.Database, readOpts.Table)
|
||||
|
||||
var keys []string
|
||||
|
||||
// Handle Prefix / suffix
|
||||
if readOpts.Prefix || readOpts.Suffix {
|
||||
var opts []store.ListOption
|
||||
if readOpts.Prefix {
|
||||
opts = append(opts, store.ListPrefix(key))
|
||||
k := m.list(prefix, readOpts.Limit, readOpts.Offset)
|
||||
|
||||
for _, kk := range k {
|
||||
if readOpts.Prefix && !strings.HasPrefix(kk, key) {
|
||||
continue
|
||||
}
|
||||
|
||||
if readOpts.Suffix && !strings.HasSuffix(kk, key) {
|
||||
continue
|
||||
}
|
||||
|
||||
keys = append(keys, kk)
|
||||
}
|
||||
if readOpts.Suffix {
|
||||
opts = append(opts, store.ListSuffix(key))
|
||||
}
|
||||
opts = append(opts, store.ListLimit(readOpts.Limit))
|
||||
opts = append(opts, store.ListOffset(readOpts.Offset))
|
||||
k, err := m.List(opts...)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Memory: Read couldn't List()")
|
||||
}
|
||||
keys = k
|
||||
} else {
|
||||
keys = []string{key}
|
||||
}
|
||||
|
||||
var results []*store.Record
|
||||
|
||||
for _, k := range keys {
|
||||
r, err := m.get(k)
|
||||
r, err := m.get(prefix, k)
|
||||
if err != nil {
|
||||
return results, err
|
||||
}
|
||||
@@ -87,40 +187,14 @@ func (m *memoryStore) Read(key string, opts ...store.ReadOption) ([]*store.Recor
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func (m *memoryStore) get(k string) (*store.Record, error) {
|
||||
if len(m.options.Table) > 0 {
|
||||
k = m.options.Table + "/" + k
|
||||
}
|
||||
if len(m.options.Database) > 0 {
|
||||
k = m.options.Database + "/" + k
|
||||
}
|
||||
var storedRecord *internalRecord
|
||||
r, found := m.store.Get(k)
|
||||
if !found {
|
||||
return nil, store.ErrNotFound
|
||||
}
|
||||
storedRecord, ok := r.(*internalRecord)
|
||||
if !ok {
|
||||
return nil, errors.New("Retrieved a non *internalRecord from the cache")
|
||||
}
|
||||
// Copy the record on the way out
|
||||
newRecord := &store.Record{}
|
||||
newRecord.Key = storedRecord.key
|
||||
newRecord.Value = make([]byte, len(storedRecord.value))
|
||||
copy(newRecord.Value, storedRecord.value)
|
||||
if !storedRecord.expiresAt.IsZero() {
|
||||
newRecord.Expiry = time.Until(storedRecord.expiresAt)
|
||||
}
|
||||
|
||||
return newRecord, nil
|
||||
}
|
||||
|
||||
func (m *memoryStore) Write(r *store.Record, opts ...store.WriteOption) error {
|
||||
writeOpts := store.WriteOptions{}
|
||||
for _, o := range opts {
|
||||
o(&writeOpts)
|
||||
}
|
||||
|
||||
prefix := m.prefix(writeOpts.Database, writeOpts.Table)
|
||||
|
||||
if len(opts) > 0 {
|
||||
// Copy the record before applying options, or the incoming record will be mutated
|
||||
newRecord := store.Record{}
|
||||
@@ -135,52 +209,25 @@ func (m *memoryStore) Write(r *store.Record, opts ...store.WriteOption) error {
|
||||
if writeOpts.TTL != 0 {
|
||||
newRecord.Expiry = writeOpts.TTL
|
||||
}
|
||||
m.set(&newRecord)
|
||||
} else {
|
||||
m.set(r)
|
||||
m.set(prefix, &newRecord)
|
||||
return nil
|
||||
}
|
||||
|
||||
// set
|
||||
m.set(prefix, r)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *memoryStore) set(r *store.Record) {
|
||||
key := r.Key
|
||||
if len(m.options.Table) > 0 {
|
||||
key = m.options.Table + "/" + key
|
||||
}
|
||||
if len(m.options.Database) > 0 {
|
||||
key = m.options.Database + "/" + key
|
||||
}
|
||||
|
||||
// copy the incoming record and then
|
||||
// convert the expiry in to a hard timestamp
|
||||
i := &internalRecord{}
|
||||
i.key = r.Key
|
||||
i.value = make([]byte, len(r.Value))
|
||||
copy(i.value, r.Value)
|
||||
if r.Expiry != 0 {
|
||||
i.expiresAt = time.Now().Add(r.Expiry)
|
||||
}
|
||||
|
||||
m.store.Set(key, i, r.Expiry)
|
||||
}
|
||||
|
||||
func (m *memoryStore) Delete(key string, opts ...store.DeleteOption) error {
|
||||
deleteOptions := store.DeleteOptions{}
|
||||
for _, o := range opts {
|
||||
o(&deleteOptions)
|
||||
}
|
||||
m.delete(key)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *memoryStore) delete(key string) {
|
||||
if len(m.options.Table) > 0 {
|
||||
key = m.options.Table + "/" + key
|
||||
}
|
||||
if len(m.options.Database) > 0 {
|
||||
key = m.options.Database + "/" + key
|
||||
}
|
||||
m.store.Delete(key)
|
||||
prefix := m.prefix(deleteOptions.Database, deleteOptions.Table)
|
||||
m.delete(prefix, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *memoryStore) Options() store.Options {
|
||||
@@ -193,59 +240,29 @@ func (m *memoryStore) List(opts ...store.ListOption) ([]string, error) {
|
||||
for _, o := range opts {
|
||||
o(&listOptions)
|
||||
}
|
||||
allKeys := m.list(listOptions.Limit, listOptions.Offset)
|
||||
|
||||
prefix := m.prefix(listOptions.Database, listOptions.Table)
|
||||
keys := m.list(prefix, listOptions.Limit, listOptions.Offset)
|
||||
|
||||
if len(listOptions.Prefix) > 0 {
|
||||
var prefixKeys []string
|
||||
for _, k := range allKeys {
|
||||
for _, k := range keys {
|
||||
if strings.HasPrefix(k, listOptions.Prefix) {
|
||||
prefixKeys = append(prefixKeys, k)
|
||||
}
|
||||
}
|
||||
allKeys = prefixKeys
|
||||
keys = prefixKeys
|
||||
}
|
||||
|
||||
if len(listOptions.Suffix) > 0 {
|
||||
var suffixKeys []string
|
||||
for _, k := range allKeys {
|
||||
for _, k := range keys {
|
||||
if strings.HasSuffix(k, listOptions.Suffix) {
|
||||
suffixKeys = append(suffixKeys, k)
|
||||
}
|
||||
}
|
||||
allKeys = suffixKeys
|
||||
keys = suffixKeys
|
||||
}
|
||||
|
||||
return allKeys, nil
|
||||
}
|
||||
|
||||
func (m *memoryStore) list(limit, offset uint) []string {
|
||||
allItems := m.store.Items()
|
||||
allKeys := make([]string, len(allItems))
|
||||
i := 0
|
||||
for k := range allItems {
|
||||
if len(m.options.Database) > 0 {
|
||||
k = strings.TrimPrefix(k, m.options.Database+"/")
|
||||
}
|
||||
if len(m.options.Table) > 0 {
|
||||
k = strings.TrimPrefix(k, m.options.Table+"/")
|
||||
}
|
||||
allKeys[i] = k
|
||||
i++
|
||||
}
|
||||
if limit != 0 || offset != 0 {
|
||||
sort.Slice(allKeys, func(i, j int) bool { return allKeys[i] < allKeys[j] })
|
||||
min := func(i, j uint) uint {
|
||||
if i < j {
|
||||
return i
|
||||
}
|
||||
return j
|
||||
}
|
||||
return allKeys[offset:min(limit, uint(len(allKeys)))]
|
||||
}
|
||||
return allKeys
|
||||
}
|
||||
|
||||
type internalRecord struct {
|
||||
key string
|
||||
value []byte
|
||||
expiresAt time.Time
|
||||
return keys, nil
|
||||
}
|
||||
|
@@ -259,13 +259,13 @@ func basictest(s store.Store, t *testing.T) {
|
||||
t.Error(err)
|
||||
} else {
|
||||
if len(results) != 5 {
|
||||
t.Error("Expected 5 results, got ", len(results))
|
||||
t.Fatal("Expected 5 results, got ", len(results))
|
||||
}
|
||||
if results[0].Key != "a0" {
|
||||
t.Errorf("Expected a0, got %s", results[0].Key)
|
||||
t.Fatalf("Expected a0, got %s", results[0].Key)
|
||||
}
|
||||
if results[4].Key != "a4" {
|
||||
t.Errorf("Expected a4, got %s", results[4].Key)
|
||||
t.Fatalf("Expected a4, got %s", results[4].Key)
|
||||
}
|
||||
}
|
||||
if results, err := s.Read("a", store.ReadLimit(30), store.ReadOffset(5), store.ReadPrefix()); err != nil {
|
||||
|
Reference in New Issue
Block a user