depot 0.2.0

A (disk) persistent queue library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
use section::{SectionItem, SectionReader, SectionWriter};
use std::ffi::OsStr;
use std::fs;
use std::fs::DirEntry;
use std::io;
use std::path::{Path, PathBuf};

const MAX_COMPONENT_VALUE: u16 = 1000;

const MAX_COMPONENT_ENCODED_VALUE: u32 = 1_999_999_999;

#[derive(Debug, PartialEq)]
pub struct Component {
    one: u16,
    two: u16,
    three: u16,
    four: u16,
}

/// A Component represents a path to a file on disk. It is
/// split into four components that count from 0 to 999.
///
/// A component can always be represented as a 32bit integer,
/// and its maximum value is 1,999,999,999.
///
/// Example: (0, 1, 2, 3) maps to the file <base>/0/1/2/3
impl Component {
    fn decode(encoded: u32) -> io::Result<Component> {
        let v = MAX_COMPONENT_VALUE as u32;

        if encoded <= MAX_COMPONENT_ENCODED_VALUE {
            let one = encoded / (v * v * v);
            let two = (encoded % (v * v * v)) / (v * v);
            let three = (encoded % (v * v)) / v;
            let four = encoded % v;

            Ok(Component {
                one: one as u16,
                two: two as u16,
                three: three as u16,
                four: four as u16,
            })
        } else {
            Err(io::Error::new(
                io::ErrorKind::Other,
                "encoded component exceeds maximum value",
            ))
        }
    }

    fn new() -> Component {
        Component {
            one: 0,
            two: 0,
            three: 0,
            four: 0,
        }
    }

    fn from(one: u16, two: u16, three: u16, four: u16) -> io::Result<Component> {
        if one <= 1
            && two < MAX_COMPONENT_VALUE
            && three < MAX_COMPONENT_VALUE
            && four < MAX_COMPONENT_VALUE
        {
            Ok(Component {
                one,
                two,
                three,
                four,
            })
        } else {
            Err(io::Error::new(
                io::ErrorKind::Other,
                "encoded component exceeds maximum value",
            ))
        }
    }

    fn encode(&self) -> u32 {
        let v = MAX_COMPONENT_VALUE as u32;

        self.one as u32 * v * v * v
            + self.two as u32 * v * v
            + self.three as u32 * v
            + self.four as u32
    }

    fn is_empty(&self) -> bool {
        self.one == 0 && self.two == 0 && self.three == 0 && self.four == 0
    }

    fn is_full(&self) -> bool {
        let m = MAX_COMPONENT_VALUE - 1;

        self.one == m && self.two == m && self.three == m && self.four == m
    }

    fn next(&self) -> Option<Component> {
        if self.four < MAX_COMPONENT_VALUE - 1 {
            Some(Component {
                one: self.one,
                two: self.two,
                three: self.three,
                four: self.four + 1,
            })
        } else if self.three < MAX_COMPONENT_VALUE - 1 {
            Some(Component {
                one: self.one,
                two: self.two,
                three: self.three + 1,
                four: 0,
            })
        } else if self.two < MAX_COMPONENT_VALUE - 1 {
            Some(Component {
                one: self.one,
                two: self.two + 1,
                three: 0,
                four: 0,
            })
        } else if self.one < MAX_COMPONENT_VALUE - 1 {
            Some(Component {
                one: self.one + 1,
                two: 0,
                three: 0,
                four: 0,
            })
        } else {
            None
        }
    }

    fn paths<P: AsRef<Path>>(&self, base: P) -> (PathBuf, PathBuf) {
        let parent = base
            .as_ref()
            .join(format!("d{}", self.one))
            .join(format!("d{}", self.two))
            .join(format!("d{}", self.three));

        let file = parent.join(format!("d{}", self.four));

        (parent, file)
    }
}

#[test]
fn test_component() {
    let component = Component::new();

    assert_eq!(component.encode(), 0);

    assert_eq!(Component::decode(0).unwrap(), component);

    let component = component.next().unwrap();

    assert_eq!(component.encode(), 1);

    assert_eq!(Component::decode(1).unwrap(), component);

    let mut component = Component::new();

    for n in 0..10000 {
        assert_eq!(component, Component::decode(n).unwrap());

        component = component.next().unwrap();
    }

    assert_eq!(
        Component::decode(1_999_999_999).unwrap(),
        Component::from(1, 999, 999, 999).unwrap()
    );

    assert!(Component::from(2, 0, 0, 0).is_err());

    assert!(Component::decode(2_000_000_000).is_err());
}

