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:
Asim Aslam
2020-06-03 09:45:08 +01:00
committed by GitHub
parent e4e56b0f3f
commit 7b379bf1f1
10 changed files with 1378 additions and 937 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,5 @@
// Code generated by protoc-gen-micro. DO NOT EDIT.
// source: store/service/proto/store.proto
// source: github.com/micro/go-micro/store/service/proto/store.proto
package go_micro_store

View File

@@ -11,6 +11,13 @@ service Store {
rpc Tables(TablesRequest) returns (TablesResponse) {};
}
message Field {
// type of value e.g string, int, int64, bool, float64
string type = 1;
// the actual value
string value = 2;
}
message Record {
// key of the record
string key = 1;
@@ -18,6 +25,8 @@ message Record {
bytes value = 2;
// time.Duration (signed int64 nanoseconds)
int64 expiry = 3;
// the associated metadata
map<string,Field> metadata = 4;
}
message ReadOptions {

View File

@@ -3,7 +3,9 @@ package service
import (
"context"
"fmt"
"io"
"reflect"
"time"
"github.com/micro/go-micro/v2/client"
@@ -137,10 +139,21 @@ func (s *serviceStore) Read(key string, opts ...store.ReadOption) ([]*store.Reco
records := make([]*store.Record, 0, len(rsp.Records))
for _, val := range rsp.Records {
metadata := make(map[string]interface{})
for k, v := range val.Metadata {
switch v.Type {
// TODO: parse all types
default:
metadata[k] = v
}
}
records = append(records, &store.Record{
Key: val.Key,
Value: val.Value,
Expiry: time.Duration(val.Expiry) * time.Second,
Key: val.Key,
Value: val.Value,
Expiry: time.Duration(val.Expiry) * time.Second,
Metadata: metadata,
})
}
@@ -163,11 +176,21 @@ func (s *serviceStore) Write(record *store.Record, opts ...store.WriteOption) er
Table: options.Table,
}
metadata := make(map[string]*pb.Field)
for k, v := range record.Metadata {
metadata[k] = &pb.Field{
Type: reflect.TypeOf(v).String(),
Value: fmt.Sprintf("%v", v),
}
}
_, err := s.Client.Write(s.Context(), &pb.WriteRequest{
Record: &pb.Record{
Key: record.Key,
Value: record.Value,
Expiry: int64(record.Expiry.Seconds()),
Key: record.Key,
Value: record.Value,
Expiry: int64(record.Expiry.Seconds()),
Metadata: metadata,
},
Options: writeOpts}, client.WithAddress(s.Nodes...))
if err != nil && errors.Equal(err, errors.NotFound("", "")) {