fastobo 0.13.0

Faultless AST for Open Biomedical Ontologies.
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
use std::collections::HashMap;
use std::io::BufRead;
use std::io::BufReader;
use std::num::NonZeroUsize;

use std::convert::TryFrom;
use std::fs::File;
use std::iter::Iterator;

use crossbeam_channel::Receiver;
use crossbeam_channel::Sender;
use crossbeam_channel::TryRecvError;

use lazy_static::lazy_static;

use crate::ast::EntityFrame;
use crate::ast::Frame;
use crate::ast::HeaderClause;
use crate::ast::HeaderFrame;
use crate::ast::OboDoc;
use crate::error::Error;
use crate::error::SyntaxError;
use crate::error::ThreadingError;
use crate::syntax::Lexer;
use crate::syntax::Rule;

use super::FromPair;
use super::Parser;

use self::consumer::Consumer;
use self::consumer::Input as ConsumerInput;
use self::consumer::Output as ConsumerOutput;

mod consumer;

// ---

/// The state of a `ThreadedParser` instance.
#[derive(PartialEq, Eq)]
enum State {
    Idle,
    Started,
    AtEof,
    Waiting,
    Finished,
}

// ---

/// An iterator reading entity frames contained in an OBO stream in parallel.
#[cfg_attr(feature = "_doc", doc(cfg(feature = "threading")))]
pub struct ThreadedParser<B: BufRead> {
    // the reader
    stream: B,
    // the state of the parser
    state: State,
    //
    consumers: Vec<Consumer>,

    // communication channels
    r_item: Receiver<ConsumerOutput>,
    s_text: Sender<Option<ConsumerInput>>,

    /// Buffer for the last line that was read.
    line: String,
    /// Number of threads requested by the user
    threads: NonZeroUsize,

    /// Offsets to report proper error position
    line_offset: usize,
    offset: usize,

    ///
    ordered: bool,
    read_index: usize,
    sent_index: usize,
    queue: HashMap<usize, Result<Frame, Error>>,
}

impl<B: BufRead> AsRef<B> for ThreadedParser<B> {
    fn as_ref(&self) -> &B {
        &self.stream
    }
}

impl<B: BufRead> AsRef<B> for Box<ThreadedParser<B>> {
    fn as_ref(&self) -> &B {
        (**self).as_ref()
    }
}

impl<B: BufRead> AsMut<B> for ThreadedParser<B> {
    fn as_mut(&mut self) -> &mut B {
        &mut self.stream
    }
}

impl<B: BufRead> AsMut<B> for Box<ThreadedParser<B>> {
    fn as_mut(&mut self) -> &mut B {
        (**self).as_mut()
    }
}

impl<B: BufRead> From<B> for ThreadedParser<B> {
    fn from(reader: B) -> Self {
        <Self as Parser<B>>::new(reader)
    }
}

impl<B: BufRead> From<B> for Box<ThreadedParser<B>> {
    fn from(reader: B) -> Self {
        Box::new(ThreadedParser::new(reader))
    }
}

impl<B: BufRead> Iterator for ThreadedParser<B> {
    type Item = Result<Frame, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        macro_rules! send_or_error {
            ($channel:expr, $msg:expr) => {
                if $channel.send($msg).is_err() {
                    self.state = State::Finished;
                    let err = ThreadingError::DisconnectedChannel;
                    return Some(Err(Error::from(err)));
                }
            };
        }

