Skip to content

Commit 029175b

Browse files
authored
Simplify ResourceSetupStatusCheck. (#286)
1 parent 47513f1 commit 029175b

File tree

8 files changed

+179
-277
lines changed

8 files changed

+179
-277
lines changed

src/execution/db_tracking_setup.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::prelude::*;
22

3-
use crate::setup::{CombinedState, ResourceSetupStatusCheck, SetupChangeType};
3+
use crate::setup::{CombinedState, ResourceSetupInfo, ResourceSetupStatusCheck, SetupChangeType};
44
use serde::{Deserialize, Serialize};
55
use sqlx::PgPool;
66

@@ -79,22 +79,21 @@ impl TrackingTableSetupStatusCheck {
7979
source_ids_to_delete,
8080
}
8181
}
82-
}
8382

84-
#[async_trait]
85-
impl ResourceSetupStatusCheck<(), TrackingTableSetupState> for TrackingTableSetupStatusCheck {
86-
fn describe_resource(&self) -> String {
87-
"Tracking Table".to_string()
88-
}
89-
90-
fn key(&self) -> &() {
91-
&()
92-
}
93-
94-
fn desired_state(&self) -> Option<&TrackingTableSetupState> {
95-
self.desired_state.as_ref()
83+
pub fn into_setup_info(
84+
self,
85+
) -> ResourceSetupInfo<(), TrackingTableSetupState, TrackingTableSetupStatusCheck> {
86+
ResourceSetupInfo {
87+
key: (),
88+
state: self.desired_state.clone(),
89+
description: "Tracking Table".to_string(),
90+
status_check: Some(self),
91+
}
9692
}
93+
}
9794

95+
#[async_trait]
96+
impl ResourceSetupStatusCheck for TrackingTableSetupStatusCheck {
9897
fn describe_changes(&self) -> Vec<String> {
9998
let mut changes: Vec<String> = vec![];
10099
if self.desired_state.is_some() && !self.legacy_table_names.is_empty() {

src/ops/factory_bases.rs

Lines changed: 10 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -296,14 +296,16 @@ pub trait StorageFactoryBase: ExportTargetFactory + Send + Sync + 'static {
296296
desired_state: Option<Self::SetupState>,
297297
existing_states: setup::CombinedState<Self::SetupState>,
298298
auth_registry: &Arc<AuthRegistry>,
299-
) -> Result<impl setup::ResourceSetupStatusCheck<Self::Key, Self::SetupState> + 'static>;
299+
) -> Result<impl setup::ResourceSetupStatusCheck + 'static>;
300300

301301
fn check_state_compatibility(
302302
&self,
303303
desired_state: &Self::SetupState,
304304
existing_state: &Self::SetupState,
305305
) -> Result<SetupStateCompatibility>;
306306

307+
fn describe_resource(&self, key: &Self::Key) -> Result<String>;
308+
307309
fn register(self, registry: &mut ExecutorFactoryRegistry) -> Result<()>
308310
where
309311
Self: Sized,
@@ -315,62 +317,6 @@ pub trait StorageFactoryBase: ExportTargetFactory + Send + Sync + 'static {
315317
}
316318
}
317319

