bitask 0.1.1

Bitask is a Rust implementation of Bitcask, a log-structured key-value store optimized for high-performance reads and writes.
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
//! A Bitcask-style key-value store implementation.
//!
//! This crate provides a simple, efficient key-value store based on the Bitcask paper.
//! It uses an append-only log structure with an in-memory index for fast lookups.
//!
//! # Features
//!
//! - Single-writer, multiple-reader architecture
//! - Process-safe file locking
//! - Automatic log file rotation
//! - CRC32 checksums for data integrity
//! - Efficient in-memory key directory
//! - Crash recovery through log replay
//!
//! # Implementation Details
//!
//! The store consists of:
//! - Active log file for current writes
//! - Immutable sealed log files
//! - In-memory key directory for O(1) lookups
//! - File-based process locking
//!
//! # Durability Guarantees
//!
//! - Atomic single-key operations
//! - Crash recovery through log replay
//! - Data integrity verification via CRC32
//!
//! # Limitations
//!
//! - All keys must fit in memory
//! - Single writer at a time
//! - No multi-key transactions

use std::{
    collections::{BTreeMap, HashMap},
    fs::{self, File, OpenOptions},
    io::{self, BufReader, BufWriter, Read, Seek, SeekFrom, Write},
    path::{Path, PathBuf},
};

use fs2::FileExt;

