linkrs-query 0.1.0

Query engine, parser, and executor for Linkrs
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
//! Query registry — process-level query identity and cancellation.
//!
//! Every query execution must first obtain a unique, non-zero `QueryId` from
//! the [`QueryRegistry`].  The registry also holds the unified cancellation
//! source, deadline, and weak handles so that `KILL QUERY` works regardless
//! of which layer initiated cancellation.

use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Instant;

use dashmap::DashMap;

use super::transaction_scope::CancelReason;

// ── QueryId ─────────────────────────────────────────────────────────────────

/// Typed, non-zero query identifier.
///
/// Allocated by [`QueryRegistry::register`].  Zero is never a valid id.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct QueryId(pub u64);

impl std::fmt::Display for QueryId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Q{}", self.0)
    }
}

impl QueryId {
    pub const fn new(value: u64) -> Self {
        Self(value)
    }

    pub fn is_zero(&self) -> bool {
        self.0 == 0
    }

    pub fn as_u64(&self) -> u64 {
        self.0
    }

    pub fn as_i64(&self) -> i64 {
        self.0 as i64
    }
}

// ── QueryMetadata ───────────────────────────────────────────────────────────

/// Metadata stored in the registry for each active query.
#[derive(Debug, Clone)]
pub struct QueryMetadata {
    pub query_id: QueryId,
    pub session_id: Option<i64>,
    pub user_name: Option<String>,
    pub space_name: Option<String>,
    pub query_text: Option<String>,
    pub start_time: Instant,
}

// ── QueryRegistry ───────────────────────────────────────────────────────────

/// Process-level query registry.
///
/// - Allocates unique, non-zero `QueryId` values.
/// - Tracks active queries with metadata and weak runtime handles.
/// - Provides unified cancellation through a per-query `CancelReason`.
/// - Responsible for clean teardown: once teardown completes, the query
///   is removed from the registry.
#[derive(Debug)]
pub struct QueryRegistry {
    next_id: AtomicU64,
    active: DashMap<QueryId, QueryEntry>,
}

#[derive(Debug)]
struct QueryEntry {
    _metadata: QueryMetadata,
    cancel_reason: Arc<parking_lot::Mutex<Option<CancelReason>>>,
}

impl QueryRegistry {
    pub fn new() -> Self {
        Self {
            next_id: AtomicU64::new(1),
            active: DashMap::new(),
        }
    }

    /// Register a new query and obtain its unique `QueryId`.
    ///
    /// The caller should later call [`Self::unregister`] once the query
    /// lifecycle ends (after teardown).
    pub fn register(&self, metadata: QueryMetadata) -> (QueryId, CancelToken) {
        self.register_with_token(QueryId(0), metadata, None)
    }

    /// Register a query under a caller-provided ID.
    ///
    /// Used to honor a request-scoped `query_id` (e.g. one supplied by the
    /// server layer).  If the requested ID is already active or zero, the
    /// registry falls back to allocating a fresh unique ID so that identity
    /// uniqueness is never violated.
    pub fn register_with_id(
        &self,
        requested: QueryId,
        metadata: QueryMetadata,
    ) -> (QueryId, CancelToken) {
        self.register_with_token(requested, metadata, None)
    }

    /// Register a query with an externally-owned cancellation token.
    ///
    /// When `external_token` is provided, the registry entry shares the same
    /// underlying cancellation state, so a `CancelToken::cancel` performed by
    /// the owner (e.g. [`QueryContext::mark_killed`]) is immediately visible
    /// through the registry and vice versa.  This is the single-token
    /// convergence point for KILL QUERY / cancellation.
    pub fn register_with_token(
        &self,
        requested: QueryId,
        metadata: QueryMetadata,
        external_token: Option<CancelToken>,
    ) -> (QueryId, CancelToken) {
        let id = if requested.is_zero() || self.active.contains_key(&requested) {
            self.allocate_id()
        } else {
            requested
        };
        let token = external_token.unwrap_or_default();
        let reason = token.0.clone();

        self.active.insert(
            id,
            QueryEntry {
                _metadata: QueryMetadata {
                    query_id: id,
                    ..metadata
                },
                cancel_reason: reason,
            },
        );

        (id, token)
    }

    /// Unregister a completed query.
    ///
    /// Returns `true` if the query was actually registered, `false` if it
    /// was already unknown (harmless — may happen after forced removal).
    pub fn unregister(&self, query_id: QueryId) -> bool {
        self.active.remove(&query_id).is_some()
    }

    /// Cancel a query with a reason.
    ///
    /// Returns the first reason that was set (idempotent — subsequent calls
    /// for the same query return `None`).
    pub fn cancel(&self, query_id: QueryId, reason: CancelReason) -> Option<CancelReason> {
        if let Some(entry) = self.active.get(&query_id) {
            let mut slot = entry.cancel_reason.lock();
            if slot.is_some() {
                None // first reason wins
            } else {
                *slot = Some(reason);
                slot.clone()
            }
        } else {
            None
        }
    }

