operations.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. use codec::{Decode, Encode};
  2. use sp_std::collections::btree_map::BTreeMap;
  3. use sp_std::vec;
  4. use sp_std::vec::Vec;
  5. use versioned_store::{ClassId, ClassPropertyValue, EntityId, PropertyValue};
  6. #[derive(Encode, Decode, Eq, PartialEq, Clone, Debug)]
  7. pub enum ParametrizedPropertyValue {
  8. /// Same fields as normal PropertyValue
  9. PropertyValue(PropertyValue),
  10. /// This is the index of an operation creating an entity in the transaction/batch operations
  11. InternalEntityJustAdded(u32), // should really be usize but it doesn't have Encode/Decode support
  12. /// Vector of mix of Entities already existing and just added in a recent operation
  13. InternalEntityVec(Vec<ParameterizedEntity>),
  14. }
  15. #[derive(Encode, Decode, Eq, PartialEq, Clone, Debug)]
  16. pub enum ParameterizedEntity {
  17. InternalEntityJustAdded(u32),
  18. ExistingEntity(EntityId),
  19. }
  20. #[derive(Encode, Decode, Eq, PartialEq, Clone, Debug)]
  21. pub struct ParametrizedClassPropertyValue {
  22. /// Index is into properties vector of class.
  23. pub in_class_index: u16,
  24. /// Value of property with index `in_class_index` in a given class.
  25. pub value: ParametrizedPropertyValue,
  26. }
  27. #[derive(Encode, Decode, Eq, PartialEq, Clone, Debug)]
  28. pub struct CreateEntityOperation {
  29. pub class_id: ClassId,
  30. }
  31. #[derive(Encode, Decode, Eq, PartialEq, Clone, Debug)]
  32. pub struct UpdatePropertyValuesOperation {
  33. pub entity_id: ParameterizedEntity,
  34. pub new_parametrized_property_values: Vec<ParametrizedClassPropertyValue>,
  35. }
  36. #[derive(Encode, Decode, Eq, PartialEq, Clone, Debug)]
  37. pub struct AddSchemaSupportToEntityOperation {
  38. pub entity_id: ParameterizedEntity,
  39. pub schema_id: u16,
  40. pub parametrized_property_values: Vec<ParametrizedClassPropertyValue>,
  41. }
  42. #[derive(Encode, Decode, Eq, PartialEq, Clone, Debug)]
  43. pub enum OperationType {
  44. CreateEntity(CreateEntityOperation),
  45. UpdatePropertyValues(UpdatePropertyValuesOperation),
  46. AddSchemaSupportToEntity(AddSchemaSupportToEntityOperation),
  47. }
  48. #[derive(Encode, Decode, Eq, PartialEq, Clone, Debug)]
  49. pub struct Operation<Credential> {
  50. pub with_credential: Option<Credential>,
  51. pub as_entity_maintainer: bool,
  52. pub operation_type: OperationType,
  53. }
  54. pub fn parametrized_entity_to_entity_id(
  55. created_entities: &BTreeMap<usize, EntityId>,
  56. entity: ParameterizedEntity,
  57. ) -> Result<EntityId, &'static str> {
  58. match entity {
  59. ParameterizedEntity::ExistingEntity(entity_id) => Ok(entity_id),
  60. ParameterizedEntity::InternalEntityJustAdded(op_index_u32) => {
  61. let op_index = op_index_u32 as usize;
  62. if created_entities.contains_key(&op_index) {
  63. let entity_id = created_entities.get(&op_index).unwrap();
  64. Ok(*entity_id)
  65. } else {
  66. Err("EntityNotCreatedByOperation")
  67. }
  68. }
  69. }
  70. }
  71. pub fn parametrized_property_values_to_property_values(
  72. created_entities: &BTreeMap<usize, EntityId>,
  73. parametrized_property_values: Vec<ParametrizedClassPropertyValue>,
  74. ) -> Result<Vec<ClassPropertyValue>, &'static str> {
  75. let mut class_property_values: Vec<ClassPropertyValue> = vec![];
  76. for parametrized_class_property_value in parametrized_property_values.into_iter() {
  77. let property_value = match parametrized_class_property_value.value {
  78. ParametrizedPropertyValue::PropertyValue(value) => value,
  79. ParametrizedPropertyValue::InternalEntityJustAdded(
  80. entity_created_in_operation_index,
  81. ) => {
  82. // Verify that referenced entity was indeed created created
  83. let op_index = entity_created_in_operation_index as usize;
  84. if created_entities.contains_key(&op_index) {
  85. let entity_id = created_entities.get(&op_index).unwrap();
  86. PropertyValue::Internal(*entity_id)
  87. } else {
  88. return Err("EntityNotCreatedByOperation");
  89. }
  90. }
  91. ParametrizedPropertyValue::InternalEntityVec(parametrized_entities) => {
  92. let mut entities: Vec<EntityId> = vec![];
  93. for parametrized_entity in parametrized_entities.into_iter() {
  94. match parametrized_entity {
  95. ParameterizedEntity::ExistingEntity(id) => entities.push(id),
  96. ParameterizedEntity::InternalEntityJustAdded(
  97. entity_created_in_operation_index,
  98. ) => {
  99. let op_index = entity_created_in_operation_index as usize;
  100. if created_entities.contains_key(&op_index) {
  101. let entity_id = created_entities.get(&op_index).unwrap();
  102. entities.push(*entity_id);
  103. } else {
  104. return Err("EntityNotCreatedByOperation");
  105. }
  106. }
  107. }
  108. }
  109. PropertyValue::InternalVec(entities)
  110. }
  111. };
  112. class_property_values.push(ClassPropertyValue {
  113. in_class_index: parametrized_class_property_value.in_class_index,
  114. value: property_value,
  115. });
  116. }
  117. Ok(class_property_values)
  118. }