Skip to content

Commit

Permalink
reparse: patch index instead of rebuilding it
Browse files Browse the repository at this point in the history
Following one of the router refactorings (cb0c5c7), the index used in GET/POST requests was immutable, it wasn't replaced by the new index built after reparsing.
To fix this, patch the index instead of creating a new one.
  • Loading branch information
handymenny committed Oct 5, 2024
1 parent ed2bcca commit 8528599
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ data class LibraryIndex(
}
}

fun replaceLine(line: IndexLine): Boolean {
synchronized(lock) {
items.removeIf { it.id == line.id }
items.add(line)
outputCache.remove(line.id)
}
return true
}

fun addMultiLine(line: MultiIndexLine): Boolean {
synchronized(lock) {
return multiItems.add(line)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ class JavalinApp {
if (reparseStrategy != "off") {
CoroutineScope(Dispatchers.IO).launch {
reparseLibrary(reparseStrategy, store, index, compression)
// Rebuild index
index = LibraryIndex.buildIndex(store, maxOutputCache)
}
}
}
Expand Down Expand Up @@ -123,12 +121,17 @@ class JavalinApp {
index
.getAll()
.filterNot { auto && it.parserVersion == parserVersion }
.map { async { reparseItem(it, store, compression) } }
.map { async { reparseItem(it, index, store, compression) } }
.awaitAll()
}
}

private fun reparseItem(indexLine: IndexLine, store: String, compression: Boolean) {
private fun reparseItem(
indexLine: IndexLine,
index: LibraryIndex,
store: String,
compression: Boolean,
) {
val compressed = indexLine.compressed
val capPath = "/output/${indexLine.id}.json"
try {
Expand Down Expand Up @@ -160,6 +163,9 @@ class JavalinApp {
it.capabilities.timestamp = capabilities.timestamp
it.store(null, store, compression)
} ?: throw NullPointerException("Reparsed Capabilities is null")

val newLine = indexLine.copy(compressed = compression)
index.replaceLine(newLine)
} catch (ex: Exception) {
echoSafe("Error re-parsing ${indexLine.id}:\t${ex.message}", true)
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ class LruCache<K, V>(private val maxCapacity: Int? = null) {

operator fun set(key: K, value: V) = put(key, value)

fun remove(key: K): V? {
synchronized(lock) {
return internalMap.remove(key)
}
}

fun put(key: K, value: V, skipIfFull: Boolean = false): Boolean {
if (maxCapacity == 0 || skipIfFull && full()) return false
synchronized(lock) { internalMap[key] = value }
Expand Down

0 comments on commit 8528599

Please sign in to comment.