Developing lightweight computation at the DSG edge

Commit 576838c0 authored by Roger Pueyo Centelles's avatar Roger Pueyo Centelles
Browse files

[monitor-fetch-demo] Demo code to test goclidote's features


Signed-off-by: Roger Pueyo Centelles's avatarRoger Pueyo Centelles <rpueyo@ac.upc.edu>
parent 8148e861
package main
import (
"fmt"
"github.com/mweberUKL/go-antidote"
)
// DatabaseHost defines the host of the AntidoteDB instance to connect to is running
var DatabaseHost = "localhost"
// DatabasePort defines the port of the host where the AntidoteDB instance to connect to is running
var DatabasePort = 8087
func main() {
var buckName = "test-bucket"
var rgName = "test-register"
var stName = "test-set-00"
// Write to a register (LWW)
AntidoteSetRegisterInBucket(buckName, rgName, "aaa")
fmt.Println(AntidoteReadRegisterInBucket(buckName, "test-register"))
// Overwrite the register
AntidoteSetRegisterInBucket(buckName, rgName, "bbb")
fmt.Println(AntidoteReadRegisterInBucket(buckName, "test-register"))
// Once again...
AntidoteSetRegisterInBucket(buckName, rgName, "ccc")
fmt.Println(AntidoteReadRegisterInBucket(buckName, "test-register"))
// So far, so good. Now let's try sets.
// Add items to a set
AntidoteAddItemToSetInBucket(buckName, stName, "test01")
fmt.Println(AntidoteReadItemsFromSetInBucket(buckName, stName))
// Add a second item
AntidoteAddItemToSetInBucket(buckName, stName, "test02")
fmt.Println(AntidoteReadItemsFromSetInBucket(buckName, stName))
// Add a third item
AntidoteAddItemToSetInBucket(buckName, stName, "test03")
fmt.Println(AntidoteReadItemsFromSetInBucket(buckName, stName))
// Remove the second item
AntidoteRemoveItemFromSetInBucket(buckName, stName, "test02")
fmt.Println(AntidoteReadItemsFromSetInBucket(buckName, stName))
// Add a fourth item
AntidoteAddItemToSetInBucket(buckName, stName, "test04")
fmt.Println(AntidoteReadItemsFromSetInBucket(buckName, stName))
}
// AntidoteAddItemToSetInBucket save an item to a set in a bucket
func AntidoteAddItemToSetInBucket(bucketName string, setName string, itemValue string) bool {
client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
errCheck(err, "")
defer client.Close()
tx, err := client.StartTransaction()
errCheck(err, "")
bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
key := antidoteclient.Key(setName)
err = bucket.Update(tx, antidoteclient.SetAdd(key, []byte(itemValue)))
errCheck(err, "")
setVal, err := bucket.ReadSet(tx, key)
errCheck(err, "")
err = tx.Commit()
errCheck(err, "")
for _, v := range setVal {
if string(v) == itemValue {
return true
}
}
return false
}
// AntidoteReadRegisterInBucket read a LWWW register in a bucket
func AntidoteReadRegisterInBucket(bucketName string, registerName string) string {
client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
errCheck(err, "")
defer client.Close()
tx, err := client.StartTransaction()
errCheck(err, "")
bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
key := antidoteclient.Key(registerName)
setVal, err := bucket.ReadReg(tx, key)
errCheck(err, "")
err = tx.Commit()
errCheck(err, "")
return string(setVal[:])
}
// AntidoteSetRegisterInBucket set a LWWW register in a bucket
func AntidoteSetRegisterInBucket(bucketName string, registerName string, itemValue string) bool {
client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
errCheck(err, "")
defer client.Close()
tx, err := client.StartTransaction()
errCheck(err, "")
bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
key := antidoteclient.Key(registerName)
err = bucket.Update(tx, antidoteclient.RegPut(key, []byte(itemValue)))
errCheck(err, "")
setVal, err := bucket.ReadReg(tx, key)
errCheck(err, "")
err = tx.Commit()
errCheck(err, "")
if string(setVal[:]) == itemValue {
return true
}
return false
}
// AntidoteRemoveRegisterInBucket remove (set null) a LWWW register in a bucket
func AntidoteRemoveRegisterInBucket(bucketName string, registerName string) bool {
client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
errCheck(err, "")
defer client.Close()
tx, err := client.StartTransaction()
errCheck(err, "")
bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
key := antidoteclient.Key(registerName)
err = bucket.Update(tx, antidoteclient.RegPut(key, []byte("")))
errCheck(err, "")
setVal, err := bucket.ReadReg(tx, key)
errCheck(err, "")
err = tx.Commit()
errCheck(err, "")
if len(setVal) == 0 {
return true
}
return false
}
// AntidoteReadItemsFromSetInBucket read items from a set in a bucket
func AntidoteReadItemsFromSetInBucket(bucketName string, setName string) []string {
var items []string
client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
errCheck(err, "")
defer client.Close()
tx, err := client.StartTransaction()
errCheck(err, "")
bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
key := antidoteclient.Key(setName)
setVal, err := bucket.ReadSet(tx, key)
errCheck(err, "")
err = tx.Commit()
errCheck(err, "")
for _, v := range setVal {
items = append(items, string(v))
}
return items
}
// AntidoteRemoveItemFromSetInBucket removes an item from a set in a bucket
func AntidoteRemoveItemFromSetInBucket(bucketName string, setName string, itemValue string) bool {
client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
errCheck(err, "")
defer client.Close()
tx, err := client.StartTransaction()
errCheck(err, "")
bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
key := antidoteclient.Key(setName)
err = bucket.Update(tx, antidoteclient.SetRemove(key, []byte(itemValue)))
errCheck(err, "")
setVal, err := bucket.ReadSet(tx, key)
errCheck(err, "")
err = tx.Commit()
errCheck(err, "")
for _, v := range setVal {
if string(v) == itemValue {
return false
}
}
return false
}
// Panic on error
func errCheck(err error, message string) {
if err != nil {
fmt.Println(message)
panic(err)
}
}
package main
import (
"fmt"
"uc-monitor-go-test/goclidote"
)
func main() {
fmt.Println("paco")
goclidote.AntidoteSetRegisterInBucket("device-paco", "graphserver", "ttt")
fmt.Println(goclidote.AntidoteReadRegisterInBucket("device-paco", "graphserver"))
}
// Panic on error
func errCheck(err error, message string) {
if err != nil {
fmt.Println(message)
panic(err)
}
}
......@@ -212,6 +212,7 @@ func main() {
// Remove old devices from AntidoteDB not in the CNML file anymore
for k, v := range oldAntidoteDevices {
v = v
// Remove the device's graphserver from AntidoteDB
if removeGraphserverFromDeviceIpv4sGraphserver(v) {
antidoteDeleteGraphserverSuccess++
......
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