@@ -17,6 +17,7 @@ import (
1717
1818const (
1919 PathSeparator = "."
20+ minCharacter = "-"
2021 // Postgres imposes a 65535 limit on the number of labels in a ltree.
2122 maxNumOfLabels = 65535
2223 // Postgres docs mention labels must be less than 256 bytes, but in practice,
@@ -297,44 +298,17 @@ func LCA(ltrees []T) (_ T, isNull bool) { // lint: uppercase function OK
297298}
298299
299300// NextSibling returns a LTree with a lexicographically incremented last label.
300- // This is different from the next lexicographic LTree. This is mainly used for
301- // defining a key-encoded span for ancestry operators.
302- // Note that this could produce a LTREE with more labels than the maximum
303- // allowed.
304- // Example: 'A.B' -> 'A.C'
305- func (lt T ) NextSibling () T {
301+ // If the LTree is empty, it returns ok=false. An empty LTree represents the
302+ // root of the tree, so it has no siblings.
303+ //
304+ // This is mainly used for defining a key-encoded span for ancestry operators.
305+ //
306+ // Example: 'A.B' -> 'A.B-'
307+ func (lt T ) NextSibling () (_ T , ok bool ) {
306308 if lt .Len () == 0 {
307- return Empty
309+ return T {}, false
308310 }
309- lastLabel := lt .path [len (lt .path )- 1 ]
310- nextLabel := incrementLabel (lastLabel )
311-
312- newLTree := lt .Copy ()
313- newLTree .path [newLTree .Len ()- 1 ] = nextLabel
314- return newLTree
315- }
316-
317- var nextCharMap = map [byte ]byte {
318- '-' : '0' ,
319- '9' : 'A' ,
320- 'Z' : '_' ,
321- '_' : 'a' ,
322- 'z' : 0 ,
323- }
324-
325- func incrementLabel (label string ) string {
326- nextLabel := []byte (label )
327- nextChar , ok := nextCharMap [nextLabel [len (nextLabel )- 1 ]]
328-
329- if ok && nextChar == 0 {
330- // Technically, this could mean exceeding the length of the label.
331- return string (append (nextLabel , '-' ))
332- }
333-
334- if ! ok {
335- nextChar = nextLabel [len (nextLabel )- 1 ] + 1
336- }
337-
338- nextLabel [len (nextLabel )- 1 ] = nextChar
339- return string (nextLabel )
311+ newLT := lt .Copy ()
312+ newLT .path [newLT .Len ()- 1 ] = newLT .path [newLT .Len ()- 1 ] + minCharacter
313+ return newLT , true
340314}
0 commit comments