pub struct Queue {
    component_section: Option<(Component, SectionWriter)>,
    max_file_size: u32,
    max_item_size: u32,
    path_buf: PathBuf,
    read_chunk_size: u32,
    write_chunk_size: u32,
}

impl Queue {
    /// Constructs a new `Queue` that is used to read and
    /// write items to the filesystem.
    pub fn new<S: AsRef<OsStr> + ?Sized>(path: &S) -> Queue {
        let path_buf = PathBuf::from(path);

        Queue {
            component_section: None,
            max_file_size: 2147287039,
            max_item_size: 65536,
            path_buf,
            read_chunk_size: 8192,
            write_chunk_size: 8192,
        }
    }

    pub fn config<S: AsRef<OsStr> + ?Sized>(
        path: &S,
        max_file_size: u32,
        max_item_size: u32,
        read_chunk_size: u32,
        write_chunk_size: u32,
    ) -> Queue {
        let path_buf = PathBuf::from(path);

        Queue {
            component_section: None,
            max_file_size,
            max_item_size,
            path_buf,
            read_chunk_size,
            write_chunk_size,
        }
    }

    pub fn append(&mut self, data: &[u8]) -> io::Result<()> {
        let advance_and_append = self.with(|ref _component, ref mut section| {
            if section.is_full() {
                Ok(true)
            } else {
                section.append(data)?;

                Ok(false)
            }
        })?;

        if advance_and_append {
            self.advance()?;

            self.with(|ref _component, ref mut section2| section2.append(data))
        } else {
            Ok(())
        }
    }

    pub fn is_empty(&mut self) -> io::Result<bool> {
        self.with(|ref component, ref mut section| Ok(component.is_empty() && section.is_empty()))
    }

    pub fn is_full(&mut self) -> io::Result<bool> {
        self.with(|ref component, ref mut section| Ok(component.is_full() && section.is_full()))
    }

    pub fn last_id(&mut self) -> io::Result<Option<u32>> {
        self.with(|ref _component, ref mut section| Ok(section.last_id()))
    }

    pub fn sync(&mut self) -> io::Result<()> {
        self.with(|ref _component, ref mut section| section.sync())
    }

    pub fn stream(
        &self,
        id: Option<u64>,
    ) -> io::Result<impl Iterator<Item = io::Result<QueueItem>>> {
        let iterator = self.stream_with_truncated(id)?;

        Ok(iterator.filter_map(|r| match r {
            Ok((_, true)) => None,
            Ok((item, false)) => Some(Ok(item)),
            Err(e) => Some(Err(e)),
        }))
    }

    pub fn stream_with_truncated(
        &self,
        id: Option<u64>,
    ) -> io::Result<impl Iterator<Item = io::Result<(QueueItem, bool)>>> {
        let (component, section_offset) = match id {
            Some(id) => offset_decode(id)?,
            None => (Component::new(), 0),
        };

        // @FIXME have the struct take a reference equal to our lifetime?
        Ok(QueueIterator::new(
            self.path_buf.clone(),
            component,
            self.max_file_size,
            self.max_item_size,
            self.read_chunk_size,
            section_offset,
        ))
    }

    fn advance(&mut self) -> io::Result<()> {
        let max_file_size = self.max_file_size;
        let max_item_size = self.max_item_size;
        let read_chunk_size = self.read_chunk_size;
        let write_chunk_size = self.write_chunk_size;

        let path_buf = self.path_buf.clone();
        let next_component_section = self.with(|ref component, ref mut section| {
            section.sync()?;

            match component.next() {
                Some(c) => {
                    // @TODO move the base path directly into components
                    let (parent, path) = c.paths(&path_buf);

                    fs::create_dir_all(&parent)?;

                    let section = SectionWriter::new(
                        &path,
                        max_file_size,
                        max_item_size,
                        read_chunk_size,
                        write_chunk_size,
                    )?;

                    Ok((c, section))
                }

                None => Err(io::Error::new(io::ErrorKind::Other, "queue is full")),
            }
        })?;

        self.component_section = Some(next_component_section);

        Ok(())
    }

