Skip to content
Snippets Groups Projects
Commit 61183f2f authored by Richard T. Carback III's avatar Richard T. Carback III
Browse files

MakeKeyPrefix -> MakeKeyWithPrefix, with associated changes to match original...

MakeKeyPrefix -> MakeKeyWithPrefix, with associated changes to match original intent of the prefix idea
parent 3c97aac2
No related branches found
No related tags found
No related merge requests found
...@@ -15,10 +15,15 @@ import ( ...@@ -15,10 +15,15 @@ import (
const currentContactVersion = 0 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) { func (s *Session) GetContact(name string) (*Contact, error) {
// Make key key := MakeKeyWithPrefix("Contact", name)
// If upgrading version, may need to add logic to update version number in key prefix
key := MakeKeyPrefix("Contact", currentContactVersion) + name
obj, err := s.Get(key) obj, err := s.Get(key)
if err != nil { if err != nil {
...@@ -26,7 +31,9 @@ func (s *Session) GetContact(name string) (*Contact, error) { ...@@ -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 // Correctly implemented upgrade should always change the version number to what's current
if obj.Version != currentContactVersion { 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 // deserialize
...@@ -35,8 +42,9 @@ func (s *Session) GetContact(name string) (*Contact, error) { ...@@ -35,8 +42,9 @@ func (s *Session) GetContact(name string) (*Contact, error) {
return &contact, err return &contact, err
} }
// SetContact saves contact information to disk.
func (s *Session) SetContact(name string, record *Contact) error { func (s *Session) SetContact(name string, record *Contact) error {
key := MakeKeyPrefix("Contact", currentContactVersion) + name key := MakeKeyWithPrefix("Contact", name)
data, err := json.Marshal(record) data, err := json.Marshal(record)
if err != nil { if err != nil {
return err return err
...@@ -48,8 +56,3 @@ func (s *Session) SetContact(name string, record *Contact) error { ...@@ -48,8 +56,3 @@ func (s *Session) SetContact(name string, record *Contact) error {
} }
return s.Set(key, &obj) return s.Set(key, &obj)
} }
type Contact struct {
Id *id.ID
PublicKey []byte
}
...@@ -13,7 +13,8 @@ import ( ...@@ -13,7 +13,8 @@ import (
var currentRegistrationVersion = uint64(0) 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 { func (s *Session) SetRegValidationSig(newVal []byte) error {
// Construct the versioned object // Construct the versioned object
vo := &VersionedObject{ vo := &VersionedObject{
...@@ -23,7 +24,7 @@ func (s *Session) SetRegValidationSig(newVal []byte) error { ...@@ -23,7 +24,7 @@ func (s *Session) SetRegValidationSig(newVal []byte) error {
} }
// Construct the key and place in the key-value store // Construct the key and place in the key-value store
key := MakeKeyPrefix("RegValidationSig", currentRegistrationVersion) key := "RegValidationSig"
return s.kv.Set(key, vo) return s.kv.Set(key, vo)
} }
...@@ -31,7 +32,7 @@ func (s *Session) SetRegValidationSig(newVal []byte) error { ...@@ -31,7 +32,7 @@ func (s *Session) SetRegValidationSig(newVal []byte) error {
// GetRegValidationSig pulls the versioned object by the key and parses // GetRegValidationSig pulls the versioned object by the key and parses
// it into the requested registration signature // it into the requested registration signature
func (s *Session) GetRegValidationSig() ([]byte, error) { func (s *Session) GetRegValidationSig() ([]byte, error) {
key := MakeKeyPrefix("RegValidationSig", currentRegistrationVersion) key := "RegValidationSig"
// Pull the object from the key-value store // Pull the object from the key-value store
voData, err := s.kv.Get(key) voData, err := s.kv.Get(key)
...@@ -40,7 +41,8 @@ func (s *Session) GetRegValidationSig() ([]byte, error) { ...@@ -40,7 +41,8 @@ func (s *Session) GetRegValidationSig() ([]byte, error) {
} }
if voData.Version != currentRegistrationVersion { 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) voData.Version, currentRegistrationVersion)
} }
...@@ -50,7 +52,7 @@ func (s *Session) GetRegValidationSig() ([]byte, error) { ...@@ -50,7 +52,7 @@ func (s *Session) GetRegValidationSig() ([]byte, error) {
// SetRegState uses the SetInterface method to place the regstate into // SetRegState uses the SetInterface method to place the regstate into
// the key-value store // the key-value store
func (s *Session) SetRegState(newVal int64) error { func (s *Session) SetRegState(newVal int64) error {
key := MakeKeyPrefix("RegState", currentRegistrationVersion) key := "RegState"
data, err := json.Marshal(newVal) data, err := json.Marshal(newVal)
if err != nil { if err != nil {
...@@ -70,7 +72,7 @@ func (s *Session) SetRegState(newVal int64) error { ...@@ -70,7 +72,7 @@ func (s *Session) SetRegState(newVal int64) error {
// it into the requested registration signature // it into the requested registration signature
func (s *Session) GetRegState() (int64, error) { func (s *Session) GetRegState() (int64, error) {
// Construct the key from the // Construct the key from the
key := MakeKeyPrefix("RegState", currentRegistrationVersion) key := "RegState"
// Pull the object from the key-value store // Pull the object from the key-value store
voData, err := s.kv.Get(key) voData, err := s.kv.Get(key)
...@@ -79,7 +81,8 @@ func (s *Session) GetRegState() (int64, error) { ...@@ -79,7 +81,8 @@ func (s *Session) GetRegState() (int64, error) {
} }
if voData.Version != currentRegistrationVersion { 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) voData.Version, currentRegistrationVersion)
} }
......
...@@ -52,7 +52,7 @@ type UserData struct { ...@@ -52,7 +52,7 @@ type UserData struct {
const currentUserDataVersion = 0 const currentUserDataVersion = 0
func makeUserDataKey() string { func makeUserDataKey() string {
return MakeKeyPrefix("UserData", currentUserDataVersion) return "UserData"
} }
func (s *Session) GetUserData() (*UserData, error) { func (s *Session) GetUserData() (*UserData, error) {
......
...@@ -10,19 +10,16 @@ import ( ...@@ -10,19 +10,16 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"gitlab.com/elixxir/ekv" "gitlab.com/elixxir/ekv"
"strconv"
"strings" "strings"
"time" "time"
) )
// MakeKeyPrefix provides a helper with a data type and a version const prefixKeySeparator = ":"
// TODO: We might need a separator string here, or a fixed number of
// digits available to the version string // MakeKeyWithPrefix creates a key for a type of data with a unique
// Otherwise version 10 could be mistaken for version 1! Bad news // identifier using a globally defined separator character.
// For now, let's hope a semicolon won't be part of the rest of the key func MakeKeyWithPrefix(dataType string, uniqueID string) string {
// It's not in base64, so maybe it will be fine return fmt.Sprintf("%s%s%s", dataType, prefixKeySeparator, uniqueID)
func MakeKeyPrefix(dataType string, version uint64) string {
return dataType + strconv.FormatUint(version, 10) + ";"
} }
// VersionedObject is used by VersionedKeyValue to keep track of // VersionedObject is used by VersionedKeyValue to keep track of
...@@ -82,14 +79,18 @@ func NewVersionedKV(data ekv.KeyValue) *VersionedKV { ...@@ -82,14 +79,18 @@ func NewVersionedKV(data ekv.KeyValue) *VersionedKV {
// should always make the key prefix before calling Set, and if they // should always make the key prefix before calling Set, and if they
// want the upgraded data persisted they should call Set with the // want the upgraded data persisted they should call Set with the
// upgraded data. // upgraded data.
newKV.upgradeTable[MakeKeyPrefix("test", 0)] = func(key string, newKV.upgradeTable[MakeKeyWithPrefix("test", "")] = func(key string,
oldObject *VersionedObject) (*VersionedObject, error) { oldObject *VersionedObject) (*VersionedObject, error) {
if oldObject.Version == 1 {
return oldObject, nil
}
return &VersionedObject{ return &VersionedObject{
Version: 1, Version: 1,
// Upgrade functions don't need to update the timestamp // Upgrade functions don't need to update
// the timestamp
Timestamp: oldObject.Timestamp, Timestamp: oldObject.Timestamp,
Data: []byte("this object was upgraded from v0" + Data: []byte("this object was upgraded from" +
" to v1"), " v0 to v1"),
}, nil }, nil
} }
newKV.data = data newKV.data = data
...@@ -126,7 +127,7 @@ func (v *VersionedKV) Delete(key string) error { ...@@ -126,7 +127,7 @@ func (v *VersionedKV) Delete(key string) error {
// Set upserts new data into the storage // Set upserts new data into the storage
// When calling this, you are responsible for prefixing the key with the correct // 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 { func (v *VersionedKV) Set(key string, object *VersionedObject) error {
return v.data.Set(key, object) return v.data.Set(key, object)
} }
...@@ -32,7 +32,7 @@ func TestVersionedObject_MarshalUnmarshal(t *testing.T) { ...@@ -32,7 +32,7 @@ func TestVersionedObject_MarshalUnmarshal(t *testing.T) {
} }
if !reflect.DeepEqual(original, unmarshalled) { 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) t.Logf("%+v", unmarshalled)
} }
...@@ -41,13 +41,15 @@ func TestVersionedObject_MarshalUnmarshal(t *testing.T) { ...@@ -41,13 +41,15 @@ func TestVersionedObject_MarshalUnmarshal(t *testing.T) {
func TestVersionedKV_Get_Err(t *testing.T) { func TestVersionedKV_Get_Err(t *testing.T) {
kv := make(ekv.Memstore) kv := make(ekv.Memstore)
vkv := NewVersionedKV(kv) vkv := NewVersionedKV(kv)
key := MakeKeyPrefix("test", 0) + "12345" key := MakeKeyWithPrefix("test", "12345")
result, err := vkv.Get(key) result, err := vkv.Get(key)
if err == nil { 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 { 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) { ...@@ -56,7 +58,7 @@ func TestVersionedKV_Get_Upgrade(t *testing.T) {
// Set up a dummy KV with the required data // Set up a dummy KV with the required data
kv := make(ekv.Memstore) kv := make(ekv.Memstore)
vkv := NewVersionedKV(kv) vkv := NewVersionedKV(kv)
key := MakeKeyPrefix("test", 0) + "12345" key := MakeKeyWithPrefix("test", "12345")
original := VersionedObject{ original := VersionedObject{
Version: 0, Version: 0,
Timestamp: time.Now(), Timestamp: time.Now(),
...@@ -67,10 +69,13 @@ func TestVersionedKV_Get_Upgrade(t *testing.T) { ...@@ -67,10 +69,13 @@ func TestVersionedKV_Get_Upgrade(t *testing.T) {
result, err := vkv.Get(key) result, err := vkv.Get(key)
if err != nil { 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")) { if !bytes.Equal(result.Data,
t.Errorf("upgrade should have overwritten data. result data: %q", 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) { ...@@ -80,7 +85,7 @@ func TestVersionedKV_Get(t *testing.T) {
kv := make(ekv.Memstore) kv := make(ekv.Memstore)
vkv := NewVersionedKV(kv) vkv := NewVersionedKV(kv)
originalVersion := uint64(1) originalVersion := uint64(1)
key := MakeKeyPrefix("test", originalVersion) + "12345" key := MakeKeyWithPrefix("test", "12345")
original := VersionedObject{ original := VersionedObject{
Version: originalVersion, Version: originalVersion,
Timestamp: time.Now(), Timestamp: time.Now(),
...@@ -91,10 +96,12 @@ func TestVersionedKV_Get(t *testing.T) { ...@@ -91,10 +96,12 @@ func TestVersionedKV_Get(t *testing.T) {
result, err := vkv.Get(key) result, err := vkv.Get(key)
if err != nil { 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")) { 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) { ...@@ -103,7 +110,7 @@ func TestVersionedKV_Set(t *testing.T) {
kv := make(ekv.Memstore) kv := make(ekv.Memstore)
vkv := NewVersionedKV(kv) vkv := NewVersionedKV(kv)
originalVersion := uint64(1) originalVersion := uint64(1)
key := MakeKeyPrefix("test", originalVersion) + "12345" key := MakeKeyWithPrefix("test", "12345")
original := VersionedObject{ original := VersionedObject{
Version: originalVersion, Version: originalVersion,
Timestamp: time.Now(), Timestamp: time.Now(),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment