disklru 0.3.3

DiskLRU is an experimental LRU store.
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
// Copyright (C) 2024 Christian Mauduit <ufoot@ufoot.org>

use crate::error::{Error, Result};
use crate::iter::*;
use crate::serial::*;
use crate::trans;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use sled;
use sled::{Config, Db};
use std::collections::HashMap;
use std::fmt;
use std::hash::Hash;
use std::marker::PhantomData;
use std::path::Path;

const MAX_TO_DISPLAY: usize = 20;

/// LRU store, backed by by [sled](https://crates.io/crates/sled).
///
/// The typical use case is: you want a persistent cache, with an
/// interface that has similarities with a basic HashMap.
/// And, you do not want it to grow too much, and prefer to drop
/// on the floor data which has not been accessed for a long time.
///
/// LRU is normally used for in-memory caches, but here it's implemented
/// on something which is persistent. Implementation is inspired from
/// work on the in-memory cache [HashLRU](https://crates.io/crates/hashlru).
///
/// In most cases this implementation tries to be as close as possible
/// to the [standard collections HashMap](https://doc.rust-lang.org/std/collections/struct.HashMap.html)
/// however there are a few differences:
///
/// - keys and values must be (de)serialiable
/// - keys must implement Eq
/// - no iterator on mutables
/// - as it relies on external data, any operation can fail
/// - inserts take references, gets return owned values
///
/// This latest aspect is really the fundamental difference: as here all
/// the data is serialized and stored within the store, returning a reference
/// does not really make any sense. Conversely, when putting values in the
/// store, you don't need to transfer ownership: the store is going to
/// make a serializable copy anyway.
///
/// # Examples
///
/// ```
/// use disklru::Store;
///
/// let mut store = Store::open_temporary(4).unwrap();
/// store.insert(&1, &10).unwrap();
/// store.insert(&2, &20).unwrap();
/// store.insert(&3, &30).unwrap();
/// store.insert(&4, &40).unwrap();
/// store.insert(&5, &50).unwrap();
/// // key1 has been dropped, size is limited to 4
/// assert_eq!(Some(2), store.lru().unwrap());
/// assert_eq!(Some(20), store.get(&2).unwrap());
/// // getting key2 has made key3 the least recently used item
/// assert_eq!(Some(3), store.lru().unwrap());
/// assert_eq!(Some(40), store.get(&4).unwrap());
/// // getting key4 makes it the most recently used item
/// assert_eq!("[3: 30, 5: 50, 2: 20, 4: 40]", format!("{}", store));
/// store.flush().unwrap(); // commit
/// ```
#[derive(Debug)]
pub struct Store<K, V>
where
    K: Serialize + DeserializeOwned + Eq,
    V: Serialize + DeserializeOwned,
{
    pub(crate) capacity: usize,
    pub(crate) len: usize,
    pub(crate) db: Db,
    pub(crate) phantom_data: PhantomData<(K, V)>,
}

/// Complete dump of a LRU store, easily (de)serializable.
///
/// It reflects the complete current state of the cache, including
/// key/value pairs but also capacity and order. It can be costly
/// to generate as it is O(n) and also it can take a lot of space,
/// but at least you get complete control on cache content.
///
/// Can be used to import/export the store and serialize/unserialize it
/// outside [sled](https://crates.io/crates/sled). There are other ways
/// to import/export keys and values but this one has the advantage
/// to preserve all parameters, including capacity.
///
/// # Examples
///
/// ```
/// use disklru::{Store, Dump};
///
/// let mut store: Store<String, usize> = Store::open_temporary(10).unwrap();;
/// store.insert(&String::from("x"), &1);
/// store.insert(&String::from("y"), &10);
/// store.insert(&String::from("z"), &100);
///
/// let dump: Dump<String, usize> = store.dump().unwrap();
/// let mut restored: Store<String, usize> = Store::open_temporary(0).unwrap();
/// assert_eq!(3, restored.restore(&dump).unwrap());
/// assert_eq!(10, restored.capacity());
/// assert_eq!("[x: 1, y: 10, z: 100]", format!("{}", &restored));
/// ```
#[derive(Debug, Serialize, Deserialize)]
pub struct Dump<K, V> {
    pub capacity: usize,
    pub data: Vec<(K, V)>,
}

impl<K, V> fmt::Display for Store<K, V>
where
    K: Serialize + DeserializeOwned + Eq + fmt::Display,
    V: Serialize + DeserializeOwned + fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let items = if self.len() <= MAX_TO_DISPLAY {
            self.iter()
                .map(|x| match x {
                    Ok(kv) => format!("{}: {}", kv.0, kv.1),
                    Err(e) => format!("ERROR: {}", e),
                })
                .collect::<Vec<String>>()
        } else {
            let mut acc = self
                .iter()
                .take(2)
                .map(|x| match x {
                    Ok(kv) => format!("{}: {}", kv.0, kv.1),
                    Err(e) => format!("ERROR: {}", e),
                })
                .collect::<Vec<String>>();
            acc.push("...".to_string());
            acc.push(match self.mru() {
                Ok(mk) => match mk {
                    Some(mru_k) => match self.peek_mru() {
                        Ok(mv) => match mv {
                            Some(mru_v) => format!("{}: {}", mru_k, mru_v),
                            None => format!("ERROR: {}", Error::invalid_data("no mru value")),
                        },
                        Err(e) => format!("ERROR: {}", e),
                    },
                    None => format!("ERROR: {}", Error::report_bug("no mru key")),
                },
                Err(e) => format!("ERROR: {}", e),
            });
            acc
        };
        write!(f, "[{}]", items.join(", "))
    }
}

