Skip to content
Snippets Groups Projects
Commit 6e267993 authored by Benjamin Wenger's avatar Benjamin Wenger
Browse files

made the key object the master of its storage

parent da879941
No related branches found
No related tags found
No related merge requests found
......@@ -10,7 +10,20 @@ import (
const currentKeyVersion = 0
type key struct {
kv *versioned.KV
k *cyclic.Int
storeKey string
}
func NewKey(kv *versioned.KV, k *cyclic.Int, id *id.ID) (*key, error) {
newKey := &key{
kv: kv,
k: k,
storeKey: keyKey(id),
}
return newKey, newKey.save()
}
// loads the key for the given node id from the versioned keystore
......@@ -34,7 +47,7 @@ func loadKey(kv *versioned.KV, id *id.ID) (*key, error) {
}
// saves the key as the key for the given node ID in the passed keystore
func (k *key) save(kv *versioned.KV, id *id.ID) error {
func (k *key) save() error {
now := time.Now()
data, err := k.marshal()
......@@ -48,9 +61,7 @@ func (k *key) save(kv *versioned.KV, id *id.ID) error {
Data: data,
}
key := keyKey(id)
return kv.Set(key, &obj)
return k.kv.Set(k.storeKey, &obj)
}
// deletes the key from the versioned keystore
......
......@@ -77,8 +77,7 @@ func (s *Store) Add(nid *id.ID, k *cyclic.Int) error {
s.mux.Lock()
defer s.mux.Unlock()
nodekey := &key{k: k}
err := nodekey.save(s.kv, nid)
nodekey, err := NewKey(s.kv, k, nid)
if err != nil {
return err
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment