aingle_graph 0.7.1

Native GraphDB for AIngle - Semantic triple store with SPO indexes
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// Copyright 2019-2026 Apilium Technologies OÜ. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR Commercial

//! Query engine for the graph database.
//!
//! This module provides a `QueryBuilder` for pattern matching and a `TraversalBuilder`
//! for graph traversal.

use crate::{GraphStore, NodeId, Predicate, Result, Triple, Value};

/// A pattern for matching `(Subject, Predicate, Object)` triples.
///
/// A pattern specifies constraints on the components of a triple. Any component
/// can be `None`, which acts as a wildcard that matches any value.
///
/// # Examples
///
/// Match all triples with a specific subject:
///
/// ```
/// use aingle_graph::{TriplePattern, NodeId};
///
/// let pattern = TriplePattern::subject(NodeId::named("user:alice"));
/// ```
///
/// Match triples with specific subject and predicate:
///
/// ```
/// use aingle_graph::{TriplePattern, NodeId, Predicate};
///
/// let pattern = TriplePattern::subject(NodeId::named("user:alice"))
///     .with_predicate(Predicate::named("has_name"));
/// ```
///
/// Match all triples (wildcard pattern):
///
/// ```
/// use aingle_graph::TriplePattern;
///
/// let pattern = TriplePattern::any();
/// assert!(pattern.is_wildcard());
/// ```
#[derive(Debug, Clone, Default)]
pub struct TriplePattern {
    /// An optional constraint on the triple's subject.
    pub subject: Option<NodeId>,
    /// An optional constraint on the triple's predicate.
    pub predicate: Option<Predicate>,
    /// An optional constraint on the triple's object.
    pub object: Option<Value>,
}

impl TriplePattern {
    /// Creates a new pattern that matches any triple.
    ///
    /// This is equivalent to a wildcard pattern with no constraints.
    ///
    /// # Examples
    ///
    /// ```
    /// use aingle_graph::TriplePattern;
    ///
    /// let pattern = TriplePattern::any();
    /// assert!(pattern.is_wildcard());
    /// ```
    pub fn any() -> Self {
        Self::default()
    }

    /// Creates a new pattern that matches a specific subject.
    ///
    /// # Examples
    ///
    /// ```
    /// use aingle_graph::{TriplePattern, NodeId};
    ///
    /// let pattern = TriplePattern::subject(NodeId::named("user:alice"));
    /// ```
    pub fn subject(subject: NodeId) -> Self {
        Self {
            subject: Some(subject),
            ..Default::default()
        }
    }

    /// Creates a new pattern that matches a specific predicate.
    pub fn predicate(predicate: Predicate) -> Self {
        Self {
            predicate: Some(predicate),
            ..Default::default()
        }
    }

    /// Creates a new pattern that matches a specific object.
    pub fn object(object: Value) -> Self {
        Self {
            object: Some(object),
            ..Default::default()
        }
    }

    /// Adds a subject constraint to the pattern.
    pub fn with_subject(mut self, subject: NodeId) -> Self {
        self.subject = Some(subject);
        self
    }

    /// Adds a predicate constraint to the pattern.
    pub fn with_predicate(mut self, predicate: Predicate) -> Self {
        self.predicate = Some(predicate);
        self
    }

    /// Adds an object constraint to the pattern.
    pub fn with_object(mut self, object: Value) -> Self {
        self.object = Some(object);
        self
    }