/// Errors that can occur during database operations.
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// Invalid log file name encountered during operations
    #[error("Invalid log file name '{filename}'")]
    InvalidLogFileName { filename: String },

    /// Failed to parse timestamp from file name or data
    #[error("Failed to parse timestamp '{value}'")]
    TimestampParse {
        value: String,
        #[source]
        source: std::num::ParseIntError,
    },

    /// Underlying IO operation failed
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    /// Attempted to open database for writing when another process has the write lock
    #[error("Only one writer allowed at a time")]
    WriterLock,

    /// Key not found in database
    #[error("Key not found")]
    KeyNotFound,

    /// Referenced file not found in database directory
    #[error("File {0} not found")]
    FileNotFound(String),

    /// Attempted to store empty value
    #[error("Value size must be greater than 0")]
    InvalidEmptyValue,

    /// Attempted to use empty key
    #[error("Key size must be greater than 0")]
    InvalidEmptyKey,

    /// System time operation failed
    #[error("Timestamp error: {0}")]
    TimestampError(#[from] std::time::SystemTimeError),

    /// Timestamp value overflow when converting to u64
    #[error("Timestamp overflow, converting to u64: {0}")]
    TimestampOverflow(#[from] std::num::TryFromIntError),

    /// No active file found when opening existing database
    #[error("Active file not found in non empty path")]
    ActiveFileNotFound,
}

/// The name of the file lock. Used to ensure only one writer at a time and process safety.
const FILE_LOCK_PATH: &str = "db.lock";

/// Maximum size of active log file before rotation (4MB)
pub const MAX_ACTIVE_FILE_SIZE: u64 = 4 * 1024 * 1024;

/// A Bitcask-style key-value store implementation.
///
/// Bitcask is an append-only log-structured storage engine that maintains an in-memory
/// index (keydir) mapping keys to their most recent value locations on disk.
///
/// # Features
/// - Single-writer, multiple-reader architecture
/// - Process-safe file locking
/// - Append-only log structure
/// - In-memory key directory
/// - Automatic log rotation at 4MB
///
/// # Thread Safety
///
/// The database ensures process-level safety through file locking, but is not
/// thread-safe internally. Concurrent access from multiple threads requires
/// appropriate synchronization mechanisms at the application level.
///
/// # Examples
///
/// Basic usage:
/// ```no_run
/// use bitask::Bitask;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut db = bitask::db::Bitask::open("my_db")?;
///
/// // Store a value
/// db.put(b"key".to_vec(), b"value".to_vec())?;
///
/// // Retrieve a value
/// let value = db.ask(b"key")?;
/// assert_eq!(value, b"value");
///
/// // Remove a value
/// db.remove(b"key".to_vec())?;
/// # Ok(())
/// # }
/// ```
///
/// # File Structure
/// - Active file: `<timestamp>.active.log` - Current file being written to
/// - Sealed files: `<timestamp>.log` - Immutable files after rotation
/// - Lock file: `db.lock` - Ensures single-writer access
///
/// # Log Rotation
/// Files are automatically rotated when they reach 4MB in size. When rotation occurs:
/// 1. Current active file is renamed from `.active.log` to `.log`
/// 2. New active file is created with current timestamp
/// 3. All existing data remains accessible
#[derive(Debug)]
pub struct Bitask {
    /// Base directory path where all database files are stored
    path: PathBuf,
    /// File lock handle to ensure single-writer access
    _file_lock: File,
    /// Timestamp identifier of the current active file
    writer_id: u64,
    /// Buffered writer for the active log file
    writer: BufWriter<File>,
    /// Map of file IDs to their respective buffered readers
    readers: HashMap<u64, BufReader<File>>,
    /// In-memory index mapping keys to their latest value locations
    keydir: BTreeMap<Vec<u8>, KeyDirEntry>,
}

/// Entry in the key directory mapping a key to its location on disk
#[derive(Debug)]
struct KeyDirEntry {
    /// File ID (timestamp) containing the value
    file_id: u64,
    /// Size of the value in bytes
    value_size: u32,
    /// Offset position of the value within the file
    value_position: u64,
    /// Timestamp when the entry was written
    timestamp: u64,
}

impl Bitask {
    /// Opens a Bitcask database at the specified path with exclusive write access.
    ///
    /// Creates a new database if one doesn't exist at the specified path.
    /// Uses file system locks to ensure only one writer exists across all processes.
    ///
    /// # Parameters
    ///
    /// * `path` - Path where the database files will be stored
    ///
    /// # Returns
    ///
    /// Returns a new [`Bitask`] instance if successful.
    ///
    /// # Errors
    ///
    /// Returns an [`Error`] if:
    /// * Another process has write access ([`Error::WriterLock`])
    /// * Filesystem operations fail ([`Error::Io`])
    /// * No active file is found when opening existing DB ([`Error::ActiveFileNotFound`])
    ///
    /// # Examples
    ///
    /// ```no_run
    /// let mut db = bitask::db::Bitask::open("my_db")?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn open(path: impl AsRef<Path>) -> Result<Self, Error> {
        fs::create_dir_all(&path)?;
        let lock_path = path.as_ref().join(FILE_LOCK_PATH);

        let lock_file = OpenOptions::new()
            .create(true)
            .read(true)
            .write(true)
            .truncate(false)
            .append(false)
            .open(lock_path)?;

        lock_file
            .try_lock_exclusive()
            .map_err(|_| Error::WriterLock)?;

        let is_empty = match fs::read_dir(&path)?.next() {
            None => true,
            Some(Ok(entry)) if entry.file_name() == FILE_LOCK_PATH => {
                // If first entry is db.lock, check if there's a second entry
                fs::read_dir(&path)?.nth(1).is_none()
            }
            Some(_) => false,
        };

        if is_empty {
            Self::open_new(path, lock_file)
        } else {
            Self::open_existing(path, lock_file)
        }
    }

    /// Creates a new database at the specified path.
    ///
    /// # Parameters
    ///
    /// * `path` - Path where the database files will be stored
    /// * `lock_file` - The exclusive lock file for this database
    ///
    /// # Returns
    ///
    /// Returns a new [`Bitask`] instance if successful.
    ///
    /// # Errors
    ///
    /// Returns an [`Error`] if:
    /// * Filesystem operations fail ([`Error::Io`])
    /// * System time operations fail ([`Error::TimestampError`])
    fn open_new(path: impl AsRef<Path>, lock_file: File) -> Result<Self, Error> {
        let timestamp = timestamp_as_u64()?;

        let writer_file = OpenOptions::new()
            .create(true)
            .read(true)
            .truncate(false)
            .append(true)
            .open(file_active_log_path(path.as_ref(), timestamp))?;

        let reader_file = OpenOptions::new()
            .create(true)
            .read(true)
            .truncate(false)
            .append(true)
            .open(file_active_log_path(path.as_ref(), timestamp))?;

        let writer = BufWriter::new(writer_file);
        let mut readers = HashMap::new();
        let reader = BufReader::new(reader_file);
        readers.insert(timestamp, reader);

        Ok(Self {
            path: path.as_ref().to_path_buf(),
            _file_lock: lock_file,
            writer_id: timestamp,
            writer,
            readers,
            keydir: BTreeMap::new(),
        })
    }

    /// Opens an existing database at the specified path.
    ///
    /// # Parameters
    ///
    /// * `path` - Path where the database files are stored
    /// * `lock_file` - The exclusive lock file for this database
    ///
    /// # Returns
    ///
    /// Returns a [`Bitask`] instance initialized with the existing database state.
    ///
    /// # Errors
    ///
    /// Returns an [`Error`] if:
    /// * Filesystem operations fail ([`Error::Io`])
    /// * Log file names are malformed ([`Error::InvalidLogFileName`])
    /// * Timestamps in filenames are invalid ([`Error::TimestampParse`])
    /// * No active log file exists ([`Error::ActiveFileNotFound`])
    fn open_existing(path: impl AsRef<Path>, lock_file: File) -> Result<Self, Error> {
        let mut active_timestamp = None;
        let mut active_file = None;
        let mut files: BTreeMap<u64, PathBuf> = BTreeMap::new();

        for entry in fs::read_dir(&path)? {
            let entry = entry?;
            let name = entry.file_name().to_string_lossy().to_string();
            if name == FILE_LOCK_PATH {
                continue;
            }

            let timestamp = name
                .split('.')
                .next()
                .ok_or_else(|| Error::InvalidLogFileName {
                    filename: name.to_string(),
                })?
                .parse()
                .map_err(|e| Error::TimestampParse {
                    value: name.to_string(),
                    source: e,
                })?;

            if name.ends_with(".active.log") {
                active_file = Some(entry.path());
                active_timestamp = Some(timestamp);
            } else if name.ends_with(".log") {
                files.insert(timestamp, entry.path());
            }
        }

        let active_timestamp = active_timestamp.ok_or(Error::ActiveFileNotFound)?;

        let writer = {
            let active_file = active_file.clone().ok_or(Error::ActiveFileNotFound)?;
            let writer_file = OpenOptions::new()
                .create(true)
                .read(true)
                .truncate(false)
                .append(true)
                .open(active_file)?;
            BufWriter::new(writer_file)
        };

        let mut reader = {
            let active_file = active_file.ok_or(Error::ActiveFileNotFound)?;

            let reader_file = OpenOptions::new()
                .create(true)
                .read(true)
                .truncate(false)
                .append(true)
                .open(active_file)?;
            BufReader::new(reader_file)
        };

        let keydir = Self::rebuild_keydir(&mut reader, active_timestamp)?;

        let mut readers = HashMap::new();
        readers.insert(active_timestamp, reader);

        Ok(Self {
            path: path.as_ref().to_path_buf(),
            _file_lock: lock_file,
            writer_id: active_timestamp,
            writer,
            readers,
            keydir,
        })
    }

    /// Rebuilds the in-memory key directory from a log file.
    ///
    /// Scans through the given log file and reconstructs the key directory by:
    /// - Reading each command header
    /// - Processing key-value entries
    /// - Updating the keydir with the latest value positions
    ///
    /// # Arguments
    ///
    /// * `reader` - Buffered reader for the log file
    /// * `file_id` - Timestamp identifier of the log file
    ///
    /// # Returns
    ///
    /// Returns a [`BTreeMap`] containing the rebuilt key directory mapping keys to their latest positions
    ///
    /// # Errors
    ///
    /// Returns an [`Error`] if:
    /// * IO operations fail while reading the file ([`Error::Io`])
    /// * Log file contains invalid or corrupted data
    fn rebuild_keydir(
        reader: &mut BufReader<File>,
        file_id: u64,
    ) -> Result<BTreeMap<Vec<u8>, KeyDirEntry>, Error> {
        let mut keydir: BTreeMap<Vec<u8>, KeyDirEntry> = BTreeMap::new();
        let mut position = 0u64;

        loop {
            // Read just the header
            let mut header_buf = vec![0u8; CommandHeader::SIZE];
            match reader.read_exact(&mut header_buf) {
                Ok(_) => (),
                Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => break,
                Err(e) => return Err(e.into()),
            }

            let header = CommandHeader::deserialize(&header_buf)?;

            // Read just the key
            let mut key = vec![0u8; header.key_len as usize];
            reader.read_exact(&mut key)?;

            // Skip the value bytes
            reader.seek(SeekFrom::Current(header.value_size as i64))?;

            if header.value_size == 0 {
                // Remove command
                keydir.remove(&key);
            } else {
                // Set command
                match keydir.get(&key) {
                    Some(existing) if existing.timestamp >= header.timestamp => {
                        // Skip older or same-age entries
                        continue;
                    }
                    _ => {
                        let value_position =
                            position + CommandHeader::SIZE as u64 + header.key_len as u64;
                        keydir.insert(
                            key,
                            KeyDirEntry {
                                file_id,
                                value_size: header.value_size,
                                value_position,
                                timestamp: header.timestamp,
                            },
                        );
                    }
                }
            }

            position +=
                CommandHeader::SIZE as u64 + header.key_len as u64 + header.value_size as u64;
        }
        Ok(keydir)
    }

    /// Rotates the active log file when it reaches the size limit.
    ///
    /// This process:
    /// 1. Renames the current active file to a sealed log file
    /// 2. Creates a new active file with current timestamp
    /// 3. Updates internal writer and reader references
    ///
    /// # Returns
    ///
    /// Returns `()` if rotation was successful.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// * File operations fail (`Error::Io`)
    /// * System time operations fail (`Error::TimestampError`)
    /// * IO operations fail (`Error::Io`)
    fn rotate_active_file(&mut self) -> Result<(), Error> {
        let timestamp = timestamp_as_u64()?;

        // Rename current active file to regular log file
        let old_path = file_active_log_path(&self.path, self.writer_id);
        let new_path = file_log_path(&self.path, self.writer_id);
        fs::rename(old_path, new_path)?;

        // Create new active file
        let writer_file = OpenOptions::new()
            .create(true)
            .read(true)
            .append(true)
            .open(file_active_log_path(&self.path, timestamp))?;

        let reader_file = OpenOptions::new()
            .create(true)
            .read(true)
            .append(true)
            .open(file_active_log_path(&self.path, timestamp))?;

        // Update writer and readers
        self.writer = BufWriter::new(writer_file);
        self.readers.insert(timestamp, BufReader::new(reader_file));
        self.writer_id = timestamp;

        Ok(())
    }

    /// Retrieves the value associated with the given key.
    ///
    /// Performs an O(1) lookup in the in-memory index followed by a single disk read.
    ///
    /// # Parameters
    ///
    /// * `key` - The key to look up
    ///
    /// # Returns
    ///
    /// Returns the value as a [`Vec<u8>`] if the key exists.
    ///
    /// # Errors
    ///
    /// Returns an [`Error`] if:
    /// * The key is empty ([`Error::InvalidEmptyKey`])
    /// * The key doesn't exist ([`Error::KeyNotFound`])
    /// * The data file is missing ([`Error::FileNotFound`])
    /// * IO operations fail ([`Error::Io`])
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # let mut db = bitask::db::Bitask::open("my_db")?;
    /// if let Ok(value) = db.ask(b"my_key") {
    ///     println!("Found value: {:?}", value);
    /// }
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn ask(&mut self, key: &[u8]) -> Result<Vec<u8>, Error> {
        if key.is_empty() {
            return Err(Error::InvalidEmptyKey);
        }

        if let Some(entry) = self.keydir.get(key) {
            if let std::collections::hash_map::Entry::Vacant(e) = self.readers.entry(entry.file_id)
            {
                let file = OpenOptions::new()
                    .read(true)
                    .open(file_log_path(&self.path, entry.file_id))?;
                e.insert(BufReader::new(file));
            }

            let reader = self
                .readers
                .get_mut(&entry.file_id)
                .ok_or(Error::FileNotFound(format!("{}", entry.file_id)))?;

            reader.seek(SeekFrom::Start(entry.value_position))?;
            let mut value = vec![0; entry.value_size as usize]; // Initialize with zeros
            reader.read_exact(&mut value)?;
            return Ok(value);
        }

        Err(Error::KeyNotFound)
    }

    /// Stores a key-value pair in the database.
    ///
    /// If the key already exists, it will be updated with the new value.
    /// The operation is atomic and durable (synced to disk).
    ///
    /// Performance: Requires one disk write (append-only) and one in-memory index update.
    /// May trigger file rotation if the active file exceeds size limit (4MB).
    ///
    /// # Parameters
    ///
    /// * `key` - The key to store
    /// * `value` - The value to associate with the key
    ///
    /// # Returns
    ///
    /// Returns `()` if the operation was successful.
    ///
    /// # Errors
    ///
    /// Returns an [`Error`] if:
    /// * The key is empty ([`Error::InvalidEmptyKey`])
    /// * The value is empty ([`Error::InvalidEmptyValue`])
    /// * IO operations fail ([`Error::Io`])
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # let mut db = bitask::db::Bitask::open("my_db")?;
    /// db.put(b"my_key".to_vec(), b"my_value".to_vec())?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn put(&mut self, key: Vec<u8>, value: Vec<u8>) -> Result<(), Error> {
        if key.is_empty() {
            return Err(Error::InvalidEmptyKey);
        }

        if value.is_empty() {
            return Err(Error::InvalidEmptyValue);
        }

        let file_size = self.writer.get_ref().metadata()?.len();
        if file_size > MAX_ACTIVE_FILE_SIZE {
            log::debug!("File size {} exceeded limit, rotating", file_size);
            self.rotate_active_file()?;

            if false {
                log::debug!("Auto-compaction is enabled, checking file count");
                // Count immutable files and trigger compaction if too many
                let immutable_files = std::fs::read_dir(&self.path)?
                    .filter_map(Result::ok)
                    .filter(|entry| {
                        let name = entry.file_name().to_string_lossy().to_string();
                        name.ends_with(".log") && !name.ends_with(".active.log")
                    })
                    .count();

                log::debug!("Found {} immutable files", immutable_files);
                if immutable_files >= 2 {
                    log::debug!(
                        "Auto-triggering compaction with {} immutable files",
                        immutable_files
                    );
                    self.compact()?;
                }
            } else {
                log::debug!("Auto-compaction is disabled");
            }
        }

        let command = CommandSet::new(key.clone(), value.clone())?;
        let mut buffer = Vec::new();
        command.serialize(&mut buffer)?;

        // TODO optimize writer to store last offset/position
        let position = self.writer.seek(SeekFrom::End(0))?;
        self.writer.write_all(&buffer)?;
        self.writer.flush()?;

        let value_position = position + CommandHeader::SIZE as u64 + key.len() as u64;
        self.keydir.insert(
            key,
            KeyDirEntry {
                file_id: self.writer_id,
                value_size: value.len() as u32,
                value_position,
                timestamp: command.timestamp,
            },
        );
        Ok(())
    }

    /// Removes a key-value pair from the database.
    ///
    /// The operation is atomic and durable. Even if the key doesn't exist,
    /// a tombstone entry is written to ensure the removal is persisted.
    ///
    /// Performance: Requires one disk write (append-only) and one in-memory index update.
    ///
    /// # Parameters
    ///
    /// * `key` - The key to remove
    ///
    /// # Returns
    ///
    /// Returns `()` if the operation was successful.
    ///
    /// # Errors
    ///
    /// Returns an [`Error`] if:
    /// * The key is empty ([`Error::InvalidEmptyKey`])
    /// * IO operations fail ([`Error::Io`])
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # let mut db = bitask::db::Bitask::open("my_db")?;
    /// db.remove(b"my_key".to_vec())?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn remove(&mut self, key: Vec<u8>) -> Result<(), Error> {
        if key.is_empty() {
            return Err(Error::InvalidEmptyKey);
        }

        let command = CommandRemove::new(key.clone())?;
        let mut buffer = Vec::new();
        command.serialize(&mut buffer)?;

        self.writer.write_all(&buffer)?;
        self.writer.flush()?;

        self.keydir.remove(&key);
        Ok(())
    }

    /// Compacts the database by removing obsolete entries and merging files.
    ///
    /// This process:
    /// 1. Identifies immutable files (not including active file)
    /// 2. Creates a new compacted file with only latest entries
    /// 3. Removes old files after successful compaction
    ///
    /// Performance: Requires reading all immutable files and writing live entries
    /// to a new file. Memory usage remains constant as entries are processed
    /// sequentially.
    ///
    /// # Returns
    ///
    /// Returns `()` if compaction was successful.
    ///
    /// # Errors
    ///
    /// Returns an [`Error`] if:
    /// * IO operations fail ([`Error::Io`])
    /// * File operations fail ([`Error::FileNotFound`])
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # let mut db = bitask::db::Bitask::open("my_db")?;
    /// // After many operations, compact to reclaim space
    /// db.compact()?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn compact(&mut self) -> Result<(), Error> {
        // Create new file for compaction
        let timestamp = timestamp_as_u64()?;
        let mut compaction_writer = BufWriter::new(
            OpenOptions::new()
                .create(true)
                .write(true)
                .truncate(true)
                .open(file_log_path(&self.path, timestamp))?,
        );

        let mut new_pos = 0;
        // Copy live entries
        for (key, entry) in self.keydir.iter_mut() {
            // Skip entries in active file
            if entry.file_id == self.writer_id {
                continue;
            }

            // Open reader at the start of the entry (header position)
            let mut reader = BufReader::new(File::open(file_log_path(&self.path, entry.file_id))?);
            let header_pos = entry.value_position - key.len() as u64 - CommandHeader::SIZE as u64;
            reader.seek(SeekFrom::Start(header_pos))?;

            // Copy the entire entry (header + key + value)
            let entry_size =
                CommandHeader::SIZE as u64 + key.len() as u64 + entry.value_size as u64;
            io::copy(&mut reader.take(entry_size), &mut compaction_writer)?;

            // Update position
            entry.file_id = timestamp;
            entry.value_position = new_pos + CommandHeader::SIZE as u64 + key.len() as u64;
            new_pos += entry_size;
        }

        compaction_writer.flush()?;

        // Remove old files
        for file in std::fs::read_dir(&self.path)? {
            let file = file?;
            let name = file.file_name().to_string_lossy().to_string();
            if name.ends_with(".log")
                && !name.ends_with(".active.log")
                && !name.starts_with(&timestamp.to_string())
            {
                std::fs::remove_file(file.path())?;
            }
        }

        Ok(())
    }
}

