lnc-client 0.2.8

LANCE client library - Rust client for the LANCE streaming platform
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
//! Client-side offset persistence for LANCE consumers
//!
//! Provides traits and implementations for storing consumer offsets locally,
//! enabling stateless broker architecture per the Client-Managed Offset Strategy.
//!
//! # Design Philosophy
//!
//! LANCE treats the broker as a stateless data pipe. Offset tracking is the
//! client's responsibility, enabling:
//! - Exactly-once semantics (store offset with business data atomically)
//! - Stateless horizontal scaling
//! - Reduced broker metadata overhead
//!
//! # Example
//!
//! ```ignore
//! use lnc_client::offset::{OffsetStore, LockFileOffsetStore};
//! use std::path::Path;
//!
//! let store = LockFileOffsetStore::open(
//!     Path::new("/var/lib/lance/offsets"),
//!     "my-consumer",
//! )?;
//!
//! // Load stored offset for a topic
//! let offset = store.load(topic_id, consumer_id).unwrap_or(0);
//!
//! // After processing, save the new offset
//! store.save(topic_id, consumer_id, new_offset)?;
//! ```

use std::collections::HashMap;
use std::fs::{self, File, OpenOptions};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};

use crate::error::{ClientError, Result};

/// Trait for client-side offset persistence
///
/// Implementations store consumer offsets locally, allowing consumers to resume
/// from their last position after restarts without server-side state.
pub trait OffsetStore: Send + Sync {
    /// Load the stored offset for a topic and consumer
    ///
    /// Returns `None` if no offset has been stored (first run).
    fn load(&self, topic_id: u32, consumer_id: u64) -> Result<Option<u64>>;

    /// Save the current offset for a topic and consumer
    ///
    /// This should be called after successfully processing records.
    fn save(&self, topic_id: u32, consumer_id: u64, offset: u64) -> Result<()>;

    /// Delete stored offset for a topic and consumer
    ///
    /// Use when resetting consumer position or cleaning up.
    fn delete(&self, topic_id: u32, consumer_id: u64) -> Result<()>;

    /// List all stored offsets
    ///
    /// Returns a map of (topic_id, consumer_id) -> offset
    fn list_all(&self) -> Result<HashMap<(u32, u64), u64>>;
}

/// In-memory offset store for testing and ephemeral consumers
///
/// Offsets are lost when the process exits. Use for testing or when
/// offset persistence is handled externally (e.g., in a database transaction).
#[derive(Debug, Default, Clone)]
pub struct MemoryOffsetStore {
    offsets: Arc<RwLock<HashMap<(u32, u64), u64>>>,
}

impl MemoryOffsetStore {
    /// Create a new in-memory offset store
    pub fn new() -> Self {
        Self {
            offsets: Arc::new(RwLock::new(HashMap::new())),
        }
    }
}

impl OffsetStore for MemoryOffsetStore {
    fn load(&self, topic_id: u32, consumer_id: u64) -> Result<Option<u64>> {
        let offsets = self.offsets.read().map_err(|e| {
            ClientError::IoError(std::io::Error::other(format!("Lock poisoned: {}", e)))
        })?;
        Ok(offsets.get(&(topic_id, consumer_id)).copied())
    }

    fn save(&self, topic_id: u32, consumer_id: u64, offset: u64) -> Result<()> {
        let mut offsets = self.offsets.write().map_err(|e| {
            ClientError::IoError(std::io::Error::other(format!("Lock poisoned: {}", e)))
        })?;
        offsets.insert((topic_id, consumer_id), offset);
        Ok(())
    }

    fn delete(&self, topic_id: u32, consumer_id: u64) -> Result<()> {
        let mut offsets = self.offsets.write().map_err(|e| {
            ClientError::IoError(std::io::Error::other(format!("Lock poisoned: {}", e)))
        })?;
        offsets.remove(&(topic_id, consumer_id));
        Ok(())
    }

