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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//! Defines an action for executing a view.

use {DatabaseName, Error, IntoViewPath, ViewResponse, serde, std};
use action::query_keys::*;
use transport::{JsonResponse, JsonResponseDecoder, Request, StatusCode, Transport};
use view::ViewResponseJsonable;

enum Inclusivity {
    Exclusive,
    Inclusive,
}

enum GroupLevel {
    Exact(bool),
    Number(u32),
}

/// Executes a view on the CouchDB server and returns the result.
///
/// Chill executes the view by sending an HTTP request to the CouchDB server to
/// `GET` from or `POST` to the view's path. For more details about executing
/// views, please see the CouchDB documentation.
///
/// # Errors
///
/// The following are _some_ errors that may occur when executing a view.
///
/// <table>
/// <tr>
///  <td><code>Error::NotFound</code></td>
///  <td>The database, design document, or view does not exist.</td>
/// </tr>
/// <tr>
///  <td><code>Error::Unauthorized</code></td>
///  <td>The client lacks permission to execute the view.</td>
/// </tr>
/// </table>
///
/// # Examples
///
/// The following program demonstrates view execution.
///
/// ```
/// extern crate chill;
/// extern crate serde_json;
///
/// let server = chill::testing::FakeServer::new().unwrap();
/// let client = chill::Client::new(server.uri()).unwrap();
///
/// // Create a database and populate it with some documents.
///
/// client.create_database("/baseball").run().unwrap();
///
/// let create_player = |name, home_runs| {
///     client.create_document("/baseball",
///                            &serde_json::builder::ObjectBuilder::new()
///                                 .insert("name", name)
///                                 .insert("home_runs", home_runs)
///                                 .unwrap())
///           .run()
///           .unwrap();
/// };
///
/// create_player("Babe Ruth", 714);
/// create_player("Hank Aaron", 755);
/// create_player("Willie Mays", 660);
///
/// client.create_document("/baseball", {
///           &serde_json::builder::ObjectBuilder::new()
///                .insert_object("views", |x| {
///                    x.insert_object("home_run", |x| {
///                        x.insert("map", r#"function(doc) { emit(doc.home_runs, doc.name); }"#)
///                    })
///                })
///                .unwrap()
///       })
///       .with_document_id("_design/stat")
///       .run()
///       .unwrap();
///
/// // Execute a view to get players with at least 700 home runs.
///
/// let view_response = client.execute_view(
///                               "/baseball/_design/stat/_view/home_run")
///                           .with_descending(true)
///                           .with_end_key_inclusive(&700)
///                           .run()
///                           .unwrap();
///
/// let expected = vec![
///     "Hank Aaron - 755",
///     "Babe Ruth - 714",
/// ];
///
/// let got = view_response.rows()
///                        .iter()
///                        .map(|x| format!("{} - {}",
///                                        x.value::<String>().unwrap(),
///                                        x.key::<i32>().unwrap().unwrap()))
///                        .collect::<Vec<_>>();
///
/// assert_eq!(expected, got);
/// ```
///
pub struct ExecuteView<'a, T, P, StartKey, EndKey>
    where EndKey: serde::Serialize,
          P: IntoViewPath,
          StartKey: serde::Serialize,
          T: Transport + 'a
{
    transport: &'a T,
    view_path: Option<P>,
    reduce: Option<bool>,
    start_key: Option<StartKey>,
    end_key: Option<(EndKey, Inclusivity)>,
    limit: Option<u64>,
    descending: Option<bool>,
    group_level: Option<GroupLevel>,
    include_docs: Option<bool>,
}

impl<'a, P, T> ExecuteView<'a, T, P, (), ()>
    where P: IntoViewPath,
          T: Transport + 'a
{
    #[doc(hidden)]
    pub fn new(transport: &'a T, view_path: P) -> Self {
        ExecuteView {
            transport: transport,
            view_path: Some(view_path),
            reduce: None,
            start_key: None,
            end_key: None,
            limit: None,
            descending: None,
            group_level: None,
            include_docs: None,
        }
    }
}

