reasonkit-mem 0.1.7

High-performance vector database & RAG memory layer - hybrid search, embeddings, RAPTOR trees, BM25 fusion, and semantic retrieval for AI systems
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
//! Background Sync Worker for Hot-to-Cold Memory Migration
//!
//! This module implements a background worker that periodically moves data from
//! hot (fast, in-memory) storage to cold (persistent, disk-based) storage.
//!
//! ## Architecture
//!
//! ```text
//! +-------------+     SyncWorker     +-------------+
//! |  HotMemory  | -----------------> | ColdMemory  |
//! | (In-memory) |     (periodic)     |  (On-disk)  |
//! +-------------+                    +-------------+
//!       |                                   ^
//!       |           WriteAheadLog           |
//!       +-----------> (WAL) ----------------+
//!                  (durability)
//! ```
//!
//! ## Features
//!
//! - Periodic background synchronization using tokio
//! - Age-based migration from hot to cold storage
//! - WAL checkpointing for crash recovery
//! - Graceful shutdown with completion signaling
//! - Configurable sync intervals and batch sizes
//!
//! ## Usage
//!
//! ```rust,ignore
//! use reasonkit_mem::storage::sync_worker::{
//!     spawn_sync_worker, SyncWorkerConfig, HotMemory, ColdMemory, WriteAheadLog,
//! };
//! use std::sync::Arc;
//! use std::time::Duration;
//!
//! let hot = Arc::new(HotMemory::new(config));
//! let cold = Arc::new(ColdMemory::new(path).await?);
//! let wal = Arc::new(WriteAheadLog::new(wal_path).await?);
//!
//! let config = SyncWorkerConfig {
//!     sync_interval: Duration::from_secs(60),
//!     hot_to_cold_age: Duration::from_secs(300),
//!     batch_size: 100,
//! };
//!
//! let (tx, handle) = spawn_sync_worker(hot, cold, wal, config);
//!
//! // Later, graceful shutdown:
//! let (done_tx, done_rx) = tokio::sync::oneshot::channel();
//! tx.send(SyncCommand::Shutdown(done_tx)).await?;
//! done_rx.await?;
//! handle.await?;
//! ```

use crate::{MemError, MemResult};
use chrono::{DateTime, Utc};
use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::sync::{mpsc, oneshot, RwLock};
use tracing::{debug, error, info, warn};
use uuid::Uuid;

// ============================================================================
// Commands and Configuration
// ============================================================================

/// Commands that can be sent to the sync worker
#[derive(Debug)]
pub enum SyncCommand {
    /// Trigger an immediate sync from hot to cold
    Sync,
    /// Trigger a WAL checkpoint
    Checkpoint,
    /// Gracefully shutdown the worker (sends completion signal)
    Shutdown(oneshot::Sender<()>),
}

/// Configuration for the sync worker
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SyncWorkerConfig {
    /// Interval between automatic sync operations
    pub sync_interval: Duration,
    /// Minimum age for entries to be moved from hot to cold
    pub hot_to_cold_age: Duration,
    /// Maximum number of entries to process in a single batch
    pub batch_size: usize,
}

impl Default for SyncWorkerConfig {
    fn default() -> Self {
        Self {
            sync_interval: Duration::from_secs(60),
            hot_to_cold_age: Duration::from_secs(300), // 5 minutes
            batch_size: 100,
        }
    }
}

// ============================================================================
// Memory Entry Types
// ============================================================================

/// A memory entry stored in hot or cold storage
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryEntry {
    /// Unique identifier for this entry
    pub id: Uuid,
    /// The actual data (embeddings, metadata, etc.)
    pub data: MemoryData,
    /// When this entry was created
    pub created_at: DateTime<Utc>,
    /// When this entry was last accessed
    pub last_accessed: DateTime<Utc>,
    /// Access count for LRU/LFU eviction
    pub access_count: u64,
    /// Optional TTL for automatic expiration
    pub ttl: Option<Duration>,
}

/// Types of data that can be stored in memory
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MemoryData {
    /// Dense embedding vector
    Embedding(Vec<f32>),
    /// Document chunk with text
    Chunk {
        text: String,
        document_id: Uuid,
        position: usize,
    },
    /// Metadata blob
    Metadata(HashMap<String, serde_json::Value>),
    /// RAPTOR tree node
    RaptorNode {
        level: usize,
        summary: String,
        children: Vec<Uuid>,
    },
}

impl MemoryEntry {
    /// Create a new memory entry
    pub fn new(id: Uuid, data: MemoryData) -> Self {
        let now = Utc::now();
        Self {
            id,
            data,
            created_at: now,
            last_accessed: now,
            access_count: 0,
            ttl: None,
        }
    }

    /// Create an embedding entry
    pub fn embedding(id: Uuid, vector: Vec<f32>) -> Self {
        Self::new(id, MemoryData::Embedding(vector))
    }

    /// Create a chunk entry
    pub fn chunk(id: Uuid, text: String, document_id: Uuid, position: usize) -> Self {
        Self::new(
            id,
            MemoryData::Chunk {
                text,
                document_id,
                position,
            },
        )
    }

