mnemefusion-core 0.1.4

Unified memory engine for AI applications - Core library
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
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
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
//! Storage engine implementation
//!
//! Wraps redb and provides CRUD operations for memories and indexes.

use crate::{
    types::{Entity, EntityId, EntityProfile, Memory, MemoryId, Timestamp},
    Error, Result,
};
use redb::{Database, ReadableTable, ReadableTableMetadata, TableDefinition};
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use super::FileHeader;

// Table definitions
const MEMORIES: TableDefinition<&[u8], &[u8]> = TableDefinition::new("memories");
pub(crate) const TEMPORAL_INDEX: TableDefinition<u64, &[u8]> =
    TableDefinition::new("temporal_index");
const METADATA_TABLE: TableDefinition<&str, &[u8]> = TableDefinition::new("metadata");
const MEMORY_ID_INDEX: TableDefinition<u64, &[u8]> = TableDefinition::new("memory_id_index");
const CAUSAL_GRAPH: TableDefinition<&str, &[u8]> = TableDefinition::new("causal_graph");
const ENTITIES: TableDefinition<&[u8], &[u8]> = TableDefinition::new("entities");
const ENTITY_NAMES: TableDefinition<&str, &[u8]> = TableDefinition::new("entity_names");
const CONTENT_HASH_INDEX: TableDefinition<&str, &[u8]> = TableDefinition::new("content_hash_index");
const LOGICAL_KEY_INDEX: TableDefinition<&str, &[u8]> = TableDefinition::new("logical_key_index");
const METADATA_INDEX: TableDefinition<&str, &[u8]> = TableDefinition::new("metadata_index");
const ENTITY_PROFILES: TableDefinition<&str, &[u8]> = TableDefinition::new("entity_profiles");
const FACT_EMBEDDINGS: TableDefinition<&[u8], &[u8]> = TableDefinition::new("fact_embeddings");

/// Storage engine wrapper around redb
///
/// Provides ACID transactions and persistent storage for all MnemeFusion data.
pub struct StorageEngine {
    db: Database,
    path: PathBuf,
}

impl StorageEngine {
    /// Open or create a database at the specified path
    pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
        let path = path.as_ref();

        // Check if file exists and validate minimum size
        if path.exists() {
            let metadata = std::fs::metadata(path)?;
            let file_size = metadata.len();

            // Minimum valid file size (redb has minimum overhead + 64 byte header)
            // redb files are typically at least a few KB even when empty
            const MIN_FILE_SIZE: u64 = 512; // Conservative minimum

            if file_size < MIN_FILE_SIZE {
                return Err(Error::FileTruncated(format!(
                    "File size ({} bytes) is too small to be a valid database",
                    file_size
                )));
            }
        }

        let db = Database::create(path)?;

        let mut engine = Self {
            db,
            path: path.to_path_buf(),
        };

        // Initialize tables
        engine.init_tables()?;

        // Store or validate header
        engine.init_header()?;

        // Validate database integrity (for existing databases)
        if path.exists() {
            engine.validate_database()?;
        }

