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
480
481
482
483
484
// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved.
//---------------------------------------------------------------

use {
    crate::{
        database_call,
        rdfox_api::{
            CDataStoreConnection,
            CDataStoreConnection_destroy,
            CDataStoreConnection_evaluateUpdate,
            CDataStoreConnection_getName,
            CDataStoreConnection_getUniqueID,
            CDataStoreConnection_importAxiomsFromTriples,
            CDataStoreConnection_importDataFromFile,
            CStatementResult,
            CUpdateType,
        },
        DataStore,
        FactDomain,
        Namespaces,
        Parameters,
        ServerConnection,
        Statement,
        Streamer,
        Transaction,
    },
    colored::Colorize,
    fancy_regex::Regex,
    ignore::{types::TypesBuilder, WalkBuilder},
    indoc::formatdoc,
    iref::Iri,
    mime::Mime,
    rdf_store_rs::{
        consts::{
            DEFAULT_BASE_IRI,
            DEFAULT_GRAPH_RDFOX,
            LOG_TARGET_DATABASE,
            LOG_TARGET_FILES,
            TEXT_TURTLE,
        },
        Graph,
        Namespace,
        RDFStoreError,
    },
    std::{
        ffi::{CStr, CString},
        fmt::{Debug, Display, Formatter},
        io::Write,
        mem::MaybeUninit,
        ops::Deref,
        os::unix::ffi::OsStrExt,
        path::Path,
        ptr::{self, null_mut},
        sync::Arc,
        time::Instant,
    },
};

/// A connection to a given [`DataStore`].
#[derive(Debug)]
pub struct DataStoreConnection {
    pub data_store:        Arc<DataStore>,
    pub server_connection: Arc<ServerConnection>,
    pub(crate) inner:      *mut CDataStoreConnection,
    started_at:            Instant,
    pub number:            usize,
}

unsafe impl Sync for DataStoreConnection {}

unsafe impl Send for DataStoreConnection {}

impl Display for DataStoreConnection {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "connection #{} to {}",
            self.number, self.data_store
        )
    }
}

impl Drop for DataStoreConnection {
    fn drop(&mut self) {
        assert!(
            !self.inner.is_null(),
            "Could not drop datastore connection #{}",
            self.number
        );

        let duration = self.started_at.elapsed();

        let self_msg = format!("{self}");
        unsafe {
            CDataStoreConnection_destroy(self.inner.cast());
        }
        self.inner = null_mut();
        tracing::debug!(
            target: LOG_TARGET_DATABASE,
            duration = ?duration,
            "Dropped {self_msg}",
        );
    }
}

impl DataStoreConnection {
    pub(crate) fn new(
        server_connection: &Arc<ServerConnection>,
        data_store: &Arc<DataStore>,
        inner: *mut CDataStoreConnection,
    ) -> Self {
        Self {
            data_store: data_store.clone(),
            server_connection: server_connection.clone(),
            inner,
            started_at: Instant::now(),
            number: Self::get_number(),
        }
    }

    pub fn same(self: &Arc<Self>, other: &Arc<Self>) -> bool { self.number == other.number }

    fn get_number() -> usize {
        use std::sync::atomic::{AtomicUsize, Ordering};
        static COUNTER: AtomicUsize = AtomicUsize::new(1);
        COUNTER.fetch_add(1, Ordering::Relaxed)
    }

    pub fn get_id(&self) -> Result<String, RDFStoreError> {
        assert!(
            !self.inner.is_null(),
            "invalid datastore connection"
        );
        let mut name: *const std::os::raw::c_char = ptr::null();
        database_call!(
            "getting the name of a datastore connection",
            CDataStoreConnection_getName(self.inner, &mut name)
        )?;
        let c_str = unsafe { CStr::from_ptr(name) };
        Ok(c_str.to_str().unwrap().into())
    }

    pub fn get_unique_id(&self) -> Result<String, RDFStoreError> {
        assert!(
            !self.inner.is_null(),
            "invalid datastore connection"
        );
        let mut unique_id: *const std::os::raw::c_char = ptr::null();
        database_call!(
            "Getting the unique id of datastore connection",
            CDataStoreConnection_getUniqueID(self.inner, &mut unique_id)
        )?;
        let c_str = unsafe { CStr::from_ptr(unique_id) };
        Ok(c_str.to_str().unwrap().into())
    }