impl<K, V> PartialEq<Store<K, V>> for Store<K, V>
where
    K: Serialize + DeserializeOwned + Eq,
    V: Serialize + DeserializeOwned + Eq,
{
    fn eq(&self, other: &Self) -> bool {
        match self.try_eq(other) {
            Ok(eq) => eq,
            Err(e) => panic!("unable to compare stores: {}", e),
        }
    }
}

impl<K, V> Eq for Store<K, V>
where
    K: Serialize + DeserializeOwned + Eq,
    V: Serialize + DeserializeOwned + Eq,
{
}

impl<K, V> Store<K, V>
where
    K: Serialize + DeserializeOwned + Eq,
    V: Serialize + DeserializeOwned + Eq,
{
    fn try_eq(&self, other: &Self) -> Result<bool> {
        if self.capacity != other.capacity {
            return Ok(false);
        }
        if self.len != other.len {
            return Ok(false);
        }

        let self_head = self.find_head()?;
        let other_head = other.find_head()?;
        if self_head != other_head {
            return Ok(false);
        }

        let iter_self = self.iter();
        let mut iter_other = other.iter();
        for self_next in iter_self {
            let self_item = self_next?;
            let other_next = iter_other.next();
            match other_next {
                Some(other_next) => {
                    let other_item = other_next?;
                    if self_item != other_item {
                        return Ok(false);
                    }
                }
                None => return Err(Error::report_bug("found self but not other")),
            }
        }

        Ok(true)
    }
}

