atomic_lib 0.41.0-beta.3

Library for creating, storing, querying, validating and converting Atomic Data.
Documentation
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//! In-memory store of Atomic data.
//! This provides many methods for finding, changing, serializing and parsing Atomic Data.

use crate::agents::Agent;
use crate::storelike::QueryResult;
use crate::Value;
use crate::{atoms::Atom, storelike::Storelike, Subject};
use crate::{errors::AtomicResult, Resource};
use async_trait::async_trait;
use std::{collections::HashMap, sync::Arc, sync::Mutex};

/// The in-memory store of data, containing the Resources, Properties and Classes
/// It uses the `default_agent` as the default client.
#[derive(Clone)]
pub struct Store {
    // The store currently holds two stores - that is not ideal
    hashmap: Arc<Mutex<HashMap<String, Resource>>>,
    default_agent: Arc<Mutex<Option<crate::agents::Agent>>>,
    /// Maps hosts to Drive DIDs
    drive_mappings: Arc<Mutex<HashMap<String, String>>>,
    /// The base domain of the store
    pub base_domain: Arc<Mutex<Option<String>>>,
}

impl Store {
    /// Creates an empty Store.
    /// Run `.populate()` to get useful standard models loaded into your store.
    pub async fn init() -> AtomicResult<Store> {
        let store = Store {
            hashmap: Arc::new(Mutex::new(HashMap::new())),
            default_agent: Arc::new(Mutex::new(None)),
            drive_mappings: Arc::new(Mutex::new(HashMap::new())),
            base_domain: Arc::new(Mutex::new(None)),
        };
        crate::populate::populate_base_models(&store).await?;
        Ok(store)
    }

    /// Sets the base URL of the store.
    pub fn set_base_url(&self, url: &str) {
        self.base_domain.lock().unwrap().replace(url.to_string());
    }