318-
struct ResourceSetupStatusCheckWrapper<T: StorageFactoryBase> {
319-
inner: Box<dyn setup::ResourceSetupStatusCheck<T::Key, T::SetupState> + Send + Sync>,
320-
key_json: serde_json::Value,
321-
state_json: Option<serde_json::Value>,
322-
}
323-
324-
impl<T: StorageFactoryBase> ResourceSetupStatusCheckWrapper<T> {
325-
fn new(
326-
inner: Box<dyn setup::ResourceSetupStatusCheck<T::Key, T::SetupState> + Send + Sync>,
327-
) -> Result<Self> {
328-
Ok(Self {
329-
key_json: serde_json::to_value(inner.key())?,
330-
state_json: inner
331-
.desired_state()
332-
.map(serde_json::to_value)
333-
.transpose()?,
334-
inner,
335-
})
336-
}
337-
}
338-
339-
impl<T: StorageFactoryBase> Debug for ResourceSetupStatusCheckWrapper<T> {
340-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
341-
std::fmt::Debug::fmt(&self.inner, f)
342-
}
343-
}
344-
345-
#[async_trait]
346-
impl<T: StorageFactoryBase> setup::ResourceSetupStatusCheck<serde_json::Value, serde_json::Value>
347-
for ResourceSetupStatusCheckWrapper<T>
348-
{
349-
fn describe_resource(&self) -> String {
350-
self.inner.describe_resource()
351-
}
352-
353-
fn key(&self) -> &serde_json::Value {
354-
&self.key_json
355-
}
356-
357-
fn desired_state(&self) -> Option<&serde_json::Value> {
358-
self.state_json.as_ref()
359-
}
360-
361-
fn describe_changes(&self) -> Vec<String> {
362-
self.inner.describe_changes()
363-
}
364-
365-
fn change_type(&self) -> setup::SetupChangeType {
366-
self.inner.change_type()
367-
}
368-
369-
async fn apply_change(&self) -> Result<()> {
370-
self.inner.apply_change().await
371-
}
372-
}
373-
374320
impl<T: StorageFactoryBase> ExportTargetFactory for T {
375321
fn build(
376322
self: Arc<Self>,
@@ -404,11 +350,7 @@ impl<T: StorageFactoryBase> ExportTargetFactory for T {
404350
desired_state: Option<serde_json::Value>,
405351
existing_states: setup::CombinedState<serde_json::Value>,
406352
auth_registry: &Arc<AuthRegistry>,
407-
) -> Result<
408-
Box<
409-
dyn setup::ResourceSetupStatusCheck<serde_json::Value, serde_json::Value> + Send + Sync,
410-
>,
411-
> {
353+
) -> Result<Box<dyn setup::ResourceSetupStatusCheck>> {
412354
let key: T::Key = serde_json::from_value(key.clone())?;
413355
let desired_state: Option<T::SetupState> = desired_state
414356
.map(|v| serde_json::from_value(v.clone()))
@@ -421,9 +363,12 @@ impl<T: StorageFactoryBase> ExportTargetFactory for T {
421363
existing_states,
422364
auth_registry,
423365
)?;
424-
Ok(Box::new(ResourceSetupStatusCheckWrapper::<T>::new(
425-
Box::new(status_check),
426-
)?))
366+
Ok(Box::new(status_check))
367+
}
368+
369+
fn describe_resource(&self, key: &serde_json::Value) -> Result<String> {
370+
let key: T::Key = serde_json::from_value(key.clone())?;
371+
StorageFactoryBase::describe_resource(self, &key)
427372
}
428373

429374
fn check_state_compatibility(

src/ops/interface.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,15 @@ pub trait ExportTargetFactory {
182182
desired_state: Option<serde_json::Value>,
183183
existing_states: setup::CombinedState<serde_json::Value>,
184184
auth_registry: &Arc<AuthRegistry>,
185-
) -> Result<
186-
Box<
187-
dyn setup::ResourceSetupStatusCheck<serde_json::Value, serde_json::Value> + Send + Sync,
188-
>,
189-
>;
185+
) -> Result<Box<dyn setup::ResourceSetupStatusCheck>>;
190186

191187
fn check_state_compatibility(
192188
&self,
193189
desired_state: &serde_json::Value,
194190
existing_state: &serde_json::Value,
195191
) -> Result<SetupStateCompatibility>;
192+
193+
fn describe_resource(&self, key: &serde_json::Value) -> Result<String>;
196194
}
197195

198196
#[derive(Clone)]

src/ops/storages/neo4j.rs

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,6 @@ struct SetupStatusCheck {
427427
graph_pool: Arc<GraphPool>,
428428
conn_spec: Neo4jConnectionSpec,
429429

430-
key: GraphRelationship,
431-
desired_state: Option<RelationshipSetupState>,
432-
433430
data_clear: Option<DataClearAction>,
434431
rel_constraint_to_delete: IndexSet<String>,
435432
rel_constraint_to_create: IndexMap<String, KeyConstraint>,
@@ -473,7 +470,7 @@ impl SetupStatusCheck {
473470

474471
let mut rel_constraint_to_create = IndexMap::new();
475472
let mut node_constraint_to_create = IndexMap::new();
476-
if let Some(desired_state) = &desired_state {
473+
if let Some(desired_state) = desired_state {
477474
let rel_constraint = KeyConstraint {
478475
label: key.relationship.clone(),
479476
field_name: desired_state.key_field_name.clone(),
@@ -485,8 +482,7 @@ impl SetupStatusCheck {
485482
.map(|c| rel_constraint.field_name == c.key_field_name)
486483
.unwrap_or(false)
487484
{
488-
rel_constraint_to_create
489-
.insert(desired_state.key_constraint_name.clone(), rel_constraint);
485+
rel_constraint_to_create.insert(desired_state.key_constraint_name, rel_constraint);
490486
}
491487

492488
old_node_constraints.swap_remove(&desired_state.src_node.key_constraint_name);
@@ -543,8 +539,6 @@ impl SetupStatusCheck {
543539
Self {
544540
graph_pool,
545541
conn_spec,
546-
key,
547-
desired_state,
548542
data_clear,
549543
rel_constraint_to_delete,
550544
rel_constraint_to_create,
@@ -556,19 +550,7 @@ impl SetupStatusCheck {
556550
}
557551

558552
#[async_trait]
559-
impl ResourceSetupStatusCheck<GraphRelationship, RelationshipSetupState> for SetupStatusCheck {
560-
fn describe_resource(&self) -> String {
561-
format!("Neo4j relationship {}", self.key.relationship)
562-
}
563-
564-
fn key(&self) -> &GraphRelationship {
565-
&self.key
566-
}
567-
568-
fn desired_state(&self) -> Option<&RelationshipSetupState> {
569-
self.desired_state.as_ref()
570-
}
571-
553+
impl ResourceSetupStatusCheck for SetupStatusCheck {
572554
fn describe_changes(&self) -> Vec<String> {
573555
let mut result = vec![];
574556
if let Some(data_clear) = &self.data_clear {
@@ -753,8 +735,7 @@ impl StorageFactoryBase for RelationshipFactory {
753735
desired: Option<RelationshipSetupState>,
754736
existing: CombinedState<RelationshipSetupState>,
755737
auth_registry: &Arc<AuthRegistry>,
756-
) -> Result<impl ResourceSetupStatusCheck<GraphRelationship, RelationshipSetupState> + 'static>
757-
{
738+
) -> Result<impl ResourceSetupStatusCheck + 'static> {
758739
let conn_spec = auth_registry.get::<Neo4jConnectionSpec>(&key.connection)?;
759740
Ok(SetupStatusCheck::new(
760741
key,
@@ -777,4 +758,8 @@ impl StorageFactoryBase for RelationshipFactory {
777758
};
778759
Ok(compatibility)
779760
}
761+
762+
fn describe_resource(&self, key: &GraphRelationship) -> Result<String> {
763+
Ok(format!("Neo4j relationship {}", key.relationship))
764+
}
780765
}

src/ops/storages/postgres.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -736,19 +736,7 @@ fn describe_index_spec(index_name: &str, index_spec: &VectorIndexDef) -> String
736736
}
737737

738738
#[async_trait]
739-
impl setup::ResourceSetupStatusCheck<TableId, SetupState> for SetupStatusCheck {
740-
fn describe_resource(&self) -> String {
741-
format!("Postgres table {}", self.table_id)
742-
}
743-
744-
fn key(&self) -> &TableId {
745-
&self.table_id
746-
}
747-
748-
fn desired_state(&self) -> Option<&SetupState> {
749-
self.desired_state.as_ref()
750-
}
751-
739+
impl setup::ResourceSetupStatusCheck for SetupStatusCheck {
752740
fn describe_changes(&self) -> Vec<String> {
753741
let mut descriptions = vec![];
754742
if self.drop_existing {
@@ -958,7 +946,7 @@ impl StorageFactoryBase for Arc<Factory> {
958946
desired: Option<SetupState>,
959947
existing: setup::CombinedState<SetupState>,
960948
_auth_registry: &Arc<AuthRegistry>,
961-
) -> Result<impl setup::ResourceSetupStatusCheck<TableId, SetupState> + 'static> {
949+
) -> Result<impl setup::ResourceSetupStatusCheck + 'static> {
962950
Ok(SetupStatusCheck::new(self.clone(), key, desired, existing))
963951
}
964952

@@ -987,6 +975,10 @@ impl StorageFactoryBase for Arc<Factory> {
987975
};
988976
Ok(compatibility)
989977
}
978+
979+
fn describe_resource(&self, key: &TableId) -> Result<String> {
980+
Ok(format!("Postgres table {}", key.table_name))
981+
}
990982
}
991983

992984
impl Factory {

src/setup/db_metadata.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::prelude::*;
22

3-
use super::{ResourceSetupStatusCheck, SetupChangeType, StateChange};
3+
use super::{ResourceSetupInfo, ResourceSetupStatusCheck, SetupChangeType, StateChange};
44
use crate::utils::db::WriteAction;
55
use axum::http::StatusCode;
66
use sqlx::PgPool;
@@ -294,20 +294,19 @@ pub struct MetadataTableSetup {
294294
pub metadata_table_missing: bool,
295295
}
296296

297-
#[async_trait]
298-
impl ResourceSetupStatusCheck<(), ()> for MetadataTableSetup {
299-
fn key(&self) -> &() {
300-
&()
301-
}
302-
303-
fn desired_state(&self) -> Option<&()> {
304-
Some(&())
305-
}
306-
307-
fn describe_resource(&self) -> String {
308-
"CocoIndex Metadata Table".to_string()
297+
impl MetadataTableSetup {
298+
pub fn into_setup_info(self) -> ResourceSetupInfo<(), (), MetadataTableSetup> {
299+
ResourceSetupInfo {
300+
key: (),
301+
state: None,
302+
description: "CocoIndex Metadata Table".to_string(),
303+
status_check: Some(self),
304+
}
309305
}
306+
}
310307

308+
#[async_trait]
309+
impl ResourceSetupStatusCheck for MetadataTableSetup {
311310
fn describe_changes(&self) -> Vec<String> {
312311
if self.metadata_table_missing {
313312
vec![format!(

0 commit comments

Comments
 (0)