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 (
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
}
......@@ -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)
}
......
......@@ -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) {
......
......@@ -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)
}
......@@ -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(),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment