evomimic / map-holons

3 stars 1 forks source link

Unify SmartCollection and StagedCollection into HolonCollection #110

Closed evomimic closed 3 months ago

evomimic commented 3 months ago

This enhancement replaces the RelationshipTarget struct with a (new) HolonCollection enum. A HolonCollection is either editable (i.e., being staged) or existing (i.e., read-only).

Current State

In effect, SmartCollections are loadable and previously persisted , whereas StagedCollections are editable and not previously persisted.

Beyond these differences, they are basically identical:

Analysis of Design Options

Because this enhancement is complex enough to warrant consideration of different design options, they are briefly discussed here:

Use State Pattern?

Should we:

  1. follow the State design pattern and retain separate struct definitions for StagedCollection and SmartCollection (wrapped in HolonCollection enum) and with shared behaviors defined in a CollectionGettable trait?
  2. Or should we unify both into a single HolonCollection struct with a state variable and is_accessible method that guards access to functions.

Given the high degree of shared functionality and the likely need to have a state variable to handle commit processing states, I'm leaning towards Option 2.

Rename RelationshipTarget to HolonCollection?

This simplifies things quite a bit, but may pose an evolvability barrier if we decide to return to supporting different cardinalities (Optional, One, Many) for relationship targets. HolonCollection really only applies to the Many variant.

There are no current plans to return to this concept and for now it seems worth the simplification.

Proposal

Unify SmartCollection and StagedCollection into HolonCollection

In a new _holoncollection.rs file in the holons zome

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
}
pub struct HolonCollection {
  state: CollectionState,
  members: Vec<HolonReference>,
  keyed_index: BTreeMap<MapString, usize>, // usize is an index into the members vector
}

Notice that all fields are private and that _sourceholon, _relationshipdescriptor and _accesspath have been dropped from this definition.

HolonCollection should support the following functions:

Address Implementation Impacts

Neither SmartCollection nor StagedCollection are broadly used, so the effects of this change are somewhat limited. However, to realize the benefits of simplification, we need to leverage the new capabilities to drive simplification in several files.

_In stagedreference.rs:

Refactor RelationshipTarget to HolonCollection (move to separate issue?)