/// Header structure for commands stored in the log files.
/// Contains metadata about the stored key-value pairs.
#[derive(Debug)]
struct CommandHeader {
    /// CRC32 checksum of the key and value
    crc: u32,
    /// Timestamp when the command was written
    timestamp: u64,
    /// Length of the key in bytes
    key_len: u32,
    /// Size of the value in bytes (0 for remove commands)
    value_size: u32,
}

impl CommandHeader {
    /// Size of the header in bytes, computed from its field types.
    const SIZE: usize = std::mem::size_of::<u32>()
        + std::mem::size_of::<u64>()
        + std::mem::size_of::<u32>()
        + std::mem::size_of::<u32>();

    /// Creates a new command header with the specified metadata.
    ///
    /// # Arguments
    ///
    /// * `crc` - CRC32 checksum of the key and value data
    /// * `timestamp` - Timestamp when the command was created (milliseconds since UNIX epoch)
    /// * `key_len` - Length of the key in bytes
    /// * `value_len` - Length of the value in bytes (0 for remove commands)
    ///
    /// # Returns
    ///
    /// Returns a new [`CommandHeader`] initialized with the provided values
    fn new(crc: u32, timestamp: u64, key_len: u32, value_len: u32) -> Self {
        Self {
            crc,
            timestamp,
            key_len,
            value_size: value_len,
        }
    }

