Skip to content

Commit 3dd21b2

Browse files
Merge pull request #262 from JetBrains/patch-refactor-db-params
XD-1199: Refactor DB params
2 parents 3c9f6d5 + ba35914 commit 3dd21b2

File tree

14 files changed

+256
-317
lines changed

14 files changed

+256
-317
lines changed

entity-store/src/main/kotlin/jetbrains/exodus/entitystore/youtrackdb/YTDBDatabaseCompacter.kt

+3-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import java.io.File
2626
class YTDBDatabaseCompacter(
2727
private val db: YouTrackDB,
2828
private val dbProvider: YTDBDatabaseProvider,
29-
private val config: YTDBDatabaseConfig
29+
private val params: YTDBDatabaseParams
3030
) {
3131
companion object : KLogging()
3232

@@ -47,10 +47,9 @@ class YTDBDatabaseCompacter(
4747
}
4848

4949
logger.info("Dropping existing database...")
50-
db.drop(config.databaseName)
50+
db.drop(params.databaseName)
5151

52-
db.create(config.databaseName, config.databaseType,
53-
config.connectionConfig.userName, config.connectionConfig.password, "admin")
52+
db.create(params.databaseName, params.databaseType, params.userName, params.password, "admin")
5453

5554
dbProvider.withSession { session ->
5655
logger.info("Importing database from dump")

entity-store/src/main/kotlin/jetbrains/exodus/entitystore/youtrackdb/YTDBDatabaseConfig.kt

-88
This file was deleted.

entity-store/src/main/kotlin/jetbrains/exodus/entitystore/youtrackdb/YTDBDatabaseConnectionConfig.kt

-53
This file was deleted.

entity-store/src/main/kotlin/jetbrains/exodus/entitystore/youtrackdb/YTDBDatabaseFactory.kt

+11-34
Original file line numberDiff line numberDiff line change
@@ -15,55 +15,32 @@
1515
*/
1616
import com.jetbrains.youtrack.db.api.YouTrackDB
1717
import com.jetbrains.youtrack.db.api.YourTracks
18-
import com.jetbrains.youtrack.db.api.config.GlobalConfiguration
19-
import com.jetbrains.youtrack.db.api.config.YouTrackDBConfig
2018
import com.jetbrains.youtrack.db.internal.core.db.YouTrackDBImpl
21-
import jetbrains.exodus.entitystore.youtrackdb.YTDBDatabaseConfig
19+
import jetbrains.exodus.entitystore.youtrackdb.YTDBDatabaseParams
2220
import jetbrains.exodus.entitystore.youtrackdb.YTDBDatabaseProvider
2321
import jetbrains.exodus.entitystore.youtrackdb.YTDBDatabaseProviderImpl
24-
import java.util.*
25-
26-
object YouTrackDBConfigFactory {
27-
28-
fun createDefaultDBConfig(params: YTDBDatabaseConfig): YouTrackDBConfig {
29-
return YouTrackDBConfig.builder()
30-
.addGlobalConfigurationParameter(GlobalConfiguration.AUTO_CLOSE_AFTER_DELAY, true)
31-
.addGlobalConfigurationParameter(GlobalConfiguration.AUTO_CLOSE_DELAY, params.closeAfterDelayTimeout)
32-
.addGlobalConfigurationParameter(GlobalConfiguration.NON_TX_READS_WARNING_MODE, "SILENT")
33-
.apply {
34-
params.cipherKey?.also { key ->
35-
addGlobalConfigurationParameter(
36-
GlobalConfiguration.STORAGE_ENCRYPTION_KEY,
37-
Base64.getEncoder().encodeToString(key)
38-
)
39-
}
40-
}
41-
.apply(params.tweakConfig)
42-
.build()
43-
}
44-
}
4522

4623
object YouTrackDBFactory {
4724

48-
fun initYouTrackDb(params: YTDBDatabaseConfig, dbConfig: YouTrackDBConfig): YouTrackDB {
49-
return YourTracks.embedded(params.connectionConfig.databaseRoot, dbConfig).apply {
25+
fun createEmbedded(params: YTDBDatabaseParams): YouTrackDB {
26+
val config = params.youTrackDBConfig
27+
return YourTracks.embedded(params.databasePath, config).apply {
5028
(this as? YouTrackDBImpl)?.let {
51-
it.serverPassword = params.connectionConfig.password
52-
it.serverUser = params.connectionConfig.userName
29+
it.serverPassword = params.password
30+
it.serverUser = params.userName
5331
}
5432
}
5533
}
5634
}
5735

5836
object YTDBDatabaseProviderFactory {
5937

60-
fun createProviderWithDb(params: YTDBDatabaseConfig): YTDBDatabaseProvider {
61-
val dbConfig = YouTrackDBConfigFactory.createDefaultDBConfig(params)
62-
val youTrackDb = YouTrackDBFactory.initYouTrackDb(params, dbConfig)
63-
return createProvider(params, youTrackDb, dbConfig)
38+
fun createProvider(params: YTDBDatabaseParams): YTDBDatabaseProvider {
39+
val youTrackDb = YouTrackDBFactory.createEmbedded(params)
40+
return createProvider(params, youTrackDb)
6441
}
6542

66-
fun createProvider(params: YTDBDatabaseConfig, db: YouTrackDB, dbConfig: YouTrackDBConfig): YTDBDatabaseProvider {
67-
return YTDBDatabaseProviderImpl(params, db, dbConfig)
43+
fun createProvider(params: YTDBDatabaseParams, youTrackDB: YouTrackDB): YTDBDatabaseProvider {
44+
return YTDBDatabaseProviderImpl(params, youTrackDB)
6845
}
6946
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright ${inceptionYear} - ${year} ${owner}
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package jetbrains.exodus.entitystore.youtrackdb
18+
19+
import com.jetbrains.youtrack.db.api.DatabaseType
20+
import com.jetbrains.youtrack.db.api.config.GlobalConfiguration
21+
import com.jetbrains.youtrack.db.api.config.YouTrackDBConfig
22+
import com.jetbrains.youtrack.db.api.config.YouTrackDBConfigBuilder
23+
import java.util.*
24+
import kotlin.math.min
25+
26+
class YTDBDatabaseParams private constructor(
27+
val databasePath: String,
28+
val databaseName: String,
29+
val databaseType: DatabaseType,
30+
val userName: String,
31+
val password: String,
32+
val encryptionKey: String?,
33+
val closeDatabaseInDbProvider: Boolean,
34+
val closeAfterDelayTimeout: Int,
35+
val configBuilder: YouTrackDBConfigBuilder.() -> Unit = {}
36+
) {
37+
38+
companion object {
39+
40+
fun builder(): Builder {
41+
return Builder()
42+
}
43+
}
44+
45+
val youTrackDBConfig: YouTrackDBConfig = YouTrackDBConfig.builder()
46+
.addGlobalConfigurationParameter(GlobalConfiguration.AUTO_CLOSE_AFTER_DELAY, true)
47+
.addGlobalConfigurationParameter(GlobalConfiguration.AUTO_CLOSE_DELAY, closeAfterDelayTimeout)
48+
.addGlobalConfigurationParameter(GlobalConfiguration.NON_TX_READS_WARNING_MODE, "SILENT")
49+
.apply {
50+
encryptionKey?.let { addGlobalConfigurationParameter(GlobalConfiguration.STORAGE_ENCRYPTION_KEY, it) }
51+
}
52+
.apply(configBuilder)
53+
.build()
54+
55+
@Suppress("unused")
56+
class Builder internal constructor() {
57+
58+
private var databasePath: String = ""
59+
private var databaseName: String = ""
60+
private var databaseType: DatabaseType = DatabaseType.MEMORY
61+
private var userName: String = "admin"
62+
private var password: String = "admin"
63+
private var closeAfterDelayTimeout: Int = 10
64+
private var encryptionKey: String? = null
65+
private var closeDatabaseInDbProvider = true
66+
private var configBuilder: YouTrackDBConfigBuilder.() -> Unit = {}
67+
68+
fun withDatabasePath(databaseUrl: String) = apply {
69+
this.databasePath = databaseUrl
70+
}
71+
72+
fun withDatabaseName(databaseName: String) = apply {
73+
this.databaseName = databaseName
74+
}
75+
76+
fun withDatabaseType(databaseType: DatabaseType) = apply {
77+
this.databaseType = databaseType
78+
}
79+
80+
fun withUserName(userName: String) = apply {
81+
this.userName = userName
82+
}
83+
84+
fun withPassword(password: String) = apply {
85+
this.password = password
86+
}
87+
88+
fun withCloseDatabaseInDbProvider(closeDatabaseInDbProvider: Boolean) = apply {
89+
this.closeDatabaseInDbProvider = closeDatabaseInDbProvider
90+
}
91+
92+
fun withCloseAfterDelayTimeout(closeAfterDelayTimeout: Int) = apply {
93+
this.closeAfterDelayTimeout = closeAfterDelayTimeout
94+
}
95+
96+
fun withEncryptionKey(encryptionKey: ByteArray) = apply {
97+
this.encryptionKey = Base64.getEncoder().encodeToString(encryptionKey)
98+
}
99+
100+
fun withEncryptionKey(encryptionKey: String) = apply {
101+
this.encryptionKey = encryptionKey
102+
}
103+
104+
fun withHexEncryptionKey(key: String, iv: Long) = apply {
105+
require(encryptionKey == null) { "Cipher is already initialized" }
106+
// Truncate the key to 16 bytes (32 hex symbols = 16 bytes) according to the YouTrackDB requirements
107+
val truncatedHex = key.substring(0, min(32, key.length))
108+
// 16 bytes hex + 8 bytes long iv = 24 bytes
109+
val bytes = HexFormat.of().parseHex(truncatedHex) + iv.toByteArray()
110+
withEncryptionKey(bytes)
111+
}
112+
113+
fun withConfigBuilder(tweakConfig: YouTrackDBConfigBuilder.() -> Unit) = apply {
114+
this.configBuilder = tweakConfig
115+
}
116+
117+
fun build(): YTDBDatabaseParams {
118+
return YTDBDatabaseParams(
119+
databasePath,
120+
databaseName,
121+
databaseType,
122+
userName,
123+
password,
124+
encryptionKey,
125+
closeDatabaseInDbProvider,
126+
closeAfterDelayTimeout,
127+
configBuilder
128+
)
129+
}
130+
131+
private fun Long.toByteArray(): ByteArray {
132+
return ByteArray(8) { i ->
133+
(this shr (i * 8) and 0xFF).toByte()
134+
}
135+
}
136+
}
137+
}
138+
139+

0 commit comments

Comments
 (0)