        Ok(engine)
    }

    /// Initialize required tables
    fn init_tables(&self) -> Result<()> {
        let write_txn = self.db.begin_write()?;
        {
            let _ = write_txn.open_table(MEMORIES)?;
            let _ = write_txn.open_table(TEMPORAL_INDEX)?;
            let _ = write_txn.open_table(METADATA_TABLE)?;
            let _ = write_txn.open_table(MEMORY_ID_INDEX)?;
            let _ = write_txn.open_table(CAUSAL_GRAPH)?;
            let _ = write_txn.open_table(ENTITIES)?;
            let _ = write_txn.open_table(ENTITY_NAMES)?;
            let _ = write_txn.open_table(CONTENT_HASH_INDEX)?;
            let _ = write_txn.open_table(LOGICAL_KEY_INDEX)?;
            let _ = write_txn.open_table(METADATA_INDEX)?;
            let _ = write_txn.open_table(ENTITY_PROFILES)?;
            let _ = write_txn.open_table(FACT_EMBEDDINGS)?;
        }
        write_txn.commit()?;
        Ok(())
    }

    /// Initialize or validate file header
    fn init_header(&mut self) -> Result<()> {
        let write_txn = self.db.begin_write()?;
        {
            let mut table = write_txn.open_table(METADATA_TABLE)?;

            // Check if header exists
            let header_exists = table.get("file_header")?.is_some();

            if header_exists {
                // Read and validate existing header
                let existing = table.get("file_header")?.unwrap();
                let existing_bytes = existing.value().to_vec();
                let header = FileHeader::from_bytes(&existing_bytes)?;
                header.validate()?;
            } else {
                // Create new header
                let header = FileHeader::new();
                table.insert("file_header", header.to_bytes().as_slice())?;
            }
        }
        write_txn.commit()?;
        Ok(())
    }

    /// Validate database integrity
    ///
    /// Verifies that all required tables exist and are accessible.
    /// This helps detect corrupted or incomplete database files.
    fn validate_database(&self) -> Result<()> {
        let read_txn = self.db.begin_read()?;

        // Check that all required tables exist and are accessible
        // We open each table individually since they have different type parameters

        if let Err(e) = read_txn.open_table(MEMORIES) {
            return Err(Error::DatabaseCorruption(format!(
                "Required table 'memories' is missing or corrupt: {}",
                e
            )));
        }

        if let Err(e) = read_txn.open_table(TEMPORAL_INDEX) {
            return Err(Error::DatabaseCorruption(format!(
                "Required table 'temporal_index' is missing or corrupt: {}",
                e
            )));
        }

        if let Err(e) = read_txn.open_table(METADATA_TABLE) {
            return Err(Error::DatabaseCorruption(format!(
                "Required table 'metadata' is missing or corrupt: {}",
                e
            )));
        }

        if let Err(e) = read_txn.open_table(MEMORY_ID_INDEX) {
            return Err(Error::DatabaseCorruption(format!(
                "Required table 'memory_id_index' is missing or corrupt: {}",
                e
            )));
        }

        if let Err(e) = read_txn.open_table(CAUSAL_GRAPH) {
            return Err(Error::DatabaseCorruption(format!(
                "Required table 'causal_graph' is missing or corrupt: {}",
                e
            )));
        }

        if let Err(e) = read_txn.open_table(ENTITIES) {
            return Err(Error::DatabaseCorruption(format!(
                "Required table 'entities' is missing or corrupt: {}",
                e
            )));
        }

        if let Err(e) = read_txn.open_table(ENTITY_NAMES) {
            return Err(Error::DatabaseCorruption(format!(
                "Required table 'entity_names' is missing or corrupt: {}",
                e
            )));
        }

        if let Err(e) = read_txn.open_table(CONTENT_HASH_INDEX) {
            return Err(Error::DatabaseCorruption(format!(
                "Required table 'content_hash_index' is missing or corrupt: {}",
                e
            )));
        }

        if let Err(e) = read_txn.open_table(LOGICAL_KEY_INDEX) {
            return Err(Error::DatabaseCorruption(format!(
                "Required table 'logical_key_index' is missing or corrupt: {}",
                e
            )));
        }

        if let Err(e) = read_txn.open_table(METADATA_INDEX) {
            return Err(Error::DatabaseCorruption(format!(
                "Required table 'metadata_index' is missing or corrupt: {}",
                e
            )));
        }

        if let Err(e) = read_txn.open_table(ENTITY_PROFILES) {
            return Err(Error::DatabaseCorruption(format!(
                "Required table 'entity_profiles' is missing or corrupt: {}",
                e
            )));
        }

        if let Err(e) = read_txn.open_table(FACT_EMBEDDINGS) {
            return Err(Error::DatabaseCorruption(format!(
                "Required table 'fact_embeddings' is missing or corrupt: {}",
                e
            )));
        }

        // Validate header exists
        let metadata = read_txn.open_table(METADATA_TABLE)?;
        match metadata.get("file_header")? {
            Some(header_bytes) => {
                // Validate header format
                let header = FileHeader::from_bytes(header_bytes.value())?;
                header.validate()?;
            }
            None => {
                return Err(Error::DatabaseCorruption(
                    "File header is missing".to_string(),
                ));
            }
        }

        Ok(())
    }

    /// Store a memory record
    pub fn store_memory(&self, memory: &Memory) -> Result<()> {
        let write_txn = self.db.begin_write()?;
        {
            let mut memories = write_txn.open_table(MEMORIES)?;
            let mut temporal = write_txn.open_table(TEMPORAL_INDEX)?;
            let mut id_index = write_txn.open_table(MEMORY_ID_INDEX)?;

            // Serialize memory
            let memory_data = self.serialize_memory(memory)?;

            // Store memory
            memories.insert(memory.id.as_bytes().as_slice(), memory_data.as_slice())?;

            // Index by timestamp
            temporal.insert(
                memory.created_at.as_micros(),
                memory.id.as_bytes().as_slice(),
            )?;

            // Index by u64 (for vector index lookups)
            id_index.insert(memory.id.to_u64(), memory.id.as_bytes().as_slice())?;
        }
        write_txn.commit()?;
        Ok(())
    }

    /// Retrieve a memory by ID
    pub fn get_memory(&self, id: &MemoryId) -> Result<Option<Memory>> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(MEMORIES)?;

        match table.get(id.as_bytes().as_slice())? {
            Some(data) => {
                let memory = self.deserialize_memory(data.value())?;
                Ok(Some(memory))
            }
            None => Ok(None),
        }
    }

    /// Retrieve a memory by its u64 key (used by vector index)
    pub fn get_memory_by_u64(&self, key: u64) -> Result<Option<Memory>> {
        let read_txn = self.db.begin_read()?;
        let id_index = read_txn.open_table(MEMORY_ID_INDEX)?;
        let memories = read_txn.open_table(MEMORIES)?;

        // First lookup the full MemoryId from the u64 index
        match id_index.get(key)? {
            Some(id_bytes) => {
                // Then fetch the memory using the full ID
                match memories.get(id_bytes.value())? {
                    Some(data) => {
                        let memory = self.deserialize_memory(data.value())?;
                        Ok(Some(memory))
                    }
                    None => Ok(None),
                }
            }
            None => Ok(None),
        }
    }

    /// Delete a memory by ID
    pub fn delete_memory(&self, id: &MemoryId) -> Result<bool> {
        let write_txn = self.db.begin_write()?;
        let removed = {
            let mut memories = write_txn.open_table(MEMORIES)?;
            let mut id_index = write_txn.open_table(MEMORY_ID_INDEX)?;

            let result = memories.remove(id.as_bytes().as_slice())?;

            // Also remove from ID index
            if result.is_some() {
                id_index.remove(id.to_u64())?;
            }

            result.is_some()
        };
        write_txn.commit()?;
        Ok(removed)
    }

    /// Get all memory IDs (for testing/debugging)
    pub fn list_memory_ids(&self) -> Result<Vec<MemoryId>> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(MEMORIES)?;

        let mut ids = Vec::new();
        for item in table.iter()? {
            let (key, _) = item?;
            let id = MemoryId::from_bytes(key.value())?;
            ids.push(id);
        }
        Ok(ids)
    }

    /// Get memory count
    pub fn count_memories(&self) -> Result<usize> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(MEMORIES)?;
        Ok(table.len()? as usize)
    }

    /// Find a memory by its dialog_id metadata value (O(n) scan).
    ///
    /// dialog_id is not stored in a separate index, so this iterates all memories.
    /// With ~3643 memories in current DBs, this takes ~1-5ms — acceptable for the
    /// bounded number of calls (≤18 per query for adjacent-turn bridging).
    ///
    /// Returns the first memory whose metadata["dialog_id"] == dialog_id.
    pub fn find_memory_by_dialog_id(&self, dialog_id: &str) -> Result<Option<Memory>> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(MEMORIES)?;
        for item in table.iter()? {
            let (_, val) = item?;
            let memory = self.deserialize_memory(val.value())?;
            if memory.metadata.get("dialog_id").map(|s| s.as_str()) == Some(dialog_id) {
                return Ok(Some(memory));
            }
        }
        Ok(None)
    }

    /// Serialize memory to bytes
    fn serialize_memory(&self, memory: &Memory) -> Result<Vec<u8>> {
        // Simple serialization format:
        // [id (16 bytes)][timestamp (8 bytes)][content_len (4 bytes)][content][embedding_len (4 bytes)][embedding][metadata_len (4 bytes)][metadata]

        let mut bytes = Vec::new();

        // ID
        bytes.extend_from_slice(memory.id.as_bytes());

        // Timestamp
        bytes.extend_from_slice(&memory.created_at.to_bytes());

        // Content
        let content_bytes = memory.content.as_bytes();
        bytes.extend_from_slice(&(content_bytes.len() as u32).to_le_bytes());
        bytes.extend_from_slice(content_bytes);

        // Embedding
        bytes.extend_from_slice(&(memory.embedding.len() as u32).to_le_bytes());
        for val in &memory.embedding {
            bytes.extend_from_slice(&val.to_le_bytes());
        }

        // Metadata
        let metadata_str = serde_json::to_string(&memory.metadata)
            .map_err(|e| Error::Serialization(e.to_string()))?;
        let metadata_bytes = metadata_str.as_bytes();
        bytes.extend_from_slice(&(metadata_bytes.len() as u32).to_le_bytes());
        bytes.extend_from_slice(metadata_bytes);

        Ok(bytes)
    }

    /// Deserialize memory from bytes
    fn deserialize_memory(&self, bytes: &[u8]) -> Result<Memory> {
        let mut offset = 0;

        // ID
        if bytes.len() < offset + 16 {
            return Err(Error::Deserialization("Incomplete memory data".to_string()));
        }
        let id = MemoryId::from_bytes(&bytes[offset..offset + 16])?;
        offset += 16;

        // Timestamp
        if bytes.len() < offset + 8 {
            return Err(Error::Deserialization(
                "Incomplete timestamp data".to_string(),
            ));
        }
        let created_at = Timestamp::from_bytes(&bytes[offset..offset + 8])?;
        offset += 8;

        // Content
        if bytes.len() < offset + 4 {
            return Err(Error::Deserialization(
                "Incomplete content length".to_string(),
            ));
        }
        let content_len = u32::from_le_bytes([
            bytes[offset],
            bytes[offset + 1],
            bytes[offset + 2],
            bytes[offset + 3],
        ]) as usize;
        offset += 4;

        if bytes.len() < offset + content_len {
            return Err(Error::Deserialization(
                "Incomplete content data".to_string(),
            ));
        }
        let content = String::from_utf8(bytes[offset..offset + content_len].to_vec())
            .map_err(|e| Error::Deserialization(e.to_string()))?;
        offset += content_len;

        // Embedding
        if bytes.len() < offset + 4 {
            return Err(Error::Deserialization(
                "Incomplete embedding length".to_string(),
            ));
        }
        let embedding_len = u32::from_le_bytes([
            bytes[offset],
            bytes[offset + 1],
            bytes[offset + 2],
            bytes[offset + 3],
        ]) as usize;
        offset += 4;

        if bytes.len() < offset + embedding_len * 4 {
            return Err(Error::Deserialization(
                "Incomplete embedding data".to_string(),
            ));
        }
        let mut embedding = Vec::with_capacity(embedding_len);
        for _ in 0..embedding_len {
            let val = f32::from_le_bytes([
                bytes[offset],
                bytes[offset + 1],
                bytes[offset + 2],
                bytes[offset + 3],
            ]);
            embedding.push(val);
            offset += 4;
        }

        // Metadata
        if bytes.len() < offset + 4 {
            return Err(Error::Deserialization(
                "Incomplete metadata length".to_string(),
            ));
        }
        let metadata_len = u32::from_le_bytes([
            bytes[offset],
            bytes[offset + 1],
            bytes[offset + 2],
            bytes[offset + 3],
        ]) as usize;
        offset += 4;

        if bytes.len() < offset + metadata_len {
            return Err(Error::Deserialization(
                "Incomplete metadata data".to_string(),
            ));
        }
        let metadata_str = String::from_utf8(bytes[offset..offset + metadata_len].to_vec())
            .map_err(|e| Error::Deserialization(e.to_string()))?;
        let metadata: HashMap<String, String> = serde_json::from_str(&metadata_str)
            .map_err(|e| Error::Deserialization(e.to_string()))?;

        Ok(Memory {
            id,
            content,
            embedding,
            created_at,
            metadata,
        })
    }

    /// Get the database path
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Get a reference to the underlying database
    ///
    /// This is used by index implementations (TemporalIndex, etc.) to access
    /// tables directly for specialized queries.
    pub(crate) fn db(&self) -> &Database {
        &self.db
    }

    /// Store vector index data
    pub fn store_vector_index(&self, buffer: &[u8]) -> Result<()> {
        let write_txn = self.db.begin_write()?;
        {
            let mut table = write_txn.open_table(METADATA_TABLE)?;
            table.insert("vector_index", buffer)?;
        }
        write_txn.commit()?;
        Ok(())
    }

    /// Load vector index data
    pub fn load_vector_index(&self) -> Result<Option<Vec<u8>>> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(METADATA_TABLE)?;

        match table.get("vector_index")? {
            Some(data) => Ok(Some(data.value().to_vec())),
            None => Ok(None),
        }
    }

    /// Store BM25 index data
    pub fn store_bm25_index(&self, buffer: &[u8]) -> Result<()> {
        let write_txn = self.db.begin_write()?;
        {
            let mut table = write_txn.open_table(METADATA_TABLE)?;
            table.insert("bm25_index", buffer)?;
        }
        write_txn.commit()?;
        Ok(())
    }

    /// Load BM25 index data
    pub fn load_bm25_index(&self) -> Result<Option<Vec<u8>>> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(METADATA_TABLE)?;

        match table.get("bm25_index")? {
            Some(data) => Ok(Some(data.value().to_vec())),
            None => Ok(None),
        }
    }

    /// Store causal graph data
    pub fn store_causal_graph(&self, data: &[u8]) -> Result<()> {
        let write_txn = self.db.begin_write()?;
        {
            let mut table = write_txn.open_table(CAUSAL_GRAPH)?;
            table.insert("graph", data)?;
        }
        write_txn.commit()?;
        Ok(())
    }

    /// Load causal graph data
    pub fn load_causal_graph(&self) -> Result<Option<Vec<u8>>> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(CAUSAL_GRAPH)?;

        match table.get("graph")? {
            Some(data) => Ok(Some(data.value().to_vec())),
            None => Ok(None),
        }
    }

    /// Store an entity
    pub fn store_entity(&self, entity: &Entity) -> Result<()> {
        let write_txn = self.db.begin_write()?;
        {
            let mut entities = write_txn.open_table(ENTITIES)?;
            let mut names = write_txn.open_table(ENTITY_NAMES)?;

            // Serialize entity to JSON
            let entity_data =
                serde_json::to_vec(entity).map_err(|e| Error::Serialization(e.to_string()))?;

            // Store entity by ID
            entities.insert(entity.id.as_bytes().as_slice(), entity_data.as_slice())?;

            // Index by normalized name (case-insensitive)
            let normalized_name = entity.normalized_name();
            names.insert(normalized_name.as_str(), entity.id.as_bytes().as_slice())?;
        }
        write_txn.commit()?;
        Ok(())
    }

    /// Get an entity by ID
    pub fn get_entity(&self, id: &EntityId) -> Result<Option<Entity>> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(ENTITIES)?;

        match table.get(id.as_bytes().as_slice())? {
            Some(data) => {
                let entity: Entity = serde_json::from_slice(data.value())
                    .map_err(|e| Error::Deserialization(e.to_string()))?;
                Ok(Some(entity))
            }
            None => Ok(None),
        }
    }

    /// Find an entity by name (case-insensitive)
    pub fn find_entity_by_name(&self, name: &str) -> Result<Option<Entity>> {
        let read_txn = self.db.begin_read()?;
        let names_table = read_txn.open_table(ENTITY_NAMES)?;
        let entities_table = read_txn.open_table(ENTITIES)?;

        // Normalize the search name
        let normalized = name.to_lowercase();

        // Look up entity ID by normalized name
        match names_table.get(normalized.as_str())? {
            Some(id_bytes) => {
                let id_bytes = id_bytes.value().to_vec();
                let entity_id = EntityId::from_bytes(&id_bytes)?;

                // Get the entity
                match entities_table.get(entity_id.as_bytes().as_slice())? {
                    Some(data) => {
                        let entity: Entity = serde_json::from_slice(data.value())
                            .map_err(|e| Error::Deserialization(e.to_string()))?;
                        Ok(Some(entity))
                    }
                    None => Ok(None),
                }
            }
            None => Ok(None),
        }
    }

    /// Delete an entity
    pub fn delete_entity(&self, id: &EntityId) -> Result<bool> {
        let write_txn = self.db.begin_write()?;
        let deleted = {
            let mut entities = write_txn.open_table(ENTITIES)?;
            let mut names = write_txn.open_table(ENTITY_NAMES)?;

            // Get entity to find its name for name index cleanup
            // Store the normalized name before dropping the guard
            let normalized_name = if let Some(data) = entities.get(id.as_bytes().as_slice())? {
                let entity: Entity = serde_json::from_slice(data.value())
                    .map_err(|e| Error::Deserialization(e.to_string()))?;
                Some(entity.normalized_name())
            } else {
                None
            };

            // Now we can mutate
            if let Some(name) = normalized_name {
                // Remove from name index
                names.remove(name.as_str())?;

                // Remove entity
                entities.remove(id.as_bytes().as_slice())?;
                true
            } else {
                false
            }
        };
        write_txn.commit()?;
        Ok(deleted)
    }

    /// List all entities
    pub fn list_entities(&self) -> Result<Vec<Entity>> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(ENTITIES)?;

        let mut entities = Vec::new();
        for result in table.iter()? {
            let (_, value) = result?;
            let entity: Entity = serde_json::from_slice(value.value())
                .map_err(|e| Error::Deserialization(e.to_string()))?;
            entities.push(entity);
        }

        Ok(entities)
    }

    /// Count entities
    pub fn count_entities(&self) -> Result<usize> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(ENTITIES)?;
        Ok(table.len()? as usize)
    }

    /// Store entity graph data
    pub fn store_entity_graph(&self, data: &[u8]) -> Result<()> {
        let write_txn = self.db.begin_write()?;
        {
            let mut table = write_txn.open_table(METADATA_TABLE)?;
            table.insert("entity_graph", data)?;
        }
        write_txn.commit()?;
        Ok(())
    }

    /// Load entity graph data
    pub fn load_entity_graph(&self) -> Result<Option<Vec<u8>>> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(METADATA_TABLE)?;

        match table.get("entity_graph")? {
            Some(data) => Ok(Some(data.value().to_vec())),
            None => Ok(None),
        }
    }

    /// Store entity-to-entity relationship graph data
    pub fn store_relationship_graph(&self, data: &[u8]) -> Result<()> {
        let write_txn = self.db.begin_write()?;
        {
            let mut table = write_txn.open_table(METADATA_TABLE)?;
            table.insert("relationship_graph", data)?;
        }
        write_txn.commit()?;
        Ok(())
    }

    /// Load entity-to-entity relationship graph data
    pub fn load_relationship_graph(&self) -> Result<Option<Vec<u8>>> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(METADATA_TABLE)?;

        match table.get("relationship_graph")? {
            Some(data) => Ok(Some(data.value().to_vec())),
            None => Ok(None),
        }
    }

    /// Check if the relationship graph key exists in storage
    pub fn has_relationship_graph(&self) -> Result<bool> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(METADATA_TABLE)?;
        Ok(table.get("relationship_graph")?.is_some())
    }

    // Content hash operations for deduplication

    /// Store content hash → memory ID mapping
    ///
    /// Used for deduplication to quickly find if content already exists
    pub fn store_content_hash(&self, hash: &str, memory_id: &MemoryId) -> Result<()> {
        let write_txn = self.db.begin_write()?;
        {
            let mut table = write_txn.open_table(CONTENT_HASH_INDEX)?;
            table.insert(hash, memory_id.as_bytes() as &[u8])?;
        }
        write_txn.commit()?;
        Ok(())
    }

    /// Find memory ID by content hash
    ///
    /// Returns None if hash not found (content is unique)
    pub fn find_by_content_hash(&self, hash: &str) -> Result<Option<MemoryId>> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(CONTENT_HASH_INDEX)?;

        match table.get(hash)? {
            Some(bytes) => {
                let id = MemoryId::from_bytes(bytes.value())?;
                Ok(Some(id))
            }
            None => Ok(None),
        }
    }

    /// Delete content hash mapping
    pub fn delete_content_hash(&self, hash: &str) -> Result<()> {
        let write_txn = self.db.begin_write()?;
        {
            let mut table = write_txn.open_table(CONTENT_HASH_INDEX)?;
            table.remove(hash)?;
        }
        write_txn.commit()?;
        Ok(())
    }

    // Logical key operations for upsert

    /// Store logical key → memory ID mapping
    ///
    /// Used for upsert operations with developer-defined keys
    pub fn store_logical_key(&self, key: &str, memory_id: &MemoryId) -> Result<()> {
        let write_txn = self.db.begin_write()?;
        {
            let mut table = write_txn.open_table(LOGICAL_KEY_INDEX)?;
            table.insert(key, memory_id.as_bytes() as &[u8])?;
        }
        write_txn.commit()?;
        Ok(())
    }

    /// Find memory ID by logical key
    ///
    /// Returns None if key not found
    pub fn find_by_logical_key(&self, key: &str) -> Result<Option<MemoryId>> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(LOGICAL_KEY_INDEX)?;

        match table.get(key)? {
            Some(bytes) => {
                let id = MemoryId::from_bytes(bytes.value())?;
                Ok(Some(id))
            }
            None => Ok(None),
        }
    }

    /// Delete logical key mapping
    pub fn delete_logical_key(&self, key: &str) -> Result<()> {
        let write_txn = self.db.begin_write()?;
        {
            let mut table = write_txn.open_table(LOGICAL_KEY_INDEX)?;
            table.remove(key)?;
        }
        write_txn.commit()?;
        Ok(())
    }

    /// Update logical key mapping to point to a new memory ID
    ///
    /// This is used when an upsert operation replaces an existing memory
    pub fn update_logical_key(&self, key: &str, new_memory_id: &MemoryId) -> Result<()> {
        // Just overwrite - same as store
        self.store_logical_key(key, new_memory_id)
    }

    // Namespace operations

    /// List all namespaces in the database
    ///
    /// Scans all memories and extracts unique namespace values.
    /// Returns sorted list of namespace strings (excluding default namespace "").
    ///
    /// # Performance
    ///
    /// O(n) where n = total memories. Can be cached if needed.
    pub fn list_namespaces(&self) -> Result<Vec<String>> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(MEMORIES)?;

        let mut namespaces = std::collections::HashSet::new();

        // Scan all memories
        for entry in table.iter()? {
            let (_, value) = entry?;
            let memory_data = value.value();

            // Deserialize memory to get namespace
            if let Ok(memory) = self.deserialize_memory(memory_data) {
                let ns = memory.get_namespace();
                if !ns.is_empty() {
                    namespaces.insert(ns);
                }
            }
        }

        let mut result: Vec<String> = namespaces.into_iter().collect();
        result.sort();
        Ok(result)
    }

    /// Count memories in a specific namespace
    ///
    /// # Arguments
    ///
    /// * `namespace` - The namespace to count (empty string "" for default)
    ///
    /// # Returns
    ///
    /// Number of memories in the namespace
    pub fn count_namespace(&self, namespace: &str) -> Result<usize> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(MEMORIES)?;

        let mut count = 0;

        // Scan all memories
        for entry in table.iter()? {
            let (_, value) = entry?;
            let memory_data = value.value();

            // Deserialize memory to check namespace
            if let Ok(memory) = self.deserialize_memory(memory_data) {
                if memory.get_namespace() == namespace {
                    count += 1;
                }
            }
        }

        Ok(count)
    }

    /// List all memory IDs in a specific namespace
    ///
    /// # Arguments
    ///
    /// * `namespace` - The namespace to list (empty string "" for default)
    ///
    /// # Returns
    ///
    /// Vector of MemoryIds in the namespace
    pub fn list_namespace_ids(&self, namespace: &str) -> Result<Vec<MemoryId>> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(MEMORIES)?;

        let mut ids = Vec::new();

        // Scan all memories
        for entry in table.iter()? {
            let (key, value) = entry?;
            let memory_data = value.value();

            // Deserialize memory to check namespace
            if let Ok(memory) = self.deserialize_memory(memory_data) {
                if memory.get_namespace() == namespace {
                    // Get memory ID from key
                    let id = MemoryId::from_bytes(key.value())?;
                    ids.push(id);
                }
            }
        }

        Ok(ids)
    }

    // Metadata index operations

    /// Build a metadata index key
    ///
    /// Format: "{field}:{value}:{namespace}"
    fn metadata_index_key(field: &str, value: &str, namespace: &str) -> String {
        format!("{}:{}:{}", field, value, namespace)
    }

    /// Store a metadata field value in the index
    ///
    /// Associates a memory with a specific metadata field value in a namespace.
    ///
    /// # Arguments
    ///
    /// * `field` - The metadata field name
    /// * `value` - The field value
    /// * `namespace` - The namespace the memory belongs to
    /// * `memory_id` - The memory ID to associate with this field value
    pub fn add_to_metadata_index(
        &self,
        field: &str,
        value: &str,
        namespace: &str,
        memory_id: &MemoryId,
    ) -> Result<()> {
        let write_txn = self.db.begin_write()?;
        {
            let mut table = write_txn.open_table(METADATA_INDEX)?;

            let key = Self::metadata_index_key(field, value, namespace);

            // Get existing memory IDs for this key
            let mut ids: Vec<MemoryId> = match table.get(key.as_str())? {
                Some(data) => serde_json::from_slice(data.value())?,
                None => Vec::new(),
            };

            // Add new ID if not already present
            if !ids.contains(memory_id) {
                ids.push(memory_id.clone());

                // Serialize and store
                let data = serde_json::to_vec(&ids)?;
                table.insert(key.as_str(), data.as_slice())?;
            }
        }
        write_txn.commit()?;
        Ok(())
    }

    /// Remove a memory from a metadata index entry
    ///
    /// # Arguments
    ///
    /// * `field` - The metadata field name
    /// * `value` - The field value
    /// * `namespace` - The namespace
    /// * `memory_id` - The memory ID to remove
    pub fn remove_from_metadata_index(
        &self,
        field: &str,
        value: &str,
        namespace: &str,
        memory_id: &MemoryId,
    ) -> Result<()> {
        let write_txn = self.db.begin_write()?;
        {
            let mut table = write_txn.open_table(METADATA_INDEX)?;

            let key = Self::metadata_index_key(field, value, namespace);

            // Get existing memory IDs for this key and clone the data
            let ids_data = table.get(key.as_str())?.map(|data| data.value().to_vec());

            if let Some(data_vec) = ids_data {
                let mut ids: Vec<MemoryId> = serde_json::from_slice(&data_vec)?;

                // Remove the memory ID
                ids.retain(|id| id != memory_id);

                if ids.is_empty() {
                    // Remove the index entry if no more memories
                    table.remove(key.as_str())?;
                } else {
                    // Update with remaining IDs
                    let data = serde_json::to_vec(&ids)?;
                    table.insert(key.as_str(), data.as_slice())?;
                }
            }
        }
        write_txn.commit()?;
        Ok(())
    }

    /// Find all memory IDs matching a metadata field value
    ///
    /// # Arguments
    ///
    /// * `field` - The metadata field name
    /// * `value` - The field value to match
    /// * `namespace` - The namespace to search in
    ///
    /// # Returns
    ///
    /// Vector of memory IDs that have the specified metadata field value
    pub fn find_by_metadata(
        &self,
        field: &str,
        value: &str,
        namespace: &str,
    ) -> Result<Vec<MemoryId>> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(METADATA_INDEX)?;

        let key = Self::metadata_index_key(field, value, namespace);

        match table.get(key.as_str())? {
            Some(data) => {
                let ids: Vec<MemoryId> = serde_json::from_slice(data.value())?;
                Ok(ids)
            }
            None => Ok(Vec::new()),
        }
    }

    /// Remove all metadata index entries for a memory
    ///
    /// Used when deleting a memory to clean up all its metadata indexes.
    ///
    /// # Arguments
    ///
    /// * `memory` - The memory being deleted
    /// * `indexed_fields` - List of fields that are indexed
    pub fn remove_metadata_indexes_for_memory(
        &self,
        memory: &Memory,
        indexed_fields: &[String],
    ) -> Result<()> {
        let namespace = memory.get_namespace();

        // For each indexed field, remove this memory from the index
        for field in indexed_fields {
            if let Some(value) = memory.metadata.get(field) {
                self.remove_from_metadata_index(field, value, &namespace, &memory.id)?;
            }
        }

        Ok(())
    }

    // Entity Profile operations

    /// Store an entity profile
    ///
    /// Profiles are keyed by the normalized (lowercase) entity name for
    /// case-insensitive lookups.
    ///
    /// # Arguments
    ///
    /// * `profile` - The entity profile to store
    ///
    /// # Example
    ///
    /// ```ignore
    /// let profile = EntityProfile::new(EntityId::new(), "Alice".into(), "person".into());
    /// storage.store_entity_profile(&profile)?;
    /// ```
    pub fn store_entity_profile(&self, profile: &EntityProfile) -> Result<()> {
        let write_txn = self.db.begin_write()?;
        {
            let mut table = write_txn.open_table(ENTITY_PROFILES)?;
            let key = profile.name.to_lowercase();
            let data =
                serde_json::to_vec(profile).map_err(|e| Error::Serialization(e.to_string()))?;
            table.insert(key.as_str(), data.as_slice())?;
        }
        write_txn.commit()?;
        Ok(())
    }

    /// Get an entity profile by name (case-insensitive)
    ///
    /// # Arguments
    ///
    /// * `name` - The entity name to look up
    ///
    /// # Returns
    ///
    /// The entity profile if found, or None
    ///
    /// # Example
    ///
    /// ```ignore
    /// let profile = storage.get_entity_profile("Alice")?;
    /// if let Some(p) = profile {
    ///     println!("Found profile for {}", p.name);
    /// }
    /// ```
    pub fn get_entity_profile(&self, name: &str) -> Result<Option<EntityProfile>> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(ENTITY_PROFILES)?;
        let key = name.to_lowercase();

        match table.get(key.as_str())? {
            Some(data) => {
                let profile: EntityProfile = serde_json::from_slice(data.value())
                    .map_err(|e| Error::Deserialization(e.to_string()))?;
                Ok(Some(profile))
            }
            None => Ok(None),
        }
    }

    /// List all entity profiles
    ///
    /// # Returns
    ///
    /// Vector of all entity profiles in the database
    ///
    /// # Performance
    ///
    /// O(n) where n = number of profiles. Use with caution on large databases.
    pub fn list_entity_profiles(&self) -> Result<Vec<EntityProfile>> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(ENTITY_PROFILES)?;

        let mut profiles = Vec::new();
        for result in table.iter()? {
            let (_, value) = result?;
            let profile: EntityProfile = serde_json::from_slice(value.value())
                .map_err(|e| Error::Deserialization(e.to_string()))?;
            profiles.push(profile);
        }
        Ok(profiles)
    }

    /// Delete an entity profile by name (case-insensitive)
    ///
    /// # Arguments
    ///
    /// * `name` - The entity name to delete
    ///
    /// # Returns
    ///
    /// true if the profile was deleted, false if it didn't exist
    pub fn delete_entity_profile(&self, name: &str) -> Result<bool> {
        let write_txn = self.db.begin_write()?;
        let deleted = {
            let mut table = write_txn.open_table(ENTITY_PROFILES)?;
            let key = name.to_lowercase();
            let result = table.remove(key.as_str())?;
            result.is_some()
        };
        write_txn.commit()?;
        Ok(deleted)
    }

    /// Count entity profiles
    ///
    /// # Returns
    ///
    /// Number of entity profiles in the database
    pub fn count_entity_profiles(&self) -> Result<usize> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(ENTITY_PROFILES)?;
        Ok(table.len()? as usize)
    }

    /// List all entity profile names (lightweight, no full deserialization)
    ///
    /// Returns the table keys directly, which are the lowercased entity names.
    /// Much faster than `list_entity_profiles()` when you only need names.
    pub fn list_entity_profile_names(&self) -> Result<Vec<String>> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(ENTITY_PROFILES)?;

        let mut names = Vec::new();
        for result in table.iter()? {
            let (key, _) = result?;
            names.push(key.value().to_string());
        }
        Ok(names)
    }

    // Fact embedding operations

    /// Store a fact embedding
    ///
    /// Key is a 16-byte hash of (entity_name, fact_type, value).
    /// Value is the embedding as raw little-endian f32 bytes.
    pub fn store_fact_embedding(&self, key: &[u8], embedding: &[f32]) -> Result<()> {
        let write_txn = self.db.begin_write()?;
        {
            let mut table = write_txn.open_table(FACT_EMBEDDINGS)?;
            let bytes: Vec<u8> = embedding.iter().flat_map(|f| f.to_le_bytes()).collect();
            table.insert(key, bytes.as_slice())?;
        }
        write_txn.commit()?;
        Ok(())
    }

    /// Get a fact embedding by key
    ///
    /// Returns the embedding as Vec<f32>, or None if not found.
    pub fn get_fact_embedding(&self, key: &[u8]) -> Result<Option<Vec<f32>>> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(FACT_EMBEDDINGS)?;

        match table.get(key)? {
            Some(data) => {
                let bytes = data.value();
                if bytes.len() % 4 != 0 {
                    return Err(Error::Deserialization(
                        "Invalid fact embedding data length".to_string(),
                    ));
                }
                let embedding: Vec<f32> = bytes
                    .chunks_exact(4)
                    .map(|chunk| f32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]))
                    .collect();
                Ok(Some(embedding))
            }
            None => Ok(None),
        }
    }
}