    fn with<A, F>(&mut self, f: F) -> io::Result<A>
    where
        F: Fn(&Component, &mut SectionWriter) -> io::Result<A>,
    {
        if self.component_section.is_none() {
            fs::create_dir_all(&self.path_buf)?;

            let (c0_path, c0) = depot_latest_init_dir(&self.path_buf)?;
            let (c1_path, c1) = depot_latest_init_dir(&c0_path)?;
            let (c2_path, c2) = depot_latest_init_dir(&c1_path)?;
            let (c3_path, c3) = depot_latest_init_file(&c2_path)?;

            self.component_section = Some((
                Component::from(c0, c1, c2, c3)?,
                SectionWriter::new(
                    &c3_path,
                    self.max_file_size,
                    self.max_item_size,
                    self.read_chunk_size,
                    self.write_chunk_size,
                )?,
            ));
        }

        match self.component_section {
            Some((ref component, ref mut section)) => f(component, section),

            None => {
                // this shouldn't be possible, given initialization above..
                Err(io::Error::new(
                    io::ErrorKind::Other,
                    "section not initialized; this is likely a bug",
                ))
            }
        }
    }
}

#[derive(Debug)]
pub struct QueueItem {
    pub id: u64,
    pub data: Vec<u8>,
}

struct QueueIterator {
    component: Component,
    known_eof: bool,
    max_file_size: u32,
    max_item_size: u32,
    path_buf: PathBuf,
    read_chunk_size: u32,
    section: Option<Box<Iterator<Item = io::Result<SectionItem>>>>,
    section_offset: u32,
}

impl QueueIterator {
    fn new(
        path_buf: PathBuf,
        component: Component,
        max_file_size: u32,
        max_item_size: u32,
        read_chunk_size: u32,
        section_offset: u32,
    ) -> QueueIterator {
        QueueIterator {
            component,
            known_eof: false,
            max_file_size,
            max_item_size,
            path_buf,
            read_chunk_size,
            section: None,
            section_offset,
        }
    }
}

impl Iterator for QueueIterator {
    type Item = io::Result<(QueueItem, bool)>;

    fn next(&mut self) -> Option<io::Result<(QueueItem, bool)>> {
        // The last file we read indicated EOF, so we need
        // to advance sections or bail out if unable to.
        if self.known_eof {
            match self.component.next() {
                Some(c) => {
                    self.component = c;
                    self.known_eof = false;
                    self.section = None;
                    self.section_offset = 0;
                }

                None => {
                    return None;
                }
            }
        }

        // We haven't opened the next section yet, so attempt to.
        // If it doesn't exist, we do nothing. If it does, attempt to
        // open the file. If that fails, which should be rare, return
        // to the user as an error.
        if self.section.is_none() {
            let (_, section_path) = self.component.paths(&self.path_buf);

            if section_path.exists() {
                let reader = SectionReader::new(
                    &section_path,
                    self.max_file_size,
                    self.max_item_size,
                    self.read_chunk_size,
                );

                match reader.stream_with_truncated(Some(self.section_offset)) {
                    Ok(iterator) => {
                        self.section = Some(Box::new(iterator));
                    }

                    Err(e) => {
                        return Some(Err(e));
                    }
                }
            }
        }

        match self.section {
            Some(ref mut s) => match s.next() {
                Some(Ok(SectionItem {
                    id,
                    data,
                    known_eof,
                    truncated,
                })) => {
                    self.known_eof = known_eof;
                    self.section_offset = id;

                    Some(Ok((
                        QueueItem {
                            id: offset_encode(&self.component, id),
                            data,
                        },
                        truncated,
                    )))
                }

                Some(Err(e)) => Some(Err(e)),

                None => None,
            },

            None => None,
        }
    }
}

/// Extracts the number from a depot directory/file name.
fn depot_number(name: &str) -> Option<u16> {
    let len = name.len();
    if name.starts_with("d") && len > 1 {
        name[1..].parse().ok().and_then(|n| {
            // @FIXME use Option#filter when in stable
            if n <= MAX_COMPONENT_VALUE {
                Some(n)
            } else {
                None
            }
        })
    } else {
        None
    }
}

/// Finds the latest directory in the specified directory (not recursive). The
/// path should already exist and be a directory.
///
/// If a directory does not exist, it should be created.
///
/// @TODO should we ensure that it's a directory if it already exists?
fn depot_latest_init_dir<P: AsRef<Path>>(path: P) -> io::Result<(PathBuf, u16)> {
    match depot_latest(&path)? {
        Some((entry, n)) => Ok((entry.path(), n)),

        None => {
            let path = path.as_ref().join(format!("d0"));
            fs::create_dir(&path)?;
            Ok((path, 0))
        }
    }
}

