2019-06-12 07:46:20 +01:00
|
|
|
// Package memory is a in-memory store store
|
2019-06-11 17:49:34 +01:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
2020-03-12 13:41:30 +00:00
|
|
|
"sort"
|
2020-01-08 22:23:14 +00:00
|
|
|
"strings"
|
2019-06-11 17:49:34 +01:00
|
|
|
"time"
|
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/store"
|
2020-03-12 13:41:30 +00:00
|
|
|
|
|
|
|
"github.com/patrickmn/go-cache"
|
|
|
|
"github.com/pkg/errors"
|
2019-06-11 17:49:34 +01:00
|
|
|
)
|
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
// 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),
|
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&s.options)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2019-06-12 07:46:20 +01:00
|
|
|
type memoryStore struct {
|
2019-12-16 14:38:51 +00:00
|
|
|
options store.Options
|
2019-06-11 17:49:34 +01:00
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
store *cache.Cache
|
2019-06-11 17:49:34 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 09:51:10 +01:00
|
|
|
func (m *memoryStore) Close() error {
|
|
|
|
m.store.Flush()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-08 12:11:31 +00:00
|
|
|
func (m *memoryStore) Init(opts ...store.Option) error {
|
2020-03-12 13:41:30 +00:00
|
|
|
m.store.Flush()
|
2020-01-08 12:11:31 +00:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&m.options)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
func (m *memoryStore) String() string {
|
|
|
|
return "memory"
|
|
|
|
}
|
2019-06-11 17:49:34 +01:00
|
|
|
|
2020-03-12 13:41:30 +00: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 17:49:34 +01:00
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
var keys []string
|
2019-06-11 17:49:34 +01:00
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
// Handle Prefix / suffix
|
|
|
|
if readOpts.Prefix || readOpts.Suffix {
|
|
|
|
var opts []store.ListOption
|
|
|
|
if readOpts.Prefix {
|
|
|
|
opts = append(opts, store.ListPrefix(key))
|
|
|
|
}
|
|
|
|
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()")
|
2019-07-11 12:58:20 +08:00
|
|
|
}
|
2020-03-12 13:41:30 +00:00
|
|
|
keys = k
|
|
|
|
} else {
|
|
|
|
keys = []string{key}
|
|
|
|
}
|
2019-07-11 12:51:55 +08:00
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
var results []*store.Record
|
|
|
|
for _, k := range keys {
|
|
|
|
r, err := m.get(k)
|
|
|
|
if err != nil {
|
|
|
|
return results, err
|
|
|
|
}
|
|
|
|
results = append(results, r)
|
2019-06-11 17:49:34 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
return results, nil
|
2019-06-11 17:49:34 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
func (m *memoryStore) get(k string) (*store.Record, error) {
|
2020-04-06 16:45:55 +01:00
|
|
|
if len(m.options.Table) > 0 {
|
|
|
|
k = m.options.Table + "/" + k
|
2020-03-12 13:41:30 +00:00
|
|
|
}
|
2020-04-06 16:45:55 +01:00
|
|
|
if len(m.options.Database) > 0 {
|
|
|
|
k = m.options.Database + "/" + k
|
2020-03-12 13:41:30 +00:00
|
|
|
}
|
|
|
|
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)
|
2020-03-19 18:19:07 +00:00
|
|
|
if !storedRecord.expiresAt.IsZero() {
|
|
|
|
newRecord.Expiry = time.Until(storedRecord.expiresAt)
|
|
|
|
}
|
2019-06-11 17:49:34 +01:00
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
return newRecord, nil
|
|
|
|
}
|
2020-01-08 22:23:14 +00:00
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
func (m *memoryStore) Write(r *store.Record, opts ...store.WriteOption) error {
|
|
|
|
writeOpts := store.WriteOptions{}
|
2020-01-08 22:23:14 +00:00
|
|
|
for _, o := range opts {
|
2020-03-12 13:41:30 +00:00
|
|
|
o(&writeOpts)
|
2020-01-08 22:23:14 +00:00
|
|
|
}
|
2019-06-11 17:49:34 +01:00
|
|
|
|
2020-03-12 13:41:30 +00: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))
|
|
|
|
copy(newRecord.Value, r.Value)
|
|
|
|
newRecord.Expiry = r.Expiry
|
2020-01-08 22:23:14 +00:00
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
if !writeOpts.Expiry.IsZero() {
|
|
|
|
newRecord.Expiry = time.Until(writeOpts.Expiry)
|
2020-01-08 22:23:14 +00:00
|
|
|
}
|
2020-03-12 13:41:30 +00:00
|
|
|
if writeOpts.TTL != 0 {
|
|
|
|
newRecord.Expiry = writeOpts.TTL
|
2020-02-03 08:16:02 +00:00
|
|
|
}
|
2020-03-12 13:41:30 +00:00
|
|
|
m.set(&newRecord)
|
2020-02-03 08:16:02 +00:00
|
|
|
} else {
|
2020-03-12 13:41:30 +00:00
|
|
|
m.set(r)
|
2020-01-08 22:23:14 +00:00
|
|
|
}
|
2020-03-12 13:41:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-01-08 22:23:14 +00:00
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
func (m *memoryStore) set(r *store.Record) {
|
|
|
|
key := r.Key
|
2020-04-06 16:45:55 +01:00
|
|
|
if len(m.options.Table) > 0 {
|
|
|
|
key = m.options.Table + "/" + key
|
2020-03-12 13:41:30 +00:00
|
|
|
}
|
2020-04-06 16:45:55 +01:00
|
|
|
if len(m.options.Database) > 0 {
|
|
|
|
key = m.options.Database + "/" + key
|
2020-03-12 13:41:30 +00:00
|
|
|
}
|
2019-10-11 14:08:50 +01:00
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
// 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)
|
2019-07-11 12:58:20 +08:00
|
|
|
}
|
2019-07-11 12:51:55 +08:00
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
m.store.Set(key, i, r.Expiry)
|
2019-06-11 17:49:34 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
func (m *memoryStore) Delete(key string, opts ...store.DeleteOption) error {
|
|
|
|
deleteOptions := store.DeleteOptions{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&deleteOptions)
|
2019-06-11 17:49:34 +01:00
|
|
|
}
|
2020-03-12 13:41:30 +00:00
|
|
|
m.delete(key)
|
2019-06-11 17:49:34 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
func (m *memoryStore) delete(key string) {
|
2020-04-06 16:45:55 +01:00
|
|
|
if len(m.options.Table) > 0 {
|
|
|
|
key = m.options.Table + "/" + key
|
2020-03-12 13:41:30 +00:00
|
|
|
}
|
2020-04-06 16:45:55 +01:00
|
|
|
if len(m.options.Database) > 0 {
|
|
|
|
key = m.options.Database + "/" + key
|
2020-03-12 13:41:30 +00:00
|
|
|
}
|
|
|
|
m.store.Delete(key)
|
2019-06-11 17:49:34 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
func (m *memoryStore) Options() store.Options {
|
|
|
|
return m.options
|
2020-01-10 19:13:55 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
func (m *memoryStore) List(opts ...store.ListOption) ([]string, error) {
|
|
|
|
listOptions := store.ListOptions{}
|
|
|
|
|
2019-12-16 14:38:51 +00:00
|
|
|
for _, o := range opts {
|
2020-03-12 13:41:30 +00:00
|
|
|
o(&listOptions)
|
2019-12-16 14:38:51 +00:00
|
|
|
}
|
2020-03-12 13:41:30 +00:00
|
|
|
allKeys := m.list(listOptions.Limit, listOptions.Offset)
|
2019-06-11 17:49:34 +01:00
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
if len(listOptions.Prefix) > 0 {
|
|
|
|
var prefixKeys []string
|
|
|
|
for _, k := range allKeys {
|
|
|
|
if strings.HasPrefix(k, listOptions.Prefix) {
|
|
|
|
prefixKeys = append(prefixKeys, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
allKeys = prefixKeys
|
|
|
|
}
|
|
|
|
if len(listOptions.Suffix) > 0 {
|
|
|
|
var suffixKeys []string
|
|
|
|
for _, k := range allKeys {
|
|
|
|
if strings.HasSuffix(k, listOptions.Suffix) {
|
|
|
|
suffixKeys = append(suffixKeys, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
allKeys = suffixKeys
|
2019-06-11 17:49:34 +01:00
|
|
|
}
|
2020-03-12 13:41:30 +00:00
|
|
|
|
|
|
|
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 {
|
2020-04-06 16:45:55 +01:00
|
|
|
if len(m.options.Database) > 0 {
|
|
|
|
k = strings.TrimPrefix(k, m.options.Database+"/")
|
2020-03-12 13:41:30 +00:00
|
|
|
}
|
2020-04-06 16:45:55 +01:00
|
|
|
if len(m.options.Table) > 0 {
|
|
|
|
k = strings.TrimPrefix(k, m.options.Table+"/")
|
2020-03-12 13:41:30 +00:00
|
|
|
}
|
|
|
|
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
|
2019-06-11 17:49:34 +01:00
|
|
|
}
|