Skip to content

Commit f9b231b

Browse files
committed
- some documentation
- move globalIds from static to database property - expose globalIds to remote api relates to #21
1 parent 9162e1e commit f9b231b

File tree

19 files changed

+244
-127
lines changed

19 files changed

+244
-127
lines changed

core/src/main/kotlin/org/utbot/jcdb/api/Api.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ enum class FieldUsageMode {
311311
*/
312312
interface CompilationDatabase : Closeable {
313313

314+
val globalIdStore: GlobalIdsStore
315+
314316
/**
315317
* create classpath instance
316318
*
@@ -361,4 +363,12 @@ interface CompilationDatabase : Closeable {
361363
* await background jobs
362364
*/
363365
suspend fun awaitBackgroundJobs()
364-
}
366+
}
367+
368+
/**
369+
* database store for storing indexes mappings between ids -> name
370+
*/
371+
interface GlobalIdsStore {
372+
suspend fun getId(name: String): Int
373+
suspend fun getName(id: Int): String?
374+
}

core/src/main/kotlin/org/utbot/jcdb/api/Index.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import java.io.OutputStream
1010
*/
1111
interface ByteCodeLocationIndexBuilder<T, INDEX: ByteCodeLocationIndex<T>> {
1212

13-
fun index(classNode: ClassNode)
13+
suspend fun index(classNode: ClassNode)
1414

15-
fun index(classNode: ClassNode, methodNode: MethodNode)
15+
suspend fun index(classNode: ClassNode, methodNode: MethodNode)
1616

1717
fun build(location: ByteCodeLocation): INDEX
1818

@@ -22,17 +22,17 @@ interface ByteCodeLocationIndex<T> {
2222

2323
val location: ByteCodeLocation
2424

25-
fun query(term: String): Sequence<T>
25+
suspend fun query(term: String): Sequence<T>
2626
}
2727

2828
interface Feature<T, INDEX: ByteCodeLocationIndex<T>> {
2929

3030
val key: String
3131

32-
fun newBuilder() : ByteCodeLocationIndexBuilder<T, INDEX>
32+
fun newBuilder(globalIdsStore: GlobalIdsStore) : ByteCodeLocationIndexBuilder<T, INDEX>
3333

3434
fun serialize(index: INDEX, out: OutputStream)
3535

36-
fun deserialize(location: ByteCodeLocation, stream: InputStream): INDEX?
36+
fun deserialize(globalIdsStore: GlobalIdsStore, location: ByteCodeLocation, stream: InputStream): INDEX?
3737

3838
}

core/src/main/kotlin/org/utbot/jcdb/impl/CompilationDatabaseImpl.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import mu.KLogging
55
import org.utbot.jcdb.CompilationDatabaseSettings
66
import org.utbot.jcdb.api.*
77
import org.utbot.jcdb.impl.fs.*
8+
import org.utbot.jcdb.impl.index.GlobalIds
89
import org.utbot.jcdb.impl.storage.PersistentEnvironment
910
import org.utbot.jcdb.impl.storage.scheme.LocationEntity
1011
import org.utbot.jcdb.impl.tree.ClassTree
@@ -18,7 +19,8 @@ import java.util.concurrent.atomic.AtomicInteger
1819

1920
class CompilationDatabaseImpl(
2021
private val persistentEnvironment: PersistentEnvironment? = null,
21-
private val settings: CompilationDatabaseSettings
22+
private val settings: CompilationDatabaseSettings,
23+
override val globalIdStore: GlobalIds = GlobalIds()
2224
) : CompilationDatabase {
2325

2426
companion object : KLogging()
@@ -27,7 +29,7 @@ class CompilationDatabaseImpl(
2729
internal val javaRuntime = JavaRuntime(settings.jre)
2830
private val hooks = settings.hooks.map { it(this) }
2931

30-
internal val featureRegistry = FeaturesRegistry(persistentEnvironment, settings.fullFeatures)
32+
internal val featureRegistry = FeaturesRegistry(persistentEnvironment, globalIdStore, settings.fullFeatures)
3133
internal val locationsRegistry = LocationsRegistry(featureRegistry)
3234
private val backgroundJobs = ConcurrentHashMap<Int, Job>()
3335

@@ -122,7 +124,7 @@ class CompilationDatabaseImpl(
122124
}
123125
}
124126
}.joinAll()
125-
persistentEnvironment?.globalIds?.sync()
127+
persistentEnvironment?.globalIds?.sync(globalIdStore)
126128
backgroundJobs.remove(backgroundJobId)
127129
}
128130
}
@@ -176,7 +178,7 @@ class CompilationDatabaseImpl(
176178

177179
internal suspend fun restoreDataFrom(locations: Map<LocationEntity, ByteCodeLocation>) {
178180
val env = persistentEnvironment ?: return
179-
env.globalIds.restore()
181+
env.globalIds.restore(globalIdStore)
180182
val trees = withContext(Dispatchers.IO) {
181183
locations.map { (entity, location) ->
182184
async {
@@ -206,7 +208,7 @@ class CompilationDatabaseImpl(
206208
val data = entity.index(key)
207209
if (data != null) {
208210
val index = try {
209-
deserialize(location, data)
211+
deserialize(globalIdStore, location, data)
210212
} catch (e: Exception) {
211213
logger.warn(e) { "can't parse location" }
212214
null

core/src/main/kotlin/org/utbot/jcdb/impl/FeaturesRegistry.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.utbot.jcdb.impl
33
import org.utbot.jcdb.api.ByteCodeLocation
44
import org.utbot.jcdb.api.ByteCodeLocationIndex
55
import org.utbot.jcdb.api.Feature
6+
import org.utbot.jcdb.api.GlobalIdsStore
67
import org.utbot.jcdb.impl.index.index
78
import org.utbot.jcdb.impl.storage.PersistentEnvironment
89
import org.utbot.jcdb.impl.tree.ClassNode
@@ -13,6 +14,7 @@ import java.util.concurrent.ConcurrentHashMap
1314

1415
class FeaturesRegistry(
1516
private val persistence: PersistentEnvironment? = null,
17+
val globalIdsStore: GlobalIdsStore,
1618
val features: List<Feature<*, *>>
1719
) : Closeable {
1820
private val indexes: ConcurrentHashMap<ByteCodeLocation, ConcurrentHashMap<String, ByteCodeLocationIndex<*>>> = ConcurrentHashMap()
@@ -38,7 +40,7 @@ class FeaturesRegistry(
3840
location: ByteCodeLocation,
3941
classes: Collection<ClassNode>
4042
) {
41-
val builder = newBuilder()
43+
val builder = newBuilder(globalIdsStore)
4244
classes.forEach { node ->
4345
index(node, builder)
4446
}

core/src/main/kotlin/org/utbot/jcdb/impl/index/HierarchyIndex.kt

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,17 @@ import kotlinx.collections.immutable.toImmutableMap
44
import org.objectweb.asm.Type
55
import org.objectweb.asm.tree.ClassNode
66
import org.objectweb.asm.tree.MethodNode
7-
import org.utbot.jcdb.api.ByteCodeLocation
8-
import org.utbot.jcdb.api.ByteCodeLocationIndex
9-
import org.utbot.jcdb.api.ByteCodeLocationIndexBuilder
10-
import org.utbot.jcdb.api.Feature
7+
import org.utbot.jcdb.api.*
118
import java.io.InputStream
129
import java.io.OutputStream
1310

14-
class HierarchyIndexBuilder : ByteCodeLocationIndexBuilder<String, HierarchyIndex> {
11+
class HierarchyIndexBuilder(private val globalIdsStore: GlobalIdsStore) :
12+
ByteCodeLocationIndexBuilder<String, HierarchyIndex> {
1513

1614
// super class -> implementations
1715
private val parentToSubClasses = hashMapOf<Int, HashSet<Int>>()
1816

19-
override fun index(classNode: ClassNode) {
17+
override suspend fun index(classNode: ClassNode) {
2018
val superClass = classNode.superName?.takeIf {
2119
it != "java/lang/Object"
2220
}
@@ -28,27 +26,27 @@ class HierarchyIndexBuilder : ByteCodeLocationIndexBuilder<String, HierarchyInde
2826
}
2927
}
3028

31-
override fun index(classNode: ClassNode, methodNode: MethodNode) {
29+
override suspend fun index(classNode: ClassNode, methodNode: MethodNode) {
3230
// nothing to do here
3331
}
3432

35-
private fun addToIndex(parentInternalName: String, subClassInternalName: String) {
36-
val parentName = parentInternalName.intId
37-
val subClassName = subClassInternalName.intId
33+
private suspend fun addToIndex(parentInternalName: String, subClassInternalName: String) {
34+
val parentName = parentInternalName.intId()
35+
val subClassName = subClassInternalName.intId()
3836
val subClasses = parentToSubClasses.getOrPut(parentName) {
3937
HashSet()
4038
}
4139
subClasses.add(subClassName)
4240
}
4341

44-
private val String.intId: Int
45-
get() {
46-
val pureName = Type.getObjectType(this).className
47-
return GlobalIds.getId(pureName)
48-
}
42+
private suspend fun String.intId(): Int {
43+
val pureName = Type.getObjectType(this).className
44+
return globalIdsStore.getId(pureName)
45+
}
4946

5047
override fun build(location: ByteCodeLocation): HierarchyIndex {
5148
return HierarchyIndex(
49+
globalIdsStore = globalIdsStore,
5250
location = location,
5351
parentToSubClasses = parentToSubClasses.toImmutableMap()
5452
)
@@ -58,14 +56,15 @@ class HierarchyIndexBuilder : ByteCodeLocationIndexBuilder<String, HierarchyInde
5856

5957

6058
class HierarchyIndex(
59+
private val globalIdsStore: GlobalIdsStore,
6160
override val location: ByteCodeLocation,
6261
internal val parentToSubClasses: Map<Int, Set<Int>>
6362
) : ByteCodeLocationIndex<String> {
6463

65-
override fun query(term: String): Sequence<String> {
66-
val parentClassId = GlobalIds.getId(term)
64+
override suspend fun query(term: String): Sequence<String> {
65+
val parentClassId = globalIdsStore.getId(term)
6766
return parentToSubClasses[parentClassId]?.map {
68-
GlobalIds.getName(it)
67+
globalIdsStore.getName(it)
6968
}?.asSequence()?.filterNotNull() ?: emptySequence()
7069
}
7170

@@ -75,9 +74,13 @@ object Hierarchy : Feature<String, HierarchyIndex> {
7574

7675
override val key = "hierarchy"
7776

78-
override fun newBuilder() = HierarchyIndexBuilder()
77+
override fun newBuilder(globalIdsStore: GlobalIdsStore) = HierarchyIndexBuilder(globalIdsStore)
7978

80-
override fun deserialize(location: ByteCodeLocation, stream: InputStream): HierarchyIndex {
79+
override fun deserialize(
80+
globalIdsStore: GlobalIdsStore,
81+
location: ByteCodeLocation,
82+
stream: InputStream
83+
): HierarchyIndex {
8184
val reader = stream.reader()
8285
val result = hashMapOf<Int, Set<Int>>()
8386
reader.use {
@@ -86,7 +89,7 @@ object Hierarchy : Feature<String, HierarchyIndex> {
8689
result[key] = value
8790
}
8891
}
89-
return HierarchyIndex(location, result)
92+
return HierarchyIndex(globalIdsStore, location, result)
9093
}
9194

9295
override fun serialize(index: HierarchyIndex, out: OutputStream) {

core/src/main/kotlin/org/utbot/jcdb/impl/index/Indexes.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.utbot.jcdb.impl.index
22

33
import org.utbot.jcdb.api.ByteCodeLocationIndexBuilder
4+
import org.utbot.jcdb.api.GlobalIdsStore
45
import org.utbot.jcdb.impl.tree.ClassNode
56
import java.util.concurrent.ConcurrentHashMap
67
import java.util.concurrent.atomic.AtomicInteger
@@ -14,7 +15,7 @@ suspend fun index(node: ClassNode, builder: ByteCodeLocationIndexBuilder<*, *>)
1415
}
1516

1617

17-
object GlobalIds {
18+
class GlobalIds : GlobalIdsStore {
1819

1920
private val counter = AtomicInteger()
2021

@@ -24,7 +25,7 @@ object GlobalIds {
2425
@Volatile
2526
private var locked = false
2627

27-
fun getId(name: String): Int {
28+
override suspend fun getId(name: String): Int {
2829
val id = all.getOrPut(name) {
2930
if (locked) {
3031
throw IllegalStateException("writing is locked")
@@ -35,7 +36,7 @@ object GlobalIds {
3536
return id
3637
}
3738

38-
fun getName(id: Int): String? {
39+
override suspend fun getName(id: Int): String? {
3940
return reversed.get(id)
4041
}
4142

core/src/main/kotlin/org/utbot/jcdb/impl/index/ReversedUsageIndex.kt

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,33 @@ import org.objectweb.asm.tree.ClassNode
88
import org.objectweb.asm.tree.FieldInsnNode
99
import org.objectweb.asm.tree.MethodInsnNode
1010
import org.objectweb.asm.tree.MethodNode
11-
import org.utbot.jcdb.api.ByteCodeLocation
12-
import org.utbot.jcdb.api.ByteCodeLocationIndex
13-
import org.utbot.jcdb.api.ByteCodeLocationIndexBuilder
14-
import org.utbot.jcdb.api.Feature
11+
import org.utbot.jcdb.api.*
1512
import java.io.InputStream
1613
import java.io.OutputStream
1714

1815

19-
class ReversedUsageIndexBuilder : ByteCodeLocationIndexBuilder<String, ReversedUsageIndex> {
16+
class ReversedUsageIndexBuilder(private val globalIdsStore: GlobalIdsStore) : ByteCodeLocationIndexBuilder<String, ReversedUsageIndex> {
2017

2118
// class method -> usages of methods|fields
2219
private val fieldsUsages = hashMapOf<Int, HashSet<Int>>()
2320
private val methodsUsages = hashMapOf<Int, HashSet<Int>>()
2421

25-
override fun index(classNode: ClassNode) {
22+
override suspend fun index(classNode: ClassNode) {
2623
}
2724

28-
override fun index(classNode: ClassNode, methodNode: MethodNode) {
25+
override suspend fun index(classNode: ClassNode, methodNode: MethodNode) {
2926
val pureName = Type.getObjectType(classNode.name).className
30-
val id = GlobalIds.getId(pureName)
27+
val id = globalIdsStore.getId(pureName)
3128
methodNode.instructions.forEach {
3229
when (it) {
3330
is FieldInsnNode -> {
3431
val owner = Type.getObjectType(it.owner).className
35-
val key = GlobalIds.getId(owner + "#" + it.name)
32+
val key = globalIdsStore.getId(owner + "#" + it.name)
3633
fieldsUsages.getOrPut(key) { hashSetOf() }.add(id)
3734
}
3835
is MethodInsnNode -> {
3936
val owner = Type.getObjectType(it.owner).className
40-
val key = GlobalIds.getId(owner + "#" + it.name)
37+
val key = globalIdsStore.getId(owner + "#" + it.name)
4138
methodsUsages.getOrPut(key) { hashSetOf() }.add(id)
4239
}
4340
}
@@ -48,6 +45,7 @@ class ReversedUsageIndexBuilder : ByteCodeLocationIndexBuilder<String, ReversedU
4845
override fun build(location: ByteCodeLocation): ReversedUsageIndex {
4946
return ReversedUsageIndex(
5047
location = location,
48+
globalIdsStore = globalIdsStore,
5149
fieldsUsages = fieldsUsages.toImmutableMap(),
5250
methodsUsages = methodsUsages.toImmutableMap()
5351
)
@@ -58,14 +56,15 @@ class ReversedUsageIndexBuilder : ByteCodeLocationIndexBuilder<String, ReversedU
5856

5957
class ReversedUsageIndex(
6058
override val location: ByteCodeLocation,
59+
internal val globalIdsStore: GlobalIdsStore,
6160
internal val fieldsUsages: ImmutableMap<Int, Set<Int>>,
6261
internal val methodsUsages: ImmutableMap<Int, Set<Int>>,
6362
) : ByteCodeLocationIndex<String> {
6463

65-
override fun query(term: String): Sequence<String> {
66-
val usages = fieldsUsages[GlobalIds.getId(term)].orEmpty() +
67-
methodsUsages[GlobalIds.getId(term)].orEmpty()
68-
return usages.map { GlobalIds.getName(it) }.asSequence().filterNotNull()
64+
override suspend fun query(term: String): Sequence<String> {
65+
val usages = fieldsUsages[globalIdsStore.getId(term)].orEmpty() +
66+
methodsUsages[globalIdsStore.getId(term)].orEmpty()
67+
return usages.map { globalIdsStore.getName(it) }.asSequence().filterNotNull()
6968
}
7069

7170
}
@@ -75,9 +74,9 @@ object ReversedUsages : Feature<String, ReversedUsageIndex> {
7574

7675
override val key = "reversed-usages"
7776

78-
override fun newBuilder() = ReversedUsageIndexBuilder()
77+
override fun newBuilder(globalIdsStore: GlobalIdsStore) = ReversedUsageIndexBuilder(globalIdsStore)
7978

80-
override fun deserialize(location: ByteCodeLocation, stream: InputStream): ReversedUsageIndex {
79+
override fun deserialize(globalIdsStore: GlobalIdsStore, location: ByteCodeLocation, stream: InputStream): ReversedUsageIndex {
8180
val reader = stream.reader()
8281

8382
val fields = hashMapOf<Int, Set<Int>>()
@@ -93,7 +92,7 @@ object ReversedUsages : Feature<String, ReversedUsageIndex> {
9392
}
9493
}
9594
}
96-
return ReversedUsageIndex(location, fields.toPersistentMap(), methods.toPersistentMap())
95+
return ReversedUsageIndex(location, globalIdsStore, fields.toPersistentMap(), methods.toPersistentMap())
9796

9897
}
9998

0 commit comments

Comments
 (0)