Skip to content

Commit

Permalink
Merge pull request #854 from nats-io/watch-without-data-via-headers-only
Browse files Browse the repository at this point in the history
ability to watch "withoutData", useful when retrieving keys
  • Loading branch information
scottf committed Oct 31, 2021
2 parents f2416a8 + 5c9ca40 commit 5c4ad16
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion kv.go
Expand Up @@ -112,6 +112,8 @@ type watchOpts struct {
ignoreDeletes bool
// Include all history per subject, not just last one.
includeHistory bool
// retrieve only the meta data of the entry
metaOnly bool
}

type watchOptFn func(opts *watchOpts) error
Expand All @@ -136,6 +138,14 @@ func IgnoreDeletes() WatchOpt {
})
}

// MetaOnly instructs the key watcher to retrieve only the entry meta data, not the entry value
func MetaOnly() WatchOpt {
return watchOptFn(func(opts *watchOpts) error {
opts.metaOnly = true
return nil
})
}

// KeyValueConfig is for configuring a KeyValue store.
type KeyValueConfig struct {
Bucket string
Expand Down Expand Up @@ -534,7 +544,7 @@ func (kv *kvs) PurgeDeletes(opts ...WatchOpt) error {

// Keys() will return all keys.
func (kv *kvs) Keys(opts ...WatchOpt) ([]string, error) {
opts = append(opts, IgnoreDeletes())
opts = append(opts, IgnoreDeletes(), MetaOnly())
watcher, err := kv.WatchAll(opts...)
if err != nil {
return nil, err
Expand Down Expand Up @@ -676,6 +686,9 @@ func (kv *kvs) Watch(keys string, opts ...WatchOpt) (KeyWatcher, error) {
if !o.includeHistory {
subOpts = append(subOpts, DeliverLastPerSubject())
}
if o.metaOnly {
subOpts = append(subOpts, HeadersOnly())
}
sub, err := kv.js.Subscribe(keys, update, subOpts...)
if err != nil {
return nil, err
Expand Down

0 comments on commit 5c4ad16

Please sign in to comment.