xd.lck lock file is not removed after store is closed - kotlin

My assumption on xodus database locking was that closing the entity store would close the database.
I implemented this with a simple example using the use pattern that calls close:
package whatever
import jetbrains.exodus.entitystore.Entity
import kotlinx.dnq.XdEntity
import kotlinx.dnq.XdModel
import kotlinx.dnq.XdNaturalEntityType
import kotlinx.dnq.store.container.StaticStoreContainer
import kotlinx.dnq.util.initMetaData
import kotlinx.dnq.xdRequiredStringProp
import org.junit.Test
import java.nio.file.Files
class UnclosedTest {
private val dbFolder = Files.createTempDirectory(null).toFile()
private val store = StaticStoreContainer.init(
dbFolder = dbFolder,
environmentName = "store"
).also {
XdModel.registerNodes(
Bogus
)
initMetaData(XdModel.hierarchy, it)
}
#Test
fun `lock file is removed when store is closed`() {
store.use { store ->
store.transactional {
Bogus.new {
text = "gnarf"
}
}
}
assert(dbFolder.exists())
assert(dbFolder.isDirectory)
assert(!dbFolder.resolve("xd.lck").exists())
}
class Bogus(entity: Entity) : XdEntity(entity) {
companion object : XdNaturalEntityType<Bogus>()
var text by xdRequiredStringProp()
}
}
Surprisingly, this test fails with the xd.lck file still being present.
How do I close all resources, making sure the lockfile is removed?

The xd.lck file is being released on closing the database, not removed, regardless of which API do you use: Environments, EntityStores, or Xodus-DNQ DSL. See how it is implemented.

Related

read an input file from /res/raw (as an Input Stream)

enter image description hereenter image description hereenter image description hereI'm new to programming and learning kotlin through CS 193a: Android App Development on youtube.
I know that it's a bit dated but I like the teaching style. Anyway...
To read a .txt file as an input stream, the method is resources.openRawResouce(R.raw.id) is used.
Before, I had to create the raw folder in res and then put the .txt file called grewords.txt into the raw folder.
so "resources.openRawResource(R.raw.grewords)"
However, the method does not find the grewords.txt file. What am I doing wrong? I've found similar issues on the internet, but non of the solutions have helped me. Is it just an outdated way of doing things that doesn't work anymore? Cheers!
resources.openRawResource(R.raw.grewords)
package com.example.vocabquiz
import android.R
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import com.example.vocabquiz.databinding.ActivityMainBinding
import java.io.File
import java.io.InputStream
import java.util.Random
import java.util.Scanner
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private val defns = java.util.ArrayList<String>()
private lateinit var myadapter : ArrayAdapter<String>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
setupList()
binding.definitionsList.setOnItemClickListener { _, _, index, _ ->
//
defns.removeAt(index)
myadapter.notifyDataSetChanged()
}
fun readDictionaryFile() {
resources.openRawResource(R.raw.grewords)
}
}
fun setupList() {
// pick a random word
val list = ArrayList<String>()
list.add("Hello")
list.add("Rob")
list.add("BoBoBo")
val rand = Random()
val index = rand.nextInt(list.size)
val word = list[index]
// pick random definitions for the word
defns.add("a greeting")
defns.add("something you say when you are done")
defns.add("a dude")
defns.add("another name for a duck")
defns.add("the President")
defns.add("nothing at all")
defns.shuffle()
myadapter = ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, defns)
binding.definitionsList.adapter = myadapter
}
}
The problem is this:
import android.R
You imported the wrong R class, the one that is built into Android. You need to import the one that matches the package name of your app.

How to start a #Bean with custom parameters after an event had happened with spring for tests?

I am working on adding RepositoryTests with TestContainers framework for a project that uses R2dbc and I am running into the following situation:
1 - On the main project I set r2dbc url (with port and hostname) on application.yaml file and spring data manages everything and things just work.
2 - On the Tests however, I am using TestContainers framework more specifically DockerComposeContainer which I use to create a mocked container using docker-compose.test.yaml file with the databases I need.
3 - This container creates a port number on the go I define a port number on my docker-compose file but the port number that DockerComposeContainer will provide me is random and changes everytime I run the tests, what makes having a static url on application-test.yaml not an option anymore.
So I need to dinamically create this bean R2dbcEntityTemplate at run time and only after the DockerComposeContainer will give me the port number. So my application can connect to the correct port and things should work as expected.
I tried to create this class:
package com.wayfair.samworkgroupsservice.adapter
import io.r2dbc.mssql.MssqlConnectionConfiguration
import io.r2dbc.mssql.MssqlConnectionFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.config.ConstructorArgumentValues
import org.springframework.beans.factory.support.BeanDefinitionRegistry
import org.springframework.beans.factory.support.GenericBeanDefinition
import org.springframework.context.ApplicationContext
import org.springframework.context.annotation.Profile
import org.springframework.data.r2dbc.core.DefaultReactiveDataAccessStrategy
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate
import org.springframework.data.r2dbc.dialect.SqlServerDialect
import org.springframework.r2dbc.core.DatabaseClient
import org.springframework.stereotype.Component
#Component
#Profile("test")
class TemplateFactory(
#Autowired val applicationContext: ApplicationContext
) {
private val beanFactory = applicationContext.autowireCapableBeanFactory as BeanDefinitionRegistry
fun registerTemplateBean(host: String, port: Int) {
val beanDefinition = GenericBeanDefinition()
beanDefinition.beanClass = R2dbcEntityTemplate::class.java
val args = ConstructorArgumentValues()
args.addIndexedArgumentValue(
0,
DatabaseClient.builder()
.connectionFactory(connectionFactory(host, port))
.bindMarkers(SqlServerDialect.INSTANCE.bindMarkersFactory)
.build()
)
args.addIndexedArgumentValue(1, DefaultReactiveDataAccessStrategy(SqlServerDialect.INSTANCE))
beanDefinition.constructorArgumentValues = args
beanFactory.registerBeanDefinition("R2dbcEntityTemplate", beanDefinition)
}
// fun entityTemplate(host: String = "localhost", port: Int = 1435) =
// R2dbcEntityTemplate(
// DatabaseClient.builder()
// .connectionFactory(connectionFactory(host, port))
// .bindMarkers(SqlServerDialect.INSTANCE.bindMarkersFactory)
// .build(),
// DefaultReactiveDataAccessStrategy(SqlServerDialect.INSTANCE)
// )
private fun connectionFactory(host: String, port: Int) =
MssqlConnectionFactory(
MssqlConnectionConfiguration.builder()
.host(host)
.port(port)
.username("sa")
.password("Password123##?")
.build()
)
}
And this is how my db initiliser looks like:
package com.wayfair.samworkgroupsservice.adapter.note
import com.wayfair.samworkgroupsservice.adapter.DBInitializerInterface
import com.wayfair.samworkgroupsservice.adapter.TemplateFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate
import org.testcontainers.containers.DockerComposeContainer
import org.testcontainers.containers.wait.strategy.Wait
import org.testcontainers.junit.jupiter.Container
import org.testcontainers.junit.jupiter.Testcontainers
import java.io.File
#Testcontainers
class NoteTagDBInitializer : DBInitializerInterface {
#Autowired
override lateinit var client: R2dbcEntityTemplate
#Autowired
lateinit var factory: TemplateFactory
override val sqlScripts = listOf(
"db/note/schema.sql",
"db/note/reset.sql",
"db/note/data.sql"
)
init {
factory.registerTemplateBean(
cont.getServiceHost("test-db-local_1", 1433),
cont.getServicePort("test-db-local_1", 1433)
)
}
companion object {
#Container
val cont: KDockerComposerContainer = KDockerComposerContainer("docker-compose.test.yml")
.withExposedService(
"test-db-local_1", 1433,
Wait.forListeningPort()
)
.withLocalCompose(true)
.also {
it.start()
val porttt = it.getServicePort("test-db-local_1", 1433)
print(porttt)
}
class KDockerComposerContainer(yamlFile: String) :
DockerComposeContainer<KDockerComposerContainer>(File(yamlFile))
}
}
I am not getting errors when trying to start this template factory with no useful error message,
But to be honest I don't know anymore if am putting effort into the correct solution, does anyone have any insight on how to pull this off or if I am doing anything wrong here?
So to summarise for production app it is fine, it starts based off of the url on application.yaml file and that's it, but for tests I need something dinamic with ports that will change everytime.
Thank you in advance ))
Spring already has a solution for your problem.
If you're using a quite recent Spring version (>= 5.2.5), you should utilize #DynamicPropertySource in order to adjust your test configuration properties with a dynamic value of the container database port. Read official spring documentation for more details and kotlin code examples.
If you're stuck with an older Spring version, the interface you need is ApplicationContextInitializer. See this spring github issue for a small example.

FlatFileParseException Parsing class path resource error in spring batch

I want to set classPath resource of csv file . Which is present in my
project resource folder. When set the class Path and run the project I
get issue in runtime issue of classpath resource
here is the issue
org.springframework.batch.item.file.FlatFileParseException: Parsing error at line: 1 in resource=[class path resource [result-match-metadata.csv]], input=[id city date player_of_match venue neutral_venue team1 team2 toss_winner toss_decision winner result result_margin eliminator method umpire1 umpire2]
result-match-metadata.csv file is present inside my resource folder
Batchconfig.kt
package com.nilmani.dashboardipl.data
import com.nilmani.dashboardipl.entity.Match
import org.springframework.batch.core.Job
import org.springframework.batch.core.Step
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory
import org.springframework.batch.core.launch.support.RunIdIncrementer
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider
import org.springframework.batch.item.database.JdbcBatchItemWriter
import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder
import org.springframework.batch.item.file.FlatFileItemReader
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.io.ClassPathResource
import javax.sql.DataSource
#Configuration
#EnableBatchProcessing
class BatchConfig {
#Autowired
private lateinit var jobBuilderFactory: JobBuilderFactory
#Autowired
private lateinit var stepBuilderFactory: StepBuilderFactory
val FIELD_NAMES = arrayOf(
"id","city","date","player_of_match","venue","neutral_venue",
"team1","team2","toss_winner","toss_decision",
"winner","result","result_margin","eliminator","method","umpire1","umpire2"
)
#Bean
fun reader(): FlatFileItemReader<MatchInput> {
return FlatFileItemReaderBuilder<MatchInput>()
.name("MatchItemReader")
.resource(ClassPathResource("result-match-metadata.csv"))
.delimited()
.names(*FIELD_NAMES)
.fieldSetMapper(object : BeanWrapperFieldSetMapper<MatchInput>() {
init {
setTargetType(MatchInput::class.java)
}
})
.build()
}
}
how to set csv file path in batch config
The resource is correctly found. The error means that the first line cannot be mapped to an instance of MatchInput. You just need to skip the header with FlatFileItemReaderBuilder#linesToSkip(1) in your item reader definition.

XDocReport converting odt to pdf how to set proper locale

