spiresql 0.1.3

SQL interface for SpireDB
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
//! Kafka-like Consumer for SpireDB
//!
//! Supports subscribe/poll pattern, manual/auto commit, consumer groups, and PEL operations.
#![allow(dead_code)]

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

use kovan_map::HashMap;
use parking_lot::Mutex;

use super::cdc::CdcBuilder;
use super::client::{PendingResult, RespClient, StreamClient};
use super::config::ConsumerConfig;
use super::error::StreamError;
use super::interceptor::ConsumerInterceptor;
use super::types::{
    AutoClaimResult, ClaimOptions, ConsumerGroupMetadata, ConsumerRecord, IsolationLevel,
    OffsetAndMetadata, OffsetReset, PendingInfo, PendingOptions, TopicPartition,
};

/// Kafka-like Consumer for SpireDB
///
/// Supports subscribe/poll pattern, manual/auto commit, consumer groups, and PEL operations.
pub struct Consumer {
    config: ConsumerConfig,
    client: Arc<dyn StreamClient>,
    interceptors: Vec<Arc<dyn ConsumerInterceptor + Send + Sync>>,
    // Use parking_lot::Mutex for Vecs (not concurrent)
    subscription: Mutex<Vec<String>>,
    assignment: Mutex<Vec<TopicPartition>>,
    paused: Mutex<Vec<TopicPartition>>,
    // Use kovan_map::HashMap directly (lock-free concurrent hashmap)
    positions: HashMap<TopicPartition, u64>,
    last_poll_ids: HashMap<String, String>,
    // Metrics
    records_received: AtomicU64,
    bytes_received: AtomicU64,
}

/// Builder for Consumer configuration
#[derive(Default)]
pub struct ConsumerBuilder {
    config: ConsumerConfig,
    client: Option<Arc<dyn StreamClient>>,
    interceptors: Vec<Arc<dyn ConsumerInterceptor + Send + Sync>>,
}

impl ConsumerBuilder {
    /// Set SpireDB endpoints (comma-separated)
    pub fn bootstrap_servers(mut self, servers: impl Into<String>) -> Self {
        self.config.bootstrap_servers = servers.into();
        self
    }

    /// Set consumer group ID (enables offset management & rebalancing)
    pub fn group_id(mut self, id: impl Into<String>) -> Self {
        self.config.group_id = Some(id.into());
        self
    }

    /// Enable/disable auto-commit (default: true)
    pub fn auto_commit(mut self, enabled: bool) -> Self {
        self.config.auto_commit = enabled;
        self
    }

    /// Set auto-commit interval (ms)
    pub fn auto_commit_interval_ms(mut self, ms: u64) -> Self {
        self.config.auto_commit_interval = Duration::from_millis(ms);
        self
    }

    /// Set offset reset behavior when no committed offset exists
    pub fn auto_offset_reset(mut self, reset: OffsetReset) -> Self {
        self.config.auto_offset_reset = reset;
        self
    }

    /// Set isolation level for transactional reads
    pub fn isolation_level(mut self, level: IsolationLevel) -> Self {
        self.config.isolation_level = level;
        self
    }

    /// Set max records per poll
    pub fn max_poll_records(mut self, n: u32) -> Self {
        self.config.max_poll_records = n;
        self
    }

    /// Set max poll interval before rebalance (ms)
    pub fn max_poll_interval_ms(mut self, ms: u64) -> Self {
        self.config.max_poll_interval = Duration::from_millis(ms);
        self
    }

    /// Set session timeout (ms)
    pub fn session_timeout_ms(mut self, ms: u64) -> Self {
        self.config.session_timeout = Duration::from_millis(ms);
        self
    }

    /// Set fetch min bytes
    pub fn fetch_min_bytes(mut self, bytes: usize) -> Self {
        self.config.fetch_min_bytes = bytes;
        self
    }

    /// Set fetch max bytes
    pub fn fetch_max_bytes(mut self, bytes: usize) -> Self {
        self.config.fetch_max_bytes = bytes;
        self
    }