impl<'a, EndKey, P, StartKey, T> ExecuteView<'a, T, P, StartKey, EndKey>
    where EndKey: serde::Serialize,
          P: IntoViewPath,
          StartKey: serde::Serialize,
          T: Transport + 'a
{
    /// Modifies the action to explicitly reduce or not reduce the view.
    ///
    /// The `with_reduce` method abstracts CouchDB's `reduce` query parameter.
    /// By default, CouchDB reduces a view if and only if the view contains a
    /// reduction function. Consequently, an application may use this method to
    /// disable reduction of a view that contains a reduction function.
    ///
    pub fn with_reduce(mut self, reduce: bool) -> Self {
        self.reduce = Some(reduce);
        self
    }
    /// Modifies the action to retrieve at most a given number of documents.
    ///
    /// The `with_limit` method abstracts CouchDB's `limit` query parameter. By
    /// default, the CouchDB server sends all rows.
    ///
    /// This method has no effect if the view is reduced.
    ///
    pub fn with_limit(mut self, limit: u64) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Modifies the action to retrieve the view rows in descending order.
    ///
    /// The `with_descending` method abstracts CouchDB's `descending` query
    /// parameter. By default, the CouchDB server sends the rows of an unreduced
    /// view in ascending order, sorted by key. Whereas, if the `descending`
    /// query parameter is `true`, then the server sends the rows in reverse
    /// order.
    ///
    /// This method has no effect if the view is reduced.
    ///
    pub fn with_descending(mut self, descending: bool) -> Self {
        self.descending = Some(descending);
        self
    }

    pub fn with_exact_groups(mut self, yes_or_no: bool) -> Self {
        self.group_level = Some(GroupLevel::Exact(yes_or_no));
        self
    }

    pub fn with_group_level(mut self, group_level: u32) -> Self {
        self.group_level = Some(GroupLevel::Number(group_level));
        self
    }

    pub fn with_documents(mut self, yes_or_no: bool) -> Self {
        self.include_docs = Some(yes_or_no);
        self
    }
}

impl<'a, EndKey, P, T> ExecuteView<'a, T, P, (), EndKey>
    where EndKey: serde::Serialize,
          P: IntoViewPath,
          T: Transport + 'a
{
    /// Modifies the action to include only records with a key greater than or
    /// equal to a given key.
    ///
    /// The `with_start_key` method abstracts CouchDB's `startkey` query
    /// parameter. By default, the CouchDB server includes all records.
    ///
    pub fn with_start_key<StartKey>(self, start_key: StartKey) -> ExecuteView<'a, T, P, StartKey, EndKey>
        where StartKey: serde::Serialize
    {
        ExecuteView {
            transport: self.transport,
            view_path: self.view_path,
            reduce: self.reduce,
            start_key: Some(start_key),
            end_key: self.end_key,
            limit: self.limit,
            descending: self.descending,
            group_level: self.group_level,
            include_docs: self.include_docs,
        }
    }
}

impl<'a, P, StartKey, T> ExecuteView<'a, T, P, StartKey, ()>
    where P: IntoViewPath,
          StartKey: serde::Serialize,
          T: Transport + 'a
{
    /// Modifies the action to include only records with a key less than or
    /// equal to a given key.
    ///
    /// The `with_end_key_inclusive` method abstracts CouchDB's `endkey` query
    /// parameter. By default, the CouchDB server includes all records.
    ///
    pub fn with_end_key_inclusive<EndKey>(self, end_key: EndKey) -> ExecuteView<'a, T, P, StartKey, EndKey>
        where EndKey: serde::Serialize
    {
        ExecuteView {
            transport: self.transport,
            view_path: self.view_path,
            reduce: self.reduce,
            start_key: self.start_key,
            end_key: Some((end_key, Inclusivity::Inclusive)),
            limit: self.limit,
            descending: self.descending,
            group_level: self.group_level,
            include_docs: self.include_docs,
        }
    }

    /// Modifies the action to include only records with a key less than a given
    /// key.
    ///
    /// The `with_end_key_exclusive` method abstracts CouchDB's `endkey` and
    /// `inclusive_end` query parameters. By default, the CouchDB server
    /// includes all records.
    ///
    pub fn with_end_key_exclusive<EndKey>(self, end_key: EndKey) -> ExecuteView<'a, T, P, StartKey, EndKey>
        where EndKey: serde::Serialize
    {
        ExecuteView {
            transport: self.transport,
            view_path: self.view_path,
            reduce: self.reduce,
            start_key: self.start_key,
            end_key: Some((end_key, Inclusivity::Exclusive)),
            limit: self.limit,
            descending: self.descending,
            group_level: self.group_level,
            include_docs: self.include_docs,
        }
    }
}

