Skip to content
This repository was archived by the owner on Feb 5, 2024. It is now read-only.

Commit 812b374

Browse files
committed
Add put data record method
1 parent d4c8af2 commit 812b374

File tree

1 file changed

+47
-8
lines changed

1 file changed

+47
-8
lines changed

src/main/kotlin/client/SimpleClient.kt

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import model.config.KeygroupConfig
66
import model.config.NodeConfig
77
import model.config.ReplicaNodeConfig
88
import model.config.TriggerNodeConfig
9-
import model.data.ClientID
10-
import model.data.KeygroupID
11-
import model.data.NodeID
9+
import model.data.*
1210
import org.apache.log4j.Logger
1311
import java.io.File
1412

@@ -23,8 +21,8 @@ private val logger = Logger.getLogger(Client::class.java.name)
2321
class Client(val address: String, val port: Int) {
2422

2523
/**
26-
* Adds the given information to the Keygroup with [keygroupId]; creates the keygroup if it did not exist yet.
27-
* If information is already present in the keygroup, this piece of information is ignored.
24+
* Adds the given information to the specified keygroup; creates the keygroup if it did not exist yet.
25+
* If a piece of information is already present in the keygroup, this specific piece of information is ignored.
2826
*
2927
* Note, that all clients and nodes have to be registered at the naming service before they can be added.
3028
*
@@ -39,7 +37,7 @@ class Client(val address: String, val port: Int) {
3937
triggerNodeIds: List<String> = emptyList()) {
4038

4139
// check whether valid keygroupID
42-
val keygroupID = KeygroupID.createFromString(keygroupId)?: run {
40+
val keygroupID = KeygroupID.createFromString(keygroupId) ?: run {
4341
logger.warn("Cannot update keygroup with the id $keygroupId as the id is not valid")
4442
return
4543
}
@@ -92,19 +90,27 @@ class Client(val address: String, val port: Int) {
9290
}
9391
}
9492

93+
/**
94+
* Removes the given information from the specified keygroup, if it exists.
95+
*
96+
* @param keygroupId - the id of the keygroup
97+
* @param clientIds - list of to be removed client ids
98+
* @param replicaNodeIds - list of to be removed replica node ids
99+
* @param triggerNodeIds - list of to be removed trigger node ids
100+
*/
95101
fun removeFromKeygroup(keygroupId: String, clientIds: List<String> = emptyList(),
96102
replicaNodeIds: List<String> = emptyList(), triggerNodeIds: List<String> = emptyList()) {
97103

98104
// check whether valid keygroupID
99-
val keygroupID = KeygroupID.createFromString(keygroupId)?: run {
105+
val keygroupID = KeygroupID.createFromString(keygroupId) ?: run {
100106
logger.warn("Cannot remove items from keygroup with the id $keygroupId as the id is not valid")
101107
return
102108
}
103109

104110
val request = KeygroupRequest(address, port)
105111

106112
// get current config first, return if not null
107-
val keygroupConfig = request.updateLocalKeygroupConfig(keygroupID)?: run {
113+
val keygroupConfig = request.updateLocalKeygroupConfig(keygroupID) ?: run {
108114
logger.warn("Keygroup $keygroupId does not exist")
109115
return
110116
}
@@ -143,6 +149,38 @@ class Client(val address: String, val port: Int) {
143149
}
144150
}
145151

152+
/**
153+
* Stores a data record of the keygroup with the given [keygroupId]. If a data record with the same [dataId] already
154+
* exists in the keygroup, it is overwritten.
155+
*/
156+
fun putData(keygroupId: String, dataId: String, key: String = "", value: String) {
157+
// check whether valid keygroupID
158+
val keygroupID = KeygroupID.createFromString(keygroupId) ?: run {
159+
logger.warn("Cannot remove items from keygroup with the id $keygroupId as the id is not valid")
160+
return
161+
}
162+
163+
val request = RecordRequest(address, port)
164+
logger.debug("Added data to keygroup $keygroupID$:" + request.putDataRecord(DataRecord(DataIdentifier(keygroupID,
165+
dataId), mapOf(key to value))))
166+
}
167+
168+
/**
169+
* Stores a data record of the keygroup with the given [keygroupId]. If a data record with the same [dataId] already
170+
* exists in the keygroup, it is overwritten.
171+
*/
172+
fun putData(keygroupId: String, dataId: String, valueMap: Map<String, String>) {
173+
// check whether valid keygroupID
174+
val keygroupID = KeygroupID.createFromString(keygroupId) ?: run {
175+
logger.warn("Cannot remove items from keygroup with the id $keygroupId as the id is not valid")
176+
return
177+
}
178+
179+
val request = RecordRequest(address, port)
180+
logger.debug("Added data to keygroup $keygroupID: " + request.putDataRecord(DataRecord(DataIdentifier(keygroupID,
181+
dataId), valueMap)))
182+
}
183+
146184
}
147185

148186
fun main(args: Array<String>) {
@@ -159,6 +197,7 @@ fun main(args: Array<String>) {
159197
nr.createNodeConfig(nodeConfig)
160198

161199
c.addToKeygroup(keygroupId, replicaNodeIds = listOf("N2"), ttl = 120)
200+
c.putData(keygroupId, "D1", value = "Testvalue")
162201

163202
// remove N3 (must fail)
164203
c.removeFromKeygroup(keygroupId, replicaNodeIds = listOf("N3"))

0 commit comments

Comments
 (0)