    fn list_all(&self) -> Result<HashMap<(u32, u64), u64>> {
        let offsets = self.offsets.read().map_err(|e| {
            ClientError::IoError(std::io::Error::other(format!("Lock poisoned: {}", e)))
        })?;
        Ok(offsets.clone())
    }
}

/// File-based offset store with lock file protection
///
/// Stores offsets in individual files under a base directory:
/// ```text
/// {base_dir}/{consumer_name}/
/// ├── topic-{topic_id}-consumer-{consumer_id}.offset
/// └── ...
/// ```
///
/// Uses file locking (flock on Unix, LockFile on Windows) to ensure
/// safe concurrent access from multiple processes.
#[derive(Debug)]
pub struct LockFileOffsetStore {
    base_dir: PathBuf,
    consumer_name: String,
    cache: RwLock<HashMap<(u32, u64), u64>>,
}

impl LockFileOffsetStore {
    /// Open or create an offset store at the given directory
    ///
    /// # Arguments
    /// * `base_dir` - Base directory for offset files
    /// * `consumer_name` - Name for this consumer instance (subdirectory name)
    ///
    /// # Example
    /// ```ignore
    /// let store = LockFileOffsetStore::open(
    ///     Path::new("/var/lib/lance/offsets"),
    ///     "my-service",
    /// )?;
    /// ```
    pub fn open(base_dir: &Path, consumer_name: &str) -> Result<Self> {
        let dir = base_dir.join(consumer_name);
        fs::create_dir_all(&dir).map_err(ClientError::IoError)?;

        let store = Self {
            base_dir: dir,
            consumer_name: consumer_name.to_string(),
            cache: RwLock::new(HashMap::new()),
        };

        // Pre-load existing offsets into cache
        store.load_all_into_cache()?;

        Ok(store)
    }

    /// Get the file path for a specific topic/consumer offset
    fn offset_file_path(&self, topic_id: u32, consumer_id: u64) -> PathBuf {
        self.base_dir.join(format!(
            "topic-{}-consumer-{}.offset",
            topic_id, consumer_id
        ))
    }

    /// Get the lock file path for a specific topic/consumer
    fn lock_file_path(&self, topic_id: u32, consumer_id: u64) -> PathBuf {
        self.base_dir
            .join(format!("topic-{}-consumer-{}.lock", topic_id, consumer_id))
    }

    /// Load all existing offsets into the cache
    fn load_all_into_cache(&self) -> Result<()> {
        let mut cache = self.cache.write().map_err(|e| {
            ClientError::IoError(std::io::Error::other(format!("Lock poisoned: {}", e)))
        })?;

        let entries = match fs::read_dir(&self.base_dir) {
            Ok(entries) => entries,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()),
            Err(e) => return Err(ClientError::IoError(e)),
        };