impl<'a, EndKey, P, StartKey, T> ExecuteView<'a, T, P, StartKey, EndKey>
    where EndKey: serde::Serialize,
          P: IntoViewPath,
          StartKey: serde::Serialize,
          T: Transport
{
    pub fn run(mut self) -> Result<ViewResponse, Error> {
        let (request, db_name) = try!(self.make_request());
        self.transport.send(request,
                            JsonResponseDecoder::new(move |response| handle_response(response, db_name)))
    }

    fn make_request(&mut self) -> Result<(Request, DatabaseName), Error> {

        let view_path = try!(std::mem::replace(&mut self.view_path, None).unwrap().into_view_path());
        let db_name = view_path.database_name().clone();

        let request = self.transport.get(view_path.iter()).with_accept_json();

        let request = match self.reduce {
            None => request,
            Some(ref yes_or_no) => request.with_query(ReduceQueryKey, yes_or_no),
        };

        let request = match self.start_key {
            None => request,
            Some(ref key) => try!(request.with_query_fallible(StartKeyQueryKey, key)),
        };

        let request = match self.end_key {
            None => request,
            Some((ref key, Inclusivity::Inclusive)) => try!(request.with_query_fallible(EndKeyQueryKey, key)),
            Some((ref key, Inclusivity::Exclusive)) => {
                try!(request.with_query_fallible(EndKeyQueryKey, key)).with_query(InclusiveEndQueryKey, &false)
            }
        };

        let request = match self.limit {
            None => request,
            Some(ref limit) => request.with_query(LimitQueryKey, limit),
        };

        let request = match self.descending {
            None => request,
            Some(ref yes_or_no) => request.with_query(DescendingQueryKey, yes_or_no),
        };

        let request = match self.group_level {
            None => request,
            Some(GroupLevel::Exact(ref yes_or_no)) => request.with_query(GroupQueryKey, yes_or_no),
            Some(GroupLevel::Number(ref group_level)) => request.with_query(GroupLevelQueryKey, group_level),
        };

        let request = match self.include_docs {
            None => request,
            Some(ref yes_or_no) => request.with_query(IncludeDocsQueryKey, yes_or_no),
        };

        Ok((request, db_name))
    }
}

fn handle_response(response: JsonResponse, db_name: DatabaseName) -> Result<ViewResponse, Error> {
    match response.status_code() {
        StatusCode::Ok => {
            let body: ViewResponseJsonable = try!(response.decode_content());
            Ok(ViewResponse::new_from_decoded(db_name, body))
        }
        StatusCode::NotFound => Err(Error::not_found(&response)),
        StatusCode::Unauthorized => Err(Error::unauthorized(&response)),
        _ => Err(Error::server_response(&response)),
    }
}

#[cfg(test)]
mod tests {

    use {DatabaseName, Error};
    use super::*;
    use transport::{JsonResponseBuilder, MockTransport, StatusCode, Transport};
    use view::ViewResponseBuilder;

    #[test]
    fn make_request_default() {

        let transport = MockTransport::new();
        let expected = (transport.get(vec!["foo", "_design", "bar", "_view", "qux"]).with_accept_json(),
                        DatabaseName::from("foo"));

        let got = {
            let mut action = ExecuteView::new(&transport, "/foo/_design/bar/_view/qux");
            action.make_request().unwrap()
        };

        assert_eq!(expected, got);
    }

    #[test]
    fn make_request_with_descending() {

        let transport = MockTransport::new();
        let expected = (transport.get(vec!["foo", "_design", "bar", "_view", "qux"])
            .with_accept_json()
            .with_query_literal("descending", "true"),
                        DatabaseName::from("foo"));

        let got = {
            let mut action = ExecuteView::new(&transport, "/foo/_design/bar/_view/qux").with_descending(true);
            action.make_request().unwrap()
        };

        assert_eq!(expected, got);
    }

