|
1 | 1 | package org.javacs.kt.index
|
2 | 2 |
|
| 3 | +import org.jetbrains.exposed.sql.* |
| 4 | +import org.jetbrains.exposed.sql.transactions.transaction |
3 | 5 | import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
4 | 6 | import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
5 | 7 | import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
|
6 | 8 | import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
7 | 9 | import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
8 | 10 | import org.jetbrains.kotlin.name.FqName
|
9 |
| -import org.javacs.kt.compiler.Compiler |
10 | 11 | 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 | +} |
14 | 19 |
|
15 | 20 | /**
|
16 | 21 | * A global view of all available symbols across all packages.
|
17 | 22 | */
|
18 | 23 | 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 | + } |
20 | 31 |
|
21 | 32 | fun update(module: ModuleDescriptor) {
|
22 | 33 | val started = System.currentTimeMillis()
|
23 | 34 | LOG.info("Updating symbol index...")
|
24 | 35 |
|
25 | 36 | 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 | + } |
29 | 44 |
|
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 | + } |
32 | 49 | } catch (e: Exception) {
|
33 | 50 | LOG.error("Error while updating symbol index")
|
34 | 51 | LOG.printStackTrace(e)
|
|
0 commit comments