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("name") { p -> (p as? Person)?.name ?: "" }, index("age") { p -> (p as? Person)?.age ?: -1 }, index("ageGt20") { p -> ((p as? Person)?.age ?: 0) > 20 }, index("ageGt23") { p -> ((p as? Person)?.age ?: 0) > 23 }, index("ageOnlyGt20") { p -> if (((p as? Person)?.age ?: 0) > 20) { true } else { null } }, index("nameAndAge") { p -> val person = p as? Person if (person == null) { null } else { person.name to person.age } }, index("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() == 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("nameAndAge") { nameAndAge -> val (name, age) = nameAndAge as Pair 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() } }