    /// Serializes the header into a byte buffer.
    ///
    /// The header is written in little-endian byte order with the following layout:
    /// - CRC32 (4 bytes)
    /// - Timestamp (8 bytes)
    /// - Key length (4 bytes)
    /// - Value size (4 bytes)
    ///
    /// # Arguments
    ///
    /// * `buffer` - The vector to write the serialized header to
    ///
    /// # Errors
    ///
    /// Returns [`Error::Io`] if writing to the buffer fails
    fn serialize(&self, buffer: &mut Vec<u8>) -> Result<(), Error> {
        buffer.write_all(&self.crc.to_le_bytes())?;
        buffer.write_all(&self.timestamp.to_le_bytes())?;
        buffer.write_all(&self.key_len.to_le_bytes())?;
        buffer.write_all(&self.value_size.to_le_bytes())?;
        Ok(())
    }

    /// Deserializes a header from a byte buffer.
    ///
    /// # Arguments
    ///
    /// * `buf` - Buffer containing the serialized header data (must be at least [`Self::SIZE`] bytes)
    ///
    /// # Errors
    ///
    /// Returns [`Error::Io`] if:
    /// * The buffer is smaller than [`Self::SIZE`] bytes
    /// * The buffer contains invalid data that can't be converted to header fields
    ///
    /// # Panics
    ///
    /// Will not panic as buffer size is checked before conversion
    fn deserialize(buf: &[u8]) -> Result<Self, Error> {
        if buf.len() < Self::SIZE {
            return Err(Error::Io(std::io::Error::new(
                std::io::ErrorKind::UnexpectedEof,
                "buffer too small for header",
            )));
        }

        Ok(Self {
            crc: u32::from_le_bytes(buf[0..4].try_into().unwrap()),
            timestamp: u64::from_le_bytes(buf[4..12].try_into().unwrap()),
            key_len: u32::from_le_bytes(buf[12..16].try_into().unwrap()),
            value_size: u32::from_le_bytes(buf[16..20].try_into().unwrap()),
        })
    }
}

