Skip to content

Commit

Permalink
reparsing: reparse capabilities in a temp directory
Browse files Browse the repository at this point in the history
  • Loading branch information
handymenny committed Nov 4, 2024
1 parent 9e8853c commit ab7d4a7
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,17 +298,16 @@ object IOUtils {
}

/**
* if [compressed] is true, automatically appends ".gz". Return null if file doesn't exist. File
* is moved from [srcPath] to [dstPath] and it's inputSource is returned.
* if [compressed] is true, automatically appends ".gz". File is moved from [srcPath] to
* [dstPath].
*/
fun inputSourceAndMove(srcPath: String, dstPath: String, compressed: Boolean): InputSource? {
fun move(srcPath: String, dstPath: String, compressed: Boolean) {
val addExtension = if (compressed) ".gz" else ""
val inputFile = File(srcPath + addExtension)
if (!inputFile.exists()) return null
if (!inputFile.exists()) return

val dstFile = File(dstPath + addExtension)
inputFile.moveTo(dstFile.path)
return dstFile.toInputSource(compressed)
return inputFile.moveTo(dstFile.path)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import it.smartphonecombo.uecapabilityparser.model.index.LibraryIndex
import it.smartphonecombo.uecapabilityparser.util.Config
import it.smartphonecombo.uecapabilityparser.util.Parsing
import java.io.File
import java.io.FileNotFoundException
import kotlin.system.measureTimeMillis
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -128,6 +129,8 @@ class JavalinApp {
val auto = strategy !== "force"

withContext(Dispatchers.Custom) {
IOUtils.createDirectories("$store/temp/output/")
IOUtils.createDirectories("$store/temp/input/")
IOUtils.createDirectories("$store/backup/output/")
IOUtils.createDirectories("$store/backup/input/")
index
Expand All @@ -144,48 +147,61 @@ class JavalinApp {
store: String,
compression: Boolean,
) {
val compressed = indexLine.compressed
val oldCapCompressed = indexLine.compressed
val capPath = "/output/${indexLine.id}.json"
val parsingResult: Parsing
val oldCapabilities: Capabilities

try {
val capText =
IOUtils.inputSourceAndMove("$store$capPath", "$store/backup$capPath", compressed)
?: NullInputSource
IOUtils.getInputSource("$store$capPath", oldCapCompressed)
?: throw FileNotFoundException("$capPath not found")

val capabilities = Json.custom().decodeFromInputSource<Capabilities>(capText)
oldCapabilities = Json.custom().decodeFromInputSource<Capabilities>(capText)
val inputMap =
indexLine.inputs.mapNotNull {
IOUtils.inputSourceAndMove(
"$store/input/$it",
"$store/backup/input/$it",
compressed,
)
IOUtils.getInputSource("$store/input/$it", oldCapCompressed)
}

val request =
RequestParse.buildRequest(
*inputMap.toTypedArray(),
type = capabilities.logType,
type = oldCapabilities.logType,
description = indexLine.description,
ratList = guessRats(capabilities, inputMap.size),
ratList = guessRats(oldCapabilities, inputMap.size),
)

Parsing.fromRequest(request)?.let {
// Reset capabilities id and timestamp
it.capabilities.id = capabilities.id
it.capabilities.timestamp = capabilities.timestamp
it.store(index, store, compression)
} ?: throw NullPointerException("Reparsed Capabilities is null")
parsingResult =
Parsing.fromRequest(request)
?: throw NullPointerException("Reparsed ${indexLine.id} Capabilities is null")

// Reset capabilities id and timestamp
parsingResult.capabilities.id = oldCapabilities.id
parsingResult.capabilities.timestamp = oldCapabilities.timestamp

// store in temp
parsingResult.store(index, "$store/temp", compression)
} catch (ex: Exception) {
echoSafe("Error re-parsing ${indexLine.id}:\t${ex.message}", true)
try {
// restore prev version
IOUtils.copy("$store/backup$capPath", "$store$capPath", compressed)
indexLine.inputs.forEach {
IOUtils.copy("$store/backup/input/$it", "$store/input/$it", compressed)
}
} catch (ex: Exception) {
ex.printStackTrace()
return
}

try {
val newIndex = index.find(oldCapabilities.id)!!

// Move old -> bak
IOUtils.move("$store$capPath", "$store/backup$capPath", oldCapCompressed)
indexLine.inputs.forEach { input ->
IOUtils.move("$store/input/$input", "$store/backup/input/$input", oldCapCompressed)
}

// Move tmp -> store
IOUtils.move("$store/temp$capPath", "$store$capPath", compression)
newIndex.inputs.forEach { input ->
IOUtils.move("$store/temp/input/$input", "$store/input/$input", compression)
}
} catch (ex: Exception) {
echoSafe("Error storing reparsed ${indexLine.id}:\t${ex.message}", true)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal class ServerModeReparseTest {
deleteDirectory(tmpStorePath)
} catch (_: Exception) {}
try {
createMultiDir("$resourcesPath/oracleForReparse")
createEmptyDirs("$resourcesPath/oracleForReparse")
copyDirectory("$resourcesPath/inputForReparse", tmpStorePath)
replaceVersion("$tmpStorePath/good", "staging", parserVersion)
} catch (_: Exception) {}
Expand Down Expand Up @@ -132,9 +132,13 @@ internal class ServerModeReparseTest {
assertTrue(dirsSimilar(oraclePath, storePath))
}

private fun createMultiDir(path: String) {
private fun createEmptyDirs(path: String) {
File(path).listFiles()?.filter(File::isDirectory)?.forEach {
IOUtils.createDirectories(it.absolutePath + "/multi")
IOUtils.createDirectories(it.absolutePath + "/backup/input")
IOUtils.createDirectories(it.absolutePath + "/backup/output")
IOUtils.createDirectories(it.absolutePath + "/temp/input")
IOUtils.createDirectories(it.absolutePath + "/temp/output")
}
}

Expand Down

This file was deleted.

This file was deleted.

Binary file not shown.
Loading

0 comments on commit ab7d4a7

Please sign in to comment.