forked from memvid/memvid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch_track.rs
More file actions
1324 lines (1149 loc) · 41.3 KB
/
sketch_track.rs
File metadata and controls
1324 lines (1149 loc) · 41.3 KB
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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Sketch Track for ultra-fast candidate generation in MV2 files.
//!
//! The Sketch Track stores fixed-size per-frame micro-indices that enable
//! fast candidate filtering before expensive BM25/vector reranking.
//!
//! Key components:
//! - **`SimHash`**: 64-bit locality-sensitive hash for approximate cosine similarity
//! - **Term Filter**: Compact bitset for fast query term overlap detection
//! - **Top Terms**: Hashed IDs of highest-weight terms for direct matching
//!
//! References:
//! - Charikar 2002: `SimHash` for cosine similarity estimation
//! - Manku et al. 2007: Web-scale near-duplicate detection
//! - `RocksDB` block filters: LSM-style membership filtering
use std::collections::HashMap;
use std::io::{Read, Seek, SeekFrom, Write};
use blake3::Hasher;
use serde::{Deserialize, Serialize};
use crate::error::{MemvidError, Result};
use crate::types::FrameId;
// ============================================================================
// Safe Byte Extraction Helpers
// ============================================================================
/// Extract a fixed-size array from a byte slice. Panics if slice is too short.
/// Only use this when the input buffer size is compile-time guaranteed.
#[inline]
fn read_u16_le(buf: &[u8], offset: usize) -> u16 {
u16::from_le_bytes([buf[offset], buf[offset + 1]])
}
#[inline]
fn read_u32_le(buf: &[u8], offset: usize) -> u32 {
u32::from_le_bytes([
buf[offset],
buf[offset + 1],
buf[offset + 2],
buf[offset + 3],
])
}
#[inline]
fn read_u64_le(buf: &[u8], offset: usize) -> u64 {
u64::from_le_bytes([
buf[offset],
buf[offset + 1],
buf[offset + 2],
buf[offset + 3],
buf[offset + 4],
buf[offset + 5],
buf[offset + 6],
buf[offset + 7],
])
}
// ============================================================================
// Constants
// ============================================================================
/// Magic bytes identifying the sketch track: "MVSK"
pub const SKETCH_TRACK_MAGIC: [u8; 4] = *b"MVSK";
/// Current version of the sketch track format.
pub const SKETCH_TRACK_VERSION: u16 = 1;
/// Default Hamming distance threshold for `SimHash` similarity (out of 64 bits).
/// Lower = stricter matching. 8-12 is typical for near-duplicate detection.
pub const DEFAULT_HAMMING_THRESHOLD: u32 = 10;
/// Term filter size in bytes for Small variant (128 bits).
pub const TERM_FILTER_SIZE_SMALL: usize = 16;
/// Term filter size in bytes for Medium variant (256 bits).
pub const TERM_FILTER_SIZE_MEDIUM: usize = 32;
/// Term filter size in bytes for Large variant (512 bits).
pub const TERM_FILTER_SIZE_LARGE: usize = 64;
/// Number of top terms to store in Small variant.
pub const TOP_TERMS_COUNT_SMALL: usize = 2;
/// Number of top terms to store in Medium variant.
pub const TOP_TERMS_COUNT_MEDIUM: usize = 4;
/// Number of top terms to store in Large variant.
pub const TOP_TERMS_COUNT_LARGE: usize = 6;
/// Entry size in bytes for Small variant (32 bytes).
pub const ENTRY_SIZE_SMALL: usize = 32;
/// Entry size in bytes for Medium variant (64 bytes).
pub const ENTRY_SIZE_MEDIUM: usize = 64;
/// Entry size in bytes for Large variant (96 bytes).
pub const ENTRY_SIZE_LARGE: usize = 96;
// ============================================================================
// Sketch Entry Variants
// ============================================================================
/// Sketch size variant determining storage overhead and precision.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[repr(u8)]
#[derive(Default)]
pub enum SketchVariant {
/// Small: 32 bytes/frame (128-bit filter, 2 top terms)
#[default]
Small = 0,
/// Medium: 64 bytes/frame (256-bit filter, 4 top terms)
Medium = 1,
/// Large: 96 bytes/frame (512-bit filter, 6 top terms, optional `MinHash`)
Large = 2,
}
impl SketchVariant {
/// Get the entry size in bytes for this variant.
#[must_use]
pub fn entry_size(&self) -> usize {
match self {
SketchVariant::Small => ENTRY_SIZE_SMALL,
SketchVariant::Medium => ENTRY_SIZE_MEDIUM,
SketchVariant::Large => ENTRY_SIZE_LARGE,
}
}
/// Get the term filter size in bytes for this variant.
#[must_use]
pub fn term_filter_size(&self) -> usize {
match self {
SketchVariant::Small => TERM_FILTER_SIZE_SMALL,
SketchVariant::Medium => TERM_FILTER_SIZE_MEDIUM,
SketchVariant::Large => TERM_FILTER_SIZE_LARGE,
}
}
/// Get the number of top terms stored for this variant.
#[must_use]
pub fn top_terms_count(&self) -> usize {
match self {
SketchVariant::Small => TOP_TERMS_COUNT_SMALL,
SketchVariant::Medium => TOP_TERMS_COUNT_MEDIUM,
SketchVariant::Large => TOP_TERMS_COUNT_LARGE,
}
}
/// Create from raw byte value.
#[must_use]
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(SketchVariant::Small),
1 => Some(SketchVariant::Medium),
2 => Some(SketchVariant::Large),
_ => None,
}
}
}
/// Feature flags for sketch entries.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct SketchFlags(u16);
impl SketchFlags {
/// Entry has valid `SimHash`.
pub const HAS_SIMHASH: u16 = 1 << 0;
/// Entry has valid term filter.
pub const HAS_TERM_FILTER: u16 = 1 << 1;
/// Entry has valid top terms.
pub const HAS_TOP_TERMS: u16 = 1 << 2;
/// Entry has `MinHash` (Large variant only).
pub const HAS_MINHASH: u16 = 1 << 3;
/// Entry was generated from short text (<50 tokens).
pub const SHORT_TEXT: u16 = 1 << 4;
/// Create new flags with all features enabled.
#[must_use]
pub fn all() -> Self {
Self(Self::HAS_SIMHASH | Self::HAS_TERM_FILTER | Self::HAS_TOP_TERMS)
}
/// Check if a flag is set.
#[must_use]
pub fn has(&self, flag: u16) -> bool {
self.0 & flag != 0
}
/// Set a flag.
pub fn set(&mut self, flag: u16) {
self.0 |= flag;
}
/// Get raw value.
#[must_use]
pub fn bits(&self) -> u16 {
self.0
}
/// Create from raw value.
#[must_use]
pub fn from_bits(bits: u16) -> Self {
Self(bits)
}
}
// ============================================================================
// Small Sketch Entry (32 bytes)
// ============================================================================
/// Small sketch entry: 32 bytes per frame.
///
/// Layout:
/// - simhash: u64 (8 bytes)
/// - `term_filter`: [u8; 16] (16 bytes) - 128-bit Bloom-like filter
/// - `top_terms`: [u32; 2] (8 bytes) - hashed IDs of top 2 terms
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SketchEntrySmall {
/// `SimHash` fingerprint (64-bit LSH for cosine similarity).
pub simhash: u64,
/// Compact term membership filter (128-bit).
pub term_filter: [u8; TERM_FILTER_SIZE_SMALL],
/// Hashed IDs of top-weighted terms.
pub top_terms: [u32; TOP_TERMS_COUNT_SMALL],
}
impl Default for SketchEntrySmall {
fn default() -> Self {
Self {
simhash: 0,
term_filter: [0u8; TERM_FILTER_SIZE_SMALL],
top_terms: [0u32; TOP_TERMS_COUNT_SMALL],
}
}
}
impl SketchEntrySmall {
/// Serialize to bytes (32 bytes).
#[must_use]
pub fn to_bytes(&self) -> [u8; ENTRY_SIZE_SMALL] {
let mut buf = [0u8; ENTRY_SIZE_SMALL];
buf[0..8].copy_from_slice(&self.simhash.to_le_bytes());
buf[8..24].copy_from_slice(&self.term_filter);
buf[24..28].copy_from_slice(&self.top_terms[0].to_le_bytes());
buf[28..32].copy_from_slice(&self.top_terms[1].to_le_bytes());
buf
}
/// Deserialize from bytes.
#[must_use]
pub fn from_bytes(buf: &[u8; ENTRY_SIZE_SMALL]) -> Self {
let simhash = read_u64_le(buf, 0);
let mut term_filter = [0u8; TERM_FILTER_SIZE_SMALL];
term_filter.copy_from_slice(&buf[8..24]);
let top_terms = [read_u32_le(buf, 24), read_u32_le(buf, 28)];
Self {
simhash,
term_filter,
top_terms,
}
}
}
// ============================================================================
// Medium Sketch Entry (64 bytes)
// ============================================================================
/// Medium sketch entry: 64 bytes per frame.
///
/// Layout:
/// - simhash: u64 (8 bytes)
/// - `term_filter`: [u8; 32] (32 bytes) - 256-bit filter
/// - `top_terms`: [u32; 4] (16 bytes) - hashed IDs of top 4 terms
/// - `term_weight_sum`: u16 (2 bytes)
/// - flags: u16 (2 bytes)
/// - `length_hint`: u16 (2 bytes) - token count bucket
/// - reserved: u16 (2 bytes)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SketchEntryMedium {
/// `SimHash` fingerprint.
pub simhash: u64,
/// Compact term membership filter (256-bit).
pub term_filter: [u8; TERM_FILTER_SIZE_MEDIUM],
/// Hashed IDs of top-weighted terms.
pub top_terms: [u32; TOP_TERMS_COUNT_MEDIUM],
/// Sum of top-term weights (for normalization).
pub term_weight_sum: u16,
/// Feature flags.
pub flags: u16,
/// Token count bucket (0-255 maps to ranges).
pub length_hint: u16,
/// Reserved for future use.
pub reserved: u16,
}
impl Default for SketchEntryMedium {
fn default() -> Self {
Self {
simhash: 0,
term_filter: [0u8; TERM_FILTER_SIZE_MEDIUM],
top_terms: [0u32; TOP_TERMS_COUNT_MEDIUM],
term_weight_sum: 0,
flags: 0,
length_hint: 0,
reserved: 0,
}
}
}
impl SketchEntryMedium {
/// Serialize to bytes (64 bytes).
#[must_use]
pub fn to_bytes(&self) -> [u8; ENTRY_SIZE_MEDIUM] {
let mut buf = [0u8; ENTRY_SIZE_MEDIUM];
let mut offset = 0;
buf[offset..offset + 8].copy_from_slice(&self.simhash.to_le_bytes());
offset += 8;
buf[offset..offset + TERM_FILTER_SIZE_MEDIUM].copy_from_slice(&self.term_filter);
offset += TERM_FILTER_SIZE_MEDIUM;
for term in &self.top_terms {
buf[offset..offset + 4].copy_from_slice(&term.to_le_bytes());
offset += 4;
}
buf[offset..offset + 2].copy_from_slice(&self.term_weight_sum.to_le_bytes());
offset += 2;
buf[offset..offset + 2].copy_from_slice(&self.flags.to_le_bytes());
offset += 2;
buf[offset..offset + 2].copy_from_slice(&self.length_hint.to_le_bytes());
offset += 2;
buf[offset..offset + 2].copy_from_slice(&self.reserved.to_le_bytes());
buf
}
/// Deserialize from bytes.
#[must_use]
pub fn from_bytes(buf: &[u8; ENTRY_SIZE_MEDIUM]) -> Self {
let mut offset = 0;
let simhash = read_u64_le(buf, offset);
offset += 8;
let mut term_filter = [0u8; TERM_FILTER_SIZE_MEDIUM];
term_filter.copy_from_slice(&buf[offset..offset + TERM_FILTER_SIZE_MEDIUM]);
offset += TERM_FILTER_SIZE_MEDIUM;
let mut top_terms = [0u32; TOP_TERMS_COUNT_MEDIUM];
for (i, term) in top_terms.iter_mut().enumerate() {
*term = read_u32_le(buf, offset + i * 4);
}
offset += TOP_TERMS_COUNT_MEDIUM * 4;
let term_weight_sum = read_u16_le(buf, offset);
offset += 2;
let flags = read_u16_le(buf, offset);
offset += 2;
let length_hint = read_u16_le(buf, offset);
offset += 2;
let reserved = read_u16_le(buf, offset);
Self {
simhash,
term_filter,
top_terms,
term_weight_sum,
flags,
length_hint,
reserved,
}
}
}
// ============================================================================
// Unified Sketch Entry (supports all variants)
// ============================================================================
/// Unified sketch entry that can represent any variant.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SketchEntry {
/// Frame ID this sketch belongs to.
pub frame_id: FrameId,
/// `SimHash` fingerprint (64-bit LSH).
pub simhash: u64,
/// Compact term membership filter (variable size).
pub term_filter: Vec<u8>,
/// Hashed IDs of top-weighted terms.
pub top_terms: Vec<u32>,
/// Sum of top-term weights.
pub term_weight_sum: u16,
/// Feature flags.
pub flags: SketchFlags,
/// Token count bucket.
pub length_hint: u16,
}
impl Default for SketchEntry {
fn default() -> Self {
Self {
frame_id: 0,
simhash: 0,
term_filter: vec![0u8; TERM_FILTER_SIZE_SMALL],
top_terms: vec![0u32; TOP_TERMS_COUNT_SMALL],
term_weight_sum: 0,
flags: SketchFlags::default(),
length_hint: 0,
}
}
}
impl SketchEntry {
/// Create a new sketch entry for a frame.
#[must_use]
pub fn new(frame_id: FrameId, variant: SketchVariant) -> Self {
Self {
frame_id,
simhash: 0,
term_filter: vec![0u8; variant.term_filter_size()],
top_terms: vec![0u32; variant.top_terms_count()],
term_weight_sum: 0,
flags: SketchFlags::default(),
length_hint: 0,
}
}
/// Convert to Small variant bytes.
#[must_use]
pub fn to_small_bytes(&self) -> [u8; ENTRY_SIZE_SMALL] {
let mut small = SketchEntrySmall::default();
small.simhash = self.simhash;
if self.term_filter.len() >= TERM_FILTER_SIZE_SMALL {
small
.term_filter
.copy_from_slice(&self.term_filter[..TERM_FILTER_SIZE_SMALL]);
}
for (i, term) in self
.top_terms
.iter()
.take(TOP_TERMS_COUNT_SMALL)
.enumerate()
{
small.top_terms[i] = *term;
}
small.to_bytes()
}
/// Convert to Medium variant bytes.
#[must_use]
pub fn to_medium_bytes(&self) -> [u8; ENTRY_SIZE_MEDIUM] {
let mut medium = SketchEntryMedium::default();
medium.simhash = self.simhash;
if self.term_filter.len() >= TERM_FILTER_SIZE_MEDIUM {
medium
.term_filter
.copy_from_slice(&self.term_filter[..TERM_FILTER_SIZE_MEDIUM]);
} else {
medium.term_filter[..self.term_filter.len()].copy_from_slice(&self.term_filter);
}
for (i, term) in self
.top_terms
.iter()
.take(TOP_TERMS_COUNT_MEDIUM)
.enumerate()
{
medium.top_terms[i] = *term;
}
medium.term_weight_sum = self.term_weight_sum;
medium.flags = self.flags.bits();
medium.length_hint = self.length_hint;
medium.to_bytes()
}
/// Create from Small variant bytes.
#[must_use]
pub fn from_small_bytes(frame_id: FrameId, buf: &[u8; ENTRY_SIZE_SMALL]) -> Self {
let small = SketchEntrySmall::from_bytes(buf);
Self {
frame_id,
simhash: small.simhash,
term_filter: small.term_filter.to_vec(),
top_terms: small.top_terms.to_vec(),
term_weight_sum: 0,
flags: SketchFlags::all(),
length_hint: 0,
}
}
/// Create from Medium variant bytes.
#[must_use]
pub fn from_medium_bytes(frame_id: FrameId, buf: &[u8; ENTRY_SIZE_MEDIUM]) -> Self {
let medium = SketchEntryMedium::from_bytes(buf);
Self {
frame_id,
simhash: medium.simhash,
term_filter: medium.term_filter.to_vec(),
top_terms: medium.top_terms.to_vec(),
term_weight_sum: medium.term_weight_sum,
flags: SketchFlags::from_bits(medium.flags),
length_hint: medium.length_hint,
}
}
/// Compute Hamming distance between this sketch's `SimHash` and another.
#[must_use]
pub fn hamming_distance(&self, other_simhash: u64) -> u32 {
(self.simhash ^ other_simhash).count_ones()
}
/// Check if term filter might contain any of the query terms.
/// Returns true if there's potential overlap (may have false positives).
#[must_use]
pub fn term_filter_maybe_overlaps(&self, query_filter: &[u8]) -> bool {
// AND the filters and check if any bits are set
self.term_filter
.iter()
.zip(query_filter.iter())
.any(|(a, b)| a & b != 0)
}
/// Count matching top terms with a query's top terms.
#[must_use]
pub fn count_matching_top_terms(&self, query_terms: &[u32]) -> usize {
self.top_terms
.iter()
.filter(|t| **t != 0 && query_terms.contains(t))
.count()
}
}
// ============================================================================
// SimHash Implementation
// ============================================================================
/// Compute `SimHash` from weighted tokens.
///
/// `SimHash` is a locality-sensitive hash that approximates cosine similarity.
/// Documents with similar content will have `SimHash` values with small Hamming distance.
///
/// Algorithm:
/// 1. For each token, compute a 64-bit hash
/// 2. Multiply each bit position by the token's weight (+weight if bit=1, -weight if bit=0)
/// 3. Sum across all tokens
/// 4. Final hash: bit i = 1 if sum[i] > 0, else 0
#[must_use]
pub fn compute_simhash(tokens: &[(u64, i32)]) -> u64 {
if tokens.is_empty() {
return 0;
}
// Accumulator for each bit position
let mut v = [0i64; 64];
for (token_hash, weight) in tokens {
let weight = i64::from(*weight);
for i in 0..64 {
if (token_hash >> i) & 1 == 1 {
v[i] += weight;
} else {
v[i] -= weight;
}
}
}
// Build final hash
let mut simhash = 0u64;
for (i, &sum) in v.iter().enumerate() {
if sum > 0 {
simhash |= 1u64 << i;
}
}
simhash
}
/// Hash a token string to u64 using xxHash-style mixing.
/// Deterministic across platforms.
#[must_use]
pub fn hash_token(token: &str) -> u64 {
// Use BLAKE3 for determinism, take first 8 bytes as u64
let hash = blake3::hash(token.as_bytes());
let bytes = hash.as_bytes();
read_u64_le(bytes, 0)
}
/// Hash a token to u32 for `top_terms` storage.
#[must_use]
pub fn hash_token_u32(token: &str) -> u32 {
let h = hash_token(token);
#[allow(clippy::cast_possible_truncation)]
let res = (h ^ (h >> 32)) as u32;
res
}
// ============================================================================
// Term Filter (Bloom-like Bitset)
// ============================================================================
/// Build a term filter bitset from token hashes.
///
/// Each token sets multiple bits (k hash functions simulated by bit rotation).
/// This creates a compact membership test with configurable false positive rate.
#[must_use]
pub fn build_term_filter(token_hashes: &[u64], filter_size_bytes: usize) -> Vec<u8> {
let mut filter = vec![0u8; filter_size_bytes];
let filter_bits = filter_size_bytes * 8;
for &hash in token_hashes {
// Use 3 hash functions (simulated via rotation)
let h1 = usize::try_from(hash % (filter_bits as u64)).unwrap_or(0);
let h2 = usize::try_from((hash >> 16) % (filter_bits as u64)).unwrap_or(0);
let h3 = usize::try_from((hash >> 32) % (filter_bits as u64)).unwrap_or(0);
filter[h1 / 8] |= 1 << (h1 % 8);
filter[h2 / 8] |= 1 << (h2 % 8);
filter[h3 / 8] |= 1 << (h3 % 8);
}
filter
}
/// Check if a term filter might contain a token.
#[must_use]
pub fn term_filter_maybe_contains(filter: &[u8], token_hash: u64) -> bool {
let filter_bits = filter.len() * 8;
let h1 = usize::try_from(token_hash % (filter_bits as u64)).unwrap_or(0);
let h2 = usize::try_from((token_hash >> 16) % (filter_bits as u64)).unwrap_or(0);
let h3 = usize::try_from((token_hash >> 32) % (filter_bits as u64)).unwrap_or(0);
(filter[h1 / 8] & (1 << (h1 % 8)) != 0)
&& (filter[h2 / 8] & (1 << (h2 % 8)) != 0)
&& (filter[h3 / 8] & (1 << (h3 % 8)) != 0)
}
// ============================================================================
// Tokenization (Deterministic)
// ============================================================================
/// Tokenize text deterministically for sketch generation.
///
/// Rules:
/// - Unicode NFKC normalization
/// - Lowercase
/// - Split on whitespace and punctuation
/// - Keep only alphanumeric tokens >= 2 chars
#[must_use]
pub fn tokenize_for_sketch(text: &str) -> Vec<String> {
use unicode_normalization::UnicodeNormalization;
// NFKC normalize and lowercase
let normalized: String = text.nfkc().collect::<String>().to_lowercase();
// Split on non-alphanumeric, filter short tokens
normalized
.split(|c: char| !c.is_alphanumeric())
.filter(|s| s.len() >= 2)
.map(String::from)
.collect()
}
/// Compute token weights using TF with cap and optional IDF.
///
/// Returns (`token_hash`, weight) pairs sorted by weight descending.
#[must_use]
pub fn compute_token_weights(
tokens: &[String],
idf_map: Option<&HashMap<String, f32>>,
) -> Vec<(u64, i32)> {
// Count term frequencies
let mut tf: HashMap<&str, u32> = HashMap::new();
for token in tokens {
*tf.entry(token.as_str()).or_default() += 1;
}
// Compute weights: capped_tf * idf_bucket
let mut weighted: Vec<(u64, i32)> = tf
.into_iter()
.map(|(token, count)| {
let capped_tf = count.min(3) as f32; // Cap TF at 3
let idf = idf_map
.and_then(|m| m.get(token))
.copied()
.unwrap_or(1.0)
.max(0.1); // Default IDF = 1.0
#[allow(clippy::cast_possible_truncation)]
let weight = (capped_tf * idf * 100.0) as i32; // Scale to integer
(hash_token(token), weight.max(1))
})
.collect();
// Sort by weight descending, then by hash for determinism
weighted.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
weighted
}
/// Extract top K term hashes by weight.
#[must_use]
pub fn extract_top_terms(weighted_tokens: &[(u64, i32)], k: usize) -> Vec<u32> {
weighted_tokens
.iter()
.take(k)
.map(|(h, _)| {
#[allow(clippy::cast_possible_truncation)]
let res = (*h ^ (*h >> 32)) as u32;
res
})
.collect()
}
// ============================================================================
// Sketch Generation
// ============================================================================
/// Generate a complete sketch entry from text.
#[must_use]
pub fn generate_sketch(
frame_id: FrameId,
text: &str,
variant: SketchVariant,
idf_map: Option<&HashMap<String, f32>>,
) -> SketchEntry {
let tokens = tokenize_for_sketch(text);
let token_count = tokens.len();
if tokens.is_empty() {
let mut entry = SketchEntry::new(frame_id, variant);
entry.flags.set(SketchFlags::SHORT_TEXT);
return entry;
}
let weighted = compute_token_weights(&tokens, idf_map);
// Compute SimHash
let simhash = compute_simhash(&weighted);
// Build term filter
let token_hashes: Vec<u64> = weighted.iter().map(|(h, _)| *h).collect();
let term_filter = build_term_filter(&token_hashes, variant.term_filter_size());
// Extract top terms
let top_terms = extract_top_terms(&weighted, variant.top_terms_count());
// Compute weight sum
let term_weight_sum: u32 = weighted
.iter()
.take(variant.top_terms_count())
.map(|(_, w)| *w as u32)
.sum();
// Length hint: bucket token count (0-255 = 0-2550 tokens in steps of 10)
#[allow(clippy::cast_possible_truncation)]
let length_hint = ((token_count / 10).min(255)) as u16;
let mut flags = SketchFlags::all();
if token_count < 50 {
flags.set(SketchFlags::SHORT_TEXT);
}
#[allow(clippy::cast_possible_truncation)]
let term_weight_sum = term_weight_sum.min(u32::from(u16::MAX)) as u16;
SketchEntry {
frame_id,
simhash,
term_filter,
top_terms,
term_weight_sum,
flags,
length_hint,
}
}
// ============================================================================
// Query Sketch
// ============================================================================
/// Sketch generated from a query for candidate matching.
#[derive(Debug, Clone)]
pub struct QuerySketch {
/// `SimHash` of the query.
pub simhash: u64,
/// Term filter for the query.
pub term_filter: Vec<u8>,
/// Top term hashes from the query.
pub top_terms: Vec<u32>,
/// Token count of the query.
pub token_count: usize,
}
impl QuerySketch {
/// Build a query sketch from query text.
#[must_use]
pub fn from_query(query: &str, variant: SketchVariant) -> Self {
let tokens = tokenize_for_sketch(query);
let token_count = tokens.len();
if tokens.is_empty() {
return Self {
simhash: 0,
term_filter: vec![0u8; variant.term_filter_size()],
top_terms: Vec::new(),
token_count: 0,
};
}
let weighted = compute_token_weights(&tokens, None);
let simhash = compute_simhash(&weighted);
let token_hashes: Vec<u64> = weighted.iter().map(|(h, _)| *h).collect();
let term_filter = build_term_filter(&token_hashes, variant.term_filter_size());
let top_terms = extract_top_terms(&weighted, variant.top_terms_count());
Self {
simhash,
term_filter,
top_terms,
token_count,
}
}
/// Score a sketch entry against this query.
/// Returns a score in [0.0, 1.0] where higher is better.
#[must_use]
pub fn score_entry(&self, entry: &SketchEntry, hamming_threshold: u32) -> Option<f32> {
// Check term filter overlap first (fast rejection)
if !entry.term_filter_maybe_overlaps(&self.term_filter) {
return None;
}
// Check SimHash Hamming distance
let hamming = entry.hamming_distance(self.simhash);
if hamming > hamming_threshold {
return None;
}
// Compute score from multiple signals
let w_term = 0.5f32;
let w_sim = 0.4f32;
let w_len = 0.1f32;
// Top term overlap score
let term_overlap = entry.count_matching_top_terms(&self.top_terms) as f32;
let max_terms = self.top_terms.len().max(1) as f32;
let term_score = term_overlap / max_terms;
// SimHash similarity score (inverse of normalized Hamming)
let sim_score = 1.0 - (hamming as f32 / 64.0);
// Length compatibility (penalize very different lengths)
let query_len_bucket = ((self.token_count / 10).min(255)) as f32;
let entry_len_bucket = f32::from(entry.length_hint);
let len_diff = (query_len_bucket - entry_len_bucket).abs();
let len_score = 1.0 / (1.0 + len_diff * 0.1);
let score = w_term * term_score + w_sim * sim_score + w_len * len_score;
Some(score)
}
}
// ============================================================================
// Sketch Track (Collection of Entries)
// ============================================================================
/// In-memory representation of a sketch track.
#[derive(Debug, Clone)]
pub struct SketchTrack {
/// Sketch variant used.
pub variant: SketchVariant,
/// Sketch entries indexed by frame ID.
entries: HashMap<FrameId, SketchEntry>,
/// Ordered list of frame IDs for sequential scanning.
frame_order: Vec<FrameId>,
}
impl Default for SketchTrack {
fn default() -> Self {
Self::new(SketchVariant::Small)
}
}
impl SketchTrack {
/// Create a new empty sketch track.
#[must_use]
pub fn new(variant: SketchVariant) -> Self {
Self {
variant,
entries: HashMap::new(),
frame_order: Vec::new(),
}
}
/// Add or update a sketch entry.
pub fn insert(&mut self, entry: SketchEntry) {
let frame_id = entry.frame_id;
if !self.entries.contains_key(&frame_id) {
self.frame_order.push(frame_id);
}
self.entries.insert(frame_id, entry);
}
/// Get a sketch entry by frame ID.
#[must_use]
pub fn get(&self, frame_id: FrameId) -> Option<&SketchEntry> {
self.entries.get(&frame_id)
}
/// Get the number of entries.
#[must_use]
pub fn len(&self) -> usize {
self.entries.len()
}
/// Check if empty.
#[must_use]
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
/// Iterate over entries in frame order.
pub fn iter(&self) -> impl Iterator<Item = &SketchEntry> {
self.frame_order
.iter()
.filter_map(|id| self.entries.get(id))
}
/// Find candidate frames matching a query.
///
/// Returns (`frame_id`, score) pairs sorted by score descending.
#[must_use]
pub fn find_candidates(
&self,
query: &QuerySketch,
hamming_threshold: u32,
max_candidates: usize,
) -> Vec<(FrameId, f32)> {
let mut candidates: Vec<(FrameId, f32)> = self
.iter()
.filter_map(|entry| {
query
.score_entry(entry, hamming_threshold)
.map(|score| (entry.frame_id, score))
})
.collect();
// Sort by score descending
candidates.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
// Limit to max_candidates
candidates.truncate(max_candidates);
candidates
}
/// Get statistics about the track.
#[must_use]
pub fn stats(&self) -> SketchTrackStats {
let entry_count = self.entries.len() as u64;
let size_bytes = entry_count * self.variant.entry_size() as u64;
let short_text_count = self
.entries
.values()
.filter(|e| e.flags.has(SketchFlags::SHORT_TEXT))
.count() as u64;
SketchTrackStats {
variant: self.variant,
entry_count,
size_bytes,
short_text_count,
}
}
}
/// Statistics about a sketch track.
#[derive(Debug, Clone)]
pub struct SketchTrackStats {
/// Variant used.
pub variant: SketchVariant,
/// Number of entries.
pub entry_count: u64,
/// Size in bytes.
pub size_bytes: u64,
/// Number of entries marked as short text.
pub short_text_count: u64,
}
// ============================================================================
// Binary IO
// ============================================================================
/// Header for the sketch track binary format.
#[derive(Debug, Clone, Copy)]
pub struct SketchTrackHeader {
/// Magic bytes (MVSK).
pub magic: [u8; 4],
/// Format version.
pub version: u16,