        for entry in entries.flatten() {
            let path = entry.path();
            if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
                if name.ends_with(".offset") {
                    // Parse topic-{id}-consumer-{id}.offset
                    if let Some((topic_id, consumer_id)) = Self::parse_offset_filename(name) {
                        if let Ok(offset) = Self::read_offset_file(&path) {
                            cache.insert((topic_id, consumer_id), offset);
                        }
                    }
                }
            }
        }

        Ok(())
    }

    /// Parse offset filename to extract topic_id and consumer_id
    fn parse_offset_filename(name: &str) -> Option<(u32, u64)> {
        // Format: topic-{topic_id}-consumer-{consumer_id}.offset
        let name = name.strip_suffix(".offset")?;
        let parts: Vec<&str> = name.split('-').collect();
        if parts.len() >= 4 && parts[0] == "topic" && parts[2] == "consumer" {
            let topic_id: u32 = parts[1].parse().ok()?;
            let consumer_id: u64 = parts[3].parse().ok()?;
            Some((topic_id, consumer_id))
        } else {
            None
        }
    }

    /// Read offset from a file
    fn read_offset_file(path: &Path) -> Result<u64> {
        let mut file = File::open(path).map_err(ClientError::IoError)?;
        let mut content = String::new();
        file.read_to_string(&mut content)
            .map_err(ClientError::IoError)?;
        content
            .trim()
            .parse()
            .map_err(|e| ClientError::IoError(std::io::Error::other(e)))
    }

    /// Write offset to a file atomically
    fn write_offset_file(&self, topic_id: u32, consumer_id: u64, offset: u64) -> Result<()> {
        let path = self.offset_file_path(topic_id, consumer_id);
        let lock_path = self.lock_file_path(topic_id, consumer_id);
        let temp_path = path.with_extension("offset.tmp");

        // Acquire lock file
        let lock_file = OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(true)
            .open(&lock_path)
            .map_err(ClientError::IoError)?;

        // Platform-specific file locking
        #[cfg(unix)]
        {
            use std::os::unix::io::AsRawFd;
            let fd = lock_file.as_raw_fd();
            // SAFETY: fd is valid file descriptor, flock with LOCK_EX is safe
            unsafe {
                if libc::flock(fd, libc::LOCK_EX) != 0 {
                    return Err(ClientError::IoError(std::io::Error::last_os_error()));
                }
            }
        }

        #[cfg(windows)]
        {
            use std::os::windows::io::AsRawHandle;
            use windows_sys::Win32::Foundation::HANDLE;
            use windows_sys::Win32::Storage::FileSystem::{LOCKFILE_EXCLUSIVE_LOCK, LockFileEx};
            let handle = lock_file.as_raw_handle() as HANDLE;
            // SAFETY: handle is valid from as_raw_handle, OVERLAPPED is zeroed correctly
            unsafe {
                let mut overlapped =
                    std::mem::zeroed::<windows_sys::Win32::System::IO::OVERLAPPED>();
                if LockFileEx(
                    handle,
                    LOCKFILE_EXCLUSIVE_LOCK,
                    0,
                    u32::MAX,
                    u32::MAX,
                    &mut overlapped,
                ) == 0
                {
                    return Err(ClientError::IoError(std::io::Error::last_os_error()));
                }
            }
        }

        // Write to temp file
        let mut temp_file = File::create(&temp_path).map_err(ClientError::IoError)?;
        writeln!(temp_file, "{}", offset).map_err(ClientError::IoError)?;
        temp_file.sync_all().map_err(ClientError::IoError)?;
        drop(temp_file);

        // Atomic rename
        fs::rename(&temp_path, &path).map_err(ClientError::IoError)?;

        // Lock is released when lock_file is dropped
        drop(lock_file);

        Ok(())
    }

    /// Get the consumer name
    pub fn consumer_name(&self) -> &str {
        &self.consumer_name
    }

    /// Get the base directory
    pub fn base_dir(&self) -> &Path {
        &self.base_dir
    }
}

impl OffsetStore for LockFileOffsetStore {
    fn load(&self, topic_id: u32, consumer_id: u64) -> Result<Option<u64>> {
        // Check cache first
        {
            let cache = self.cache.read().map_err(|e| {
                ClientError::IoError(std::io::Error::other(format!("Lock poisoned: {}", e)))
            })?;
            if let Some(&offset) = cache.get(&(topic_id, consumer_id)) {
                return Ok(Some(offset));
            }
        }

        // Try to read from file
        let path = self.offset_file_path(topic_id, consumer_id);
        if !path.exists() {
            return Ok(None);
        }

        let offset = Self::read_offset_file(&path)?;

        // Update cache
        {
            let mut cache = self.cache.write().map_err(|e| {
                ClientError::IoError(std::io::Error::other(format!("Lock poisoned: {}", e)))
            })?;
            cache.insert((topic_id, consumer_id), offset);
        }

        Ok(Some(offset))
    }

