incrededup 0.3.1

Incremental document deduplication using MinHash LSH
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
//! LSH (Locality-Sensitive Hashing) implementation with disk-backed storage.
//!
//! The in-memory banding and candidate lookup are adapted from Rensa's
//! `RMinHashLSH` design (https://github.com/beowolx/rensa). Copyright (c) 2024
//! beowulf; incorporated under the MIT License and modified for `incrededup`.
//! See THIRD_PARTY_NOTICES.md. The `redb` sidecar storage layer is
//! `incrededup`-specific.
//!
//! This module provides both in-memory and disk-backed LSH indices using redb.
//! The disk-backed version allows processing datasets larger than memory.

use crate::minhash::{calculate_band_hash, NUM_BANDS, NUM_PERM, ROWS_PER_BAND};
use crate::tokenizer::TOKENIZER_VERSION;
use anyhow::{Context, Result};
use indicatif::{ProgressBar, ProgressStyle};
use redb::{Database, ReadableTable, ReadableTableMetadata, TableDefinition};
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use std::path::Path;
use uuid::Uuid;

/// Table for storing band -> document IDs mapping
/// Key: band_index:band_hash (as string), Value: serialized Vec<Uuid>
const BAND_TABLE: TableDefinition<&str, &[u8]> = TableDefinition::new("bands");

/// Table for storing document signatures
/// Key: doc_id (as string), Value: serialized signature
const SIGNATURE_TABLE: TableDefinition<&str, &[u8]> = TableDefinition::new("signatures");

/// Table for index metadata used to detect incompatible sidecar reuse.
const METADATA_TABLE: TableDefinition<&str, &[u8]> = TableDefinition::new("metadata");
const METADATA_KEY: &str = "lsh_config";
const LSH_METADATA_VERSION: u32 = 1;
const HASH_VERSION: &str = "rminhash_fx_v1";

fn begin_quick_repair_write(db: &Database) -> Result<redb::WriteTransaction> {
    let mut write_txn = db.begin_write()?;
    write_txn.set_quick_repair(true);
    Ok(write_txn)
}

/// Metadata that determines whether an existing LSH sidecar can be reused.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct LshMetadata {
    pub version: u32,
    pub seed: u64,
    pub num_perm: usize,
    pub num_bands: usize,
    pub rows_per_band: usize,
    pub tokenizer_version: String,
    pub hash_version: String,
}

impl LshMetadata {
    #[must_use]
    pub fn current(seed: u64, num_bands: usize, rows_per_band: usize) -> Self {
        Self {
            version: LSH_METADATA_VERSION,
            seed,
            num_perm: NUM_PERM,
            num_bands,
            rows_per_band,
            tokenizer_version: TOKENIZER_VERSION.to_string(),
            hash_version: HASH_VERSION.to_string(),
        }
    }
}

/// In-memory LSH index for fast lookups (used during batch processing)
#[derive(Debug, Clone)]
pub struct InMemoryLSH {
    /// Hash tables for each band: band_hash -> list of doc IDs
    pub hash_tables: Vec<FxHashMap<u64, Vec<Uuid>>>,
    /// Document signatures: doc_id -> signature
    pub signatures: FxHashMap<Uuid, Vec<u32>>,
    /// Configuration
    pub num_bands: usize,
    pub rows_per_band: usize,
}

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

impl InMemoryLSH {
    /// Create a new in-memory LSH index with default parameters
    #[must_use]
    pub fn new() -> Self {
        Self::with_params(NUM_BANDS, ROWS_PER_BAND)
    }

    /// Create a new in-memory LSH index with custom parameters
    #[must_use]
    pub fn with_params(num_bands: usize, rows_per_band: usize) -> Self {
        let hash_tables = (0..num_bands).map(|_| FxHashMap::default()).collect();

        Self {
            hash_tables,
            signatures: FxHashMap::default(),
            num_bands,
            rows_per_band,
        }
    }

    /// Insert a document signature into the index
    pub fn insert(&mut self, doc_id: Uuid, signature: Vec<u32>) {
        let _ = self.try_insert(doc_id, signature);
    }

