-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
42 lines (35 loc) · 780 Bytes
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"context"
"fmt"
"github.com/coreos/etcd/client"
)
type Store interface {
Put(v string) uint64
Get(k uint64) (string, bool)
}
type EtcdStore struct {
dir string
api client.KeysAPI
}
func NewEtcdStore(d string, c client.Client) *EtcdStore {
return &EtcdStore{d, client.NewKeysAPI(c)}
}
func (store *EtcdStore) Get(k uint64) (string, bool) {
p := fmt.Sprintf("%s/%020d", store.dir, k)
resp, err := store.api.Get(context.Background(), p, nil)
if err != nil {
if client.IsKeyNotFound(err) {
return "", false
}
panic(err)
}
return resp.Node.Value, true
}
func (store *EtcdStore) Put(v string) uint64 {
resp, err := store.api.CreateInOrder(context.Background(), store.dir, v, nil)
if err != nil {
panic(err)
}
return resp.Index
}