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
extern crate boolinator;
extern crate downcast_rs;
extern crate failure;
use crate::command::ExtCommand;
use crate::config::{Collection, Encoding};
use crate::run::EvaluateData;
use crate::*;
use boolinator::Boolinator;
use downcast_rs::Downcast;
use failure::ResultExt;
use std::convert::TryFrom;
use std::fmt::Debug;
use std::path::{Path, PathBuf};
pub trait PisaExecutor: Debug + Downcast {
fn command(&self, program: &str) -> ExtCommand;
}
impl_downcast!(PisaExecutor);
#[cfg_attr(tarpaulin, skip)]
impl dyn PisaExecutor {
pub fn invert<P1, P2>(
&self,
forward_index: P1,
inverted_index: P2,
term_count: usize,
) -> Result<(), Error>
where
P1: AsRef<Path>,
P2: AsRef<Path>,
{
let fwd = forward_index
.as_ref()
.to_str()
.ok_or("Failed to parse forward index path")?;
let inv = inverted_index
.as_ref()
.to_str()
.ok_or("Failed to parse inverted index path")?;
self.command("invert")
.args(&[
"-i",
fwd,
"-o",
inv,
"--term-count",
&term_count.to_string(),
])
.status()
.context("Failed to execute: invert")?
.success()
.ok_or("Failed to invert index")?;
Ok(())
}
pub fn compress<P>(&self, inverted_index: P, encoding: &Encoding) -> Result<(), Error>
where
P: AsRef<Path>,
{
let inv = inverted_index
.as_ref()
.to_str()
.ok_or("Failed to parse inverted index path")?;
self.command("create_freq_index")
.args(&[
"-t",
encoding.as_ref(),
"-c",
inv,
"-o",
&format!("{}.{}", inv, encoding),
"--check",
])
.status()
.context("Failed to execute: create_freq_index")?
.success()
.ok_or("Failed to compress index")?;
Ok(())
}
pub fn create_wand_data<P>(&self, inverted_index: P) -> Result<(), Error>
where
P: AsRef<Path>,
{
let inv = inverted_index
.as_ref()
.to_str()
.ok_or("Failed to parse inverted index path")?;
self.command("create_wand_data")
.args(&["-c", inv, "-o", &format!("{}.wand", inv)])
.status()
.context("Failed to execute create_wand_data")?
.success()
.ok_or("Failed to create WAND data")?;
Ok(())
}
pub fn build_lexicon<P1, P2>(&self, input: P1, output: P2) -> Result<(), Error>
where
P1: AsRef<Path>,
P2: AsRef<Path>,
{
let input = input
.as_ref()
.to_str()
.ok_or("Failed to parse input path")?;
let output = output
.as_ref()
.to_str()
.ok_or("Failed to parse output path")?;
self.command("lexicon")
.args(&["build", input, output])
.status()
.context("Failed to execute lexicon build")?
.success()
.ok_or("Failed to build lexicon")?;
Ok(())
}
pub fn extract_topics<P1, P2>(&self, input: P1, output: P2) -> Result<(), Error>
where
P1: AsRef<Path>,
P2: AsRef<Path>,
{
let input = input
.as_ref()
.to_str()
.ok_or("Failed to parse input path")?;
let output = output
.as_ref()
.to_str()
.ok_or("Failed to parse output path")?;
self.command("extract_topics")
.args(&["-i", input, "-o", output])
.status()
.context("Failed to execute extract_topics")?
.success()
.ok_or("Failed to extract topics")?;
Ok(())
}
pub fn evaluate_queries<S>(
&self,
collection: &Collection,
_run_data: &EvaluateData,
queries: S,
) -> Result<String, Error>
where
S: AsRef<str>,
{
let inv = collection
.inverted_index
.to_str()
.ok_or("Failed to parse inverted index path")?;
let fwd = collection
.forward_index
.to_str()
.ok_or("Failed to parse forward index path")?;
let encoding = &collection.encodings.first().unwrap();
let output = self
.command("evaluate_queries")
.args(&["-t", encoding.as_ref()])
.args(&["-i", &format!("{}.{}", inv, encoding)])
.args(&["-w", &format!("{}.wand", inv)])
.args(&["-a", "wand"])
.args(&["-q", queries.as_ref()])
.args(&["--terms", &format!("{}.termmap", fwd)])
.args(&["--documents", &format!("{}.docmap", fwd)])
.args(&["--stemmer", "porter2"])
.args(&["-k", "1000"])
.output()
.context("Failed to run evaluate_queries")?;
if output.status.success() {
Ok(String::from_utf8(output.stdout).unwrap())
} else {
Err(Error::from(String::from_utf8(output.stderr).unwrap()))
}
}
}
#[derive(Default, Debug)]
pub struct SystemPathExecutor {}
impl SystemPathExecutor {
pub fn new() -> Self {
Self {}
}
}
impl PisaExecutor for SystemPathExecutor {
fn command(&self, program: &str) -> ExtCommand {
ExtCommand::new(program)
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct CustomPathExecutor {
bin: PathBuf,
}
impl TryFrom<&Path> for CustomPathExecutor {
type Error = Error;
fn try_from(bin_path: &Path) -> Result<Self, Error> {
if bin_path.is_dir() {
Ok(Self {
bin: bin_path.to_path_buf(),
})
} else {
Err(format!(
"Failed to construct executor: not a directory: {}",
bin_path.display()
)
.into())
}
}
}
impl TryFrom<PathBuf> for CustomPathExecutor {
type Error = Error;
fn try_from(bin_path: PathBuf) -> Result<Self, Error> {
Self::try_from(bin_path.as_path())
}
}
impl TryFrom<&str> for CustomPathExecutor {
type Error = Error;
fn try_from(bin_path: &str) -> Result<Self, Error> {
Self::try_from(Path::new(bin_path))
}
}
impl CustomPathExecutor {
pub fn path(&self) -> &Path {
self.bin.as_path()
}
}
impl PisaExecutor for CustomPathExecutor {
fn command(&self, program: &str) -> ExtCommand {
ExtCommand::new(&self.bin.join(program).to_str().unwrap().to_string())
}
}
#[cfg(test)]
mod tests;