    /// Check if this entry has expired based on its TTL
    pub fn is_expired(&self) -> bool {
        if let Some(ttl) = self.ttl {
            let age = Utc::now()
                .signed_duration_since(self.created_at)
                .to_std()
                .unwrap_or(Duration::ZERO);
            age > ttl
        } else {
            false
        }
    }

    /// Check if this entry is old enough to be moved to cold storage
    pub fn is_cold_eligible(&self, threshold: Duration) -> bool {
        let age = Utc::now()
            .signed_duration_since(self.last_accessed)
            .to_std()
            .unwrap_or(Duration::ZERO);
        age > threshold
    }

    /// Record an access to this entry
    pub fn record_access(&mut self) {
        self.last_accessed = Utc::now();
        self.access_count += 1;
    }

    /// Get the serialized size estimate in bytes
    pub fn estimated_size(&self) -> usize {
        match &self.data {
            MemoryData::Embedding(v) => v.len() * 4 + 64, // floats + overhead
            MemoryData::Chunk { text, .. } => text.len() + 128,
            MemoryData::Metadata(m) => m.len() * 64, // rough estimate
            MemoryData::RaptorNode {
                summary, children, ..
            } => summary.len() + children.len() * 16 + 32,
        }
    }
}

// ============================================================================
// Hot Memory (In-Memory Cache)
// ============================================================================

/// Configuration for hot memory
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HotMemoryConfig {
    /// Maximum number of entries
    pub max_entries: usize,
    /// Maximum memory usage in bytes
    pub max_memory_bytes: usize,
    /// Enable LRU eviction
    pub enable_lru_eviction: bool,
}

impl Default for HotMemoryConfig {
    fn default() -> Self {
        Self {
            max_entries: 10_000,
            max_memory_bytes: 256 * 1024 * 1024, // 256 MB
            enable_lru_eviction: true,
        }
    }
}

/// Hot memory storage (fast, in-memory)
///
/// Uses DashMap for concurrent access with minimal locking.
pub struct HotMemory {
    /// The actual storage
    entries: DashMap<Uuid, MemoryEntry>,
    /// Configuration
    config: HotMemoryConfig,
    /// Current memory usage estimate
    memory_usage: AtomicU64,
    /// Statistics
    stats: Arc<RwLock<HotMemoryStats>>,
}

/// Statistics for hot memory
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct HotMemoryStats {
    /// Total entries stored
    pub entry_count: usize,
    /// Total memory usage in bytes
    pub memory_bytes: u64,
    /// Cache hits
    pub hits: u64,
    /// Cache misses
    pub misses: u64,
    /// Entries evicted due to capacity
    pub evictions: u64,
}

impl HotMemory {
    /// Create a new hot memory instance
    pub fn new(config: HotMemoryConfig) -> Self {
        Self {
            entries: DashMap::with_capacity(config.max_entries),
            config,
            memory_usage: AtomicU64::new(0),
            stats: Arc::new(RwLock::new(HotMemoryStats::default())),
        }
    }

    /// Insert an entry into hot memory
    pub async fn insert(&self, entry: MemoryEntry) -> MemResult<()> {
        let size = entry.estimated_size() as u64;
        let id = entry.id;

        // Check capacity and evict if necessary
        if self.config.enable_lru_eviction {
            while self.entries.len() >= self.config.max_entries
                || self.memory_usage.load(Ordering::Relaxed) + size
                    > self.config.max_memory_bytes as u64
            {
                if !self.evict_lru().await {
                    break;
                }
            }
        }

        // Remove old entry if exists
        if let Some(old) = self.entries.remove(&id) {
            self.memory_usage
                .fetch_sub(old.1.estimated_size() as u64, Ordering::Relaxed);
        }

        // Insert new entry
        self.entries.insert(id, entry);
        self.memory_usage.fetch_add(size, Ordering::Relaxed);

        // Update stats
        {
            let mut stats = self.stats.write().await;
            stats.entry_count = self.entries.len();
            stats.memory_bytes = self.memory_usage.load(Ordering::Relaxed);
        }

        Ok(())
    }

    /// Get an entry from hot memory (updates access time)
    pub async fn get(&self, id: &Uuid) -> Option<MemoryEntry> {
        if let Some(mut entry) = self.entries.get_mut(id) {
            entry.record_access();
            let mut stats = self.stats.write().await;
            stats.hits += 1;
            Some(entry.clone())
        } else {
            let mut stats = self.stats.write().await;
            stats.misses += 1;
            None
        }
    }

    /// Get an entry without updating access time (for sync operations)
    pub fn get_readonly(&self, id: &Uuid) -> Option<MemoryEntry> {
        self.entries.get(id).map(|e| e.clone())
    }

