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
extern crate futures_cpupool;
use futures_cpupool::CpuPool;
use futures_cpupool::CpuFuture;
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::RwLock;
use global::Global;
use index::Index;
pub struct Search {
pub indices: Arc<
RwLock<
HashMap<
String,
Arc<RwLock<Global>>
>
>
>,
threadpool: CpuPool
}
impl Search {
pub fn new() -> Search {
Search {
indices: Arc::new(
RwLock::new(
HashMap::new()
)
),
threadpool: CpuPool::new_num_cpus()
}
}
pub fn create_index(&mut self, name: &str) -> Result< Arc<RwLock<Global>>, &str > {
let indices = self.indices.clone();
let mut indices = indices.write().unwrap();
if indices.contains_key(name) {
Err("Global Index already exists")
} else {
let index: Arc<RwLock<Global>> =
Arc::new(
RwLock::new(
Global::new(name)
)
);
indices.insert(name.to_string(), index.clone());
Ok(index.clone())
}
}
pub fn remove_index(&mut self, name: &str) -> Result< Arc<RwLock<Global>>, &str > {
let indices = self.indices.clone();
let mut indices = indices.write().unwrap();
match indices.remove(name) {
Some(val) => Ok(val),
None => Err("Global Index not found")
}
}
pub fn insert(&mut self, name: String, corpus: String) -> CpuFuture<Arc<Index>, String> {
let indices = self.indices.clone();
let future: CpuFuture<Arc<Index>, String> = self.threadpool.spawn_fn(move || {
let indices = indices.read().unwrap();
match indices.get(&name) {
Some(val) => {
let val = val.clone();
let mut global = val.write().unwrap();
let index = global.insert(&corpus);
let res: Result<Arc<Index>, String> = Ok(index.clone());
res
},
None => {
let res: Result<Arc<Index>, String> = Err("Global Index not found.".to_string());
res
}
}
});
future
}
pub fn search(&self, name: String, text: String) -> CpuFuture<Vec<(Arc<Index>, f32)>, String> {
let indices = self.indices.clone();
let future: CpuFuture<Vec<(Arc<Index>, f32)>, String> = self.threadpool.spawn_fn(move || {
let indices = indices.read().unwrap();
match indices.get(&name) {
Some(val) => {
let val = val.clone();
let global = val.read().unwrap();
let res: Result<Vec<(Arc<Index>, f32)>, String> = Ok(global.search(&text));
res
},
None => {
let res: Result<Vec<(Arc<Index>, f32)>, String> = Err("Global Index not found.".to_string());
res
}
}
});
future
}
}