1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! EAV stands for entity-attribute-value. It is a pattern implemented here
//! for adding metadata about entries in the DHT, additionally
//! being used to define relationships between AddressableContent values.
//! See [wikipedia](https://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model) to learn more about this pattern.

use crate::{
    cas::content::{Address, AddressableContent, Content},
    error::PersistenceResult,
};

use chrono::offset::Utc;
use eav::{
    query::{EaviQuery, IndexFilter},
    storage::{EntityAttributeValueStorage, ExampleEntityAttributeValueStorage},
};
use holochain_json_api::{
    error::{JsonError, JsonResult},
    json::JsonString,
};
use std::{
    collections::BTreeSet,
    convert::{TryFrom, TryInto},
    fmt::{Debug, Display, Formatter},
    hash::Hash,
    option::NoneError,
};

/// Address of AddressableContent representing the EAV entity
pub type Entity = Address;

///  This is the minimal bounds defined for any attribute type. Some storage implementations
/// may require other traits.

pub trait Attribute:
    PartialEq + Eq + PartialOrd + Hash + Clone + serde::Serialize + Debug + Ord
{
}

#[derive(
    PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Debug, Serialize, Deserialize, DefaultJson,
)]
pub enum ExampleAttribute {
    WithoutPayload,
    WithPayload(String),
}

impl Display for ExampleAttribute {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        let str = match self {
            ExampleAttribute::WithoutPayload => "without-payload",
            ExampleAttribute::WithPayload(payload) => payload,
        };
        write!(f, "{}", str)
    }
}

impl Default for ExampleAttribute {
    fn default() -> ExampleAttribute {
        ExampleAttribute::WithoutPayload
    }
}

impl From<String> for ExampleAttribute {
    fn from(str: String) -> Self {
        if str == "without-payload" {
            ExampleAttribute::WithoutPayload
        } else {
            ExampleAttribute::WithPayload(str)
        }
    }
}
impl Attribute for ExampleAttribute {}

#[derive(PartialEq, Debug)]
pub enum AttributeError {
    Unrecognized(String),
    ParseError,
}

impl From<AttributeError> for JsonError {
    fn from(err: AttributeError) -> JsonError {
        let msg = match err {
            AttributeError::Unrecognized(a) => format!("Unknown attribute: {}", a),
            AttributeError::ParseError => {
                String::from("Could not parse attribute, bad regex match")
            }
        };
        JsonError::ErrorGeneric(msg)
    }
}
impl From<NoneError> for AttributeError {
    fn from(_: NoneError) -> AttributeError {
        AttributeError::ParseError
    }
}

/// Address of AddressableContent representing the EAV value
pub type Value = Address;

// @TODO do we need this?
// unique (local to the source) monotonically increasing number that can be used for crdt/ordering
// @see https://papers.radixdlt.com/tempo/#logical-clocks
pub type Index = i64;

// @TODO do we need this?
// source agent asserting the meta
// type Source ...
/// The basic struct for EntityAttributeValue triple, implemented as AddressableContent
/// including the necessary serialization inherited.
#[derive(PartialEq, Eq, Hash, Clone, Debug, Serialize, Deserialize, PartialOrd, Ord)]
pub struct EntityAttributeValueIndex<A: Attribute> {
    index: Index,
    entity: Entity,
    value: Value,
    attribute: A,
    // source: Source,
}

impl<A: Attribute> From<&EntityAttributeValueIndex<A>> for JsonString
where
    A: serde::de::DeserializeOwned,
{
    fn from(v: &EntityAttributeValueIndex<A>) -> JsonString {
        match ::serde_json::to_string(&v) {
            Ok(s) => Ok(JsonString::from_json(&s)),
            Err(e) => {
                eprintln!("Error serializing to JSON: {:?}", e);
                Err(JsonError::SerializationError(e.to_string()))
            }
        }
        .unwrap_or_else(|_| panic!("could not Jsonify {}: {:?}", "EntityAttributeValueIndex", v))
    }
}

impl<A: Attribute> From<EntityAttributeValueIndex<A>> for JsonString
where
    A: serde::de::DeserializeOwned,
{
    fn from(v: EntityAttributeValueIndex<A>) -> JsonString {
        JsonString::from(&v)
    }
}