    /// Remove an entry from hot memory
    pub async fn remove(&self, id: &Uuid) -> Option<MemoryEntry> {
        if let Some((_, entry)) = self.entries.remove(id) {
            self.memory_usage
                .fetch_sub(entry.estimated_size() as u64, Ordering::Relaxed);
            let mut stats = self.stats.write().await;
            stats.entry_count = self.entries.len();
            stats.memory_bytes = self.memory_usage.load(Ordering::Relaxed);
            Some(entry)
        } else {
            None
        }
    }

    /// Get entries eligible for cold storage migration
    pub fn get_cold_eligible(&self, threshold: Duration, limit: usize) -> Vec<MemoryEntry> {
        let mut eligible = Vec::new();

        for entry_ref in self.entries.iter() {
            if entry_ref.is_cold_eligible(threshold) && !entry_ref.is_expired() {
                eligible.push(entry_ref.clone());
                if eligible.len() >= limit {
                    break;
                }
            }
        }

        // Sort by last_accessed (oldest first)
        eligible.sort_by(|a, b| a.last_accessed.cmp(&b.last_accessed));
        eligible
    }

    /// Evict the least recently used entry
    async fn evict_lru(&self) -> bool {
        // Find LRU entry
        let lru_id = self
            .entries
            .iter()
            .min_by(|a, b| a.last_accessed.cmp(&b.last_accessed))
            .map(|e| *e.key());

        if let Some(id) = lru_id {
            self.remove(&id).await;
            let mut stats = self.stats.write().await;
            stats.evictions += 1;
            true
        } else {
            false
        }
    }

    /// Get current statistics
    pub async fn stats(&self) -> HotMemoryStats {
        self.stats.read().await.clone()
    }

    /// Get number of entries
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
}

// ============================================================================
// Cold Memory (Persistent Disk Storage)
// ============================================================================

/// Cold memory storage (persistent, disk-based)
///
/// Uses sled for embedded key-value storage with crash recovery.
pub struct ColdMemory {
    /// Sled database
    db: sled::Db,
    /// Path to the database
    path: PathBuf,
    /// Statistics
    stats: Arc<RwLock<ColdMemoryStats>>,
}

/// Statistics for cold memory
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ColdMemoryStats {
    /// Total entries stored
    pub entry_count: usize,
    /// Total size on disk in bytes
    pub disk_bytes: u64,
    /// Reads performed
    pub reads: u64,
    /// Writes performed
    pub writes: u64,
}

impl ColdMemory {
    /// Create or open cold memory storage
    pub async fn new(path: PathBuf) -> MemResult<Self> {
        // Ensure parent directory exists
        if let Some(parent) = path.parent() {
            tokio::fs::create_dir_all(parent).await.map_err(|e| {
                MemError::storage(format!("Failed to create cold memory directory: {}", e))
            })?;
        }

        // Open sled database
        let db = sled::open(&path)
            .map_err(|e| MemError::storage(format!("Failed to open cold memory: {}", e)))?;

        let stats = Arc::new(RwLock::new(ColdMemoryStats {
            entry_count: db.len(),
            ..Default::default()
        }));

        Ok(Self { db, path, stats })
    }

    /// Store an entry in cold memory
    pub async fn store(&self, entry: &MemoryEntry) -> MemResult<()> {
        let key = entry.id.as_bytes().to_vec();
        let value = postcard::to_allocvec(entry)
            .map_err(|e| MemError::storage(format!("Failed to serialize entry: {}", e)))?;

        self.db
            .insert(key, value)
            .map_err(|e| MemError::storage(format!("Failed to store in cold memory: {}", e)))?;

        let mut stats = self.stats.write().await;
        stats.writes += 1;
        stats.entry_count = self.db.len();

        Ok(())
    }

    /// Store multiple entries in a batch
    pub async fn store_batch(&self, entries: &[MemoryEntry]) -> MemResult<usize> {
        let mut batch = sled::Batch::default();
        let mut stored = 0;

        for entry in entries {
            let key = entry.id.as_bytes().to_vec();
            match postcard::to_allocvec(entry) {
                Ok(value) => {
                    batch.insert(key, value);
                    stored += 1;
                }
                Err(e) => {
                    warn!("Failed to serialize entry {}: {}", entry.id, e);
                }
            }
        }

        self.db
            .apply_batch(batch)
            .map_err(|e| MemError::storage(format!("Failed to apply batch: {}", e)))?;

        let mut stats = self.stats.write().await;
        stats.writes += stored as u64;
        stats.entry_count = self.db.len();

        Ok(stored)
    }

    /// Retrieve an entry from cold memory
    pub async fn get(&self, id: &Uuid) -> MemResult<Option<MemoryEntry>> {
        let key = id.as_bytes().to_vec();

        let result = self
            .db
            .get(key)
            .map_err(|e| MemError::storage(format!("Failed to read from cold memory: {}", e)))?;

        let mut stats = self.stats.write().await;
        stats.reads += 1;

        if let Some(value) = result {
            let entry: MemoryEntry = postcard::from_bytes(&value)
                .map_err(|e| MemError::storage(format!("Failed to deserialize entry: {}", e)))?;
            Ok(Some(entry))
        } else {
            Ok(None)
        }
    }

