From 6e267993dee7209128eb4cc6e288cbac14f57595 Mon Sep 17 00:00:00 2001
From: Benjamin Wenger <ben@elixxir.ioo>
Date: Thu, 27 Aug 2020 13:53:03 -0700
Subject: [PATCH] made the key object the master of its storage

---
 storage/cmix/key.go   | 21 ++++++++++++++++-----
 storage/cmix/store.go |  3 +--
 2 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/storage/cmix/key.go b/storage/cmix/key.go
index 389482e8b..b0f9bcf2d 100644
--- a/storage/cmix/key.go
+++ b/storage/cmix/key.go
@@ -10,7 +10,20 @@ import (
 const currentKeyVersion = 0
 
 type key struct {
-	k *cyclic.Int
+	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
diff --git a/storage/cmix/store.go b/storage/cmix/store.go
index 1fdb4aff3..043f0dbdd 100644
--- a/storage/cmix/store.go
+++ b/storage/cmix/store.go
@@ -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
 	}
-- 
GitLab