69 lines
2.1 KiB
Kotlin
69 lines
2.1 KiB
Kotlin
package nl.astraeus.persistence
|
|
|
|
import java.io.File
|
|
import java.io.ObjectInputStream
|
|
import java.util.*
|
|
|
|
class TransactionLog(
|
|
directory: File,
|
|
val decryptionKey: String? = null,
|
|
) {
|
|
val fileManager = FileManager(directory)
|
|
|
|
fun showTransactions(printer: (String) -> Unit = ::println) {
|
|
fileManager.findLastSnapshot().let { (after, snapshot) ->
|
|
printer("Last snapshot: $snapshot")
|
|
|
|
printer("Snapshot:")
|
|
snapshot?.inputStream()?.use { input ->
|
|
ObjectInputStream(DecryptingInputStream(input, decryptionKey)).use { ois ->
|
|
val versionNumber = ois.readInt()
|
|
check(versionNumber == 1) {
|
|
"Unsupported version number: $versionNumber"
|
|
}
|
|
val transactionNumber = ois.readLong()
|
|
printer("[$versionNumber] $transactionNumber")
|
|
val dataObj = ois.readObject()
|
|
val data = dataObj as MutableMap<Class<*>, TypeData>
|
|
printer("Data:")
|
|
printer("\tClasses:")
|
|
for ((cls, entries) in data.entries) {
|
|
printer("\t\t- $cls: ${entries.data.keys.size}")
|
|
}
|
|
}
|
|
}
|
|
|
|
val transactions = fileManager.findTransactionsAfter(after ?: 0L)
|
|
|
|
printer("Transactions:")
|
|
transactions?.forEach { transaction ->
|
|
transaction.inputStream().use { input ->
|
|
ObjectInputStream(DecryptingInputStream(input, decryptionKey)).use { ois ->
|
|
val versionNumber = ois.readInt()
|
|
check(versionNumber == 1) {
|
|
"Unsupported version number: $versionNumber"
|
|
}
|
|
val transactionNumber = ois.readLong()
|
|
val actions = ois.readObject()
|
|
val actionList = when(actions) {
|
|
is Set<*> -> {
|
|
LinkedList(actions as Set<Action>)
|
|
}
|
|
is List<*> -> {
|
|
actions as List<Action>
|
|
}
|
|
else -> {
|
|
emptyList()
|
|
}
|
|
}
|
|
printer("\t[$transactionNumber]")
|
|
for (action in actionList) {
|
|
printer("\t\t- $action")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|