    /// Remove an entry from cold memory
    pub async fn remove(&self, id: &Uuid) -> MemResult<Option<MemoryEntry>> {
        let key = id.as_bytes().to_vec();

        let result = self
            .db
            .remove(key)
            .map_err(|e| MemError::storage(format!("Failed to remove from cold memory: {}", e)))?;

        if let Some(value) = result {
            let entry: MemoryEntry = postcard::from_bytes(&value)
                .map_err(|e| MemError::storage(format!("Failed to deserialize entry: {}", e)))?;
            let mut stats = self.stats.write().await;
            stats.entry_count = self.db.len();
            Ok(Some(entry))
        } else {
            Ok(None)
        }
    }

    /// Flush all pending writes to disk
    pub async fn flush(&self) -> MemResult<()> {
        self.db
            .flush_async()
            .await
            .map_err(|e| MemError::storage(format!("Failed to flush cold memory: {}", e)))?;
        Ok(())
    }

    /// Get disk usage estimate
    pub async fn disk_usage(&self) -> MemResult<u64> {
        let size = self
            .db
            .size_on_disk()
            .map_err(|e| MemError::storage(format!("Failed to get disk size: {}", e)))?;
        Ok(size)
    }

    /// Get current statistics
    pub async fn stats(&self) -> ColdMemoryStats {
        let mut stats = self.stats.read().await.clone();
        stats.disk_bytes = self.disk_usage().await.unwrap_or(0);
        stats
    }

    /// Get number of entries
    pub fn len(&self) -> usize {
        self.db.len()
    }

    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.db.is_empty()
    }
}

// ============================================================================
// Write-Ahead Log (WAL)
// ============================================================================

/// WAL entry type
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum WalOperation {
    /// Insert or update an entry
    Insert(MemoryEntry),
    /// Remove an entry
    Remove(Uuid),
    /// Batch insert
    BatchInsert(Vec<MemoryEntry>),
    /// Checkpoint marker
    Checkpoint { sequence: u64 },
}

/// Write-ahead log entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WalEntry {
    /// Sequence number
    pub sequence: u64,
    /// Timestamp
    pub timestamp: DateTime<Utc>,
    /// Operation
    pub operation: WalOperation,
    /// CRC32 checksum for integrity
    pub checksum: u32,
}

impl WalEntry {
    /// Create a new WAL entry
    pub fn new(sequence: u64, operation: WalOperation) -> Self {
        let entry = Self {
            sequence,
            timestamp: Utc::now(),
            operation,
            checksum: 0, // Calculated on serialize
        };
        entry.with_checksum()
    }

    /// Calculate and set the checksum
    fn with_checksum(mut self) -> Self {
        // Serialize without checksum for calculation
        let checksum_data =
            postcard::to_allocvec(&(&self.sequence, &self.timestamp, &self.operation))
                .unwrap_or_default();
        self.checksum = crc32fast::hash(&checksum_data);
        self
    }

    /// Verify the checksum
    pub fn verify_checksum(&self) -> bool {
        let checksum_data =
            postcard::to_allocvec(&(&self.sequence, &self.timestamp, &self.operation))
                .unwrap_or_default();
        let calculated = crc32fast::hash(&checksum_data);
        calculated == self.checksum
    }
}

/// Write-ahead log for durability
pub struct WriteAheadLog {
    /// Path to WAL file
    path: PathBuf,
    /// Current sequence number
    sequence: AtomicU64,
    /// Last checkpoint sequence
    last_checkpoint: AtomicU64,
    /// Write lock
    write_lock: RwLock<()>,
}

impl WriteAheadLog {
    /// Create or open a WAL
    pub async fn new(path: PathBuf) -> MemResult<Self> {
        // Ensure parent directory exists
        if let Some(parent) = path.parent() {
            tokio::fs::create_dir_all(parent)
                .await
                .map_err(|e| MemError::storage(format!("Failed to create WAL directory: {}", e)))?;
        }

        // Get the last sequence number from existing WAL
        let (sequence, last_checkpoint) = Self::recover_sequence(&path).await.unwrap_or((0, 0));

        Ok(Self {
            path,
            sequence: AtomicU64::new(sequence),
            last_checkpoint: AtomicU64::new(last_checkpoint),
            write_lock: RwLock::new(()),
        })
    }

    /// Recover sequence number from existing WAL
    async fn recover_sequence(path: &PathBuf) -> MemResult<(u64, u64)> {
        if !path.exists() {
            return Ok((0, 0));
        }

        let content = tokio::fs::read(path)
            .await
            .map_err(|e| MemError::storage(format!("Failed to read WAL for recovery: {}", e)))?;

        let mut max_sequence = 0u64;
        let mut last_checkpoint = 0u64;
        let mut offset = 0;

        while offset < content.len() {
            // Try to read length prefix (u32)
            if offset + 4 > content.len() {
                break;
            }
            let len = u32::from_le_bytes(content[offset..offset + 4].try_into().unwrap()) as usize;
            offset += 4;

            if offset + len > content.len() {
                break;
            }

            // Try to deserialize entry
            if let Ok(entry) = postcard::from_bytes::<WalEntry>(&content[offset..offset + len]) {
                if entry.verify_checksum() {
                    max_sequence = max_sequence.max(entry.sequence);
                    if let WalOperation::Checkpoint { sequence } = entry.operation {
                        last_checkpoint = sequence;
                    }
                }
            }
            offset += len;
        }

        Ok((max_sequence, last_checkpoint))
    }