    fn save(&self, topic_id: u32, consumer_id: u64, offset: u64) -> Result<()> {
        // Write to file
        self.write_offset_file(topic_id, consumer_id, offset)?;

        // Update cache
        {
            let mut cache = self.cache.write().map_err(|e| {
                ClientError::IoError(std::io::Error::other(format!("Lock poisoned: {}", e)))
            })?;
            cache.insert((topic_id, consumer_id), offset);
        }

        Ok(())
    }

    fn delete(&self, topic_id: u32, consumer_id: u64) -> Result<()> {
        let path = self.offset_file_path(topic_id, consumer_id);
        let lock_path = self.lock_file_path(topic_id, consumer_id);

        // Remove from cache
        {
            let mut cache = self.cache.write().map_err(|e| {
                ClientError::IoError(std::io::Error::other(format!("Lock poisoned: {}", e)))
            })?;
            cache.remove(&(topic_id, consumer_id));
        }

        // Delete files (ignore if not found)
        let _ = fs::remove_file(&path);
        let _ = fs::remove_file(&lock_path);

        Ok(())
    }

    fn list_all(&self) -> Result<HashMap<(u32, u64), u64>> {
        let cache = self.cache.read().map_err(|e| {
            ClientError::IoError(std::io::Error::other(format!("Lock poisoned: {}", e)))
        })?;
        Ok(cache.clone())
    }
}

/// Information about a committed offset
#[derive(Debug, Clone)]
pub struct CommitInfo {
    /// Topic ID
    pub topic_id: u32,
    /// Consumer ID
    pub consumer_id: u64,
    /// Committed offset
    pub offset: u64,
    /// Previous offset (if known)
    pub previous_offset: Option<u64>,
    /// Timestamp of commit
    pub timestamp: std::time::SystemTime,
}

/// Trait for post-commit hooks
///
/// Implement this trait to receive notifications after offsets are committed.
/// Use cases include: external offset persistence, metrics, audit logging.
pub trait PostCommitHook: Send + Sync {
    /// Called after an offset is successfully committed
    ///
    /// This method should not block for extended periods. For expensive
    /// operations, consider spawning a task or using a channel.
    fn on_commit(&self, info: &CommitInfo) -> Result<()>;
}

/// Offset store wrapper that invokes post-commit hooks
///
/// Wraps an inner `OffsetStore` and calls registered hooks after each commit.
pub struct HookedOffsetStore<S: OffsetStore> {
    inner: S,
    hooks: Vec<Arc<dyn PostCommitHook>>,
    previous_offsets: RwLock<HashMap<(u32, u64), u64>>,
}

impl<S: OffsetStore> HookedOffsetStore<S> {
    /// Create a new hooked offset store
    pub fn new(inner: S) -> Self {
        Self {
            inner,
            hooks: Vec::new(),
            previous_offsets: RwLock::new(HashMap::new()),
        }
    }

    /// Add a post-commit hook
    pub fn add_hook(mut self, hook: Arc<dyn PostCommitHook>) -> Self {
        self.hooks.push(hook);
        self
    }

    /// Add multiple post-commit hooks
    pub fn with_hooks(mut self, hooks: Vec<Arc<dyn PostCommitHook>>) -> Self {
        self.hooks.extend(hooks);
        self
    }

    /// Get the inner store
    pub fn inner(&self) -> &S {
        &self.inner
    }

    /// Invoke all registered hooks
    fn invoke_hooks(&self, info: &CommitInfo) -> Result<()> {
        for hook in &self.hooks {
            hook.on_commit(info)?;
        }
        Ok(())
    }
}

impl<S: OffsetStore> OffsetStore for HookedOffsetStore<S> {
    fn load(&self, topic_id: u32, consumer_id: u64) -> Result<Option<u64>> {
        let offset = self.inner.load(topic_id, consumer_id)?;

        // Track for previous_offset in commits
        if let Some(off) = offset {
            if let Ok(mut prev) = self.previous_offsets.write() {
                prev.insert((topic_id, consumer_id), off);
            }
        }

        Ok(offset)
    }