impl<'a, A: Attribute> ::std::convert::TryFrom<&'a JsonString> for EntityAttributeValueIndex<A>
where
    A: serde::de::DeserializeOwned,
{
    type Error = JsonError;
    fn try_from(json_string: &JsonString) -> Result<Self, Self::Error> {
        let str = String::from(json_string);

        let from_json = ::serde_json::from_str(&str);

        match from_json {
            Ok(d) => Ok(d),
            Err(e) => Err(JsonError::SerializationError(e.to_string())),
        }
    }
}

impl<A: Attribute> ::std::convert::TryFrom<JsonString> for EntityAttributeValueIndex<A>
where
    A: serde::de::DeserializeOwned,
{
    type Error = JsonError;
    fn try_from(json_string: JsonString) -> Result<Self, Self::Error> {
        EntityAttributeValueIndex::try_from(&json_string)
    }
}

impl<A: Attribute> AddressableContent for EntityAttributeValueIndex<A>
where
    A: serde::de::DeserializeOwned,
{
    fn content(&self) -> Content {
        self.to_owned().into()
    }

    fn try_from_content(content: &Content) -> Result<Self, JsonError> {
        content.to_owned().try_into()
    }
}

fn validate_attribute<A: Attribute>(_attribute: &A) -> JsonResult<()> {
    Ok(())
}

impl<A: Attribute> EntityAttributeValueIndex<A> {
    pub fn new(
        entity: &Entity,
        attribute: &A,
        value: &Value,
    ) -> PersistenceResult<EntityAttributeValueIndex<A>> {
        validate_attribute(attribute)?;
        Ok(EntityAttributeValueIndex {
            entity: entity.clone(),
            attribute: attribute.clone(),
            value: value.clone(),
            index: Utc::now().timestamp_nanos(),
        })
    }

    pub fn new_with_index(
        entity: &Entity,
        attribute: &A,
        value: &Value,
        timestamp: i64,
    ) -> PersistenceResult<EntityAttributeValueIndex<A>> {
        validate_attribute(attribute)?;
        Ok(EntityAttributeValueIndex {
            entity: entity.clone(),
            attribute: attribute.clone(),
            value: value.clone(),
            index: timestamp,
        })
    }

    pub fn entity(&self) -> Entity {
        self.entity.clone()
    }

    pub fn attribute(&self) -> A {
        self.attribute.clone()
    }

    pub fn value(&self) -> Value {
        self.value.clone()
    }

    pub fn index(&self) -> Index {
        self.index
    }

    pub fn set_index(&mut self, new_index: i64) {
        self.index = new_index
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, DefaultJson)]
pub struct ExampleEntry {
    pub data: String,
}

impl AddressableContent for ExampleEntry {
    fn address(&self) -> Address {
        Address::from(self.data.clone())
    }

    fn content(&self) -> Content {
        self.into()
    }

    fn try_from_content(content: &Content) -> JsonResult<ExampleEntry> {
        ExampleEntry::try_from(content.to_owned())
    }
}

impl ExampleEntry {
    pub fn new(data: String) -> Self {
        Self { data }
    }
}

pub fn eav_round_trip_test_runner<A: Attribute>(
    entity_content: impl AddressableContent + Clone,
    attribute: A,
    value_content: impl AddressableContent + Clone,
) where
    A: std::default::Default + std::marker::Sync + std::marker::Send,
{
    let eav = EntityAttributeValueIndex::new(
        &entity_content.address(),
        &attribute,
        &value_content.address(),
    )
    .expect("Could not create EAV");
    let mut eav_storage = ExampleEntityAttributeValueStorage::new();

    assert_eq!(
        BTreeSet::new(),
        eav_storage
            .fetch_eavi(&EaviQuery::new(
                Some(entity_content.address()).into(),
                Some(attribute.clone()).into(),
                Some(value_content.address()).into(),
                IndexFilter::LatestByAttribute,
                None
            ))
            .expect("could not fetch eav"),
    );

    eav_storage.add_eavi(&eav).expect("could not add eav");

    let mut expected = BTreeSet::new();
    expected.insert(eav);
    // some examples of constraints that should all return the eav
    for (e, a, v) in vec![
        // constrain all
        (
            Some(entity_content.address()),
            Some(attribute.clone()),
            Some(value_content.address()),
        ),
        // open entity
        (None, Some(attribute.clone()), Some(value_content.address())),
        // open attribute
        (
            Some(entity_content.address()),
            None,
            Some(value_content.address()),
        ),
        // open value
        (Some(entity_content.address()), Some(attribute), None),
        // open
        (None, None, None),
    ] {
        assert_eq!(
            expected,
            eav_storage
                .fetch_eavi(&EaviQuery::new(
                    e.into(),
                    a.into(),
                    v.into(),
                    IndexFilter::LatestByAttribute,
                    None
                ))
                .expect("could not fetch eav")
        );
    }
}

