Skip to content

Commit ed66893

Browse files
authored
doc: advanced metadata topics in book (#407)
1 parent 41826a3 commit ed66893

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- [Defining metadata types in rust](./metadata_derive.md)
2020
- [Metadata and tables](./metadata_tables.md)
2121
- [Metadata schema](./metadata_schema.md)
22+
- [Advanced topics](./metadata_advanced.md)
2223

2324
* [Error handling](./error_handling.md)
2425

book/src/metadata_advanced.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## Advanced topics
2+
3+
### Bulk decoding of metadata
4+
5+
To populate a `Vec` with decoded metadata and accounting
6+
for some rows not having metadata:
7+
8+
* Realize that the decode methods of the `MetadataRoundtrip` trait are associated functions.
9+
* We can use a lending iterator over rows to avoid unnecessary copying.
10+
11+
Therefore:
12+
13+
```rust, noplayground, ignore
14+
{{#include ../../tests/book_metadata.rs:metadata_bulk_decode_lending_iter}}
15+
```
16+
17+
To filter out rows without metadata:
18+
19+
```rust, noplayground, ignore
20+
{{#include ../../tests/book_metadata.rs:metadata_bulk_decode_lending_iter_with_filter}}
21+
```
22+
23+
The first method gives `Vec<Option<M

tests/book_metadata.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#[cfg(feature = "derive")]
22
#[test]
33
fn book_mutation_metadata() {
4+
use streaming_iterator::StreamingIterator;
5+
use tskit::metadata::MetadataRoundtrip;
6+
47
// ANCHOR: metadata_derive
58
#[derive(serde::Serialize, serde::Deserialize, tskit::metadata::MutationMetadata)]
69
#[serializer("serde_json")]
@@ -89,4 +92,30 @@ fn book_mutation_metadata() {
8992
.metadata::<MutationMetadata>(2.into())
9093
.is_none());
9194
// ANCHOR_END: metadata_retrieval_none
95+
96+
// ANCHOR: metadata_bulk_decode_lending_iter
97+
let mut mutation_row_lending_iterator = tables.mutations().lending_iter();
98+
let mut decoded_md = vec![];
99+
while let Some(row_view) = mutation_row_lending_iterator.next() {
100+
match row_view.metadata {
101+
Some(slice) => decoded_md.push(Some(MutationMetadata::decode(slice).unwrap())),
102+
None => decoded_md.push(None),
103+
}
104+
}
105+
// ANCHOR_END: metadata_bulk_decode_lending_iter
106+
107+
// ANCHOR: metadata_bulk_decode_lending_iter_with_filter
108+
let mut mutation_row_lending_iterator = tables.mutations().lending_iter();
109+
let mut decoded_md = vec![];
110+
while let Some(row_view) = mutation_row_lending_iterator
111+
.next()
112+
.filter(|rv| rv.metadata.is_some())
113+
{
114+
decoded_md.push((
115+
row_view.id,
116+
// The unwrap will never panic because of our filter
117+
MutationMetadata::decode(row_view.metadata.unwrap()).unwrap(),
118+
));
119+
}
120+
// ANCHOR_END: metadata_bulk_decode_lending_iter_with_filter
92121
}

0 commit comments

Comments
 (0)