use super::*;
use crate::{
DefinedProjectionStagingSession, ProjectionConsumerCatchUpConfig,
ProjectionConsumerCatchUpError, ProjectionConsumerStagingSession,
ProjectionCoveredConsumerUpdate, ProjectionMappingFunction, ProjectionPreparedConsumerUpdate,
ProjectionSourceDurability, ProjectionSourceScope, SearchProjectionConsumer,
SearchProjectionConsumerCatchUpReport, SearchProjectionConsumerId,
SearchProjectionConsumerOptions, SearchProjectionConsumerReadiness,
SearchProjectionConsumerResult, SearchProjectionConsumerStatus, SearchProjectionDefinition,
};
type CatchUpResult<T> = std::result::Result<T, ProjectionConsumerCatchUpError>;
pub struct NowledgeMemSearchProjectionConsumer {
consumer: SearchProjectionConsumer,
owner: NowledgeMemEmbeddedStoreHandle,
}
pub struct NowledgeMemDefinedSearchProjectionConsumer {
consumer: NowledgeMemSearchProjectionConsumer,
definition: SearchProjectionDefinition,
}
#[must_use]
pub struct NowledgeMemPreparedConsumerUpdate<'consumer> {
prepared: ProjectionPreparedConsumerUpdate<'consumer>,
owner: NowledgeMemEmbeddedStoreHandle,
}
impl NowledgeMemEmbeddedStoreHandle {
pub fn open_or_create_defined_search_projection_consumer(
&self,
definition: SearchProjectionDefinition,
projection_directory: impl AsRef<Path>,
options: SearchProjectionConsumerOptions,
) -> SearchProjectionConsumerResult<NowledgeMemDefinedSearchProjectionConsumer> {
let consumer = self.open_or_create_search_projection_consumer(
definition.consumer_id().clone(),
projection_directory,
options,
|source, index| definition.initialize(source, index),
)?;
Ok(NowledgeMemDefinedSearchProjectionConsumer {
consumer,
definition,
})
}
pub fn unregister_search_projection_consumer(
&self,
id: &SearchProjectionConsumerId,
) -> SearchProjectionConsumerResult<()> {
self.write_store()?
.graph_mut()
.database_mut()
.unregister_search_projection_consumer(id)
}
pub fn open_or_create_search_projection_consumer<F>(
&self,
id: SearchProjectionConsumerId,
projection_directory: impl AsRef<Path>,
options: SearchProjectionConsumerOptions,
initialize: F,
) -> SearchProjectionConsumerResult<NowledgeMemSearchProjectionConsumer>
where
F: FnOnce(&mut DatabaseReadTransaction, &mut SearchIndex) -> Result<()>,
{
let consumer = self
.write_store()?
.graph_mut()
.database_mut()
.open_or_create_search_projection_consumer(
id,
projection_directory,
options,
initialize,
)?;
Ok(NowledgeMemSearchProjectionConsumer {
consumer,
owner: self.clone(),
})
}
}
impl NowledgeMemDefinedSearchProjectionConsumer {
pub fn definition(&self) -> &SearchProjectionDefinition {
&self.definition
}
pub fn consumer(&self) -> &SearchProjectionConsumer {
self.consumer.consumer()
}
pub fn read_permit(&self, request: RuntimeWorkRequest) -> Result<RuntimePermit> {
self.consumer.read_permit(request)
}
pub fn readiness(
&self,
max_operations: Option<usize>,
) -> SearchProjectionConsumerResult<SearchProjectionConsumerReadiness> {
self.consumer.readiness(max_operations)
}
pub fn begin_catch_up(
&self,
config: ProjectionConsumerCatchUpConfig,
) -> CatchUpResult<DefinedProjectionStagingSession<'_>> {
self.consumer
.owner
.write_store()?
.graph_mut()
.database_mut()
.begin_defined_search_projection_consumer_catch_up(
&self.consumer.consumer,
&self.definition,
config,
)
}
pub fn prepare_covered_update(
&mut self,
covered: ProjectionCoveredConsumerUpdate,
durability: ProjectionSourceDurability,
) -> CatchUpResult<NowledgeMemPreparedConsumerUpdate<'_>> {
self.consumer.prepare_covered_update(covered, durability)
}
pub fn resolve_publication(&mut self) -> SearchProjectionConsumerResult<()> {
self.consumer.resolve_publication()
}
pub fn renew(&mut self) -> SearchProjectionConsumerResult<SearchProjectionConsumerStatus> {
self.consumer.renew()
}
pub fn cleanup(
&mut self,
options: &crate::search::consumer::ConsumerCleanupOptions,
) -> SearchProjectionConsumerResult<crate::search::consumer::ConsumerCleanupReport> {
self.consumer.cleanup(options)
}
}
impl NowledgeMemSearchProjectionConsumer {
pub fn consumer(&self) -> &SearchProjectionConsumer {
&self.consumer
}
pub fn read_permit(&self, request: RuntimeWorkRequest) -> Result<RuntimePermit> {
self.owner
.write_store()?
.graph_mut()
.database_mut()
.search_projection_consumer_read_permit(request)
}
pub fn readiness(
&self,
max_operations: Option<usize>,
) -> SearchProjectionConsumerResult<SearchProjectionConsumerReadiness> {
self.owner
.read_store()?
.graph()
.database()
.search_projection_consumer_readiness(&self.consumer, max_operations)
}
pub fn begin_catch_up(
&self,
config: ProjectionConsumerCatchUpConfig,
mapper: ProjectionMappingFunction,
scope: ProjectionSourceScope,
) -> CatchUpResult<ProjectionConsumerStagingSession<'_>> {
self.owner
.write_store()?
.graph_mut()
.database_mut()
.begin_search_projection_consumer_catch_up_in(&self.consumer, config, mapper, scope)
}
pub fn prepare_covered_update(
&mut self,
covered: ProjectionCoveredConsumerUpdate,
durability: ProjectionSourceDurability,
) -> CatchUpResult<NowledgeMemPreparedConsumerUpdate<'_>> {
let reserved = {
let mut store = self.owner.write_store()?;
store
.graph_mut()
.database_mut()
.prepare_search_projection_consumer_covered_update(
&mut self.consumer,
covered,
durability,
)?
};
Ok(NowledgeMemPreparedConsumerUpdate {
prepared: reserved.prepare(),
owner: self.owner.clone(),
})
}
pub fn resolve_publication(&mut self) -> SearchProjectionConsumerResult<()> {
self.owner
.write_store()?
.graph_mut()
.database_mut()
.resolve_search_projection_consumer_publication(&mut self.consumer)
}
pub fn renew(&mut self) -> SearchProjectionConsumerResult<SearchProjectionConsumerStatus> {
self.owner
.write_store()?
.graph_mut()
.database_mut()
.renew_search_projection_consumer(&self.consumer)
}
pub fn cleanup(
&mut self,
options: &crate::search::consumer::ConsumerCleanupOptions,
) -> SearchProjectionConsumerResult<crate::search::consumer::ConsumerCleanupReport> {
self.owner
.write_store()?
.graph_mut()
.database_mut()
.cleanup_search_projection_consumer(&self.consumer, options)
}
}
impl NowledgeMemPreparedConsumerUpdate<'_> {
pub fn source_epoch(&self) -> u64 {
self.prepared.source_epoch()
}
pub fn publish(self) -> CatchUpResult<SearchProjectionConsumerCatchUpReport> {
let published = {
let mut store = self.owner.write_store()?;
store
.graph_mut()
.database_mut()
.publish_search_projection_consumer_prepared_update(self.prepared)?
};
Ok(published.into_report())
}
}