    /// Append an operation to the WAL
    pub async fn append(&self, operation: WalOperation) -> MemResult<u64> {
        let _lock = self.write_lock.write().await;
        let sequence = self.sequence.fetch_add(1, Ordering::SeqCst) + 1;
        let entry = WalEntry::new(sequence, operation);

        let data = postcard::to_allocvec(&entry)
            .map_err(|e| MemError::storage(format!("WAL serialize: {}", e)))?;

        let mut file = tokio::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&self.path)
            .await
            .map_err(|e| MemError::storage(format!("WAL open: {}", e)))?;

        // Write length prefix + data
        let len = (data.len() as u32).to_le_bytes();
        file.write_all(&len)
            .await
            .map_err(|e| MemError::storage(format!("WAL write: {}", e)))?;
        file.write_all(&data)
            .await
            .map_err(|e| MemError::storage(format!("WAL write: {}", e)))?;
        file.sync_all()
            .await
            .map_err(|e| MemError::storage(format!("WAL sync: {}", e)))?;

        Ok(sequence)
    }

    /// Write a checkpoint marker and truncate old entries
    pub async fn checkpoint(&self) -> MemResult<u64> {
        let sequence = self.sequence.load(Ordering::SeqCst);

        // Write checkpoint marker
        self.append(WalOperation::Checkpoint { sequence }).await?;

        // Update last checkpoint
        self.last_checkpoint.store(sequence, Ordering::SeqCst);

        // Truncate old entries (create new file with only checkpoint)
        let checkpoint_entry = WalEntry::new(sequence, WalOperation::Checkpoint { sequence });
        let data = postcard::to_allocvec(&checkpoint_entry)
            .map_err(|e| MemError::storage(format!("Checkpoint serialize: {}", e)))?;

        let len = (data.len() as u32).to_le_bytes();
        let mut content = Vec::with_capacity(4 + data.len());
        content.extend_from_slice(&len);
        content.extend_from_slice(&data);

        tokio::fs::write(&self.path, content)
            .await
            .map_err(|e| MemError::storage(format!("Checkpoint write: {}", e)))?;

        info!(sequence, "WAL checkpoint completed");
        Ok(sequence)
    }

    /// Get entries since the last checkpoint for recovery
    pub async fn get_entries_since_checkpoint(&self) -> MemResult<Vec<WalEntry>> {
        if !self.path.exists() {
            return Ok(Vec::new());
        }

        let content = tokio::fs::read(&self.path)
            .await
            .map_err(|e| MemError::storage(format!("WAL read: {}", e)))?;

        let last_checkpoint = self.last_checkpoint.load(Ordering::SeqCst);
        let mut entries = Vec::new();
        let mut offset = 0;

        while offset < content.len() {
            if offset + 4 > content.len() {
                break;
            }
            let len = u32::from_le_bytes(content[offset..offset + 4].try_into().unwrap()) as usize;
            offset += 4;

            if offset + len > content.len() {
                break;
            }

            if let Ok(entry) = postcard::from_bytes::<WalEntry>(&content[offset..offset + len]) {
                if entry.verify_checksum() && entry.sequence > last_checkpoint {
                    entries.push(entry);
                }
            }
            offset += len;
        }

        entries.sort_by_key(|e| e.sequence);
        Ok(entries)
    }

    /// Get current sequence number
    pub fn current_sequence(&self) -> u64 {
        self.sequence.load(Ordering::SeqCst)
    }

    /// Get last checkpoint sequence
    pub fn last_checkpoint_sequence(&self) -> u64 {
        self.last_checkpoint.load(Ordering::SeqCst)
    }
}

// ============================================================================
// Sync Worker
// ============================================================================

/// Statistics from a sync operation
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SyncStats {
    /// Number of entries moved from hot to cold
    pub entries_synced: usize,
    /// Number of entries skipped (too young)
    pub entries_skipped: usize,
    /// Number of errors encountered
    pub errors: usize,
    /// Total bytes synced
    pub bytes_synced: u64,
    /// Duration of the sync operation
    pub duration: Duration,
    /// Timestamp of the sync
    pub timestamp: DateTime<Utc>,
}

/// Background sync worker for hot-to-cold migration
pub struct SyncWorker {
    /// Hot memory reference
    hot: Arc<HotMemory>,
    /// Cold memory reference
    cold: Arc<ColdMemory>,
    /// Write-ahead log reference
    wal: Arc<WriteAheadLog>,
    /// Worker configuration
    config: SyncWorkerConfig,
    /// Command receiver
    command_rx: mpsc::Receiver<SyncCommand>,
}

