Add FileManager, File cleanup, Logger
This commit is contained in:
@@ -5,7 +5,6 @@ import java.io.ObjectInputStream
|
||||
import java.io.ObjectOutputStream
|
||||
import java.io.Serializable
|
||||
import java.text.DecimalFormat
|
||||
import java.util.*
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
@@ -35,10 +34,6 @@ class Datastore(
|
||||
private val indexes: MutableMap<Class<*>, MutableMap<String, PersistableIndex>> = ConcurrentHashMap()
|
||||
|
||||
init {
|
||||
if (!directory.exists()) {
|
||||
directory.mkdirs()
|
||||
}
|
||||
|
||||
for (index in indexes) {
|
||||
this.indexes.getOrPut(index.cls) {
|
||||
ConcurrentHashMap()
|
||||
@@ -48,49 +43,43 @@ class Datastore(
|
||||
loadTransactions()
|
||||
}
|
||||
|
||||
private fun loadTransactions() {
|
||||
synchronized(this) {
|
||||
val snapshots: Array<File>? = directory.listFiles { _, name -> name.startsWith("transaction-") && name.endsWith(".snp") }
|
||||
val files: Array<File>? = directory.listFiles { _, name -> name.startsWith("transaction-") && name.endsWith(".trn") }
|
||||
override fun toString(): String {
|
||||
return "Datastore(directory=${fileManager.directory}, classes=${data.keys.size}, indexes=${indexes.keys.size})"
|
||||
}
|
||||
|
||||
var lastSnapshot: Long? = null
|
||||
var lastSnapshotFile: File? = null
|
||||
|
||||
snapshots?.let {
|
||||
it.forEach {
|
||||
val trnx = getTrnx(it)
|
||||
if (lastSnapshot == null || trnx > (lastSnapshot ?: 0L)) {
|
||||
lastSnapshot = trnx
|
||||
lastSnapshotFile = it
|
||||
}
|
||||
}
|
||||
// print status, show number of entries for each class and index
|
||||
fun printStatus() {
|
||||
println(this)
|
||||
for ((cls, typeData) in data) {
|
||||
println(" ${cls.simpleName}: ${typeData.data.size}")
|
||||
for ((name, index) in indexes.getOrDefault(cls, mutableMapOf())) {
|
||||
println(" $name: ${index.index.size}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val lastSnapshotFile2 = fileManager.findLastSnapshot()
|
||||
private fun loadTransactions() {
|
||||
val start = System.nanoTime()
|
||||
|
||||
synchronized(this) {
|
||||
val (lastSnapshot, lastSnapshotFile) = fileManager.findLastSnapshot()
|
||||
|
||||
if (lastSnapshotFile != null) {
|
||||
ObjectInputStream(lastSnapshotFile?.inputStream()).use { ois ->
|
||||
ObjectInputStream(lastSnapshotFile.inputStream()).use { ois ->
|
||||
readSnapshot(ois)
|
||||
}
|
||||
}
|
||||
|
||||
val trns = fileManager.findTransactionsAfter(lastSnapshot ?: 0L)
|
||||
val transactions = fileManager.findTransactionsAfter(lastSnapshot ?: 0L)
|
||||
|
||||
files?.also { snaphotFiles ->
|
||||
Arrays.sort(snaphotFiles) { o1, o2 -> if (getTrnx(o1) > getTrnx(o2)) 1 else -1}
|
||||
|
||||
snaphotFiles.forEach { file ->
|
||||
if (getTrnx(file) > (lastSnapshot ?: 0L)) {
|
||||
ObjectInputStream(file.inputStream()).use { ois ->
|
||||
val transactionNumber = ois.readLong()
|
||||
nextTransactionNumber = transactionNumber + 1
|
||||
val actions = ois.readObject() as MutableList<Action>
|
||||
execute(actions)
|
||||
}
|
||||
}
|
||||
transactions?.forEach { file ->
|
||||
ObjectInputStream(file.inputStream()).use { ois ->
|
||||
readTransaction(ois)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Logger.debug("Loaded transactions in ${(System.nanoTime() - start) / 1_000_000}ms")
|
||||
}
|
||||
|
||||
private fun getTrnx(file: File): Long {
|
||||
@@ -112,6 +101,7 @@ class Datastore(
|
||||
typeData.data[action.obj.id] = action.obj
|
||||
|
||||
for (index in indexes[action.obj::class.java]?.values ?: listOf()) {
|
||||
index.remove(action.obj.id)
|
||||
index.add(action.obj as Persistable)
|
||||
}
|
||||
}
|
||||
@@ -120,7 +110,7 @@ class Datastore(
|
||||
typeData.data.remove(action.obj.id)
|
||||
|
||||
for (index in indexes[action.obj::class.java]?.values ?: listOf()) {
|
||||
index.remove(action.obj)
|
||||
index.remove(action.obj.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -161,17 +151,29 @@ class Datastore(
|
||||
fun storeActions(actions: MutableList<Action>) {
|
||||
if (actions.isNotEmpty()) {
|
||||
synchronized(this) {
|
||||
val number = transactionFormatter.format(nextTransactionNumber)
|
||||
val file = File(directory, "transaction-$number.trn")
|
||||
ObjectOutputStream(file.outputStream()).use { oos ->
|
||||
oos.writeLong(nextTransactionNumber++)
|
||||
oos.writeObject(actions)
|
||||
}
|
||||
writeTransaction(actions)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun readTransaction(ois: ObjectInputStream) {
|
||||
val transactionNumber = ois.readLong()
|
||||
nextTransactionNumber = transactionNumber + 1
|
||||
val actions = ois.readObject() as MutableList<Action>
|
||||
execute(actions)
|
||||
}
|
||||
|
||||
private fun writeTransaction(actions: MutableList<Action>) {
|
||||
val number = transactionFormatter.format(nextTransactionNumber)
|
||||
val file = File(directory, "transaction-$number.trn")
|
||||
ObjectOutputStream(file.outputStream()).use { oos ->
|
||||
oos.writeLong(nextTransactionNumber++)
|
||||
oos.writeObject(actions)
|
||||
}
|
||||
}
|
||||
|
||||
fun snapshot() {
|
||||
val start = System.nanoTime()
|
||||
synchronized(this) {
|
||||
val number = transactionFormatter.format(nextTransactionNumber)
|
||||
val file = File(directory, "transaction-$number.snp")
|
||||
@@ -189,6 +191,7 @@ class Datastore(
|
||||
}
|
||||
}
|
||||
}
|
||||
Logger.debug("Snapshot in ${(System.nanoTime() - start) / 1_000_000}ms")
|
||||
}
|
||||
|
||||
private fun readSnapshot(ois: ObjectInputStream) {
|
||||
@@ -226,4 +229,8 @@ class Datastore(
|
||||
}
|
||||
}
|
||||
|
||||
fun removeOldFiles() {
|
||||
fileManager.removeOldFiles()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user