impl<K, V> Store<K, V>
where
    K: Serialize + DeserializeOwned + Eq,
    V: Serialize + DeserializeOwned,
{
    pub fn open_with_config(config: Config, capacity: usize) -> Result<Self> {
        let db = match config.open() {
            Ok(db) => db,
            Err(e) => return Err(Error::from(e)),
        };
        let mut store = Store {
            capacity,
            len: 0,
            db,
            phantom_data: PhantomData,
        };
        // We've initialized the store with some capacity passed
        // as an arg, but we still read it from db, if it already
        // existed. Also we store it back within the db, this both:
        // - initializes it if it did not exist yet
        // - ensures the store can be read/write, this way we fail early
        store.set_capacity_from_db()?;
        store.update_db_capacity()?;

        // Set let from value stored in DB if available.
        // Calculating it dynamically on big stores can *REALLY* be slow.
        store.set_len_from_db()?;
        store.update_db_len()?;

        // Set the head of the store to none, having a head, even
        // undefined, is really an invariant.
        store.init_head_if_not_exists()?;

        // A bit of cleanup, trim extra data, having unclean stuff
        // here would really mess up things. We do not error on this,
        // there could be a workable state where all things are good
        // but there is just a capacity mismatch.
        store.remove_extra(capacity)?;

        Ok(store)
    }

    fn init_head_if_not_exists(&self) -> Result<()> {
        let db = self.db.clone();
        db.transaction(|db| trans::init_head_if_not_exists::<K>(db))?;
        Ok(())
    }

    pub fn open_with_path<P: AsRef<Path>>(path: P, capacity: usize) -> Result<Self> {
        Self::open_with_config(Config::default().path(path), capacity)
    }

    pub fn open_temporary(capacity: usize) -> Result<Self> {
        Self::open_with_config(Config::default().temporary(true), capacity)
    }

    pub fn capacity(&self) -> usize {
        self.capacity
    }

    fn set_len_from_db(&mut self) -> Result<()> {
        let db = self.db.clone();
        let trans_ret = db.transaction(|db| trans::get_len(db))?;
        match trans_ret {
            Some(len) => {
                self.len = len;
            }
            None => (),
        }
        Ok(())
    }

    fn set_capacity_from_db(&mut self) -> Result<()> {
        let db = self.db.clone();
        let trans_ret = db.transaction(|db| trans::get_capacity(db))?;
        match trans_ret {
            Some(capacity) => {
                self.capacity = capacity;
            }
            None => (),
        }
        Ok(())
    }

    fn update_db_capacity(&mut self) -> Result<()> {
        let db = self.db.clone();
        let capacity = self.capacity;
        db.transaction(|db| trans::set_capacity(db, capacity))?;
        Ok(())
    }

    pub fn len(&self) -> usize {
        self.len
    }

    fn update_db_len(&mut self) -> Result<()> {
        let db = self.db.clone();
        let len = self.len;
        db.transaction(|db| trans::set_len(db, len))?;
        Ok(())
    }

    pub fn resize(&mut self, capacity: usize) -> Result<usize> {
        self.capacity = capacity;
        let removed = self.remove_extra(self.capacity)?;
        self.update_db_capacity()?;
        Ok(removed)
    }

    pub fn clear(&mut self) -> Result<()> {
        self.remove_extra(0)?;
        Ok(())
    }

    pub(crate) fn find_head(&self) -> Result<Option<K>> {
        let db = self.db.clone();
        let trans_ret = db.transaction(|db| trans::find_head(db))?;
        Ok(trans_ret)
    }

    pub(crate) fn find_tail(&self) -> Result<Option<K>> {
        let db = self.db.clone();
        let trans_ret = db.transaction(|db| trans::find_tail(db))?;
        Ok(trans_ret)
    }

    pub fn flush(&self) -> Result<usize> {
        match self.db.flush() {
            Ok(s) => Ok(s),
            Err(e) => Err(Error::from(e)),
        }
    }

    pub async fn flush_async(&self) -> Result<usize> {
        match self.db.flush_async().await {
            Ok(s) => Ok(s),
            Err(e) => Err(Error::from(e)),
        }
    }

    pub fn mru(&self) -> Result<Option<K>> {
        self.find_head()
    }

    pub fn lru(&self) -> Result<Option<K>> {
        self.find_tail()
    }

    fn remove_extra(&mut self, limit: usize) -> Result<usize> {
        let mut removed: usize = 0;
        if self.len > limit {
            let db = self.db.clone();
            while self.len > limit {
                let len = self.len;
                self.len = db.transaction(|db| trans::forget_tail_must_exist::<K>(db, len))?;
                removed += 1;
            }
        }
        Ok(removed)
    }

    pub fn insert(&mut self, k: &K, v: &V) -> Result<Option<V>> {
        if self.capacity == 0 {
            return Ok(None);
        }

        let db = self.db.clone();
        let len = self.len;

        let trans_ret = db.transaction(|db| trans::insert(db, k, v, len))?;
        self.len = trans_ret.1;

        self.remove_extra(self.capacity)?;

        Ok(trans_ret.0)
    }

    pub fn push(&mut self, k: &K, v: &V) -> Result<Option<(K, V)>> {
        if self.capacity == 0 {
            return Ok(None);
        }

        self.remove_extra(self.capacity)?;

        let db = self.db.clone();
        let len = self.len;
        let capacity = self.capacity;

        let trans_ret = db.transaction(|db| trans::push(db, k, v, len, capacity))?;
        self.len = trans_ret.1;

        Ok(trans_ret.0)
    }

    pub fn remove(&mut self, k: &K) -> Result<Option<V>> {
        let db = self.db.clone();
        let len = self.len;

        let trans_ret = db.transaction(|db| trans::remove(db, k, len))?;
        self.len = trans_ret.1;

        Ok(trans_ret.0)
    }

    pub fn pop(&mut self, k: &K) -> Result<Option<(K, V)>> {
        let db = self.db.clone();
        let len = self.len;

        let trans_ret = db.transaction(|db| trans::pop(db, k, len))?;
        self.len = trans_ret.1;

        Ok(trans_ret.0)
    }

    pub fn pop_lru(&mut self) -> Result<Option<(K, V)>> {
        let db = self.db.clone();

        let ret = match self.len {
            0 => None,
            len => {
                // Using a custom transaction as popping the LRU is something
                // very common, and also it may have an optimized code path.
                let trans_ret = db.transaction(|db| trans::pop_tail_must_exist(db, len))?;
                self.len = trans_ret.1;
                Some(trans_ret.0)
            }
        };
        Ok(ret)
    }

    pub fn pop_mru(&mut self) -> Result<Option<(K, V)>> {
        let mru = self.mru()?;

        match mru {
            Some(mru) => self.pop(&mru),
            None => Ok(None),
        }
    }

    pub fn forget(&mut self, k: &K) -> Result<()> {
        let db = self.db.clone();
        let len = self.len;

        let trans_ret = db.transaction(|db| trans::forget(db, k, len))?;
        self.len = trans_ret;

        Ok(())
    }

    pub fn forget_mru(&mut self) -> Result<()> {
        let mru = self.mru()?;
        match mru {
            Some(mru) => self.forget(&mru),
            None => Ok(()),
        }
    }

    pub fn forget_lru(&mut self) -> Result<()> {
        let lru = self.lru()?;
        match lru {
            Some(lru) => self.forget(&lru),
            None => Ok(()),
        }
    }

    pub fn contains_key(&mut self, k: &K) -> Result<bool> {
        let db = self.db.clone();

        let contains = db.transaction(|db| trans::contains_key(db, k))?;

        Ok(contains)
    }

    pub fn bump(&mut self, k: &K) -> Result<()> {
        let db = self.db.clone();

        db.transaction(|db| trans::bump(db, k))?;

        Ok(())
    }

    pub fn get(&mut self, k: &K) -> Result<Option<V>> {
        let db = self.db.clone();

        let trans_ret = db.transaction(|db| trans::get_data(db, k))?;
        match trans_ret {
            Some(data) => Ok(Some(data.value)),
            None => Ok(None),
        }
    }

    pub fn get_key_value(&mut self, k: &K) -> Result<Option<(K, V)>> {
        let db = self.db.clone();

        let trans_ret = db.transaction(|db| trans::get_data(db, k))?;
        match trans_ret {
            Some(data) => Ok(Some((data.key, data.value))),
            None => Ok(None),
        }
    }

    pub fn get_lru(&mut self) -> Result<Option<V>> {
        let lru = self.lru()?;
        match lru {
            Some(lru) => {
                let kv = self.get(&lru)?;
                Ok(kv)
            }
            None => Ok(None),
        }
    }

    pub fn peek(&self, k: &K) -> Result<Option<V>> {
        let db = self.db.clone();

        let trans_ret = db.transaction(|db| trans::peek_data(db, k))?;
        match trans_ret {
            Some(data) => Ok(Some(data.value)),
            None => Ok(None),
        }
    }

    pub fn peek_mru(&self) -> Result<Option<V>> {
        let mru = self.mru()?;
        match mru {
            Some(mru) => self.peek(&mru),
            None => Ok(None),
        }
    }

    pub fn peek_lru(&self) -> Result<Option<V>> {
        let lru = self.lru()?;
        match lru {
            Some(lru) => self.peek(&lru),
            None => Ok(None),
        }
    }

    pub fn peek_key_value(&self, k: &K) -> Result<Option<(K, V)>> {
        let db = self.db.clone();

        let trans_ret = db.transaction(|db| trans::peek_data(db, k))?;
        match trans_ret {
            Some(data) => Ok(Some((data.key, data.value))),
            None => Ok(None),
        }
    }

    pub(crate) fn peek_node(&self, k: &K) -> Result<Option<Node<K, V>>> {
        let db = self.db.clone();

        let trans_ret = db.transaction(|db| trans::peek_node(db, k))?;
        match trans_ret {
            Some(node) => Ok(Some(node)),
            None => Ok(None),
        }
    }

    pub fn export(&self) -> Export<'_, K, V> {
        Export {
            pos: None,
            done: 0,
            store: &self,
        }
    }

    pub fn iter(&self) -> Iter<'_, K, V> {
        Iter {
            pos: None,
            done: 0,
            store: &self,
        }
    }

    pub fn to_vec(&self) -> Result<Vec<(K, V)>> {
        let mut iter = self.export();
        let mut ret: Vec<(K, V)> = Vec::with_capacity(self.len);
        loop {
            let next_item = iter.try_next()?;
            match next_item {
                Some(kv) => ret.push((kv.0, kv.1)),
                None => break,
            }
        }
        Ok(ret)
    }

    pub fn import<I>(&mut self, iter: I) -> Result<usize>
    where
        I: Iterator<Item = (K, V)>,
    {
        let mut imported = 0;
        for kv in iter {
            let old = self.insert(&kv.0, &kv.1)?;
            match old {
                Some(_) => {
                    return Err(Error::invalid_data(
                        "import is overwritting existing values",
                    ))
                }
                None => imported += 1,
            }
        }
        Ok(imported)
    }

    pub fn import_map_iter<'a, I>(&mut self, iter: I) -> Result<usize>
    where
        K: Serialize + DeserializeOwned + Eq + 'a,
        V: Serialize + DeserializeOwned + 'a,
        I: Iterator<Item = (&'a K, &'a V)>,
    {
        let mut imported = 0;
        for kv in iter {
            let old = self.insert(kv.0, kv.1)?;
            match old {
                Some(_) => {
                    return Err(Error::invalid_data(
                        "import is overwritting existing values",
                    ))
                }
                None => imported += 1,
            }
        }
        Ok(imported)
    }

    pub fn import_vec_iter<'a, I>(&mut self, iter: I) -> Result<usize>
    where
        K: Serialize + DeserializeOwned + Eq + 'a,
        V: Serialize + DeserializeOwned + 'a,
        I: Iterator<Item = &'a (K, V)>,
    {
        let mut imported = 0;
        for kv in iter {
            let old = self.insert(&kv.0, &kv.1)?;
            match old {
                Some(_) => {
                    return Err(Error::invalid_data(
                        "import is overwritting existing values",
                    ))
                }
                None => imported += 1,
            }
        }
        Ok(imported)
    }

    pub fn keys(&self) -> Keys<'_, K, V> {
        Keys { iter: self.iter() }
    }

    pub fn values(&self) -> Values<'_, K, V> {
        Values { iter: self.iter() }
    }

    pub fn export_keys(&self) -> ExportKeys<'_, K, V> {
        ExportKeys {
            export: self.export(),
        }
    }

    pub fn export_values(&self) -> ExportValues<'_, K, V> {
        ExportValues {
            export: self.export(),
        }
    }

    pub fn dump(&self) -> Result<Dump<K, V>> {
        let mut data: Vec<(K, V)> = Vec::with_capacity(self.len());
        for i in self.iter() {
            let i = i?;
            data.push((i.0, i.1));
        }
        Ok(Dump {
            capacity: self.capacity(),
            data,
        })
    }

    pub fn restore(&mut self, dump: &Dump<K, V>) -> Result<usize> {
        self.clear()?;
        self.resize(dump.capacity)?;
        for i in dump.data.iter() {
            self.insert(&i.0, &i.1)?;
        }
        Ok(self.len)
    }
}

