WIP: Add metadata to store record (#1604)
* Add metadata to store record * Add metadata to cockroach store * add metadata to store service implementation * fix breaking cache test * Test/fix cockroach metadata usage * fix store memory metadata bug
This commit is contained in:
@@ -33,9 +33,10 @@ type memoryStore struct {
|
||||
store *cache.Cache
|
||||
}
|
||||
|
||||
type internalRecord struct {
|
||||
type storeRecord struct {
|
||||
key string
|
||||
value []byte
|
||||
metadata map[string]interface{}
|
||||
expiresAt time.Time
|
||||
}
|
||||
|
||||
@@ -56,26 +57,36 @@ func (m *memoryStore) prefix(database, table string) string {
|
||||
func (m *memoryStore) get(prefix, key string) (*store.Record, error) {
|
||||
key = m.key(prefix, key)
|
||||
|
||||
var storedRecord *internalRecord
|
||||
var storedRecord *storeRecord
|
||||
r, found := m.store.Get(key)
|
||||
if !found {
|
||||
return nil, store.ErrNotFound
|
||||
}
|
||||
|
||||
storedRecord, ok := r.(*internalRecord)
|
||||
storedRecord, ok := r.(*storeRecord)
|
||||
if !ok {
|
||||
return nil, errors.New("Retrieved a non *internalRecord from the cache")
|
||||
return nil, errors.New("Retrieved a non *storeRecord from the cache")
|
||||
}
|
||||
|
||||
// Copy the record on the way out
|
||||
newRecord := &store.Record{}
|
||||
newRecord.Key = strings.TrimPrefix(storedRecord.key, prefix+"/")
|
||||
newRecord.Value = make([]byte, len(storedRecord.value))
|
||||
newRecord.Metadata = make(map[string]interface{})
|
||||
|
||||
// copy the value into the new record
|
||||
copy(newRecord.Value, storedRecord.value)
|
||||
|
||||
// check if we need to set the expiry
|
||||
if !storedRecord.expiresAt.IsZero() {
|
||||
newRecord.Expiry = time.Until(storedRecord.expiresAt)
|
||||
}
|
||||
|
||||
// copy in the metadata
|
||||
for k, v := range storedRecord.metadata {
|
||||
newRecord.Metadata[k] = v
|
||||
}
|
||||
|
||||
return newRecord, nil
|
||||
}
|
||||
|
||||
@@ -84,15 +95,24 @@ func (m *memoryStore) set(prefix string, r *store.Record) {
|
||||
|
||||
// copy the incoming record and then
|
||||
// convert the expiry in to a hard timestamp
|
||||
i := &internalRecord{}
|
||||
i := &storeRecord{}
|
||||
i.key = r.Key
|
||||
i.value = make([]byte, len(r.Value))
|
||||
i.metadata = make(map[string]interface{})
|
||||
|
||||
// copy the the value
|
||||
copy(i.value, r.Value)
|
||||
|
||||
// set the expiry
|
||||
if r.Expiry != 0 {
|
||||
i.expiresAt = time.Now().Add(r.Expiry)
|
||||
}
|
||||
|
||||
// set the metadata
|
||||
for k, v := range r.Metadata {
|
||||
i.metadata[k] = v
|
||||
}
|
||||
|
||||
m.store.Set(key, i, r.Expiry)
|
||||
}
|
||||
|
||||
@@ -199,6 +219,7 @@ func (m *memoryStore) Write(r *store.Record, opts ...store.WriteOption) error {
|
||||
newRecord := store.Record{}
|
||||
newRecord.Key = r.Key
|
||||
newRecord.Value = make([]byte, len(r.Value))
|
||||
newRecord.Metadata = make(map[string]interface{})
|
||||
copy(newRecord.Value, r.Value)
|
||||
newRecord.Expiry = r.Expiry
|
||||
|
||||
@@ -208,6 +229,11 @@ func (m *memoryStore) Write(r *store.Record, opts ...store.WriteOption) error {
|
||||
if writeOpts.TTL != 0 {
|
||||
newRecord.Expiry = writeOpts.TTL
|
||||
}
|
||||
|
||||
for k, v := range r.Metadata {
|
||||
newRecord.Metadata[k] = v
|
||||
}
|
||||
|
||||
m.set(prefix, &newRecord)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user