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
use std::path::Path;

use file_mmap::FileMmap;

mod fragment;
use fragment::Fragment;

#[derive(Clone, Copy, PartialEq, Debug)]
pub struct DataAddress {
    offset: i64,
    len: u64,
}

impl DataAddress {
    #[inline(always)]
    pub fn offset(&self) -> i64 {
        self.offset
    }

    #[inline(always)]
    pub fn len(&self) -> u64 {
        self.len
    }
}
pub struct Data<'a> {
    address: DataAddress,
    data: &'a VariousDataFile,
}

impl Data<'_> {
    #[inline(always)]
    pub fn bytes(&self) -> &[u8] {
        unsafe { self.data.bytes(&self.address) }
    }

    #[inline(always)]
    pub fn address(&self) -> &DataAddress {
        &self.address
    }
}

pub struct VariousDataFile {
    filemmap: FileMmap,
    fragment: Fragment,
}

impl VariousDataFile {
    pub fn new<P: AsRef<Path>>(path: P) -> Self {
        let path = path.as_ref();
        let mut filemmap = FileMmap::new(path).unwrap();
        if filemmap.len() == 0 {
            filemmap.set_len(1).unwrap();
        }
        VariousDataFile {
            filemmap,
            fragment: {
                let mut path = path.to_path_buf();
                path.set_file_name(
                    &(path
                        .file_name()
                        .map_or("".into(), |v| v.to_string_lossy())
                        .into_owned()
                        + ".f"),
                );
                Fragment::new(path)
            },
        }
    }

    #[inline(always)]
    pub unsafe fn bytes(&self, word: &DataAddress) -> &'static [u8] {
        self.filemmap
            .bytes(word.offset() as isize, word.len as usize)
    }

    #[inline(always)]
    pub fn insert(&mut self, target: &[u8]) -> Data {
        let len = target.len();
        Data {
            address: DataAddress {
                offset: match self.fragment.search_blank(len) {
                    Some(r) => {
                        self.filemmap.write(r.addr as isize, target).unwrap();
                        unsafe {
                            self.fragment.release(r.fragment_id, len);
                        }
                        r.addr as i64
                    }
                    None => self.filemmap.append(target).unwrap() as i64,
                },
                len: len as u64,
            },
            data: self,
        }
    }

    #[inline(always)]
    pub fn delete(&mut self, addr: &DataAddress) {
        self.filemmap
            .write(addr.offset as isize, &vec![0; addr.len as usize])
            .unwrap();
        self.fragment.insert(addr);
    }
}

#[test]
fn test() {
    let dir = "./vdf-test";
    if std::path::Path::new(dir).exists() {
        std::fs::remove_dir_all(dir).unwrap();
        std::fs::create_dir_all(dir).unwrap();
    } else {
        std::fs::create_dir_all(dir).unwrap();
    }
    let mut s = VariousDataFile::new(&(dir.to_owned() + "/test.str"));

    let noah = s.insert(b"Noah").address;
    let liam = s.insert(b"Liam").address;
    let olivia = s.insert(b"Olivia").address;

    s.delete(&noah);

    assert_eq!(
        "Renamed Noah".to_string(),
        std::str::from_utf8(s.insert(b"Renamed Noah").bytes())
            .unwrap()
            .to_string()
    );
    s.delete(&liam);
    assert_eq!(
        "Renamed Liam".to_string(),
        std::str::from_utf8(s.insert(b"Renamed Liam").bytes())
            .unwrap()
            .to_string()
    );
    s.delete(&olivia);
    assert_eq!(
        "Renamed Olivia".to_string(),
        std::str::from_utf8(s.insert(b"Renamed Olivia").bytes())
            .unwrap()
            .to_string()
    );
    assert_eq!(
        "Noah".to_string(),
        std::str::from_utf8(s.insert(b"Noah").bytes())
            .unwrap()
            .to_string()
    );
    assert_eq!(
        "Liam".to_string(),
        std::str::from_utf8(s.insert(b"Liam").bytes())
            .unwrap()
            .to_string()
    );
    assert_eq!(
        "Olivia".to_string(),
        std::str::from_utf8(s.insert(b"Olivia").bytes())
            .unwrap()
            .to_string()
    );
}