        loop {
            // return and item from the queue if in ordered mode
            if self.ordered {
                if let Some(result) = self.queue.remove(&self.read_index) {
                    self.read_index += 1;
                    return Some(result);
                }
            }

            // poll for parsed frames to return
            match self.r_item.try_recv().map(|i| (i.res, i.index)) {
                // item is found, don't care about order: simply return it
                Ok((Ok(entry), _)) if !self.ordered => return Some(Ok(entry)),
                // error is found: finalize and return it
                Ok((Err(e), _)) if !self.ordered => {
                    self.state = State::Finished;
                    return Some(Err(e));
                }
                // item is found and is the right index: return it
                Ok((result, index)) if index == self.read_index => {
                    self.read_index += 1;
                    return Some(result);
                }
                // item is found but is not the right index: store it
                Ok((result, index)) => {
                    self.queue.insert(index, result);
                }
                // empty queue after all the threads were joined: we are done
                Err(TryRecvError::Empty) if self.state == State::Waiting => {
                    self.state = State::Finished;
                    return None;
                }
                // empty queue in any other state: just do something else
                Err(TryRecvError::Empty) => (),
                // queue was disconnected: stop and return an error
                Err(TryRecvError::Disconnected) => {
                    if self.state != State::Finished {
                        self.state = State::Finished;
                        return Some(Err(Error::from(ThreadingError::DisconnectedChannel)));
                    }
                }
            }

            // depending on the state, do something before polling
            match self.state {
                State::Waiting => (),
                State::AtEof => {
                    self.state = State::Waiting;
                    for consumer in self.consumers.iter_mut() {
                        consumer.join().unwrap();
                    }
                }
                State::Idle => {
                    self.state = State::Started;
                    for consumer in &mut self.consumers {
                        consumer.start();
                    }
                }
                State::Finished => {
                    return None;
                }
                State::Started => {
                    //
                    let mut lines = String::new();
                    let mut l: &str;
                    let mut local_line_offset = 0;
                    let mut local_offset = 0;

                    loop {
                        // store the previous line and process the next line
                        lines.push_str(&self.line);
                        self.line.clear();

                        // read the next line
                        if let Err(e) = self.stream.read_line(&mut self.line) {
                            self.state = State::Finished;
                            return Some(Err(Error::from(e)));
                        }

                        // check if we reached the end of the frame
                        l = self.line.trim_start();
                        if l.starts_with('[') {
                            // send the entire frame with the location offsets
                            let msg = ConsumerInput::new(
                                lines,
                                self.sent_index,
                                self.line_offset,
                                self.offset,
                            );
                            send_or_error!(self.s_text, Some(msg));
                            // update the local offsets and bail out
                            self.sent_index += 1;
                            self.line_offset += local_line_offset + 1;
                            self.offset += local_offset + self.line.len();
                            break;
                        } else if self.line.is_empty() {
                            // change the state to wait for workers to finish
                            self.state = State::AtEof;
                            // if some lines remain, send them as text
                            if !lines.chars().all(|c| c.is_whitespace()) {
                                let msg = ConsumerInput::new(
                                    lines,
                                    self.sent_index,
                                    self.line_offset,
                                    self.offset,
                                );
                                send_or_error!(self.s_text, Some(msg));
                            }
                            // poison-pill the remaining workers and bail out
                            for _ in 0..self.threads.get() {
                                send_or_error!(self.s_text, None);
                            }
                            break;
                        }

                        // Update local offsets
                        local_line_offset += 1;
                        local_offset += self.line.len();
                    }
                }
            }
        }
    }
}

impl<B: BufRead> Parser<B> for ThreadedParser<B> {
    /// Create a new `ThreadedParser` with all available CPUs.
    ///
    /// The number of available CPUs will be polled at runtime and then the
    /// right number of threads will be spawned accordingly.
    fn new(stream: B) -> Self {
        lazy_static! {
            static ref THREADS: usize = num_cpus::get();
        }
        let threads = unsafe { NonZeroUsize::new_unchecked(*THREADS) };
        Self::with_threads(stream, threads)
    }

