Entity Read
Entities Querying
EntityStorage
provides the ability to query data from WorkspaceModel
.
Basic request
To request all entities of the concrete type use EntityStorage.entities()
.
// Take a copy of the current storage
val currentSnapshot: ImmutableEntityStorage =
WorkspaceModel.getInstance(project).currentSnapshot
// Search for the module with the given name throughout the whole list
val moduleEntity: ModuleEntity? =
currentSnapshot.entities(ModuleEntity::class.java)
.find { it.name == moduleName }
// Extension function shortcut for
// EntityStorage.entities(E::class.java)
currentSnapshot.entities<ModuleEntity>()
Index-Based Queries
Throughout the entity's lifecycle, a number of internal indexes are created and maintained to speed up certain types of searches.
EntitySource
Index
This index stores information about entities' EntitySource
and allows obtaining all entities matching the given predicate.
val currentSnapshot: ImmutableEntityStorage =
WorkspaceModel.getInstance(project).currentSnapshot
// Querying all entities of different types
// but with concrete entitySource
currentSnapshot.entitiesBySource { it is GradleEntitySource }.forEach {
// ...
}
VirtualFileUrl
Index
EntityStorage.getVirtualFileUrlIndex()
provides a way to quickly find entities referring to a particular VirtualFileUrl
.
val currentSnapshot: ImmutableEntityStorage =
WorkspaceModel.getInstance(project).currentSnapshot
// Searching all entities with the given path
currentSnapshot.getVirtualFileUrlIndex()
.findEntitiesByUrl(virtualFileUrl)
SymbolicEntityId
Index
Allows searching for an entity by its SymbolicEntityId
.
val moduleId = ModuleId("moduleName")
val entityStorage: ImmutableEntityStorage =
WorkspaceModel.getInstance(project).currentSnapshot
entityStorage.resolve(moduleId)
Arbitrary Data
Provides a way to associate WorkspaceEntity
with an external data type. The association survives modifications of an entity, and is automatically removed when the entity is deleted. Use MutableEntityStorage.getMutableExternalMapping()
to fill the index and EntityStorage.getExternalMapping()
to access it.
Example
In the following example, data stored in instances of Foo
is associated with ModuleEntity
.
Filling the Index
// Сlass whose instances we want to store in the index
data class Foo(val someData: String)
// Unique identifier of the concrete mapping
val externalMappingKey = ExternalMappingKey.create<Foo>("intellij.foo")
val mutableEntityStorage = WorkspaceModel.getInstance(project)
.currentSnapshot.toBuilder()
// Getting mutable instance of specific mapping
val mutableFooExternalMapping =
mutableEntityStorage.getMutableExternalMapping(externalMappingKey)
val moduleEntity = mutableEntityStorage
.entities(ModuleEntity::class.java).single()
// Associate data with specific `ModuleEntity`
mutableFooExternalMapping.addMapping(moduleEntity, Foo("someData"))
Getting the Data
// Сlass whose instances we want to store in the index
data class Foo(val someData: String)
// Unique identifier of the concrete mapping
val externalMappingKey = ExternalMappingKey.create<Foo>("intellij.foo")
val entityStorage = WorkspaceModel.getInstance(project).currentSnapshot
// Immutable instance of specific mapping
val fooExternalMapping = entityStorage
.getExternalMapping(externalMappingKey)
val moduleEntity = entityStorage
.entities(ModuleEntity::class.java).single()
// Getting the data associated with specific `ModuleEntity`
val foo = fooExternalMapping.getDataByEntity(moduleEntity)
Last modified: 11 September 2024