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"
"github.com/hashicorp/consul/api"
"github.com/micro/go-micro/data"
"github.com/micro/go-micro/options"
"github.com/micro/go-micro/store"
)
type ckv struct {
@ -15,17 +15,17 @@ type ckv struct {
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)
if err != nil {
return nil, err
}
if keyval == nil {
return nil, data.ErrNotFound
return nil, store.ErrNotFound
}
return &data.Record{
return &store.Record{
Key: keyval.Key,
Value: keyval.Value,
}, nil
@ -36,7 +36,7 @@ func (c *ckv) Delete(key string) error {
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{
Key: record.Key,
Value: record.Value,
@ -44,17 +44,17 @@ func (c *ckv) Write(record *data.Record) error {
return err
}
func (c *ckv) Dump() ([]*data.Record, error) {
func (c *ckv) Dump() ([]*store.Record, error) {
keyval, _, err := c.client.KV().List("/", nil)
if err != nil {
return nil, err
}
if keyval == nil {
return nil, data.ErrNotFound
return nil, store.ErrNotFound
}
var vals []*data.Record
var vals []*store.Record
for _, keyv := range keyval {
vals = append(vals, &data.Record{
vals = append(vals, &store.Record{
Key: keyv.Key,
Value: keyv.Value,
})
@ -66,13 +66,13 @@ func (c *ckv) String() string {
return "consul"
}
func NewData(opts ...options.Option) data.Data {
func NewStore(opts ...options.Option) store.Store {
options := options.NewOptions(opts...)
config := api.DefaultConfig()
var nodes []string
if n, ok := options.Values().Get("data.nodes"); ok {
if n, ok := options.Values().Get("store.nodes"); ok {
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
import (
"sync"
"time"
"github.com/micro/go-micro/data"
"github.com/micro/go-micro/options"
"github.com/micro/go-micro/store"
)
type memoryData struct {
type memoryStore struct {
options.Options
sync.RWMutex
@ -17,15 +17,15 @@ type memoryData struct {
}
type memoryRecord struct {
r *data.Record
r *store.Record
c time.Time
}
func (m *memoryData) Dump() ([]*data.Record, error) {
func (m *memoryStore) Dump() ([]*store.Record, error) {
m.RLock()
defer m.RUnlock()
var values []*data.Record
var values []*store.Record
for _, v := range m.values {
// get expiry
@ -42,13 +42,13 @@ func (m *memoryData) Dump() ([]*data.Record, error) {
return values, nil
}
func (m *memoryData) Read(key string) (*data.Record, error) {
func (m *memoryStore) Read(key string) (*store.Record, error) {
m.RLock()
defer m.RUnlock()
v, ok := m.values[key]
if !ok {
return nil, data.ErrNotFound
return nil, store.ErrNotFound
}
// get expiry
@ -57,13 +57,13 @@ func (m *memoryData) Read(key string) (*data.Record, error) {
// expired
if d > time.Duration(0) && t > d {
return nil, data.ErrNotFound
return nil, store.ErrNotFound
}
return v.r, nil
}
func (m *memoryData) Write(r *data.Record) error {
func (m *memoryStore) Write(r *store.Record) error {
m.Lock()
defer m.Unlock()
@ -76,7 +76,7 @@ func (m *memoryData) Write(r *data.Record) error {
return nil
}
func (m *memoryData) Delete(key string) error {
func (m *memoryStore) Delete(key string) error {
m.Lock()
defer m.Unlock()
@ -86,11 +86,11 @@ func (m *memoryData) Delete(key string) error {
return nil
}
// NewData returns a new data.Data
func NewData(opts ...options.Option) data.Data {
// NewStore returns a new store.Store
func NewStore(opts ...options.Option) store.Store {
options := options.NewOptions(opts...)
return &memoryData{
return &memoryStore{
Options: options,
values: make(map[string]*memoryRecord),
}

View File

@ -1,15 +1,15 @@
package data
package store
import (
"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 {
return options.WithValue("data.nodes", a)
return options.WithValue("store.nodes", a)
}
// Prefix sets a prefix to any key ids used
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 data
// Package store is an interface for distribute data storage.
package store
import (
"errors"
@ -12,8 +12,8 @@ var (
ErrNotFound = errors.New("not found")
)
// Data is a data storage interface
type Data interface {
// Store is a data storage interface
type Store interface {
// embed options
options.Options
// Dump the known records