    #[test]
    fn make_request_with_end_key_exclusive() {

        let transport = MockTransport::new();

        let expected = (transport.get(vec!["foo", "_design", "bar", "_view", "qux"])
            .with_accept_json()
            .with_query_literal("endkey", r#""my key""#)
            .with_query_literal("inclusive_end", "false"),
                        DatabaseName::from("foo"));

        let end_key = String::from("my key");
        let got = {
            let mut action = ExecuteView::new(&transport, "/foo/_design/bar/_view/qux")
                .with_end_key_exclusive(&end_key);
            action.make_request().unwrap()
        };

        assert_eq!(expected, got);
    }

    #[test]
    fn make_request_with_end_key_inclusive() {

        let transport = MockTransport::new();

        let expected = (transport.get(vec!["foo", "_design", "bar", "_view", "qux"])
            .with_accept_json()
            .with_query_literal("endkey", r#""my key""#),
                        DatabaseName::from("foo"));

        let end_key = String::from("my key");
        let got = {
            let mut action = ExecuteView::new(&transport, "/foo/_design/bar/_view/qux")
                .with_end_key_inclusive(&end_key);
            action.make_request().unwrap()
        };

        assert_eq!(expected, got);
    }

    #[test]
    fn make_request_with_exact_groups() {

        let transport = MockTransport::new();

        let expected = (transport.get(vec!["foo", "_design", "bar", "_view", "qux"])
            .with_accept_json()
            .with_query_literal("group", "true"),
                        DatabaseName::from("foo"));

        let got = {
            let mut action = ExecuteView::new(&transport, "/foo/_design/bar/_view/qux").with_exact_groups(true);
            action.make_request().unwrap()
        };

        assert_eq!(expected, got);
    }

    #[test]
    fn make_request_with_group_level() {

        let transport = MockTransport::new();

        let expected = (transport.get(vec!["foo", "_design", "bar", "_view", "qux"])
            .with_accept_json()
            .with_query_literal("group_level", "42"),
                        DatabaseName::from("foo"));

        let got = {
            let mut action = ExecuteView::new(&transport, "/foo/_design/bar/_view/qux").with_group_level(42);
            action.make_request().unwrap()
        };

        assert_eq!(expected, got);
    }

    #[test]
    fn make_request_with_limit() {
        let transport = MockTransport::new();

        let expected = (transport.get(vec!["foo", "_design", "bar", "_view", "qux"])
            .with_accept_json()
            .with_query_literal("limit", "42"),
                        DatabaseName::from("foo"));

        let got = {
            let mut action = ExecuteView::new(&transport, "/foo/_design/bar/_view/qux").with_limit(42);
            action.make_request().unwrap()
        };

        assert_eq!(expected, got);
    }

    #[test]
    fn make_request_with_reduce() {
        let transport = MockTransport::new();

        let expected = (transport.get(vec!["foo", "_design", "bar", "_view", "qux"])
            .with_accept_json()
            .with_query_literal("reduce", "false"),
                        DatabaseName::from("foo"));

        let got = {
            let mut action = ExecuteView::new(&transport, "/foo/_design/bar/_view/qux").with_reduce(false);
            action.make_request().unwrap()
        };

        assert_eq!(expected, got);
    }

    #[test]
    fn make_request_with_start_key() {
        let transport = MockTransport::new();

        let expected = (transport.get(vec!["foo", "_design", "bar", "_view", "qux"])
            .with_accept_json()
            .with_query_literal("startkey", r#""my key""#),
                        DatabaseName::from("foo"));

        let start_key = String::from("my key");
        let got = {
            let mut action = ExecuteView::new(&transport, "/foo/_design/bar/_view/qux").with_start_key(start_key);
            action.make_request().unwrap()
        };

        assert_eq!(expected, got);
    }

    #[test]
    fn handle_response_ok_reduced() {

        let response = JsonResponseBuilder::new(StatusCode::Ok)
            .with_json_content_raw(r#"{"rows":[{"key":null,"value":42}]}"#)
            .unwrap();

        let expected = ViewResponseBuilder::new_reduced(42).unwrap();
        let db_name = DatabaseName::from("baseball");
        let got = super::handle_response(response, db_name).unwrap();
        assert_eq!(expected, got);
    }

    #[test]
    fn handle_response_ok_unreduced() {

        let response = JsonResponseBuilder::new(StatusCode::Ok)
            .with_json_content_raw("{\"total_rows\":20,\"offset\":10,\"rows\":[\
                                   {\"key\":\"Babe Ruth\",\"value\":714,\"id\":\"babe_ruth\"},\
                                   {\"key\":\"Hank Aaron\",\"value\":755,\"id\":\"hank_aaron\"}]}")
            .unwrap();

        let expected = ViewResponseBuilder::new_unreduced("baseball", 20, 10)
            .with_row("babe_ruth", "Babe Ruth", 714)
            .with_row("hank_aaron", "Hank Aaron", 755)
            .unwrap();

        let db_name = DatabaseName::from("baseball");
        let got = super::handle_response(response, db_name).unwrap();
        assert_eq!(expected, got);
    }

    #[test]
    fn handle_response_not_found() {

        let response = JsonResponseBuilder::new(StatusCode::NotFound)
            .with_json_content_raw(r#"{"error":"not_found","reason":"missing_named_view"}"#)
            .unwrap();

        let db_name = DatabaseName::from("foo");
        match super::handle_response(response, db_name) {
            Err(Error::NotFound(ref error_response)) if error_response.error() == "not_found" &&
                                                        error_response.reason() == "missing_named_view" => (),
            x @ _ => unexpected_result!(x),
        }
    }

    #[test]
    fn handle_response_unauthorized() {

        let response = JsonResponseBuilder::new(StatusCode::Unauthorized)
            .with_json_content_raw(r#"{"error":"unauthorized","reason":"Authentication required."}"#)
            .unwrap();

        let db_name = DatabaseName::from("foo");
        match super::handle_response(response, db_name) {
            Err(Error::Unauthorized(ref error_response)) if error_response.error() == "unauthorized" &&
                                                            error_response.reason() == "Authentication required." => (),
            x @ _ => unexpected_result!(x),
        }
    }
}