I'm trying to convert some *.odt file to *.pdf using IXDocReport.
Here is the hypothetical content of the *.odt file: ${amount?string.currency} to be paid
Here is the code I do conversion with (you can run it in kotlin REPL):
import fr.opensagres.xdocreport.converter.ConverterTypeTo
import fr.opensagres.xdocreport.converter.ConverterTypeVia
import fr.opensagres.xdocreport.converter.Options
import fr.opensagres.xdocreport.document.IXDocReport
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry
import fr.opensagres.xdocreport.template.TemplateEngineKind
import java.io.ByteArrayInputStream
import java.io.File
val options: Options = Options.getTo(ConverterTypeTo.PDF).via(ConverterTypeVia.ODFDOM)
val content: ByteArray = File("/home/sandro/tmp/report.odt").readBytes()
val templateId: String = "someId"
val registry: XDocReportRegistry = XDocReportRegistry.getRegistry()
val data: MutableMap<String, Any> = mutableMapOf("amount" to 10)
ByteArrayInputStream(content).use { input ->
val report: IXDocReport =
registry.loadReport(input, templateId, TemplateEngineKind.Freemarker, true)
val tmpFile: File = createTempFile("out", ".pdf")
tmpFile.outputStream().use { output ->
report.convert(data, options, output)
println(tmpFile.toString())
}
}
and the result is the pdf file with string $10.00 to be paid
How can I set needed locale to XDocReport during conversion so the result could be changed to other currencies correctly?
P.S. I cannot control the template itself - so please do not tell me to add <#setting locale="${bean.locale}"> or something else to the template itself. The only place I can change is the code. Thanks in advance.
P.P.S. I need to render many templates per request and need to set locale per each template.
I have never used XDocReport, but maybe this will work: https://github.com/opensagres/xdocreport/wiki/FreemarkerTemplate "How to configure Freemarker?"
Quotation from there:
To configure Freemarker with XDocReport you must get the Configuration instance. To do > that you must
create a class (ex :
fr.opensagres.xdocreport.MyFreemarkerConfiguration) which implements
fr.opensagres.xdocreport.document.discovery.ITemplateEngineInitializerDiscovery.
register with SPI this class by creating the file
META-INF/services/fr.opensagres.xdocreport.document.discovery.ITemplateEngineInitializerDiscovery
with the name of you class :
fr.opensagres.xdocreport.MyFreemarkerConfiguration This file should be
in your classpath (you can for instance host it in the
src/META-INF/services/ of your project).
So you will need a class like this:
public class MyFreemarkerConfiguration implements ITemplateEngineInitializerDiscovery {
[...]
public void initialize(ITemplateEngine templateEngine) {
if (TemplateEngineKind.Freemarker.name().equals( templateEngine.getKind())) {
Configuration cfg = ((FreemarkerTemplateEngine) templateEngine).getFreemarkerConfiguration();
cfg.setLocale(...);
}
}
}

How do I rollback a session in ScalaQuery?

For my unit tests I want to setup a database, populate it with base information and run each tests within a session that rollbacks all changes made to the DB in order to always have a pristine copy for each tests.
I'm looking for something like
db withSession {
<create my objects under test>
<run operations>
<run asserts>
this.rollback()
}
The rollback function was in early versions of Scala Query but it seems that it is missing now. How should I implement this functionality?
Best regards
Here is a unit test that illustrates this behaviour
https://github.com/szeiger/scala-query/blob/master/src/test/scala/org/scalaquery/test/TransactionTest.scala
GitHub currently 404s on the link, but I pulled the source code out of the google cache:
package org.scalaquery.test
import org.junit.Test
import org.junit.Assert._
import org.scalaquery.ql._
import org.scalaquery.ql.extended.{ExtendedTable => Table}
import org.scalaquery.session.Database.threadLocalSession
import org.scalaquery.test.util._
import org.scalaquery.test.util.TestDB._
object TransactionTest extends DBTestObject(H2Disk, SQLiteDisk, Postgres, MySQL, DerbyDisk, HsqldbDisk, MSAccess, SQLServer)
class TransactionTest(tdb: TestDB) extends DBTest(tdb) {
import tdb.driver.Implicit._
#Test def test() {
val T = new Table[Int]("t") {
def a = column[Int]("a")
def * = a
}
db withSession {
T.ddl.create
}
val q = Query(T)
db withSession {
threadLocalSession withTransaction {
T.insert(42)
assertEquals(Some(42), q.firstOption)
threadLocalSession.rollback()
}
assertEquals(None, q.firstOption)
}
}
}