    /// Cancel all active queries with a given reason (used on shutdown).
    pub fn cancel_all(&self, reason: CancelReason) -> Vec<QueryId> {
        let ids: Vec<QueryId> = self.active.iter().map(|e| *e.key()).collect();
        for id in &ids {
            self.cancel(*id, reason.clone());
        }
        ids
    }

    /// Check whether a query has been cancelled and get the reason.
    pub fn cancellation_reason(&self, query_id: QueryId) -> Option<CancelReason> {
        self.active
            .get(&query_id)
            .and_then(|e| e.cancel_reason.lock().clone())
    }

    /// Number of currently active queries.
    pub fn active_count(&self) -> usize {
        self.active.len()
    }

    /// Iterate over all active query IDs.
    pub fn active_queries(&self) -> Vec<QueryId> {
        self.active.iter().map(|e| *e.key()).collect()
    }

    fn allocate_id(&self) -> QueryId {
        loop {
            let raw = self.next_id.fetch_add(1, Ordering::Relaxed);
            if raw != 0 {
                return QueryId(raw);
            }
        }
    }
}

impl Default for QueryRegistry {
    fn default() -> Self {
        Self::new()
    }
}

// ── CancelToken ─────────────────────────────────────────────────────────────

/// Thread-safe token that tracks whether a query was cancelled and why.
///
/// Shared with [`ExecutionRuntime`](super::runtime::ExecutionRuntime) and
/// checked by operators during execution.
#[derive(Debug, Clone)]
pub struct CancelToken(Arc<parking_lot::Mutex<Option<CancelReason>>>);

impl CancelToken {
    pub fn new() -> Self {
        Self(Arc::new(parking_lot::Mutex::new(None)))
    }

    /// Mark as cancelled.  Returns the _first_ reason that was set, or
    /// `None` if already cancelled.
    pub fn cancel(&self, reason: CancelReason) -> Option<CancelReason> {
        let mut slot = self.0.lock();
        if slot.is_some() {
            None
        } else {
            *slot = Some(reason);
            slot.clone()
        }
    }

    /// Check whether cancelled.
    pub fn is_cancelled(&self) -> bool {
        self.0.lock().is_some()
    }

    /// Get the cancellation reason, if any.
    pub fn reason(&self) -> Option<CancelReason> {
        self.0.lock().clone()
    }

    /// Clear the cancellation state (for reuse).
    pub fn clear(&self) {
        *self.0.lock() = None;
    }
}

impl Default for CancelToken {
    fn default() -> Self {
        Self::new()
    }
}

// ── QueryGuard ──────────────────────────────────────────────────────────────

/// RAII guard that unregisters a query from the registry on drop.
#[derive(Debug)]
pub struct QueryGuard {
    registry: Option<Arc<QueryRegistry>>,
    query_id: QueryId,
    finished: bool,
}

impl QueryGuard {
    pub fn new(registry: Arc<QueryRegistry>, query_id: QueryId) -> Self {
        Self {
            registry: Some(registry),
            query_id,
            finished: false,
        }
    }

    /// Unregister immediately without waiting for drop.
    pub fn finish(&mut self) {
        if !self.finished {
            self.finished = true;
            if let Some(ref reg) = self.registry {
                reg.unregister(self.query_id);
            }
        }
    }
}