    /// Create a new `ThreadedParser` with the given number of threads.
    fn with_threads(mut stream: B, threads: NonZeroUsize) -> Self {
        //
        let mut frame_clauses = Vec::new();
        let mut line = String::new();
        let mut l: &str;
        let mut offset = 0;
        let mut line_offset = 0;

        // create the communication channels
        let (s_text, r_text) = crossbeam_channel::unbounded();
        let (s_item, r_item) = crossbeam_channel::unbounded();

        // read until we reach the first entity frame
        let header = loop {
            // Read the next line
            line.clear();
            if let Err(e) = stream.read_line(&mut line) {
                break Err(Error::from(e));
            };
            l = line.trim_start();

            // if the line is not empty, parse it
            if !l.starts_with('[') && !l.is_empty() {
                // parse the header clause
                let clause = Lexer::tokenize(Rule::HeaderClause, &line)
                    .map_err(SyntaxError::from)
                    .map(|mut p| p.next().unwrap())
                    .and_then(HeaderClause::from_pair)
                    .map_err(SyntaxError::from);
                // check if the clause was parsed properly or not
                match clause {
                    Ok(c) => frame_clauses.push(c),
                    Err(e) => {
                        let err = e.with_offsets(line_offset, offset);
                        break Err(Error::from(err));
                    }
                };
            }

            // if the line is the beginning of an entity frame, stop
            if l.starts_with('[') || line.is_empty() {
                break Ok(Frame::from(HeaderFrame::from(frame_clauses)));
            } else {
                line_offset += 1;
                offset += line.len();
            }
        };

        // create the consumers
        let mut consumers = Vec::with_capacity(threads.get());
        for _ in 0..threads.get() {
            let c = Consumer::new(r_text.clone(), s_item.clone());
            consumers.push(c);
        }

        // send the header to the channel (to get it back immediately after)
        s_item.send(ConsumerOutput::new(header, 0)).ok();

        // return the parser
        Self {
            stream,
            r_item,
            s_text,
            threads,
            consumers,
            line,
            line_offset,
            offset,
            ordered: false,
            read_index: 0,
            sent_index: 1,
            queue: HashMap::new(),
            state: State::Idle,
        }
    }

    /// Make the parser yield frames in the order they appear in the document.
    ///
    /// Note that this has a small performance impact, so this is disabled
    /// by default.
    fn ordered(&mut self, ordered: bool) -> &mut Self {
        self.ordered = ordered;
        self
    }

    /// Consume the reader and extract the internal reader.
    fn into_inner(self) -> B {
        self.stream
    }
}

impl<B: BufRead> Parser<B> for Box<ThreadedParser<B>> {
    fn new(stream: B) -> Self {
        Box::new(ThreadedParser::new(stream))
    }

    fn with_threads(stream: B, threads: NonZeroUsize) -> Self {
        Box::new(ThreadedParser::with_threads(stream, threads))
    }

    fn ordered(&mut self, ordered: bool) -> &mut Self {
        (**self).ordered(ordered);
        self
    }

    fn into_inner(self) -> B {
        (*self).into_inner()
    }
}

impl<B: BufRead> TryFrom<ThreadedParser<B>> for OboDoc {
    type Error = Error;
    fn try_from(mut reader: ThreadedParser<B>) -> Result<Self, Self::Error> {
        OboDoc::try_from(&mut reader)
    }
}

impl<B: BufRead> TryFrom<&mut ThreadedParser<B>> for OboDoc {
    type Error = Error;
    fn try_from(reader: &mut ThreadedParser<B>) -> Result<Self, Self::Error> {
        // extract the header and create the doc
        let header = reader.next().unwrap()?.into_header_frame().unwrap();

        // extract the remaining entities
        let entities = reader
            .map(|r| r.map(|f| f.into_entity_frame().unwrap()))
            .collect::<Result<Vec<EntityFrame>, Error>>()?;

        // return the doc
        Ok(OboDoc::with_header(header).and_entities(entities))
    }
}

impl<B: BufRead> TryFrom<Box<ThreadedParser<B>>> for OboDoc {
    type Error = Error;
    fn try_from(mut reader: Box<ThreadedParser<B>>) -> Result<Self, Self::Error> {
        OboDoc::try_from(&mut (*reader))
    }
}

impl From<File> for ThreadedParser<BufReader<File>> {
    fn from(f: File) -> Self {
        Self::new(BufReader::new(f))
    }
}

impl From<File> for Box<ThreadedParser<BufReader<File>>> {
    fn from(f: File) -> Self {
        Box::new(ThreadedParser::new(BufReader::new(f)))
    }
}