2019-06-12 09:46:20 +03:00
|
|
|
// Package memory is a in-memory store store
|
2019-06-11 19:49:34 +03:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
2020-05-01 00:51:25 +03:00
|
|
|
"path/filepath"
|
2020-03-12 16:41:30 +03:00
|
|
|
"sort"
|
2020-01-09 01:23:14 +03:00
|
|
|
"strings"
|
2019-06-11 19:49:34 +03:00
|
|
|
"time"
|
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/store"
|
2020-03-12 16:41:30 +03:00
|
|
|
"github.com/patrickmn/go-cache"
|
|
|
|
"github.com/pkg/errors"
|
2019-06-11 19:49:34 +03:00
|
|
|
)
|
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
// NewStore returns a memory store
|
|
|
|
func NewStore(opts ...store.Option) store.Store {
|
|
|
|
s := &memoryStore{
|
2020-05-01 00:51:25 +03:00
|
|
|
options: store.Options{
|
|
|
|
Database: "micro",
|
|
|
|
Table: "micro",
|
|
|
|
},
|
|
|
|
store: cache.New(cache.NoExpiration, 5*time.Minute),
|
2020-03-12 16:41:30 +03:00
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&s.options)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2019-06-12 09:46:20 +03:00
|
|
|
type memoryStore struct {
|
2019-12-16 17:38:51 +03:00
|
|
|
options store.Options
|
2019-06-11 19:49:34 +03:00
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
store *cache.Cache
|
2019-06-11 19:49:34 +03:00
|
|
|
}
|
|
|
|
|
2020-06-03 11:45:08 +03:00
|
|
|
type storeRecord struct {
|
2020-05-01 00:51:25 +03:00
|
|
|
key string
|
|
|
|
value []byte
|
2020-06-03 11:45:08 +03:00
|
|
|
metadata map[string]interface{}
|
2020-05-01 00:51:25 +03:00
|
|
|
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)
|
|
|
|
|
2020-06-03 11:45:08 +03:00
|
|
|
var storedRecord *storeRecord
|
2020-05-01 00:51:25 +03:00
|
|
|
r, found := m.store.Get(key)
|
|
|
|
if !found {
|
|
|
|
return nil, store.ErrNotFound
|
|
|
|
}
|
|
|
|
|
2020-06-03 11:45:08 +03:00
|
|
|
storedRecord, ok := r.(*storeRecord)
|
2020-05-01 00:51:25 +03:00
|
|
|
if !ok {
|
2020-06-03 11:45:08 +03:00
|
|
|
return nil, errors.New("Retrieved a non *storeRecord from the cache")
|
2020-05-01 00:51:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the record on the way out
|
|
|
|
newRecord := &store.Record{}
|
|
|
|
newRecord.Key = strings.TrimPrefix(storedRecord.key, prefix+"/")
|
|
|
|
newRecord.Value = make([]byte, len(storedRecord.value))
|
2020-06-03 11:45:08 +03:00
|
|
|
newRecord.Metadata = make(map[string]interface{})
|
|
|
|
|
|
|
|
// copy the value into the new record
|
2020-05-01 00:51:25 +03:00
|
|
|
copy(newRecord.Value, storedRecord.value)
|
2020-06-03 11:45:08 +03:00
|
|
|
|
|
|
|
// check if we need to set the expiry
|
2020-05-01 00:51:25 +03:00
|
|
|
if !storedRecord.expiresAt.IsZero() {
|
|
|
|
newRecord.Expiry = time.Until(storedRecord.expiresAt)
|
|
|
|
}
|
|
|
|
|
2020-06-03 11:45:08 +03:00
|
|
|
// copy in the metadata
|
|
|
|
for k, v := range storedRecord.metadata {
|
|
|
|
newRecord.Metadata[k] = v
|
|
|
|
}
|
|
|
|
|
2020-05-01 00:51:25 +03:00
|
|
|
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
|
2020-06-03 11:45:08 +03:00
|
|
|
i := &storeRecord{}
|
2020-05-01 00:51:25 +03:00
|
|
|
i.key = r.Key
|
|
|
|
i.value = make([]byte, len(r.Value))
|
2020-06-03 11:45:08 +03:00
|
|
|
i.metadata = make(map[string]interface{})
|
|
|
|
|
|
|
|
// copy the the value
|
2020-05-01 00:51:25 +03:00
|
|
|
copy(i.value, r.Value)
|
|
|
|
|
2020-06-03 11:45:08 +03:00
|
|
|
// set the expiry
|
2020-05-01 00:51:25 +03:00
|
|
|
if r.Expiry != 0 {
|
|
|
|
i.expiresAt = time.Now().Add(r.Expiry)
|
|
|
|
}
|
|
|
|
|
2020-06-03 11:45:08 +03:00
|
|
|
// set the metadata
|
|
|
|
for k, v := range r.Metadata {
|
|
|
|
i.metadata[k] = v
|
|
|
|
}
|
|
|
|
|
2020-05-01 00:51:25 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-08 11:51:10 +03:00
|
|
|
func (m *memoryStore) Close() error {
|
|
|
|
m.store.Flush()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-08 15:11:31 +03:00
|
|
|
func (m *memoryStore) Init(opts ...store.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&m.options)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
func (m *memoryStore) String() string {
|
|
|
|
return "memory"
|
|
|
|
}
|
2019-06-11 19:49:34 +03:00
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
func (m *memoryStore) Read(key string, opts ...store.ReadOption) ([]*store.Record, error) {
|
|
|
|
readOpts := store.ReadOptions{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&readOpts)
|
|
|
|
}
|
2019-06-11 19:49:34 +03:00
|
|
|
|
2020-05-01 00:51:25 +03:00
|
|
|
prefix := m.prefix(readOpts.Database, readOpts.Table)
|
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
var keys []string
|
2019-06-11 19:49:34 +03:00
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
// Handle Prefix / suffix
|
|
|
|
if readOpts.Prefix || readOpts.Suffix {
|
2020-05-01 00:51:25 +03:00
|
|
|
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)
|
2019-07-11 07:58:20 +03:00
|
|
|
}
|
2020-03-12 16:41:30 +03:00
|
|
|
} else {
|
|
|
|
keys = []string{key}
|
|
|
|
}
|
2019-07-11 07:51:55 +03:00
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
var results []*store.Record
|
2020-05-01 00:51:25 +03:00
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
for _, k := range keys {
|
2020-05-01 00:51:25 +03:00
|
|
|
r, err := m.get(prefix, k)
|
2020-03-12 16:41:30 +03:00
|
|
|
if err != nil {
|
|
|
|
return results, err
|
|
|
|
}
|
|
|
|
results = append(results, r)
|
2019-06-11 19:49:34 +03:00
|
|
|
}
|
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
return results, nil
|
2019-06-11 19:49:34 +03:00
|
|
|
}
|
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
func (m *memoryStore) Write(r *store.Record, opts ...store.WriteOption) error {
|
|
|
|
writeOpts := store.WriteOptions{}
|
2020-01-09 01:23:14 +03:00
|
|
|
for _, o := range opts {
|
2020-03-12 16:41:30 +03:00
|
|
|
o(&writeOpts)
|
2020-01-09 01:23:14 +03:00
|
|
|
}
|
2019-06-11 19:49:34 +03:00
|
|
|
|
2020-05-01 00:51:25 +03:00
|
|
|
prefix := m.prefix(writeOpts.Database, writeOpts.Table)
|
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
if len(opts) > 0 {
|
|
|
|
// Copy the record before applying options, or the incoming record will be mutated
|
|
|
|
newRecord := store.Record{}
|
|
|
|
newRecord.Key = r.Key
|
|
|
|
newRecord.Value = make([]byte, len(r.Value))
|
2020-06-03 11:45:08 +03:00
|
|
|
newRecord.Metadata = make(map[string]interface{})
|
2020-03-12 16:41:30 +03:00
|
|
|
copy(newRecord.Value, r.Value)
|
|
|
|
newRecord.Expiry = r.Expiry
|
2020-01-09 01:23:14 +03:00
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
if !writeOpts.Expiry.IsZero() {
|
|
|
|
newRecord.Expiry = time.Until(writeOpts.Expiry)
|
2020-01-09 01:23:14 +03:00
|
|
|
}
|
2020-03-12 16:41:30 +03:00
|
|
|
if writeOpts.TTL != 0 {
|
|
|
|
newRecord.Expiry = writeOpts.TTL
|
2020-02-03 11:16:02 +03:00
|
|
|
}
|
2020-06-03 11:45:08 +03:00
|
|
|
|
|
|
|
for k, v := range r.Metadata {
|
|
|
|
newRecord.Metadata[k] = v
|
|
|
|
}
|
|
|
|
|
2020-05-01 00:51:25 +03:00
|
|
|
m.set(prefix, &newRecord)
|
|
|
|
return nil
|
2020-03-12 16:41:30 +03:00
|
|
|
}
|
2019-10-11 16:08:50 +03:00
|
|
|
|
2020-05-01 00:51:25 +03:00
|
|
|
// set
|
|
|
|
m.set(prefix, r)
|
2019-07-11 07:51:55 +03:00
|
|
|
|
2020-05-01 00:51:25 +03:00
|
|
|
return nil
|
2019-06-11 19:49:34 +03:00
|
|
|
}
|
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
func (m *memoryStore) Delete(key string, opts ...store.DeleteOption) error {
|
|
|
|
deleteOptions := store.DeleteOptions{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&deleteOptions)
|
2019-06-11 19:49:34 +03:00
|
|
|
}
|
|
|
|
|
2020-05-01 00:51:25 +03:00
|
|
|
prefix := m.prefix(deleteOptions.Database, deleteOptions.Table)
|
|
|
|
m.delete(prefix, key)
|
|
|
|
return nil
|
2019-06-11 19:49:34 +03:00
|
|
|
}
|
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
func (m *memoryStore) Options() store.Options {
|
|
|
|
return m.options
|
2020-01-10 22:13:55 +03:00
|
|
|
}
|
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
func (m *memoryStore) List(opts ...store.ListOption) ([]string, error) {
|
|
|
|
listOptions := store.ListOptions{}
|
|
|
|
|
2019-12-16 17:38:51 +03:00
|
|
|
for _, o := range opts {
|
2020-03-12 16:41:30 +03:00
|
|
|
o(&listOptions)
|
2019-12-16 17:38:51 +03:00
|
|
|
}
|
2020-05-01 00:51:25 +03:00
|
|
|
|
|
|
|
prefix := m.prefix(listOptions.Database, listOptions.Table)
|
|
|
|
keys := m.list(prefix, listOptions.Limit, listOptions.Offset)
|
2019-06-11 19:49:34 +03:00
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
if len(listOptions.Prefix) > 0 {
|
|
|
|
var prefixKeys []string
|
2020-05-01 00:51:25 +03:00
|
|
|
for _, k := range keys {
|
2020-03-12 16:41:30 +03:00
|
|
|
if strings.HasPrefix(k, listOptions.Prefix) {
|
|
|
|
prefixKeys = append(prefixKeys, k)
|
|
|
|
}
|
|
|
|
}
|
2020-05-01 00:51:25 +03:00
|
|
|
keys = prefixKeys
|
2020-03-12 16:41:30 +03:00
|
|
|
}
|
2020-05-01 00:51:25 +03:00
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
if len(listOptions.Suffix) > 0 {
|
|
|
|
var suffixKeys []string
|
2020-05-01 00:51:25 +03:00
|
|
|
for _, k := range keys {
|
2020-03-12 16:41:30 +03:00
|
|
|
if strings.HasSuffix(k, listOptions.Suffix) {
|
|
|
|
suffixKeys = append(suffixKeys, k)
|
|
|
|
}
|
|
|
|
}
|
2020-05-01 00:51:25 +03:00
|
|
|
keys = suffixKeys
|
2020-03-12 16:41:30 +03:00
|
|
|
}
|
|
|
|
|
2020-05-01 00:51:25 +03:00
|
|
|
return keys, nil
|
2019-06-11 19:49:34 +03:00
|
|
|
}
|