/// A command to append a key-value pair to the log.
#[derive(Debug)]
struct CommandSet {
    /// CRC32 checksum of key and value
    crc: u32,
    /// Timestamp when command was created (milliseconds since UNIX epoch)
    timestamp: u64,
    /// Key to be stored as [`Vec<u8>`]
    key: Vec<u8>,
    /// Value to be associated with the key as [`Vec<u8>`]
    value: Vec<u8>,
}

/// A command to remove a key from the database.
#[derive(Debug)]
struct CommandRemove {
    /// CRC32 checksum of key
    crc: u32,
    /// Timestamp when command was created (milliseconds since UNIX epoch)
    timestamp: u64,
    /// Key to be removed as [`Vec<u8>`]
    key: Vec<u8>,
}

impl CommandSet {
    /// Creates a new set command.
    ///
    /// Generates a CRC32 checksum of the key-value pair and includes current timestamp.
    ///
    /// # Arguments
    ///
    /// * `key` - The key to store as [`Vec<u8>`]
    /// * `value` - The value to associate with the key as [`Vec<u8>`]
    ///
    /// # Returns
    ///
    /// Returns a new [`CommandSet`] if successful.
    ///
    /// # Errors
    ///
    /// Returns an [`Error`] if:
    /// * System time operations fail ([`Error::TimestampError`])
    /// * Timestamp conversion fails ([`Error::TimestampOverflow`])
    pub fn new(key: Vec<u8>, value: Vec<u8>) -> Result<Self, Error> {
        let timestamp = timestamp_as_u64()?;

        let mut hasher = crc32fast::Hasher::new();
        hasher.update(key.as_slice());
        hasher.update(value.as_slice());
        let crc = hasher.finalize();

        Ok(Self {
            crc,
            timestamp,
            key,
            value,
        })
    }

