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
use serde;
use std;

use DatabaseName;

/// Database meta-information, as returned from an action to GET a database.
///
/// Although the `Database` type implements the `Ord` and `PartialOrd` traits,
/// it provides no guarantees how that ordering is defined and may change the
/// definition between any two releases of the couchdb crate. That is, for two
/// `Database` values `a` and `b`, the expression `a < b` may hold true now but
/// not in a subsequent release. Consequently, applications must not rely upon
/// any particular ordering definition.
///
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Database {
    /// The name of the database.
    pub db_name: DatabaseName,

    /// The current number of updates to the database.
    pub update_seq: u64,

    /// The number of committed updates.
    pub committed_update_seq: u64,

    /// Timestamp of when the database was opened, expressed in microseconds
    /// since the epoch.
    pub instance_start_time: u64,

    /// The version of the physical format used for the data when it is stored
    /// on disk.
    pub disk_format_version: i32,

    /// Number of documents in the database.
    pub doc_count: u64,

    /// Number of deleted documents.
    pub doc_del_count: u64,

    /// Actual data size, in bytes, of the database data.
    pub data_size: u64,

    /// Size, in bytes, of the data as stored on the disk. Views indexes are not
    /// included in the calculation.
    pub disk_size: u64,

    /// The number of purge operations on the database.
    pub purge_seq: u64,

    /// Set to true if the database compaction routine is operating on this
    /// database.
    pub compact_running: bool,

    // Include a private field to prevent applications from directly
    // constructing this struct. This allows us to add new fields without
    // breaking applications.
    _dummy: std::marker::PhantomData<()>,
}

