Developing lightweight computation at the DSG edge

goclidote.go 5.33 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
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, "")

39
40
41
	err = tx.Commit()
	errCheck(err, "")

42
43
44
45
46
47
48
49
50
51
	for _, v := range setVal {
		if string(v) == itemValue {
			return true
		}
	}

	return false
}

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

54
55
56
	client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
	errCheck(err, "")
	defer client.Close()
57

58
59
	tx, err := client.StartTransaction()
	errCheck(err, "")
60

61
62
	bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
	key := antidoteclient.Key(registerName)
63

64
65
	setVal, err := bucket.ReadReg(tx, key)
	errCheck(err, "")
66

67
68
69
70
	err = tx.Commit()
	errCheck(err, "")

	return string(setVal[:])
71
72
73
}

// AntidoteSetRegisterInBucket set a LWWW register in a bucket
74
75
76
func AntidoteSetRegisterInBucket(bucketName string, registerName string, itemValue string) bool {

	client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
77
	errCheck(err, "")
78
	defer client.Close()
79

80
81
	tx, err := client.StartTransaction()
	errCheck(err, "")
82

83
84
	bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
	key := antidoteclient.Key(registerName)
85

86
87
	err = bucket.Update(tx, antidoteclient.RegPut(key, []byte(itemValue)))
	errCheck(err, "")
88

89
90
91
	setVal, err := bucket.ReadReg(tx, key)
	errCheck(err, "")

92
93
94
95
	err = tx.Commit()
	errCheck(err, "")

	if string(setVal[:]) == itemValue {
96
		return true
97
	}
98

99
100
101
102
	return false
}

// AntidoteRemoveRegisterInBucket remove (set null) a LWWW register in a bucket
103
104
105
func AntidoteRemoveRegisterInBucket(bucketName string, registerName string) bool {

	client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
106
	errCheck(err, "")
107
	defer client.Close()
108

109
110
	tx, err := client.StartTransaction()
	errCheck(err, "")
111

112
113
	bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
	key := antidoteclient.Key(registerName)
114

115
116
	err = bucket.Update(tx, antidoteclient.RegPut(key, []byte(nil)))
	errCheck(err, "")
117

118
119
120
	setVal, err := bucket.ReadReg(tx, key)
	errCheck(err, "")

121
122
123
124
	err = tx.Commit()
	errCheck(err, "")

	if string(setVal[:]) == "" {
125
		return true
126
	}
127

128
129
130
131
	return false
}

// AntidoteReadItemsFromSetInBucket read items from a set in a bucket
132
func AntidoteReadItemsFromSetInBucket(bucketName string, setName string) []string {
133
134
135

	var items []string

136
137
138
	client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
	errCheck(err, "")
	defer client.Close()
139

140
141
	tx, err := client.StartTransaction()
	errCheck(err, "")
142

143
144
145
146
147
148
	bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
	key := antidoteclient.Key(setName)

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

149
150
151
	err = tx.Commit()
	errCheck(err, "")

152
153
	for _, v := range setVal {
		items = append(items, string(v))
154
155
156
157
158
159
	}

	return items
}

// AntidoteRemoveItemFromSetInBucket removes an item from a set in a bucket
160
161
162
func AntidoteRemoveItemFromSetInBucket(bucketName string, setName string, itemValue string) bool {

	client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
163
	errCheck(err, "")
164
	defer client.Close()
165

166
167
	tx, err := client.StartTransaction()
	errCheck(err, "")
168

169
170
	bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
	key := antidoteclient.Key(setName)
171

172
173
174
175
176
177
	err = bucket.Update(tx, antidoteclient.SetRemove(key, []byte(itemValue)))
	errCheck(err, "")

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

178
179
180
	err = tx.Commit()
	errCheck(err, "")

181
182
183
184
185
	itemNotInSet := true

	for _, v := range setVal {
		if string(v) == itemValue {
			itemNotInSet = false
186
187
188
		}
	}

189
	return itemNotInSet
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
}

// 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)
	}
}