Skip to content

Commit 380d9fb

Browse files
committed
[ECO-5482] Annotated objects realtime API with relevant spec ids
1 parent ef1b3d7 commit 380d9fb

6 files changed

Lines changed: 29 additions & 19 deletions

File tree

lib/src/main/java/io/ably/lib/objects/LiveObjects.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import io.ably.lib.objects.type.counter.LiveCounter;
55
import io.ably.lib.objects.type.map.LiveMap;
66
import io.ably.lib.objects.type.map.LiveMapValue;
7-
import io.ably.lib.types.Callback;
87
import org.jetbrains.annotations.Blocking;
98
import org.jetbrains.annotations.NonBlocking;
109
import org.jetbrains.annotations.NotNull;

live-objects/src/main/kotlin/io/ably/lib/objects/DefaultLiveObjects.kt

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,19 @@ internal class DefaultLiveObjects(internal val channelName: String, internal val
9898
}
9999

100100
private suspend fun createMapAsync(entries: MutableMap<String, LiveMapValue>): LiveMap {
101-
adapter.throwIfInvalidWriteApiConfiguration(channelName)
101+
adapter.throwIfInvalidWriteApiConfiguration(channelName) // RTO11c, RTO11d, RTO11e
102102

103-
if (entries.keys.any { it.isEmpty() }) {
104-
throw objectError("Map keys should not be empty")
103+
if (entries.keys.any { it.isEmpty() }) { // RTO11f2
104+
throw invalidInputError("Map keys should not be empty")
105105
}
106106

107-
// Create initial value operation
107+
// RTO11f4 - Create initial value operation
108108
val initialMapValue = DefaultLiveMap.initialValue(entries)
109109

110-
// Create initial value JSON string
110+
// RTO11f5 - Create initial value JSON string
111111
val initialValueJSONString = gson.toJson(initialMapValue)
112112

113-
// Create object ID from initial value
113+
// RTO11f8 - Create object ID from initial value
114114
val (objectId, nonce) = getObjectIdStringWithNonce(ObjectType.Map, initialValueJSONString)
115115

116116
// Create ObjectMessage with the operation
@@ -124,28 +124,29 @@ internal class DefaultLiveObjects(internal val channelName: String, internal val
124124
)
125125
)
126126

127-
// Publish the message
127+
// RTO11g - Publish the message
128128
publish(arrayOf(msg))
129129

130-
// Check if object already exists in pool, otherwise create a zero-value object using the sequential scope
130+
// RTO11h - Check if object already exists in pool, otherwise create a zero-value object using the sequential scope
131131
return objectsPool.get(objectId) as? LiveMap ?: withContext(sequentialScope.coroutineContext) {
132132
objectsPool.createZeroValueObjectIfNotExists(objectId) as LiveMap
133133
}
134134
}
135135

136136
private suspend fun createCounterAsync(initialValue: Number): LiveCounter {
137-
adapter.throwIfInvalidWriteApiConfiguration(channelName)
137+
adapter.throwIfInvalidWriteApiConfiguration(channelName) // RTO12c, RTO12d, RTO12e
138138

139139
// Validate input parameter
140140
if (initialValue.toDouble().isNaN() || initialValue.toDouble().isInfinite()) {
141-
throw objectError("Counter value should be a valid number")
141+
throw invalidInputError("Counter value should be a valid number")
142142
}
143143

144+
// RTO12f2
144145
val initialCounterValue = DefaultLiveCounter.initialValue(initialValue)
145-
// Create initial value operation
146+
// RTO12f3 - Create initial value operation
146147
val initialValueJSONString = gson.toJson(initialCounterValue)
147148

148-
// Create object ID from initial value
149+
// RTO12f6- Create object ID from initial value
149150
val (objectId, nonce) = getObjectIdStringWithNonce(ObjectType.Counter, initialValueJSONString)
150151

151152
// Create ObjectMessage with the operation
@@ -159,15 +160,18 @@ internal class DefaultLiveObjects(internal val channelName: String, internal val
159160
)
160161
)
161162

162-
// Publish the message
163+
// RTO12g - Publish the message
163164
publish(arrayOf(msg))
164165

