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
//! Fail-safe file sequence
//!
//! Works by versioning values of sequences and throwing away all versions,
//! but the current and the previous one.
//!
//! Inspired by [this Java implementation](https://commons.apache.org/proper/commons-transaction/apidocs/org/apache/commons/transaction/file/FileSequence.html)
//!
//! # Usage
//!
//! ```
//! use file_seq::FileSeq;
//! use std::path::Path;
//!
//! let dir = Path::new("/tmp/example");
//! let initial_value = 1;
//!
//! let seq = FileSeq::new(&dir, initial_value).unwrap();
//!
//! // Get current value
//! assert_eq!(initial_value, seq.value().unwrap());
//!
//! // Increment and get
//! assert_eq!(initial_value + 1, seq.increment_and_get(1).unwrap());
//!
//! // Get and then increment
//! assert_eq!(initial_value + 1, seq.get_and_increment(1).unwrap());
//! assert_eq!(initial_value + 2, seq.value().unwrap());
//! ```

use std::fs;
use std::io::{Error, ErrorKind, Read};
use std::path::{Path, PathBuf};

use log::warn;

#[derive(Debug)]
pub struct FileSeq {
    path_1: PathBuf,
    path_2: PathBuf,
}

impl FileSeq {
    pub fn new<P: AsRef<Path>>(store_dir: P, initial_value: u64) -> std::io::Result<Self> {
        let store_path = store_dir.as_ref();

        fs::create_dir_all(store_path)?;
        let store_path_buf = store_path.to_path_buf();
        let path_1 = store_path_buf.join("_1.seq");
        let path_2 = store_path_buf.join("_2.seq");

        let seq = Self { path_1, path_2 };

        seq.initialize_if_necessary(initial_value)?;

        Ok(seq)
    }

    fn initialize_if_necessary(&self, initial_value: u64) -> std::io::Result<()> {
        if fs::metadata(&self.path_1).is_ok() || fs::metadata(&self.path_2).is_ok() {
            Ok(())
        } else {
            self.write(initial_value)
        }
    }

    /// Deletes this sequence
    ///
    /// Once deleted, the sequence must be recreated
    ///
    /// # Example
    ///
    /// ```
    /// use file_seq::FileSeq;
    /// use std::path::Path;
    ///
    /// let dir = Path::new("/tmp/example_delete");
    /// let initial_value = 1;
    ///
    /// let seq = FileSeq::new(&dir, initial_value).unwrap();
    ///
    /// // Get current value
    /// assert_eq!(initial_value, seq.value().unwrap());
    ///
    /// seq.delete();
    ///
    /// // Attempts to read the sequence after it's deleted returns an error
    /// assert_eq!(seq.value().is_err(), true)
    /// ```
    pub fn delete(&self) -> () {
        // The files might not exist already
        let _ = fs::remove_file(&self.path_1);
        let _ = fs::remove_file(&self.path_2);
    }

    /// Returns the current value of the sequence and then increments it.
    ///
    /// # Example
    ///
    /// ```
    /// use file_seq::FileSeq;
    /// use std::path::Path;
    ///
    /// let dir = Path::new("/tmp/example_get_and_increment");
    /// let initial_value = 1;
    ///
    /// let seq = FileSeq::new(&dir, initial_value).unwrap();
    ///
    /// assert_eq!(initial_value, seq.get_and_increment(1).unwrap());
    /// assert_eq!(initial_value + 1, seq.value().unwrap());
    ///
    /// ```
    pub fn get_and_increment(&self, increment: u64) -> std::io::Result<u64> {
        let value = self.read()?;
        self.write(value + increment)?;
        Ok(value)
    }

    /// Increments the sequence and return the value.
    ///
    /// # Example
    ///
    /// ```
    /// use file_seq::FileSeq;
    /// use std::path::Path;
    ///
    /// let dir = Path::new("/tmp/example_increment_and_get");
    /// let initial_value = 1;
    ///
    /// let seq = FileSeq::new(&dir, initial_value).unwrap();
    ///
    /// assert_eq!(initial_value + 1, seq.increment_and_get(1).unwrap());
    /// assert_eq!(initial_value + 1, seq.value().unwrap());
    ///
    /// ```
    pub fn increment_and_get(&self, increment: u64) -> std::io::Result<u64> {
        let value = self.get_and_increment(increment)?;
        Ok(value + increment)
    }

