Browse Source

Architecture reworkings: in_class_schema_indexes switched to BTreeSet representation, renamed to supported_schemas

iorveth 4 years ago
parent
commit
d0b1fe94ad

+ 6 - 8
runtime-modules/content-directory/src/lib.rs

@@ -202,7 +202,7 @@ pub struct Entity {
     /// What schemas under which this entity of a class is available, think
     /// v.2.0 Person schema for John, v3.0 Person schema for John
     /// Unlikely to be more than roughly 20ish, assuming schemas for a given class eventually stableize, or that very old schema are eventually removed.
-    pub in_class_schema_indexes: Vec<u16>, // indices of schema in corresponding class
+    pub supported_schemas: BTreeSet<u16>, // indices of schema in corresponding class
 
     /// Values for properties on class that are used by some schema used by this entity!
     /// Length is no more than Class.properties.
@@ -738,7 +738,7 @@ impl<T: Trait> Module<T> {
 
         let new_entity = Entity {
             class_id,
-            in_class_schema_indexes: vec![],
+            supported_schemas: BTreeSet::new(),
             values: BTreeMap::new(),
         };
 
@@ -1187,7 +1187,7 @@ impl<T: Trait> Module<T> {
     
             EntityById::mutate(entity_id, |entity| {
                 // Add a new schema to the list of schemas supported by this entity.
-                entity.in_class_schema_indexes.push(schema_id);
+                entity.supported_schemas.insert(schema_id);
     
                 // Update entity values only if new properties have been added.
                 if appended_entity_values.len() > entity.values.len() {
@@ -1237,11 +1237,9 @@ impl<T: Trait> Module<T> {
         }
 
         pub fn ensure_schema_id_is_not_added(entity: &Entity, schema_id: u16) -> dispatch::Result {
-            let schema_not_added = entity
-                .in_class_schema_indexes
-                .iter()
-                .position(|x| *x == schema_id)
-                .is_none();
+            let schema_not_added = !entity
+                .supported_schemas
+                .contains(&schema_id);
             ensure!(schema_not_added, ERROR_SCHEMA_ALREADY_ADDED_TO_ENTITY);
             Ok(())
         }

+ 3 - 3
runtime-modules/content-directory/src/tests.rs

@@ -3,7 +3,7 @@
 use super::*;
 use crate::mock::*;
 use rstd::collections::btree_set::BTreeSet;
-
+use core::iter::FromIterator;
 use srml_support::{assert_err, assert_ok};
 
 #[test]
@@ -666,7 +666,7 @@ fn batch_transaction_vector_of_entities() {
             EntityById::get(entity_id),
             Entity {
                 class_id: new_class_id,
-                in_class_schema_indexes: vec![0],
+                supported_schemas: BTreeSet::from_iter(vec![SCHEMA_ID_0].into_iter()),
                 values: prop_value(0, PropertyValue::ReferenceVec(vec![entity_id + 1, entity_id + 2,]))
             }
         );
@@ -1099,7 +1099,7 @@ fn should_add_schema_to_entity_when_some_optional_props_provided() {
         ));
 
         let entity = TestModule::entity_by_id(entity_id);
-        assert_eq!(entity.in_class_schema_indexes, [SCHEMA_ID_0]);
+        assert_eq!(entity.supported_schemas, BTreeSet::from_iter(vec![SCHEMA_ID_0].into_iter()));
         prop_values = bool_prop_value();
         prop_values.append(&mut prop_value(PROP_ID_U32, PropertyValue::Uint32(123)));
         prop_values.append(&mut prop_value(PROP_ID_INTERNAL, PropertyValue::Bool(false)));