/// Finds the latest file in the directory (not recursive). The path
/// should already exist and be a directory.
///
/// Unlike the directory variant, if a file does not exist,
/// its name is determined but it is not created. This is
/// because the underlying section will create the file when
/// it's opened.
///
/// @TODO should we ensure that it's NOT a directory if it already exists?
fn depot_latest_init_file<P: AsRef<Path>>(path: P) -> io::Result<(PathBuf, u16)> {
    match depot_latest(&path)? {
        Some((entry, n)) => Ok((entry.path(), n)),

        None => {
            let path = path.as_ref().join(format!("d0"));
            Ok((path, 0))
        }
    }
}

/// Finds the latest depot file or directory in a directory
fn depot_latest<P: AsRef<Path>>(path: P) -> io::Result<Option<(DirEntry, u16)>> {
    let paths = fs::read_dir(path)?;

    let mut max = None;

    for entry in paths {
        let entry = entry?;

        if let Some(n) = entry.file_name().to_str().and_then(depot_number) {
            match max {
                Some((_, en)) if en > n => (),
                _ => max = Some((entry, n)),
            }
        }
    }

    Ok(max)
}

fn offset_encode(component: &Component, section_offset: u32) -> u64 {
    let f = component.encode() as u64;
    let s = section_offset as u64;

    (f << 32) + s
}

fn offset_decode(offset: u64) -> io::Result<(Component, u32)> {
    let f = (offset >> 32) as u32;
    let s = (offset << 32 >> 32) as u32;
    let c = Component::decode(f)?;

    Ok((c, s))
}

#[test]
fn test_offset_encode_decode() {
    let test = |component: Component, section_offset: u32, expected: u64| {
        let encoded = offset_encode(&component, section_offset);
        let decoded = offset_decode(encoded).unwrap();

        assert_eq!(encoded, expected);
        assert_eq!((component, section_offset), decoded);
    };

    test(Component::new(), 0, 0);
    test(Component::new(), 1, 1);
    test(Component::from(0, 0, 0, 1).unwrap(), 0, 1 << 32);
    test(Component::from(0, 0, 0, 2).unwrap(), 1, (1 << 33) + 1);
    test(
        Component::from(1, 999, 999, 999).unwrap(),
        1,
        8589934587705032705,
    );
}

#[cfg(test)]
mod tests {
    extern crate tempdir;

    use queue::*;
    use std::path::PathBuf;
    use std::thread;
    use std::time;

    #[test]
    fn test_reader_writer_concurrent() {
        let tmp_dir = tempdir::TempDir::new("depot-tests").unwrap();
        let size = 10_000_000;

        let producer = {
            let tmp_path = tmp_dir.path().to_owned();

            thread::spawn(move || {
                let mut queue =
                    Queue::config(&PathBuf::from(&tmp_path), 8388608, 65536, 8192, 8192);

                for i in 0..size {
                    let message =
                        format!("the quick brown fox jumped over the lazy dog, -\n #{}", i);
                    let data = message.as_bytes();
                    queue.append(&data).unwrap();
                }

                queue.sync().unwrap();
            })
        };

        let consumer = {
            let tmp_path = tmp_dir.path().to_owned();

            thread::spawn(move || {
                let queue = Queue::config(&PathBuf::from(&tmp_path), 8388608, 65536, 8192, 8192);
                let mut reader = queue.stream(None).unwrap();

                for _ in 0..size {
                    loop {
                        if let Some(_) = reader.next() {
                            break;
                        } else {
                            thread::sleep(time::Duration::from_millis(10));
                        }
                    }
                }
            })
        };

        let pr = producer.join();
        let cr = consumer.join();

        pr.unwrap();
        cr.unwrap();
    }

    #[test]
    fn test_reader_writer_sequential() {
        let tmp_dir = tempdir::TempDir::new("depot-tests").unwrap();
        let size = 1_000_000;

        {
            let tmp_path = tmp_dir.path().to_owned();

            let mut queue = Queue::config(&PathBuf::from(&tmp_path), 8388608, 65536, 8192, 8192);

            for i in 0..size {
                let message = format!("the quick brown fox jumped over the lazy dog, -\n #{}", i);
                let data = message.as_bytes();
                queue.append(&data).unwrap();
            }

            queue.sync().unwrap();
        }

        {
            let tmp_path = tmp_dir.path().to_owned();

            let queue = Queue::config(&PathBuf::from(&tmp_path), 8388608, 65536, 8192, 8192);
            let mut reader = queue.stream(None).unwrap();

            for _ in 0..size {
                loop {
                    if let Some(_) = reader.next() {
                        break;
                    } else {
                        thread::sleep(time::Duration::from_millis(10));
                    }
                }
            }
        }
    }
}