#[cfg(test)]
pub mod tests {
    use super::*;
    use crate::{
        cas::{
            content::{AddressableContent, AddressableContentTestSuite, ExampleAddressableContent},
            storage::{
                test_content_addressable_storage, EavTestSuite, ExampleContentAddressableStorage,
            },
        },
        eav::EntityAttributeValueIndex,
    };
    use fixture::{test_eav, test_eav_address, test_eav_content, test_eav_entity};
    use holochain_json_api::json::RawString;

    pub fn test_eav_storage<A: Attribute>() -> ExampleEntityAttributeValueStorage<A>
    where
        A: std::default::Default,
    {
        ExampleEntityAttributeValueStorage::new()
    }

    #[test]
    fn example_eav_round_trip() {
        let eav_storage = test_eav_storage();
        let entity =
            ExampleAddressableContent::try_from_content(&JsonString::from(RawString::from("foo")))
                .unwrap();
        let attribute = ExampleAttribute::WithPayload("favourite-color".into());
        let value =
            ExampleAddressableContent::try_from_content(&JsonString::from(RawString::from("blue")))
                .unwrap();

        EavTestSuite::test_round_trip(eav_storage, entity, attribute, value)
    }

    #[test]
    fn example_eav_one_to_many() {
        EavTestSuite::test_one_to_many::<
            ExampleAddressableContent,
            ExampleAttribute,
            ExampleEntityAttributeValueStorage<ExampleAttribute>,
        >(test_eav_storage(), &ExampleAttribute::default());
    }

    #[test]
    fn example_eav_many_to_one() {
        EavTestSuite::test_many_to_one::<
            ExampleAddressableContent,
            ExampleAttribute,
            ExampleEntityAttributeValueStorage<ExampleAttribute>,
        >(test_eav_storage(), &ExampleAttribute::default());
    }

    #[test]
    fn example_eav_range() {
        EavTestSuite::test_range::<
            ExampleAddressableContent,
            ExampleAttribute,
            ExampleEntityAttributeValueStorage<ExampleAttribute>,
        >(test_eav_storage(), &ExampleAttribute::default());
    }

    #[test]
    fn example_eav_prefixes() {
        EavTestSuite::test_multiple_attributes::<
            ExampleAddressableContent,
            ExampleAttribute,
            ExampleEntityAttributeValueStorage<ExampleAttribute>,
        >(test_eav_storage(), {
            let mut attrs: Vec<ExampleAttribute> = vec!["a_", "b_", "c_", "d_"]
                .into_iter()
                .map(|p| ExampleAttribute::WithPayload(p.to_string() + "one_to_many"))
                .collect();
            attrs.push(ExampleAttribute::WithoutPayload);
            attrs
        });
    }

    #[test]
    /// show AddressableContent implementation
    fn addressable_content_test() {
        // from_content()
        AddressableContentTestSuite::addressable_content_trait_test::<
            EntityAttributeValueIndex<ExampleAttribute>,
        >(test_eav_content(), test_eav(), test_eav_address());
    }

    #[test]
    /// show CAS round trip
    fn cas_round_trip_test() {
        let addressable_contents = vec![test_eav()];
        AddressableContentTestSuite::addressable_content_round_trip::<
            EntityAttributeValueIndex<ExampleAttribute>,
            ExampleContentAddressableStorage,
        >(addressable_contents, test_content_addressable_storage());
    }

    #[test]
    fn validate_attribute_paths() {
        assert!(EntityAttributeValueIndex::new(
            &test_eav_entity().address(),
            &ExampleAttribute::WithPayload("abc".into()),
            &test_eav_entity().address()
        )
        .is_ok());
        assert!(EntityAttributeValueIndex::new(
            &test_eav_entity().address(),
            &ExampleAttribute::WithPayload("abc123".into()),
            &test_eav_entity().address()
        )
        .is_ok());
        assert!(EntityAttributeValueIndex::new(
            &test_eav_entity().address(),
            &ExampleAttribute::WithPayload("123".into()),
            &test_eav_entity().address()
        )
        .is_ok());
    }
}