stamtools/
info.rs

1use stam::{AnnotationStore, AssociatedFile, Configurable, Handle, Text};
2
3const BYTES: &str = "bytes";
4const GIB: &str = "GiB";
5const MIB: &str = "MiB";
6const KIB: &str = "KiB";
7
8/// Returns a human readable representation of memory usage. Input is bytes, output is a float and an automatically determined unit string.
9fn humanmem(bytes: usize) -> (f64, &'static str) {
10    if bytes >= 1024 * 1024 * 1024 {
11        let bytes: f64 = bytes as f64;
12        let gb = bytes / 1024.0 / 1024.0 / 1024.0;
13        (gb, GIB)
14    } else if bytes >= 1024 * 1024 {
15        let bytes: f64 = bytes as f64;
16        let mb = bytes / 1024.0 / 1024.0;
17        (mb, MIB)
18    } else if bytes >= 1024 {
19        let bytes: f64 = bytes as f64;
20        let kb = bytes / 1024.0;
21        (kb, KIB)
22    } else {
23        let bytes: f64 = bytes as f64;
24        (bytes, BYTES)
25    }
26}
27
28/// Output information of the annotation store to stdout/stderr
29pub fn info(store: &AnnotationStore, verbose: bool) {
30    if !verbose {
31        eprintln!("(Tip: add --verbose for more detailed info output)");
32    }
33    let mut totalbytes = 0; //total memory consumption
34    if let Some(id) = store.id() {
35        println!("ID: {}", id);
36    }
37    println!("Configuration: {:?}", store.config());
38    println!("Filename: {:?}", store.filename().unwrap_or("(none)"));
39    let len = store.index_len();
40    let partial = store.index_partialcount();
41    let total = store.index_totalcount();
42    let bytes = store.index_meminfo();
43    println!("Indices:");
44    totalbytes += bytes.0;
45    let mem = humanmem(bytes.0);
46    println!(
47        "    - dataset_data_annotation_map:      {} -> {} -> {} (> {:.2} {})",
48        len.0, partial.0, total.0, mem.0, mem.1
49    );
50    totalbytes += bytes.1;
51    let mem = humanmem(bytes.1);
52    println!(
53        "    - textrelationmap:                  {} -> {} -> {} (> {:.2} {})",
54        len.1, partial.1, total.1, mem.0, mem.1
55    );
56    totalbytes += bytes.2;
57    let mem = humanmem(bytes.2);
58    println!(
59        "    - resource_annotation_map:          {} -> {} (> {:.2} {})",
60        len.2, total.2, mem.0, mem.1
61    );
62    totalbytes += bytes.3;
63    let mem = humanmem(bytes.3);
64    println!(
65        "    - dataset_annotation_map:           {} -> {} (> {:.2} {})",
66        len.3, total.3, mem.0, mem.1
67    );
68    totalbytes += bytes.4;
69    let mem = humanmem(bytes.4);
70    println!(
71        "    - annotation_annotation_map:        {} -> {} (> {:.2} {})",
72        len.4, total.4, mem.0, mem.1
73    );
74    totalbytes += bytes.5;
75    let mem = humanmem(bytes.5);
76    println!(
77        "    - resource_idmap:        {} (> {:.2} {})",
78        len.5, mem.0, mem.1
79    );
80    totalbytes += bytes.6;
81    let mem = humanmem(bytes.6);
82    println!(
83        "    - dataset_idmap:        {} (> {:.2} {})",
84        len.6, mem.0, mem.1
85    );
86    totalbytes += bytes.7;
87    let mem = humanmem(bytes.7);
88    println!(
89        "    - annotation_idmap:        {} (> {:.2} {})",
90        len.7, mem.0, mem.1
91    );
92    totalbytes += bytes.7;
93    let mem = humanmem(bytes.8);
94    println!(
95        "    - key_annotation_map:        {} (> {:.2} {})",
96        len.8, mem.0, mem.1
97    );
98    let mem = humanmem(bytes.9);
99    println!(
100        "    - key_annotation_metamap:        {} (> {:.2} {})",
101        len.9, mem.0, mem.1
102    );
103    let mem = humanmem(bytes.10);
104    println!(
105        "    - data_annotation_metamap:        {} (> {:.2} {})",
106        len.10, mem.0, mem.1
107    );
108    let mem = humanmem(bytes.11);
109    println!(
110        "    - annotation_substore_map:        {} (> {:.2} {})",
111        len.11, mem.0, mem.1
112    );
113    let mem = humanmem(bytes.12);
114    println!(
115        "    - resource_substore_map:        {} (> {:.2} {})",
116        len.12, mem.0, mem.1
117    );
118    let mem = humanmem(bytes.13);
119    println!(
120        "    - dataset_substore_map:        {} (> {:.2} {})",
121        len.13, mem.0, mem.1
122    );
123    println!("Substores:              {}", store.substores_len());
124    for substore in store.substores() {
125        let bytes = substore.as_ref().meminfo();
126        totalbytes += bytes;
127        println!(
128            "    - [{}] Substore ID: {:?}; filename: {:?}, parents: {:?}, #resources: {}, #datasets: {}, #annotations: {}, memory estimate: {:.2} {}",
129            substore.handle().as_usize(),
130            substore.id().unwrap_or("(none)"),
131            substore.as_ref().filename(),
132            substore.as_ref().parents(),
133            substore.as_ref().resources_len(),
134            substore.as_ref().datasets_len(),
135            substore.as_ref().annotations_len(),
136            mem.0,
137            mem.1,
138        );
139    }
140    println!("Resources:              {}", store.resources_len());
141    for resource in store.resources() {
142        let textsize = humanmem(resource.text().len());
143        let bytes = resource.as_ref().meminfo();
144        totalbytes += bytes;
145        let mem = humanmem(bytes);
146        println!(
147            "    - [{}] Resource ID: {:?}; filename: {:?}, textlength: {}, textsize: {:.2} {}, #positions: {}, #textselections: {}, memory estimate: {:.2} {}",
148            resource.handle().as_usize(),
149            resource.id().unwrap_or("(none)"),
150            resource.as_ref().filename().unwrap_or("(none)"),
151            resource.textlen(),
152            textsize.0,
153            textsize.1,
154            resource.as_ref().positionindex_len(),
155            resource.textselections_len(),
156            mem.0,
157            mem.1,
158        );
159        if verbose {
160            for textselection in resource.textselections() {
161                println!(
162                    "        - [{}] TextSelection; begin: {}; end: {}, text: {:?}, #annotations: {}",
163                    textselection.handle().expect("handle must exist").as_usize(),
164                    textselection.begin(),
165                    textselection.end(),
166                    //text:
167                    {
168                        let text = textselection.text();
169                        if text.len() > 1024 {
170                            "(too long)"
171                        } else {
172                            text
173                        }
174                    },
175                    //nrannotations:
176                    textselection.annotations_len()
177                );
178            }
179        }
180    }
181    println!("Annotation datasets:    {}", store.datasets_len());
182    for dataset in store.datasets() {
183        let bytes = dataset.as_ref().meminfo();
184        totalbytes += bytes;
185        let mem = humanmem(bytes);
186        println!(
187            "    - [{}] Set ID: {:?}; filename: {:?}, #keys: {}; #data: {}, memory estimate: {:.2} {}",
188            dataset.handle().as_usize(),
189            dataset.id().unwrap_or("(none)"),
190            dataset.as_ref().filename().unwrap_or("(none)"),
191            dataset.as_ref().keys_len(),
192            dataset.as_ref().data_len(),
193            mem.0,
194            mem.1,
195        );
196        if verbose {
197            for key in dataset.keys() {
198                println!(
199                    "        - [{}] Key ID: {:?}; #data: {}",
200                    key.handle().as_usize(),
201                    key.id().unwrap_or("(none)"),
202                    dataset.as_ref().data_by_key(key).unwrap_or(&vec!()).len()
203                );
204            }
205            for data in dataset.data() {
206                println!(
207                    "        - [{}] Data ID: {:?}; Key: {:?}; Value: {:?}; #annotations: {}",
208                    data.handle().as_usize(),
209                    data.id().unwrap_or("(none)"),
210                    data.key().id().unwrap_or("(none)"),
211                    data.value(),
212                    data.annotations().count()
213                );
214            }
215        }
216    }
217    let bytes = store.annotations_meminfo();
218    totalbytes += bytes;
219    let mem = humanmem(bytes);
220    println!(
221        "Annotations:            {} (> {:.2} {})",
222        store.annotations_len(),
223        mem.0,
224        mem.1
225    );
226    if verbose {
227        for annotation in store.annotations() {
228            println!(
229                "    - [{}] Annotation ID: {:?}; target: {:?}; text: {:?}, #data: {}",
230                annotation.handle().as_usize(),
231                annotation.id().unwrap_or("(none)"),
232                annotation.as_ref().target(),
233                //text:
234                {
235                    let text: Vec<&str> = annotation.text().collect();
236                    text
237                },
238                annotation.as_ref().len(),
239            );
240            for data in annotation.data() {
241                println!(
242                    "        - [{}] Data ID: {:?}; Set ID: {:?}; Key: {:?}; Value: {:?}",
243                    data.handle().as_usize(),
244                    data.id().unwrap_or("(none)"),
245                    data.set().id().unwrap_or("(none)"),
246                    data.key().id().unwrap_or("(none)"),
247                    data.value(),
248                );
249            }
250        }
251    }
252    let mem = humanmem(totalbytes);
253    println!(
254        "Total estimated memory consumption: > {:.2} {}",
255        mem.0, mem.1
256    );
257}