    /// Returns `true` if the given [`Triple`] matches this pattern.
    ///
    /// A triple matches the pattern if all non-None constraints are satisfied.
    ///
    /// # Examples
    ///
    /// ```
    /// use aingle_graph::{Triple, TriplePattern, NodeId, Predicate, Value};
    ///
    /// let triple = Triple::new(
    ///     NodeId::named("user:alice"),
    ///     Predicate::named("has_name"),
    ///     Value::literal("Alice"),
    /// );
    ///
    /// let pattern = TriplePattern::subject(NodeId::named("user:alice"));
    /// assert!(pattern.matches(&triple));
    ///
    /// let wrong_pattern = TriplePattern::subject(NodeId::named("user:bob"));
    /// assert!(!wrong_pattern.matches(&triple));
    /// ```
    pub fn matches(&self, triple: &Triple) -> bool {
        if let Some(ref s) = self.subject {
            if &triple.subject != s {
                return false;
            }
        }
        if let Some(ref p) = self.predicate {
            if &triple.predicate != p {
                return false;
            }
        }
        if let Some(ref o) = self.object {
            if &triple.object != o {
                return false;
            }
        }
        true
    }

    /// Returns `true` if all components (subject, predicate, object) of the pattern are specified.
    pub fn is_exact(&self) -> bool {
        self.subject.is_some() && self.predicate.is_some() && self.object.is_some()
    }

    /// Returns `true` if the pattern is a wildcard (all components are `None`).
    pub fn is_wildcard(&self) -> bool {
        self.subject.is_none() && self.predicate.is_none() && self.object.is_none()
    }
}

/// The result of a query execution.
///
/// Contains the matched triples along with metadata about the result set,
/// including the total count and whether there are more results available.
///
/// # Examples
///
/// ```
/// use aingle_graph::{GraphDB, Triple, NodeId, Predicate, Value};
///
/// # fn main() -> Result<(), aingle_graph::Error> {
/// let db = GraphDB::memory()?;
///
/// db.insert(Triple::new(
///     NodeId::named("user:alice"),
///     Predicate::named("has_name"),
///     Value::literal("Alice"),
/// ))?;
///
/// let result = db.query()
///     .subject(NodeId::named("user:alice"))
///     .execute()?;
///
/// assert_eq!(result.len(), 1);
/// assert!(!result.is_empty());
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct QueryResult {
    /// The list of triples that matched the query, up to the specified limit.
    pub triples: Vec<Triple>,
    /// The total number of triples that matched the query, ignoring any limit.
    pub total_count: usize,
    /// `true` if there are more results available beyond the returned `triples`.
    pub has_more: bool,
}

impl QueryResult {
    /// Creates a new `QueryResult`.
    pub fn new(triples: Vec<Triple>) -> Self {
        let total_count = triples.len();
        Self {
            triples,
            total_count,
            has_more: false,
        }
    }

    /// Returns a reference to the first triple in the result set, if any.
    pub fn first(&self) -> Option<&Triple> {
        self.triples.first()
    }

    /// Returns `true` if the result set is empty.
    pub fn is_empty(&self) -> bool {
        self.triples.is_empty()
    }

    /// Returns the number of triples in the current result set.
    pub fn len(&self) -> usize {
        self.triples.len()
    }
}

/// A builder for constructing and executing queries against a [`GraphStore`].
///
/// Provides a fluent API for building pattern-based queries with optional
/// pagination through limit and offset.
///
/// # Examples
///
/// Basic query with subject constraint:
///
/// ```
/// use aingle_graph::{GraphDB, Triple, NodeId, Predicate, Value};
///
/// # fn main() -> Result<(), aingle_graph::Error> {
/// let db = GraphDB::memory()?;
///
/// db.insert(Triple::new(
///     NodeId::named("user:alice"),
///     Predicate::named("has_age"),
///     Value::integer(30),
/// ))?;
///
/// let results = db.query()
///     .subject(NodeId::named("user:alice"))
///     .execute()?;
///
/// assert_eq!(results.len(), 1);
/// # Ok(())
/// # }
/// ```
///
/// Query with multiple constraints and pagination:
///
/// ```
/// use aingle_graph::{GraphDB, Triple, NodeId, Predicate, Value};
///
/// # fn main() -> Result<(), aingle_graph::Error> {
/// let db = GraphDB::memory()?;
///
/// // Insert multiple triples
/// for i in 0..20 {
///     db.insert(Triple::new(
///         NodeId::named(format!("user:{}", i)),
///         Predicate::named("has_type"),
///         Value::literal("user"),
///     ))?;
/// }
///
/// let results = db.query()
///     .predicate(Predicate::named("has_type"))
///     .limit(10)
///     .offset(5)
///     .execute()?;
///
/// assert_eq!(results.len(), 10);
/// assert_eq!(results.total_count, 20);
/// assert!(results.has_more);
/// # Ok(())
/// # }
/// ```
pub struct QueryBuilder<'a> {
    store: &'a GraphStore,
    pattern: TriplePattern,
    limit: Option<usize>,
    offset: usize,
}