impl<K, V> Store<K, V>
where
    K: Serialize + DeserializeOwned + Eq + Hash,
    V: Serialize + DeserializeOwned,
{
    pub fn to_map(&self) -> Result<HashMap<K, V>> {
        let mut iter = self.export();
        let mut ret: HashMap<K, V> = HashMap::with_capacity(self.len);
        loop {
            let next_item = iter.try_next()?;
            match next_item {
                Some(kv) => {
                    ret.insert(kv.0, kv.1);
                }
                None => break,
            }
        }
        Ok(ret)
    }
}

impl<K, V> Store<K, V>
where
    K: Serialize + DeserializeOwned + Eq + Clone + std::fmt::Debug,
    V: Serialize + DeserializeOwned,
{
    pub fn audit(&self) -> Result<()> {
        let db = self.db.clone();

        let trans_len = db.transaction(|db| trans::get_len(db))?;
        match trans_len {
            Some(db_len) => {
                if self.len != db_len {
                    return Err(Error::invalid_data(&format!(
                        "len mismatch, expected {} but db contains {}",
                        self.len, db_len
                    )));
                }
            }
            None => return Err(Error::invalid_data("no len in db")),
        }

        let trans_capacity = db.transaction(|db| trans::get_capacity(db))?;
        match trans_capacity {
            Some(db_capacity) => {
                if self.capacity != db_capacity {
                    return Err(Error::invalid_data(&format!(
                        "capacity mismatch, expected {} but db contains {}",
                        self.capacity, db_capacity
                    )));
                }
            }
            None => return Err(Error::invalid_data("no capacity in db")),
        }

        let audit_len = db.transaction(|db| trans::audit::<K, V>(db))?;
        if self.len != audit_len {
            return Err(Error::invalid_data(&format!(
                "len mismatch, expected {} but audit returned {}",
                self.len, audit_len
            )));
        }

        let mut ptr_count = 0;
        let mut size_count = 0;
        let mut link_count = 0;
        let mut data_count = 0;
        for key in self.db.iter().keys() {
            let key = key?;
            let key_details: (Option<K>, KeyType) = deserialize_key(key)?;
            match key_details.1 {
                KeyType::Ptr => ptr_count += 1,
                KeyType::Size => size_count += 1,
                KeyType::Data => data_count += 1,
                KeyType::Link => link_count += 1,
            }
        }
        if ptr_count != 1 {
            return Err(Error::invalid_data(&format!(
                "expected 1 pointer, got {}",
                ptr_count
            )));
        }
        if size_count != 2 {
            return Err(Error::invalid_data(&format!(
                "expected 2 sizes, got {}",
                size_count
            )));
        }
        if link_count != self.len {
            return Err(Error::invalid_data(&format!(
                "expected {} links, got {}",
                self.len, link_count
            )));
        }
        if data_count != self.len {
            return Err(Error::invalid_data(&format!(
                "expected {} datas, got {}",
                self.len, data_count
            )));
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sled::Mode;
    use rand::Rng;
    use std::collections::HashMap;
    use tempfile::TempDir;

    #[test]
    fn test_open_new() {
        let tmp_dir = TempDir::new().unwrap();
        let file_path = tmp_dir.path().join("test.db");
        let store: Store<String, String> = Store::open_with_path(file_path, 10).unwrap();
        assert_eq!(10, store.capacity());
        assert_eq!(0, store.len());
        assert_eq!(None, store.mru().unwrap());
        store.audit().unwrap();
    }

    #[test]
    fn test_simple_insert_peek_1() {
        let tmp_dir = TempDir::new().unwrap();
        let file_path = tmp_dir.path().join("test.db");
        let mut store: Store<usize, usize> = Store::open_with_path(file_path, 10).unwrap();
        assert_eq!(10, store.capacity());
        assert_eq!(0, store.len());
        assert_eq!(None, store.mru().unwrap());
        assert_eq!(None, store.peek(&1).unwrap());
        assert_eq!(None, store.insert(&1, &100).unwrap());
        assert_eq!(10, store.capacity());
        assert_eq!(1, store.len());
        store.audit().unwrap();
        assert_eq!(Some(100), store.peek(&1).unwrap());
        assert_eq!(Some(1), store.mru().unwrap());
        assert_eq!(Some(1), store.lru().unwrap());
        store.audit().unwrap();
    }

    #[test]
    fn test_simple_insert_peek_remove_1() {
        let tmp_dir = TempDir::new().unwrap();
        let file_path = tmp_dir.path().join("test.db");
        let mut store: Store<usize, usize> = Store::open_with_path(file_path, 10).unwrap();
        assert_eq!(10, store.capacity());
        assert_eq!(0, store.len());
        store.audit().unwrap();
        assert_eq!(None, store.mru().unwrap());
        assert_eq!(None, store.peek(&1).unwrap());
        assert_eq!(None, store.remove(&1).unwrap());
        assert_eq!(None, store.insert(&1, &100).unwrap());
        assert_eq!(10, store.capacity());
        assert_eq!(1, store.len());
        store.audit().unwrap();
        assert_eq!(Some(100), store.peek(&1).unwrap());
        assert_eq!(Some(1), store.mru().unwrap());
        assert_eq!(None, store.remove(&2).unwrap());
        assert_eq!(Some(100), store.remove(&1).unwrap());
        assert_eq!(10, store.capacity());
        assert_eq!(0, store.len());
        store.audit().unwrap();
    }

    #[test]
    fn test_simple_insert_peek_remove_3() {
        let tmp_dir = TempDir::new().unwrap();
        let file_path = tmp_dir.path().join("test.db");
        let mut store: Store<usize, usize> = Store::open_with_path(file_path, 10).unwrap();
        assert_eq!(10, store.capacity());
        assert_eq!(0, store.len());
        store.audit().unwrap();
        assert_eq!(None, store.mru().unwrap());
        assert_eq!(None, store.peek(&1).unwrap());
        assert_eq!(None, store.remove(&1).unwrap());
        assert_eq!(None, store.insert(&1, &100).unwrap());
        store.audit().unwrap();
        assert_eq!(None, store.insert(&2, &200).unwrap());
        store.audit().unwrap();
        assert_eq!(None, store.insert(&3, &300).unwrap());
        assert_eq!(10, store.capacity());
        assert_eq!(3, store.len());
        store.audit().unwrap();
        assert_eq!(Some(100), store.peek(&1).unwrap());
        assert_eq!(Some(3), store.mru().unwrap());
        assert_eq!(Some(1), store.lru().unwrap());
        assert_eq!(None, store.remove(&4).unwrap());
        assert_eq!(Some(100), store.remove(&1).unwrap());
        store.audit().unwrap();
        assert_eq!(Some(200), store.remove(&2).unwrap());
        store.audit().unwrap();
        assert_eq!(Some(300), store.remove(&3).unwrap());
        assert_eq!(10, store.capacity());
        assert_eq!(0, store.len());
        store.audit().unwrap();
    }

    #[test]
    fn test_insert_respects_capacity() {
        let mut store: Store<usize, usize> = Store::open_temporary(10).unwrap();
        assert_eq!(10, store.capacity());
        assert_eq!(0, store.len());
        for i in 0..100 as usize {
            store.insert(&i, &i).unwrap();
        }
        assert_eq!(10, store.capacity());
        assert_eq!(10, store.len());
        assert_eq!(Some(99), store.mru().unwrap());
        assert_eq!(Some(90), store.lru().unwrap());
        store.audit().unwrap();
    }

    #[test]
    fn test_get_reorders_store() {
        let mut store: Store<usize, usize> = Store::open_temporary(10).unwrap();
        assert_eq!(10, store.capacity());
        assert_eq!(0, store.len());
        assert_eq!(None, store.insert(&1, &10).unwrap());
        assert_eq!(None, store.insert(&2, &20).unwrap());
        assert_eq!(None, store.insert(&3, &30).unwrap());
        assert_eq!(10, store.capacity());
        assert_eq!(3, store.len());
        store.audit().unwrap();
        assert_eq!(Some(10), store.get(&1).unwrap());
        assert_eq!(10, store.capacity());
        assert_eq!(3, store.len());
        store.audit().unwrap();
        assert_eq!(Some(1), store.mru().unwrap());
        assert_eq!(Some(2), store.lru().unwrap());
        store.audit().unwrap();
    }

    #[test]
    fn test_get_in_detail() {
        let mut store: Store<usize, usize> = Store::open_temporary(10).unwrap();
        assert_eq!(10, store.capacity());
        assert_eq!(0, store.len());

        assert_eq!(None, store.get(&1).unwrap());
        store.audit().unwrap();

        assert_eq!(None, store.insert(&1, &10).unwrap());
        store.audit().unwrap();
        assert_eq!(Some(10), store.get(&1).unwrap());
        store.audit().unwrap();
        assert_eq!(None, store.get(&2).unwrap());
        store.audit().unwrap();

        assert_eq!(None, store.insert(&2, &20).unwrap());
        store.audit().unwrap();
        assert_eq!(Some(10), store.get(&1).unwrap());
        store.audit().unwrap();
        assert_eq!(Some(10), store.get(&1).unwrap());
        store.audit().unwrap();
        assert_eq!(Some(20), store.get(&2).unwrap());
        store.audit().unwrap();
        assert_eq!(Some(20), store.get(&2).unwrap());
        store.audit().unwrap();

        assert_eq!(None, store.insert(&3, &30).unwrap());
        store.audit().unwrap();
        let mut gets: Vec<usize> = vec![
            1, 1, 2, 2, 3, 3, 1, 2, 3, 3, 2, 1, 2, 3, 1, 2, 1, 3, 5, 3, 3, 2, 2, 1, 1, 2, 3, 2, 3,
            1, 3, 2, 5, 3, 2, 1,
        ];
        for i in 0..gets.len() {
            let get = gets[i];
            println!("len {}, step {}, getting {}", store.len(), i, get);
            if i >= 1 && i <= 3 {
                assert_eq!(Some(10 * i), store.get(&i).unwrap());
            } else {
                assert_eq!(None, store.get(&i).unwrap());
            }
        }
        store.audit().unwrap();

        assert_eq!(None, store.insert(&4, &40).unwrap());
        store.audit().unwrap();
        gets = vec![
            1, 1, 2, 2, 3, 3, 4, 4, 1, 2, 3, 4, 4, 3, 2, 1, 1, 3, 2, 4, 2, 4, 1, 3, 2, 4, 3, 1, 5,
            3, 1, 5, 4, 2,
        ];
        for i in 0..gets.len() {
            let get = gets[i];
            println!("len {}, step {}, getting {}", store.len(), i, get);
            if i >= 1 && i <= 4 {
                assert_eq!(Some(10 * i), store.get(&i).unwrap());
            } else {
                assert_eq!(None, store.get(&i).unwrap());
            }
        }
        store.audit().unwrap();

        assert_eq!(None, store.insert(&5, &50).unwrap());
        store.audit().unwrap();
        gets = vec![
            1, 2, 3, 4, 5, 1, 1, 2, 3, 4, 5, 2, 1, 2, 3, 4, 5, 3, 1, 2, 3, 4, 5, 4, 1, 2, 3, 4, 5,
            5, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5,
        ];
        for i in 0..gets.len() {
            let get = gets[i];
            println!("len {}, step {}, getting {}", store.len(), i, get);
            if i >= 1 && i <= 5 {
                assert_eq!(Some(10 * i), store.get(&i).unwrap());
            } else {
                assert_eq!(None, store.get(&i).unwrap());
            }
        }
        store.audit().unwrap();

        // BEGIN bug-fix on gets
        // What happens -> the *FIRST* successful get somewhere in the
        // middle of the store fails.
        store.clear().unwrap();
        store.audit().unwrap();
        for i in 0..10 {
            assert_eq!(None, store.insert(&i, &(i * 10)).unwrap());
            store.audit().unwrap();
        }
        assert_eq!(Some(50), store.peek(&5).unwrap());
        store.audit().unwrap();
        assert_eq!(Some(50), store.get(&5).unwrap());
        store.audit().unwrap();
        // END bug-fix on gets
    }

    #[test]
    fn test_push() {
        let mut store: Store<usize, usize> = Store::open_temporary(3).unwrap();
        assert_eq!(3, store.capacity());
        assert_eq!(0, store.len());
        assert_eq!(None, store.push(&1, &10).unwrap());
        store.audit().unwrap();
        assert_eq!(None, store.push(&2, &20).unwrap());
        store.audit().unwrap();
        assert_eq!(None, store.push(&3, &30).unwrap());
        store.audit().unwrap();
        assert_eq!(Some((1, 10)), store.push(&4, &40).unwrap());
        store.audit().unwrap();
        assert_eq!(Some((2, 20)), store.push(&5, &50).unwrap());
        assert_eq!(3, store.capacity());
        assert_eq!(3, store.len());
        store.audit().unwrap();
    }

    #[test]
    fn test_pop_lru() {
        let mut store: Store<usize, usize> = Store::open_temporary(10).unwrap();
        assert_eq!(10, store.capacity());
        assert_eq!(0, store.len());
        assert_eq!(None, store.insert(&1, &10).unwrap());
        assert_eq!(None, store.insert(&2, &20).unwrap());
        assert_eq!(None, store.insert(&3, &30).unwrap());
        assert_eq!(10, store.capacity());
        assert_eq!(3, store.len());
        store.audit().unwrap();
        assert_eq!(Some((1, 10)), store.pop_lru().unwrap());
        store.audit().unwrap();
        assert_eq!(Some((2, 20)), store.pop_lru().unwrap());
        store.audit().unwrap();
        assert_eq!(Some((3, 30)), store.pop_lru().unwrap());
        store.audit().unwrap();
        assert_eq!(None, store.pop_lru().unwrap());
        store.audit().unwrap();
    }

    #[test]
    fn test_pop() {
        let mut store: Store<usize, usize> = Store::open_temporary(10).unwrap();
        assert_eq!(10, store.capacity());
        assert_eq!(0, store.len());
        assert_eq!(None, store.insert(&1, &10).unwrap());
        assert_eq!(None, store.insert(&2, &20).unwrap());
        assert_eq!(None, store.insert(&3, &30).unwrap());
        assert_eq!(10, store.capacity());
        assert_eq!(3, store.len());
        store.audit().unwrap();
        assert_eq!(Some((2, 20)), store.pop(&2).unwrap());
        store.audit().unwrap();
        assert_eq!(None, store.pop(&2).unwrap());
        store.audit().unwrap();
        assert_eq!(Some(1), store.lru().unwrap());
        assert_eq!(Some(3), store.mru().unwrap());
        assert_eq!(Some((1, 10)), store.pop(&1).unwrap());
        store.audit().unwrap();
        assert_eq!(Some((3, 30)), store.pop(&3).unwrap());
        assert_eq!(0, store.len());
        store.audit().unwrap();
        assert_eq!(None, store.pop(&3).unwrap());
        store.audit().unwrap();
    }

    #[test]
    fn test_capacity_is_stored_in_db() {
        let tmp_dir = TempDir::new().unwrap();
        let file_path = tmp_dir.path().join("test.db");
        {
            let mut store: Store<usize, usize> =
                Store::open_with_path(file_path.clone(), 10).unwrap();
            assert_eq!(10, store.capacity());
            for i in 0..20 as usize {
                store.push(&i, &(i * 10)).unwrap();
            }
            store.resize(5).unwrap();
            store.resize(15).unwrap();
            store.flush().unwrap();
        }
        {
            let mut store: Store<usize, usize> =
                Store::open_with_path(file_path.clone(), 10).unwrap();
            assert_eq!(15, store.capacity());
            assert_eq!(5, store.len());
            assert_eq!(Some((15, 150)), store.pop_lru().unwrap());
            store.flush().unwrap();
        }
    }

    #[test]
    fn test_import_export() {
        let mut src: Store<usize, usize> = Store::open_temporary(10).unwrap();
        let mut dst: Store<usize, usize> = Store::open_temporary(10).unwrap();

        for i in 0..5 as usize {
            src.push(&i, &(i * 10)).unwrap();
        }
        let export = src.export();
        dst.import(export).unwrap();
        assert_eq!(&src, &dst);
    }

    #[test]
    fn test_import_map_iter() {
        let mut src: HashMap<usize, usize> = HashMap::new();
        let mut dst: Store<usize, usize> = Store::open_temporary(10).unwrap();

        for i in 0..5 as usize {
            src.insert(i, i * 10);
        }
        let export = src.iter();
        dst.import_map_iter(export).unwrap();
    }

    #[test]
    fn test_import_vec_iter() {
        let mut src: Vec<(usize, usize)> = Vec::new();
        let mut dst: Store<usize, usize> = Store::open_temporary(10).unwrap();

        for i in 0..5 as usize {
            src.push((i, i * 10));
        }
        let export = src.iter();
        dst.import_vec_iter(export).unwrap();
    }

    #[test]
    fn test_dump_restore() {
        let mut src: Store<usize, usize> = Store::open_temporary(10).unwrap();
        let mut dst: Store<usize, usize> = Store::open_temporary(10).unwrap();

        for i in 0..5 as usize {
            src.push(&i, &(i * 10)).unwrap();
            dst.push(&(i * 100), &(i * 1000)).unwrap();
        }
        dst.resize(2).unwrap();
        let dump = src.dump().unwrap();
        assert_eq!(5, dst.restore(&dump).unwrap());
        assert_eq!(src, dst);
    }

    #[test]
    fn test_to_vec() {
        let mut store: Store<usize, usize> = Store::open_temporary(3).unwrap();

        for i in 0..10 as usize {
            store.push(&i, &(i * 10)).unwrap();
        }
        let vec = store.to_vec().unwrap();
        assert_eq!(vec![(7, 70), (8, 80), (9, 90)], vec);
    }

    #[test]
    fn test_to_map() {
        let mut store: Store<usize, usize> = Store::open_temporary(3).unwrap();

        for i in 0..10 as usize {
            store.push(&i, &(i * 10)).unwrap();
        }
        let mut expected: HashMap<usize, usize> = HashMap::new();
        expected.insert(7, 70);
        expected.insert(8, 80);
        expected.insert(9, 90);
        let map = store.to_map().unwrap();
        assert_eq!(expected, map);
    }

    #[test]
    fn test_display() {
        let mut store: Store<String, String> = Store::open_temporary(10).unwrap();
        assert_eq!(10, store.capacity());
        assert_eq!(0, store.len());
        store.audit().unwrap();
        assert_eq!("[]", format!("{}", &store));
        assert_eq!(None, store.mru().unwrap());
        assert_eq!(
            None,
            store
                .insert(&"hip".to_string(), &"hop".to_string())
                .unwrap()
        );
        assert_eq!("[hip: hop]", format!("{}", &store));
        store.audit().unwrap();
    }

    #[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
    struct Coord {
        x: i64,
        y: i64,
    }

    impl fmt::Display for Coord {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(f, "({},{})", self.x, self.y)
        }
    }

    #[test]
    fn test_monkey() {
        let tmp_dir = TempDir::new().unwrap();
        let file_path = tmp_dir.path().join("monkey.db");
        let config = Config::default()
            .path(file_path)
            .temporary(false)
            .create_new(true)
            .use_compression(true)
            .compression_factor(10)
            .mode(Mode::LowSpace)
            .flush_every_ms(Some(100));

        let mut store: Store<String, Coord> = Store::open_with_config(config, 10).unwrap();
        let mut rng = rand::thread_rng();
        let mut dummy: i64 = 0;
        for i in 0..200 {
            if i % 19 == 0 {
                println!("flush");
                store.flush().unwrap();
                continue;
            }
            if i % 83 == 0 {
                println!("audit/clear");
                println!("clear");
                store.audit().unwrap();
                store.clear().unwrap();
                continue;
            }
            let dice = rng.gen_range(0..=100);
            if dice < 20 {
                let key = format!("key_{}", i % 15);
                let value = Coord {
                    x: i % 89,
                    y: i % 97,
                };
                println!("insert {:?} -> {:?}", &key, &value);
                store.insert(&key, &value).unwrap();
                continue;
            }
            if dice < 40 {
                let key = format!("key_{}", i % 15);
                let value = Coord {
                    x: i % 89,
                    y: i % 97,
                };
                println!("push {:?} -> {:?}", &key, &value);
                store.push(&key, &value).unwrap();
                continue;
            }
            if dice < 50 {
                let key = format!("key_{}", i % 15);
                println!("get {:?}", &key);
                println!("current state {}", &store);
                store.get(&key).unwrap();
                continue;
            }
            if dice < 60 {
                let key = format!("key_{}", i % 15);
                println!("peek {:?}", &key);
                store.peek(&key).unwrap();
                continue;
            }
            if dice < 70 {
                let key = format!("key_{}", i % 15);
                println!("pop {:?}", &key);
                store.pop(&key).unwrap();
                continue;
            }
            if dice < 80 {
                let key = format!("key_{}", i % 15);
                println!("remove {:?}", &key);
                store.remove(&key).unwrap();
                continue;
            }
            if dice < 81 {
                println!("dump");
                let dump = store.dump().unwrap();
                let mut other: Store<String, Coord> = Store::open_temporary(100).unwrap();
                other.restore(&dump).unwrap();
            }
            if dice < 82 {
                println!("export");
                for kv in store.export() {
                    dummy += kv.1.x * kv.1.y;
                }
            }
            if dice < 83 {
                println!("export_values");
                for v in store.export_values() {
                    dummy += v.x * v.y;
                }
            }
            if dice < 84 {
                println!("iter");
                for item in store.iter() {
                    let kv = item.unwrap();
                    dummy += kv.1.x * kv.1.y;
                }
            }
            if dice < 85 {
                println!("values");
                for item in store.values() {
                    let v = item.unwrap();
                    dummy += v.x * v.y;
                }
            }
        }
        assert_ne!(0, dummy);
    }

    #[test]
    fn test_this_is_not_a_bench() {
        let tmp_dir = TempDir::new().unwrap();
        let file_path = tmp_dir.path().join("monkey.db");
        let config = Config::default()
            .path(file_path)
            .temporary(false)
            .create_new(true)
            .use_compression(false)
            .mode(Mode::HighThroughput)
            .flush_every_ms(Some(3000));

        let mut store: Store<String, String> = Store::open_with_config(config, 100).unwrap();
        for i in 0..100 {
            store
                .insert(&format!("key{}", i % 17), &format!("value{}", i))
                .unwrap();
        }
        store.flush().unwrap();
    }
}