Skip to content

Commit 42829cb

Browse files
committed
Store symbol index in in-memory H2 database (WIP)
1 parent 87d7fa2 commit 42829cb

File tree

3 files changed

+81
-15
lines changed

3 files changed

+81
-15
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.javacs.kt.index
2+
3+
import org.jetbrains.kotlin.descriptors.*
4+
5+
object ExtractSymbolKind : DeclarationDescriptorVisitor<Symbol.Kind, Unit> {
6+
override fun visitPropertySetterDescriptor(desc: PropertySetterDescriptor, nothing: Unit?) = Symbol.Kind.FIELD
7+
8+
override fun visitConstructorDescriptor(desc: ConstructorDescriptor, nothing: Unit?) = Symbol.Kind.CONSTRUCTOR
9+
10+
override fun visitReceiverParameterDescriptor(desc: ReceiverParameterDescriptor, nothing: Unit?) = Symbol.Kind.VARIABLE
11+
12+
override fun visitPackageViewDescriptor(desc: PackageViewDescriptor, nothing: Unit?) = Symbol.Kind.MODULE
13+
14+
override fun visitFunctionDescriptor(desc: FunctionDescriptor, nothing: Unit?) = Symbol.Kind.FUNCTION
15+
16+
override fun visitModuleDeclaration(desc: ModuleDescriptor, nothing: Unit?) = Symbol.Kind.MODULE
17+
18+
override fun visitClassDescriptor(desc: ClassDescriptor, nothing: Unit?): Symbol.Kind = when (desc.kind) {
19+
ClassKind.INTERFACE -> Symbol.Kind.INTERFACE
20+
ClassKind.ENUM_CLASS -> Symbol.Kind.ENUM
21+
ClassKind.ENUM_ENTRY -> Symbol.Kind.ENUM_MEMBER
22+
else -> Symbol.Kind.CLASS
23+
}
24+
25+
override fun visitPackageFragmentDescriptor(desc: PackageFragmentDescriptor, nothing: Unit?) = Symbol.Kind.MODULE
26+
27+
override fun visitValueParameterDescriptor(desc: ValueParameterDescriptor, nothing: Unit?) = Symbol.Kind.VARIABLE
28+
29+
override fun visitTypeParameterDescriptor(desc: TypeParameterDescriptor, nothing: Unit?) = Symbol.Kind.VARIABLE
30+
31+
override fun visitScriptDescriptor(desc: ScriptDescriptor, nothing: Unit?) = Symbol.Kind.MODULE
32+
33+
override fun visitTypeAliasDescriptor(desc: TypeAliasDescriptor, nothing: Unit?) = Symbol.Kind.VARIABLE
34+
35+
override fun visitPropertyGetterDescriptor(desc: PropertyGetterDescriptor, nothing: Unit?) = Symbol.Kind.VARIABLE
36+
37+
override fun visitVariableDescriptor(desc: VariableDescriptor, nothing: Unit?) = Symbol.Kind.VARIABLE
38+
39+
override fun visitPropertyDescriptor(desc: PropertyDescriptor, nothing: Unit?) = Symbol.Kind.VARIABLE
40+
}

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,19 @@ data class Symbol(
77
private val fqName: FqName,
88
private val kind: Kind
99
) {
10-
enum class Kind {
11-
CLASS,
12-
INTERFACE,
13-
FUNCTION,
14-
VARIABLE
10+
enum class Kind(val rawValue: Int) {
11+
CLASS(0),
12+
INTERFACE(1),
13+
FUNCTION(2),
14+
VARIABLE(3),
15+
MODULE(4),
16+
ENUM(5),
17+
ENUM_MEMBER(6),
18+
CONSTRUCTOR(7),
19+
FIELD(8);
20+
21+
companion object {
22+
fun fromRaw(rawValue: Int) = Kind.values().first { it.rawValue == rawValue }
23+
}
1524
}
1625
}

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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,51 @@
11
package org.javacs.kt.index
22

3+
import org.jetbrains.exposed.sql.*
4+
import org.jetbrains.exposed.sql.transactions.transaction
35
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
46
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
57
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
68
import org.jetbrains.kotlin.resolve.scopes.MemberScope
79
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
810
import org.jetbrains.kotlin.name.FqName
9-
import org.javacs.kt.compiler.Compiler
1011
import org.javacs.kt.LOG
11-
import java.util.concurrent.ConcurrentHashMap
12-
import java.util.concurrent.locks.ReentrantLock
13-
import kotlin.concurrent.withLock
12+
import org.jetbrains.exposed.sql.Database
13+
import org.jetbrains.exposed.sql.insert
14+
15+
private object Symbols : Table() {
16+
val fqName = varchar("fqname", length = 255).autoIncrement().primaryKey()
17+
val kind = integer("kind")
18+
}
1419

1520
/**
1621
* A global view of all available symbols across all packages.
1722
*/
1823
class SymbolIndex {
19-
val globalDescriptors = ConcurrentHashMap<FqName, DeclarationDescriptor>()
24+
private val db = Database.connect("jdbc:h2:mem:symbolindex", "org.h2.Driver")
25+
26+
init {
27+
transaction(db) {
28+
SchemaUtils.create(Symbols)
29+
}
30+
}
2031

2132
fun update(module: ModuleDescriptor) {
2233
val started = System.currentTimeMillis()
2334
LOG.info("Updating symbol index...")
2435

2536
try {
26-
for (descriptor in allDescriptors(module)) {
27-
globalDescriptors[descriptor.fqNameSafe] = descriptor
28-
}
37+
transaction(db) {
38+
for (descriptor in allDescriptors(module)) {
39+
Symbols.insert {
40+
it[fqName] = descriptor.fqNameSafe.toString()
41+
it[kind] = descriptor.accept(ExtractSymbolKind, Unit).rawValue
42+
}
43+
}
2944

30-
val finished = System.currentTimeMillis()
31-
LOG.info("Updated symbol index in ${finished - started} ms! (${globalDescriptors.size} symbol(s))")
45+
val finished = System.currentTimeMillis()
46+
val count = Symbols.slice(Symbols.fqName.count()).selectAll().first()[Symbols.fqName.count()]
47+
LOG.info("Updated symbol index in ${finished - started} ms! (${count} symbol(s))")
48+
}
3249
} catch (e: Exception) {
3350
LOG.error("Error while updating symbol index")
3451
LOG.printStackTrace(e)

0 commit comments

Comments
 (0)