impl Drop for QueryGuard {
    fn drop(&mut self) {
        if !self.finished {
            if let Some(ref reg) = self.registry {
                reg.unregister(self.query_id);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_register_and_unregister() {
        let reg = Arc::new(QueryRegistry::new());
        let meta = QueryMetadata {
            query_id: QueryId(0),
            session_id: Some(1),
            user_name: Some("test".to_string()),
            space_name: None,
            query_text: Some("MATCH (n) RETURN n".to_string()),
            start_time: Instant::now(),
        };
        let (id, _) = reg.register(meta);
        assert!(!id.is_zero());
        assert_eq!(reg.active_count(), 1);

        assert!(reg.unregister(id));
        assert_eq!(reg.active_count(), 0);
    }

    #[test]
    fn test_cancel_token() {
        let token = CancelToken::new();
        assert!(!token.is_cancelled());

        let first = token.cancel(CancelReason::UserKill);
        assert_eq!(first, Some(CancelReason::UserKill));
        assert!(token.is_cancelled());

        let second = token.cancel(CancelReason::Deadline);
        assert!(second.is_none());
        assert_eq!(token.reason(), Some(CancelReason::UserKill));
    }

    #[test]
    fn test_cancel_all() {
        let reg = Arc::new(QueryRegistry::new());
        let meta1 = QueryMetadata {
            query_id: QueryId(0),
            session_id: None,
            user_name: None,
            space_name: None,
            query_text: None,
            start_time: Instant::now(),
        };
        let meta2 = meta1.clone();
        reg.register(meta1);
        reg.register(meta2);

        let cancelled = reg.cancel_all(CancelReason::Shutdown);
        assert_eq!(cancelled.len(), 2);
        assert_eq!(reg.active_count(), 2);

        for id in &cancelled {
            assert_eq!(reg.cancellation_reason(*id), Some(CancelReason::Shutdown));
        }
    }

    #[test]
    fn test_query_guard_drop() {
        let reg = Arc::new(QueryRegistry::new());
        let meta = QueryMetadata {
            query_id: QueryId(0),
            session_id: None,
            user_name: None,
            space_name: None,
            query_text: None,
            start_time: Instant::now(),
        };
        let (id, _) = reg.register(meta);
        {
            let _guard = QueryGuard::new(reg.clone(), id);
            assert_eq!(reg.active_count(), 1);
        }
        assert_eq!(reg.active_count(), 0);
    }

    #[test]
    fn test_query_guard_finish() {
        let reg = Arc::new(QueryRegistry::new());
        let meta = QueryMetadata {
            query_id: QueryId(0),
            session_id: None,
            user_name: None,
            space_name: None,
            query_text: None,
            start_time: Instant::now(),
        };
        let (id, _) = reg.register(meta);
        let mut guard = QueryGuard::new(reg.clone(), id);
        guard.finish();
        assert_eq!(reg.active_count(), 0);
        // Drop after finish is safe (no double-unregister)
    }

    #[test]
    fn test_ids_are_unique_and_non_zero() {
        let reg = QueryRegistry::new();
        let mut ids = std::collections::HashSet::new();
        for _ in 0..100 {
            let meta = QueryMetadata {
                query_id: QueryId(0),
                session_id: None,
                user_name: None,
                space_name: None,
                query_text: None,
                start_time: Instant::now(),
            };
            let (id, _) = reg.register(meta);
            assert!(!id.is_zero());
            assert!(ids.insert(id));
        }
    }

    #[test]
    fn test_register_with_requested_id_honored() {
        let reg = Arc::new(QueryRegistry::new());
        let meta = QueryMetadata {
            query_id: QueryId(0),
            session_id: Some(7),
            user_name: None,
            space_name: None,
            query_text: None,
            start_time: Instant::now(),
        };
        let (id, _) = reg.register_with_id(QueryId(42), meta);
        assert_eq!(id, QueryId(42), "request-scoped id must be honored");
        assert_eq!(reg.active_count(), 1);

        let (id2, _) = reg.register_with_id(
            QueryId(42),
            QueryMetadata {
                query_id: QueryId(0),
                session_id: None,
                user_name: None,
                space_name: None,
                query_text: None,
                start_time: Instant::now(),
            },
        );
        assert_ne!(
            id2,
            QueryId(42),
            "a duplicate requested id must fall back to a fresh allocation"
        );
        assert!(!id2.is_zero());
        assert_eq!(reg.active_count(), 2);
    }

    #[test]
    fn test_register_with_zero_id_allocates() {
        let reg = Arc::new(QueryRegistry::new());
        let meta = QueryMetadata {
            query_id: QueryId(0),
            session_id: None,
            user_name: None,
            space_name: None,
            query_text: None,
            start_time: Instant::now(),
        };
        let (id, _) = reg.register_with_id(QueryId(0), meta);
        assert!(!id.is_zero(), "zero is never a valid id");
    }

    #[test]
    fn test_register_with_external_token_shares_state() {
        let reg = Arc::new(QueryRegistry::new());
        let external = CancelToken::new();
        let meta = QueryMetadata {
            query_id: QueryId(0),
            session_id: None,
            user_name: None,
            space_name: None,
            query_text: None,
            start_time: Instant::now(),
        };
        let (id, token) = reg.register_with_token(QueryId(0), meta.clone(), Some(external.clone()));
        assert!(!id.is_zero());
        assert!(!token.is_cancelled());
        assert_eq!(reg.cancellation_reason(id), None);

        // Cancelling the external token must be visible through the registry.
        external.cancel(CancelReason::UserKill);
        assert!(token.is_cancelled());
        assert_eq!(reg.cancellation_reason(id), Some(CancelReason::UserKill));

        // Cancelling via the registry must be visible through the token.
        let external2 = CancelToken::new();
        let (id2, token2) = reg.register_with_token(QueryId(0), meta, Some(external2.clone()));
        reg.cancel(id2, CancelReason::Deadline);
        assert!(token2.is_cancelled());
        assert_eq!(external2.reason(), Some(CancelReason::Deadline));
    }

    #[test]
    fn test_register_without_token_allocates_fresh() {
        let reg = Arc::new(QueryRegistry::new());
        let meta = QueryMetadata {
            query_id: QueryId(0),
            session_id: None,
            user_name: None,
            space_name: None,
            query_text: None,
            start_time: Instant::now(),
        };
        let (id, token) = reg.register_with_token(QueryId(0), meta, None);
        assert!(!token.is_cancelled());
        reg.cancel(id, CancelReason::Shutdown);
        assert!(token.is_cancelled());
    }
}