Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor for HolonWritable and HolonReadable traits #191

Merged
merged 2 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/type_system/base_types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

5 changes: 2 additions & 3 deletions zomes/coordinator/core_schema/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ pub fn load_core_schema(context: &HolonsContext) -> Result<CommitResponse, Holon
)?;

info!("Staging Schema...");
let staged_schema_ref = HolonReference::Staged(
context.space_manager.borrow().stage_new_holon(schema.0.clone())?,
);
let staged_schema_ref =
HolonReference::Staged(context.space_manager.borrow().stage_new_holon(schema.0.clone())?);

context.add_reference_to_dance_state(staged_schema_ref.clone())?;

Expand Down
20 changes: 9 additions & 11 deletions zomes/coordinator/dances/src/dance_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,18 @@ impl DanceRequest {
}

// Method to initialize the HolonsContext + HolonSpaceManger from the session state
// This method creates a nursery and a local space holon from the session state if available
// If the session is available it will creata a space manager from session otherwise a new one
//lastly, it will initialize the HolonsContext with the space manager and return it
pub fn init_context_from_state(&self) -> HolonsContext {
let staged_holons = self.get_state().get_staging_area().get_staged_rc_holons();//from_stage_to_nursery();
let stage_index =self.get_state().get_staging_area().get_staged_index();
let staged_holons = self.get_state().get_staging_area().get_staged_rc_holons(); //from_stage_to_nursery();
let stage_index = self.get_state().get_staging_area().get_staged_index();
let local_space_holon = self.get_state().get_local_holon_space();
let space_manager = HolonSpaceManager::new_from_session(staged_holons, stage_index, local_space_holon);
let space_manager =
HolonSpaceManager::new_from_session(staged_holons, stage_index, local_space_holon);
//let space_manager = match local_space_holon {
// None => HolonSpaceManager::new(),
// Some(local_space_holon) => {
// debug!("Space manager created from session state in dance request");
// HolonSpaceManager::new_from_session(nursery, local_space_holon)
// }
// None => HolonSpaceManager::new(),
// Some(local_space_holon) => {
// debug!("Space manager created from session state in dance request");
// HolonSpaceManager::new_from_session(nursery, local_space_holon)
// }
//};
HolonsContext::init_context(space_manager)
}
Expand Down
4 changes: 3 additions & 1 deletion zomes/coordinator/dances/src/descriptors_dance_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ pub fn load_core_schema_dance(
match result {
Ok(commit_response) => match commit_response.status {
CommitRequestStatus::Complete => Ok(ResponseBody::None),
CommitRequestStatus::Incomplete => Err(HolonError::CommitFailure("Incomplete commit".to_string())),
CommitRequestStatus::Incomplete => {
Err(HolonError::CommitFailure("Incomplete commit".to_string()))
}
},
Err(e) => Err(e),
}
Expand Down
16 changes: 7 additions & 9 deletions zomes/coordinator/dances/src/holon_dance_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use holons::context::HolonsContext;
use holons::holon::Holon;
use holons::holon_error::HolonError;
use holons::holon_reference::HolonReference;
use holons::holon_writable::HolonWritable;
use holons::query::*;
use holons::relationship::RelationshipName;
use holons::smart_reference::SmartReference;
Expand Down Expand Up @@ -225,7 +226,6 @@ pub fn get_all_holons_dance(
Ok(holons) => Ok(ResponseBody::Holons(holons)),
Err(holon_error) => Err(holon_error.into()),
}

}

