107 lines
2.6 KiB
Kotlin
107 lines
2.6 KiB
Kotlin
package nl.astraeus.persistence
|
|
|
|
import nl.astraeus.persistence.domain.Company
|
|
import nl.astraeus.persistence.domain.Person
|
|
import java.io.File
|
|
import kotlin.random.Random
|
|
import kotlin.test.Test
|
|
|
|
class TestThreaded {
|
|
|
|
@Test
|
|
fun showTransactions() {
|
|
val log = TransactionLog(File("data", "test-threaded"))
|
|
|
|
log.showTransactions()
|
|
}
|
|
|
|
@Test
|
|
fun testThreaded() {
|
|
println("Test threaded")
|
|
|
|
val pst = Persistent(
|
|
directory = File("data", "test-threaded"),
|
|
arrayOf(
|
|
index<Person>("name") { p -> (p as? Person)?.name ?: "" },
|
|
index<Person>("age") { p -> (p as? Person)?.age ?: -1 },
|
|
index<Person>("ageGt20") { p -> ((p as? Person)?.age ?: 0) > 20 },
|
|
index<Person>("ageGt23") { p -> ((p as? Person)?.age ?: 0) > 23 },
|
|
index<Person>("ageOnlyGt20") { p ->
|
|
if (((p as? Person)?.age ?: 0) > 20) {
|
|
true
|
|
} else {
|
|
null
|
|
}
|
|
},
|
|
index<Person>("nameAndAge") { p ->
|
|
val person = p as? Person
|
|
|
|
if (person == null) {
|
|
null
|
|
} else {
|
|
person.name to person.age
|
|
}
|
|
},
|
|
index<Company>("name") { p -> (p as? Company)?.name ?: "" },
|
|
)
|
|
)
|
|
|
|
val companyNames = arrayOf("Company A", "Company B", "Company C", "Company D", "Company E")
|
|
val names = arrayOf("John Doe", "Jane Doe", "John Smith", "Jane Smith", "John Johnson", "Jane Johnson")
|
|
val random = Random(System.currentTimeMillis())
|
|
|
|
val empty = pst.query {
|
|
count<Person>() == 0
|
|
}
|
|
|
|
if (empty) {
|
|
val runnable = {
|
|
repeat(10) {
|
|
pst.transaction {
|
|
val company = Company(
|
|
id = 0L,
|
|
name = companyNames[random.nextInt(companyNames.size)]
|
|
)
|
|
repeat(10) {
|
|
val person = Person(
|
|
id = 0L,
|
|
name = names[random.nextInt(names.size)],
|
|
age = random.nextInt(0, 100),
|
|
)
|
|
//person.company = company
|
|
|
|
store(person)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
val threads = Array(25) {
|
|
Thread(runnable)
|
|
}
|
|
|
|
for (thread in threads) {
|
|
thread.start()
|
|
}
|
|
|
|
for (thread in threads) {
|
|
thread.join()
|
|
}
|
|
}
|
|
|
|
pst.query {
|
|
searchIndex<Person>("nameAndAge") { nameAndAge ->
|
|
val (name, age) = nameAndAge as Pair<String, Int>
|
|
|
|
name.contains("mit") && age > 80
|
|
}.forEach { p ->
|
|
println("Found person by name and age: ${p.id}: ${p.name} - ${p.age}")
|
|
}
|
|
}
|
|
|
|
//pst.snapshot()
|
|
pst.datastore.printStatus()
|
|
pst.removeOldFiles()
|
|
}
|
|
}
|