Developing lightweight computation at the DSG edge

goclidote.go 5.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package goclidote

import (
	"fmt"
	"strconv"

	"uc-monitor-go-test/cnml"

	"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

var api_host_port = "localhost:3000"

// 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, "")

	for _, v := range setVal {
		if string(v) == itemValue {
			return true
		}
	}

	return false
}

// AntidoteReadRegisterInBucket read a LWWW register  in a bucket
49
func AntidoteReadRegisterInBucket(bucketName string, registerName string) string {
50

51
52
53
	client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
	errCheck(err, "")
	defer client.Close()
54

55
56
	tx, err := client.StartTransaction()
	errCheck(err, "")
57

58
59
	bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
	key := antidoteclient.Key(registerName)
60

61
62
	setVal, err := bucket.ReadReg(tx, key)
	errCheck(err, "")
63

64
	return string(setVal)
65
66
67
}

// AntidoteSetRegisterInBucket set a LWWW register in a bucket
68
69
70
func AntidoteSetRegisterInBucket(bucketName string, registerName string, itemValue string) bool {

	client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
71
	errCheck(err, "")
72
	defer client.Close()
73

74
75
	tx, err := client.StartTransaction()
	errCheck(err, "")
76

77
78
	bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
	key := antidoteclient.Key(registerName)
79

80
81
	err = bucket.Update(tx, antidoteclient.RegPut(key, []byte(itemValue)))
	errCheck(err, "")
82

83
84
85
86
87
	setVal, err := bucket.ReadReg(tx, key)
	errCheck(err, "")

	if string(setVal[0]) == itemValue {
		return true
88
	}
89

90
91
92
93
	return false
}

// AntidoteRemoveRegisterInBucket remove (set null) a LWWW register in a bucket
94
95
96
func AntidoteRemoveRegisterInBucket(bucketName string, registerName string) bool {

	client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
97
	errCheck(err, "")
98
	defer client.Close()
99

100
101
	tx, err := client.StartTransaction()
	errCheck(err, "")
102

103
104
	bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
	key := antidoteclient.Key(registerName)
105

106
107
	err = bucket.Update(tx, antidoteclient.RegPut(key, []byte(nil)))
	errCheck(err, "")
108

109
110
111
112
113
	setVal, err := bucket.ReadReg(tx, key)
	errCheck(err, "")

	if string(setVal[0]) == "" {
		return true
114
	}
115

116
117
118
119
	return false
}

// AntidoteReadItemsFromSetInBucket read items from a set in a bucket
120
func AntidoteReadItemsFromSetInBucket(bucketName string, setName string) []string {
121
122
123

	var items []string

124
125
126
	client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
	errCheck(err, "")
	defer client.Close()
127

128
129
	tx, err := client.StartTransaction()
	errCheck(err, "")
130

131
132
133
134
135
136
137
138
	bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
	key := antidoteclient.Key(setName)

	setVal, err := bucket.ReadSet(tx, key)
	errCheck(err, "")

	for _, v := range setVal {
		items = append(items, string(v))
139
140
141
142
143
144
	}

	return items
}

// AntidoteRemoveItemFromSetInBucket removes an item from a set in a bucket
145
146
147
func AntidoteRemoveItemFromSetInBucket(bucketName string, setName string, itemValue string) bool {

	client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
148
	errCheck(err, "")
149
	defer client.Close()
150

151
152
	tx, err := client.StartTransaction()
	errCheck(err, "")
153

154
155
	bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
	key := antidoteclient.Key(setName)
156

157
158
159
160
161
162
163
164
165
166
167
	err = bucket.Update(tx, antidoteclient.SetRemove(key, []byte(itemValue)))
	errCheck(err, "")

	setVal, err := bucket.ReadSet(tx, key)
	errCheck(err, "")

	itemNotInSet := true

	for _, v := range setVal {
		if string(v) == itemValue {
			itemNotInSet = false
168
169
170
		}
	}

171
	return itemNotInSet
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
}

// ReadDevicesFromAntidote read all the devices from Antidote using João's REST server
func ReadDevicesFromAntidote() []cnml.DeviceIpv4sGraphserver {
	var devices []cnml.DeviceIpv4sGraphserver

	for _, v := range AntidoteReadItemsFromSetInBucket("guifi", "devices") {
		thisDevID, err := strconv.Atoi(v)
		errCheck(err, "")
		var thisDev cnml.DeviceIpv4sGraphserver
		thisDev.ID = thisDevID
		thisDev.GraphServer, err = strconv.Atoi(AntidoteReadRegisterInBucket(fmt.Sprintf("device-%d", thisDevID), "graphserver"))
		errCheck(err, "")
		thisDev.Addresses = AntidoteReadItemsFromSetInBucket(fmt.Sprintf("device-%d", thisDevID), "ipv4s")
		devices = append(devices, thisDev)
	}

	return devices
}

// Panic on error
func errCheck(err error, message string) {
	if err != nil {
		fmt.Println(message)
		panic(err)
	}
}