From 6c45ce24e2dd66101fc9ab8f1f1a6e903d36145a Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Wed, 3 Jun 2020 09:45:08 +0100 Subject: [PATCH] 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 --- file.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/file.go b/file.go index 5abd6da..71bd0f8 100644 --- a/file.go +++ b/file.go @@ -54,6 +54,7 @@ type fileHandle struct { type record struct { Key string Value []byte + Metadata map[string]interface{} ExpiresAt time.Time } @@ -221,6 +222,11 @@ func (m *fileStore) get(fd *fileHandle, k string) (*store.Record, error) { newRecord := &store.Record{} newRecord.Key = storedRecord.Key newRecord.Value = storedRecord.Value + newRecord.Metadata = make(map[string]interface{}) + + for k, v := range storedRecord.Metadata { + newRecord.Metadata[k] = v + } if !storedRecord.ExpiresAt.IsZero() { if storedRecord.ExpiresAt.Before(time.Now()) { @@ -238,10 +244,16 @@ func (m *fileStore) set(fd *fileHandle, r *store.Record) error { item := &record{} item.Key = r.Key item.Value = r.Value + item.Metadata = make(map[string]interface{}) + if r.Expiry != 0 { item.ExpiresAt = time.Now().Add(r.Expiry) } + for k, v := range r.Metadata { + item.Metadata[k] = v + } + // marshal the data data, _ := json.Marshal(item) @@ -348,6 +360,7 @@ func (m *fileStore) Write(r *store.Record, opts ...store.WriteOption) error { newRecord := store.Record{} newRecord.Key = r.Key newRecord.Value = r.Value + newRecord.Metadata = make(map[string]interface{}) newRecord.Expiry = r.Expiry if !writeOpts.Expiry.IsZero() { @@ -357,6 +370,10 @@ func (m *fileStore) Write(r *store.Record, opts ...store.WriteOption) error { newRecord.Expiry = writeOpts.TTL } + for k, v := range r.Metadata { + newRecord.Metadata[k] = v + } + return m.set(fd, &newRecord) }