impl SyncWorker {
    /// Create a new sync worker
    ///
    /// Returns the worker and a command sender for controlling it.
    pub fn new(
        hot: Arc<HotMemory>,
        cold: Arc<ColdMemory>,
        wal: Arc<WriteAheadLog>,
        config: SyncWorkerConfig,
    ) -> (Self, mpsc::Sender<SyncCommand>) {
        let (command_tx, command_rx) = mpsc::channel(16);

        let worker = Self {
            hot,
            cold,
            wal,
            config,
            command_rx,
        };

        (worker, command_tx)
    }

    /// Run the sync worker
    ///
    /// This is the main loop that:
    /// - Listens for commands
    /// - Periodically syncs hot to cold
    /// - Handles graceful shutdown
    pub async fn run(mut self) {
        info!(
            sync_interval_secs = self.config.sync_interval.as_secs(),
            hot_to_cold_age_secs = self.config.hot_to_cold_age.as_secs(),
            batch_size = self.config.batch_size,
            "Sync worker started"
        );

        let mut interval = tokio::time::interval(self.config.sync_interval);
        // Don't immediately tick
        interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);

        loop {
            tokio::select! {
                // Handle incoming commands
                Some(cmd) = self.command_rx.recv() => {
                    match cmd {
                        SyncCommand::Sync => {
                            debug!("Received manual sync command");
                            match self.sync_hot_to_cold().await {
                                Ok(stats) => {
                                    info!(
                                        entries_synced = stats.entries_synced,
                                        bytes_synced = stats.bytes_synced,
                                        duration_ms = stats.duration.as_millis() as u64,
                                        "Manual sync completed"
                                    );
                                }
                                Err(e) => {
                                    error!(error = %e, "Manual sync failed");
                                }
                            }
                        }
                        SyncCommand::Checkpoint => {
                            debug!("Received checkpoint command");
                            match self.checkpoint().await {
                                Ok(seq) => {
                                    info!(sequence = seq, "Checkpoint completed");
                                }
                                Err(e) => {
                                    error!(error = %e, "Checkpoint failed");
                                }
                            }
                        }
                        SyncCommand::Shutdown(done) => {
                            info!("Received shutdown command, performing final sync");

                            // Final sync before shutdown
                            if let Err(e) = self.sync_hot_to_cold().await {
                                warn!(error = %e, "Final sync failed during shutdown");
                            }

                            // Final checkpoint
                            if let Err(e) = self.checkpoint().await {
                                warn!(error = %e, "Final checkpoint failed during shutdown");
                            }

                            // Flush cold storage
                            if let Err(e) = self.cold.flush().await {
                                warn!(error = %e, "Cold storage flush failed during shutdown");
                            }

                            info!("Sync worker shutdown complete");
                            let _ = done.send(());
                            return;
                        }
                    }
                }

                // Periodic sync
                _ = interval.tick() => {
                    debug!("Periodic sync triggered");
                    match self.sync_hot_to_cold().await {
                        Ok(stats) => {
                            if stats.entries_synced > 0 {
                                info!(
                                    entries_synced = stats.entries_synced,
                                    bytes_synced = stats.bytes_synced,
                                    duration_ms = stats.duration.as_millis() as u64,
                                    "Periodic sync completed"
                                );
                            } else {
                                debug!("Periodic sync: no entries eligible for migration");
                            }
                        }
                        Err(e) => {
                            error!(error = %e, "Periodic sync failed");
                        }
                    }
                }
            }
        }
    }

    /// Sync entries from hot to cold memory
    pub async fn sync_hot_to_cold(&self) -> MemResult<SyncStats> {
        let start = std::time::Instant::now();
        let mut stats = SyncStats {
            timestamp: Utc::now(),
            ..Default::default()
        };

        // Get entries eligible for cold storage
        let eligible = self
            .hot
            .get_cold_eligible(self.config.hot_to_cold_age, self.config.batch_size);

        if eligible.is_empty() {
            stats.duration = start.elapsed();
            return Ok(stats);
        }

        debug!(
            eligible_count = eligible.len(),
            "Found entries eligible for cold migration"
        );

        // Write to WAL first (durability)
        for entry in &eligible {
            if let Err(e) = self.wal.append(WalOperation::Insert(entry.clone())).await {
                warn!(id = %entry.id, error = %e, "Failed to write to WAL");
                stats.errors += 1;
                continue;
            }
        }

        // Store in cold memory
        match self.cold.store_batch(&eligible).await {
            Ok(stored) => {
                stats.entries_synced = stored;
                stats.bytes_synced = eligible.iter().map(|e| e.estimated_size() as u64).sum();

                // Remove from hot memory after successful cold storage
                for entry in &eligible {
                    if let Err(e) = async {
                        self.hot.remove(&entry.id).await;
                        Ok::<_, MemError>(())
                    }
                    .await
                    {
                        warn!(id = %entry.id, error = %e, "Failed to remove from hot memory");
                    }
                }
            }
            Err(e) => {
                error!(error = %e, "Failed to store batch in cold memory");
                stats.errors += eligible.len();
            }
        }

        stats.entries_skipped = self.hot.len();
        stats.duration = start.elapsed();

        Ok(stats)
    }

    /// Checkpoint the WAL
    pub async fn checkpoint(&self) -> MemResult<u64> {
        // Flush cold storage first
        self.cold.flush().await?;

        // Then checkpoint the WAL
        self.wal.checkpoint().await
    }
}

