Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADDED] Filter streams by subject #1062

Merged
merged 2 commits into from Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions js.go
Expand Up @@ -239,6 +239,8 @@ type jsOpts struct {
purgeOpts *StreamPurgeRequest
// streamInfoOpts contains optional stream info options
streamInfoOpts *StreamInfoRequest
// streamListSubject is used for subject filtering when listing streams / stream names
streamListSubject string
// For direct get message requests
directGet bool
// For direct get next message
Expand Down Expand Up @@ -359,6 +361,17 @@ func DirectGetNext(subject string) JSOpt {
})
}

// StreamListFilter is an option that can be used to configure `StreamsInfo()` and `StreamNames()` requests.
// It allows filtering the retured streams by subject associated with each stream.
// Wildcards can be used. For example, `StreamListFilter(FOO.>.A) will return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FOO.>.A is an invalid subject I think, maybe switch to the asterisk wildcard variant:

Suggested change
// Wildcards can be used. For example, `StreamListFilter(FOO.>.A) will return
// Wildcards can be used. For example, `StreamListFilter(FOO.*.A) will return

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching that - fixed

// all streams which have at least one subject matching the provided pattern (e.g. FOO.TEST.A).
func StreamListFilter(subject string) JSOpt {
return jsOptFn(func(opts *jsOpts) error {
opts.streamListSubject = subject
return nil
})
}

func (js *js) apiSubj(subj string) string {
if js.opts.pre == _EMPTY_ {
return subj
Expand Down
2 changes: 2 additions & 0 deletions jsm.go
Expand Up @@ -1211,6 +1211,7 @@ func (s *streamLister) Next() bool {

req, err := json.Marshal(streamNamesRequest{
apiPagedRequest: apiPagedRequest{Offset: s.offset},
Subject: s.js.opts.streamListSubject,
})
if err != nil {
s.err = err
Expand Down Expand Up @@ -1311,6 +1312,7 @@ func (l *streamNamesLister) Next() bool {

req, err := json.Marshal(streamNamesRequest{
apiPagedRequest: apiPagedRequest{Offset: l.offset},
Subject: l.js.opts.streamListSubject,
})
if err != nil {
l.err = err
Expand Down
69 changes: 69 additions & 0 deletions test/js_test.go
Expand Up @@ -1692,6 +1692,75 @@ func TestStreamLister(t *testing.T) {
}
}

func TestStreamLister_FilterSubject(t *testing.T) {
streams := map[string][]string{
"s1": {"foo"},
"s2": {"bar"},
"s3": {"foo.*", "bar.*"},
"s4": {"foo-1.A"},
"s5": {"foo.A.bar.B"},
"s6": {"foo.C.bar.D.E"},
}
tests := []struct {
filter string
expected []string
}{
{
filter: "foo",
expected: []string{"s1"},
},
{
filter: "bar",
expected: []string{"s2"},
},
{
filter: "*",
expected: []string{"s1", "s2"},
},
{
filter: ">",
expected: []string{"s1", "s2", "s3", "s4", "s5", "s6"},
},
{
filter: "*.A",
expected: []string{"s3", "s4"},
},
}
s := RunBasicJetStreamServer()
defer shutdownJSServerAndRemoveStorage(t, s)

nc, js := jsClient(t, s)
defer nc.Close()
for name, subjects := range streams {
if _, err := js.AddStream(&nats.StreamConfig{Name: name, Subjects: subjects}); err != nil {
t.Fatalf("Unexpected error: %v", err)
}
}

for _, test := range tests {
t.Run(test.filter, func(t *testing.T) {
names := make([]string, 0)

// list stream names
for name := range js.StreamNames(nats.StreamListFilter(test.filter)) {
names = append(names, name)
}
if !reflect.DeepEqual(names, test.expected) {
t.Fatalf("Invalid result; want: %v; got: %v", test.expected, names)
}

// list streams
names = make([]string, 0)
for info := range js.StreamsInfo(nats.StreamListFilter(test.filter)) {
names = append(names, info.Config.Name)
}
if !reflect.DeepEqual(names, test.expected) {
t.Fatalf("Invalid result; want: %v; got: %v", test.expected, names)
}
})
}
}

func TestConsumersLister(t *testing.T) {
tests := []struct {
name string
Expand Down