Use concrete Item rather than an interface. Removes unnecessary dupe structs

This commit is contained in:
Asim 2015-05-23 21:25:55 +01:00
parent 74fd1fc989
commit b5dcbbe998
7 changed files with 34 additions and 121 deletions

View File

@ -1,14 +0,0 @@
package store
type consulItem struct {
key string
value []byte
}
func (c *consulItem) Key() string {
return c.key
}
func (c *consulItem) Value() []byte {
return c.value
}

View File

@ -10,7 +10,7 @@ type consulStore struct {
Client *consul.Client
}
func (c *consulStore) Get(key string) (Item, error) {
func (c *consulStore) Get(key string) (*Item, error) {
kv, _, err := c.Client.KV().Get(key, nil)
if err != nil {
return nil, err
@ -19,9 +19,9 @@ func (c *consulStore) Get(key string) (Item, error) {
return nil, errors.New("key not found")
}
return &consulItem{
key: kv.Key,
value: kv.Value,
return &Item{
Key: kv.Key,
Value: kv.Value,
}, nil
}
@ -30,22 +30,15 @@ func (c *consulStore) Del(key string) error {
return err
}
func (c *consulStore) Put(item Item) error {
func (c *consulStore) Put(item *Item) error {
_, err := c.Client.KV().Put(&consul.KVPair{
Key: item.Key(),
Value: item.Value(),
Key: item.Key,
Value: item.Value,
}, nil)
return err
}
func (c *consulStore) NewItem(key string, value []byte) Item {
return &consulItem{
key: key,
value: value,
}
}
func newConsulStore(addrs []string, opt ...Option) Store {
config := consul.DefaultConfig()
if len(addrs) > 0 {

View File

@ -11,20 +11,7 @@ type estore struct {
Client *etcd.Client
}
type item struct {
key string
value []byte
}
func (i *item) Key() string {
return i.key
}
func (i *item) Value() []byte {
return i.value
}
func (e *estore) Get(key string) (store.Item, error) {
func (e *estore) Get(key string) (*store.Item, error) {
kv, err := e.Client.Get(key, false, false)
if err != nil {
return nil, err
@ -33,9 +20,9 @@ func (e *estore) Get(key string) (store.Item, error) {
return nil, errors.New("key not found")
}
return &item{
key: kv.Node.Key,
value: []byte(kv.Node.Value),
return &store.Item{
Key: kv.Node.Key,
Value: []byte(kv.Node.Value),
}, nil
}
@ -44,19 +31,12 @@ func (e *estore) Del(key string) error {
return err
}
func (e *estore) Put(item store.Item) error {
_, err := e.Client.Set(item.Key(), string(item.Value()), 0)
func (e *estore) Put(item *store.Item) error {
_, err := e.Client.Set(item.Key, string(item.Value), 0)
return err
}
func (e *estore) NewItem(key string, value []byte) store.Item {
return &item{
key: key,
value: value,
}
}
func NewStore(addrs []string, opts ...store.Option) store.Store {
if len(addrs) == 0 {
addrs = []string{"127.0.0.1:2379"}

View File

@ -1,6 +0,0 @@
package store
type Item interface {
Key() string
Value() []byte
}

View File

@ -11,20 +11,7 @@ type mstore struct {
Client *mc.Client
}
type item struct {
key string
value []byte
}
func (i *item) Key() string {
return i.key
}
func (i *item) Value() []byte {
return i.value
}
func (m *mstore) Get(key string) (store.Item, error) {
func (m *mstore) Get(key string) (*store.Item, error) {
kv, err := m.Client.Get(key)
if err != nil && err == mc.ErrCacheMiss {
return nil, errors.New("key not found")
@ -36,9 +23,9 @@ func (m *mstore) Get(key string) (store.Item, error) {
return nil, errors.New("key not found")
}
return &item{
key: kv.Key,
value: kv.Value,
return &store.Item{
Key: kv.Key,
Value: kv.Value,
}, nil
}
@ -46,20 +33,13 @@ func (m *mstore) Del(key string) error {
return m.Client.Delete(key)
}
func (m *mstore) Put(item store.Item) error {
func (m *mstore) Put(item *store.Item) error {
return m.Client.Set(&mc.Item{
Key: item.Key(),
Value: item.Value(),
Key: item.Key,
Value: item.Value,
})
}
func (m *mstore) NewItem(key string, value []byte) store.Item {
return &item{
key: key,
value: value,
}
}
func NewStore(addrs []string, opts ...store.Option) store.Store {
if len(addrs) == 0 {
addrs = []string{"127.0.0.1:11211"}

View File

@ -9,23 +9,10 @@ import (
type mstore struct {
sync.RWMutex
store map[string]store.Item
store map[string]*store.Item
}
type item struct {
key string
value []byte
}
func (i *item) Key() string {
return i.key
}
func (i *item) Value() []byte {
return i.value
}
func (m *mstore) Get(key string) (store.Item, error) {
func (m *mstore) Get(key string) (*store.Item, error) {
m.RLock()
v, ok := m.store[key]
m.RUnlock()
@ -42,22 +29,15 @@ func (m *mstore) Del(key string) error {
return nil
}
func (m *mstore) Put(item store.Item) error {
func (m *mstore) Put(item *store.Item) error {
m.Lock()
m.store[item.Key()] = item
m.store[item.Key] = item
m.Unlock()
return nil
}
func (m *mstore) NewItem(key string, value []byte) store.Item {
return &item{
key: key,
value: value,
}
}
func NewStore(addrs []string, opt ...store.Option) store.Store {
return &mstore{
store: make(map[string]store.Item),
store: make(map[string]*store.Item),
}
}

View File

@ -1,10 +1,14 @@
package store
type Store interface {
Get(string) (Item, error)
Get(string) (*Item, error)
Del(string) error
Put(Item) error
NewItem(string, []byte) Item
Put(*Item) error
}
type Item struct {
Key string
Value []byte
}
type options struct{}
@ -19,7 +23,7 @@ func NewStore(addrs []string, opt ...Option) Store {
return newConsulStore(addrs, opt...)
}
func Get(key string) (Item, error) {
func Get(key string) (*Item, error) {
return DefaultStore.Get(key)
}
@ -27,10 +31,6 @@ func Del(key string) error {
return DefaultStore.Del(key)
}
func Put(item Item) error {
func Put(item *Item) error {
return DefaultStore.Put(item)
}
func NewItem(key string, value []byte) Item {
return DefaultStore.NewItem(key, value)
}