diff --git a/storage/contact.go b/storage/contact.go index 8dc5775b3e1333b63f9c37bc881c731fe6a3fab7..e494c250abb37057807895c9543b0dee0d183d8f 100644 --- a/storage/contact.go +++ b/storage/contact.go @@ -15,10 +15,15 @@ import ( const currentContactVersion = 0 +// Contact holds the public key and ID of a given contact. +type Contact struct { + Id *id.ID + PublicKey []byte +} + +// GetContact reads contact information from disk func (s *Session) GetContact(name string) (*Contact, error) { - // Make key - // If upgrading version, may need to add logic to update version number in key prefix - key := MakeKeyPrefix("Contact", currentContactVersion) + name + key := MakeKeyWithPrefix("Contact", name) obj, err := s.Get(key) if err != nil { @@ -26,7 +31,9 @@ func (s *Session) GetContact(name string) (*Contact, error) { } // Correctly implemented upgrade should always change the version number to what's current if obj.Version != currentContactVersion { - globals.Log.WARN.Printf("Session.GetContact: got unexpected version %v, expected version %v", obj.Version, currentContactVersion) + globals.Log.WARN.Printf("Session.GetContact: got unexpected "+ + "version %v, expected version %v", obj.Version, + currentContactVersion) } // deserialize @@ -35,8 +42,9 @@ func (s *Session) GetContact(name string) (*Contact, error) { return &contact, err } +// SetContact saves contact information to disk. func (s *Session) SetContact(name string, record *Contact) error { - key := MakeKeyPrefix("Contact", currentContactVersion) + name + key := MakeKeyWithPrefix("Contact", name) data, err := json.Marshal(record) if err != nil { return err @@ -48,8 +56,3 @@ func (s *Session) SetContact(name string, record *Contact) error { } return s.Set(key, &obj) } - -type Contact struct { - Id *id.ID - PublicKey []byte -} diff --git a/storage/registration.go b/storage/registration.go index 6168b490ee89c1639f14bc275cb8b9ef5359100e..3b624867ab47b1968a1c07d8bd7362300afa6b9c 100644 --- a/storage/registration.go +++ b/storage/registration.go @@ -13,7 +13,8 @@ import ( var currentRegistrationVersion = uint64(0) -// SetRegValidationSig builds the versioned object and sets it in the key-value store +// SetRegValidationSig builds the versioned object and sets it in the +// key-value store func (s *Session) SetRegValidationSig(newVal []byte) error { // Construct the versioned object vo := &VersionedObject{ @@ -23,7 +24,7 @@ func (s *Session) SetRegValidationSig(newVal []byte) error { } // Construct the key and place in the key-value store - key := MakeKeyPrefix("RegValidationSig", currentRegistrationVersion) + key := "RegValidationSig" return s.kv.Set(key, vo) } @@ -31,7 +32,7 @@ func (s *Session) SetRegValidationSig(newVal []byte) error { // GetRegValidationSig pulls the versioned object by the key and parses // it into the requested registration signature func (s *Session) GetRegValidationSig() ([]byte, error) { - key := MakeKeyPrefix("RegValidationSig", currentRegistrationVersion) + key := "RegValidationSig" // Pull the object from the key-value store voData, err := s.kv.Get(key) @@ -40,7 +41,8 @@ func (s *Session) GetRegValidationSig() ([]byte, error) { } if voData.Version != currentRegistrationVersion { - globals.Log.WARN.Printf("Session.GetRegValidationSig: got unexpected version %v, expected version %v", + globals.Log.WARN.Printf("Session.GetRegValidationSig: got "+ + "unexpected version %v, expected version %v", voData.Version, currentRegistrationVersion) } @@ -50,7 +52,7 @@ func (s *Session) GetRegValidationSig() ([]byte, error) { // SetRegState uses the SetInterface method to place the regstate into // the key-value store func (s *Session) SetRegState(newVal int64) error { - key := MakeKeyPrefix("RegState", currentRegistrationVersion) + key := "RegState" data, err := json.Marshal(newVal) if err != nil { @@ -70,7 +72,7 @@ func (s *Session) SetRegState(newVal int64) error { // it into the requested registration signature func (s *Session) GetRegState() (int64, error) { // Construct the key from the - key := MakeKeyPrefix("RegState", currentRegistrationVersion) + key := "RegState" // Pull the object from the key-value store voData, err := s.kv.Get(key) @@ -79,7 +81,8 @@ func (s *Session) GetRegState() (int64, error) { } if voData.Version != currentRegistrationVersion { - globals.Log.WARN.Printf("Session.GetRegState: got unexpected version %v, expected version %v", + globals.Log.WARN.Printf("Session.GetRegState: got unexpected "+ + "version %v, expected version %v", voData.Version, currentRegistrationVersion) } diff --git a/storage/userdata.go b/storage/userdata.go index 551f25ce3f61d80683df24d46a54f6eb06b34fc3..266c0d4f37fed0aa348febb19ffff3c83ef9f0b8 100644 --- a/storage/userdata.go +++ b/storage/userdata.go @@ -52,7 +52,7 @@ type UserData struct { const currentUserDataVersion = 0 func makeUserDataKey() string { - return MakeKeyPrefix("UserData", currentUserDataVersion) + return "UserData" } func (s *Session) GetUserData() (*UserData, error) { diff --git a/storage/versionedkv.go b/storage/versionedkv.go index 17567b7dc99bbd4d2082057fa04ce64fb4d47f08..e8781bc695c48ea4519f5c81b206db53e8396271 100644 --- a/storage/versionedkv.go +++ b/storage/versionedkv.go @@ -10,19 +10,16 @@ import ( "encoding/json" "fmt" "gitlab.com/elixxir/ekv" - "strconv" "strings" "time" ) -// MakeKeyPrefix provides a helper with a data type and a version -// TODO: We might need a separator string here, or a fixed number of -// digits available to the version string -// Otherwise version 10 could be mistaken for version 1! Bad news -// For now, let's hope a semicolon won't be part of the rest of the key -// It's not in base64, so maybe it will be fine -func MakeKeyPrefix(dataType string, version uint64) string { - return dataType + strconv.FormatUint(version, 10) + ";" +const prefixKeySeparator = ":" + +// MakeKeyWithPrefix creates a key for a type of data with a unique +// identifier using a globally defined separator character. +func MakeKeyWithPrefix(dataType string, uniqueID string) string { + return fmt.Sprintf("%s%s%s", dataType, prefixKeySeparator, uniqueID) } // VersionedObject is used by VersionedKeyValue to keep track of @@ -82,14 +79,18 @@ func NewVersionedKV(data ekv.KeyValue) *VersionedKV { // should always make the key prefix before calling Set, and if they // want the upgraded data persisted they should call Set with the // upgraded data. - newKV.upgradeTable[MakeKeyPrefix("test", 0)] = func(key string, + newKV.upgradeTable[MakeKeyWithPrefix("test", "")] = func(key string, oldObject *VersionedObject) (*VersionedObject, error) { + if oldObject.Version == 1 { + return oldObject, nil + } return &VersionedObject{ Version: 1, - // Upgrade functions don't need to update the timestamp + // Upgrade functions don't need to update + // the timestamp Timestamp: oldObject.Timestamp, - Data: []byte("this object was upgraded from v0" + - " to v1"), + Data: []byte("this object was upgraded from" + + " v0 to v1"), }, nil } newKV.data = data @@ -126,7 +127,7 @@ func (v *VersionedKV) Delete(key string) error { // Set upserts new data into the storage // When calling this, you are responsible for prefixing the key with the correct -// type and version! Call MakeKeyPrefix() to do so. +// type optionally unique id! Call MakeKeyWithPrefix() to do so. func (v *VersionedKV) Set(key string, object *VersionedObject) error { return v.data.Set(key, object) } diff --git a/storage/versionedkv_test.go b/storage/versionedkv_test.go index deceaacb2d96899d16a4ef4e73a412f0c7cc620f..e58e8b73156b45187e762c3b92f88a1f97b51936 100644 --- a/storage/versionedkv_test.go +++ b/storage/versionedkv_test.go @@ -32,7 +32,7 @@ func TestVersionedObject_MarshalUnmarshal(t *testing.T) { } if !reflect.DeepEqual(original, unmarshalled) { - t.Error("Original and serialized/deserialized objects not equal") + t.Error("Original and deserialized objects not equal") } t.Logf("%+v", unmarshalled) } @@ -41,13 +41,15 @@ func TestVersionedObject_MarshalUnmarshal(t *testing.T) { func TestVersionedKV_Get_Err(t *testing.T) { kv := make(ekv.Memstore) vkv := NewVersionedKV(kv) - key := MakeKeyPrefix("test", 0) + "12345" + key := MakeKeyWithPrefix("test", "12345") result, err := vkv.Get(key) if err == nil { - t.Error("Getting a key that didn't exist should have returned an error") + t.Error("Getting a key that didn't exist should have" + + " returned an error") } if result != nil { - t.Error("Getting a key that didn't exist shouldn't have returned data") + t.Error("Getting a key that didn't exist shouldn't " + + "have returned data") } } @@ -56,7 +58,7 @@ func TestVersionedKV_Get_Upgrade(t *testing.T) { // Set up a dummy KV with the required data kv := make(ekv.Memstore) vkv := NewVersionedKV(kv) - key := MakeKeyPrefix("test", 0) + "12345" + key := MakeKeyWithPrefix("test", "12345") original := VersionedObject{ Version: 0, Timestamp: time.Now(), @@ -67,10 +69,13 @@ func TestVersionedKV_Get_Upgrade(t *testing.T) { result, err := vkv.Get(key) if err != nil { - t.Fatalf("Error getting something that should have been in: %v", err) + t.Fatalf("Error getting something that should have been in: %v", + err) } - if !bytes.Equal(result.Data, []byte("this object was upgraded from v0 to v1")) { - t.Errorf("upgrade should have overwritten data. result data: %q", result.Data) + if !bytes.Equal(result.Data, + []byte("this object was upgraded from v0 to v1")) { + t.Errorf("upgrade should have overwritten data."+ + " result data: %q", result.Data) } } @@ -80,7 +85,7 @@ func TestVersionedKV_Get(t *testing.T) { kv := make(ekv.Memstore) vkv := NewVersionedKV(kv) originalVersion := uint64(1) - key := MakeKeyPrefix("test", originalVersion) + "12345" + key := MakeKeyWithPrefix("test", "12345") original := VersionedObject{ Version: originalVersion, Timestamp: time.Now(), @@ -91,10 +96,12 @@ func TestVersionedKV_Get(t *testing.T) { result, err := vkv.Get(key) if err != nil { - t.Fatalf("Error getting something that should have been in: %v", err) + t.Fatalf("Error getting something that should have been in: %v", + err) } if !bytes.Equal(result.Data, []byte("not upgraded")) { - t.Errorf("upgrade should not have overwritten data. result data: %q", result.Data) + t.Errorf("upgrade should not have overwritten data."+ + " result data: %q", result.Data) } } @@ -103,7 +110,7 @@ func TestVersionedKV_Set(t *testing.T) { kv := make(ekv.Memstore) vkv := NewVersionedKV(kv) originalVersion := uint64(1) - key := MakeKeyPrefix("test", originalVersion) + "12345" + key := MakeKeyWithPrefix("test", "12345") original := VersionedObject{ Version: originalVersion, Timestamp: time.Now(),