impl serde::Deserialize for Database {
    fn deserialize<D>(d: &mut D) -> Result<Self, D::Error>
        where D: serde::Deserializer
    {
        enum Field {
            CommittedUpdateSeq,
            CompactRunning,
            DbName,
            DiskFormatVersion,
            DataSize,
            DiskSize,
            DocCount,
            DocDelCount,
            InstanceStartTime,
            PurgeSeq,
            UpdateSeq,
        }

        impl serde::Deserialize for Field {
            fn deserialize<D>(d: &mut D) -> Result<Field, D::Error>
                where D: serde::Deserializer
            {
                struct Visitor;

                impl serde::de::Visitor for Visitor {
                    type Value = Field;

                    fn visit_str<E>(&mut self, value: &str) -> Result<Field, E>
                        where E: serde::de::Error
                    {
                        match value {
                            "committed_update_seq" => Ok(Field::CommittedUpdateSeq),
                            "compact_running" => Ok(Field::CompactRunning),
                            "db_name" => Ok(Field::DbName),
                            "disk_format_version" => Ok(Field::DiskFormatVersion),
                            "data_size" => Ok(Field::DataSize),
                            "disk_size" => Ok(Field::DiskSize),
                            "doc_count" => Ok(Field::DocCount),
                            "doc_del_count" => Ok(Field::DocDelCount),
                            "instance_start_time" => Ok(Field::InstanceStartTime),
                            "purge_seq" => Ok(Field::PurgeSeq),
                            "update_seq" => Ok(Field::UpdateSeq),
                            _ => Err(E::unknown_field(value)),
                        }
                    }
                }

                d.visit(Visitor)
            }
        }

        struct Visitor;

        impl serde::de::Visitor for Visitor {
            type Value = Database;

            fn visit_map<V>(&mut self, mut visitor: V) -> Result<Self::Value, V::Error>
                where V: serde::de::MapVisitor
            {
                let mut committed_update_seq = None;
                let mut compact_running = None;
                let mut db_name = None;
                let mut disk_format_version = None;
                let mut data_size = None;
                let mut disk_size = None;
                let mut doc_count = None;
                let mut doc_del_count = None;
                let mut instance_start_time = None;
                let mut purge_seq = None;
                let mut update_seq = None;
                loop {
                    match try!(visitor.visit_key()) {
                        Some(Field::CommittedUpdateSeq) => {
                            committed_update_seq = Some(try!(visitor.visit_value()));
                        }
                        Some(Field::CompactRunning) => {
                            compact_running = Some(try!(visitor.visit_value()));
                        }
                        Some(Field::DbName) => {
                            db_name = Some(try!(visitor.visit_value()));
                        }
                        Some(Field::DiskFormatVersion) => {
                            disk_format_version = Some(try!(visitor.visit_value()));
                        }
                        Some(Field::DataSize) => {
                            data_size = Some(try!(visitor.visit_value()));
                        }
                        Some(Field::DiskSize) => {
                            disk_size = Some(try!(visitor.visit_value()));
                        }
                        Some(Field::DocCount) => {
                            doc_count = Some(try!(visitor.visit_value()));
                        }
                        Some(Field::DocDelCount) => {
                            doc_del_count = Some(try!(visitor.visit_value()));
                        }
                        Some(Field::InstanceStartTime) => {
                            instance_start_time = Some(try!(visitor.visit_value::<String>()));
                        }
                        Some(Field::PurgeSeq) => {
                            purge_seq = Some(try!(visitor.visit_value()));
                        }
                        Some(Field::UpdateSeq) => {
                            update_seq = Some(try!(visitor.visit_value()));
                        }
                        None => {
                            break;
                        }
                    }
                }

                try!(visitor.end());

                let committed_update_seq = match committed_update_seq {
                    Some(x) => x,
                    None => try!(visitor.missing_field("committed_update_seq")),
                };

                let compact_running = match compact_running {
                    Some(x) => x,
                    None => try!(visitor.missing_field("compact_running")),
                };

                let db_name = match db_name {
                    Some(x) => x,
                    None => try!(visitor.missing_field("db_name")),
                };

                let disk_format_version = match disk_format_version {
                    Some(x) => x,
                    None => try!(visitor.missing_field("disk_format_version")),
                };

                let data_size = match data_size {
                    Some(x) => x,
                    None => try!(visitor.missing_field("data_size")),
                };

                let disk_size = match disk_size {
                    Some(x) => x,
                    None => try!(visitor.missing_field("disk_size")),
                };

                let doc_count = match doc_count {
                    Some(x) => x,
                    None => try!(visitor.missing_field("doc_count")),
                };

                let doc_del_count = match doc_del_count {
                    Some(x) => x,
                    None => try!(visitor.missing_field("doc_del_count")),
                };

                let instance_start_time = match instance_start_time {
                    Some(x) => {
                        try!(u64::from_str_radix(&x, 10).map_err(|e| {
                            use std::error::Error;
                            use serde::de::Error as SerdeError;
                            V::Error::invalid_value(e.description())
                        }))
                    }
                    None => try!(visitor.missing_field("instance_start_time")),
                };

                let purge_seq = match purge_seq {
                    Some(x) => x,
                    None => try!(visitor.missing_field("purge_seq")),
                };

                let update_seq = match update_seq {
                    Some(x) => x,
                    None => try!(visitor.missing_field("update_seq")),
                };

                Ok(Database {
                    _dummy: std::marker::PhantomData,
                    committed_update_seq: committed_update_seq,
                    compact_running: compact_running,
                    db_name: db_name,
                    disk_format_version: disk_format_version,
                    data_size: data_size,
                    disk_size: disk_size,
                    doc_count: doc_count,
                    doc_del_count: doc_del_count,
                    instance_start_time: instance_start_time,
                    purge_seq: purge_seq,
                    update_seq: update_seq,
                })
            }
        }

        static FIELDS: &'static [&'static str] = &["committed_update_seq",
                                                   "compact_running",
                                                   "db_name",
                                                   "disk_format_version",
                                                   "data_size",
                                                   "disk_size",
                                                   "doc_count",
                                                   "doc_del_count",
                                                   "instance_start_time",
                                                   "purge_seq",
                                                   "update_seq"];
        d.visit_struct("Database", FIELDS, Visitor)
    }
}

#[cfg(test)]
mod tests {

    use serde_json;
    use std;

    use Database;
    use DatabaseName;

    #[test]
    fn database_deserialization_with_all_fields() {
        let expected = Database {
            _dummy: std::marker::PhantomData,
            committed_update_seq: 1,
            compact_running: true,
            db_name: DatabaseName::from("foo"),
            disk_format_version: 2,
            data_size: 3,
            disk_size: 4,
            doc_count: 5,
            doc_del_count: 6,
            instance_start_time: 7,
            purge_seq: 8,
            update_seq: 9,
        };
        let source = serde_json::builder::ObjectBuilder::new()
                         .insert("committed_update_seq", 1)
                         .insert("compact_running", true)
                         .insert("db_name", "foo")
                         .insert("disk_format_version", 2)
                         .insert("data_size", 3)
                         .insert("disk_size", 4)
                         .insert("doc_count", 5)
                         .insert("doc_del_count", 6)
                         .insert("instance_start_time", "7")
                         .insert("purge_seq", 8)
                         .insert("update_seq", 9)
                         .unwrap();
        let s = serde_json::to_string(&source).unwrap();
        let got = serde_json::from_str(&s).unwrap();
        assert_eq!(expected, got);
    }

