Browse Source

Add documentation for lib.rs, mmap_blob.rs, examples

Johannes Hofmann 8 years ago
parent
commit
095dd6b38c
3 changed files with 124 additions and 6 deletions
  1. 3
    0
      examples/count.rs
  2. 70
    2
      src/lib.rs
  3. 51
    4
      src/mmap_blob.rs

+ 3
- 0
examples/count.rs View File

1
+// Count the number of nodes, ways and relations in a PBF file given as the
2
+// first command line argument.
3
+
1
 extern crate osmpbf;
4
 extern crate osmpbf;
2
 
5
 
3
 use osmpbf::*;
6
 use osmpbf::*;

+ 70
- 2
src/lib.rs View File

1
-//! A fast reader for the PBF file format (\*.osm.pbf) for OpenStreetMap data.
1
+/*!
2
+A fast reader for the OpenStreetMap PBF file format (\*.osm.pbf).
3
+
4
+## Usage
5
+
6
+Add this to your `Cargo.toml`:
7
+
8
+```toml
9
+[dependencies]
10
+osmpbf = "0.1"
11
+```
12
+
13
+and this to your crate root:
14
+
15
+```rust
16
+extern crate osmpbf;
17
+```
18
+
19
+## Example: Count ways
20
+
21
+Here's a simple example that counts all the OpenStreetMap way elements in a
22
+file:
23
+
24
+```rust
25
+extern crate osmpbf;
26
+
27
+use osmpbf::*;
28
+
29
+fn main() {
30
+    let reader = ElementReader::from_path("tests/test.osm.pbf").unwrap();
31
+    let mut ways = 0_u64;
32
+
33
+    // Increment the counter by one for each way.
34
+    reader.for_each(|element| {
35
+        if let Element::Way(_) = element {
36
+            ways += 1;
37
+        }
38
+    }).unwrap();
39
+
40
+    println!("Number of ways: {}", ways);
41
+}
42
+```
43
+
44
+## Example: Count ways in parallel
45
+
46
+In this second example, we also count the ways but make use of all cores by
47
+decoding the file in parallel:
48
+
49
+```rust
50
+use osmpbf::*;
51
+
52
+fn main() {
53
+    let reader = ElementReader::from_path("tests/test.osm.pbf").unwrap();
54
+
55
+    // Count the ways
56
+    let ways = reader.par_map_reduce(
57
+        |element| {
58
+            match element {
59
+                Element::Way(_) => 1,
60
+                _ => 0,
61
+            }
62
+        },
63
+        || 0_u64,      // Zero is the identity value for addition
64
+        |a, b| a + b   // Sum the partial results
65
+    ).unwrap();
66
+
67
+    println!("Number of ways: {}", ways);
68
+}
69
+```
70
+*/
2
 
71
 
3
 #![recursion_limit = "1024"]
72
 #![recursion_limit = "1024"]
4
 
73
 
32
 pub mod dense;
101
 pub mod dense;
33
 pub mod elements;
102
 pub mod elements;
34
 pub mod mmap_blob;
103
 pub mod mmap_blob;
35
-

+ 51
- 4
src/mmap_blob.rs View File

14
 use std::path::Path;
14
 use std::path::Path;
15
 
15
 
16
 
16
 
17
+/// A read-only memory map.
17
 pub struct Mmap {
18
 pub struct Mmap {
18
     mmap: memmap::Mmap,
19
     mmap: memmap::Mmap,
19
 }
20
 }
20
 
21
 
21
 impl Mmap {
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
     pub unsafe fn from_file(file: &File) -> Result<Mmap> {
39
     pub unsafe fn from_file(file: &File) -> Result<Mmap> {
25
         memmap::Mmap::map(file)
40
         memmap::Mmap::map(file)
26
             .map(|m| Mmap { mmap: m })
41
             .map(|m| Mmap { mmap: m })
27
             .chain_err(|| "Could not create memory map from file")
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
     pub unsafe fn from_path<P: AsRef<Path>>(path: P) -> Result<Mmap> {
60
     pub unsafe fn from_path<P: AsRef<Path>>(path: P) -> Result<Mmap> {
33
         let file = File::open(&path)?;
61
         let file = File::open(&path)?;
34
         memmap::Mmap::map(&file)
62
         memmap::Mmap::map(&file)
36
             .chain_err(|| format!("Could not create memory map from path {}", path.as_ref().display()))
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
     pub fn blob_iter(&self) -> MmapBlobReader {
68
     pub fn blob_iter(&self) -> MmapBlobReader {
40
         MmapBlobReader::new(self)
69
         MmapBlobReader::new(self)
41
     }
70
     }
45
     }
74
     }
46
 }
75
 }
47
 
76
 
77
+/// A PBF blob from a memory map.
48
 pub struct MmapBlob<'a> {
78
 pub struct MmapBlob<'a> {
49
     header: BlobHeader,
79
     header: BlobHeader,
50
     data: &'a [u8],
80
     data: &'a [u8],
51
 }
81
 }
52
 
82
 
53
 impl<'a> MmapBlob<'a> {
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
     pub fn decode(&'a self) -> Result<BlobDecode<'a>> {
86
     pub fn decode(&'a self) -> Result<BlobDecode<'a>> {
55
         let blob: fileformat::Blob = protobuf::parse_from_bytes(self.data)
87
         let blob: fileformat::Blob = protobuf::parse_from_bytes(self.data)
56
             .chain_err(|| "failed to parse Blob")?;
88
             .chain_err(|| "failed to parse Blob")?;
68
     }
100
     }
69
 }
101
 }
70
 
102
 
103
+/// A reader for memory mapped PBF files that allows iterating over `MmapBlob`s.
71
 #[derive(Clone)]
104
 #[derive(Clone)]
72
 pub struct MmapBlobReader<'a> {
105
 pub struct MmapBlobReader<'a> {
73
     mmap: &'a Mmap,
106
     mmap: &'a Mmap,
76
 }
109
 }
77
 
110
 
78
 impl<'a> MmapBlobReader<'a> {
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
     pub fn new(mmap: &Mmap) -> MmapBlobReader {
126
     pub fn new(mmap: &Mmap) -> MmapBlobReader {
80
         MmapBlobReader {
127
         MmapBlobReader {
81
             mmap: mmap,
128
             mmap: mmap,