impl<'a> QueryBuilder<'a> {
    /// Creates a new `QueryBuilder` for a given `GraphStore`.
    pub fn new(store: &'a GraphStore) -> Self {
        Self {
            store,
            pattern: TriplePattern::default(),
            limit: None,
            offset: 0,
        }
    }

    /// Adds a subject constraint to the query.
    ///
    /// # Examples
    ///
    /// ```
    /// use aingle_graph::{GraphDB, NodeId};
    ///
    /// # fn main() -> Result<(), aingle_graph::Error> {
    /// let db = GraphDB::memory()?;
    ///
    /// let results = db.query()
    ///     .subject(NodeId::named("user:alice"))
    ///     .execute()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn subject(mut self, subject: NodeId) -> Self {
        self.pattern.subject = Some(subject);
        self
    }

    /// Adds a predicate constraint to the query.
    pub fn predicate(mut self, predicate: Predicate) -> Self {
        self.pattern.predicate = Some(predicate);
        self
    }

    /// Adds an object constraint to the query.
    pub fn object(mut self, object: Value) -> Self {
        self.pattern.object = Some(object);
        self
    }

    /// Sets the maximum number of results to return.
    ///
    /// # Examples
    ///
    /// ```
    /// use aingle_graph::{GraphDB, Predicate};
    ///
    /// # fn main() -> Result<(), aingle_graph::Error> {
    /// let db = GraphDB::memory()?;
    ///
    /// let results = db.query()
    ///     .predicate(Predicate::named("has_name"))
    ///     .limit(10)
    ///     .execute()?;
    ///
    /// assert!(results.len() <= 10);
    /// # Ok(())
    /// # }
    /// ```
    pub fn limit(mut self, limit: usize) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Sets the number of results to skip from the beginning.
    ///
    /// Useful for pagination in combination with [`limit`](Self::limit).
    ///
    /// # Examples
    ///
    /// ```
    /// use aingle_graph::{GraphDB, Predicate};
    ///
    /// # fn main() -> Result<(), aingle_graph::Error> {
    /// let db = GraphDB::memory()?;
    ///
    /// // Get second page of results
    /// let results = db.query()
    ///     .predicate(Predicate::named("has_name"))
    ///     .limit(10)
    ///     .offset(10)
    ///     .execute()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn offset(mut self, offset: usize) -> Self {
        self.offset = offset;
        self
    }

    /// Executes the constructed query.
    pub fn execute(self) -> Result<QueryResult> {
        let mut triples = self.store.find(self.pattern)?;
        let total_count = triples.len();

        // Apply offset
        if self.offset > 0 {
            if self.offset >= triples.len() {
                triples.clear();
            } else {
                triples = triples.into_iter().skip(self.offset).collect();
            }
        }

        // Apply limit
        let has_more = if let Some(limit) = self.limit {
            let exceeded = triples.len() > limit;
            triples.truncate(limit);
            exceeded
        } else {
            false
        };

        Ok(QueryResult {
            triples,
            total_count,
            has_more,
        })
    }
}

