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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
// Copyright 2020-2022 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Support for Key Value Store.
//! This feature is experimental and the API may change.

use std::io;
use std::time::Duration;

use crate::header::{self, HeaderMap};
use crate::jetstream::{
    DateTime, DiscardPolicy, Error, ErrorCode, JetStream, PushSubscription, StorageType,
    StreamConfig, StreamInfo, StreamMessage, SubscribeOptions,
};
use crate::message::Message;
use lazy_static::lazy_static;
use regex::Regex;

/// Configuration values for key value stores.
#[derive(Debug, Default)]
pub struct Config {
    /// Name of the bucket
    pub bucket: String,
    /// Human readable description.
    pub description: String,
    /// Maximum size of a single value.
    pub max_value_size: i32,
    /// Maximum historical entries.
    pub history: i64,
    /// Maximum age of any entry in the bucket, expressed in nanoseconds
    pub max_age: Duration,
    /// How large the bucket may become in total bytes before the configured discard policy kicks in
    pub max_bytes: i64,
    /// The type of storage backend, `File` (default) and `Memory`
    pub storage: StorageType,
    /// How many replicas to keep for each entry in a cluster.
    pub num_replicas: usize,
}

const MAX_HISTORY: i64 = 64;
const ALL_KEYS: &str = ">";

const KV_OPERATION: &str = "KV-Operation";
const KV_OPERATION_DELETE: &str = "DEL";
const KV_OPERATION_PURGE: &str = "PURGE";

const NATS_ROLLUP: &str = "Nats-Rollup";
const ROLLUP_SUBJECT: &str = "sub";

/// Describes what kind of operation and entry represents
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Operation {
    /// A value was put into the bucket
    Put,
    /// A value was deleted from a bucket
    Delete,
    /// A value was purged from a bucket
    Purge,
}

// Helper to extract key value operation from message headers
fn kv_operation_from_maybe_headers(maybe_headers: Option<&HeaderMap>) -> Operation {
    if let Some(headers) = maybe_headers {
        if let Some(op) = headers.get(KV_OPERATION) {
            return match op.as_str() {
                KV_OPERATION_DELETE => Operation::Delete,
                KV_OPERATION_PURGE => Operation::Purge,
                _ => Operation::Put,
            };
        }
    }

    Operation::Put
}

fn kv_operation_from_stream_message(message: &StreamMessage) -> Operation {
    kv_operation_from_maybe_headers(message.headers.as_ref())
}