    /// Set custom client (for testing with mocks)
    pub fn client(mut self, client: Arc<dyn StreamClient>) -> Self {
        self.client = Some(client);
        self
    }

    /// Add an interceptor
    pub fn interceptor(mut self, interceptor: Arc<dyn ConsumerInterceptor + Send + Sync>) -> Self {
        self.interceptors.push(interceptor);
        self
    }

    /// Build the consumer
    pub async fn build(self) -> Result<Consumer, StreamError> {
        let client = self
            .client
            .unwrap_or_else(|| Arc::new(RespClient::new(&self.config.bootstrap_servers)));

        Ok(Consumer {
            config: self.config,
            client,
            interceptors: self.interceptors,
            subscription: Mutex::new(Vec::new()),
            assignment: Mutex::new(Vec::new()),
            paused: Mutex::new(Vec::new()),
            positions: HashMap::new(),
            last_poll_ids: HashMap::new(),
            records_received: AtomicU64::new(0),
            bytes_received: AtomicU64::new(0),
        })
    }
}

impl Consumer {
    /// Create a new consumer builder
    pub fn builder() -> ConsumerBuilder {
        ConsumerBuilder::default()
    }

    /// Subscribe to topics (triggers rebalance for group consumers)
    pub async fn subscribe(&self, topics: &[&str]) -> Result<(), StreamError> {
        let topic_strings: Vec<String> = topics.iter().map(|s| s.to_string()).collect();

        // If using consumer group, create groups on the server
        if let Some(group_id) = &self.config.group_id {
            for topic in topics {
                let start_id = match self.config.auto_offset_reset {
                    OffsetReset::Earliest => "0-0",
                    OffsetReset::Latest => "$",
                    OffsetReset::None => "0-0",
                };
                self.client.xgroup_create(topic, group_id, start_id)?;
            }
        }

        // Update local subscription state
        *self.subscription.lock() = topic_strings.clone();

        // Generate assignment (simplified: one partition per topic)
        *self.assignment.lock() = topic_strings
            .iter()
            .map(|t| TopicPartition::new(t, 0))
            .collect();

        // Initialize last poll IDs (kovan_map is concurrent, no lock needed)
        for topic in topics {
            if self.last_poll_ids.get(*topic).is_none() {
                self.last_poll_ids
                    .insert(topic.to_string(), "0-0".to_string());
            }
        }

        Ok(())
    }

    /// Unsubscribe from all topics
    pub async fn unsubscribe(&self) -> Result<(), StreamError> {
        self.subscription.lock().clear();
        self.assignment.lock().clear();
        self.positions.clear();
        self.last_poll_ids.clear();
        Ok(())
    }

    /// Poll for records (with timeout)
    pub async fn poll(&self, timeout: Duration) -> Result<Vec<ConsumerRecord>, StreamError> {
        let topics: Vec<String> = self.subscription.lock().clone();

        if topics.is_empty() {
            return Ok(Vec::new());
        }

        // Get paused partitions
        let paused: Vec<String> = self
            .paused
            .lock()
            .iter()
            .map(|tp| tp.topic.clone())
            .collect();

        // Filter out paused topics
        let active_topics: Vec<&str> = topics
            .iter()
            .filter(|t| !paused.contains(t))
            .map(|s| s.as_str())
            .collect();

        if active_topics.is_empty() {
            return Ok(Vec::new());
        }

        // Get last poll IDs from concurrent hashmap
        let ids: Vec<String> = active_topics
            .iter()
            .map(|t| {
                self.last_poll_ids
                    .get(*t)
                    .unwrap_or_else(|| "0-0".to_string())
            })
            .collect();

        let id_refs: Vec<&str> = ids.iter().map(|s| s.as_str()).collect();
        let block_ms = timeout.as_millis() as u64;

        // Choose read method based on group membership
        let results = if let Some(group_id) = &self.config.group_id {
            let consumer_id = format!("consumer-{}", std::process::id());
            self.client.xreadgroup(
                group_id,
                &consumer_id,
                &active_topics,
                Some(self.config.max_poll_records),
                Some(block_ms),
            )?
        } else {
            self.client.xread(
                &active_topics,
                &id_refs,
                Some(self.config.max_poll_records),
                Some(block_ms),
            )?
        };

        // Flatten results and apply interceptors
        let mut all_records = Vec::new();
        for (topic, records) in results {
            for record in records {
                // Update last poll ID (concurrent hashmap, no lock)
                let new_id = format!("{}-{}", record.timestamp, record.offset);
                self.last_poll_ids.insert(topic.clone(), new_id);

                // Update position (concurrent hashmap, no lock)
                self.positions.insert(
                    TopicPartition::new(&topic, record.partition),
                    record.offset + 1,
                );

                // Update metrics
                self.records_received.fetch_add(1, Ordering::Relaxed);
                self.bytes_received
                    .fetch_add(record.value.len() as u64, Ordering::Relaxed);

                // Apply interceptors
                let record = self.apply_interceptors(record);
                all_records.push(record);
            }
        }

        Ok(all_records)
    }

