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
use crossbeam_channel::{Receiver, Sender};
use pipeline::{PipelineInfo, PipelineJoin};
use pipeline_matcher::PathMatch;
use std::collections::HashMap;
use std::time::{Duration, Instant};

// ---------------------------------------------------------------------------------------------------------------------
// PipelineSorter
// ---------------------------------------------------------------------------------------------------------------------

pub struct PipelineSorter {
    pub infos: Vec<String>,
    pub errors: Vec<String>,
    pub through: bool,
    map: HashMap<usize, PathMatch>,
    seq_no: usize,
    join_num: usize,
    time_beg: Instant,
    time_bsy: Duration,
}

impl PipelineSorter {
    pub fn new(num: usize) -> Self {
        PipelineSorter {
            infos: Vec::new(),
            errors: Vec::new(),
            through: false,
            map: HashMap::new(),
            seq_no: 0,
            join_num: num,
            time_beg: Instant::now(),
            time_bsy: Duration::new(0, 0),
        }
    }
}

impl PipelineJoin<PathMatch, PathMatch> for PipelineSorter {
    fn setup(&mut self, id: usize, rx: Vec<Receiver<PipelineInfo<PathMatch>>>, tx: Sender<PipelineInfo<PathMatch>>) {
        self.infos = Vec::new();
        self.errors = Vec::new();
        let mut seq_beg_arrived = false;
        let mut end_num = 0;

        loop {
            for rx in &rx {
                match rx.recv() {
                    Ok(PipelineInfo::SeqDat(x, p)) => {
                        watch_time!(self.time_bsy, {
                            if self.through {
                                let _ = tx.send(PipelineInfo::SeqDat(x, p));
                            } else {
                                self.map.insert(x, p);
                                loop {
                                    if !self.map.contains_key(&self.seq_no) {
                                        break;
                                    }
                                    {
                                        let ret = self.map.get(&self.seq_no).unwrap();
                                        let _ = tx.send(PipelineInfo::SeqDat(self.seq_no, ret.clone()));
                                    }
                                    let _ = self.map.remove(&self.seq_no);
                                    self.seq_no += 1;
                                }
                            }
                        });
                    }

                    Ok(PipelineInfo::SeqBeg(x)) => {
                        if !seq_beg_arrived {
                            self.seq_no = x;
                            self.time_beg = Instant::now();
                            let _ = tx.send(PipelineInfo::SeqBeg(x));
                            seq_beg_arrived = true;
                        }
                    }

                    Ok(PipelineInfo::SeqEnd(x)) => {
                        end_num += 1;
                        if end_num != self.join_num {
                            continue;
                        }

                        for i in &self.infos {
                            let _ = tx.send(PipelineInfo::MsgInfo(id, i.clone()));
                        }
                        for e in &self.errors {
                            let _ = tx.send(PipelineInfo::MsgErr(id, e.clone()));
                        }

                        let _ = tx.send(PipelineInfo::MsgTime(id, self.time_bsy, self.time_beg.elapsed()));
                        let _ = tx.send(PipelineInfo::SeqEnd(x));
                        break;
                    }

                    Ok(PipelineInfo::MsgInfo(i, e)) => {
                        let _ = tx.send(PipelineInfo::MsgInfo(i, e));
                    }
                    Ok(PipelineInfo::MsgErr(i, e)) => {
                        let _ = tx.send(PipelineInfo::MsgErr(i, e));
                    }
                    Ok(PipelineInfo::MsgTime(i, t0, t1)) => {
                        let _ = tx.send(PipelineInfo::MsgTime(i, t0, t1));
                    }
                    Err(_) => break,
                }
            }
        }
    }
}

// ---------------------------------------------------------------------------------------------------------------------
// Test
// ---------------------------------------------------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crossbeam_channel::unbounded;
    use pipeline::{PipelineInfo, PipelineJoin};
    use pipeline_matcher::PathMatch;
    use std::path::PathBuf;
    use std::thread;

    #[test]
    fn pipeline_sorter() {
        let mut sorter = PipelineSorter::new(1);

        let (in_tx, in_rx) = unbounded();
        let (out_tx, out_rx) = unbounded();
        thread::spawn(move || {
            sorter.setup(0, vec![in_rx], out_tx);
        });

        let _ = in_tx.send(PipelineInfo::SeqBeg(0));
        let _ = in_tx.send(PipelineInfo::SeqDat(
            2,
            PathMatch {
                path: PathBuf::from("./"),
                matches: Vec::new(),
            },
        ));
        let _ = in_tx.send(PipelineInfo::SeqDat(
            1,
            PathMatch {
                path: PathBuf::from("./"),
                matches: Vec::new(),
            },
        ));
        let _ = in_tx.send(PipelineInfo::SeqDat(
            0,
            PathMatch {
                path: PathBuf::from("./"),
                matches: Vec::new(),
            },
        ));
        let _ = in_tx.send(PipelineInfo::SeqEnd(3));

        let mut ret = Vec::new();
        loop {
            match out_rx.recv().unwrap() {
                PipelineInfo::SeqDat(x, _) => ret.push(x),
                PipelineInfo::SeqEnd(_) => break,
                _ => (),
            }
        }

        assert_eq!(ret[0], 0);
        assert_eq!(ret[1], 1);
        assert_eq!(ret[2], 2);
    }
}