get.pme.sh/pnats@v0.0.0-20240304004023-26bb5a137ed0/server/stree/leaf.go (about)

     1  // Copyright 2023-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 stree
    15  
    16  import (
    17  	"bytes"
    18  )
    19  
    20  // Leaf node
    21  type leaf[T any] struct {
    22  	// This could be the whole subject, but most likely just the suffix portion.
    23  	// We will only store the suffix here and assume all prior prefix paths have
    24  	// been checked once we arrive at this leafnode.
    25  	suffix []byte
    26  	value  T
    27  }
    28  
    29  func newLeaf[T any](suffix []byte, value T) *leaf[T] {
    30  	return &leaf[T]{copyBytes(suffix), value}
    31  }
    32  
    33  func (n *leaf[T]) isLeaf() bool                               { return true }
    34  func (n *leaf[T]) base() *meta                                { return nil }
    35  func (n *leaf[T]) match(subject []byte) bool                  { return bytes.Equal(subject, n.suffix) }
    36  func (n *leaf[T]) setSuffix(suffix []byte)                    { n.suffix = copyBytes(suffix) }
    37  func (n *leaf[T]) isFull() bool                               { return true }
    38  func (n *leaf[T]) matchParts(parts [][]byte) ([][]byte, bool) { return matchParts(parts, n.suffix) }
    39  func (n *leaf[T]) iter(f func(node) bool)                     {}
    40  func (n *leaf[T]) children() []node                           { return nil }
    41  func (n *leaf[T]) numChildren() uint16                        { return 0 }
    42  func (n *leaf[T]) path() []byte                               { return n.suffix }
    43  
    44  // Not applicable to leafs and should not be called, so panic if we do.
    45  func (n *leaf[T]) setPrefix(pre []byte)    { panic("setPrefix called on leaf") }
    46  func (n *leaf[T]) addChild(_ byte, _ node) { panic("addChild called on leaf") }
    47  func (n *leaf[T]) findChild(_ byte) *node  { panic("findChild called on leaf") }
    48  func (n *leaf[T]) grow() node              { panic("grow called on leaf") }
    49  func (n *leaf[T]) deleteChild(_ byte)      { panic("deleteChild called on leaf") }
    50  func (n *leaf[T]) shrink() node            { panic("shrink called on leaf") }