    fn save(&self, topic_id: u32, consumer_id: u64, offset: u64) -> Result<()> {
        // Get previous offset
        let previous_offset = self
            .previous_offsets
            .read()
            .ok()
            .and_then(|prev| prev.get(&(topic_id, consumer_id)).copied());

        // Save to inner store
        self.inner.save(topic_id, consumer_id, offset)?;

        // Update previous offset tracking
        if let Ok(mut prev) = self.previous_offsets.write() {
            prev.insert((topic_id, consumer_id), offset);
        }

        // Invoke hooks
        let info = CommitInfo {
            topic_id,
            consumer_id,
            offset,
            previous_offset,
            timestamp: std::time::SystemTime::now(),
        };
        self.invoke_hooks(&info)?;

        Ok(())
    }

    fn delete(&self, topic_id: u32, consumer_id: u64) -> Result<()> {
        // Remove from previous tracking
        if let Ok(mut prev) = self.previous_offsets.write() {
            prev.remove(&(topic_id, consumer_id));
        }

        self.inner.delete(topic_id, consumer_id)
    }

    fn list_all(&self) -> Result<HashMap<(u32, u64), u64>> {
        self.inner.list_all()
    }
}

/// A simple logging hook for debugging
#[derive(Debug, Default)]
pub struct LoggingCommitHook;

impl PostCommitHook for LoggingCommitHook {
    fn on_commit(&self, info: &CommitInfo) -> Result<()> {
        tracing::debug!(
            topic_id = info.topic_id,
            consumer_id = info.consumer_id,
            offset = info.offset,
            previous = ?info.previous_offset,
            "Offset committed"
        );
        Ok(())
    }
}

/// A hook that collects commit events for testing
#[derive(Debug, Default)]
pub struct CollectingCommitHook {
    commits: RwLock<Vec<CommitInfo>>,
}

impl CollectingCommitHook {
    /// Create a new collecting hook
    pub fn new() -> Self {
        Self::default()
    }

    /// Get all collected commits
    pub fn commits(&self) -> Vec<CommitInfo> {
        self.commits.read().map(|c| c.clone()).unwrap_or_default()
    }

    /// Clear collected commits
    pub fn clear(&self) {
        if let Ok(mut commits) = self.commits.write() {
            commits.clear();
        }
    }
}