    #[test]
    fn database_deserialization_with_no_committed_update_seq_field() {
        let source = serde_json::builder::ObjectBuilder::new()
                         .insert("compact_running", true)
                         .insert("db_name", "foo")
                         .insert("disk_format_version", 2)
                         .insert("data_size", 3)
                         .insert("disk_size", 4)
                         .insert("doc_count", 5)
                         .insert("doc_del_count", 6)
                         .insert("instance_start_time", "7")
                         .insert("purge_seq", 8)
                         .insert("update_seq", 9)
                         .unwrap();
        let s = serde_json::to_string(&source).unwrap();
        let got = serde_json::from_str::<Database>(&s);
        expect_json_error_missing_field!(got, "committed_update_seq");
    }

    #[test]
    fn database_deserialization_with_no_compact_running_field() {
        let source = serde_json::builder::ObjectBuilder::new()
                         .insert("committed_update_seq", 1)
                         .insert("db_name", "foo")
                         .insert("disk_format_version", 2)
                         .insert("data_size", 3)
                         .insert("disk_size", 4)
                         .insert("doc_count", 5)
                         .insert("doc_del_count", 6)
                         .insert("instance_start_time", "7")
                         .insert("purge_seq", 8)
                         .insert("update_seq", 9)
                         .unwrap();
        let s = serde_json::to_string(&source).unwrap();
        let got = serde_json::from_str::<Database>(&s);
        expect_json_error_missing_field!(got, "compact_running");
    }

    #[test]
    fn database_deserialization_with_no_db_name_field() {
        let source = serde_json::builder::ObjectBuilder::new()
                         .insert("committed_update_seq", 1)
                         .insert("compact_running", true)
                         .insert("disk_format_version", 2)
                         .insert("data_size", 3)
                         .insert("disk_size", 4)
                         .insert("doc_count", 5)
                         .insert("doc_del_count", 6)
                         .insert("instance_start_time", "7")
                         .insert("purge_seq", 8)
                         .insert("update_seq", 9)
                         .unwrap();
        let s = serde_json::to_string(&source).unwrap();
        let got = serde_json::from_str::<Database>(&s);
        expect_json_error_missing_field!(got, "db_name");
    }

    #[test]
    fn database_deserialization_with_no_disk_format_version_field() {
        let source = serde_json::builder::ObjectBuilder::new()
                         .insert("committed_update_seq", 1)
                         .insert("compact_running", true)
                         .insert("db_name", "foo")
                         .insert("data_size", 3)
                         .insert("disk_size", 4)
                         .insert("doc_count", 5)
                         .insert("doc_del_count", 6)
                         .insert("instance_start_time", "7")
                         .insert("purge_seq", 8)
                         .insert("update_seq", 9)
                         .unwrap();
        let s = serde_json::to_string(&source).unwrap();
        let got = serde_json::from_str::<Database>(&s);
        expect_json_error_missing_field!(got, "disk_format_version");
    }

    #[test]
    fn database_deserialization_with_no_data_size_field() {
        let source = serde_json::builder::ObjectBuilder::new()
                         .insert("committed_update_seq", 1)
                         .insert("compact_running", true)
                         .insert("db_name", "foo")
                         .insert("disk_format_version", 2)
                         .insert("disk_size", 4)
                         .insert("doc_count", 5)
                         .insert("doc_del_count", 6)
                         .insert("instance_start_time", "7")
                         .insert("purge_seq", 8)
                         .insert("update_seq", 9)
                         .unwrap();
        let s = serde_json::to_string(&source).unwrap();
        let got = serde_json::from_str::<Database>(&s);
        expect_json_error_missing_field!(got, "data_size");
    }

    #[test]
    fn database_deserialization_with_no_disk_size_field() {
        let source = serde_json::builder::ObjectBuilder::new()
                         .insert("committed_update_seq", 1)
                         .insert("compact_running", true)
                         .insert("db_name", "foo")
                         .insert("disk_format_version", 2)
                         .insert("data_size", 3)
                         .insert("doc_count", 5)
                         .insert("doc_del_count", 6)
                         .insert("instance_start_time", "7")
                         .insert("purge_seq", 8)
                         .insert("update_seq", 9)
                         .unwrap();
        let s = serde_json::to_string(&source).unwrap();
        let got = serde_json::from_str::<Database>(&s);
        expect_json_error_missing_field!(got, "disk_size");
    }

    #[test]
    fn database_deserialization_with_no_doc_count_field() {
        let source = serde_json::builder::ObjectBuilder::new()
                         .insert("committed_update_seq", 1)
                         .insert("compact_running", true)
                         .insert("db_name", "foo")
                         .insert("disk_format_version", 2)
                         .insert("data_size", 3)
                         .insert("disk_size", 4)
                         .insert("doc_del_count", 6)
                         .insert("instance_start_time", "7")
                         .insert("purge_seq", 8)
                         .insert("update_seq", 9)
                         .unwrap();
        let s = serde_json::to_string(&source).unwrap();
        let got = serde_json::from_str::<Database>(&s);
        expect_json_error_missing_field!(got, "doc_count");
    }

    #[test]
    fn database_deserialization_with_no_doc_del_count_field() {
        let source = serde_json::builder::ObjectBuilder::new()
                         .insert("committed_update_seq", 1)
                         .insert("compact_running", true)
                         .insert("db_name", "foo")
                         .insert("disk_format_version", 2)
                         .insert("data_size", 3)
                         .insert("disk_size", 4)
                         .insert("doc_count", 5)
                         .insert("instance_start_time", "7")
                         .insert("purge_seq", 8)
                         .insert("update_seq", 9)
                         .unwrap();
        let s = serde_json::to_string(&source).unwrap();
        let got = serde_json::from_str::<Database>(&s);
        expect_json_error_missing_field!(got, "doc_del_count");
    }

    #[test]
    fn database_deserialization_with_no_instance_start_time_field() {
        let source = serde_json::builder::ObjectBuilder::new()
                         .insert("committed_update_seq", 1)
                         .insert("compact_running", true)
                         .insert("db_name", "foo")
                         .insert("disk_format_version", 2)
                         .insert("data_size", 3)
                         .insert("disk_size", 4)
                         .insert("doc_count", 5)
                         .insert("doc_del_count", 6)
                         .insert("purge_seq", 8)
                         .insert("update_seq", 9)
                         .unwrap();
        let s = serde_json::to_string(&source).unwrap();
        let got = serde_json::from_str::<Database>(&s);
        expect_json_error_missing_field!(got, "instance_start_time");
    }

    #[test]
    fn database_deserialization_with_bad_instance_start_time_field() {
        let source = serde_json::builder::ObjectBuilder::new()
                         .insert("committed_update_seq", 1)
                         .insert("compact_running", true)
                         .insert("db_name", "foo")
                         .insert("disk_format_version", 2)
                         .insert("data_size", 3)
                         .insert("disk_size", 4)
                         .insert("doc_count", 5)
                         .insert("doc_del_count", 6)
                         .insert("instance_start_time", "not_a_number")
                         .insert("purge_seq", 8)
                         .insert("update_seq", 9)
                         .unwrap();
        let s = serde_json::to_string(&source).unwrap();
        let got = serde_json::from_str::<Database>(&s);
        expect_json_error_invalid_value!(got);
    }

    #[test]
    fn database_deserialization_with_no_purge_seq_field() {
        let source = serde_json::builder::ObjectBuilder::new()
                         .insert("committed_update_seq", 1)
                         .insert("compact_running", true)
                         .insert("db_name", "foo")
                         .insert("disk_format_version", 2)
                         .insert("data_size", 3)
                         .insert("disk_size", 4)
                         .insert("doc_count", 5)
                         .insert("doc_del_count", 6)
                         .insert("instance_start_time", "7")
                         .insert("update_seq", 9)
                         .unwrap();
        let s = serde_json::to_string(&source).unwrap();
        let got = serde_json::from_str::<Database>(&s);
        expect_json_error_missing_field!(got, "purge_seq");
    }

    #[test]
    fn database_deserialization_with_no_update_seq_field() {
        let source = serde_json::builder::ObjectBuilder::new()
                         .insert("committed_update_seq", 1)
                         .insert("compact_running", true)
                         .insert("db_name", "foo")
                         .insert("disk_format_version", 2)
                         .insert("data_size", 3)
                         .insert("disk_size", 4)
                         .insert("doc_count", 5)
                         .insert("doc_del_count", 6)
                         .insert("instance_start_time", "7")
                         .insert("purge_seq", 8)
                         .unwrap();
        let s = serde_json::to_string(&source).unwrap();
        let got = serde_json::from_str::<Database>(&s);
        expect_json_error_missing_field!(got, "update_seq");
    }
}