From a5412dd4a04823fb3179f9959b421b09f78591ad Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Wed, 12 Jun 2019 07:46:20 +0100 Subject: [PATCH] Move data to store --- {data => store}/consul/consul.go | 22 +++++++++++----------- {data => store}/memory/memory.go | 28 ++++++++++++++-------------- {data => store}/options.go | 8 ++++---- data/data.go => store/store.go | 8 ++++---- 4 files changed, 33 insertions(+), 33 deletions(-) rename {data => store}/consul/consul.go (75%) rename {data => store}/memory/memory.go (63%) rename {data => store}/options.go (55%) rename data/data.go => store/store.go (77%) diff --git a/data/consul/consul.go b/store/consul/consul.go similarity index 75% rename from data/consul/consul.go rename to store/consul/consul.go index 58c62495..9fa1634f 100644 --- a/data/consul/consul.go +++ b/store/consul/consul.go @@ -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) } diff --git a/data/memory/memory.go b/store/memory/memory.go similarity index 63% rename from data/memory/memory.go rename to store/memory/memory.go index 12a529af..4c1d6c8a 100644 --- a/data/memory/memory.go +++ b/store/memory/memory.go @@ -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), } diff --git a/data/options.go b/store/options.go similarity index 55% rename from data/options.go rename to store/options.go index 510e0fe3..2f63e582 100644 --- a/data/options.go +++ b/store/options.go @@ -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) } diff --git a/data/data.go b/store/store.go similarity index 77% rename from data/data.go rename to store/store.go index a7dbf3c0..e8c17eae 100644 --- a/data/data.go +++ b/store/store.go @@ -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