Developing lightweight computation at the DSG edge

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

[goclidote] Add function AntidoteAddArrayToSetInMapInBucket



This function should be significantly faster than writing arrays
item by item...
Signed-off-by: Roger Pueyo Centelles's avatarRoger Pueyo Centelles <rpueyo@ac.upc.edu>
parent f739b0d0
......@@ -63,14 +63,13 @@ func AntidoteAddItemToSetInBucket(bucketName string, setName string, itemValue s
return false
}
// AntidoteAddItemToSetInMapInBucket save an item to a set in a map in a bucket
func AntidoteAddItemToSetInMapInBucket(bucketName string, mapName string, setName string, itemValue string, DatabaseHost string, DatabasePort int) bool {
client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
errCheck(err, ERROR, "[AntidoteAddItemToSetInMapInBucket]: Unable to create a new AntidoteDB go client")
defer client.Close()
//TODO: remove this line
// fmt.Printf("About to write data \"%s\" in set \"%s\" in map \"%s\" in bucket \"%s\"", itemValue, setName, mapName, bucketName)
tx, err := client.StartTransaction()
errCheck(err, ERROR, "[AntidoteAddItemToSetInMapInBucket]: Unable to start a transaction using the go client")
......@@ -95,11 +94,58 @@ func AntidoteAddItemToSetInMapInBucket(bucketName string, mapName string, setNam
}
}
// Return false otherwise
return false
}
// AntidoteAddArrayToSetInMapInBucket save an item to a set in a map in a bucket
func AntidoteAddArrayToSetInMapInBucket(bucketName string, mapName string, setName string, itemArray []string, DatabaseHost string, DatabasePort int) bool {
client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
errCheck(err, ERROR, "[AntidoteAddArrayToSetInMapInBucket]: Unable to create a new AntidoteDB go client")
defer client.Close()
tx, err := client.StartTransaction()
errCheck(err, ERROR, "[AntidoteAddArrayToSetInMapInBucket]: Unable to start a transaction using the go client")
bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
mapKey := antidoteclient.Key(mapName)
setKey := antidoteclient.Key(setName)
for _, v := range itemArray {
err = bucket.Update(tx, antidoteclient.MapUpdate(mapKey,
antidoteclient.SetAdd(setKey, []byte(v))))
errCheck(err, ERROR, fmt.Sprintf("[AntidoteAddArrayToSetInMapInBucket]: Unable to add value %s to set %s in bucket %s", v, setName, bucketName))
}
mapVal, err := bucket.ReadMap(tx, mapKey)
errCheck(err, ERROR, fmt.Sprintf("[AntidoteAddArrayToSetInMapInBucket]: Unable to read set %s in bucket %s", setName, bucketName))
setVal, err := mapVal.Set(setKey)
errCheck(err, ERROR, fmt.Sprintf("[AntidoteAddArrayToSetInMapInBucket]: Unable to read set %s in bucket %s", setName, bucketName))
err = tx.Commit()
errCheck(err, ERROR, "[AntidoteAddArrayToSetInMapInBucket]: Unable to commit transaction")
// Return true if all values belong now to the set (either they have been added now, or were there already)
anyMissing := false
for _,v := range itemArray {
thisFound := false
for _, w := range setVal {
if v == string(w) {
thisFound = true
}
if !thisFound {
anyMissing = true
break
}
}
}
// Return if all itemswere found
return !anyMissing
}
// AntidoteReadRegisterInBucket read a LWWW register in a bucket
func AntidoteReadRegisterInBucket(bucketName string, registerName string, DatabaseHost string, DatabasePort int) string {
......
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