    /// Checked insert for callers that want malformed signatures reported.
    pub fn try_insert(&mut self, doc_id: Uuid, signature: Vec<u32>) -> Result<()> {
        validate_signature_len(&signature, self.num_bands, self.rows_per_band)?;

        // Store signature
        self.signatures.insert(doc_id, signature.clone());

        // Add to hash tables
        for (band_idx, table) in self.hash_tables.iter_mut().enumerate() {
            let start = band_idx * self.rows_per_band;
            let end = start + self.rows_per_band;
            let band_hash = calculate_band_hash(&signature[start..end]);
            table.entry(band_hash).or_default().push(doc_id);
        }

        Ok(())
    }

    /// Query for candidate duplicates (documents that share at least one band)
    #[must_use]
    pub fn query(&self, signature: &[u32]) -> Vec<Uuid> {
        self.try_query(signature).unwrap_or_default()
    }

    /// Checked query for callers that want malformed signatures reported.
    pub fn try_query(&self, signature: &[u32]) -> Result<Vec<Uuid>> {
        validate_signature_len(signature, self.num_bands, self.rows_per_band)?;
        let mut candidates = Vec::new();

        for (band_idx, table) in self.hash_tables.iter().enumerate() {
            let start = band_idx * self.rows_per_band;
            let end = start + self.rows_per_band;
            let band_hash = calculate_band_hash(&signature[start..end]);

            if let Some(doc_ids) = table.get(&band_hash) {
                candidates.extend(doc_ids.iter().copied());
            }
        }

        // Deduplicate
        candidates.sort();
        candidates.dedup();
        Ok(candidates)
    }

    /// Get a signature by document ID
    #[must_use]
    pub fn get_signature(&self, doc_id: &Uuid) -> Option<&Vec<u32>> {
        self.signatures.get(doc_id)
    }

    /// Get number of documents in index
    #[must_use]
    pub fn len(&self) -> usize {
        self.signatures.len()
    }

    /// Check if index is empty
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.signatures.is_empty()
    }

    /// Get all document IDs
    pub fn doc_ids(&self) -> impl Iterator<Item = &Uuid> {
        self.signatures.keys()
    }
}

/// Document entry for disk storage
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DocumentEntry {
    pub signature: Vec<u32>,
    pub content_len: usize,
}

/// Disk-backed LSH index using redb
pub struct DiskLSH {
    db: Database,
    num_bands: usize,
    rows_per_band: usize,
}

fn validate_signature_len(signature: &[u32], num_bands: usize, rows_per_band: usize) -> Result<()> {
    let expected = num_bands
        .checked_mul(rows_per_band)
        .context("Invalid LSH band configuration")?;
    if expected == 0 || signature.len() != expected {
        anyhow::bail!(
            "Invalid signature length: got {}, expected {} ({} bands x {} rows)",
            signature.len(),
            expected,
            num_bands,
            rows_per_band
        );
    }
    Ok(())
}

