mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-15 18:50:41 -07:00
Made Remove return proper error
This commit is contained in:
@@ -79,6 +79,7 @@ var (
|
||||
|
||||
var (
|
||||
ErrInvalidSubject = errors.New("Invalid Subject")
|
||||
ErrNotFound = errors.New("No Matches Found")
|
||||
)
|
||||
|
||||
// split will split a subject into tokens
|
||||
@@ -278,11 +279,12 @@ type lnt struct {
|
||||
|
||||
// Remove will remove any item associated with key. It will track descent
|
||||
// into the trie and prune upon successful removal.
|
||||
func (s *Sublist) Remove(subject []byte, sub interface{}) {
|
||||
func (s *Sublist) Remove(subject []byte, sub interface{}) error {
|
||||
tsa := [16][]byte{}
|
||||
toks := split(subject, tsa[:0])
|
||||
|
||||
s.mu.Lock()
|
||||
sfwc := false
|
||||
l := s.root
|
||||
var n *node
|
||||
|
||||
@@ -290,15 +292,20 @@ func (s *Sublist) Remove(subject []byte, sub interface{}) {
|
||||
levels := lnts[:0]
|
||||
|
||||
for _, t := range toks {
|
||||
if l == nil || len(t) == 0 {
|
||||
if len(t) == 0 || sfwc {
|
||||
s.mu.Unlock()
|
||||
return
|
||||
return ErrInvalidSubject
|
||||
}
|
||||
if l == nil {
|
||||
s.mu.Unlock()
|
||||
return ErrNotFound
|
||||
}
|
||||
switch t[0] {
|
||||
case _PWC:
|
||||
n = l.pwc
|
||||
case _FWC:
|
||||
n = l.fwc
|
||||
sfwc = true
|
||||
default:
|
||||
if v := l.nodes.Get(t); v == nil {
|
||||
n = nil
|
||||
@@ -315,7 +322,7 @@ func (s *Sublist) Remove(subject []byte, sub interface{}) {
|
||||
}
|
||||
if !s.removeFromNode(n, sub) {
|
||||
s.mu.Unlock()
|
||||
return
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
s.count--
|
||||
@@ -329,6 +336,7 @@ func (s *Sublist) Remove(subject []byte, sub interface{}) {
|
||||
}
|
||||
s.removeFromCache(subject, sub)
|
||||
s.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// pruneNode is used to prune and empty node from the tree.
|
||||
|
||||
@@ -362,8 +362,18 @@ func TestBadSubjectOnRemove(t *testing.T) {
|
||||
value := "bad"
|
||||
|
||||
s := New()
|
||||
s.Insert(bad, value)
|
||||
s.Remove(bad, value)
|
||||
if err := s.Insert(bad, value); err != ErrInvalidSubject {
|
||||
t.Fatalf("Expected ErrInvalidSubject, got %v\n", err)
|
||||
}
|
||||
|
||||
if err := s.Remove(bad, value); err != ErrInvalidSubject {
|
||||
t.Fatalf("Expected ErrInvalidSubject, got %v\n", err)
|
||||
}
|
||||
|
||||
badfwc := []byte("a.>.b")
|
||||
if err := s.Remove(badfwc, value); err != ErrInvalidSubject {
|
||||
t.Fatalf("Expected ErrInvalidSubject, got %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
// -- Benchmarks Setup --
|
||||
|
||||
Reference in New Issue
Block a user