/// A builder for performing graph traversals.
///
/// Traversals allow you to explore the graph starting from a node and following
/// relationships (predicates) to discover connected nodes.
///
/// # Examples
///
/// ```
/// use aingle_graph::{GraphDB, Triple, NodeId, Predicate};
///
/// # fn main() -> Result<(), aingle_graph::Error> {
/// let db = GraphDB::memory()?;
///
/// // Build a social graph
/// db.insert(Triple::link(
///     NodeId::named("alice"),
///     Predicate::named("knows"),
///     NodeId::named("bob"),
/// ))?;
///
/// db.insert(Triple::link(
///     NodeId::named("bob"),
///     Predicate::named("knows"),
///     NodeId::named("charlie"),
/// ))?;
///
/// // Traverse from alice following "knows" relationships
/// let reachable = db.traverse(
///     &NodeId::named("alice"),
///     &[Predicate::named("knows")],
/// )?;
///
/// assert!(reachable.contains(&NodeId::named("bob")));
/// assert!(reachable.contains(&NodeId::named("charlie")));
/// # Ok(())
/// # }
/// ```
pub struct TraversalBuilder<'a> {
    store: &'a GraphStore,
    start: NodeId,
    predicates: Vec<Predicate>,
    max_depth: usize,
    follow_inverse: bool,
}

impl<'a> TraversalBuilder<'a> {
    /// Creates a new traversal builder starting from a specific node.
    pub fn from(store: &'a GraphStore, start: NodeId) -> Self {
        Self {
            store,
            start,
            predicates: Vec::new(),
            max_depth: 10,
            follow_inverse: false,
        }
    }

    /// Adds a predicate to follow during the traversal.
    pub fn follow(mut self, predicate: Predicate) -> Self {
        self.predicates.push(predicate);
        self
    }

    /// Adds multiple predicates to follow during the traversal.
    pub fn follow_all(mut self, predicates: Vec<Predicate>) -> Self {
        self.predicates.extend(predicates);
        self
    }

    /// Sets the maximum depth for the traversal.
    pub fn max_depth(mut self, depth: usize) -> Self {
        self.max_depth = depth;
        self
    }

    /// Configures the traversal to also follow inverse relationships (from object to subject).
    pub fn bidirectional(mut self) -> Self {
        self.follow_inverse = true;
        self
    }

    /// Executes the traversal.
    pub fn execute(self) -> Result<Vec<NodeId>> {
        self.store.traverse(&self.start, &self.predicates)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_pattern_matches() {
        let triple = Triple::new(
            NodeId::named("user:alice"),
            Predicate::named("has_name"),
            Value::literal("Alice"),
        );

        // Wildcard matches everything
        assert!(TriplePattern::any().matches(&triple));

        // Subject match
        assert!(TriplePattern::subject(NodeId::named("user:alice")).matches(&triple));
        assert!(!TriplePattern::subject(NodeId::named("user:bob")).matches(&triple));

        // Predicate match
        assert!(TriplePattern::predicate(Predicate::named("has_name")).matches(&triple));
        assert!(!TriplePattern::predicate(Predicate::named("has_age")).matches(&triple));

        // Object match
        assert!(TriplePattern::object(Value::literal("Alice")).matches(&triple));
        assert!(!TriplePattern::object(Value::literal("Bob")).matches(&triple));

        // Combined match
        let pattern = TriplePattern::subject(NodeId::named("user:alice"))
            .with_predicate(Predicate::named("has_name"));
        assert!(pattern.matches(&triple));
    }

    #[test]
    fn test_pattern_is_exact() {
        let partial = TriplePattern::subject(NodeId::named("a"));
        assert!(!partial.is_exact());

        let exact = TriplePattern::subject(NodeId::named("a"))
            .with_predicate(Predicate::named("b"))
            .with_object(Value::literal("c"));
        assert!(exact.is_exact());
    }

    #[test]
    fn test_query_result() {
        let t1 = Triple::new(
            NodeId::named("a"),
            Predicate::named("p"),
            Value::literal("b"),
        );
        let t2 = Triple::new(
            NodeId::named("c"),
            Predicate::named("p"),
            Value::literal("d"),
        );

        let result = QueryResult::new(vec![t1.clone(), t2]);
        assert_eq!(result.len(), 2);
        assert!(!result.is_empty());
        assert_eq!(result.first().unwrap().subject, t1.subject);
    }
}