|
|
@@ -32,11 +32,11 @@ type RemoteKV struct {
|
|
|
val string
|
|
|
}
|
|
|
|
|
|
-func NewKey(kv v3.KV, key string, leaseID v3.LeaseID) (*RemoteKV, error) {
|
|
|
- return NewKV(kv, key, "", leaseID)
|
|
|
+func newKey(kv v3.KV, key string, leaseID v3.LeaseID) (*RemoteKV, error) {
|
|
|
+ return newKV(kv, key, "", leaseID)
|
|
|
}
|
|
|
|
|
|
-func NewKV(kv v3.KV, key, val string, leaseID v3.LeaseID) (*RemoteKV, error) {
|
|
|
+func newKV(kv v3.KV, key, val string, leaseID v3.LeaseID) (*RemoteKV, error) {
|
|
|
rev, err := putNewKV(kv, key, val, leaseID)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
@@ -44,25 +44,7 @@ func NewKV(kv v3.KV, key, val string, leaseID v3.LeaseID) (*RemoteKV, error) {
|
|
|
return &RemoteKV{kv, key, rev, val}, nil
|
|
|
}
|
|
|
|
|
|
-func GetRemoteKV(kv v3.KV, key string) (*RemoteKV, error) {
|
|
|
- resp, err := kv.Get(context.TODO(), key)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
- rev := resp.Header.Revision
|
|
|
- val := ""
|
|
|
- if len(resp.Kvs) > 0 {
|
|
|
- rev = resp.Kvs[0].ModRevision
|
|
|
- val = string(resp.Kvs[0].Value)
|
|
|
- }
|
|
|
- return &RemoteKV{kv: kv, key: key, rev: rev, val: val}, nil
|
|
|
-}
|
|
|
-
|
|
|
-func NewUniqueKey(kv v3.KV, prefix string) (*RemoteKV, error) {
|
|
|
- return NewUniqueKV(kv, prefix, "", 0)
|
|
|
-}
|
|
|
-
|
|
|
-func NewUniqueKV(kv v3.KV, prefix string, val string, leaseID v3.LeaseID) (*RemoteKV, error) {
|
|
|
+func newUniqueKV(kv v3.KV, prefix string, val string) (*RemoteKV, error) {
|
|
|
for {
|
|
|
newKey := fmt.Sprintf("%s/%v", prefix, time.Now().UnixNano())
|
|
|
rev, err := putNewKV(kv, newKey, val, 0)
|
|
|
@@ -90,14 +72,9 @@ func putNewKV(kv v3.KV, key, val string, leaseID v3.LeaseID) (int64, error) {
|
|
|
return txnresp.Header.Revision, nil
|
|
|
}
|
|
|
|
|
|
-// NewSequentialKV allocates a new sequential key-value pair at <prefix>/nnnnn
|
|
|
-func NewSequentialKV(kv v3.KV, prefix, val string) (*RemoteKV, error) {
|
|
|
- return newSequentialKV(kv, prefix, val, 0)
|
|
|
-}
|
|
|
-
|
|
|
// newSequentialKV allocates a new sequential key <prefix>/nnnnn with a given
|
|
|
// value and lease. Note: a bookkeeping node __<prefix> is also allocated.
|
|
|
-func newSequentialKV(kv v3.KV, prefix, val string, leaseID v3.LeaseID) (*RemoteKV, error) {
|
|
|
+func newSequentialKV(kv v3.KV, prefix, val string) (*RemoteKV, error) {
|
|
|
resp, err := kv.Get(context.TODO(), prefix, v3.WithLastKey()...)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
@@ -118,22 +95,22 @@ func newSequentialKV(kv v3.KV, prefix, val string, leaseID v3.LeaseID) (*RemoteK
|
|
|
// base prefix key must be current (i.e., <=) with the server update;
|
|
|
// the base key is important to avoid the following:
|
|
|
// N1: LastKey() == 1, start txn.
|
|
|
- // N2: New Key 2, New Key 3, Delete Key 2
|
|
|
+ // N2: new Key 2, new Key 3, Delete Key 2
|
|
|
// N1: txn succeeds allocating key 2 when it shouldn't
|
|
|
baseKey := "__" + prefix
|
|
|
|
|
|
// current revision might contain modification so +1
|
|
|
cmp := v3.Compare(v3.ModRevision(baseKey), "<", resp.Header.Revision+1)
|
|
|
- reqPrefix := v3.OpPut(baseKey, "", v3.WithLease(leaseID))
|
|
|
- reqNewKey := v3.OpPut(newKey, val, v3.WithLease(leaseID))
|
|
|
+ reqPrefix := v3.OpPut(baseKey, "")
|
|
|
+ reqnewKey := v3.OpPut(newKey, val)
|
|
|
|
|
|
txn := kv.Txn(context.TODO())
|
|
|
- txnresp, err := txn.If(cmp).Then(reqPrefix, reqNewKey).Commit()
|
|
|
+ txnresp, err := txn.If(cmp).Then(reqPrefix, reqnewKey).Commit()
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
if !txnresp.Succeeded {
|
|
|
- return newSequentialKV(kv, prefix, val, leaseID)
|
|
|
+ return newSequentialKV(kv, prefix, val)
|
|
|
}
|
|
|
return &RemoteKV{kv, newKey, txnresp.Header.Revision, val}, nil
|
|
|
}
|
|
|
@@ -159,25 +136,25 @@ func (rk *RemoteKV) Put(val string) error {
|
|
|
// EphemeralKV is a new key associated with a session lease
|
|
|
type EphemeralKV struct{ RemoteKV }
|
|
|
|
|
|
-// NewEphemeralKV creates a new key/value pair associated with a session lease
|
|
|
-func NewEphemeralKV(s *concurrency.Session, key, val string) (*EphemeralKV, error) {
|
|
|
- k, err := NewKV(s.Client(), key, val, s.Lease())
|
|
|
+// newEphemeralKV creates a new key/value pair associated with a session lease
|
|
|
+func newEphemeralKV(s *concurrency.Session, key, val string) (*EphemeralKV, error) {
|
|
|
+ k, err := newKV(s.Client(), key, val, s.Lease())
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
return &EphemeralKV{*k}, nil
|
|
|
}
|
|
|
|
|
|
-// NewUniqueEphemeralKey creates a new unique valueless key associated with a session lease
|
|
|
-func NewUniqueEphemeralKey(s *concurrency.Session, prefix string) (*EphemeralKV, error) {
|
|
|
- return NewUniqueEphemeralKV(s, prefix, "")
|
|
|
+// newUniqueEphemeralKey creates a new unique valueless key associated with a session lease
|
|
|
+func newUniqueEphemeralKey(s *concurrency.Session, prefix string) (*EphemeralKV, error) {
|
|
|
+ return newUniqueEphemeralKV(s, prefix, "")
|
|
|
}
|
|
|
|
|
|
-// NewUniqueEphemeralKV creates a new unique key/value pair associated with a session lease
|
|
|
-func NewUniqueEphemeralKV(s *concurrency.Session, prefix, val string) (ek *EphemeralKV, err error) {
|
|
|
+// newUniqueEphemeralKV creates a new unique key/value pair associated with a session lease
|
|
|
+func newUniqueEphemeralKV(s *concurrency.Session, prefix, val string) (ek *EphemeralKV, err error) {
|
|
|
for {
|
|
|
newKey := fmt.Sprintf("%s/%v", prefix, time.Now().UnixNano())
|
|
|
- ek, err = NewEphemeralKV(s, newKey, val)
|
|
|
+ ek, err = newEphemeralKV(s, newKey, val)
|
|
|
if err == nil || err != ErrKeyExists {
|
|
|
break
|
|
|
}
|