    /// Commit all polled offsets synchronously
    pub async fn commit(&self) -> Result<(), StreamError> {
        let group_id =
            self.config.group_id.as_ref().ok_or_else(|| {
                StreamError::Config("group_id must be set for offset commits".into())
            })?;

        // Iterate over concurrent hashmap
        for (tp, offset) in self.positions.iter() {
            self.client.commit_offset(group_id, &tp.topic, offset)?;
        }

        Ok(())
    }

    /// Commit specific offsets synchronously
    pub async fn commit_offsets(
        &self,
        offsets: HashMap<TopicPartition, OffsetAndMetadata>,
    ) -> Result<(), StreamError> {
        let group_id =
            self.config.group_id.as_ref().ok_or_else(|| {
                StreamError::Config("group_id must be set for offset commits".into())
            })?;

        for (tp, offset_meta) in offsets.iter() {
            self.client
                .commit_offset(group_id, &tp.topic, offset_meta.offset)?;
        }

        Ok(())
    }

    /// Commit all polled offsets asynchronously
    pub fn commit_async(&self) {
        if let Some(group_id) = &self.config.group_id {
            for (tp, offset) in self.positions.iter() {
                let _ = self.client.commit_offset(group_id, &tp.topic, offset);
            }
        }
    }

    /// Commit specific offsets asynchronously with callback
    pub fn commit_async_with_callback<F>(
        &self,
        offsets: HashMap<TopicPartition, OffsetAndMetadata>,
        callback: F,
    ) where
        F: FnOnce(Result<(), StreamError>) + Send + 'static,
    {
        let result = if let Some(group_id) = &self.config.group_id {
            let mut err = None;
            for (tp, offset_meta) in offsets.iter() {
                if let Err(e) = self
                    .client
                    .commit_offset(group_id, &tp.topic, offset_meta.offset)
                {
                    err = Some(e);
                    break;
                }
            }
            err.map_or(Ok(()), Err)
        } else {
            Err(StreamError::Config(
                "group_id must be set for offset commits".into(),
            ))
        };

        callback(result);
    }

    /// Seek to specific offset for a partition
    pub fn seek(&self, tp: TopicPartition, offset: u64) {
        self.positions.insert(tp.clone(), offset);
        self.last_poll_ids.insert(tp.topic, format!("0-{}", offset));
    }

    /// Seek to beginning for partitions
    pub fn seek_to_beginning(&self, partitions: &[TopicPartition]) {
        for tp in partitions {
            self.positions.insert(tp.clone(), 0);
            self.last_poll_ids
                .insert(tp.topic.clone(), "0-0".to_string());
        }
    }