impl PostCommitHook for CollectingCommitHook {
    fn on_commit(&self, info: &CommitInfo) -> Result<()> {
        if let Ok(mut commits) = self.commits.write() {
            commits.push(info.clone());
        }
        Ok(())
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn test_memory_offset_store() {
        let store = MemoryOffsetStore::new();

        // Initially empty
        assert!(store.load(1, 100).unwrap().is_none());

        // Save and load
        store.save(1, 100, 42).unwrap();
        assert_eq!(store.load(1, 100).unwrap(), Some(42));

        // Update
        store.save(1, 100, 100).unwrap();
        assert_eq!(store.load(1, 100).unwrap(), Some(100));

        // Different topic/consumer
        store.save(2, 200, 999).unwrap();
        assert_eq!(store.load(2, 200).unwrap(), Some(999));
        assert_eq!(store.load(1, 100).unwrap(), Some(100));

        // List all
        let all = store.list_all().unwrap();
        assert_eq!(all.len(), 2);
        assert_eq!(all.get(&(1, 100)), Some(&100));
        assert_eq!(all.get(&(2, 200)), Some(&999));

        // Delete
        store.delete(1, 100).unwrap();
        assert!(store.load(1, 100).unwrap().is_none());
        assert_eq!(store.load(2, 200).unwrap(), Some(999));
    }

    #[test]
    fn test_lock_file_offset_store() {
        let temp_dir = TempDir::new().unwrap();
        let store = LockFileOffsetStore::open(temp_dir.path(), "test-consumer").unwrap();

        // Initially empty
        assert!(store.load(1, 100).unwrap().is_none());

        // Save and load
        store.save(1, 100, 12345).unwrap();
        assert_eq!(store.load(1, 100).unwrap(), Some(12345));

        // Verify file exists
        let file_path = store.offset_file_path(1, 100);
        assert!(file_path.exists());

        // Read file content
        let content = fs::read_to_string(&file_path).unwrap();
        assert_eq!(content.trim(), "12345");

        // Update
        store.save(1, 100, 67890).unwrap();
        assert_eq!(store.load(1, 100).unwrap(), Some(67890));

        // Delete
        store.delete(1, 100).unwrap();
        assert!(store.load(1, 100).unwrap().is_none());
        assert!(!file_path.exists());
    }

    #[test]
    fn test_lock_file_offset_store_persistence() {
        let temp_dir = TempDir::new().unwrap();

        // Create store and save offset
        {
            let store = LockFileOffsetStore::open(temp_dir.path(), "persist-test").unwrap();
            store.save(5, 500, 99999).unwrap();
        }

        // Reopen and verify offset is still there
        {
            let store = LockFileOffsetStore::open(temp_dir.path(), "persist-test").unwrap();
            assert_eq!(store.load(5, 500).unwrap(), Some(99999));
        }
    }

    #[test]
    fn test_parse_offset_filename() {
        assert_eq!(
            LockFileOffsetStore::parse_offset_filename("topic-1-consumer-100.offset"),
            Some((1, 100))
        );
        assert_eq!(
            LockFileOffsetStore::parse_offset_filename("topic-999-consumer-12345.offset"),
            Some((999, 12345))
        );
        assert_eq!(
            LockFileOffsetStore::parse_offset_filename("invalid.offset"),
            None
        );
        assert_eq!(
            LockFileOffsetStore::parse_offset_filename("topic-abc-consumer-100.offset"),
            None
        );
    }

    #[test]
    fn test_hooked_offset_store() {
        let inner = MemoryOffsetStore::new();
        let hook = Arc::new(CollectingCommitHook::new());
        let store = HookedOffsetStore::new(inner).add_hook(hook.clone());

        // Save triggers hook
        store.save(1, 100, 42).unwrap();

        let commits = hook.commits();
        assert_eq!(commits.len(), 1);
        assert_eq!(commits[0].topic_id, 1);
        assert_eq!(commits[0].consumer_id, 100);
        assert_eq!(commits[0].offset, 42);
        assert!(commits[0].previous_offset.is_none());

        // Second save has previous offset
        store.save(1, 100, 100).unwrap();

        let commits = hook.commits();
        assert_eq!(commits.len(), 2);
        assert_eq!(commits[1].offset, 100);
        assert_eq!(commits[1].previous_offset, Some(42));

        // Different topic/consumer
        store.save(2, 200, 500).unwrap();
        assert_eq!(hook.commits().len(), 3);

        // Load returns correct values
        assert_eq!(store.load(1, 100).unwrap(), Some(100));
        assert_eq!(store.load(2, 200).unwrap(), Some(500));

        // Clear and verify
        hook.clear();
        assert!(hook.commits().is_empty());
    }

    #[test]
    fn test_collecting_commit_hook() {
        let hook = CollectingCommitHook::new();

        let info = CommitInfo {
            topic_id: 1,
            consumer_id: 100,
            offset: 42,
            previous_offset: None,
            timestamp: std::time::SystemTime::now(),
        };

        hook.on_commit(&info).unwrap();

        let commits = hook.commits();
        assert_eq!(commits.len(), 1);
        assert_eq!(commits[0].topic_id, 1);

        hook.clear();
        assert!(hook.commits().is_empty());
    }
}