// Add serde_json for metadata serialization
// This is a temporary solution - we'll use rkyv for zero-copy in later sprints

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

    #[test]
    fn test_storage_engine_open() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");

        let engine = StorageEngine::open(&path).unwrap();
        assert_eq!(engine.path(), path);
    }

    #[test]
    fn test_storage_engine_store_and_retrieve() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");
        let engine = StorageEngine::open(&path).unwrap();

        let memory = Memory::new("test content".to_string(), vec![0.1, 0.2, 0.3]);
        let id = memory.id.clone();

        engine.store_memory(&memory).unwrap();

        let retrieved = engine.get_memory(&id).unwrap();
        assert!(retrieved.is_some());

        let retrieved = retrieved.unwrap();
        assert_eq!(retrieved.id, id);
        assert_eq!(retrieved.content, "test content");
        assert_eq!(retrieved.embedding, vec![0.1, 0.2, 0.3]);
    }

    #[test]
    fn test_storage_engine_delete() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");
        let engine = StorageEngine::open(&path).unwrap();

        let memory = Memory::new("test".to_string(), vec![0.1]);
        let id = memory.id.clone();

        engine.store_memory(&memory).unwrap();
        assert!(engine.get_memory(&id).unwrap().is_some());

        let deleted = engine.delete_memory(&id).unwrap();
        assert!(deleted);
        assert!(engine.get_memory(&id).unwrap().is_none());
    }

    #[test]
    fn test_storage_engine_not_found() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");
        let engine = StorageEngine::open(&path).unwrap();

        let id = MemoryId::new();
        assert!(engine.get_memory(&id).unwrap().is_none());
    }

    #[test]
    fn test_storage_engine_multiple_memories() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");
        let engine = StorageEngine::open(&path).unwrap();

        let mem1 = Memory::new("first".to_string(), vec![0.1]);
        let mem2 = Memory::new("second".to_string(), vec![0.2]);
        let mem3 = Memory::new("third".to_string(), vec![0.3]);

        engine.store_memory(&mem1).unwrap();
        engine.store_memory(&mem2).unwrap();
        engine.store_memory(&mem3).unwrap();

        assert_eq!(engine.count_memories().unwrap(), 3);

        let ids = engine.list_memory_ids().unwrap();
        assert_eq!(ids.len(), 3);
    }

    #[test]
    fn test_storage_engine_with_metadata() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");
        let engine = StorageEngine::open(&path).unwrap();

        let mut metadata = HashMap::new();
        metadata.insert("source".to_string(), "test".to_string());
        metadata.insert("category".to_string(), "example".to_string());

        let memory = Memory::new_with_metadata("test".to_string(), vec![0.1], metadata);
        let id = memory.id.clone();

        engine.store_memory(&memory).unwrap();

        let retrieved = engine.get_memory(&id).unwrap().unwrap();
        assert_eq!(retrieved.metadata.len(), 2);
        assert_eq!(retrieved.metadata.get("source"), Some(&"test".to_string()));
        assert_eq!(
            retrieved.metadata.get("category"),
            Some(&"example".to_string())
        );
    }

    #[test]
    fn test_storage_engine_reopen() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");

        let memory = Memory::new("persistent".to_string(), vec![0.5]);
        let id = memory.id.clone();

        // Store in first instance
        {
            let engine = StorageEngine::open(&path).unwrap();
            engine.store_memory(&memory).unwrap();
        }

        // Retrieve in second instance
        {
            let engine = StorageEngine::open(&path).unwrap();
            let retrieved = engine.get_memory(&id).unwrap();
            assert!(retrieved.is_some());
            assert_eq!(retrieved.unwrap().content, "persistent");
        }
    }

    #[test]
    fn test_storage_list_namespaces_empty() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");
        let engine = StorageEngine::open(&path).unwrap();

        // No namespaces initially
        let namespaces = engine.list_namespaces().unwrap();
        assert!(namespaces.is_empty());
    }

    #[test]
    fn test_storage_list_namespaces() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");
        let engine = StorageEngine::open(&path).unwrap();

        // Add memories to different namespaces
        let mut mem1 = Memory::new("content 1".to_string(), vec![0.1; 384]);
        mem1.set_namespace("user_123");
        engine.store_memory(&mem1).unwrap();

        let mut mem2 = Memory::new("content 2".to_string(), vec![0.2; 384]);
        mem2.set_namespace("user_456");
        engine.store_memory(&mem2).unwrap();

        let mut mem3 = Memory::new("content 3".to_string(), vec![0.3; 384]);
        mem3.set_namespace("user_123"); // Duplicate namespace
        engine.store_memory(&mem3).unwrap();

        // Default namespace (no set_namespace)
        let mem4 = Memory::new("content 4".to_string(), vec![0.4; 384]);
        engine.store_memory(&mem4).unwrap();

        // Should return 2 unique non-default namespaces (sorted)
        let namespaces = engine.list_namespaces().unwrap();
        assert_eq!(namespaces.len(), 2);
        assert_eq!(namespaces[0], "user_123");
        assert_eq!(namespaces[1], "user_456");
    }

    #[test]
    fn test_storage_count_namespace() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");
        let engine = StorageEngine::open(&path).unwrap();

        // Add memories to different namespaces
        let mut mem1 = Memory::new("content 1".to_string(), vec![0.1; 384]);
        mem1.set_namespace("user_123");
        engine.store_memory(&mem1).unwrap();

        let mut mem2 = Memory::new("content 2".to_string(), vec![0.2; 384]);
        mem2.set_namespace("user_123");
        engine.store_memory(&mem2).unwrap();

        let mut mem3 = Memory::new("content 3".to_string(), vec![0.3; 384]);
        mem3.set_namespace("user_456");
        engine.store_memory(&mem3).unwrap();

        let mem4 = Memory::new("content 4".to_string(), vec![0.4; 384]);
        engine.store_memory(&mem4).unwrap();

        // Count by namespace
        assert_eq!(engine.count_namespace("user_123").unwrap(), 2);
        assert_eq!(engine.count_namespace("user_456").unwrap(), 1);
        assert_eq!(engine.count_namespace("").unwrap(), 1); // Default namespace
        assert_eq!(engine.count_namespace("nonexistent").unwrap(), 0);
    }

    #[test]
    fn test_storage_list_namespace_ids() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");
        let engine = StorageEngine::open(&path).unwrap();

        // Add memories to namespace
        let mut mem1 = Memory::new("content 1".to_string(), vec![0.1; 384]);
        mem1.set_namespace("user_123");
        let id1 = mem1.id.clone();
        engine.store_memory(&mem1).unwrap();

        let mut mem2 = Memory::new("content 2".to_string(), vec![0.2; 384]);
        mem2.set_namespace("user_123");
        let id2 = mem2.id.clone();
        engine.store_memory(&mem2).unwrap();

        let mut mem3 = Memory::new("content 3".to_string(), vec![0.3; 384]);
        mem3.set_namespace("user_456");
        engine.store_memory(&mem3).unwrap();

        // List IDs by namespace
        let ids = engine.list_namespace_ids("user_123").unwrap();
        assert_eq!(ids.len(), 2);
        assert!(ids.contains(&id1));
        assert!(ids.contains(&id2));

        let ids_456 = engine.list_namespace_ids("user_456").unwrap();
        assert_eq!(ids_456.len(), 1);

        let ids_empty = engine.list_namespace_ids("nonexistent").unwrap();
        assert!(ids_empty.is_empty());
    }

    #[test]
    fn test_storage_namespace_default() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");
        let engine = StorageEngine::open(&path).unwrap();

        // Memory without explicit namespace
        let mem = Memory::new("default content".to_string(), vec![0.1; 384]);
        let id = mem.id.clone();
        engine.store_memory(&mem).unwrap();

        // Should be in default namespace ""
        assert_eq!(engine.count_namespace("").unwrap(), 1);

        let ids = engine.list_namespace_ids("").unwrap();
        assert_eq!(ids.len(), 1);
        assert_eq!(ids[0], id);

        // Should not appear in list_namespaces (only non-default)
        let namespaces = engine.list_namespaces().unwrap();
        assert!(namespaces.is_empty());
    }

    #[test]
    fn test_metadata_index_add_and_find() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");
        let engine = StorageEngine::open(&path).unwrap();

        let mut mem = Memory::new("test content".to_string(), vec![0.1; 384]);
        mem.metadata.insert("type".to_string(), "event".to_string());
        mem.metadata
            .insert("priority".to_string(), "high".to_string());
        let id = mem.id.clone();

        // Add to metadata index
        engine
            .add_to_metadata_index("type", "event", "", &id)
            .unwrap();
        engine
            .add_to_metadata_index("priority", "high", "", &id)
            .unwrap();

        // Find by metadata
        let ids = engine.find_by_metadata("type", "event", "").unwrap();
        assert_eq!(ids.len(), 1);
        assert_eq!(ids[0], id);

        let ids = engine.find_by_metadata("priority", "high", "").unwrap();
        assert_eq!(ids.len(), 1);
        assert_eq!(ids[0], id);

        // Non-existent value
        let ids = engine.find_by_metadata("type", "task", "").unwrap();
        assert!(ids.is_empty());
    }

    #[test]
    fn test_metadata_index_multiple_memories() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");
        let engine = StorageEngine::open(&path).unwrap();

        let mut mem1 = Memory::new("content 1".to_string(), vec![0.1; 384]);
        mem1.metadata
            .insert("type".to_string(), "event".to_string());
        let id1 = mem1.id.clone();

        let mut mem2 = Memory::new("content 2".to_string(), vec![0.2; 384]);
        mem2.metadata
            .insert("type".to_string(), "event".to_string());
        let id2 = mem2.id.clone();

        let mut mem3 = Memory::new("content 3".to_string(), vec![0.3; 384]);
        mem3.metadata.insert("type".to_string(), "task".to_string());
        let id3 = mem3.id.clone();

        // Add to index
        engine
            .add_to_metadata_index("type", "event", "", &id1)
            .unwrap();
        engine
            .add_to_metadata_index("type", "event", "", &id2)
            .unwrap();
        engine
            .add_to_metadata_index("type", "task", "", &id3)
            .unwrap();

        // Find all events
        let ids = engine.find_by_metadata("type", "event", "").unwrap();
        assert_eq!(ids.len(), 2);
        assert!(ids.contains(&id1));
        assert!(ids.contains(&id2));

        // Find all tasks
        let ids = engine.find_by_metadata("type", "task", "").unwrap();
        assert_eq!(ids.len(), 1);
        assert_eq!(ids[0], id3);
    }

    #[test]
    fn test_metadata_index_with_namespace() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");
        let engine = StorageEngine::open(&path).unwrap();

        let mut mem1 = Memory::new("content 1".to_string(), vec![0.1; 384]);
        mem1.set_namespace("user_123");
        mem1.metadata
            .insert("type".to_string(), "event".to_string());
        let id1 = mem1.id.clone();

        let mut mem2 = Memory::new("content 2".to_string(), vec![0.2; 384]);
        mem2.set_namespace("user_456");
        mem2.metadata
            .insert("type".to_string(), "event".to_string());
        let id2 = mem2.id.clone();

        // Add to index
        engine
            .add_to_metadata_index("type", "event", "user_123", &id1)
            .unwrap();
        engine
            .add_to_metadata_index("type", "event", "user_456", &id2)
            .unwrap();

        // Find in each namespace
        let ids = engine
            .find_by_metadata("type", "event", "user_123")
            .unwrap();
        assert_eq!(ids.len(), 1);
        assert_eq!(ids[0], id1);

        let ids = engine
            .find_by_metadata("type", "event", "user_456")
            .unwrap();
        assert_eq!(ids.len(), 1);
        assert_eq!(ids[0], id2);

        // Not found in wrong namespace
        let ids = engine.find_by_metadata("type", "event", "").unwrap();
        assert!(ids.is_empty());
    }

    #[test]
    fn test_metadata_index_remove() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");
        let engine = StorageEngine::open(&path).unwrap();

        let mut mem = Memory::new("test content".to_string(), vec![0.1; 384]);
        mem.metadata.insert("type".to_string(), "event".to_string());
        let id = mem.id.clone();

        // Add to index
        engine
            .add_to_metadata_index("type", "event", "", &id)
            .unwrap();

        // Verify it's there
        let ids = engine.find_by_metadata("type", "event", "").unwrap();
        assert_eq!(ids.len(), 1);

        // Remove from index
        engine
            .remove_from_metadata_index("type", "event", "", &id)
            .unwrap();

        // Verify it's gone
        let ids = engine.find_by_metadata("type", "event", "").unwrap();
        assert!(ids.is_empty());
    }

    #[test]
    fn test_metadata_index_remove_one_of_many() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");
        let engine = StorageEngine::open(&path).unwrap();

        let mut mem1 = Memory::new("content 1".to_string(), vec![0.1; 384]);
        mem1.metadata
            .insert("type".to_string(), "event".to_string());
        let id1 = mem1.id.clone();

        let mut mem2 = Memory::new("content 2".to_string(), vec![0.2; 384]);
        mem2.metadata
            .insert("type".to_string(), "event".to_string());
        let id2 = mem2.id.clone();

        // Add both to index
        engine
            .add_to_metadata_index("type", "event", "", &id1)
            .unwrap();
        engine
            .add_to_metadata_index("type", "event", "", &id2)
            .unwrap();

        // Verify both are there
        let ids = engine.find_by_metadata("type", "event", "").unwrap();
        assert_eq!(ids.len(), 2);

        // Remove one
        engine
            .remove_from_metadata_index("type", "event", "", &id1)
            .unwrap();

        // Verify only one remains
        let ids = engine.find_by_metadata("type", "event", "").unwrap();
        assert_eq!(ids.len(), 1);
        assert_eq!(ids[0], id2);
    }

    #[test]
    fn test_metadata_index_remove_all_for_memory() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");
        let engine = StorageEngine::open(&path).unwrap();

        let mut mem = Memory::new("test content".to_string(), vec![0.1; 384]);
        mem.metadata.insert("type".to_string(), "event".to_string());
        mem.metadata
            .insert("priority".to_string(), "high".to_string());
        mem.metadata
            .insert("category".to_string(), "work".to_string());
        let id = mem.id.clone();

        // Add to indexes
        engine
            .add_to_metadata_index("type", "event", "", &id)
            .unwrap();
        engine
            .add_to_metadata_index("priority", "high", "", &id)
            .unwrap();
        engine
            .add_to_metadata_index("category", "work", "", &id)
            .unwrap();

        // Verify all are indexed
        assert!(!engine
            .find_by_metadata("type", "event", "")
            .unwrap()
            .is_empty());
        assert!(!engine
            .find_by_metadata("priority", "high", "")
            .unwrap()
            .is_empty());
        assert!(!engine
            .find_by_metadata("category", "work", "")
            .unwrap()
            .is_empty());

        // Remove all indexes for this memory
        let indexed_fields = vec![
            "type".to_string(),
            "priority".to_string(),
            "category".to_string(),
        ];
        engine
            .remove_metadata_indexes_for_memory(&mem, &indexed_fields)
            .unwrap();

        // Verify all are removed
        assert!(engine
            .find_by_metadata("type", "event", "")
            .unwrap()
            .is_empty());
        assert!(engine
            .find_by_metadata("priority", "high", "")
            .unwrap()
            .is_empty());
        assert!(engine
            .find_by_metadata("category", "work", "")
            .unwrap()
            .is_empty());
    }

    // Validation tests for Task 4: File header validation on open

    #[test]
    fn test_truncated_file_detection() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("truncated.mfdb");

        // Create a tiny truncated file
        std::fs::write(&path, b"MF").unwrap();

        // Should fail with FileTruncated error
        let result = StorageEngine::open(&path);
        assert!(result.is_err());

        if let Err(err) = result {
            assert!(matches!(err, Error::FileTruncated(_)));
        }
    }

    #[test]
    fn test_validate_database_integrity() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");

        // Create a valid database
        let engine = StorageEngine::open(&path).unwrap();

        // Validation should pass
        assert!(engine.validate_database().is_ok());
    }

    #[test]
    fn test_validate_database_with_data() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");

        // Create database with some data
        let engine = StorageEngine::open(&path).unwrap();
        let mem = Memory::new("test content".to_string(), vec![0.1; 384]);
        engine.store_memory(&mem).unwrap();

        // Validation should still pass
        assert!(engine.validate_database().is_ok());

        // Close and reopen
        drop(engine);
        let engine = StorageEngine::open(&path).unwrap();

        // Validation should pass on reopen
        assert!(engine.validate_database().is_ok());
    }

    #[test]
    fn test_open_validates_existing_database() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");

        // Create a valid database
        {
            let _engine = StorageEngine::open(&path).unwrap();
        }

        // Reopening should validate the database
        let engine = StorageEngine::open(&path).unwrap();

        // Verify header is valid
        assert!(engine.validate_database().is_ok());
    }

    // Entity Profile tests

    #[test]
    fn test_entity_profile_store_and_retrieve() {
        use crate::types::{EntityFact, EntityId, EntityProfile};

        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");
        let engine = StorageEngine::open(&path).unwrap();

        // Create a profile
        let entity_id = EntityId::new();
        let mut profile =
            EntityProfile::new(entity_id.clone(), "Alice".to_string(), "person".to_string());

        // Add a fact
        let memory_id = MemoryId::new();
        profile.add_fact(EntityFact::new(
            "occupation",
            "engineer",
            0.9,
            memory_id.clone(),
        ));
        profile.add_source_memory(memory_id);

        // Store
        engine.store_entity_profile(&profile).unwrap();

        // Retrieve
        let retrieved = engine.get_entity_profile("Alice").unwrap();
        assert!(retrieved.is_some());

        let retrieved = retrieved.unwrap();
        assert_eq!(retrieved.name, "Alice");
        assert_eq!(retrieved.entity_type, "person");
        assert_eq!(retrieved.facts.get("occupation").unwrap().len(), 1);
        assert_eq!(
            retrieved.facts.get("occupation").unwrap()[0].value,
            "engineer"
        );
    }

    #[test]
    fn test_entity_profile_case_insensitive_lookup() {
        use crate::types::{EntityId, EntityProfile};

        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");
        let engine = StorageEngine::open(&path).unwrap();

        let profile =
            EntityProfile::new(EntityId::new(), "Alice".to_string(), "person".to_string());
        engine.store_entity_profile(&profile).unwrap();

        // Case-insensitive lookup
        assert!(engine.get_entity_profile("alice").unwrap().is_some());
        assert!(engine.get_entity_profile("ALICE").unwrap().is_some());
        assert!(engine.get_entity_profile("Alice").unwrap().is_some());
        assert!(engine.get_entity_profile("aLiCe").unwrap().is_some());
    }

    #[test]
    fn test_entity_profile_not_found() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");
        let engine = StorageEngine::open(&path).unwrap();

        let result = engine.get_entity_profile("Nonexistent").unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_entity_profile_list() {
        use crate::types::{EntityId, EntityProfile};

        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");
        let engine = StorageEngine::open(&path).unwrap();

        // Create and store profiles
        let profile1 =
            EntityProfile::new(EntityId::new(), "Alice".to_string(), "person".to_string());
        let profile2 = EntityProfile::new(EntityId::new(), "Bob".to_string(), "person".to_string());
        let profile3 = EntityProfile::new(
            EntityId::new(),
            "Acme Corp".to_string(),
            "organization".to_string(),
        );

        engine.store_entity_profile(&profile1).unwrap();
        engine.store_entity_profile(&profile2).unwrap();
        engine.store_entity_profile(&profile3).unwrap();

        // List all
        let profiles = engine.list_entity_profiles().unwrap();
        assert_eq!(profiles.len(), 3);

        let names: Vec<_> = profiles.iter().map(|p| p.name.as_str()).collect();
        assert!(names.contains(&"Alice"));
        assert!(names.contains(&"Bob"));
        assert!(names.contains(&"Acme Corp"));
    }

    #[test]
    fn test_entity_profile_delete() {
        use crate::types::{EntityId, EntityProfile};

        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");
        let engine = StorageEngine::open(&path).unwrap();

        let profile =
            EntityProfile::new(EntityId::new(), "Alice".to_string(), "person".to_string());
        engine.store_entity_profile(&profile).unwrap();

        // Verify it exists
        assert!(engine.get_entity_profile("Alice").unwrap().is_some());

        // Delete
        let deleted = engine.delete_entity_profile("Alice").unwrap();
        assert!(deleted);

        // Verify it's gone
        assert!(engine.get_entity_profile("Alice").unwrap().is_none());

        // Deleting non-existent returns false
        let deleted = engine.delete_entity_profile("Alice").unwrap();
        assert!(!deleted);
    }

    #[test]
    fn test_entity_profile_count() {
        use crate::types::{EntityId, EntityProfile};

        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");
        let engine = StorageEngine::open(&path).unwrap();

        assert_eq!(engine.count_entity_profiles().unwrap(), 0);

        engine
            .store_entity_profile(&EntityProfile::new(
                EntityId::new(),
                "Alice".to_string(),
                "person".to_string(),
            ))
            .unwrap();
        assert_eq!(engine.count_entity_profiles().unwrap(), 1);

        engine
            .store_entity_profile(&EntityProfile::new(
                EntityId::new(),
                "Bob".to_string(),
                "person".to_string(),
            ))
            .unwrap();
        assert_eq!(engine.count_entity_profiles().unwrap(), 2);
    }

    #[test]
    fn test_entity_profile_update() {
        use crate::types::{EntityFact, EntityId, EntityProfile};

        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");
        let engine = StorageEngine::open(&path).unwrap();

        // Create initial profile
        let entity_id = EntityId::new();
        let mut profile =
            EntityProfile::new(entity_id.clone(), "Alice".to_string(), "person".to_string());
        profile.add_fact(EntityFact::new(
            "occupation",
            "engineer",
            0.9,
            MemoryId::new(),
        ));
        engine.store_entity_profile(&profile).unwrap();

        // Update profile with new fact
        profile.add_fact(EntityFact::new("skill", "Rust", 0.85, MemoryId::new()));
        engine.store_entity_profile(&profile).unwrap();

        // Retrieve and verify update
        let retrieved = engine.get_entity_profile("Alice").unwrap().unwrap();
        assert_eq!(retrieved.facts.len(), 2);
        assert!(retrieved.facts.contains_key("occupation"));
        assert!(retrieved.facts.contains_key("skill"));
    }

    #[test]
    fn test_entity_profile_persistence() {
        use crate::types::{EntityFact, EntityId, EntityProfile};

        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");

        let entity_id = EntityId::new();

        // Create and store profile
        {
            let engine = StorageEngine::open(&path).unwrap();
            let mut profile =
                EntityProfile::new(entity_id.clone(), "Alice".to_string(), "person".to_string());
            profile.add_fact(EntityFact::new(
                "occupation",
                "engineer",
                0.9,
                MemoryId::new(),
            ));
            engine.store_entity_profile(&profile).unwrap();
        }

        // Reopen and verify
        {
            let engine = StorageEngine::open(&path).unwrap();
            let retrieved = engine.get_entity_profile("Alice").unwrap().unwrap();
            assert_eq!(retrieved.name, "Alice");
            assert_eq!(
                retrieved.facts.get("occupation").unwrap()[0].value,
                "engineer"
            );
        }
    }
}