    /// Serializes the command into a byte array.
    ///
    /// Format:
    /// 1. Command header (CRC, timestamp, key length, value length)
    /// 2. Key bytes
    /// 3. Value bytes
    ///
    /// # Arguments
    ///
    /// * `buffer` - The buffer to write the serialized command to as [`Vec<u8>`]
    ///
    /// # Errors
    ///
    /// Returns an [`Error::Io`] if IO operations fail
    fn serialize(&self, buffer: &mut Vec<u8>) -> Result<(), Error> {
        CommandHeader::new(
            self.crc,
            self.timestamp,
            self.key.len() as u32,
            self.value.len() as u32,
        )
        .serialize(buffer)?;
        buffer.write_all(&self.key)?;
        buffer.write_all(&self.value)?;
        Ok(())
    }
}

impl CommandRemove {
    /// Creates a new remove command.
    ///
    /// Generates a CRC32 checksum of the key and includes current timestamp.
    ///
    /// # Arguments
    ///
    /// * `key` - The key to remove as [`Vec<u8>`]
    ///
    /// # Returns
    ///
    /// Returns a new [`CommandRemove`] if successful.
    ///
    /// # Errors
    ///
    /// Returns an [`Error`] if:
    /// * System time operations fail ([`Error::TimestampError`])
    /// * Timestamp conversion fails ([`Error::TimestampOverflow`])
    pub fn new(key: Vec<u8>) -> Result<Self, Error> {
        let timestamp = timestamp_as_u64()?;

        let mut hasher = crc32fast::Hasher::new();
        hasher.update(key.as_slice());
        let crc = hasher.finalize();

        Ok(Self {
            crc,
            timestamp,
            key,
        })
    }

