Fix multi-threading, add Query interface, java test
This commit is contained in:
@@ -6,6 +6,7 @@ import java.io.ObjectOutputStream
|
||||
import java.io.Serializable
|
||||
import java.text.DecimalFormat
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.atomic.AtomicLong
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
enum class ActionType {
|
||||
@@ -14,8 +15,8 @@ enum class ActionType {
|
||||
}
|
||||
|
||||
class TypeData(
|
||||
var nextId: Long = 1L,
|
||||
val data: MutableMap<Any, Persistable> = ConcurrentHashMap(),
|
||||
var nextId: AtomicLong = AtomicLong(1L),
|
||||
val data: MutableMap<Serializable, Persistable> = ConcurrentHashMap(),
|
||||
) : Serializable
|
||||
|
||||
class Action(
|
||||
@@ -43,6 +44,23 @@ class Datastore(
|
||||
loadTransactions()
|
||||
}
|
||||
|
||||
fun getNextId(javaClass: Class<Persistable>): Long {
|
||||
if (data[javaClass] == null) {
|
||||
synchronized(this) {
|
||||
if (data[javaClass] == null) {
|
||||
data[javaClass] = TypeData()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return data[javaClass]!!.nextId.getAndIncrement()
|
||||
}
|
||||
|
||||
fun setMaxId(javaClass: Class<Persistable>, id: Long) {
|
||||
val nextId = data.getOrPut(javaClass) { TypeData() }.nextId
|
||||
if (nextId.get() <= id) nextId.set(id + 1)
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return "Datastore(directory=${fileManager.directory}, classes=${data.keys.size}, indexes=${indexes.keys.size})"
|
||||
}
|
||||
@@ -95,14 +113,15 @@ class Datastore(
|
||||
|
||||
when (action.type) {
|
||||
ActionType.STORE -> {
|
||||
if (action.obj.id == 0L) {
|
||||
action.obj.id = typeData.nextId++
|
||||
}
|
||||
typeData.data[action.obj.id] = action.obj
|
||||
|
||||
if (action.obj.id >= typeData.nextId.get()) {
|
||||
typeData.nextId.set(action.obj.id + 1)
|
||||
}
|
||||
|
||||
for (index in indexes[action.obj::class.java]?.values ?: listOf()) {
|
||||
index.remove(action.obj.id)
|
||||
index.add(action.obj as Persistable)
|
||||
index.add(action.obj)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,6 +137,15 @@ class Datastore(
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun <T : Persistable> count(clazz: KClass<T>): Int {
|
||||
val typeData = data.getOrPut(clazz.java) {
|
||||
TypeData()
|
||||
}
|
||||
|
||||
return typeData.data.size
|
||||
}
|
||||
|
||||
fun <T : Persistable> find(clazz: KClass<T>, id: Long): T? {
|
||||
val typeData = data.getOrPut(clazz.java) {
|
||||
TypeData()
|
||||
@@ -148,15 +176,18 @@ class Datastore(
|
||||
return indexes[kClass.java]?.get(indexName)
|
||||
}
|
||||
|
||||
fun storeActions(actions: MutableList<Action>) {
|
||||
fun storeAndExecute(actions: MutableList<Action>) {
|
||||
if (actions.isNotEmpty()) {
|
||||
synchronized(this) {
|
||||
writeTransaction(actions)
|
||||
execute(actions)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun readTransaction(ois: ObjectInputStream) {
|
||||
val versionNumber = ois.readInt()
|
||||
check (versionNumber == 1) { "Unsupported version number: $versionNumber" }
|
||||
val transactionNumber = ois.readLong()
|
||||
nextTransactionNumber = transactionNumber + 1
|
||||
val actions = ois.readObject() as MutableList<Action>
|
||||
@@ -167,6 +198,8 @@ class Datastore(
|
||||
val number = transactionFormatter.format(nextTransactionNumber)
|
||||
val file = File(directory, "transaction-$number.trn")
|
||||
ObjectOutputStream(file.outputStream()).use { oos ->
|
||||
// version number
|
||||
oos.writeInt(1)
|
||||
oos.writeLong(nextTransactionNumber++)
|
||||
oos.writeObject(actions)
|
||||
}
|
||||
@@ -178,6 +211,8 @@ class Datastore(
|
||||
val number = transactionFormatter.format(nextTransactionNumber)
|
||||
val file = File(directory, "transaction-$number.snp")
|
||||
ObjectOutputStream(file.outputStream()).use { oos ->
|
||||
// version number
|
||||
oos.writeInt(1)
|
||||
oos.writeLong(nextTransactionNumber++)
|
||||
oos.writeObject(data)
|
||||
oos.writeInt(indexes.size)
|
||||
@@ -195,6 +230,8 @@ class Datastore(
|
||||
}
|
||||
|
||||
private fun readSnapshot(ois: ObjectInputStream) {
|
||||
val versionNumber = ois.readInt()
|
||||
check (versionNumber == 1) { "Unsupported version number: $versionNumber" }
|
||||
nextTransactionNumber = ois.readLong() + 1
|
||||
data.clear()
|
||||
data.putAll(ois.readObject() as MutableMap<Class<*>, TypeData>)
|
||||
|
||||
@@ -5,14 +5,28 @@ import kotlin.reflect.KClass
|
||||
|
||||
typealias PersistableIndex = Index<out Persistable>
|
||||
|
||||
inline fun <reified T : Persistable> index(
|
||||
name: String,
|
||||
noinline value: (Persistable) -> Serializable?
|
||||
): Index<T> = Index(
|
||||
T::class,
|
||||
name,
|
||||
value
|
||||
)
|
||||
|
||||
class Index<T : Persistable>(
|
||||
kcls: KClass<T>,
|
||||
val cls: Class<T>,
|
||||
val name: String,
|
||||
val value: (Persistable) -> Serializable?,
|
||||
) : Serializable {
|
||||
val cls: Class<T> = kcls.java
|
||||
val index = mutableMapOf<Serializable, MutableSet<Long>>()
|
||||
|
||||
constructor(
|
||||
cls: KClass<T>,
|
||||
name: String,
|
||||
value: (Persistable) -> Serializable?
|
||||
) : this(cls.java, name, value)
|
||||
|
||||
fun add(obj: Persistable) {
|
||||
val key = value(obj)
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import java.io.ObjectInputStream
|
||||
import java.io.ObjectOutputStream
|
||||
import java.io.Serializable
|
||||
|
||||
interface Persistable : Serializable, Cloneable {
|
||||
interface Persistable : Serializable {
|
||||
var id: Long
|
||||
var version: Long
|
||||
|
||||
@@ -23,3 +23,13 @@ interface Persistable : Serializable, Cloneable {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
abstract class AbstractPersistable : Persistable {
|
||||
override fun copy(): Persistable {
|
||||
return super.copy()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID: Long = 1L
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,10 @@ class Persistent(
|
||||
) {
|
||||
val datastore: Datastore = Datastore(directory, indexes)
|
||||
|
||||
fun <T> query(block: Query.() -> T): T {
|
||||
return block(Query(this))
|
||||
}
|
||||
|
||||
fun transaction(block: Transaction.() -> Unit) {
|
||||
var cleanup = false
|
||||
if (transactions.get() == null) {
|
||||
|
||||
@@ -3,12 +3,105 @@ package nl.astraeus.nl.astraeus.persistence
|
||||
import java.io.Serializable
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
class Transaction(
|
||||
inline fun <reified T : Persistable> Query.count(): Int = this.count(T::class)
|
||||
inline fun <reified T : Persistable> Query.find(id: Long): T? = this.find(T::class, id)
|
||||
inline fun <reified T : Persistable> Query.search(noinline search: (T) -> Boolean): List<T> =
|
||||
this.search(T::class, search)
|
||||
inline fun <reified T : Persistable> Query.findByIndex(
|
||||
indexName: String,
|
||||
search: Serializable
|
||||
): List<T> = this.findByIndex(T::class, indexName, search)
|
||||
inline fun <reified T : Persistable> Query.searchIndex(
|
||||
indexName: String,
|
||||
noinline search: (Serializable) -> Boolean,
|
||||
): List<T> = this.searchIndex(T::class, indexName, search)
|
||||
|
||||
inline fun <reified T : Persistable> Transaction.count(): Int = this.count(T::class)
|
||||
inline fun <reified T : Persistable> Transaction.find(id: Long): T? = this.find(T::class, id)
|
||||
inline fun <reified T : Persistable> Transaction.search(noinline search: (T) -> Boolean): List<T> =
|
||||
this.search(T::class, search)
|
||||
inline fun <reified T : Persistable> Transaction.findByIndex(
|
||||
indexName: String,
|
||||
search: Serializable
|
||||
): List<T> = this.findByIndex(T::class, indexName, search)
|
||||
inline fun <reified T : Persistable> Transaction.searchIndex(
|
||||
indexName: String,
|
||||
noinline search: (Serializable) -> Boolean,
|
||||
): List<T> = this.searchIndex(T::class, indexName, search)
|
||||
|
||||
open class Query(
|
||||
val persistent: Persistent,
|
||||
) : Serializable {
|
||||
|
||||
fun <T : Persistable> count(clazz: Class<T>): Int = count(clazz.kotlin)
|
||||
fun <T : Persistable> count(clazz: KClass<T>): Int = persistent.datastore.count(clazz)
|
||||
|
||||
fun <T : Persistable> find(clazz: Class<T>, id: Long): T? {
|
||||
return find(clazz.kotlin, id)
|
||||
}
|
||||
|
||||
open fun <T : Persistable> find(clazz: KClass<T>, id: Long): T? = persistent.datastore.find(clazz, id)
|
||||
|
||||
open fun <T : Persistable> search(clazz: KClass<T>, search: (T) -> Boolean): List<T> = persistent.datastore.search(clazz, search)
|
||||
|
||||
fun <T : Persistable> findByIndex(
|
||||
kcls: KClass<T>,
|
||||
indexName: String,
|
||||
search: Serializable
|
||||
): List<T> = findByIndex(kcls.java, indexName, search)
|
||||
|
||||
open fun <T : Persistable> findByIndex(
|
||||
cls: Class<T>,
|
||||
indexName: String,
|
||||
search: Serializable
|
||||
): List<T> {
|
||||
val result = mutableListOf<T>()
|
||||
val index = persistent.datastore.findIndex(cls.kotlin, indexName)
|
||||
?: throw IllegalArgumentException("Index with name $indexName not found for class ${cls.simpleName}")
|
||||
|
||||
index.find(search).forEach { id ->
|
||||
result.add(id as T)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
fun <T : Persistable> searchIndex(
|
||||
kcls: KClass<T>,
|
||||
indexName: String,
|
||||
search: (Serializable) -> Boolean,
|
||||
): List<T> = searchIndex(kcls.java, indexName, search)
|
||||
|
||||
open fun <T : Persistable> searchIndex(
|
||||
cls: Class<T>,
|
||||
indexName: String,
|
||||
search: (Serializable) -> Boolean,
|
||||
): List<T> {
|
||||
val result = mutableListOf<T>()
|
||||
val index = persistent.datastore.findIndex(cls.kotlin, indexName) ?: throw IllegalArgumentException("Index not found")
|
||||
|
||||
index.index.keys.forEach { key ->
|
||||
if (search(key)) {
|
||||
index.find(key).forEach { id ->
|
||||
result.add(id as T)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
class Transaction(
|
||||
persistent: Persistent,
|
||||
) : Query(persistent), Serializable {
|
||||
private val actions = mutableListOf<Action>()
|
||||
|
||||
fun store(obj: Persistable) {
|
||||
if (obj.id == 0L) {
|
||||
obj.id = persistent.datastore.getNextId(obj.javaClass)
|
||||
}
|
||||
|
||||
actions.add(Action(ActionType.STORE, obj))
|
||||
}
|
||||
|
||||
@@ -16,23 +109,23 @@ class Transaction(
|
||||
actions.add(Action(ActionType.DELETE, obj))
|
||||
}
|
||||
|
||||
fun <T : Persistable> find(clazz: KClass<T>, id: Long): T? {
|
||||
var result: T? = persistent.datastore.find(clazz, id)
|
||||
fun commit() {
|
||||
persistent.datastore.storeAndExecute(actions)
|
||||
actions.clear()
|
||||
}
|
||||
|
||||
override fun <T : Persistable> find(clazz: KClass<T>, id: Long): T? {
|
||||
var result = super.find(clazz, id)
|
||||
|
||||
for (action in actions) {
|
||||
if (action.obj::class == clazz && action.obj.id == id) {
|
||||
result = when {
|
||||
action.type == ActionType.DELETE -> {
|
||||
result = when(action.type) {
|
||||
ActionType.DELETE -> {
|
||||
null
|
||||
}
|
||||
|
||||
action.type == ActionType.STORE -> {
|
||||
ActionType.STORE -> {
|
||||
action.obj as? T
|
||||
}
|
||||
|
||||
else -> {
|
||||
result
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,10 +133,9 @@ class Transaction(
|
||||
return result
|
||||
}
|
||||
|
||||
fun <T : Persistable> search(clazz: KClass<T>, search: (T) -> Boolean): List<T> {
|
||||
val fromDatastore: List<T> = persistent.datastore.search(clazz, search)
|
||||
override fun <T : Persistable> search(clazz: KClass<T>, search: (T) -> Boolean): List<T> {
|
||||
val result = mutableListOf<T>()
|
||||
result.addAll(fromDatastore)
|
||||
result.addAll(super.search(clazz, search))
|
||||
|
||||
for (obj in result) {
|
||||
for (action in actions) {
|
||||
@@ -61,27 +153,13 @@ class Transaction(
|
||||
return result
|
||||
}
|
||||
|
||||
fun commit() {
|
||||
persistent.datastore.storeActions(actions)
|
||||
persistent.datastore.execute(actions)
|
||||
actions.clear()
|
||||
}
|
||||
|
||||
fun <T : Persistable> findByIndex(
|
||||
kClass: KClass<T>,
|
||||
indexName: String,
|
||||
search: Serializable
|
||||
): List<T> {
|
||||
override fun <T : Persistable> findByIndex(cls: Class<T>, indexName: String, search: Serializable): List<T> {
|
||||
val result = mutableListOf<T>()
|
||||
val index = persistent.datastore.findIndex(kClass, indexName)
|
||||
?: throw IllegalArgumentException("Index with name $indexName not found for class ${kClass.simpleName}")
|
||||
|
||||
index.find(search).forEach { id ->
|
||||
result.add(id as T)
|
||||
}
|
||||
val index = persistent.datastore.findIndex(cls.kotlin, indexName) ?: throw IllegalArgumentException("Index not found")
|
||||
result.addAll(super.findByIndex(cls, indexName, search))
|
||||
|
||||
for (action in actions) {
|
||||
if (action.obj::class == kClass) {
|
||||
if (action.obj::class == cls.kotlin) {
|
||||
if (action.type == ActionType.DELETE) {
|
||||
if (index.matches(action.obj, search)) {
|
||||
result.remove(action.obj as T)
|
||||
@@ -98,24 +176,13 @@ class Transaction(
|
||||
return result
|
||||
}
|
||||
|
||||
fun <T : Persistable> searchIndex(
|
||||
kClass: KClass<T>,
|
||||
indexName: String,
|
||||
search: (Serializable) -> Boolean,
|
||||
): List<T> {
|
||||
override fun <T : Persistable> searchIndex(cls: Class<T>, indexName: String, search: (Serializable) -> Boolean): List<T> {
|
||||
val result = mutableListOf<T>()
|
||||
val index = persistent.datastore.findIndex(kClass, indexName) ?: throw IllegalArgumentException("Index not found")
|
||||
|
||||
index.index.keys.forEach { key ->
|
||||
if (search(key)) {
|
||||
index.find(key).forEach { id ->
|
||||
result.add(id as T)
|
||||
}
|
||||
}
|
||||
}
|
||||
val index = persistent.datastore.findIndex(cls.kotlin, indexName) ?: throw IllegalArgumentException("Index not found")
|
||||
result.addAll(super.searchIndex(cls, indexName, search))
|
||||
|
||||
for (action in actions) {
|
||||
if (action.obj::class == kClass) {
|
||||
if (action.obj::class == cls.kotlin) {
|
||||
val indexedValue = index.value(action.obj)
|
||||
if (indexedValue != null && index.matches(action.obj, indexedValue)) {
|
||||
if (action.type == ActionType.DELETE) {
|
||||
|
||||
Reference in New Issue
Block a user