    /// Seek to end for partitions
    pub fn seek_to_end(&self, partitions: &[TopicPartition]) {
        for tp in partitions {
            let len = self.client.xlen(&tp.topic).unwrap_or(0);
            self.positions.insert(tp.clone(), len);
            self.last_poll_ids.insert(tp.topic.clone(), "$".to_string());
        }
    }

    /// Get current position (next offset to be fetched)
    pub fn position(&self, tp: &TopicPartition) -> Option<u64> {
        self.positions.get(tp)
    }

    /// Get committed offset for partitions
    pub async fn committed(
        &self,
        partitions: &[TopicPartition],
    ) -> Result<HashMap<TopicPartition, OffsetAndMetadata>, StreamError> {
        let group_id = self.config.group_id.as_ref().ok_or_else(|| {
            StreamError::Config("group_id must be set to get committed offsets".into())
        })?;

        let result = HashMap::new();
        for tp in partitions {
            if let Some(offset) = self.client.get_committed_offset(group_id, &tp.topic)? {
                result.insert(tp.clone(), OffsetAndMetadata::new(offset));
            }
        }

        Ok(result)
    }

    /// Pause consumption for partitions
    pub fn pause(&self, partitions: &[TopicPartition]) {
        let mut paused = self.paused.lock();
        for tp in partitions {
            if !paused.contains(tp) {
                paused.push(tp.clone());
            }
        }
    }

    /// Resume consumption for partitions
    pub fn resume(&self, partitions: &[TopicPartition]) {
        self.paused.lock().retain(|tp| !partitions.contains(tp));
    }

    /// Get currently paused partitions
    pub fn paused(&self) -> Vec<TopicPartition> {
        self.paused.lock().clone()
    }

    /// Get current subscription
    pub fn subscription(&self) -> Vec<String> {
        self.subscription.lock().clone()
    }

    /// Get assigned partitions
    pub fn assignment(&self) -> Vec<TopicPartition> {
        self.assignment.lock().clone()
    }

    /// Get offsets for commit (for transactional producer)
    pub fn offsets_for_commit(&self) -> HashMap<TopicPartition, OffsetAndMetadata> {
        let result = HashMap::new();
        for (tp, offset) in self.positions.iter() {
            result.insert(tp.clone(), OffsetAndMetadata::new(offset));
        }
        result
    }

    /// Get group metadata (for transactional producer)
    pub fn group_metadata(&self) -> ConsumerGroupMetadata {
        ConsumerGroupMetadata {
            group_id: self.config.group_id.clone().unwrap_or_default(),
            generation_id: 0,
            member_id: format!("consumer-{}", std::process::id()),
        }
    }

    // === Pending Entry List (PEL) Operations ===

    /// Get pending entries for this consumer group (XPENDING)
    pub async fn pending(
        &self,
        topic: &str,
        opts: PendingOptions,
    ) -> Result<PendingInfo, StreamError> {
        let group_id =
            self.config.group_id.as_ref().ok_or_else(|| {
                StreamError::Config("group_id must be set for PEL operations".into())
            })?;

        let result: PendingResult = self.client.xpending(
            topic,
            group_id,
            opts.start.as_deref(),
            opts.end.as_deref(),
            opts.count,
            opts.consumer.as_deref(),
        )?;

        Ok(PendingInfo {
            count: result.count,
            min_id: result.min_id,
            max_id: result.max_id,
            consumers: result.consumers,
        })
    }

    /// Claim messages from idle consumers (XCLAIM)
    pub async fn claim(
        &self,
        topic: &str,
        min_idle: Duration,
        ids: &[&str],
        opts: ClaimOptions,
    ) -> Result<Vec<ConsumerRecord>, StreamError> {
        let group_id =
            self.config.group_id.as_ref().ok_or_else(|| {
                StreamError::Config("group_id must be set for PEL operations".into())
            })?;

        let consumer_id = format!("consumer-{}", std::process::id());

        self.client.xclaim(
            topic,
            group_id,
            &consumer_id,
            min_idle.as_millis() as u64,
            ids,
            opts.force,
        )
    }

