Skip to content

Commit

Permalink
rename CompilationDatabase -> JCDB
Browse files Browse the repository at this point in the history
more remote tests
  • Loading branch information
lehvolk committed Jul 21, 2022
1 parent e2eca1d commit 7183ff7
Show file tree
Hide file tree
Showing 36 changed files with 290 additions and 194 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ stored data from different processes simultaneously.
Java Compilation Database is ignited by number of jars or directories with build classes:

```kotlin
interface CompilationDatabase {
interface JCDB {

suspend fun classpathSet(dirOrJars: List<File>): ClasspathSet

Expand Down Expand Up @@ -81,7 +81,7 @@ suspend fun findNormalDistribution(): Any {
val commonsMath32 = File("commons-math3-3.2.jar")
val commonsMath36 = File("commons-math3-3.6.1.jar")
val buildDir = File("my-project/build/classes/java/main")
val database = compilationDatabase {
val database = jcdb {
useProcessJRE()
persistent {
location = "/tmp/compilation-db/${System.currentTimeMillis()}"
Expand Down Expand Up @@ -112,7 +112,7 @@ If classpath is inconsistent you may receive `ClassNotFoundException` in runtime
changes in a background or refresh jars explicitly:

```kotlin
val database = compilationDatabase {
val database = jcdb {
watchFileSystemChanges = true
useProcessJRE()
predefinendJars = listOf(lib1, buildDir)
Expand All @@ -133,7 +133,7 @@ library files will not affect `ClasspathSet` instance structure. `ClasspathSet#c
clean up persistent store in case of some libraries are outdated.

```kotlin
val database = compilationDatabase {
val database = jcdb {
watchFileSystemChanges()
useProcessJRE()
predefinedDirOrJars = listOf(lib1, buildDir)
Expand All @@ -150,7 +150,7 @@ If `ClasspathSet` requested with libraries which are not indexed yet, then they
returned new instance of set.

```kotlin
val database = compilationDatabase {
val database = jcdb {
watchFileSystemChanges()
useProcessJRE()
predefinedDirOrJars = listOf(lib1)
Expand All @@ -160,13 +160,13 @@ returned new instance of set.
val cp = database.classpathSet(buildDir) // database will automatically process buildDir
```

`CompilationDatabase` is thread safe. If someone requested `ClasspathSet` instance during loading jars from different
`JCDB` is thread safe. If someone requested `ClasspathSet` instance during loading jars from different
thread then `ClasspathSet` will be created on a consistent state of loaded jars. That means that jar can't appear in
`ClasspathSet` in partly loaded state. Apart from that there is no guarantee that all submitted for loading jars will be
loaded.

```kotlin
val database = compilationDatabase {
val database = jcdb {
persistent()
}

Expand Down
26 changes: 17 additions & 9 deletions api/src/main/kotlin/org/utbot/jcdb/api/Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.objectweb.asm.tree.MethodNode
import java.io.Closeable
import java.io.File
import java.io.InputStream
import java.io.Serializable
import java.net.URL

/**
Expand Down Expand Up @@ -237,7 +238,7 @@ interface ClasspathSet : Closeable {
val locations: List<ByteCodeLocation>

/** reference to database */
val db: CompilationDatabase
val db: JCDB

/**
* in case if some locations are outdated it's required to call this method which will create new instance
Expand Down Expand Up @@ -274,7 +275,7 @@ interface ClasspathSet : Closeable {
* @param key index key
* @param term term for index
*/
suspend fun <T> query(key: String, term: String): List<T>
suspend fun <T: Serializable> query(key: String, term: String): List<T>

/**
* query index by specified term
Expand All @@ -283,7 +284,7 @@ interface ClasspathSet : Closeable {
* @param location to limit location
* @param term term for index
*/
suspend fun <T> query(key: String, location: ByteCodeLocation, term: String): List<T>
suspend fun <T: Serializable> query(key: String, location: ByteCodeLocation, term: String): List<T>

}

Expand All @@ -302,11 +303,11 @@ enum class FieldUsageMode {


/**
* Compilation database for
* Compilation database
*
* `close` method should be called when database is not needed anymore
*/
interface CompilationDatabase : Closeable {
interface JCDB : Closeable {

val globalIdStore: GlobalIdsStore

Expand All @@ -323,21 +324,21 @@ interface CompilationDatabase : Closeable {
* @param dirOrJar build folder or jar file
* @return current database instance
*/
suspend fun load(dirOrJar: File): CompilationDatabase
suspend fun load(dirOrJar: File): JCDB

/**
* process and index byte-code resources
* @param dirOrJars list of build folder or jar file
* @return current database instance
*/
suspend fun load(dirOrJars: List<File>): CompilationDatabase
suspend fun load(dirOrJars: List<File>): JCDB

/**
* load locations
* @param locations locations to load
* @return current database instance
*/
suspend fun loadLocations(locations: List<ByteCodeLocation>): CompilationDatabase
suspend fun loadLocations(locations: List<ByteCodeLocation>): JCDB

/**
* explicitly refreshes the state of resources from file-system.
Expand All @@ -354,7 +355,7 @@ interface CompilationDatabase : Closeable {
*
* @return current database instance
*/
fun watchFileSystemChanges(): CompilationDatabase
fun watchFileSystemChanges(): JCDB

/**
* await background jobs
Expand All @@ -366,6 +367,13 @@ interface CompilationDatabase : Closeable {
* database store for storing indexes mappings between ids -> name
*/
interface GlobalIdsStore {
/**
* get unique id for this string
*/
suspend fun getId(name: String): Int

/**
* get name based on id
*/
suspend fun getName(id: Int): String?
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ package org.utbot.jcdb

import org.utbot.jcdb.api.Feature
import org.utbot.jcdb.api.Hook
import org.utbot.jcdb.impl.CompilationDatabaseImpl
import org.utbot.jcdb.impl.JCDBImpl
import org.utbot.jcdb.impl.index.Hierarchy
import java.io.File

/**
* Settings for database
*/
class CompilationDatabaseSettings {
class JCDBSettings {
/** watch file system changes setting */
var watchFileSystemChanges: CompilationDatabaseWatchFileSystemSettings? = null
var watchFileSystemChanges: JCDBWatchFileSystemSettings? = null

/** persisted */
var persistentSettings: CompilationDatabasePersistentSettings? = null
var persistentSettings: JCDBPersistentSettings? = null

/** jar files which should be loaded right after database is created */
var predefinedDirOrJars: List<File> = emptyList()

var hooks: MutableList<(CompilationDatabaseImpl) -> Hook> = arrayListOf()
var hooks: MutableList<(JCDBImpl) -> Hook> = arrayListOf()

/** mandatory setting for java location */
lateinit var jre: File
Expand All @@ -30,17 +30,17 @@ class CompilationDatabaseSettings {
val fullFeatures get() = listOf(Hierarchy) + features

/** builder for persistent settings */
fun persistent(settings: (CompilationDatabasePersistentSettings.() -> Unit) = {}) {
persistentSettings = CompilationDatabasePersistentSettings().also(settings)
fun persistent(settings: (JCDBPersistentSettings.() -> Unit) = {}) {
persistentSettings = JCDBPersistentSettings().also(settings)
}

/** builder for watching file system changes */
fun watchFileSystem(settings: (CompilationDatabaseWatchFileSystemSettings.() -> Unit) = {}) {
watchFileSystemChanges = CompilationDatabaseWatchFileSystemSettings().also(settings)
fun watchFileSystem(settings: (JCDBWatchFileSystemSettings.() -> Unit) = {}) {
watchFileSystemChanges = JCDBWatchFileSystemSettings().also(settings)
}

/** builder for hooks */
fun withHook(hook: (CompilationDatabaseImpl) -> Hook) {
fun withHook(hook: (JCDBImpl) -> Hook) {
hooks += hook
}

Expand Down Expand Up @@ -77,7 +77,7 @@ class CompilationDatabaseSettings {
}


class CompilationDatabasePersistentSettings {
class JCDBPersistentSettings {
/** location folder for persisting data */
var location: String? = null

Expand All @@ -88,7 +88,7 @@ class CompilationDatabasePersistentSettings {
var clearOnStart: Boolean = false
}

class CompilationDatabaseWatchFileSystemSettings {
class JCDBWatchFileSystemSettings {
/** delay between looking up for new changes */
var delay: Long? = 10_000 // 10 seconds
}
7 changes: 4 additions & 3 deletions core/src/main/kotlin/org/utbot/jcdb/impl/ClasspathSetImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import org.utbot.jcdb.impl.tree.ClassTree
import org.utbot.jcdb.impl.tree.ClasspathClassTree
import org.utbot.jcdb.impl.types.ArrayClassIdImpl
import org.utbot.jcdb.impl.types.PredefinedPrimitives
import java.io.Serializable

class ClasspathSetImpl(
private val locationsRegistrySnapshot: LocationsRegistrySnapshot,
private val featuresRegistry: FeaturesRegistry,
override val db: CompilationDatabaseImpl,
override val db: JCDBImpl,
classTree: ClassTree
) : ClasspathSet {

Expand Down Expand Up @@ -51,12 +52,12 @@ class ClasspathSetImpl(
return hierarchyExt.findSubClasses(classId, allHierarchy)
}

override suspend fun <T> query(key: String, term: String): List<T> {
override suspend fun <T: Serializable> query(key: String, term: String): List<T> {
db.awaitBackgroundJobs()
return locations.flatMap { featuresRegistry.findIndex<T>(key, it)?.query(term).orEmpty() }
}

override suspend fun <T> query(key: String, location: ByteCodeLocation, term: String): List<T> {
override suspend fun <T: Serializable> query(key: String, location: ByteCodeLocation, term: String): List<T> {
db.awaitBackgroundJobs()
return featuresRegistry.findIndex<T>(key, location)?.query(term).orEmpty().toList()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package org.utbot.jcdb.impl

import kotlinx.coroutines.*
import mu.KLogging
import org.utbot.jcdb.CompilationDatabaseSettings
import org.utbot.jcdb.JCDBSettings
import org.utbot.jcdb.api.*
import org.utbot.jcdb.impl.fs.*
import org.utbot.jcdb.impl.index.GlobalIds
import org.utbot.jcdb.impl.index.InMemeoryGlobalIdsStore
import org.utbot.jcdb.impl.storage.PersistentEnvironment
import org.utbot.jcdb.impl.storage.scheme.LocationEntity
import org.utbot.jcdb.impl.tree.ClassTree
Expand All @@ -17,11 +17,11 @@ import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger

class CompilationDatabaseImpl(
class JCDBImpl(
private val persistentEnvironment: PersistentEnvironment? = null,
private val settings: CompilationDatabaseSettings,
override val globalIdStore: GlobalIds = GlobalIds()
) : CompilationDatabase {
private val settings: JCDBSettings,
override val globalIdStore: InMemeoryGlobalIdsStore = InMemeoryGlobalIdsStore()
) : JCDB {

companion object : KLogging()

Expand Down Expand Up @@ -102,7 +102,7 @@ class CompilationDatabaseImpl(
}
}
}.awaitAll().filterNotNull()
persistentEnvironment?.databaseStore?.save(this@CompilationDatabaseImpl, false)
persistentEnvironment?.databaseStore?.save(this@JCDBImpl, false)

val locationClasses = libraryTrees.map {
it.location to it.pushInto(classTree).values
Expand Down Expand Up @@ -138,7 +138,7 @@ class CompilationDatabaseImpl(
classTree.visit(RemoveLocationsVisitor(outdatedLocations))
}

override fun watchFileSystemChanges(): CompilationDatabase {
override fun watchFileSystemChanges(): JCDB {
val delay = settings.watchFileSystemChanges?.delay
if (delay != null) { // just paranoid check
BackgroundScope.launch {
Expand Down Expand Up @@ -221,5 +221,4 @@ class CompilationDatabaseImpl(
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.utbot.jcdb.api.ByteCodeLocation
import java.io.Closeable

/**
* registry of locations for CompilationDatabase
* registry of locations for JCDB
*/
class LocationsRegistry(private val featuresRegistry: FeaturesRegistry): Closeable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.utbot.jcdb.api.*
import org.utbot.jcdb.api.ext.HierarchyExtension
import org.utbot.jcdb.impl.fs.relevantLocations

class HierarchyExtensionImpl(private val db: CompilationDatabase, private val cp: ClasspathSet) : HierarchyExtension {
class HierarchyExtensionImpl(private val db: JCDB, private val cp: ClasspathSet) : HierarchyExtension {

override suspend fun findSubClasses(name: String, allHierarchy: Boolean): List<ClassId> {
val classId = cp.findClassOrNull(name) ?: return emptyList()
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/kotlin/org/utbot/jcdb/impl/index/Indexes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ suspend fun index(node: ClassNode, builder: ByteCodeLocationIndexBuilder<*, *>)
}


class GlobalIds : GlobalIdsStore {
class InMemeoryGlobalIdsStore : GlobalIdsStore {

private val counter = AtomicInteger()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.objectweb.asm.tree.MethodInsnNode
import org.utbot.jcdb.api.*
import org.utbot.jcdb.impl.fs.relevantLocations

class ReversedUsagesExtension(private val db: CompilationDatabase, private val cp: ClasspathSet) {
class ReversedUsagesExtension(private val db: JCDB, private val cp: ClasspathSet) {

/**
* find all methods that directly modifies field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.utbot.jcdb.impl.storage.scheme
import jetbrains.exodus.entitystore.Entity
import jetbrains.exodus.entitystore.StoreTransaction
import mu.KLogging
import org.utbot.jcdb.impl.CompilationDatabaseImpl
import org.utbot.jcdb.impl.JCDBImpl
import org.utbot.jcdb.impl.storage.PersistentEnvironment

class DatabaseEntity(internal val entity: Entity) {
Expand Down Expand Up @@ -34,15 +34,15 @@ class DatabaseEntity(internal val entity: Entity) {
class DatabaseStore(private val dbStore: PersistentEnvironment) {

companion object : KLogging() {
const val type = "CompilationDatabase"
const val type = "JCDB"
}

fun get(tx: StoreTransaction): DatabaseEntity {
val entity = tx.getAll(type).first ?: tx.newEntity(type)
return DatabaseEntity(entity)
}

fun save(db: CompilationDatabaseImpl, clearOnStart: Boolean): DatabaseEntity {
fun save(db: JCDBImpl, clearOnStart: Boolean): DatabaseEntity {
return dbStore.transactional {
var existed = getAll(type).first
if (clearOnStart && existed != null) {
Expand Down
Loading

0 comments on commit 7183ff7

Please sign in to comment.