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
/// Riak Search integrates Apache Solr for indexing and querying Riak KV
///
/// For more information: https://docs.basho.com/riak/kv/latest/developing/usage/search/

use errors::RiakErr;
use protobuf::{Message, RepeatedField};
use rpb::riak_yokozuna::{RpbYokozunaIndex, RpbYokozunaIndexPutReq};
use rpb::riak_search::{RpbSearchQueryReq, RpbSearchQueryResp};
use utils::{ProtobufBytes, SearchQueryRespPrivate, YokozunaIndexPrivate};

/// `YokozunaIndex` represents an index for Riak search
#[derive(Clone, Debug)]
pub struct YokozunaIndex(RpbYokozunaIndex);

impl YokozunaIndex {
    /// constructs a new `YokozunaIndex`
    pub fn new<T: Into<Vec<u8>>>(name: T) -> YokozunaIndex {
        let mut rpb_yokozuna_index = RpbYokozunaIndex::new();
        rpb_yokozuna_index.set_name(name.into());
        YokozunaIndex(rpb_yokozuna_index)
    }

    /// get the "name" property
    pub fn get_name(&self) -> Vec<u8> {
        self.0.get_name().to_vec()
    }

    /// set the "name" property
    pub fn set_name<T: Into<Vec<u8>>>(&mut self, name: T) {
        self.0.set_name(name.into());
    }

    /// get the "schema" property
    pub fn get_schema(&self) -> Option<Vec<u8>> {
        if self.0.has_schema() {
            Some(self.0.get_schema().to_vec())
        } else {
            None
        }
    }

    /// set the "schema" property
    pub fn set_schema<T: Into<Vec<u8>>>(&mut self, schema: T) {
        self.0.set_schema(schema.into());
    }

    /// get the "n_val" property
    pub fn get_n_val(&self) -> Option<u32> {
        if self.0.has_n_val() {
            Some(self.0.get_n_val())
        } else {
            None
        }
    }

    /// set the "n_val" property
    pub fn set_n_val(&mut self, n_val: u32) {
        self.0.set_n_val(n_val);
    }
}

impl ProtobufBytes for YokozunaIndex {
    fn write_to_bytes(self) -> Result<Vec<u8>, RiakErr> {
        let mut req = RpbYokozunaIndexPutReq::new();
        req.set_index(self.0.clone());
        match req.write_to_bytes() {
            Ok(bytes) => Ok(bytes),
            Err(error) => return Err(RiakErr::ProtobufError(error)),
        }
    }
}

impl YokozunaIndexPrivate for YokozunaIndex {
    fn new_from_rpb_yokozuna_index(rpb_yokozuna_index: RpbYokozunaIndex) -> YokozunaIndex {
        YokozunaIndex(rpb_yokozuna_index)
    }
}

/// `SearchQuery` represents a query that can be performed on Riak
#[derive(Clone, Debug)]
pub struct SearchQuery(RpbSearchQueryReq);

impl SearchQuery {
    /// constructs a new `SearchQuery`
    pub fn new<T: Into<Vec<u8>>>(q: T, index: T) -> SearchQuery {
        let mut req = RpbSearchQueryReq::new();
        req.set_q(q.into());
        req.set_index(index.into());
        SearchQuery(req)
    }

    /// get the "q" property
    pub fn get_q(&self) -> Vec<u8> {
        self.0.get_q().to_vec()
    }

    /// set the "q" property
    pub fn set_q<T: Into<Vec<u8>>>(&mut self, q: T) {
        self.0.set_q(q.into());
    }

    /// get the "index" property
    pub fn get_index(&self) -> Vec<u8> {
        self.0.get_index().to_vec()
    }

    /// set the "index" property
    pub fn set_index<T: Into<Vec<u8>>>(&mut self, index: T) {
        self.0.set_index(index.into());
    }

    /// get the "rows" property
    pub fn get_rows(&self) -> Option<u32> {
        if self.0.has_rows() {
            Some(self.0.get_rows())
        } else {
            None
        }
    }

    /// set the "rows" property
    pub fn set_rows(&mut self, rows: u32) {
        self.0.set_rows(rows);
    }