    /// Import RDF data from the given file into the given graph.
    ///
    /// NOTE: Only supports turtle files at the moment.
    pub fn import_data_from_file<P>(&self, file: P, graph: &Graph) -> Result<(), RDFStoreError>
    where P: AsRef<Path> {
        assert!(
            !self.inner.is_null(),
            "invalid datastore connection"
        );

        let rdf_file = file.as_ref().as_os_str().as_bytes();
        tracing::trace!(
            target: LOG_TARGET_DATABASE,
            conn = self.number,
            "Importing file {} into {:} of {:}",
            file.as_ref().display(),
            graph,
            self
        );

        let c_graph_name = graph.as_c_string()?;
        let file_name = CString::new(rdf_file).unwrap();
        let format_name = CString::new(TEXT_TURTLE.as_ref()).unwrap();

        database_call!(
            format!("Importing data from {file_name:?} (format={format_name:?})").as_str(),
            CDataStoreConnection_importDataFromFile(
                self.inner,
                c_graph_name.as_ptr() as *const std::os::raw::c_char,
                CUpdateType::UPDATE_TYPE_ADDITION,
                file_name.as_ptr() as *const std::os::raw::c_char,
                format_name.as_ptr() as *const std::os::raw::c_char,
            )
        )?;
        tracing::debug!(
            target: LOG_TARGET_DATABASE,
            conn = self.number,
            "Imported file {} into {:}",
            file.as_ref().display(),
            graph
        );
        Ok(())
    }

    pub fn import_axioms_from_triples(
        &self,
        source_graph: &Graph,
        target_graph: &Graph,
    ) -> Result<(), RDFStoreError> {
        assert!(
            !self.inner.is_null(),
            "invalid datastore connection"
        );

        let c_source_graph_name = source_graph.as_c_string()?;
        let c_target_graph_name = target_graph.as_c_string()?;

        database_call!(
            "importing axioms",
            CDataStoreConnection_importAxiomsFromTriples(
                self.inner,
                c_source_graph_name.as_ptr() as *const std::os::raw::c_char,
                false,
                c_target_graph_name.as_ptr() as *const std::os::raw::c_char,
                CUpdateType::UPDATE_TYPE_ADDITION,
            )
        )?;
        tracing::debug!(
            target: LOG_TARGET_DATABASE,
            conn = self.number,
            "Imported axioms from {:} into graph {:}",
            source_graph,
            target_graph
        );
        Ok(())
    }

    /// Read all RDF files (currently it supports .ttl and .nt files) from
    /// the given directory, applying ignore files like `.gitignore`.
    ///
    /// Returns the number of loaded files.
    ///
    /// TODO: Support all the types that RDFox supports (and more)
    /// TODO: Support '*.gz' files
    /// TODO: Parallelize appropriately in sync with number of threads that
    /// RDFox uses
    pub fn import_rdf_from_directory(
        &self,
        root: &Path,
        graph: &Graph,
    ) -> Result<u16, RDFStoreError> {
        let mut count = 0u16;
        let regex = Regex::new(r"^.*.ttl$").unwrap();

        tracing::debug!(
            target: LOG_TARGET_FILES,
            "Read all RDF files from directory {}",
            format!("{:?}", &root).green()
        );
        tracing::debug!(
            target: LOG_TARGET_FILES,
            "WalkBuilder::new({:?}), searching for {:?}",
            root,
            regex
        );

        let mut builder = TypesBuilder::new();
        builder.add("rdf", "*.nt").unwrap();
        builder.add("rdf", "*.ttl").unwrap();
        let file_types = builder.select("rdf").build().unwrap();

        let iter = WalkBuilder::new(root)
            .standard_filters(true)
            .ignore(false)
            .git_global(true)
            .git_ignore(true)
            .git_exclude(true)
            .follow_links(false)
            .parents(false)
            .threads(6)
            .types(file_types)
            .build();

        for rdf_file in iter {
            match rdf_file {
                Ok(dir_entry) => {
                    let file_type = dir_entry.file_type().unwrap();
                    if file_type.is_dir() {
                        continue
                    }
                    let rdf_file = dir_entry.path();
                    // tracing::debug!("entry {:?}", dir_entry);
                    self.import_data_from_file(rdf_file, graph)?;
                    count += 1;
                },
                Err(error) => {
                    tracing::error!(target: LOG_TARGET_FILES, "error {:?}", error);
                    return Err(RDFStoreError::WalkError(error))
                },
            }
        }
        Ok(count)
    }

