Skip to content

Commit 22c0112

Browse files
committed
Wrap Database in DatabaseService
1 parent 0fb98aa commit 22c0112

File tree

7 files changed

+46
-24
lines changed

7 files changed

+46
-24
lines changed

server/src/main/kotlin/org/javacs/kt/CompilerClassPath.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package org.javacs.kt
33
import org.javacs.kt.classpath.ClassPathEntry
44
import org.javacs.kt.classpath.defaultClassPathResolver
55
import org.javacs.kt.compiler.Compiler
6+
import org.javacs.kt.database.DatabaseService
67
import org.javacs.kt.util.AsyncExecutor
7-
import org.jetbrains.exposed.sql.Database
88
import java.io.Closeable
99
import java.io.File
1010
import java.nio.file.FileSystems
@@ -15,13 +15,12 @@ import java.nio.file.Path
1515
* Manages the class path (compiled JARs, etc), the Java source path
1616
* and the compiler. Note that Kotlin sources are stored in SourcePath.
1717
*/
18-
class CompilerClassPath(private val config: CompilerConfiguration) : Closeable {
18+
class CompilerClassPath(private val config: CompilerConfiguration, private val databaseService: DatabaseService) : Closeable {
1919
val workspaceRoots = mutableSetOf<Path>()
2020

2121
private val javaSourcePath = mutableSetOf<Path>()
2222
private val buildScriptClassPath = mutableSetOf<Path>()
2323
val classPath = mutableSetOf<ClassPathEntry>()
24-
var db: Database? = null
2524
val outputDirectory: File = Files.createTempDirectory("klsBuildOutput").toFile()
2625
val javaHome: String? = System.getProperty("java.home", null)
2726

@@ -41,7 +40,7 @@ class CompilerClassPath(private val config: CompilerConfiguration) : Closeable {
4140
updateJavaSourcePath: Boolean = true
4241
): Boolean {
4342
// TODO: Fetch class path and build script class path concurrently (and asynchronously)
44-
val resolver = defaultClassPathResolver(workspaceRoots, db)
43+
val resolver = defaultClassPathResolver(workspaceRoots, databaseService.db)
4544
var refreshCompiler = updateJavaSourcePath
4645

4746
if (updateClassPath) {

server/src/main/kotlin/org/javacs/kt/Configuration.kt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import com.google.gson.JsonDeserializer
66
import com.google.gson.JsonElement
77
import com.google.gson.JsonParseException
88
import org.eclipse.lsp4j.InitializeParams
9-
import org.jetbrains.exposed.sql.Database
109
import java.lang.reflect.Type
11-
import java.nio.file.Files
1210
import java.nio.file.InvalidPathException
1311
import java.nio.file.Path
1412
import java.nio.file.Paths
@@ -49,18 +47,12 @@ public data class ExternalSourcesConfiguration(
4947
)
5048

5149

52-
fun setupServerDatabase(params: InitializeParams): Database? {
53-
val dbName = "kls_database"
54-
50+
fun getStoragePath(params: InitializeParams): Path? {
5551
params.initializationOptions?.let { initializationOptions ->
5652
val gson = GsonBuilder().registerTypeHierarchyAdapter(Path::class.java, GsonPathConverter()).create()
5753
val options = gson.fromJson(initializationOptions as JsonElement, InitializationOptions::class.java)
5854

59-
options.storagePath?.let { storagePath ->
60-
if (Files.isDirectory(storagePath)) {
61-
return Database.connect("jdbc:sqlite:${Path.of(storagePath.toString(), dbName)}.db")
62-
}
63-
}
55+
return options.storagePath
6456
}
6557

6658
return null

server/src/main/kotlin/org/javacs/kt/KotlinLanguageServer.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.eclipse.lsp4j.services.LanguageClientAware
88
import org.eclipse.lsp4j.services.LanguageServer
99
import org.eclipse.lsp4j.services.NotebookDocumentService
1010
import org.javacs.kt.command.ALL_COMMANDS
11+
import org.javacs.kt.database.DatabaseService
1112
import org.javacs.kt.progress.LanguageClientProgress
1213
import org.javacs.kt.progress.Progress
1314
import org.javacs.kt.semantictokens.semanticTokensLegend
@@ -23,11 +24,12 @@ import java.util.concurrent.CompletableFuture.completedFuture
2324

2425
class KotlinLanguageServer : LanguageServer, LanguageClientAware, Closeable {
2526
val config = Configuration()
26-
val classPath = CompilerClassPath(config.compiler)
27+
val databaseService = DatabaseService()
28+
val classPath = CompilerClassPath(config.compiler, databaseService)
2729

2830
private val tempDirectory = TemporaryDirectory()
2931
private val uriContentProvider = URIContentProvider(ClassContentProvider(config.externalSources, classPath, tempDirectory, CompositeSourceArchiveProvider(JdkSourceArchiveProvider(classPath), ClassPathSourceArchiveProvider(classPath))))
30-
val sourcePath = SourcePath(classPath, uriContentProvider, config.indexing)
32+
val sourcePath = SourcePath(classPath, uriContentProvider, config.indexing, databaseService)
3133
val sourceFiles = SourceFiles(sourcePath, uriContentProvider)
3234

3335
private val textDocuments = KotlinTextDocumentService(sourceFiles, sourcePath, config, tempDirectory, uriContentProvider, classPath)
@@ -90,10 +92,8 @@ class KotlinLanguageServer : LanguageServer, LanguageClientAware, Closeable {
9092
serverCapabilities.executeCommandProvider = ExecuteCommandOptions(ALL_COMMANDS)
9193
serverCapabilities.documentHighlightProvider = Either.forLeft(true)
9294

93-
val db = setupServerDatabase(params)
94-
95-
sourcePath.index = if (db != null) SymbolIndex(db) else SymbolIndex()
96-
classPath.db = db
95+
val storagePath = getStoragePath(params)
96+
databaseService.setup(storagePath)
9797

9898
val clientCapabilities = params.capabilities
9999
config.completion.snippets.enabled = clientCapabilities?.textDocument?.completion?.completionItem?.snippetSupport ?: false

server/src/main/kotlin/org/javacs/kt/SourcePath.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.javacs.kt.util.describeURI
88
import org.javacs.kt.index.SymbolIndex
99
import org.javacs.kt.progress.Progress
1010
import com.intellij.lang.Language
11+
import org.javacs.kt.database.DatabaseService
1112
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
1213
import org.jetbrains.kotlin.psi.KtFile
1314
import org.jetbrains.kotlin.resolve.BindingContext
@@ -22,14 +23,15 @@ import java.util.concurrent.locks.ReentrantLock
2223
class SourcePath(
2324
private val cp: CompilerClassPath,
2425
private val contentProvider: URIContentProvider,
25-
private val indexingConfig: IndexingConfiguration
26+
private val indexingConfig: IndexingConfiguration,
27+
private val databaseService: DatabaseService
2628
) {
2729
private val files = mutableMapOf<URI, SourceFile>()
2830
private val parseDataWriteLock = ReentrantLock()
2931

3032
private val indexAsync = AsyncExecutor()
3133
var indexEnabled: Boolean by indexingConfig::enabled
32-
lateinit var index: SymbolIndex
34+
val index = SymbolIndex(databaseService)
3335

3436
var beforeCompileCallback: () -> Unit = {}
3537

server/src/main/kotlin/org/javacs/kt/index/SymbolIndex.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
77
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
88
import org.jetbrains.kotlin.name.FqName
99
import org.javacs.kt.LOG
10+
import org.javacs.kt.database.DatabaseService
1011
import org.javacs.kt.progress.Progress
1112
import org.jetbrains.exposed.dao.IntEntity
1213
import org.jetbrains.exposed.dao.IntEntityClass
@@ -81,8 +82,12 @@ class PositionEntity(id: EntityID<Int>) : IntEntity(id) {
8182
* A global view of all available symbols across all packages.
8283
*/
8384
class SymbolIndex(
84-
private val db: Database = Database.connect("jdbc:h2:mem:symbolindex;DB_CLOSE_DELAY=-1", "org.h2.Driver")
85+
private val databaseService: DatabaseService
8586
) {
87+
private val db: Database by lazy {
88+
databaseService.db ?: Database.connect("jdbc:h2:mem:symbolindex;DB_CLOSE_DELAY=-1", "org.h2.Driver")
89+
}
90+
8691
var progressFactory: Progress.Factory = Progress.Factory.None
8792

8893
init {

server/src/test/kotlin/org/javacs/kt/CompiledFileTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.javacs.kt
22

33
import org.hamcrest.Matchers.equalTo
44
import org.javacs.kt.compiler.Compiler
5+
import org.javacs.kt.database.DatabaseService
56
import org.junit.AfterClass
67
import org.junit.Assert.assertThat
78
import org.junit.Test
@@ -29,7 +30,7 @@ class CompiledFileTest {
2930
val file = testResourcesRoot().resolve("compiledFile/CompiledFileExample.kt")
3031
val content = Files.readAllLines(file).joinToString("\n")
3132
val parse = compiler.createKtFile(content, file)
32-
val classPath = CompilerClassPath(CompilerConfiguration())
33+
val classPath = CompilerClassPath(CompilerConfiguration(), DatabaseService())
3334
val sourcePath = listOf(parse)
3435
val (context, container) = compiler.compileKtFiles(sourcePath, sourcePath)
3536
CompiledFile(content, parse, context, container, sourcePath, classPath)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.javacs.kt.database
2+
3+
import org.jetbrains.exposed.sql.Database
4+
import java.nio.file.Files
5+
import java.nio.file.Path
6+
7+
class DatabaseService {
8+
9+
var db: Database? = null
10+
private set
11+
12+
fun setup(storagePath: Path?) {
13+
val dbName = "kls_database"
14+
15+
db = storagePath?.let {
16+
if (Files.isDirectory(it)) {
17+
Database.connect("jdbc:sqlite:${Path.of(storagePath.toString(), dbName)}.db")
18+
} else {
19+
null
20+
}
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)