Developing lightweight computation at the DSG edge

Commit f739b0d0 authored by Roger Pueyo Centelles's avatar Roger Pueyo Centelles
Browse files

[goclidote-demo] Demo 4: write values in set in map inside map



This commit contains a simple demo in which different values are
written to a set in a map inside a map:

root/
    /
    /myBucket/
             /
             /myOuterMap/
                        /
	                /myOuterSet = [1, 2, 3...]
                        /
                        /myInnerMap/
                                   /
                                   /myInnerSet=[1, 6, 7...]

Lessons learnt:

 - Yes, it's possible to have a map inside a map
 - Sets (as in CRDTType_ORSET) store duplicate entries only once
Signed-off-by: Roger Pueyo Centelles's avatarRoger Pueyo Centelles <rpueyo@ac.upc.edu>
parent 534a8f1c
......@@ -10,7 +10,7 @@ import (
// The port needs to be the port of the protocol-buffer interface (usually 8087)
// AntidoteAddItemToSetInBucket save an item to a set in a bucketdcrf
func maain() {
func main1() {
client, err := antidoteclient.NewClient(antidoteclient.Host{"127.0.0.1", 8087})
errCheck(err, "")
......@@ -74,7 +74,7 @@ func maain() {
}
// Panic on error
func errChseck(err error, message string) {
func errCheck(err error, message string) {
if err != nil {
fmt.Println(message)
panic(err)
......
......@@ -12,7 +12,7 @@ import (
// The port needs to be the port of the protocol-buffer interface (usually 8087)
// AntidoteAddItemToSetInBucket save an item to a set in a bucketdcrf
func maaain() {
func main2() {
var myBucket = "device-123"
var myTimestamp = time.Now().UTC()
var myKey = myTimestamp.Format("2006-01-02-15-04-05")
......@@ -83,7 +83,7 @@ func maaain() {
}
// Panic on error
func errdCheck(err error, message string) {
func errCheck(err error, message string) {
if err != nil {
fmt.Println(message)
panic(err)
......
......@@ -13,7 +13,7 @@ import (
// The port needs to be the port of the protocol-buffer interface (usually 8087)
// AntidoteAddItemToSetInBucket save an item to a set in a bucketdcrf
func main() {
func main3() {
var myBucket = "device-1234"
var myMap = "raw"
......
package main
import (
"fmt"
"math/rand"
"time"
"github.com/AntidoteDB/antidote-go-client"
)
// This demo program writes an element in a set in a map in a bucket,
// then writes an element in a set in a map inside a map in a bucket
// AntidoteAddItemToSetInBucket save an item to a set in a bucketdcrf
func main() {
var myBucket = "myBucket"
var myOuterMap = "myOuterMap"
var myInnerMap = "myInnerMap"
var myOuterSet = "myOuterSet" //time.Now().UTC().Format("2006-01-02-15-04-05")
var myInnerSet = "myInnerSet" //time.Now().UTC().Format("2006-01-02-15-04-05")
r1 := rand.New(rand.NewSource(time.Now().UnixNano()))
client, err := antidoteclient.NewClient(antidoteclient.Host{"127.0.0.1", 8087})
errCheck(err, "")
defer client.Close()
tx, err := client.StartTransaction()
errCheck(err, "")
bucket := antidoteclient.Bucket{[]byte(myBucket)}
outerMapKey := antidoteclient.Key(myOuterMap)
outerSetKey := antidoteclient.Key(myOuterSet)
//Write an element in a set in a map in a bucket
err = bucket.Update(tx, antidoteclient.MapUpdate(outerMapKey,
antidoteclient.SetAdd(outerSetKey, []byte(fmt.Sprintf("%d", r1.Intn(100))),
[]byte(fmt.Sprintf("%d", r1.Intn(100))), []byte(fmt.Sprintf("%d", r1.Intn(100))))))
errCheck(err, "")
outerMapVal, err := bucket.ReadMap(tx, outerMapKey)
errCheck(err, "")
outerKeyList := outerMapVal.ListMapKeys()
fmt.Println("Listing keys in outer map:")
for k, v := range outerKeyList {
fmt.Print(k)
fmt.Print(": ")
fmt.Print(string(v.Key))
found := false
for _, w := range []antidoteclient.MapEntryKey{
{[]byte("map"), antidoteclient.CRDTType_RRMAP},
{[]byte("set"), antidoteclient.CRDTType_ORSET},
{[]byte("counter"), antidoteclient.CRDTType_COUNTER},
{[]byte("reg"), antidoteclient.CRDTType_LWWREG},
} {
if v.CrdtType == w.CrdtType {
fmt.Print(" (")
fmt.Print(string(w.Key))
fmt.Print(")")
if v.CrdtType == antidoteclient.CRDTType_ORSET {
setVal, err := outerMapVal.Set(outerSetKey)
errCheck(err, "")
fmt.Print("[")
for _, x := range setVal {
fmt.Print(string(x))
fmt.Print(",")
}
fmt.Print("]")
}
found = true
fmt.Println("")
break
}
}
if !found {
fmt.Print("(unknown)")
}
}
fmt.Println("")
innerMapKey := antidoteclient.Key(myInnerMap)
innerSetKey := antidoteclient.Key(myInnerSet)
//Write an element in a set in a map inside a map in a bucket
err = bucket.Update(tx, antidoteclient.MapUpdate(outerMapKey,
antidoteclient.MapUpdate(innerMapKey,
antidoteclient.SetAdd(innerSetKey, []byte(fmt.Sprintf("%d", r1.Intn(100)))))))
errCheck(err, "")
outerMapVal, err = bucket.ReadMap(tx, outerMapKey)
errCheck(err, "")
outerKeyList = outerMapVal.ListMapKeys()
fmt.Println("Listing keys in outer map:")
for k, v := range outerKeyList {
fmt.Print(k)
fmt.Print(": ")
fmt.Print(string(v.Key))
found := false
for _, w := range []antidoteclient.MapEntryKey{
{[]byte("set"), antidoteclient.CRDTType_ORSET},
{[]byte("counter"), antidoteclient.CRDTType_COUNTER},
{[]byte("reg"), antidoteclient.CRDTType_LWWREG},
{[]byte("map"), antidoteclient.CRDTType_RRMAP},
} {
if v.CrdtType == w.CrdtType {
fmt.Print(" (")
fmt.Print(string(w.Key))
fmt.Print(")")
if v.CrdtType == antidoteclient.CRDTType_ORSET {
setVal, err := outerMapVal.Set(outerSetKey)
errCheck(err, "")
fmt.Print("[")
for _, x := range setVal {
fmt.Print(string(x))
fmt.Print(",")
}
fmt.Print("]\n")
} else if v.CrdtType == antidoteclient.CRDTType_RRMAP {
fmt.Println("")
innerMapVal, err := outerMapVal.Map(innerMapKey)
errCheck(err, "")
fmt.Println("Listing keys in inner map:")
innerKeyList := innerMapVal.ListMapKeys()
for m, y := range innerKeyList {
fmt.Print(k)
fmt.Print(".")
fmt.Print(m)
fmt.Print(": ")
fmt.Print(string(y.Key))
}
fmt.Println("")
}
found = true
break
}
}
if !found {
fmt.Print("(unknown)")
}
}
err = tx.Commit()
errCheck(err, "")
}
// Panic on error
func errCheck(err error, message string) {
if err != nil {
fmt.Println(message)
panic(err)
}
}
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