    /// Auto-claim idle messages starting from an ID (XAUTOCLAIM)
    pub async fn auto_claim(
        &self,
        topic: &str,
        min_idle: Duration,
        start_id: &str,
        count: u32,
    ) -> Result<AutoClaimResult, StreamError> {
        let group_id =
            self.config.group_id.as_ref().ok_or_else(|| {
                StreamError::Config("group_id must be set for PEL operations".into())
            })?;

        let consumer_id = format!("consumer-{}", std::process::id());

        let response = self.client.xautoclaim(
            topic,
            group_id,
            &consumer_id,
            min_idle.as_millis() as u64,
            start_id,
            count,
        )?;

        Ok(AutoClaimResult {
            next_id: response.next_id,
            records: response.records,
            deleted_ids: response.deleted_ids,
        })
    }

    /// Acknowledge messages (XACK)
    pub async fn ack(&self, topic: &str, ids: &[&str]) -> Result<u32, StreamError> {
        let group_id =
            self.config.group_id.as_ref().ok_or_else(|| {
                StreamError::Config("group_id must be set for PEL operations".into())
            })?;

        self.client.xack(topic, group_id, ids)
    }

    /// Close the consumer
    pub async fn close(&self) -> Result<(), StreamError> {
        self.unsubscribe().await?;
        Ok(())
    }

    /// Get CDC builder for this consumer
    pub fn cdc(&self) -> CdcBuilder<'_> {
        CdcBuilder::from_consumer(self)
    }

    /// Get current configuration
    pub fn config(&self) -> &ConsumerConfig {
        &self.config
    }

    /// Get number of records received
    pub fn records_received(&self) -> u64 {
        self.records_received.load(Ordering::Relaxed)
    }

    /// Get bytes received
    pub fn bytes_received(&self) -> u64 {
        self.bytes_received.load(Ordering::Relaxed)
    }

    // === Internal helpers ===

    fn apply_interceptors(&self, record: ConsumerRecord) -> ConsumerRecord {
        let mut records = vec![record];
        for interceptor in &self.interceptors {
            records = interceptor.on_consume(records);
        }
        records
            .into_iter()
            .next()
            .unwrap_or_else(|| ConsumerRecord {
                topic: String::new(),
                partition: 0,
                offset: 0,
                key: None,
                value: Vec::new(),
                headers: Vec::new(),
                timestamp: 0,
            })
    }
}

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

    #[test]
    fn test_consumer_builder() {
        let builder = Consumer::builder()
            .bootstrap_servers("localhost:6379")
            .group_id("my-group")
            .auto_commit(false)
            .auto_commit_interval_ms(1000)
            .auto_offset_reset(OffsetReset::Earliest)
            .max_poll_records(100)
            .fetch_min_bytes(1024)
            .fetch_max_bytes(1048576);

        assert_eq!(builder.config.bootstrap_servers, "localhost:6379");
        assert_eq!(builder.config.group_id, Some("my-group".to_string()));
        assert!(!builder.config.auto_commit);
    }

    #[test]
    fn test_topic_partition() {
        let tp = TopicPartition::new("orders", 3);
        assert_eq!(tp.topic, "orders");
        assert_eq!(tp.partition, 3);
    }

    #[test]
    fn test_offset_and_metadata() {
        let offset = OffsetAndMetadata::new(1000);
        assert_eq!(offset.offset, 1000);
        assert!(offset.metadata.is_none());

        let offset = OffsetAndMetadata::with_metadata(2000, "metadata");
        assert_eq!(offset.offset, 2000);
        assert_eq!(offset.metadata.as_deref(), Some("metadata"));
    }

    #[test]
    fn test_pending_options() {
        let opts = PendingOptions {
            start: Some("-".to_string()),
            end: Some("+".to_string()),
            count: Some(10),
            consumer: None,
            min_idle: Some(60000),
        };

        assert_eq!(opts.count, Some(10));
        assert_eq!(opts.min_idle, Some(60000));
    }
}