/// Builds a DanceRequest for retrieving all holons from the persistent store
Expand Down Expand Up @@ -614,8 +614,7 @@ pub fn with_properties_dance(
};
//let staged_holon = space_manager.get_mut_holon_by_index(staged_index.clone());
let holon = space_manager.get_holon_by_index(staged_index.clone())?;
let staged_holon = holon.try_borrow_mut()
.map_err(|e| {
let staged_holon = holon.try_borrow_mut().map_err(|e| {
HolonError::FailedToBorrow(format!("Unable to borrow holon immutably: {}", e))
});

Expand Down Expand Up @@ -691,12 +690,11 @@ pub fn abandon_staged_changes_dance(
// Try to get a mutable reference to the staged holon referenced by its index
let space_manager = context.space_manager.borrow();
debug!("commit_manager borrowed_mut");
// let staged_holon = space_manager_mut.get_mut_holon_by_index(staged_index.clone());
let holon = space_manager.get_holon_by_index(staged_index.clone())?;
let staged_holon = holon.try_borrow_mut()
.map_err(|e| {
HolonError::FailedToBorrow(format!("Unable to borrow holon immutably: {}", e))
});
// let staged_holon = space_manager_mut.get_mut_holon_by_index(staged_index.clone());
let holon = space_manager.get_holon_by_index(staged_index.clone())?;
let staged_holon = holon.try_borrow_mut().map_err(|e| {
HolonError::FailedToBorrow(format!("Unable to borrow holon immutably: {}", e))
});
//debug!("Result of borrow_mut on the staged holon {:#?}", staged_holon.clone());

match staged_holon {
Expand Down
9 changes: 6 additions & 3 deletions zomes/coordinator/dances/src/staging_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ pub struct StagingArea {
index: BTreeMap<MapString, usize>, // Allows lookup by key to staged holons for which keys are defined
}


impl StagingArea {
pub fn empty() -> Self {
StagingArea { staged_holons: Vec::new(), index: BTreeMap::new() }
}

// Function to create StagingArea from the holon references and index
pub fn new_from_references(rc_holons:Vec<Rc<RefCell<Holon>>>, index:BTreeMap<MapString, usize>) -> Self {
let staged_holons: Vec<Holon> = rc_holons.iter().map(|holon_rc| holon_rc.borrow().clone()).collect();
pub fn new_from_references(
rc_holons: Vec<Rc<RefCell<Holon>>>,
index: BTreeMap<MapString, usize>,
) -> Self {
let staged_holons: Vec<Holon> =
rc_holons.iter().map(|holon_rc| holon_rc.borrow().clone()).collect();
StagingArea { staged_holons, index }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@

use core::panic;
use dances::dance_response::ResponseStatusCode;
use holons::context::HolonsContext;
use holons::helpers::*;
use holons::holon::Holon;
use holons::holon_api::*;
use rstest::*;
use shared_types_holon::value_types::BaseValue;
use std::collections::btree_map::BTreeMap;
use holons::context::HolonsContext;

use crate::shared_test::test_data_types::DancesTestCase;

Expand Down
1 change: 0 additions & 1 deletion zomes/coordinator/dances/tests/shared_test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub mod test_with_properties_command;

use hdk::prelude::*;
use holochain::sweettest::{SweetAgents, SweetCell, SweetConductor, SweetDnaFile};
use holons::holon_reference::HolonGettable;
use holons::{
context::HolonsContext, holon::Holon, holon_error::HolonError, holon_reference::HolonReference,
relationship::RelationshipName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use holons::helpers::*;
use holons::holon::Holon;
use holons::holon_api::*;
use holons::holon_error::HolonError;
use holons::holon_reference::HolonGettable;
use shared_types_holon::holon_node::{HolonNode, PropertyMap, PropertyName};
use shared_types_holon::value_types::BaseValue;
use shared_types_holon::{HolonId, MapBoolean, MapInteger, MapString};
Expand Down
4 changes: 2 additions & 2 deletions zomes/coordinator/descriptors/src/boolean_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use holons::context::HolonsContext;
use holons::holon::Holon;
use holons::holon_error::HolonError;
use holons::holon_reference::HolonReference;
use holons::holon_writable::HolonWritable;
use holons::space_manager::HolonStagingBehavior;
use holons::staged_reference::StagedReference;
use shared_types_holon::value_types::{BaseType, ValueType};
Expand Down Expand Up @@ -53,8 +54,7 @@ pub fn define_boolean_type(

debug!("Staging... {:#?}", boolean_type.clone());

let boolean_type_ref =
context.space_manager.borrow().stage_new_holon(boolean_type.clone())?;
let boolean_type_ref = context.space_manager.borrow().stage_new_holon(boolean_type.clone())?;

// Add its relationships

Expand Down
4 changes: 3 additions & 1 deletion zomes/coordinator/descriptors/src/collection_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use hdi::prelude::debug;
use holons::context::HolonsContext;
use holons::holon::Holon;
use holons::holon_error::HolonError;
use holons::holon_reference::{HolonGettable, HolonReference};
use holons::holon_readable::HolonReadable;
use holons::holon_reference::HolonReference;
use holons::holon_writable::HolonWritable;
use holons::space_manager::HolonStagingBehavior;
use holons::staged_reference::StagedReference;
use shared_types_holon::value_types::{BaseValue, MapBoolean, MapInteger, MapString};
Expand Down
1 change: 1 addition & 0 deletions zomes/coordinator/descriptors/src/descriptor_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ pub enum SchemaTypeDefinition {
StringType(StringTypeDefinition),
}

#[allow(dead_code)]
trait SchemaNamesTrait {
/// This method returns unique type_name for this type
fn derive_type_name(&self) -> MapString;
Expand Down
1 change: 1 addition & 0 deletions zomes/coordinator/descriptors/src/enum_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use holons::context::HolonsContext;
use holons::holon::Holon;
use holons::holon_error::HolonError;
use holons::holon_reference::HolonReference;
use holons::holon_writable::HolonWritable;
use holons::space_manager::HolonStagingBehavior;
use holons::staged_reference::StagedReference;
use shared_types_holon::value_types::{BaseType, BaseValue, MapString, ValueType};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use holons::context::HolonsContext;
use holons::holon::Holon;
use holons::holon_error::HolonError;
use holons::holon_reference::HolonReference;
use holons::holon_writable::HolonWritable;
use holons::space_manager::HolonStagingBehavior;
use holons::staged_reference::StagedReference;
use shared_types_holon::value_types::{BaseType, BaseValue, MapInteger, MapString, ValueType};
Expand Down
1 change: 1 addition & 0 deletions zomes/coordinator/descriptors/src/holon_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use holons::context::HolonsContext;
use holons::holon::Holon;
use holons::holon_error::HolonError;
use holons::holon_reference::HolonReference;
use holons::holon_writable::HolonWritable;
use holons::space_manager::HolonStagingBehavior;
use holons::staged_reference::StagedReference;
use shared_types_holon::value_types::MapString;
Expand Down
4 changes: 2 additions & 2 deletions zomes/coordinator/descriptors/src/integer_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use holons::context::HolonsContext;
use holons::holon::Holon;
use holons::holon_error::HolonError;
use holons::holon_reference::HolonReference;
use holons::holon_writable::HolonWritable;
use holons::space_manager::HolonStagingBehavior;
use holons::staged_reference::StagedReference;
use shared_types_holon::value_types::{BaseType, BaseValue, MapInteger, MapString, ValueType};
Expand Down Expand Up @@ -60,8 +61,7 @@ pub fn define_integer_type(
)?;

// Stage new holon type
let integer_type_ref =
context.space_manager.borrow().stage_new_holon(integer_type.clone())?;
let integer_type_ref = context.space_manager.borrow().stage_new_holon(integer_type.clone())?;

// Add some relationships

Expand Down
3 changes: 2 additions & 1 deletion zomes/coordinator/descriptors/src/property_descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::descriptor_types::{CoreSchemaPropertyTypeName, CoreSchemaRelationshipTypeName};
use hdi::prelude::debug;
use holons::{context::HolonsContext, space_manager::HolonStagingBehavior};
use holons::holon::Holon;
use holons::holon_error::HolonError;
use holons::holon_reference::HolonReference;
use holons::holon_writable::HolonWritable;
use holons::staged_reference::StagedReference;
use holons::{context::HolonsContext, space_manager::HolonStagingBehavior};
use shared_types_holon::{BaseType, BaseValue, MapString, PropertyName};

use crate::type_descriptor::{define_type_descriptor, TypeDescriptorDefinition};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use holons::context::HolonsContext;
use holons::holon::Holon;
use holons::holon_error::HolonError;
use holons::holon_reference::HolonReference;
use holons::holon_writable::HolonWritable;
use holons::relationship::RelationshipName;
use holons::space_manager::HolonStagingBehavior;
use holons::staged_reference::StagedReference;
Expand Down
4 changes: 2 additions & 2 deletions zomes/coordinator/descriptors/src/string_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use holons::context::HolonsContext;
use holons::holon::Holon;
use holons::holon_error::HolonError;
use holons::holon_reference::HolonReference;
use holons::holon_writable::HolonWritable;
use holons::space_manager::HolonStagingBehavior;
use holons::staged_reference::StagedReference;
use shared_types_holon::value_types::{BaseType, BaseValue, MapInteger, MapString, ValueType};
Expand Down Expand Up @@ -67,8 +68,7 @@ pub fn define_string_type(
)?;

// Stage new string type
let string_type_ref =
context.space_manager.borrow().stage_new_holon(string_type.clone())?;
let string_type_ref = context.space_manager.borrow().stage_new_holon(string_type.clone())?;

// Add some relationships
string_type_ref.add_related_holons(
Expand Down
4 changes: 2 additions & 2 deletions zomes/coordinator/descriptors/src/type_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use holons::context::HolonsContext;
use holons::holon::Holon;
use holons::holon_error::HolonError;
use holons::holon_reference::HolonReference;
use holons::holon_writable::HolonWritable;
use holons::relationship::RelationshipName;
use holons::space_manager::HolonStagingBehavior;
use holons::staged_reference::StagedReference;
Expand Down Expand Up @@ -96,8 +97,7 @@ pub fn define_type_descriptor(

debug!("{:#?}", descriptor.clone());

let staged_reference =
context.space_manager.borrow().stage_new_holon(descriptor.clone())?;
let staged_reference = context.space_manager.borrow().stage_new_holon(descriptor.clone())?;

// Add related holons

Expand Down
1 change: 0 additions & 1 deletion zomes/coordinator/holons/src/all_holon_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use holons_integrity::*;

use crate::holon_node::get_latest_holon_node;


//TODO: move this function to holon_node.rs and delete the file

/// Get all the HolonNodes from the HolonSpace. In a case where a Holon has more than one version, only return the latest version.
Expand Down
14 changes: 6 additions & 8 deletions zomes/coordinator/holons/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::rc::Rc;
pub struct HolonsContext {
pub dance_state: RefCell<TransientCollection>,
pub space_manager: Rc<RefCell<HolonSpaceManager>>,
pub query_manager: RefCell<QueryManager>
pub query_manager: RefCell<QueryManager>,
}

impl HolonsContext {
Expand All @@ -20,19 +20,17 @@ impl HolonsContext {
HolonsContext {
dance_state: TransientCollection::new().into(),
space_manager, //Rc::from(HolonSpaceManager::new().into()),
query_manager,// QueryManager::new(Rc.clone(space_manager)).into()
query_manager, // QueryManager::new(Rc.clone(space_manager)).into()
}
}
pub fn init_context(
space_manager: HolonSpaceManager,
) -> HolonsContext {
pub fn init_context(space_manager: HolonSpaceManager) -> HolonsContext {
// Return the initialized context
let space_manager = Rc::from(RefCell::from(space_manager));
let query_manager = RefCell::from(QueryManager::new(Rc::clone(&space_manager)));
HolonsContext {
dance_state: TransientCollection::new().into(),
space_manager,//: Rc::from(RefCell::from(space_manager)),
query_manager //QueryManager::new(Rc::new(RefCell::new(space_manager))).into()
space_manager, //: Rc::from(RefCell::from(space_manager)),
query_manager, //QueryManager::new(Rc::new(RefCell::new(space_manager))).into()
}
}

Expand All @@ -41,7 +39,7 @@ impl HolonsContext {
pub fn get_local_space_holon(&self) -> Option<HolonReference> {
let space_manager = self.space_manager.borrow();
space_manager.get_space_holon()
// local_space_holon.clone() // If no panic, return cloned value
// local_space_holon.clone() // If no panic, return cloned value
}

pub fn add_references_to_dance_state(
Expand Down
10 changes: 5 additions & 5 deletions zomes/coordinator/holons/src/holon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ impl Holon {
}
}

//TODO: move this static/stateless function to the Holon_service, the "get()" logic
//including the GetOptions logic should only be in the holon_node module
//TODO: move this static/stateless function to the Holon_service, the "get()" logic
//including the GetOptions logic should only be in the holon_node module
pub fn delete_holon(id: LocalId) -> Result<ActionHash, HolonError> {
let record = get(id.0.clone(), GetOptions::default())
.map_err(|e| HolonError::from(e))?
Expand Down Expand Up @@ -732,7 +732,7 @@ impl Holon {
key, local_id, self.state, self.validation_state
)
}

/// try_from_node inflates a Holon from a HolonNode.
/// Since Implemented here to avoid conflicts with hdk::core's implementation of TryFrom Trait
pub fn try_from_node(holon_node_record: Record) -> Result<Holon, HolonError> {
Expand Down Expand Up @@ -793,7 +793,7 @@ impl Holon {
property_name.0
))),
}
}
}

pub fn get_description(&self) -> Result<MapString, HolonError> {
let property_name = PropertyName(MapString("description".to_string()));
Expand Down Expand Up @@ -826,5 +826,5 @@ impl Holon {
name.clone().into_base_value(),
)?;
Ok(self)
}
}
}
3 changes: 2 additions & 1 deletion zomes/coordinator/holons/src/holon_collection.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::context::HolonsContext;
use crate::holon::AccessType;
use crate::holon_error::HolonError;
use crate::holon_reference::{HolonGettable, HolonReference};
use crate::holon_readable::HolonReadable;
use crate::holon_reference::HolonReference;
use crate::relationship::RelationshipName;
use crate::smartlink::{save_smartlink, SmartLink};
use core::fmt;
Expand Down
Loading
Loading