lazy_static! {
    static ref VALID_BUCKET_RE: Regex = Regex::new(r#"\A[a-zA-Z0-9_-]+\z"#).unwrap();
    static ref VALID_KEY_RE: Regex = Regex::new(r#"\A[-/_=\.a-zA-Z0-9]+\z"#).unwrap();
}

fn is_valid_bucket_name(bucket_name: &str) -> bool {
    VALID_BUCKET_RE.is_match(bucket_name)
}

fn is_valid_key(key: &str) -> bool {
    if key.is_empty() || key.starts_with('.') || key.ends_with('.') {
        return false;
    }

    VALID_KEY_RE.is_match(key)
}

impl JetStream {
    /// Bind to an existing key-value store bucket.
    ///
    /// # Example
    ///
    /// ```
    /// # use nats::kv::Config;
    /// # fn main() -> std::io::Result<()> {
    /// # let client = nats::connect("demo.nats.io")?;
    /// # let context = nats::jetstream::new(client);
    /// #
    /// context.create_key_value(&Config {
    ///   bucket: "key_value".to_string(),
    ///   ..Default::default()
    /// })?;
    ///
    /// let key_value = context.key_value("key_value")?;
    ///
    /// # context.delete_key_value("key_value")?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn key_value(&self, bucket: &str) -> io::Result<Store> {
        if !self.connection.is_server_compatible_version(2, 6, 2) {
            return Err(io::Error::new(
                io::ErrorKind::Other,
                "key-value requires at least server version 2.6.2",
            ));
        }

        if !is_valid_bucket_name(bucket) {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "invalid bucket name",
            ));
        }

        let stream_name = format!("KV_{}", bucket);
        let stream_info = self.stream_info(&stream_name)?;

        // Do some quick sanity checks that this is a correctly formed stream for KV.
        // Max msgs per subject should be > 0.
        if stream_info.config.max_msgs_per_subject < 1 {
            return Err(io::Error::new(
                io::ErrorKind::Other,
                "bucket not valid key-value store",
            ));
        }

        Ok(Store {
            name: bucket.to_string(),
            stream_name,
            prefix: format!("$KV.{}.", bucket),
            context: self.clone(),
            domain_prefix: self
                .options
                .has_domain
                .then(|| self.options.api_prefix.clone()),
        })
    }

    /// Create a new key-value store bucket.
    ///
    /// # Examples
    ///
    /// ```
    /// # use nats::kv::Config;
    /// # fn main() -> std::io::Result<()> {
    /// # let client = nats::connect("demo.nats.io")?;
    /// # let context = nats::jetstream::new(client);
    /// #
    /// let bucket = context.create_key_value(&Config {
    ///   bucket: "create_key_value".to_string(),
    ///   ..Default::default()
    /// })?;
    ///
    /// # context.delete_key_value("create_key_value")?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn create_key_value(&self, config: &Config) -> io::Result<Store> {
        if !self.connection.is_server_compatible_version(2, 6, 2) {
            return Err(io::Error::new(
                io::ErrorKind::Other,
                "key-value requires at least server version 2.6.2",
            ));
        }

        let discard_policy = {
            if self.connection.is_server_compatible_version(2, 7, 2) {
                DiscardPolicy::New
            } else {
                DiscardPolicy::Old
            }
        };

        if !is_valid_bucket_name(&config.bucket) {
            return Err(io::Error::new(io::ErrorKind::Other, "invalid bucket name"));
        }

        self.account_info()?;

        // Default to 1 for history. Max is 64 for now.
        let history = if config.history > 0 {
            if config.history > MAX_HISTORY {
                return Err(io::Error::new(
                    io::ErrorKind::Other,
                    "history limited to a max of 64",
                ));
            }

            config.history
        } else {
            1
        };

        let num_replicas = if config.num_replicas == 0 {
            1
        } else {
            config.num_replicas
        };

        let stream_info = self.add_stream(&StreamConfig {
            name: format!("KV_{}", config.bucket),
            description: Some(config.description.to_string()),
            subjects: vec![format!("$KV.{}.>", config.bucket)],
            max_msgs_per_subject: history,
            max_bytes: config.max_bytes,
            max_age: config.max_age,
            max_msg_size: config.max_value_size,
            storage: config.storage,
            allow_rollup: true,
            deny_delete: true,
            num_replicas,
            discard: discard_policy,
            ..Default::default()
        })?;

        Ok(Store {
            name: config.bucket.to_string(),
            stream_name: stream_info.config.name,
            prefix: format!("$KV.{}.", config.bucket),
            context: self.clone(),
            domain_prefix: self
                .options
                .has_domain
                .then(|| self.options.api_prefix.clone()),
        })
    }

    /// Delete the specified key value store bucket.
    ///
    /// # Example
    ///
    /// ```
    /// use nats::kv::Config;
    /// # fn main() -> std::io::Result<()> {
    /// # let client = nats::connect("demo.nats.io")?;
    /// # let context = nats::jetstream::new(client);
    /// #
    /// # let bucket = context.create_key_value(&Config {
    /// #  bucket: "delete_key_value".to_string(),
    /// #  ..Default::default()
    /// # })?;
    ///
    /// context.delete_key_value("delete_key_value")?;
    ///
    /// # Ok(())
    /// # }
    /// ```
    ///
    pub fn delete_key_value(&self, bucket: &str) -> io::Result<()> {
        if !self.connection.is_server_compatible_version(2, 6, 2) {
            return Err(io::Error::new(
                io::ErrorKind::Other,
                "key-value requires at least server version 2.6.2",
            ));
        }

        if !is_valid_bucket_name(bucket) {
            return Err(io::Error::new(io::ErrorKind::Other, "invalid bucket name"));
        }

        let stream_name = format!("KV_{}", bucket);
        self.delete_stream(&stream_name)?;

        Ok(())
    }
}

/// An entry in a key-value bucket.
#[derive(Debug, Clone)]
pub struct Entry {
    /// Name of the bucket the entry is in.
    pub bucket: String,
    /// The key that was retrieved.
    pub key: String,
    /// The value that was retreived.
    pub value: Vec<u8>,
    /// A unique sequence for this value.
    pub revision: u64,
    /// Distance from the latest value.
    pub delta: u64,
    /// The time the data was put in the bucket.
    pub created: DateTime,
    /// The kind of operation that caused this entry.
    pub operation: Operation,
}

/// A key value store
#[derive(Debug, Clone)]
pub struct Store {
    name: String,
    stream_name: String,
    prefix: String,
    context: JetStream,
    domain_prefix: Option<String>,
}

impl Store {
    /// Returns the status of the bucket
    pub fn status(&self) -> io::Result<BucketStatus> {
        let info = self.context.stream_info(&self.stream_name)?;

        Ok(BucketStatus {
            bucket: self.name.to_string(),
            info,
        })
    }

    /// Returns the latest entry for the key, if any.
    ///
    /// # Examples
    ///
    /// ```
    /// # use nats::kv::Config;
    /// # fn main() -> std::io::Result<()> {
    /// # let client = nats::connect("demo.nats.io")?;
    /// # let context = nats::jetstream::new(client);
    /// #
    /// # let bucket = context.create_key_value(&Config {
    /// #  bucket: "entry_bucket".to_string(),
    /// #  ..Default::default()
    /// # })?;
    /// #
    /// bucket.put("foo", b"bar")?;
    /// let maybe_entry = bucket.entry("foo")?;
    /// if let Some(entry) = maybe_entry {
    ///   println!("Found entry {:?}", entry);
    /// }
    /// #
    /// # Ok(())
    /// # }
    /// ```
    pub fn entry(&self, key: &str) -> io::Result<Option<Entry>> {
        if !is_valid_key(key) {
            return Err(io::Error::new(io::ErrorKind::InvalidInput, "invalid key"));
        }

        let mut subject = String::new();
        subject.push_str(&self.prefix);
        subject.push_str(key);

        match self.context.get_last_message(&self.stream_name, &subject) {
            Ok(message) => {
                let operation = kv_operation_from_stream_message(&message);
                let entry = Entry {
                    bucket: self.name.clone(),
                    key: key.to_string(),
                    value: message.data,
                    revision: message.sequence,
                    created: message.time,
                    operation,
                    delta: 0,
                };

                Ok(Some(entry))
            }
            Err(err) => {
                if let Some(inner_err) = err.get_ref() {
                    if let Some(error) = inner_err.downcast_ref::<Error>() {
                        if error.error_code() == ErrorCode::NoMessageFound {
                            return Ok(None);
                        }
                    }
                }

                Err(err)
            }
        }
    }

    /// Returns the latest value for the key, if any.
    ///
    /// # Examples
    ///
    /// ```
    /// # use nats::kv::Config;
    /// # fn main() -> std::io::Result<()> {
    /// # let client = nats::connect("demo.nats.io")?;
    /// # let context = nats::jetstream::new(client);
    /// #
    /// # let bucket = context.create_key_value(&Config {
    /// #  bucket: "get".to_string(),
    /// #  ..Default::default()
    /// # })?;
    /// #
    /// bucket.put("foo", b"bar")?;
    /// let maybe_value = bucket.get("foo")?;
    /// if let Some(value) = maybe_value {
    ///   println!("Found value {:?}", value);
    /// }
    /// #
    /// # context.delete_key_value("get")?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn get(&self, key: &str) -> io::Result<Option<Vec<u8>>> {
        match self.entry(key) {
            Ok(Some(entry)) => match entry.operation {
                Operation::Put => Ok(Some(entry.value)),
                _ => Ok(None),
            },
            Ok(None) => Ok(None),
            Err(err) => Err(err),
        }
    }

    /// Places the new value for the key into the bucket.
    ///
    /// # Examples
    ///
    /// ```
    /// # use nats::kv::Config;
    /// # fn main() -> std::io::Result<()> {
    /// # let client = nats::connect("demo.nats.io")?;
    /// # let context = nats::jetstream::new(client);
    /// #
    /// # let bucket = context.create_key_value(&Config {
    /// #  bucket: "put".to_string(),
    /// #  ..Default::default()
    /// # })?;
    /// #
    /// bucket.put("foo", b"bar")?;
    /// # context.delete_key_value("put")?;
    /// #
    /// # Ok(())
    /// # }
    /// ```
    pub fn put(&self, key: &str, value: impl AsRef<[u8]>) -> io::Result<u64> {
        if !is_valid_key(key) {
            return Err(io::Error::new(io::ErrorKind::InvalidInput, "invalid key"));
        }

        let mut subject = String::new();
        if let Some(api_prefix) = self.domain_prefix.as_ref() {
            subject.push_str(api_prefix);
        }
        subject.push_str(&self.prefix);
        subject.push_str(key);

        let publish_ack = self.context.publish(&subject, value)?;

        Ok(publish_ack.sequence)
    }

    /// Creates the key/value pair if it does not exist or is marked for deletion.
    ///
    /// # Examples
    ///
    /// ```
    /// # use nats::kv::Config;
    /// # fn main() -> std::io::Result<()> {
    /// # let client = nats::connect("demo.nats.io")?;
    /// # let context = nats::jetstream::new(client);
    /// #
    /// # let bucket = context.create_key_value(&Config {
    /// #  bucket: "create".to_string(),
    /// #  ..Default::default()
    /// # })?;
    /// #
    /// bucket.purge("foo")?;
    /// bucket.create("foo", b"bar")?;
    /// #
    /// # context.delete_key_value("create")?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn create(&self, key: &str, value: impl AsRef<[u8]>) -> io::Result<u64> {
        let result = self.update(key, &value, 0);
        if result.is_ok() {
            return result;
        }

        // Check if the last entry is a delete marker
        if let Ok(Some(entry)) = self.entry(key) {
            if entry.operation != Operation::Put {
                return self.update(key, &value, entry.revision);
            }
        }

        result
    }

    /// Updates the value if the latest revision matches.
    ///
    /// # Examples
    ///
    /// ```
    /// # use nats::kv::Config;
    /// # fn main() -> std::io::Result<()> {
    /// # let client = nats::connect("demo.nats.io")?;
    /// # let context = nats::jetstream::new(client);
    /// #
    /// # let bucket = context.create_key_value(&Config {
    /// #  bucket: "update".to_string(),
    /// #  ..Default::default()
    /// # })?;
    /// #
    /// let revision = bucket.put("foo", b"bar")?;
    /// let new_revision = bucket.update("foo", b"baz", revision)?;
    /// # context.delete_key_value("update")?;
    /// #
    /// # Ok(())
    /// # }
    /// ```
    pub fn update(&self, key: &str, value: impl AsRef<[u8]>, revision: u64) -> io::Result<u64> {
        if !is_valid_key(key) {
            return Err(io::Error::new(io::ErrorKind::InvalidInput, "invalid key"));
        }

        let mut subject = String::new();
        if let Some(api_prefix) = self.domain_prefix.as_ref() {
            subject.push_str(api_prefix);
        }
        subject.push_str(&self.prefix);
        subject.push_str(key);

        let mut headers = HeaderMap::default();
        headers.insert(
            header::NATS_EXPECTED_LAST_SUBJECT_SEQUENCE,
            revision.to_string(),
        );

        let message = Message::new(&subject, None, value, Some(headers));
        let publish_ack = self.context.publish_message(&message)?;

        Ok(publish_ack.sequence)
    }

    /// Marks an entry as deleted by placing a delete marker but leaves the revision history intact.
    ///
    /// # Examples
    ///
    /// ```
    /// # use nats::kv::Config;
    /// # fn main() -> std::io::Result<()> {
    /// # let client = nats::connect("demo.nats.io")?;
    /// # let context = nats::jetstream::new(client);
    /// #
    /// # let bucket = context.create_key_value(&Config {
    /// #  bucket: "delete".to_string(),
    /// #  ..Default::default()
    /// # })?;
    /// #
    /// bucket.create("foo", b"bar")?;
    /// bucket.delete("foo")?;
    /// #
    /// # context.delete_key_value("delete");
    /// #
    /// # Ok(())
    /// # }
    /// ```
    pub fn delete(&self, key: &str) -> io::Result<()> {
        if !is_valid_key(key) {
            return Err(io::Error::new(io::ErrorKind::InvalidInput, "invalid key"));
        }

        let mut subject = String::new();
        if let Some(api_prefix) = self.domain_prefix.as_ref() {
            subject.push_str(api_prefix);
        }
        subject.push_str(&self.prefix);
        subject.push_str(key);

        let mut headers = HeaderMap::default();
        headers.insert(KV_OPERATION, KV_OPERATION_DELETE.to_string());

        let message = Message::new(&subject, None, b"", Some(headers));
        self.context.publish_message(&message)?;

        Ok(())
    }

    /// Remove any entries associated with the key and all historical revisions.
    ///
    /// # Examples
    ///
    /// ```
    /// # use nats::kv::Config;
    /// # fn main() -> std::io::Result<()> {
    /// # let client = nats::connect("demo.nats.io")?;
    /// # let context = nats::jetstream::new(client);
    /// #
    /// # let bucket = context.create_key_value(&Config {
    /// #  bucket: "purge".to_string(),
    /// #  ..Default::default()
    /// # })?;
    /// #
    /// bucket.create("foo", b"bar")?;
    /// bucket.purge("foo")?;
    /// # context.delete_key_value("purge")?;
    /// #
    /// # Ok(())
    /// # }
    /// ```
    pub fn purge(&self, key: &str) -> io::Result<()> {
        if !is_valid_key(key) {
            return Err(io::Error::new(io::ErrorKind::InvalidInput, "invalid key"));
        }

        let mut subject = String::new();
        subject.push_str(&self.prefix);
        subject.push_str(key);

        let mut headers = HeaderMap::default();
        headers.insert(KV_OPERATION, KV_OPERATION_PURGE.to_string());
        headers.insert(NATS_ROLLUP, ROLLUP_SUBJECT.to_string());

        let message = Message::new(&subject, None, b"", Some(headers));
        self.context.publish_message(&message)?;

        Ok(())
    }

    /// Returns an iterator which iterate over all the current keys.
    ///
    /// # Examples
    ///
    /// ```
    /// # use nats::kv::Config;
    /// # fn main() -> std::io::Result<()> {
    /// # let client = nats::connect("demo.nats.io")?;
    /// # let context = nats::jetstream::new(client);
    /// #
    /// # let bucket = context.create_key_value(&Config {
    /// #  bucket: "keys".to_string(),
    /// #  ..Default::default()
    /// # })?;
    /// #
    /// bucket.put("foo", b"fizz")?;
    /// bucket.put("bar", b"buzz")?;
    ///
    /// let mut keys = bucket.keys()?;
    ///
    /// assert_eq!(keys.next(), Some("foo".to_string()));
    /// assert_eq!(keys.next(), Some("bar".to_string()));
    /// #
    /// # context.delete_key_value("keys")?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn keys(&self) -> io::Result<Keys> {
        let mut subject = String::new();
        subject.push_str(&self.prefix);
        subject.push_str(ALL_KEYS);

        let subscription = self.context.subscribe_with_options(
            &subject,
            &SubscribeOptions::ordered()
                .headers_only()
                .deliver_last_per_subject(),
        )?;

        Ok(Keys {
            prefix: self.prefix.clone(),
            subscription,
            done: false,
        })
    }

    /// Returns an iterator which iterates over each entry in historical order.
    ///
    /// # Examples
    ///
    /// ```
    /// # use nats::kv::Config;
    /// # fn main() -> std::io::Result<()> {
    /// # let client = nats::connect("demo.nats.io")?;
    /// # let context = nats::jetstream::new(client);
    /// #
    /// let bucket = context.create_key_value(&Config {
    ///   bucket: "history_iter".to_string(),
    ///   history: 2,
    ///   ..Default::default()
    /// })?;
    ///
    /// bucket.put("foo", b"fizz")?;
    /// bucket.put("foo", b"buzz")?;
    ///
    /// let mut history = bucket.history("foo")?;
    ///
    /// let next = history.next().unwrap();
    /// assert_eq!(next.key, "foo".to_string());
    /// assert_eq!(next.value, b"fizz");
    ///
    /// let next = history.next().unwrap();
    /// assert_eq!(next.key, "foo".to_string());
    /// assert_eq!(next.value, b"buzz");
    ///
    /// # context.delete_key_value("history_iter")?;
    /// #
    /// # Ok(())
    /// # }
    /// ```
    pub fn history(&self, key: &str) -> io::Result<History> {
        let mut subject = String::new();
        subject.push_str(&self.prefix);
        subject.push_str(key);

        let subscription = self.context.subscribe_with_options(
            &subject,
            &SubscribeOptions::ordered()
                .deliver_all()
                .enable_flow_control()
                .idle_heartbeat(Duration::from_millis(5000)),
        )?;

        Ok(History {
            bucket: self.name.clone(),
            prefix: self.prefix.clone(),
            subscription,
            done: false,
        })
    }

    /// Returns an iterator which iterates over each entry as they happen.
    pub fn watch_all(&self) -> io::Result<Watch> {
        self.watch(">")
    }

    /// Returns an iterator which iterates over each entry for specific key pattern as they happen.
    pub fn watch<T: AsRef<str>>(&self, key: T) -> io::Result<Watch> {
        let subject = format!("{}{}", self.prefix, key.as_ref());
        let subscription = self.context.subscribe_with_options(
            subject.as_str(),
            &SubscribeOptions::ordered()
                .deliver_last_per_subject()
                .enable_flow_control()
                .idle_heartbeat(Duration::from_millis(5000)),
        )?;

        Ok(Watch {
            bucket: self.name.clone(),
            prefix: self.prefix.clone(),
            subscription,
        })
    }

    /// Returns the name of the bucket
    pub fn bucket(&self) -> &String {
        &self.name
    }
}

/// An iterator used to iterate through the keys of a bucket.
pub struct Keys {
    prefix: String,
    subscription: PushSubscription,
    done: bool,
}

impl Iterator for Keys {
    type Item = String;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            if self.done {
                return None;
            }
            return match self.subscription.next() {
                Some(message) => {
                    // If there are no more pending messages we'll stop after delivering the key
                    // derived from this message.
                    if let Some(info) = message.jetstream_message_info() {
                        if info.pending == 0 {
                            self.done = true;
                        }
                    }

                    // We are only interested in unique current keys from subjects so we skip delete
                    // and purge markers.
                    let operation = kv_operation_from_maybe_headers(message.headers.as_ref());
                    if operation != Operation::Put {
                        continue;
                    }

                    message
                        .subject
                        .strip_prefix(&self.prefix)
                        .map(|s| s.to_string())
                }
                None => None,
            };
        }
    }
}

/// An iterator used to iterate through the history of a bucket.
pub struct History {
    bucket: String,
    prefix: String,
    subscription: PushSubscription,
    done: bool,
}

impl Iterator for History {
    type Item = Entry;

    fn next(&mut self) -> Option<Self::Item> {
        if self.done {
            return None;
        }

        match self.subscription.next() {
            Some(message) => {
                if let Some(info) = message.jetstream_message_info() {
                    if info.pending == 0 {
                        self.done = true;
                    }

                    let operation = kv_operation_from_maybe_headers(message.headers.as_ref());

                    let key = message
                        .subject
                        .strip_prefix(&self.prefix)
                        .map(|s| s.to_string())
                        .unwrap();

                    Some(Entry {
                        bucket: self.bucket.clone(),
                        key,
                        value: message.data.clone(),
                        revision: info.stream_seq,
                        created: info.published,
                        delta: info.pending,
                        operation,
                    })
                } else {
                    None
                }
            }

            None => None,
        }
    }
}

/// An iterator used to watch changes in a bucket.
pub struct Watch {
    bucket: String,
    prefix: String,
    subscription: PushSubscription,
}

impl Iterator for Watch {
    type Item = Entry;

    fn next(&mut self) -> Option<Self::Item> {
        match self.subscription.next() {
            Some(message) => {
                if let Some(info) = message.jetstream_message_info() {
                    let operation = kv_operation_from_maybe_headers(message.headers.as_ref());

                    let key = message
                        .subject
                        .strip_prefix(&self.prefix)
                        .map(|s| s.to_string())
                        .unwrap();

                    Some(Entry {
                        bucket: self.bucket.clone(),
                        key,
                        value: message.data.clone(),
                        revision: info.stream_seq,
                        created: info.published,
                        delta: info.pending,
                        operation,
                    })
                } else {
                    None
                }
            }
            None => None,
        }
    }
}

/// Represents status information about a key value store bucket
pub struct BucketStatus {
    info: StreamInfo,
    bucket: String,
}

impl BucketStatus {
    /// The name of the bucket
    pub fn bucket(&self) -> &String {
        &self.bucket
    }

    /// How many messages are in the bucket, including historical values
    pub fn values(&self) -> u64 {
        self.info.state.messages
    }

    /// Configured history kept per key
    pub fn history(&self) -> i64 {
        self.info.config.max_msgs_per_subject
    }

    /// How long the bucket keeps values for
    pub fn max_age(&self) -> Duration {
        self.info.config.max_age
    }
}