impl DiskLSH {
    /// Open or create a disk-backed LSH index
    pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
        Self::open_with_params(path, NUM_BANDS, ROWS_PER_BAND)
    }

    /// Open or create with custom parameters
    pub fn open_with_params<P: AsRef<Path>>(
        path: P,
        num_bands: usize,
        rows_per_band: usize,
    ) -> Result<Self> {
        let db = Database::create(path.as_ref())
            .with_context(|| format!("Failed to create database at {:?}", path.as_ref()))?;

        // Initialize tables
        let write_txn = begin_quick_repair_write(&db)?;
        {
            let _ = write_txn.open_table(BAND_TABLE)?;
            let _ = write_txn.open_table(SIGNATURE_TABLE)?;
            let _ = write_txn.open_table(METADATA_TABLE)?;
        }
        write_txn.commit()?;

        Ok(Self {
            db,
            num_bands,
            rows_per_band,
        })
    }

    /// Validate or initialize sidecar metadata for this index.
    ///
    /// Empty sidecars are initialized with current metadata. Populated sidecars
    /// without metadata are not reusable because their tokenizer, hash version,
    /// and seed cannot be verified.
    pub fn validate_or_initialize_metadata(&self, seed: u64) -> Result<()> {
        let expected = LshMetadata::current(seed, self.num_bands, self.rows_per_band);

        let read_txn = self.db.begin_read()?;
        let metadata_table = read_txn.open_table(METADATA_TABLE)?;
        let existing = metadata_table
            .get(METADATA_KEY)?
            .map(|bytes| bincode::deserialize::<LshMetadata>(bytes.value()))
            .transpose()?;
        drop(read_txn);

        match existing {
            Some(existing) if existing == expected => Ok(()),
            Some(existing) => {
                anyhow::bail!(
                    "Existing LSH sidecar was built with incompatible metadata: {:?}; current run expects {:?}. Rebuild with fresh=true/--fresh.",
                    existing,
                    expected
                );
            }
            None => {
                let count = self.count()?;
                if count > 0 {
                    anyhow::bail!(
                        "Existing LSH sidecar has no metadata. Cannot safely verify tokenizer/hash compatibility; rebuild with fresh=true/--fresh."
                    );
                }

                let write_txn = begin_quick_repair_write(&self.db)?;
                {
                    let mut metadata_table = write_txn.open_table(METADATA_TABLE)?;
                    let bytes = bincode::serialize(&expected)?;
                    metadata_table.insert(METADATA_KEY, bytes.as_slice())?;
                }
                write_txn.commit()?;
                Ok(())
            }
        }
    }

    /// Insert a document signature into the index
    pub fn insert(&self, doc_id: Uuid, signature: Vec<u32>, content_len: usize) -> Result<()> {
        validate_signature_len(&signature, self.num_bands, self.rows_per_band)?;
        let write_txn = begin_quick_repair_write(&self.db)?;

        {
            let mut band_table = write_txn.open_table(BAND_TABLE)?;
            let mut sig_table = write_txn.open_table(SIGNATURE_TABLE)?;

            // Store signature
            let entry = DocumentEntry {
                signature: signature.clone(),
                content_len,
            };
            let entry_bytes = bincode::serialize(&entry)?;
            sig_table.insert(doc_id.to_string().as_str(), entry_bytes.as_slice())?;

            // Add to band tables
            for band_idx in 0..self.num_bands {
                let start = band_idx * self.rows_per_band;
                let end = start + self.rows_per_band;
                let band_hash = calculate_band_hash(&signature[start..end]);

                let key = format!("{}:{}", band_idx, band_hash);

                // Get existing doc IDs for this band hash
                let mut doc_ids: Vec<Uuid> = if let Some(existing) = band_table.get(key.as_str())? {
                    bincode::deserialize(existing.value())?
                } else {
                    Vec::new()
                };

                if !doc_ids.contains(&doc_id) {
                    doc_ids.push(doc_id);
                }
                let doc_ids_bytes = bincode::serialize(&doc_ids)?;
                band_table.insert(key.as_str(), doc_ids_bytes.as_slice())?;
            }
        }

        write_txn.commit()?;
        Ok(())
    }

    /// Batch insert multiple documents (more efficient)
    pub fn insert_batch(&self, documents: &[(Uuid, Vec<u32>, usize)]) -> Result<()> {
        for (_, signature, _) in documents {
            validate_signature_len(signature, self.num_bands, self.rows_per_band)?;
        }

        let write_txn = begin_quick_repair_write(&self.db)?;

        {
            let mut band_table = write_txn.open_table(BAND_TABLE)?;
            let mut sig_table = write_txn.open_table(SIGNATURE_TABLE)?;

            // First, collect all band updates
            let mut band_updates: FxHashMap<String, Vec<Uuid>> = FxHashMap::default();

            for (doc_id, signature, content_len) in documents {
                // Store signature
                let entry = DocumentEntry {
                    signature: signature.clone(),
                    content_len: *content_len,
                };
                let entry_bytes = bincode::serialize(&entry)?;
                sig_table.insert(doc_id.to_string().as_str(), entry_bytes.as_slice())?;

                // Collect band updates
                for band_idx in 0..self.num_bands {
                    let start = band_idx * self.rows_per_band;
                    let end = start + self.rows_per_band;
                    let band_hash = calculate_band_hash(&signature[start..end]);
                    let key = format!("{}:{}", band_idx, band_hash);
                    band_updates.entry(key).or_default().push(*doc_id);
                }
            }

            // Apply band updates
            for (key, new_ids) in band_updates {
                let mut doc_ids: Vec<Uuid> = if let Some(existing) = band_table.get(key.as_str())? {
                    bincode::deserialize(existing.value())?
                } else {
                    Vec::new()
                };
                doc_ids.extend(new_ids);
                doc_ids.sort();
                doc_ids.dedup();
                let doc_ids_bytes = bincode::serialize(&doc_ids)?;
                band_table.insert(key.as_str(), doc_ids_bytes.as_slice())?;
            }
        }

        write_txn.commit()?;
        Ok(())
    }

    /// Query for candidate duplicates
    pub fn query(&self, signature: &[u32]) -> Result<Vec<Uuid>> {
        validate_signature_len(signature, self.num_bands, self.rows_per_band)?;
        let read_txn = self.db.begin_read()?;
        let band_table = read_txn.open_table(BAND_TABLE)?;

        let mut candidates = Vec::new();

        for band_idx in 0..self.num_bands {
            let start = band_idx * self.rows_per_band;
            let end = start + self.rows_per_band;
            let band_hash = calculate_band_hash(&signature[start..end]);
            let key = format!("{}:{}", band_idx, band_hash);

            if let Some(doc_ids_bytes) = band_table.get(key.as_str())? {
                let doc_ids: Vec<Uuid> = bincode::deserialize(doc_ids_bytes.value())?;
                candidates.extend(doc_ids);
            }
        }

        // Deduplicate
        candidates.sort();
        candidates.dedup();
        Ok(candidates)
    }

    /// Get a document entry by ID
    pub fn get_document(&self, doc_id: &Uuid) -> Result<Option<DocumentEntry>> {
        let read_txn = self.db.begin_read()?;
        let sig_table = read_txn.open_table(SIGNATURE_TABLE)?;

        if let Some(entry_bytes) = sig_table.get(doc_id.to_string().as_str())? {
            let entry: DocumentEntry = bincode::deserialize(entry_bytes.value())?;
            Ok(Some(entry))
        } else {
            Ok(None)
        }
    }

    /// Get all document IDs
    pub fn all_doc_ids(&self) -> Result<Vec<Uuid>> {
        let read_txn = self.db.begin_read()?;
        let sig_table = read_txn.open_table(SIGNATURE_TABLE)?;

        let mut doc_ids = Vec::new();
        for item in sig_table.iter()? {
            let (key, _) = item?;
            if let Ok(uuid) = Uuid::parse_str(key.value()) {
                doc_ids.push(uuid);
            }
        }
        Ok(doc_ids)
    }

    /// Get count of documents
    pub fn count(&self) -> Result<u64> {
        let read_txn = self.db.begin_read()?;
        let sig_table = read_txn.open_table(SIGNATURE_TABLE)?;
        Ok(sig_table.len()?)
    }

    /// Compact the database (call periodically for performance)
    pub fn compact(&mut self) -> Result<()> {
        self.db.compact()?;
        Ok(())
    }

    /// Insert signatures only (no band updates) - O(1) per doc
    /// Call build_bands_from_signatures() after all signatures are inserted
    pub fn insert_signatures_only(&self, documents: &[(Uuid, Vec<u32>, usize)]) -> Result<()> {
        let write_txn = begin_quick_repair_write(&self.db)?;

        {
            let mut sig_table = write_txn.open_table(SIGNATURE_TABLE)?;

            for (doc_id, signature, content_len) in documents {
                let entry = DocumentEntry {
                    signature: signature.clone(),
                    content_len: *content_len,
                };
                let entry_bytes = bincode::serialize(&entry)?;
                sig_table.insert(doc_id.to_string().as_str(), entry_bytes.as_slice())?;
            }
        }

        write_txn.commit()?;
        Ok(())
    }

    /// Build band index from all signatures in memory, then flush to disk
    /// This is O(n) and much faster than incremental band updates
    pub fn build_bands_from_signatures(&self) -> Result<usize> {
        // Load all signatures into memory
        let read_txn = self.db.begin_read()?;
        let sig_table = read_txn.open_table(SIGNATURE_TABLE)?;
        let sig_count = sig_table.len()?;

        let pb = ProgressBar::new(sig_count);
        pb.set_style(
            ProgressStyle::default_bar()
                .template("[{elapsed_precise}] {bar:40.green/blue} {pos}/{len} ({per_sec}) Phase 1b: Loading signatures... ETA: {eta}")
                .unwrap(),
        );

        let mut all_sigs: Vec<(Uuid, Vec<u32>)> = Vec::with_capacity(sig_count as usize);
        for item in sig_table.iter()? {
            let (key, value) = item?;
            let doc_id = Uuid::parse_str(key.value())?;
            let entry: DocumentEntry = bincode::deserialize(value.value())?;
            all_sigs.push((doc_id, entry.signature));
            pb.inc(1);
        }
        drop(read_txn);
        pb.finish_with_message(format!("Loaded {} signatures", all_sigs.len()));

        let total_docs = all_sigs.len();
        if total_docs == 0 {
            return Ok(0);
        }

        // Build band buckets in memory with progress
        let pb2 = ProgressBar::new(total_docs as u64);
        pb2.set_style(
            ProgressStyle::default_bar()
                .template("[{elapsed_precise}] {bar:40.green/blue} {pos}/{len} ({per_sec}) Phase 1b: Building bands... ETA: {eta}")
                .unwrap(),
        );

        let mut band_buckets: FxHashMap<String, Vec<Uuid>> = FxHashMap::default();

        for (doc_id, signature) in &all_sigs {
            validate_signature_len(signature, self.num_bands, self.rows_per_band)?;
            for band_idx in 0..self.num_bands {
                let start = band_idx * self.rows_per_band;
                let end = start + self.rows_per_band;
                let band_hash = calculate_band_hash(&signature[start..end]);
                let key = format!("{}:{}", band_idx, band_hash);
                band_buckets.entry(key).or_default().push(*doc_id);
            }
            pb2.inc(1);
        }
        pb2.finish_with_message(format!("Built {} band buckets", band_buckets.len()));

        // Write all bands to disk in one transaction with progress
        let pb3 = ProgressBar::new(band_buckets.len() as u64);
        pb3.set_style(
            ProgressStyle::default_bar()
                .template("[{elapsed_precise}] {bar:40.green/blue} {pos}/{len} ({per_sec}) Phase 1b: Writing bands... ETA: {eta}")
                .unwrap(),
        );

        let write_txn = begin_quick_repair_write(&self.db)?;
        {
            let mut band_table = write_txn.open_table(BAND_TABLE)?;

            for (key, doc_ids) in band_buckets {
                let doc_ids_bytes = bincode::serialize(&doc_ids)?;
                band_table.insert(key.as_str(), doc_ids_bytes.as_slice())?;
                pb3.inc(1);
            }
        }
        write_txn.commit()?;
        pb3.finish_with_message("Bands written to disk");

        Ok(total_docs)
    }

    /// Load all signatures into memory for fast lookups
    /// Returns HashMap of doc_id -> (signature, content_len)
    pub fn load_all_signatures(&self) -> Result<FxHashMap<Uuid, (Vec<u32>, usize)>> {
        let read_txn = self.db.begin_read()?;
        let sig_table = read_txn.open_table(SIGNATURE_TABLE)?;

        let mut signatures = FxHashMap::default();
        for item in sig_table.iter()? {
            let (key, value) = item?;
            let doc_id = Uuid::parse_str(key.value())?;
            let entry: DocumentEntry = bincode::deserialize(value.value())?;
            signatures.insert(doc_id, (entry.signature, entry.content_len));
        }

        Ok(signatures)
    }

    /// Check if bands have been built (by checking if band table is non-empty)
    pub fn has_bands(&self) -> Result<bool> {
        let read_txn = self.db.begin_read()?;
        let band_table = read_txn.open_table(BAND_TABLE)?;
        Ok(band_table.len()? > 0)
    }

    /// Check if a document exists in the index
    pub fn has_document(&self, doc_id: &Uuid) -> Result<bool> {
        let read_txn = self.db.begin_read()?;
        let sig_table = read_txn.open_table(SIGNATURE_TABLE)?;
        Ok(sig_table.get(doc_id.to_string().as_str())?.is_some())
    }

    /// Load entire index into memory for parallel deduplication
    /// Returns an InMemoryLSH populated with all signatures and bands
    pub fn load_into_memory(&self) -> Result<(InMemoryLSH, FxHashMap<Uuid, usize>)> {
        let read_txn = self.db.begin_read()?;
        let sig_table = read_txn.open_table(SIGNATURE_TABLE)?;
        let band_table = read_txn.open_table(BAND_TABLE)?;

        let sig_count = sig_table.len()?;
        let band_count = band_table.len()?;

        // Progress bar for loading signatures
        let pb = ProgressBar::new(sig_count);
        pb.set_style(
            ProgressStyle::default_bar()
                .template("[{elapsed_precise}] {bar:40.cyan/blue} {pos}/{len} ({per_sec}) Loading signatures into memory... ETA: {eta}")
                .unwrap(),
        );

        // Load signatures and content lengths
        let mut signatures: FxHashMap<Uuid, Vec<u32>> = FxHashMap::default();
        let mut content_lens: FxHashMap<Uuid, usize> = FxHashMap::default();
        signatures.reserve(sig_count as usize);
        content_lens.reserve(sig_count as usize);

        for item in sig_table.iter()? {
            let (key, value) = item?;
            let doc_id = Uuid::parse_str(key.value())?;
            let entry: DocumentEntry = bincode::deserialize(value.value())?;
            signatures.insert(doc_id, entry.signature);
            content_lens.insert(doc_id, entry.content_len);
            pb.inc(1);
        }
        pb.finish_with_message(format!("Loaded {} signatures", signatures.len()));

        // Progress bar for loading bands
        let pb2 = ProgressBar::new(band_count);
        pb2.set_style(
            ProgressStyle::default_bar()
                .template("[{elapsed_precise}] {bar:40.green/blue} {pos}/{len} ({per_sec}) Loading band buckets into memory... ETA: {eta}")
                .unwrap(),
        );

        // Load band buckets
        let mut hash_tables: Vec<FxHashMap<u64, Vec<Uuid>>> =
            (0..self.num_bands).map(|_| FxHashMap::default()).collect();

        for item in band_table.iter()? {
            let (key, value) = item?;
            let key_str = key.value();
            // Parse "band_idx:band_hash"
            if let Some((band_idx_str, band_hash_str)) = key_str.split_once(':') {
                if let (Ok(band_idx), Ok(band_hash)) =
                    (band_idx_str.parse::<usize>(), band_hash_str.parse::<u64>())
                {
                    if band_idx < self.num_bands {
                        let doc_ids: Vec<Uuid> = bincode::deserialize(value.value())?;
                        hash_tables[band_idx].insert(band_hash, doc_ids);
                    }
                }
            }
            pb2.inc(1);
        }
        pb2.finish_with_message(format!("Loaded {} band buckets", band_count));

        // Build InMemoryLSH
        let lsh = InMemoryLSH {
            hash_tables,
            signatures,
            num_bands: self.num_bands,
            rows_per_band: self.rows_per_band,
        };

        Ok((lsh, content_lens))
    }

    /// Find the largest band buckets (potential pathological clusters)
    /// Returns Vec of (bucket_key, doc_count, sample_doc_ids) sorted by count descending
    pub fn find_large_buckets(
        &self,
        min_size: usize,
        limit: usize,
    ) -> Result<Vec<(String, usize, Vec<Uuid>)>> {
        use chrono::Local;
        use tracing::info;

        info!(
            "[{}] Opening LSH index for bucket scan...",
            Local::now().format("%H:%M:%S")
        );
        let read_txn = self.db.begin_read()?;
        let band_table = read_txn.open_table(BAND_TABLE)?;

        // Get total bucket count for progress bar
        info!("[{}] Counting buckets...", Local::now().format("%H:%M:%S"));
        let total_buckets = band_table.len()?;
        info!(
            "[{}] Found {} buckets to scan",
            Local::now().format("%H:%M:%S"),
            total_buckets
        );

        let pb = ProgressBar::new(total_buckets);
        pb.set_style(
            ProgressStyle::default_bar()
                .template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} buckets ({per_sec}) {msg}")
                .unwrap()
                .progress_chars("█▓▒░ "),
        );
        pb.set_message("Scanning for large buckets...");

        let mut large_buckets: Vec<(String, usize, Vec<Uuid>)> = Vec::new();

        for item in band_table.iter()? {
            pb.inc(1);
            let (key, value) = item?;
            let doc_ids: Vec<Uuid> = bincode::deserialize(value.value())?;

            if doc_ids.len() >= min_size {
                let key_str = key.value().to_string();
                // Keep first 5 doc IDs as samples
                let samples: Vec<Uuid> = doc_ids.iter().take(5).copied().collect();
                large_buckets.push((key_str, doc_ids.len(), samples));
                pb.set_message(format!("Found {} large buckets", large_buckets.len()));
            }
        }

        pb.finish_with_message(format!(
            "Scanned {} buckets, found {} large (>= {} docs)",
            total_buckets,
            large_buckets.len(),
            min_size
        ));

        // Sort by size descending
        large_buckets.sort_by_key(|b| std::cmp::Reverse(b.1));
        large_buckets.truncate(limit);

        Ok(large_buckets)
    }

    /// Get all doc IDs in a specific bucket
    pub fn get_bucket_docs(&self, bucket_key: &str) -> Result<Vec<Uuid>> {
        let read_txn = self.db.begin_read()?;
        let band_table = read_txn.open_table(BAND_TABLE)?;

        if let Some(value) = band_table.get(bucket_key)? {
            let doc_ids: Vec<Uuid> = bincode::deserialize(value.value())?;
            Ok(doc_ids)
        } else {
            Ok(Vec::new())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::minhash::RMinHash;
    use tempfile::tempdir;

    #[test]
    fn test_in_memory_lsh_basic() {
        let mut lsh = InMemoryLSH::new();

        let mut mh1 = RMinHash::default();
        mh1.update(&["hello".to_string(), "world".to_string()]);

        let mut mh2 = RMinHash::default();
        mh2.update(&["hello".to_string(), "world".to_string()]);

        let id1 = Uuid::new_v4();
        let id2 = Uuid::new_v4();

        lsh.insert(id1, mh1.digest_owned());
        lsh.insert(id2, mh2.digest_owned());

        // Same content should find each other
        let candidates = lsh.query(mh1.digest());
        assert!(candidates.contains(&id1));
        assert!(candidates.contains(&id2));
    }

    #[test]
    fn test_in_memory_lsh_different() {
        let mut lsh = InMemoryLSH::new();

        let mut mh1 = RMinHash::default();
        mh1.update(&["completely".to_string(), "different".to_string()]);

        let mut mh2 = RMinHash::default();
        mh2.update(&["totally".to_string(), "unique".to_string()]);

        let id1 = Uuid::new_v4();
        let id2 = Uuid::new_v4();

        lsh.insert(id1, mh1.digest_owned());
        lsh.insert(id2, mh2.digest_owned());

        // Different content should not find each other (usually)
        let candidates = lsh.query(mh1.digest());
        assert!(candidates.contains(&id1)); // Should find itself
    }

    #[test]
    fn test_disk_lsh_basic() -> Result<()> {
        let dir = tempdir()?;
        let db_path = dir.path().join("test.redb");

        let lsh = DiskLSH::open(&db_path)?;

        let mut mh = RMinHash::default();
        mh.update(&["test".to_string(), "document".to_string()]);

        let id = Uuid::new_v4();
        lsh.insert(id, mh.digest_owned(), 100)?;

        // Query should find it
        let candidates = lsh.query(mh.digest())?;
        assert!(candidates.contains(&id));

        // Get document should work
        let doc = lsh.get_document(&id)?;
        assert!(doc.is_some());
        assert_eq!(doc.unwrap().content_len, 100);

        Ok(())
    }

    #[test]
    fn test_disk_lsh_batch() -> Result<()> {
        let dir = tempdir()?;
        let db_path = dir.path().join("test.redb");

        let lsh = DiskLSH::open(&db_path)?;

        let mut docs = Vec::new();
        for i in 0..100 {
            let mut mh = RMinHash::default();
            mh.update(&[format!("document {}", i)]);
            docs.push((Uuid::new_v4(), mh.digest_owned(), 100 + i));
        }

        lsh.insert_batch(&docs)?;

        assert_eq!(lsh.count()?, 100);

        Ok(())
    }

    #[test]
    fn test_disk_lsh_reinsert_does_not_duplicate_bucket_membership() -> Result<()> {
        let dir = tempdir()?;
        let db_path = dir.path().join("test.redb");
        let lsh = DiskLSH::open(&db_path)?;

        let id = Uuid::new_v4();
        let sig = vec![42u32; NUM_BANDS * ROWS_PER_BAND];

        lsh.insert_batch(&[(id, sig.clone(), 100)])?;
        lsh.insert_batch(&[(id, sig.clone(), 100)])?;

        let candidates = lsh.query(&sig)?;
        assert_eq!(candidates, vec![id]);

        Ok(())
    }
}