-
Notifications
You must be signed in to change notification settings - Fork 10
/
model.rs
322 lines (270 loc) · 8.02 KB
/
model.rs
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
use elasticsearch::{
Client,
JsonObjectBuilder,
SearchBuilder,
};
pub struct Error {
code: uint,
msg: ~str,
}
impl Error: to_str::ToStr {
pure fn to_str() -> ~str { fmt!("[%u] %s", self.code, self.msg) }
}
impl Error: ToBytes {
fn to_bytes(lsb: bool) -> ~[u8] { self.to_str().to_bytes(lsb) }
}
pub struct Model {
es: Client,
_index: ~str,
_type: ~str,
_id: ~str,
_parent: Option<~str>,
_version: Option<uint>,
source: json::Object,
}
pub fn Model(es: Client, index: ~str, typ: ~str, id: ~str) -> Model {
Model {
es: es,
_index: index,
_type: typ,
_id: id,
_parent: None,
_version: None,
source: LinearMap()
}
}
fn hit_to_model(es: Client, hit: ~json::Object) -> Model {
let mut hit = move hit;
let index = match hit.pop(&~"_index") {
None => fail,
Some(json::String(move s)) => s,
Some(_) => fail,
};
let typ = match hit.pop(&~"_type") {
None => fail,
Some(json::String(move s)) => s,
Some(_) => fail,
};
let id = match hit.pop(&~"_id") {
None => fail,
Some(json::String(move s)) => s,
Some(_) => fail,
};
let parent = match hit.pop(&~"_parent") {
None => None,
Some(json::String(move s)) => Some(s),
Some(_) => fail,
};
let version = match hit.pop(&~"_version") {
None => None,
Some(json::Number(n)) => Some(n as uint),
Some(_) => fail,
};
let source = match hit.pop(&~"_source") {
None => fail,
Some(json::Object(move source)) => source,
Some(_) => fail,
};
Model {
es: es,
_index: index,
_type: typ,
mut _id: id,
mut _parent: parent,
mut _version: version,
source: *source
}
}
pub fn find(es: Client, index: ~str, typ: ~str, id: ~str) -> Option<Model> {
let mut rep = es.get(index, typ, id);
// Fail if ES had an error.
if rep.code != 200u { fail rep.body.to_str(); }
// FIXME: https://github.com/mozilla/rust/issues/3767
let hit = match rep.body {
json::Object(hit) => copy hit,
_ => fail
};
match hit.get_ref(&~"exists") {
&json::Boolean(true) => {},
_ => return None,
}
Some(hit_to_model(es, move hit))
}
pub fn search(es: Client, f: fn(@SearchBuilder)) -> ~[Model] {
let bld = es.prepare_search();
f(bld);
let rep = bld.execute();
// Fail if ES had an error.
if rep.code != 200u { fail rep.body.to_str(); }
// FIXME: https://github.com/mozilla/rust/issues/3767
match rep.body {
json::Object(body) => {
match body.get_ref(&~"hits") {
&json::Object(hits) => {
match hits.get_ref(&~"hits") {
&json::List(hits) => {
do hits.map |hit| {
match hit {
&json::Object(hit) => {
hit_to_model(es, copy hit)
}
_ => fail
}
}
},
_ => fail
}
},
_ => fail
}
},
_ => fail
}
}
pub fn all(es: Client, index: ~str, typ: ~str) -> ~[Model] {
do search(es) |bld| {
bld
.set_indices(~[copy index])
.set_types(~[copy typ])
.set_source(
JsonObjectBuilder()
.insert_object(~"query", |bld| {
bld.insert_object(~"match_all", |_bld| { });
})
.object.take()
);
}
}
impl Model {
fn set_null(&mut self, key: ~str) -> bool {
self.source.insert(key, json::Null)
}
fn get_bool(&self, key: &~str) -> bool {
match self.find_bool(key) {
None => fail,
Some(value) => value,
}
}
fn find_bool(&self, key: &~str) -> Option<bool> {
match self.source.find_ref(key) {
None => None,
Some(&json::Boolean(value)) => Some(value),
Some(_) => None,
}
}
fn set_bool(&mut self, key: ~str, value: bool) -> bool {
self.source.insert(key, json::Boolean(value))
}
fn find_str(&self, key: &~str) -> Option<~str> {
match self.source.find_ref(key) {
None => None,
Some(&json::String(value)) => Some(copy value),
Some(_) => None,
}
}
fn get_str(&self, key: &~str) -> ~str {
match self.find_str(key) {
None => fail,
Some(value) => copy value,
}
}
fn set_str(&mut self, key: ~str, value: ~str) -> bool {
self.source.insert(key, json::String(value))
}
fn find_float(&self, key: &~str) -> Option<float> {
match self.source.find_ref(key) {
None => None,
Some(&json::Number(value)) => Some(value),
Some(_) => None,
}
}
fn get_float(&self, key: &~str) -> float {
match self.find_float(key) {
None => fail,
Some(value) => value,
}
}
fn set_float(&mut self, key: ~str, value: float) -> bool {
self.source.insert(key, json::Number(value))
}
fn find_uint(&self, key: &~str) -> Option<uint> {
self.find_float(key).map(|value| *value as uint)
}
fn get_uint(&self, key: &~str) -> uint {
self.get_float(key) as uint
}
fn set_uint(&mut self, key: ~str, value: uint) -> bool {
self.set_float(key, value as float)
}
fn find_int(&self, key: &~str) -> Option<int> {
self.find_float(key).map(|value| *value as int)
}
fn get_int(&self, key: &~str) -> int {
self.get_float(key) as int
}
fn set_int(&mut self, key: ~str, value: int) -> bool {
self.set_float(key, value as float)
}
fn index(
&self,
op_type: elasticsearch::OpType
) -> Result<(~str, uint), Error> {
let index = self.es.prepare_index(copy self._index, copy self._type)
.set_op_type(op_type)
.set_source(~copy self.source)
.set_refresh(true);
if !self._id.is_empty() { index.set_id(copy self._id); }
match self._parent {
None => {},
Some(copy parent) => { index.set_parent(parent); },
}
match self._version {
None => {},
Some(copy version) => { index.set_version(version); },
}
let rep = index.execute();
if rep.code == 200 || rep.code == 201 {
let body = match rep.body {
json::Object(ref body) => body,
_ => fail,
};
let id = match body.get_ref(&~"_id") {
&json::String(id) => copy id,
_ => fail,
};
let version = match body.get_ref(&~"_version") {
&json::Number(version) => version as uint,
_ => fail,
};
Ok((id, version))
} else {
match rep.body {
json::Object(ref body) => {
match body.get_ref(&~"error") {
&json::String(ref e) => {
Err(Error { code: rep.code, msg: *e })
},
_ => fail,
}
}
_ => fail,
}
}
}
fn create(&self) -> Result<(~str, uint), Error> {
self.index(elasticsearch::CREATE)
}
fn save(&self) -> Result<(~str, uint), Error> {
self.index(elasticsearch::INDEX)
}
fn delete(&self) {
if !self._id.is_empty() {
self.es.prepare_delete(
copy self._index,
copy self._type,
copy self._id)
.set_refresh(true)
.execute();
}
}
}