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

relationship_descriptor -> name, RelationshipTarget -> HolonCollection #108

Merged
merged 7 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
9 changes: 5 additions & 4 deletions crates/coordinator/dances/src/dance_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ pub enum ResponseStatusCode {
Unauthorized, // 401
Forbidden, // 403 -- use this for authorization / permission errors
NotFound, // 404
Conflict, // 409 -- use this when request denied due to a conflict with the current state of the resource
ServerError, // 500
NotImplemented, // 501
Conflict, // 409 -- use this when request denied due to a conflict with the current state of the resource
ServerError, // 500
NotImplemented, // 501
ServiceUnavailable, // 503
}

Expand Down Expand Up @@ -69,7 +69,8 @@ impl From<HolonError> for ResponseStatusCode {
HolonError::CacheError(_) => ResponseStatusCode::ServerError,
HolonError::NotAccessible(_, _) => ResponseStatusCode::Conflict,
HolonError::ValidationError(_) => ResponseStatusCode::BadRequest,
HolonError::GuardError(_) => ResponseStatusCode::BadRequest,
HolonError::Utf8Conversion(_, _) => ResponseStatusCode::ServerError,
HolonError::HashConversion(_, _) => ResponseStatusCode::ServerError,
HolonError::UnexpectedValueType(_, _) => ResponseStatusCode::ServerError,
}
}
Expand Down
36 changes: 18 additions & 18 deletions crates/coordinator/dances/src/dancer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,25 +175,25 @@ fn process_dispatch_result(dispatch_result: Result<ResponseBody, HolonError>) ->
Err(error) => {
// If the dispatch_result is an error, extract the associated string value
let error_message = match error.clone() {
HolonError::EmptyField(msg)
| HolonError::InvalidParameter(msg)
| HolonError::HolonNotFound(msg)
| HolonError::CommitFailure(msg)
| HolonError::WasmError(msg)
| HolonError::RecordConversion(msg)
| HolonError::InvalidHolonReference(msg)
| HolonError::IndexOutOfRange(msg)
| HolonError::NotImplemented(msg)
| HolonError::MissingStagedCollection(msg)
| HolonError::FailedToBorrow(msg)
| HolonError::UnableToAddHolons(msg)
| HolonError::InvalidRelationship(msg, _)
| HolonError::NotAccessible(msg, _)
| HolonError::GuardError(msg)
| HolonError::UnexpectedValueType(msg, _)
| HolonError::CacheError(msg) => msg,
HolonError::EmptyField(_)
| HolonError::InvalidParameter(_)
| HolonError::HolonNotFound(_)
| HolonError::CommitFailure(_)
| HolonError::WasmError(_)
| HolonError::RecordConversion(_)
| HolonError::InvalidHolonReference(_)
| HolonError::IndexOutOfRange(_)
| HolonError::NotImplemented(_)
| HolonError::MissingStagedCollection(_)
| HolonError::FailedToBorrow(_)
| HolonError::UnableToAddHolons(_)
| HolonError::InvalidRelationship(_, _)
| HolonError::NotAccessible(_, _)
| HolonError::UnexpectedValueType(_, _)
| HolonError::Utf8Conversion(_, _)
| HolonError::HashConversion(_, _)
| HolonError::CacheError(_) => error.to_string(),
HolonError::ValidationError(validation_error) => validation_error.to_string(),

};

// Construct DanceResponse with error details
Expand Down
6 changes: 3 additions & 3 deletions crates/coordinator/descriptors/src/relationship_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use holons::context::HolonsContext;
use holons::holon_error::HolonError;

use crate::descriptor_types::{DeletionSemantic, RelationshipDescriptor};
use holons::relationship::RelationshipTarget;
use holons::holon_collection::HolonCollection;
use holons::staged_reference::StagedReference;
use shared_types_holon::value_types::BaseType::Holon as BaseTypeHolon;
use shared_types_holon::value_types::{BaseValue, MapBoolean, MapInteger, MapString};
Expand Down Expand Up @@ -35,8 +35,8 @@ pub fn define_relationship_descriptor(
max_target_cardinality: MapInteger,
deletion_semantic: DeletionSemantic,
affinity: MapInteger,
_source_for: RelationshipTarget, // TODO: switch type to HolonReference
_target_for: RelationshipTarget, // TODO: switch type to HolonReference
_source_for: HolonCollection, // TODO: switch type to HolonReference
_target_for: HolonCollection, // TODO: switch type to HolonReference
has_supertype: Option<StagedReference>,
described_by: Option<StagedReference>,
_has_inverse: Option<StagedReference>,
Expand Down
2 changes: 1 addition & 1 deletion crates/coordinator/descriptors/src/semantic_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn define_semantic_version(major: i64, minor: i64, patch: i64) -> Result<Hol

// TODO: Implement and debug the following function
// pub fn define_semantic_version_descriptor(
// schema: &RelationshipTarget,
// schema: &HolonCollection,
//
// ) -> Holon {
//
Expand Down
8 changes: 4 additions & 4 deletions crates/coordinator/descriptors/src/type_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use holons::holon::Holon;
use holons::holon_error::HolonError;
use holons::staged_reference::StagedReference;

// use holons::relationship::{RelationshipName, RelationshipTarget};
// use holons::relationship::{RelationshipName, HolonCollection};

use crate::descriptor_types::TypeDescriptor;
use crate::semantic_version::define_semantic_version;
Expand Down Expand Up @@ -41,7 +41,7 @@ pub fn define_type_descriptor(
// ---------------- GET A NEW (EMPTY) HOLON -------------------------------
let mut descriptor = Holon::new();
// let schema_reference = StagedReference::from_holon()from_holon(schema.0.clone()));
// let schema_target = RelationshipTarget::One(schema_reference);
// let schema_target = HolonCollection::One(schema_reference);

// ---------------- USE THE INTERNAL HOLONS API TO ADD TYPE_HEADER PROPERTIES -----------------
descriptor
Expand Down Expand Up @@ -96,7 +96,7 @@ pub fn define_type_descriptor(
// let supertype_reference = HolonReference::Local(LocalHolonReference::from_holon(supertype.0.clone()));
// descriptor.add_related_holon(
// RelationshipName(MapString("HAS_SUPERTYPE".to_string())),
// RelationshipTarget::ZeroOrOne(Some(supertype_reference)),
// HolonCollection::ZeroOrOne(Some(supertype_reference)),
// );
// }
// // TODO: If described_by is supplied, populate that relationship
Expand All @@ -106,7 +106,7 @@ pub fn define_type_descriptor(
// descriptor
// .add_related_holon(
// RelationshipName(MapString("DESCRIBED_BY".to_string())),
// RelationshipTarget::ZeroOrOne(Some(described_by_reference)),
// HolonCollection::ZeroOrOne(Some(described_by_reference)),
// );
// }
//TODO: Populate owned_by relationship
Expand Down
33 changes: 9 additions & 24 deletions crates/coordinator/holons/src/commit_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use std::rc::Rc;
// use crate::cache_manager::HolonCacheManager;
use crate::context::HolonsContext;
use crate::holon::{Holon, HolonState};
use crate::holon_collection::HolonCollection;
use crate::holon_error::HolonError;
use crate::relationship::RelationshipMap;
use crate::relationship::RelationshipTarget;
use crate::smart_reference::SmartReference;
use crate::staged_reference::StagedReference;
use shared_types_holon::{MapInteger, MapString};
Expand Down Expand Up @@ -225,24 +225,16 @@ impl CommitManager {
holon.property_map = existing_holon.get_property_map(context)?;

// Iterate through existing holon's RelationshipMap
// For each RelationshipTarget, create a new StagedCollection in the new holon, from the existing holon's SmartCollection
// For each HolonCollection, create a new StagedCollection in the new holon, from the existing holon's SmartCollection
let existing_relationship_map = existing_holon.get_relationship_map(context)?;
holon.relationship_map = RelationshipMap::new();
for (relationship_name, relationship_target) in existing_relationship_map.0 {
let mut new_relationship_target = RelationshipTarget {
editable: None,
cursors: Vec::new(),
};
// for now populate 0th cursor
new_relationship_target.stage_collection(
staged_reference.clone_reference(),
relationship_target.cursors[0].clone(),
);
for (relationship_name, holon_collection) in existing_relationship_map.0 {
holon_collection.into_staged()?;

holon
.relationship_map
.0
.insert(relationship_name, new_relationship_target);
.insert(relationship_name, holon_collection);
}

Ok(staged_reference)
Expand Down Expand Up @@ -382,24 +374,17 @@ impl CommitManager {
holon.property_map = existing_holon.get_property_map(context)?;

// Iterate through existing holon's RelationshipMap
// For each RelationshipTarget, create a new StagedCollection in the new holon, from the existing holon's SmartCollection
// For each HolonCollection, create a new StagedCollection in the new holon, from the existing holon's SmartCollection
let existing_relationship_map = existing_holon.get_relationship_map(context)?;
holon.relationship_map = RelationshipMap::new();
for (relationship_name, relationship_target) in existing_relationship_map.0 {
let mut new_relationship_target = RelationshipTarget {
editable: None,
cursors: Vec::new(),
};
for (relationship_name, holon_collection) in existing_relationship_map.0 {
// *Note: temp implementation, populate 0th cursor. TODO: set strategy for how to determine which SmartCollection (cursor) to choose
new_relationship_target.stage_collection(
staged_reference.clone_reference(),
relationship_target.cursors[0].clone(),
);
holon_collection.into_staged()?;

holon
.relationship_map
.0
.insert(relationship_name, new_relationship_target);
.insert(relationship_name, holon_collection);
}

Ok(staged_reference)
Expand Down
18 changes: 10 additions & 8 deletions crates/coordinator/holons/src/holon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub trait HolonFieldGettable {
property_name: &PropertyName,
) -> Result<PropertyValue, HolonError>;

fn get_key(&mut self, context: &HolonsContext) -> Result<Option<MapString>, HolonError>;
fn get_key(&self, context: &HolonsContext) -> Result<Option<MapString>, HolonError>;

// fn query_relationship(&self, context: HolonsContext, relationship_name: RelationshipName, query_spec: Option<QuerySpec>-> SmartCollection;
}
Expand Down Expand Up @@ -138,11 +138,13 @@ impl Holon {
/// returns a HolonError::UnexpectedValueType.
pub fn get_key(&self) -> Result<Option<MapString>, HolonError> {
self.is_accessible(AccessType::Read)?;
let key = self.property_map.get(&PropertyName(MapString("key".to_string())));
let key = self
.property_map
.get(&PropertyName(MapString("key".to_string())));
if let Some(key) = key {
let string_value: String = key
.try_into()
.map_err(|_| HolonError::UnexpectedValueType(format!("{:?}", key), "MapString".to_string()))?;
let string_value: String = key.try_into().map_err(|_| {
HolonError::UnexpectedValueType(format!("{:?}", key), "MapString".to_string())
})?;
Ok(Some(MapString(string_value)))
} else {
Ok(None)
Expand Down Expand Up @@ -306,7 +308,7 @@ impl Holon {
/// If the staged holon is `Fetched`, `New`, or `Changed` commit does nothing.
///
/// If the staged holon is `Saved`, commit_relationship iterates through the holon's
/// `relationship_map` and calls commit on each member's RelationshipTarget.
/// `relationship_map` and calls commit on each member's HolonCollection.
///
/// If all commits are successful, the function returns a clone a self. Otherwise, the
/// function returns an error.
Expand All @@ -319,9 +321,9 @@ impl Holon {
Some(record) => {
let source_holon_id = record.action_address().clone();
// Iterate through the holon's relationship map, invoking commit on each
for (name, target) in self.relationship_map.0.clone() {
for (name, holon_collection) in self.relationship_map.0.clone() {
debug!("COMMITTING {:#?} relationship", name.clone());
target.commit_relationship(
holon_collection.commit_relationship(
context,
HolonId::from(source_holon_id.clone()),
name.clone(),
Expand Down
142 changes: 142 additions & 0 deletions crates/coordinator/holons/src/holon_collection.rs
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The is_accessible function logic looks correct, but it is a bit of work to translate between the requirements and the code. The "READ" check explicitly checks the collection_state that errors, else returns OK on anything else, whereas the "WRITE" checks for the collection_state that is OK and errors on anything else.

It may be a bit more maintainable and easier to update if requirements change by having both arms use similar logic.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

evomimic marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
use crate::context::HolonsContext;
use crate::holon::{AccessType, HolonFieldGettable};
use crate::holon_error::HolonError;
use crate::holon_reference::HolonReference;
use crate::relationship::RelationshipName;
use crate::smart_link_manager::{create_smart_link, SmartLinkInput};
use hdk::prelude::*;
use shared_types_holon::{HolonId, MapString};
use std::collections::BTreeMap;

#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub enum CollectionState {
Fetched, // links have been fetched from the persistent store for this collection
Staged, // the links for this collection have not been persisted
Saved, // a staged collection for which SmartLinks have been successfully committed
Abandoned, // a previously staged collection that was abandoned prior to being committed
}

#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct HolonCollection {
state: CollectionState,
members: Vec<HolonReference>,
keyed_index: BTreeMap<MapString, usize>, // usize is an index into the members vector
}

impl HolonCollection {
pub fn new_staged() -> Self {
HolonCollection {
state: CollectionState::Staged,
members: Vec::new(),
keyed_index: BTreeMap::new(),
}
}
pub fn new_existing() -> Self {
HolonCollection {
state: CollectionState::Fetched,
members: Vec::new(),
keyed_index: BTreeMap::new(),
}
}
pub fn is_accessible(&self, access_type: AccessType) -> Result<(), HolonError> {
match access_type {
AccessType::Read => {
if self.state == CollectionState::Abandoned {
Err(HolonError::NotAccessible(
"Read".to_string(),
format!("{:?}", self.state),
))
} else {
Ok(())
}
}
AccessType::Write => match self.state {
CollectionState::Staged => Ok(()),
_ => Err(HolonError::NotAccessible(
"Write".to_string(),
format!("{:?}", self.state),
)),
},
}
}

pub fn into_staged(&self) -> Result<HolonCollection, HolonError> {
self.is_accessible(AccessType::Read)?;
if self.state == CollectionState::Fetched {
Ok(HolonCollection {
state: CollectionState::Staged,
members: self.members.clone(),
keyed_index: self.keyed_index.clone(),
})
} else {
Err(HolonError::InvalidParameter("CollectionState".to_string()))
}
}

pub fn get_by_key(&self, key: &MapString) -> Result<Option<HolonReference>, HolonError> {
self.is_accessible(AccessType::Read)?;
let index = self.keyed_index.get(key);
if let Some(index) = index {
Ok(Some(self.members[*index].clone()))
} else {
Ok(None)
}
}

pub fn add_references(
&mut self,
context: &HolonsContext,
holons: Vec<HolonReference>,
) -> Result<(), HolonError> {
self.is_accessible(AccessType::Write)?;

for holon in holons {
let index = self.members.len();
self.members.push(holon.clone());
let key = holon.get_key(context)?;
if let Some(key) = key {
self.keyed_index.insert(key, index);
}
}

Ok(())
}

/// This method creates smartlinks from the specified source_id for the specified relationship name
/// to each holon its collection that has a holon_id.
pub fn add_smartlinks_for_collection(
&self,
context: &HolonsContext,
source_id: HolonId,
name: RelationshipName,
) -> Result<(), HolonError> {
debug!(
"Calling commit on each HOLON_REFERENCE in the collection for {:#?}.",
name.0.clone()
);
for holon_reference in self.members.clone() {
// Only commit references to holons with id's (i.e., Saved)
if let Ok(target_id) = holon_reference.get_holon_id(context) {
let input = SmartLinkInput {
from_address: source_id.clone(),
to_address: target_id,
relationship_name: name.clone(),
};
create_smart_link(input)?;
}
}
Ok(())
}

/// The method
pub fn commit_relationship(
&self,
context: &HolonsContext,
source_id: HolonId,
name: RelationshipName,
) -> Result<(), HolonError> {
self.add_smartlinks_for_collection(context, source_id.clone(), name.clone())?;

Ok(())
}
}
Loading