165-
// Check if object already exists in pool, otherwise create a zero-value object using the sequential scope
166+
// RTO12h - Check if object already exists in pool, otherwise create a zero-value object using the sequential scope
166167
return objectsPool.get(objectId) as? LiveCounter ?: withContext(sequentialScope.coroutineContext) {
167168
objectsPool.createZeroValueObjectIfNotExists(objectId) as LiveCounter
168169
}
169170
}
170171

172+
/**
173+
* Spec: RTO11f8, RTO12f6
174+
*/
171175
private suspend fun getObjectIdStringWithNonce(objectType: ObjectType, initialValue: String): Pair<String, String> {
172176
val nonce = generateNonce()
173177
val msTimestamp = ServerTime.getCurrentTime(adapter) // RTO16 - Get server time for nonce generation

live-objects/src/main/kotlin/io/ably/lib/objects/ErrorCodes.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ internal enum class ErrorCode(public val code: Int) {
66
MaxMessageSizeExceeded(40_009),
77
InvalidObject(92_000),
88
// LiveMap specific error codes
9-
MapKeyShouldBeString(40_003),
9+
InvalidInputParams(40_003),
1010
MapValueDataTypeUnsupported(40_013),
1111
// Channel mode and state validation error codes
1212
ChannelModeRequired(40_024),

live-objects/src/main/kotlin/io/ably/lib/objects/Utils.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ internal fun serverError(errorMessage: String) = ablyException(errorMessage, Err
4141
internal fun objectError(errorMessage: String, cause: Throwable? = null): AblyException {
4242
return ablyException(errorMessage, ErrorCode.InvalidObject, HttpStatusCode.InternalServerError, cause)
4343
}
44+
45+
internal fun invalidInputError(errorMessage: String, cause: Throwable? = null): AblyException {
46+
return ablyException(errorMessage, ErrorCode.InvalidInputParams, HttpStatusCode.InternalServerError, cause)
47+
}
48+
4449
/**
4550
* Calculates the byte size of a string.
4651
* For non-ASCII, the byte size can be 2–4x the character count. For ASCII, there is no difference.

live-objects/src/main/kotlin/io/ably/lib/objects/type/livecounter/DefaultLiveCounter.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ internal class DefaultLiveCounter private constructor(
7575

7676
// Validate input parameter
7777
if (amount.isNaN() || amount.isInfinite()) {
78-
throw objectError("Counter value increment should be a valid number")
78+
throw invalidInputError("Counter value increment should be a valid number")
7979
}
8080

8181
// Create ObjectMessage with the COUNTER_INC operation
@@ -127,6 +127,7 @@ internal class DefaultLiveCounter private constructor(
127127

128128
/**
129129
* Creates initial value operation for counter creation.
130+
* Spec: RTO12f2
130131
*/
131132
internal fun initialValue(count: Number): CounterCreatePayload {
132133
return CounterCreatePayload(

live-objects/src/main/kotlin/io/ably/lib/objects/type/livemap/DefaultLiveMap.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ internal class DefaultLiveMap private constructor(
122122

123123
// Validate input parameters
124124
if (keyName.isEmpty()) {
125-
throw objectError("Map key should not be empty")
125+
throw invalidInputError("Map key should not be empty")
126126
}
127127

128128
// Create ObjectMessage with the MAP_SET operation
@@ -147,7 +147,7 @@ internal class DefaultLiveMap private constructor(
147147

148148
// Validate input parameter
149149
if (keyName.isEmpty()) {
150-
throw objectError("Map key should not be empty")
150+
throw invalidInputError("Map key should not be empty")
151151
}
152152

153153
// Create ObjectMessage with the MAP_REMOVE operation
@@ -199,6 +199,7 @@ internal class DefaultLiveMap private constructor(
199199

200200
/**
201201
* Creates an ObjectMap from map entries.
202+
* Spec: RTO11f4
202203
*/
203204
internal fun initialValue(entries: MutableMap<String, LiveMapValue>): MapCreatePayload {
204205
return MapCreatePayload(

0 commit comments

Comments
 (0)