// ============================================================================
// Spawn Helper
// ============================================================================

/// Spawn the sync worker as a background task
///
/// Returns a command sender and the task join handle.
///
/// # Example
///
/// ```rust,ignore
/// let (tx, handle) = spawn_sync_worker(hot, cold, wal, config);
///
/// // Trigger manual sync
/// tx.send(SyncCommand::Sync).await?;
///
/// // Graceful shutdown
/// let (done_tx, done_rx) = tokio::sync::oneshot::channel();
/// tx.send(SyncCommand::Shutdown(done_tx)).await?;
/// done_rx.await?;
/// handle.await?;
/// ```
pub fn spawn_sync_worker(
    hot: Arc<HotMemory>,
    cold: Arc<ColdMemory>,
    wal: Arc<WriteAheadLog>,
    config: SyncWorkerConfig,
) -> (mpsc::Sender<SyncCommand>, tokio::task::JoinHandle<()>) {
    let (worker, command_tx) = SyncWorker::new(hot, cold, wal, config);
    let handle = tokio::spawn(worker.run());
    (command_tx, handle)
}

// ============================================================================
// Tests
// ============================================================================

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

    #[tokio::test]
    async fn test_memory_entry_creation() {
        let id = Uuid::new_v4();
        let entry = MemoryEntry::embedding(id, vec![1.0, 2.0, 3.0]);
        assert_eq!(entry.id, id);
        assert_eq!(entry.access_count, 0);
        assert!(!entry.is_expired());
    }

    #[tokio::test]
    async fn test_memory_entry_cold_eligibility() {
        let id = Uuid::new_v4();
        let mut entry = MemoryEntry::embedding(id, vec![1.0, 2.0, 3.0]);

        // Fresh entry should not be cold eligible
        assert!(!entry.is_cold_eligible(Duration::from_secs(1)));

        // Simulate old access time
        entry.last_accessed = Utc::now() - chrono::Duration::seconds(10);
        assert!(entry.is_cold_eligible(Duration::from_secs(5)));
    }

    #[tokio::test]
    async fn test_hot_memory_insert_get() {
        let config = HotMemoryConfig::default();
        let hot = HotMemory::new(config);

        let id = Uuid::new_v4();
        let entry = MemoryEntry::embedding(id, vec![1.0, 2.0, 3.0]);

        hot.insert(entry.clone()).await.unwrap();
        assert_eq!(hot.len(), 1);

        let retrieved = hot.get(&id).await.unwrap();
        assert_eq!(retrieved.id, id);
        assert_eq!(retrieved.access_count, 1); // Access was recorded
    }

    #[tokio::test]
    async fn test_hot_memory_lru_eviction() {
        let config = HotMemoryConfig {
            max_entries: 2,
            max_memory_bytes: 1024 * 1024,
            enable_lru_eviction: true,
        };
        let hot = HotMemory::new(config);

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

        hot.insert(MemoryEntry::embedding(id1, vec![1.0]))
            .await
            .unwrap();
        tokio::time::sleep(Duration::from_millis(10)).await;
        hot.insert(MemoryEntry::embedding(id2, vec![2.0]))
            .await
            .unwrap();

        // Access id1 to make it more recent
        hot.get(&id1).await;
        tokio::time::sleep(Duration::from_millis(10)).await;

        // Insert id3, should evict id2 (least recently accessed)
        hot.insert(MemoryEntry::embedding(id3, vec![3.0]))
            .await
            .unwrap();

        assert_eq!(hot.len(), 2);
        assert!(hot.get(&id1).await.is_some());
        assert!(hot.get(&id2).await.is_none()); // Evicted
        assert!(hot.get(&id3).await.is_some());
    }

    #[tokio::test]
    async fn test_cold_memory_store_get() {
        let temp_dir = TempDir::new().unwrap();
        let path = temp_dir.path().join("cold.db");

        let cold = ColdMemory::new(path).await.unwrap();

        let id = Uuid::new_v4();
        let entry = MemoryEntry::embedding(id, vec![1.0, 2.0, 3.0]);

        cold.store(&entry).await.unwrap();
        assert_eq!(cold.len(), 1);

        let retrieved = cold.get(&id).await.unwrap().unwrap();
        assert_eq!(retrieved.id, id);
    }

    #[tokio::test]
    async fn test_cold_memory_batch_store() {
        let temp_dir = TempDir::new().unwrap();
        let path = temp_dir.path().join("cold.db");

        let cold = ColdMemory::new(path).await.unwrap();

        let entries: Vec<_> = (0..10)
            .map(|i| MemoryEntry::embedding(Uuid::new_v4(), vec![i as f32]))
            .collect();

        let stored = cold.store_batch(&entries).await.unwrap();
        assert_eq!(stored, 10);
        assert_eq!(cold.len(), 10);
    }

    #[tokio::test]
    async fn test_wal_append_checkpoint() {
        let temp_dir = TempDir::new().unwrap();
        let path = temp_dir.path().join("test.wal");

        let wal = WriteAheadLog::new(path).await.unwrap();

        // Append some operations
        let id = Uuid::new_v4();
        let entry = MemoryEntry::embedding(id, vec![1.0, 2.0]);

        let seq1 = wal
            .append(WalOperation::Insert(entry.clone()))
            .await
            .unwrap();
        let seq2 = wal.append(WalOperation::Remove(id)).await.unwrap();

        assert_eq!(seq1, 1);
        assert_eq!(seq2, 2);
        assert_eq!(wal.current_sequence(), 2);

        // Checkpoint
        let checkpoint_seq = wal.checkpoint().await.unwrap();
        assert_eq!(checkpoint_seq, 2);
    }

    #[tokio::test]
    async fn test_wal_entry_checksum() {
        let entry = WalEntry::new(1, WalOperation::Remove(Uuid::new_v4()));
        assert!(entry.verify_checksum());

        // Corrupt the entry
        let mut corrupted = entry.clone();
        corrupted.sequence = 999;
        assert!(!corrupted.verify_checksum());
    }

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

        let hot = Arc::new(HotMemory::new(HotMemoryConfig::default()));
        let cold = Arc::new(
            ColdMemory::new(temp_dir.path().join("cold.db"))
                .await
                .unwrap(),
        );
        let wal = Arc::new(
            WriteAheadLog::new(temp_dir.path().join("test.wal"))
                .await
                .unwrap(),
        );
        let config = SyncWorkerConfig::default();

        let (worker, tx) = SyncWorker::new(hot, cold, wal, config);

        // Worker should be created
        assert!(tx.capacity() > 0);

        // Verify we can send commands
        let (done_tx, done_rx) = oneshot::channel();

        // Spawn the worker
        let handle = tokio::spawn(worker.run());

        // Send shutdown
        tx.send(SyncCommand::Shutdown(done_tx)).await.unwrap();

        // Wait for completion
        tokio::time::timeout(Duration::from_secs(5), done_rx)
            .await
            .expect("Shutdown timed out")
            .expect("Shutdown channel closed");

        handle.await.unwrap();
    }

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

        let hot = Arc::new(HotMemory::new(HotMemoryConfig::default()));
        let cold = Arc::new(
            ColdMemory::new(temp_dir.path().join("cold.db"))
                .await
                .unwrap(),
        );
        let wal = Arc::new(
            WriteAheadLog::new(temp_dir.path().join("test.wal"))
                .await
                .unwrap(),
        );

        // Insert entries with old access times
        for i in 0..5 {
            let mut entry = MemoryEntry::embedding(Uuid::new_v4(), vec![i as f32]);
            entry.last_accessed = Utc::now() - chrono::Duration::seconds(600);
            hot.insert(entry).await.unwrap();
        }

        assert_eq!(hot.len(), 5);
        assert_eq!(cold.len(), 0);

        let config = SyncWorkerConfig {
            sync_interval: Duration::from_secs(1),
            hot_to_cold_age: Duration::from_secs(1), // 1 second threshold
            batch_size: 10,
        };

        let (worker, tx) = SyncWorker::new(
            Arc::clone(&hot),
            Arc::clone(&cold),
            Arc::clone(&wal),
            config,
        );

        let handle = tokio::spawn(worker.run());

        // Trigger sync
        tx.send(SyncCommand::Sync).await.unwrap();

        // Give it a moment to process
        tokio::time::sleep(Duration::from_millis(100)).await;

        // Graceful shutdown
        let (done_tx, done_rx) = oneshot::channel();
        tx.send(SyncCommand::Shutdown(done_tx)).await.unwrap();
        done_rx.await.unwrap();
        handle.await.unwrap();

        // Entries should have moved from hot to cold
        assert_eq!(hot.len(), 0);
        assert_eq!(cold.len(), 5);
    }

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

        let hot = Arc::new(HotMemory::new(HotMemoryConfig::default()));
        let cold = Arc::new(
            ColdMemory::new(temp_dir.path().join("cold.db"))
                .await
                .unwrap(),
        );
        let wal = Arc::new(
            WriteAheadLog::new(temp_dir.path().join("test.wal"))
                .await
                .unwrap(),
        );
        let config = SyncWorkerConfig::default();

        let (tx, handle) = spawn_sync_worker(hot, cold, wal, config);

        // Graceful shutdown
        let (done_tx, done_rx) = oneshot::channel();
        tx.send(SyncCommand::Shutdown(done_tx)).await.unwrap();
        done_rx.await.unwrap();
        handle.await.unwrap();
    }
}