    /// Triple Pattern Fragments interface.
    /// Use this for most queries, e.g. finding all items with some property / value combination.
    /// Returns an empty array if nothing is found.
    // Very costly, slow implementation.
    // Does not assume any indexing.
    async fn tpf(
        &self,
        q_subject: Option<&str>,
        q_property: Option<&str>,
        q_value: Option<&Value>,
        // Whether resources from outside the store should be searched through
        include_external: bool,
    ) -> AtomicResult<Vec<Atom>> {
        let mut vec: Vec<Atom> = Vec::new();

        let hassub = q_subject.is_some();
        let hasprop = q_property.is_some();
        let hasval = q_value.is_some();

        // Simply return all the atoms
        if !hassub && !hasprop && !hasval {
            for resource in self.all_resources(include_external) {
                for (property, value) in resource.get_propvals() {
                    vec.push(Atom::new(
                        resource.get_subject().clone(),
                        property.clone(),
                        value.clone(),
                    ))
                }
            }
            return Ok(vec);
        }

        // Find atoms matching the TPF query in a single resource
        let mut find_in_resource = |resource: &Resource| {
            let subj = resource.get_subject();
            for (prop, val) in resource.get_propvals().iter() {
                if hasprop && q_property.as_ref().unwrap() == prop {
                    if hasval {
                        if val.contains_value(q_value.unwrap()) {
                            vec.push(Atom::new(subj.clone(), prop.into(), val.clone()))
                        }
                        break;
                    } else {
                        vec.push(Atom::new(subj.clone(), prop.into(), val.clone()))
                    }
                    break;
                } else if hasval && !hasprop && val.contains_value(q_value.unwrap()) {
                    vec.push(Atom::new(subj.clone(), prop.into(), val.clone()))
                }
            }
        };

        match q_subject {
            Some(sub) => {
                let s: Subject = sub.into();
                match self.get_resource(&s).await {
                    Ok(resource) => {
                        if hasprop | hasval {
                            find_in_resource(&resource);
                            Ok(vec)
                        } else {
                            Ok(resource.to_atoms())
                        }
                    }
                    Err(_) => Ok(vec),
                }
            }
            None => {
                for resource in self.all_resources(include_external) {
                    find_in_resource(&resource);
                }
                Ok(vec)
            }
        }
    }
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl Storelike for Store {
    async fn add_atoms(&self, atoms: Vec<Atom>) -> AtomicResult<()> {
        // Start with a nested HashMap, containing only strings.
        let mut map: HashMap<Subject, Resource> = HashMap::new();
        for atom in atoms {
            let subject = atom.subject;
            let property = atom.property;
            let value = atom.value;
            match map.get_mut(&subject) {
                // Resource exists in map
                Some(resource) => {
                    resource.set_unsafe(property, value)?;
                }
                // Resource does not exist
                None => {
                    let mut resource = Resource::new(subject.to_string());
                    resource.set_unsafe(property, value)?;
                    map.insert(subject, resource);
                }
            }
        }
        for (_subject, resource) in map.iter() {
            self.add_resource(resource).await?
        }
        Ok(())
    }

    fn add_drive_mapping(&self, host: &str, drive_did: &Value) -> AtomicResult<()> {
        self.drive_mappings
            .lock()
            .unwrap()
            .insert(host.to_string(), drive_did.to_string());
        Ok(())
    }

    fn remove_drive_mapping(&self, host: &str) -> AtomicResult<()> {
        self.drive_mappings.lock().unwrap().remove(host);
        Ok(())
    }

    fn get_base_domain(&self) -> Option<String> {
        self.base_domain.lock().unwrap().clone()
    }

    fn set_base_url(&self, url: &str) {
        self.set_base_url(url);
    }

    async fn add_resource_opts(
        &self,
        resource: &Resource,
        check_required_props: bool,
        update_index: bool,
        overwrite_existing: bool,
    ) -> AtomicResult<()> {
        if check_required_props {
            resource.check_required_props(self).await?;
        }
        if !overwrite_existing {
            let subject = resource.get_subject();
            if let Some(_r) = self.hashmap.lock().unwrap().get(&subject.to_string()) {
                return Err(format!("{} already present, will not overwrite.", subject).into());
            }
        }
        let _ = update_index;
        // This store has no index, so we don't need to update it.
        self.hashmap
            .lock()
            .unwrap()
            .insert(resource.get_subject().to_string(), resource.clone());
        Ok(())
    }

    // TODO: Fix this for local stores, include external does not make sense here
    fn all_resources(&self, _include_external: bool) -> Box<dyn Iterator<Item = Resource> + Send> {
        Box::new(self.hashmap.lock().unwrap().clone().into_values())
    }

    fn get_default_agent(&self) -> AtomicResult<Agent> {
        match self.default_agent.lock().unwrap().to_owned() {
            Some(agent) => Ok(agent),
            None => Err("No default agent has been set.".into()),
        }
    }

    async fn get_resource(&self, subject: &Subject) -> AtomicResult<Resource> {
        let normalized = self.normalize_subject(subject);
        let subject_str = normalized.to_string();
        if let Some(resource) = self.hashmap.lock().unwrap().get(&subject_str) {
            return Ok(resource.clone());
        }

        if let Ok(resource) = self
            .fetch_resource(&subject_str, self.get_default_agent().ok().as_ref())
            .await
        {
            return Ok(resource);
        };

        self.handle_not_found(
            &subject_str,
            "Not found in HashMap.".into(),
            self.get_default_agent().ok().as_ref(),
        )
        .await
    }

    fn has_stored_resource(&self, subject: &Subject) -> bool {
        let normalized = self.normalize_subject(subject);
        self.hashmap
            .lock()
            .unwrap()
            .contains_key(&normalized.to_string())
    }

    async fn remove_resource(&self, subject: &Subject) -> AtomicResult<()> {
        let subject_str = subject.to_string();
        let resource = self.get_resource(subject).await?;
        for child in resource.get_children(self).await? {
            Box::pin(self.remove_resource(child.get_subject())).await?;
        }
        self.hashmap
            .lock()
            .unwrap()
            .remove_entry(&subject_str)
            .ok_or(format!(
                "Resource {} could not be deleted, because it is not found",
                subject
            ))?;
        Ok(())
    }

    fn set_default_agent(&self, agent: Agent) {
        self.default_agent.lock().unwrap().replace(agent);
    }

    async fn query(
        &self,
        q: &crate::storelike::Query,
    ) -> AtomicResult<crate::storelike::QueryResult> {
        let atoms = self
            .tpf(
                None,
                q.property.as_deref(),
                q.value.as_ref(),
                q.include_external,
            )
            .await?;

        // Remove duplicate subjects
        let mut subjects_deduplicated: Vec<Subject> = atoms
            .iter()
            .map(|atom| atom.subject.clone())
            .collect::<std::collections::HashSet<Subject>>()
            .into_iter()
            .collect();

        // Sort by subject, better than no sorting
        subjects_deduplicated.sort_by(|a, b| a.as_str().cmp(b.as_str()));

        // WARNING: Entering expensive loop!
        // This is needed for sorting, authorization and including nested resources.
        // It could be skipped if there is no authorization and sorting requirement.
        let mut resources = Vec::new();
        for subject in subjects_deduplicated.iter() {
            // These nested resources are not fully calculated - they will be presented as -is
            match self
                .get_resource_extended(subject, true, &q.for_agent)
                .await
            {
                Ok(resource) => {
                    resources.push(resource.to_single());
                }
                Err(e) => match &e.error_type {
                    crate::AtomicErrorType::NotFoundError => {}
                    crate::AtomicErrorType::UnauthorizedError => {}
                    _other => {
                        return Err(
                            format!("Error when getting resource in collection: {}", e).into()
                        )
                    }
                },
            }
        }

        if let Some(sort) = &q.sort_by {
            resources = crate::collections::sort_resources(resources, sort, q.sort_desc);
        }
        let subjects: Vec<Subject> = resources.iter().map(|r| r.get_subject().clone()).collect();

        Ok(QueryResult {
            aggregates: Vec::new(),
            count: atoms.len(),
            subjects,
            resources,
        })
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::{agents::ForAgent, urls, Value};

    async fn init_store() -> Store {
        let store = Store::init().await.unwrap();
        store.populate().await.unwrap();
        store
    }

    #[tokio::test]
    async fn populate_base_models() {
        let store = Store::init().await.unwrap();
        crate::populate::populate_base_models(&store).await.unwrap();
        let property = store.get_property(urls::DESCRIPTION).await.unwrap();
        assert_eq!(property.shortname, "description")
    }

    #[tokio::test]
    async fn single_get_empty_server_to_class() {
        let store = Store::init().await.unwrap();
        crate::populate::populate_base_models(&store).await.unwrap();
        // Should fetch the agent class, since it's not in the store
        let agent = store.get_class(urls::AGENT).await.unwrap();
        assert_eq!(agent.shortname, "agent")
    }

    #[tokio::test]
    async fn get_full_resource_and_shortname() {
        let store = init_store().await;
        let resource = store.get_resource(&urls::CLASS.into()).await.unwrap();
        let shortname = resource
            .get_shortname("shortname", &store)
            .await
            .unwrap()
            .to_string();
        assert!(shortname == "class");
    }

    #[tokio::test]
    async fn serialize() {
        let store = init_store().await;
        let subject = urls::CLASS;
        let resource = store.get_resource(&subject.into()).await.unwrap();
        resource.to_json_ad(None).unwrap();
    }

    #[tokio::test]
    async fn tpf() {
        let store = init_store().await;
        let val = &Value::Slug("class".into());
        let val_url = &Value::AtomicUrl(urls::CLASS.into());
        // All atoms
        let atoms = store.tpf(None, None, None, true).await.unwrap();
        assert!(atoms.len() > 10);
        // Find by subject
        let atoms = store
            .tpf(Some(urls::CLASS), None, None, true)
            .await
            .unwrap();
        assert_eq!(atoms.len(), 6);
        // Find by value
        let atoms = store.tpf(None, None, Some(val), true).await.unwrap();
        assert_eq!(atoms[0].subject, urls::CLASS);
        assert_eq!(atoms.len(), 1);
        // Find by property and value
        let atoms = store
            .tpf(None, Some(urls::SHORTNAME), Some(val), true)
            .await
            .unwrap();
        assert!(atoms[0].subject == urls::CLASS);
        assert_eq!(atoms.len(), 1);
        // Find item in array
        let atoms = store
            .tpf(None, Some(urls::IS_A), Some(val_url), true)
            .await
            .unwrap();
        println!("{:?}", atoms);
        assert!(atoms.len() > 3, "Find item in array");
    }

    #[tokio::test]
    async fn path() {
        let store = init_store().await;
        let res = store
            .get_path(
                "https://atomicdata.dev/classes/Class shortname",
                None,
                &ForAgent::Sudo,
            )
            .await
            .unwrap();
        match res {
            crate::storelike::PathReturn::Subject(_) => panic!("Should be an Atom"),
            crate::storelike::PathReturn::Atom(atom) => {
                assert_eq!(atom.value.to_string(), "class");
            }
        }
        let res = store
            .get_path(
                "https://atomicdata.dev/classes/Class requires 0",
                None,
                &ForAgent::Sudo,
            )
            .await
            .unwrap();
        match res {
            crate::storelike::PathReturn::Subject(sub) => {
                assert_eq!(sub, urls::SHORTNAME);
            }
            crate::storelike::PathReturn::Atom(_) => panic!("Should be an Subject"),
        }
    }

    #[test]
    fn get_external_resource() {
        let runtime = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .unwrap();
        runtime.block_on(async {
            let store = Store::init().await.unwrap();
            store.populate().await.unwrap();
            // If nothing happens - this night be deadlock.
            store.get_resource(&urls::CLASS.into()).await.unwrap();
        });
    }

    #[tokio::test]
    #[should_panic]
    async fn path_fail() {
        let store = init_store().await;
        store
            .get_path(
                "https://atomicdata.dev/classes/Class requires isa description",
                None,
                &ForAgent::Sudo,
            )
            .await
            .unwrap();
    }

    #[tokio::test]
    #[should_panic]
    async fn path_fail2() {
        let store = init_store().await;
        store
            .get_path(
                "https://atomicdata.dev/classes/Class requires requires",
                None,
                &ForAgent::Sudo,
            )
            .await
            .unwrap();
    }
}