    /// Serializes the command into a byte array.
    ///
    /// Format:
    /// 1. Command header (CRC, timestamp, key length, value length = 0)
    /// 2. Key bytes
    ///
    /// # Arguments
    ///
    /// * `buffer` - The buffer to write the serialized command to as [`Vec<u8>`]
    ///
    /// # Errors
    ///
    /// Returns an [`Error::Io`] if IO operations fail
    fn serialize(&self, buffer: &mut Vec<u8>) -> Result<(), Error> {
        CommandHeader::new(self.crc, self.timestamp, self.key.len() as u32, 0).serialize(buffer)?;
        buffer.write_all(&self.key)?;
        Ok(())
    }
}

/// Constructs the path for an active log file.
///
/// # Arguments
///
/// * `path` - Base directory path
/// * `timestamp` - Timestamp used as file identifier
///
/// # Returns
///
/// Returns a [`PathBuf`] containing the full path to the active log file in format:
/// `<path>/<timestamp>.active.log`
fn file_active_log_path(path: impl AsRef<Path>, timestamp: u64) -> PathBuf {
    path.as_ref().join(format!("{}.active.log", timestamp))
}

/// Constructs the path for a regular (sealed) log file.
///
/// # Arguments
///
/// * `path` - Base directory path
/// * `timestamp` - Timestamp used as file identifier
///
/// # Returns
///
/// Returns a [`PathBuf`] containing the full path to the log file in format:
/// `<path>/<timestamp>.log`
fn file_log_path(path: impl AsRef<Path>, timestamp: u64) -> PathBuf {
    path.as_ref().join(format!("{}.log", timestamp))
}

