Move data to store

This commit is contained in:
Asim Aslam 2019-06-12 07:46:20 +01:00
parent f81f66c98b
commit a5412dd4a0
4 changed files with 33 additions and 33 deletions

View File

@ -6,8 +6,8 @@ import (
"net" "net"
"github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api"
"github.com/micro/go-micro/data"
"github.com/micro/go-micro/options" "github.com/micro/go-micro/options"
"github.com/micro/go-micro/store"
) )
type ckv struct { type ckv struct {
@ -15,17 +15,17 @@ type ckv struct {
client *api.Client client *api.Client
} }
func (c *ckv) Read(key string) (*data.Record, error) { func (c *ckv) Read(key string) (*store.Record, error) {
keyval, _, err := c.client.KV().Get(key, nil) keyval, _, err := c.client.KV().Get(key, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if keyval == nil { if keyval == nil {
return nil, data.ErrNotFound return nil, store.ErrNotFound
} }
return &data.Record{ return &store.Record{
Key: keyval.Key, Key: keyval.Key,
Value: keyval.Value, Value: keyval.Value,
}, nil }, nil
@ -36,7 +36,7 @@ func (c *ckv) Delete(key string) error {
return err return err
} }
func (c *ckv) Write(record *data.Record) error { func (c *ckv) Write(record *store.Record) error {
_, err := c.client.KV().Put(&api.KVPair{ _, err := c.client.KV().Put(&api.KVPair{
Key: record.Key, Key: record.Key,
Value: record.Value, Value: record.Value,
@ -44,17 +44,17 @@ func (c *ckv) Write(record *data.Record) error {
return err return err
} }
func (c *ckv) Dump() ([]*data.Record, error) { func (c *ckv) Dump() ([]*store.Record, error) {
keyval, _, err := c.client.KV().List("/", nil) keyval, _, err := c.client.KV().List("/", nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if keyval == nil { if keyval == nil {
return nil, data.ErrNotFound return nil, store.ErrNotFound
} }
var vals []*data.Record var vals []*store.Record
for _, keyv := range keyval { for _, keyv := range keyval {
vals = append(vals, &data.Record{ vals = append(vals, &store.Record{
Key: keyv.Key, Key: keyv.Key,
Value: keyv.Value, Value: keyv.Value,
}) })
@ -66,13 +66,13 @@ func (c *ckv) String() string {
return "consul" return "consul"
} }
func NewData(opts ...options.Option) data.Data { func NewStore(opts ...options.Option) store.Store {
options := options.NewOptions(opts...) options := options.NewOptions(opts...)
config := api.DefaultConfig() config := api.DefaultConfig()
var nodes []string var nodes []string
if n, ok := options.Values().Get("data.nodes"); ok { if n, ok := options.Values().Get("store.nodes"); ok {
nodes = n.([]string) nodes = n.([]string)
} }

View File

@ -1,15 +1,15 @@
// Package memory is a in-memory data store // Package memory is a in-memory store store
package memory package memory
import ( import (
"sync" "sync"
"time" "time"
"github.com/micro/go-micro/data"
"github.com/micro/go-micro/options" "github.com/micro/go-micro/options"
"github.com/micro/go-micro/store"
) )
type memoryData struct { type memoryStore struct {
options.Options options.Options
sync.RWMutex sync.RWMutex
@ -17,15 +17,15 @@ type memoryData struct {
} }
type memoryRecord struct { type memoryRecord struct {
r *data.Record r *store.Record
c time.Time c time.Time
} }
func (m *memoryData) Dump() ([]*data.Record, error) { func (m *memoryStore) Dump() ([]*store.Record, error) {
m.RLock() m.RLock()
defer m.RUnlock() defer m.RUnlock()
var values []*data.Record var values []*store.Record
for _, v := range m.values { for _, v := range m.values {
// get expiry // get expiry
@ -42,13 +42,13 @@ func (m *memoryData) Dump() ([]*data.Record, error) {
return values, nil return values, nil
} }
func (m *memoryData) Read(key string) (*data.Record, error) { func (m *memoryStore) Read(key string) (*store.Record, error) {
m.RLock() m.RLock()
defer m.RUnlock() defer m.RUnlock()
v, ok := m.values[key] v, ok := m.values[key]
if !ok { if !ok {
return nil, data.ErrNotFound return nil, store.ErrNotFound
} }
// get expiry // get expiry
@ -57,13 +57,13 @@ func (m *memoryData) Read(key string) (*data.Record, error) {
// expired // expired
if d > time.Duration(0) && t > d { if d > time.Duration(0) && t > d {
return nil, data.ErrNotFound return nil, store.ErrNotFound
} }
return v.r, nil return v.r, nil
} }
func (m *memoryData) Write(r *data.Record) error { func (m *memoryStore) Write(r *store.Record) error {
m.Lock() m.Lock()
defer m.Unlock() defer m.Unlock()
@ -76,7 +76,7 @@ func (m *memoryData) Write(r *data.Record) error {
return nil return nil
} }
func (m *memoryData) Delete(key string) error { func (m *memoryStore) Delete(key string) error {
m.Lock() m.Lock()
defer m.Unlock() defer m.Unlock()
@ -86,11 +86,11 @@ func (m *memoryData) Delete(key string) error {
return nil return nil
} }
// NewData returns a new data.Data // NewStore returns a new store.Store
func NewData(opts ...options.Option) data.Data { func NewStore(opts ...options.Option) store.Store {
options := options.NewOptions(opts...) options := options.NewOptions(opts...)
return &memoryData{ return &memoryStore{
Options: options, Options: options,
values: make(map[string]*memoryRecord), values: make(map[string]*memoryRecord),
} }

View File

@ -1,15 +1,15 @@
package data package store
import ( import (
"github.com/micro/go-micro/options" "github.com/micro/go-micro/options"
) )
// Set the nodes used to back the data // Set the nodes used to back the store
func Nodes(a ...string) options.Option { func Nodes(a ...string) options.Option {
return options.WithValue("data.nodes", a) return options.WithValue("store.nodes", a)
} }
// Prefix sets a prefix to any key ids used // Prefix sets a prefix to any key ids used
func Prefix(p string) options.Option { func Prefix(p string) options.Option {
return options.WithValue("data.prefix", p) return options.WithValue("store.prefix", p)
} }

View File

@ -1,5 +1,5 @@
// Package data is an interface for distribute data storage. // Package store is an interface for distribute data storage.
package data package store
import ( import (
"errors" "errors"
@ -12,8 +12,8 @@ var (
ErrNotFound = errors.New("not found") ErrNotFound = errors.New("not found")
) )
// Data is a data storage interface // Store is a data storage interface
type Data interface { type Store interface {
// embed options // embed options
options.Options options.Options
// Dump the known records // Dump the known records