2019-10-03 09:46:20 +01:00
|
|
|
// Package etcd is an etcd v3 implementation of kv
|
|
|
|
package etcd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
client "github.com/coreos/etcd/clientv3"
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/store"
|
2019-10-03 09:46:20 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type ekv struct {
|
2019-12-16 14:38:51 +00:00
|
|
|
options store.Options
|
|
|
|
kv client.KV
|
2019-10-03 09:46:20 +01:00
|
|
|
}
|
|
|
|
|
2020-01-08 12:11:31 +00:00
|
|
|
func (e *ekv) Init(opts ...store.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&e.options)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-08 22:23:14 +00:00
|
|
|
func (e *ekv) Read(key string, opts ...store.ReadOption) ([]*store.Record, error) {
|
|
|
|
var options store.ReadOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
2019-10-11 14:08:50 +01:00
|
|
|
|
2020-01-08 22:23:14 +00:00
|
|
|
var etcdOpts []client.OpOption
|
2019-10-11 14:08:50 +01:00
|
|
|
|
2020-01-08 22:23:14 +00:00
|
|
|
// set options prefix
|
|
|
|
if options.Prefix {
|
|
|
|
etcdOpts = append(etcdOpts, client.WithPrefix())
|
|
|
|
}
|
2019-10-11 14:08:50 +01:00
|
|
|
|
2020-01-08 22:23:14 +00:00
|
|
|
keyval, err := e.kv.Get(context.Background(), key, etcdOpts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-10-03 09:46:20 +01:00
|
|
|
}
|
|
|
|
|
2020-01-08 22:23:14 +00:00
|
|
|
if keyval == nil || len(keyval.Kvs) == 0 {
|
|
|
|
return nil, store.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
records := make([]*store.Record, 0, len(keyval.Kvs))
|
2019-10-11 14:08:50 +01:00
|
|
|
|
2020-01-08 22:23:14 +00:00
|
|
|
for _, kv := range keyval.Kvs {
|
2019-10-11 14:08:50 +01:00
|
|
|
records = append(records, &store.Record{
|
|
|
|
Key: string(kv.Key),
|
|
|
|
Value: kv.Value,
|
|
|
|
// TODO: implement expiry
|
|
|
|
})
|
2019-10-03 09:46:20 +01:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:08:50 +01:00
|
|
|
return records, nil
|
2019-10-03 09:46:20 +01:00
|
|
|
}
|
|
|
|
|
2020-01-08 22:23:14 +00:00
|
|
|
func (e *ekv) Delete(key string) error {
|
|
|
|
_, err := e.kv.Delete(context.Background(), key)
|
|
|
|
return err
|
2019-10-03 09:46:20 +01:00
|
|
|
}
|
|
|
|
|
2020-01-08 22:23:14 +00:00
|
|
|
func (e *ekv) Write(record *store.Record) error {
|
|
|
|
// TODO create lease to expire keys
|
|
|
|
_, err := e.kv.Put(context.Background(), record.Key, string(record.Value))
|
|
|
|
return err
|
2019-10-03 09:46:20 +01:00
|
|
|
}
|
|
|
|
|
2019-10-23 22:05:39 +01:00
|
|
|
func (e *ekv) List() ([]*store.Record, error) {
|
2019-10-03 09:46:20 +01:00
|
|
|
keyval, err := e.kv.Get(context.Background(), "/", client.WithPrefix())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if keyval == nil || len(keyval.Kvs) == 0 {
|
2019-12-03 22:59:44 +03:00
|
|
|
return nil, nil
|
2019-10-03 09:46:20 +01:00
|
|
|
}
|
2019-12-03 22:59:44 +03:00
|
|
|
vals := make([]*store.Record, 0, len(keyval.Kvs))
|
2019-10-03 09:46:20 +01:00
|
|
|
for _, keyv := range keyval.Kvs {
|
|
|
|
vals = append(vals, &store.Record{
|
|
|
|
Key: string(keyv.Key),
|
|
|
|
Value: keyv.Value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return vals, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ekv) String() string {
|
|
|
|
return "etcd"
|
|
|
|
}
|
|
|
|
|
2019-12-16 14:38:51 +00:00
|
|
|
func NewStore(opts ...store.Option) store.Store {
|
|
|
|
var options store.Options
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
2019-10-03 09:46:20 +01:00
|
|
|
}
|
|
|
|
|
2019-12-16 14:38:51 +00:00
|
|
|
// get the endpoints
|
|
|
|
endpoints := options.Nodes
|
|
|
|
|
2019-10-03 09:46:20 +01:00
|
|
|
if len(endpoints) == 0 {
|
|
|
|
endpoints = []string{"http://127.0.0.1:2379"}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: parse addresses
|
|
|
|
c, err := client.New(client.Config{
|
|
|
|
Endpoints: endpoints,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ekv{
|
2019-12-16 14:38:51 +00:00
|
|
|
options: options,
|
2019-10-03 09:46:20 +01:00
|
|
|
kv: client.NewKV(c),
|
|
|
|
}
|
|
|
|
}
|