|
|
@@ -14,21 +14,49 @@ use std::fs::File;
|
|
14
|
14
|
use std::path::Path;
|
|
15
|
15
|
|
|
16
|
16
|
|
|
|
17
|
+/// A read-only memory map.
|
|
17
|
18
|
pub struct Mmap {
|
|
18
|
19
|
mmap: memmap::Mmap,
|
|
19
|
20
|
}
|
|
20
|
21
|
|
|
21
|
22
|
impl Mmap {
|
|
22
|
|
- // The underlying file should not be modified while holding the memory map.
|
|
23
|
|
- // See https://github.com/danburkert/memmap-rs/issues/25
|
|
|
23
|
+ /// Creates a memory map from a given file.
|
|
|
24
|
+ ///
|
|
|
25
|
+ /// # Safety
|
|
|
26
|
+ /// The underlying file should not be modified while holding the memory map.
|
|
|
27
|
+ /// See https://github.com/danburkert/memmap-rs/issues/25
|
|
|
28
|
+ ///
|
|
|
29
|
+ /// # Example
|
|
|
30
|
+ /// ```
|
|
|
31
|
+ /// use osmpbf::*;
|
|
|
32
|
+ ///
|
|
|
33
|
+ /// # fn foo() -> Result<()> {
|
|
|
34
|
+ /// let f = std::fs::File::open("tests/test.osm.pbf")?;
|
|
|
35
|
+ /// let mmap = unsafe { Mmap::from_file(&f)? };
|
|
|
36
|
+ /// # Ok(())
|
|
|
37
|
+ /// # }
|
|
|
38
|
+ /// ```
|
|
24
|
39
|
pub unsafe fn from_file(file: &File) -> Result<Mmap> {
|
|
25
|
40
|
memmap::Mmap::map(file)
|
|
26
|
41
|
.map(|m| Mmap { mmap: m })
|
|
27
|
42
|
.chain_err(|| "Could not create memory map from file")
|
|
28
|
43
|
}
|
|
29
|
44
|
|
|
30
|
|
- // The underlying file should not be modified while holding the memory map.
|
|
31
|
|
- // See https://github.com/danburkert/memmap-rs/issues/25
|
|
|
45
|
+ /// Creates a memory map from a given path.
|
|
|
46
|
+ ///
|
|
|
47
|
+ /// # Safety
|
|
|
48
|
+ /// The underlying file should not be modified while holding the memory map.
|
|
|
49
|
+ /// See https://github.com/danburkert/memmap-rs/issues/25
|
|
|
50
|
+ ///
|
|
|
51
|
+ /// # Example
|
|
|
52
|
+ /// ```
|
|
|
53
|
+ /// use osmpbf::*;
|
|
|
54
|
+ ///
|
|
|
55
|
+ /// # fn foo() -> Result<()> {
|
|
|
56
|
+ /// let mmap = unsafe { Mmap::from_path("tests/test.osm.pbf")? };
|
|
|
57
|
+ /// # Ok(())
|
|
|
58
|
+ /// # }
|
|
|
59
|
+ /// ```
|
|
32
|
60
|
pub unsafe fn from_path<P: AsRef<Path>>(path: P) -> Result<Mmap> {
|
|
33
|
61
|
let file = File::open(&path)?;
|
|
34
|
62
|
memmap::Mmap::map(&file)
|
|
|
@@ -36,6 +64,7 @@ impl Mmap {
|
|
36
|
64
|
.chain_err(|| format!("Could not create memory map from path {}", path.as_ref().display()))
|
|
37
|
65
|
}
|
|
38
|
66
|
|
|
|
67
|
+ /// Returns an iterator over the blobs in this memory map.
|
|
39
|
68
|
pub fn blob_iter(&self) -> MmapBlobReader {
|
|
40
|
69
|
MmapBlobReader::new(self)
|
|
41
|
70
|
}
|
|
|
@@ -45,12 +74,15 @@ impl Mmap {
|
|
45
|
74
|
}
|
|
46
|
75
|
}
|
|
47
|
76
|
|
|
|
77
|
+/// A PBF blob from a memory map.
|
|
48
|
78
|
pub struct MmapBlob<'a> {
|
|
49
|
79
|
header: BlobHeader,
|
|
50
|
80
|
data: &'a [u8],
|
|
51
|
81
|
}
|
|
52
|
82
|
|
|
53
|
83
|
impl<'a> MmapBlob<'a> {
|
|
|
84
|
+ /// Decodes the Blob and tries to obtain the inner content (usually a `HeaderBlock` or a
|
|
|
85
|
+ /// `PrimitiveBlock`). This operation might involve an expensive decompression step.
|
|
54
|
86
|
pub fn decode(&'a self) -> Result<BlobDecode<'a>> {
|
|
55
|
87
|
let blob: fileformat::Blob = protobuf::parse_from_bytes(self.data)
|
|
56
|
88
|
.chain_err(|| "failed to parse Blob")?;
|
|
|
@@ -68,6 +100,7 @@ impl<'a> MmapBlob<'a> {
|
|
68
|
100
|
}
|
|
69
|
101
|
}
|
|
70
|
102
|
|
|
|
103
|
+/// A reader for memory mapped PBF files that allows iterating over `MmapBlob`s.
|
|
71
|
104
|
#[derive(Clone)]
|
|
72
|
105
|
pub struct MmapBlobReader<'a> {
|
|
73
|
106
|
mmap: &'a Mmap,
|
|
|
@@ -76,6 +109,20 @@ pub struct MmapBlobReader<'a> {
|
|
76
|
109
|
}
|
|
77
|
110
|
|
|
78
|
111
|
impl<'a> MmapBlobReader<'a> {
|
|
|
112
|
+ /// Creates a new `MmapBlobReader`.
|
|
|
113
|
+ ///
|
|
|
114
|
+ /// # Example
|
|
|
115
|
+ /// ```
|
|
|
116
|
+ /// use osmpbf::*;
|
|
|
117
|
+ ///
|
|
|
118
|
+ /// # fn foo() -> Result<()> {
|
|
|
119
|
+ ///
|
|
|
120
|
+ /// let mmap = unsafe { Mmap::from_path("tests/test.osm.pbf")? };
|
|
|
121
|
+ /// let reader = MmapBlobReader::new(&mmap);
|
|
|
122
|
+ ///
|
|
|
123
|
+ /// # Ok(())
|
|
|
124
|
+ /// # }
|
|
|
125
|
+ /// ```
|
|
79
|
126
|
pub fn new(mmap: &Mmap) -> MmapBlobReader {
|
|
80
|
127
|
MmapBlobReader {
|
|
81
|
128
|
mmap: mmap,
|