    /// Returns the current value of the sequence.
    ///
    /// # Example
    ///
    /// ```
    /// use file_seq::FileSeq;
    /// use std::path::Path;
    ///
    /// let dir = Path::new("/tmp/example_value");
    /// let initial_value = 1;
    ///
    /// let seq = FileSeq::new(&dir, initial_value).unwrap();
    ///
    /// assert_eq!(initial_value, seq.value().unwrap());
    ///
    /// ```
    pub fn value(&self) -> std::io::Result<u64> {
        self.read()
    }

    fn read(&self) -> std::io::Result<u64> {
        let mut value1: Option<u64> = None;
        if fs::metadata(&self.path_1).is_ok() {
            let value = self.read_from_path(&self.path_1)?;
            value1 = Some(value);
        }

        let mut value2: Option<u64> = None;
        if fs::metadata(&self.path_2).is_ok() {
            value2 = self.read_from_path(&self.path_2).ok();
        }

        match value2 {
            Some(v2) => match value1 {
                Some(v1) => {
                    if v2 > v1 {
                        Ok(v2)
                    } else {
                        warn!("Latest sequence value is smaller than backup, using backup.");
                        fs::remove_file(&self.path_2).ok();
                        Ok(v1)
                    }
                }
                None => Ok(v2),
            },
            None => {
                fs::remove_file(&self.path_2).ok();

                match value1 {
                    Some(v1) => Ok(v1),
                    None => Err(Error::new(
                        ErrorKind::InvalidData,
                        "Looks like both backup and latest sequence files are corrupted.",
                    )),
                }
            }
        }
    }

    fn read_from_path<P: AsRef<Path>>(&self, path: P) -> std::io::Result<u64> {
        let mut buff = [0; 8];
        let mut f = fs::File::open(path.as_ref())?;
        f.read_exact(&mut buff)?;
        let value = u64::from_be_bytes(buff);
        Ok(value)
    }

    fn write(&self, value: u64) -> std::io::Result<()> {
        if fs::metadata(&self.path_2).is_ok() {
            fs::rename(&self.path_2, &self.path_1)?;
        }
        self.write_to_path(&self.path_2, value)
    }

    fn write_to_path<P: AsRef<Path>>(&self, path: P, value: u64) -> std::io::Result<()> {
        fs::write(path.as_ref(), value.to_be_bytes())
    }
}

#[cfg(test)]
mod tests {
    use std::env;
    use std::fs;
    use std::path::PathBuf;

    use rand::RngCore;

    use crate::FileSeq;

    pub fn tmpdir() -> PathBuf {
        let p = env::temp_dir();
        let mut r = rand::thread_rng();
        let ret = p.join(&format!("file-seq-{}", r.next_u32()));
        fs::create_dir(&ret).unwrap();
        ret
    }

    #[test]
    fn should_store_initial_seq_correctly() {
        let dir = tmpdir();
        let seq = FileSeq::new(&dir, 1).unwrap();
        assert!(std::fs::metadata(dir).is_ok());
        assert!(std::fs::metadata(seq.path_2).is_ok());
    }

    #[test]
    fn should_cycle_seq_files() {
        let dir = tmpdir();
        let seq = FileSeq::new(&dir, 1).unwrap();
        assert!(std::fs::metadata(dir).is_ok());
        assert!(std::fs::metadata(&seq.path_2).is_ok());
        let path_2_value = std::fs::read(&seq.path_2).unwrap();
        seq.increment_and_get(1).unwrap();
        let path_1_value = std::fs::read(&seq.path_1).unwrap();
        assert_eq!(path_2_value, path_1_value);
    }

    #[test]
    fn should_delete() {
        let dir = tmpdir();
        let seq = FileSeq::new(&dir, 1).unwrap();
        assert!(std::fs::metadata(dir).is_ok());
        assert!(std::fs::metadata(&seq.path_2).is_ok());
        seq.increment_and_get(1).unwrap();
        seq.delete();
        assert!(!std::fs::metadata(&seq.path_1).is_ok());
        assert!(!std::fs::metadata(&seq.path_2).is_ok());
    }

    #[test]
    fn should_increment_and_get() {
        let dir = tmpdir();
        let seq = FileSeq::new(dir, 1).unwrap();
        let prev_value = seq.value().unwrap();
        let curr_value = seq.increment_and_get(1).unwrap();
        assert_eq!(prev_value + 1, curr_value);
        assert_eq!(curr_value, seq.value().unwrap());
    }

    #[test]
    fn should_get_and_increment() {
        let dir = tmpdir();
        let seq = FileSeq::new(dir, 1).unwrap();
        let prev_value = seq.value().unwrap();
        let curr_value = seq.get_and_increment(1).unwrap();
        assert_eq!(prev_value, curr_value);
        assert_eq!(curr_value + 1, seq.value().unwrap())
    }
}