Developing lightweight computation at the DSG edge

Commit d407a874 authored by Dimitrios Vasilas's avatar Dimitrios Vasilas Committed by Peter Zeller
Browse files

Add function for listing keys and types of CRDTs embedded in a map CRDT (#1)

New function `ListMapKeys`
parent cbe18517
Pipeline #3 canceled with stages
......@@ -385,3 +385,53 @@ func testReadMany(t *testing.T) {
fmt.Print(counterVal)
}
func TestMapListMapKeys(t *testing.T) {
client, err := NewClient(Host{"127.0.0.1", 8087})
if err != nil {
t.Fatal(err)
}
defer client.Close()
tx, err := client.StartTransaction()
if err != nil {
t.Fatal(err)
}
timestamp := time.Now().Unix()
bucketname := fmt.Sprintf("bucket%d", timestamp)
bucket := Bucket{[]byte(bucketname)}
key := Key("keyMap")
err = bucket.Update(tx, MapUpdate(key,
CounterInc(Key("counter"), 13),
RegPut(Key("reg"), []byte("Hello World")),
SetAdd(Key("set"), []byte("A"), []byte("B"))))
if err != nil {
t.Fatal(err)
}
mapV, err := bucket.ReadMap(tx, key)
if err != nil {
t.Fatal(err)
}
keyList := mapV.ListMapKeys()
if err != nil {
t.Fatal(err)
}
for _, expected := range []MapEntryKey{
{[]byte("counter"), CRDTType_COUNTER},
{[]byte("reg"), CRDTType_LWWREG},
{[]byte("set"), CRDTType_ORSET},
} {
found := false
for _, entry := range keyList {
if bytes.Equal(entry.Key, expected.Key) && entry.CrdtType == expected.CrdtType {
found = true
break
}
}
if !found {
t.Fatalf("expected value %s not found in result (%s)", expected.Key, keyList)
}
}
}
......@@ -278,6 +278,24 @@ func (mrr *MapReadResult) Counter(key Key) (val int32, err error) {
return 0, fmt.Errorf("counter entry with key '%s' not found", key)
}
// MapEntryKey represents the key and type of a map entry (embedded CRDT).
type MapEntryKey struct {
Key []byte
CrdtType CRDTType
}
// ListMapKeys gives access to the keys and types of map entries (embedded CRDTs).
func (mrr *MapReadResult) ListMapKeys() []MapEntryKey {
keyList := make([]MapEntryKey, len(mrr.mapResp.Entries))
for i, me := range mrr.mapResp.Entries {
keyList[i] = MapEntryKey{
Key: me.Key.Key,
CrdtType: *me.Key.Type,
}
}
return keyList
}
// Represents updates that can be converted to top-level updates applicable to a bucket
// or nested updates applicable to a map
type UpdateConverter interface {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment