Debian Codegraph @

jetstream/kv_options.go

raw
1 // Copyright 2024 The NATS Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package jetstream
15
16 import (
17 "fmt"
18 "time"
19 )
20
21 type watchOptFn func(opts *watchOpts) error
22
23 func (opt watchOptFn) configureWatcher(opts *watchOpts) error {
24 return opt(opts)
25 }
26
27 // IncludeHistory instructs the key watcher to include historical values as
28 // well (up to KeyValueMaxHistory).
29 func IncludeHistory() WatchOpt {
30 return watchOptFn(func(opts *watchOpts) error {
31 if opts.updatesOnly {
32 return fmt.Errorf("%w: include history cannot be used with updates only", ErrInvalidOption)
33 }
34 opts.includeHistory = true
35 return nil
36 })
37 }
38
39 // UpdatesOnly instructs the key watcher to only include updates on values
40 // (without latest values when started).
41 func UpdatesOnly() WatchOpt {
42 return watchOptFn(func(opts *watchOpts) error {
43 if opts.includeHistory {
44 return fmt.Errorf("%w: updates only cannot be used with include history", ErrInvalidOption)
45 }
46 opts.updatesOnly = true
47 return nil
48 })
49 }
50
51 // IgnoreDeletes will prevent the key watcher from passing any deleted keys.
52 func IgnoreDeletes() WatchOpt {
53 return watchOptFn(func(opts *watchOpts) error {
54 opts.ignoreDeletes = true
55 return nil
56 })
57 }
58
59 // MetaOnly instructs the key watcher to retrieve only the entry metadata, not
60 // the entry value.
61 func MetaOnly() WatchOpt {
62 return watchOptFn(func(opts *watchOpts) error {
63 opts.metaOnly = true
64 return nil
65 })
66 }
67
68 // ResumeFromRevision instructs the key watcher to resume from a specific
69 // revision number.
70 func ResumeFromRevision(revision uint64) WatchOpt {
71 return watchOptFn(func(opts *watchOpts) error {
72 opts.resumeFromRevision = revision
73 return nil
74 })
75 }
76
77 // DeleteMarkersOlderThan indicates that delete or purge markers older than that
78 // will be deleted as part of [KeyValue.PurgeDeletes] operation, otherwise, only the data
79 // will be removed but markers that are recent will be kept.
80 // Note that if no option is specified, the default is 30 minutes. You can set
81 // this option to a negative value to instruct to always remove the markers,
82 // regardless of their age.
83 type DeleteMarkersOlderThan time.Duration
84
85 func (ttl DeleteMarkersOlderThan) configurePurge(opts *purgeOpts) error {
86 opts.dmthr = time.Duration(ttl)
87 return nil
88 }
89
90 type deleteOptFn func(opts *deleteOpts) error
91
92 func (opt deleteOptFn) configureDelete(opts *deleteOpts) error {
93 return opt(opts)
94 }
95
96 // LastRevision deletes if the latest revision matches the provided one. If the
97 // provided revision is not the latest, the delete will return an error.
98 func LastRevision(revision uint64) KVDeleteOpt {
99 return deleteOptFn(func(opts *deleteOpts) error {
100 opts.revision = revision
101 return nil
102 })
103 }
104
105 // PurgeTTL sets the TTL for the purge operation.
106 // After the TTL expires, the delete markers will be removed.
107 // This requires LimitMarkerTTL to be enabled on the bucket.
108 // Note that this is not the same as the TTL for the key itself, which is set
109 // using the KeyTTL option when creating the key.
110 func PurgeTTL(ttl time.Duration) KVDeleteOpt {
111 return deleteOptFn(func(opts *deleteOpts) error {
112 opts.ttl = ttl
113 return nil
114 })
115 }
116
117 type createOptFn func(opts *createOpts) error
118
119 func (opt createOptFn) configureCreate(opts *createOpts) error {
120 return opt(opts)
121 }
122
123 // KeyTTL sets the TTL for the key. This is the time after which the key will be
124 // automatically deleted. The TTL is set when the key is created and cannot be
125 // changed later. This requires LimitMarkerTTL to be enabled on the bucket.
126 func KeyTTL(ttl time.Duration) KVCreateOpt {
127 return createOptFn(func(opts *createOpts) error {
128 opts.ttl = ttl
129 return nil
130 })
131 }