    /// get the "start" property
    pub fn get_start(&self) -> Option<u32> {
        if self.0.has_start() {
            Some(self.0.get_start())
        } else {
            None
        }
    }

    /// set the "start" property
    pub fn set_start(&mut self, start: u32) {
        self.0.set_start(start);
    }

    /// get the "sort" property
    pub fn get_sort(&self) -> Option<Vec<u8>> {
        if self.0.has_sort() {
            Some(self.0.get_sort().to_vec())
        } else {
            None
        }
    }

    /// set the "sort" property
    pub fn set_sort<T: Into<Vec<u8>>>(&mut self, sort: T) {
        self.0.set_sort(sort.into());
    }

    /// get the "filter" property
    pub fn get_filter(&self) -> Option<Vec<u8>> {
        if self.0.has_filter() {
            Some(self.0.get_filter().to_vec())
        } else {
            None
        }
    }

    /// set the "filter" property
    pub fn set_filter<T: Into<Vec<u8>>>(&mut self, filter: T) {
        self.0.set_filter(filter.into());
    }

    /// get the "df" property
    pub fn get_df(&self) -> Option<Vec<u8>> {
        if self.0.has_df() {
            Some(self.0.get_df().to_vec())
        } else {
            None
        }
    }

    /// set the "df" property
    pub fn set_df<T: Into<Vec<u8>>>(&mut self, df: T) {
        self.0.set_df(df.into());
    }

    /// get the "op" property
    pub fn get_op(&self) -> Option<Vec<u8>> {
        if self.0.has_op() {
            Some(self.0.get_op().to_vec())
        } else {
            None
        }
    }

    /// set the "op" property
    pub fn set_op<T: Into<Vec<u8>>>(&mut self, op: T) {
        self.0.set_op(op.into());
    }

    /// get the "fl" property
    pub fn get_fl(&self) -> Option<Vec<Vec<u8>>> {
        if self.0.get_fl().len() > 0 {
            Some(self.0.get_fl().to_vec())
        } else {
            None
        }
    }

    /// set the "fl" property
    pub fn set_fl(&mut self, fl: Vec<Vec<u8>>) {
        self.0.set_fl(RepeatedField::from_vec(fl));
    }

    /// get the "presort" property
    pub fn get_presort(&self) -> Option<Vec<u8>> {
        if self.0.has_presort() {
            Some(self.0.get_presort().to_vec())
        } else {
            None
        }
    }

    /// set the "presort" property
    pub fn set_presort<T: Into<Vec<u8>>>(&mut self, presort: T) {
        self.0.set_presort(presort.into());
    }
}

impl ProtobufBytes for SearchQuery {
    fn write_to_bytes(self) -> Result<Vec<u8>, RiakErr> {
        match self.0.write_to_bytes() {
            Ok(bytes) => Ok(bytes),
            Err(error) => Err(RiakErr::ProtobufError(error)),
        }
    }
}

/// `SearchQueryResp` represents the response for a successful query
#[derive(Clone, Debug)]
pub struct SearchQueryResp {
    pub pairs: Vec<(Vec<u8>, Option<Vec<u8>>)>,
    pub max_score: Option<f32>,
    pub num_found: Option<u32>,
}

impl SearchQueryRespPrivate for SearchQueryResp {
    fn new_from_rpb(mut rpb: RpbSearchQueryResp) -> SearchQueryResp {
        let max_score = rpb.get_max_score();
        let num_found = rpb.get_num_found();

        let mut pairs: Vec<(Vec<u8>, Option<Vec<u8>>)> = Vec::new();
        for mut rpb_search_doc in rpb.take_docs().into_iter() {
            for mut pair in rpb_search_doc.take_fields().into_iter() {
                if pair.has_value() {
                    pairs.push((pair.take_key(), Some(pair.take_value())));
                } else {
                    pairs.push((pair.take_key(), None))
                }
            }
        }

        SearchQueryResp {
            pairs: pairs,
            max_score: Some(max_score),
            num_found: Some(num_found),
        }
    }
}