2019-10-03 11:46:20 +03:00
|
|
|
// Package etcd is an etcd v3 implementation of kv
|
|
|
|
package etcd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
client "github.com/coreos/etcd/clientv3"
|
2019-10-11 16:44:34 +03:00
|
|
|
"github.com/coreos/etcd/mvcc/mvccpb"
|
2019-10-03 11:46:20 +03:00
|
|
|
"github.com/micro/go-micro/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ekv struct {
|
2019-12-16 17:38:51 +03:00
|
|
|
options store.Options
|
|
|
|
kv client.KV
|
2019-10-03 11:46:20 +03:00
|
|
|
}
|
|
|
|
|
2019-10-11 16:08:50 +03:00
|
|
|
func (e *ekv) Read(keys ...string) ([]*store.Record, error) {
|
2019-12-03 22:59:44 +03:00
|
|
|
//nolint:prealloc
|
2019-10-11 16:08:50 +03:00
|
|
|
var values []*mvccpb.KeyValue
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
keyval, err := e.kv.Get(context.Background(), key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if keyval == nil || len(keyval.Kvs) == 0 {
|
|
|
|
return nil, store.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
values = append(values, keyval.Kvs...)
|
2019-10-03 11:46:20 +03:00
|
|
|
}
|
|
|
|
|
2019-11-04 13:33:53 +03:00
|
|
|
records := make([]*store.Record, 0, len(values))
|
2019-10-11 16:08:50 +03:00
|
|
|
|
|
|
|
for _, kv := range values {
|
|
|
|
records = append(records, &store.Record{
|
|
|
|
Key: string(kv.Key),
|
|
|
|
Value: kv.Value,
|
|
|
|
// TODO: implement expiry
|
|
|
|
})
|
2019-10-03 11:46:20 +03:00
|
|
|
}
|
|
|
|
|
2019-10-11 16:08:50 +03:00
|
|
|
return records, nil
|
2019-10-03 11:46:20 +03:00
|
|
|
}
|
|
|
|
|
2019-10-11 16:08:50 +03:00
|
|
|
func (e *ekv) Delete(keys ...string) error {
|
|
|
|
var gerr error
|
|
|
|
for _, key := range keys {
|
|
|
|
_, err := e.kv.Delete(context.Background(), key)
|
|
|
|
if err != nil {
|
|
|
|
gerr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return gerr
|
2019-10-03 11:46:20 +03:00
|
|
|
}
|
|
|
|
|
2019-10-11 16:08:50 +03:00
|
|
|
func (e *ekv) Write(records ...*store.Record) error {
|
|
|
|
var gerr error
|
|
|
|
for _, record := range records {
|
|
|
|
// TODO create lease to expire keys
|
|
|
|
_, err := e.kv.Put(context.Background(), record.Key, string(record.Value))
|
|
|
|
if err != nil {
|
|
|
|
gerr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return gerr
|
2019-10-03 11:46:20 +03:00
|
|
|
}
|
|
|
|
|
2019-10-24 00:05:39 +03:00
|
|
|
func (e *ekv) List() ([]*store.Record, error) {
|
2019-10-03 11:46:20 +03: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 11:46:20 +03:00
|
|
|
}
|
2019-12-03 22:59:44 +03:00
|
|
|
vals := make([]*store.Record, 0, len(keyval.Kvs))
|
2019-10-03 11:46:20 +03: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 17:38:51 +03:00
|
|
|
func NewStore(opts ...store.Option) store.Store {
|
|
|
|
var options store.Options
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
2019-10-03 11:46:20 +03:00
|
|
|
}
|
|
|
|
|
2019-12-16 17:38:51 +03:00
|
|
|
// get the endpoints
|
|
|
|
endpoints := options.Nodes
|
|
|
|
|
2019-10-03 11:46:20 +03: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 17:38:51 +03:00
|
|
|
options: options,
|
2019-10-03 11:46:20 +03:00
|
|
|
kv: client.NewKV(c),
|
|
|
|
}
|
|
|
|
}
|