Skip to content

Commit 301a76d

Browse files
committed
fix(model-server): implement Long range safe ID generator
1 parent 9901d4e commit 301a76d

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

model-server/src/main/kotlin/org/modelix/model/server/store/ClientIdProcessor.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@ import javax.cache.processor.MutableEntry
2121
class ClientIdProcessor : EntryProcessor<String?, String?, Long> {
2222
@Throws(EntryProcessorException::class)
2323
override fun process(mutableEntry: MutableEntry<String?, String?>, vararg objects: Any): Long {
24-
val idStr = mutableEntry.value
25-
val id = try {
26-
idStr?.toLong() ?: 0L
27-
} catch (e : NumberFormatException) {
28-
0L
29-
} + 1L
24+
val id = generateId(mutableEntry.value)
3025
mutableEntry.value = id.toString()
3126
return id
3227
}

model-server/src/main/kotlin/org/modelix/model/server/store/InMemoryStoreClient.kt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ import java.io.FileWriter
2222
import java.io.IOException
2323
import java.util.*
2424

25+
fun generateId(idStr: String?): Long {
26+
return try {
27+
val candidate = idStr?.toLong()
28+
if (candidate == null || candidate == Long.MAX_VALUE || candidate < 0L) {
29+
0L
30+
} else {
31+
candidate
32+
}
33+
} catch (e : NumberFormatException) {
34+
0L
35+
} + 1L
36+
}
37+
2538
class InMemoryStoreClient : IStoreClient {
2639
companion object {
2740
private val LOG = LoggerFactory.getLogger(InMemoryStoreClient::class.java)
@@ -79,11 +92,7 @@ class InMemoryStoreClient : IStoreClient {
7992

8093
@Synchronized
8194
override fun generateId(key: String): Long {
82-
val id = try {
83-
get(key)?.toLong() ?: 0L
84-
} catch (e : NumberFormatException) {
85-
0L
86-
} + 1L
95+
val id = generateId(get(key))
8796
put(key, id.toString(), false)
8897
return id
8998
}

0 commit comments

Comments
 (0)