    // noinspection DuplicatedCode
    pub fn evaluate_update(
        &self,
        statement: &Statement,
        parameters: &Parameters,
    ) -> Result<CStatementResult, RDFStoreError> {
        assert!(
            !self.inner.is_null(),
            "invalid datastore connection"
        );
        // let c_base_iri = if let Some(base_iri) = base_iri {
        //     CString::new(base_iri.as_str()).unwrap()
        // } else {
        //     CString::new(DEFAULT_BASE_IRI).unwrap()
        // };
        let statement_text = statement.as_c_string()?;
        let statement_text_len = statement_text.as_bytes().len();
        let mut statement_result = MaybeUninit::uninit();
        database_call!(
            "evaluating an update statement",
            CDataStoreConnection_evaluateUpdate(
                self.inner,
                statement_text.as_ptr(),
                statement_text_len,
                parameters.inner.as_ref().cast_const(),
                statement_result.as_mut_ptr(),
            )
        )?;
        let statement_result = unsafe { statement_result.assume_init() };
        tracing::trace!("Evaluated update statement: {statement_result:?}",);
        Ok(statement_result)
    }

    pub fn evaluate_to_stream<'a, W>(
        self: &Arc<Self>,
        writer: W,
        statement: &'a Statement,
        mime_type: &'static Mime,
        base_iri: Option<Iri>,
    ) -> Result<Streamer<'a, W>, RDFStoreError>
    where
        W: 'a + Write,
    {
        Streamer::run(
            self,
            writer,
            statement,
            mime_type,
            Namespace::declare_from_str(
                "base",
                base_iri
                    .as_ref()
                    .map(|iri| iri.as_str())
                    .unwrap_or_else(|| DEFAULT_BASE_IRI),
            ),
        )
    }

    pub fn get_triples_count(
        self: &Arc<Self>,
        tx: &Arc<Transaction>,
        fact_domain: FactDomain,
    ) -> Result<usize, RDFStoreError> {
        let default_graph = DEFAULT_GRAPH_RDFOX.deref().as_display_iri();
        Statement::new(
            &Namespaces::empty()?,
            formatdoc!(
                r##"
                SELECT ?graph ?s ?p ?o
                WHERE {{
                    {{
                        GRAPH ?graph {{ ?s ?p ?o }}
                    }} UNION {{
                        ?s ?p ?o .
                        BIND({default_graph} AS ?graph)
                    }}
                }}
            "##
            )
            .into(),
        )?
        .cursor(
            self,
            &Parameters::empty()?.fact_domain(fact_domain)?,
        )?
        .count(tx)
    }

    pub fn get_subjects_count(
        self: &Arc<Self>,
        tx: &Arc<Transaction>,
        fact_domain: FactDomain,
    ) -> Result<usize, RDFStoreError> {
        let default_graph = DEFAULT_GRAPH_RDFOX.deref().as_display_iri();
        Statement::new(
            &Namespaces::empty()?,
            formatdoc!(
                r##"
                SELECT DISTINCT ?subject
                WHERE {{
                    {{
                        GRAPH ?graph {{
                            ?subject ?p ?o
                        }}
                    }} UNION {{
                        ?subject ?p ?o .
                        BIND({default_graph} AS ?graph)
                    }}
                }}
            "##
            )
            .into(),
        )?
        .cursor(
            self,
            &Parameters::empty()?.fact_domain(fact_domain)?,
        )?
        .count(tx)
    }

    pub fn get_predicates_count(
        self: &Arc<Self>,
        tx: &Arc<Transaction>,
        fact_domain: FactDomain,
    ) -> Result<usize, RDFStoreError> {
        let default_graph = DEFAULT_GRAPH_RDFOX.deref().as_display_iri();
        Statement::new(
            &Namespaces::empty()?,
            formatdoc!(
                r##"
                SELECT DISTINCT ?predicate
                WHERE {{
                    {{
                        GRAPH ?graph {{
                            ?s ?predicate ?o
                        }}
                    }} UNION {{
                        ?s ?predicate ?o .
                        BIND({default_graph} AS ?graph)
                    }}
                }}
            "##
            )
            .into(),
        )?
        .cursor(
            self,
            &Parameters::empty()?.fact_domain(fact_domain)?,
        )?
        .count(tx)
    }

    pub fn get_ontologies_count(
        self: &Arc<Self>,
        tx: &Arc<Transaction>,
        fact_domain: FactDomain,
    ) -> Result<usize, RDFStoreError> {
        let default_graph = DEFAULT_GRAPH_RDFOX.deref().as_display_iri();
        Statement::new(
            &Namespaces::empty()?,
            formatdoc!(
                r##"
                SELECT DISTINCT ?ontology
                WHERE {{
                    {{
                        GRAPH ?graph {{
                            ?ontology a <http://www.w3.org/2002/07/owl#Ontology>
                        }}
                    }} UNION {{
                        ?ontology a <http://www.w3.org/2002/07/owl#Ontology>
                        BIND({default_graph} AS ?graph)
                    }}
                }}
                "##
            )
            .into(),
        )?
        .cursor(
            self,
            &Parameters::empty()?.fact_domain(fact_domain)?,
        )?
        .count(tx)
    }
}