/// Gets current timestamp as milliseconds since UNIX epoch.
///
/// # Returns
///
/// Returns the current time in milliseconds since UNIX epoch as [`u64`]
///
/// # Errors
///
/// Returns an [`Error`] if:
/// * System time operations fail ([`Error::TimestampError`])
/// * Milliseconds value doesn't fit in [`u64`] ([`Error::TimestampOverflow`])
fn timestamp_as_u64() -> Result<u64, Error> {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_err(Error::TimestampError)?
        .as_millis()
        .try_into()
        .map_err(Error::TimestampOverflow)
}

impl Drop for Bitask {
    /// Cleans up resources when the database is dropped.
    ///
    /// Removes the physical lock file from the filesystem to allow
    /// future database instances to acquire the write lock.
    fn drop(&mut self) {
        // Remove the physical lock file from the filesystem
        if let Ok(path) = self.path.join("db.lock").canonicalize() {
            let _ = std::fs::remove_file(path);
        }
    }
}

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

    #[test]
    fn test_set_command_serialization() {
        let key = b"key".to_vec();
        let value = b"value".to_vec();
        let command = CommandSet::new(key.clone(), value.clone()).unwrap();

        let mut buffer = Vec::new();
        command.serialize(&mut buffer).unwrap();

        // Check header structure
        let header = CommandHeader::deserialize(&buffer[..CommandHeader::SIZE]).unwrap();
        assert_eq!(header.key_len, key.len() as u32);
        assert_eq!(header.value_size, value.len() as u32);

        // Check key and value bytes
        assert_eq!(
            &buffer[CommandHeader::SIZE..CommandHeader::SIZE + key.len()],
            key
        );
        assert_eq!(&buffer[CommandHeader::SIZE + key.len()..], value);

        // Verify CRC
        let mut hasher = crc32fast::Hasher::new();
        hasher.update(&key);
        hasher.update(&value);
        assert_eq!(header.crc, hasher.finalize());
    }

    #[test]
    fn test_remove_command_serialization() {
        let key = b"key".to_vec();
        let command = CommandRemove::new(key.clone()).unwrap();

        let mut buffer = Vec::new();
        command.serialize(&mut buffer).unwrap();

        // Check header structure
        let header = CommandHeader::deserialize(&buffer[..CommandHeader::SIZE]).unwrap();
        assert_eq!(header.key_len, key.len() as u32);
        assert_eq!(header.value_size, 0);

        // Check key bytes
        assert_eq!(&buffer[CommandHeader::SIZE..], key);

        // Verify CRC
        let mut hasher = crc32fast::Hasher::new();
        hasher.update(&key);
        assert_eq!(header.crc, hasher.finalize());
    }

    #[test]
    fn test_automatic_compaction_disabled() {
        // Create test directory
        let dir = tempfile::tempdir().unwrap();
        let mut db = Bitask::open(dir.path()).unwrap();

        // Insert enough data to trigger multiple rotations
        for i in 0..3000 {
            let key = format!("key{}", i).into_bytes();
            let value = vec![0; 8 * 1024]; // 8KB value to fill files quickly
            db.put(key, value).unwrap();
        }

        // Count immutable log files - should be MORE than 2 since auto-compaction is disabled
        let log_files = std::fs::read_dir(dir.path())
            .unwrap()
            .filter_map(Result::ok)
            .filter(|entry| {
                let name = entry.file_name().to_string_lossy().to_string();
                name.ends_with(".log") && !name.ends_with(".active.log")
            })
            .count();

        assert!(
            log_files >= 2,
            "Expected 2 or more log files since auto-compaction is disabled"
        );
    }
}