The Encode method encodes node path in a way that the following uses cases can be achieved by doing a simple a range query in DB based on prefixes of the encoded path: 1. Getting all nodes for a chasm tree. 2. Getting all nodes for a sub-tree. 3. Getting all immediate children of a Collection node.
( node *Node, path []string, )
| 45 | // |
| 46 | // path >= "foo" AND path < "foo%" |
| 47 | func (e *defaultPathEncoder) Encode( |
| 48 | node *Node, |
| 49 | path []string, |
| 50 | ) (string, error) { |
| 51 | if path == nil { |
| 52 | path = node.path() |
| 53 | } |
| 54 | |
| 55 | if len(path) == 0 { |
| 56 | return "", nil |
| 57 | } |
| 58 | |
| 59 | var b strings.Builder |
| 60 | lastIdx := len(path) - 1 |
| 61 | for i, nodeName := range path { |
| 62 | if i > 0 { |
| 63 | if i == lastIdx && |
| 64 | node.parent != nil && |
| 65 | node.parent.serializedNode.GetMetadata().GetCollectionAttributes() != nil { |
| 66 | _, _ = b.WriteRune(collectionSeparator) |
| 67 | } else { |
| 68 | _, _ = b.WriteRune(nameSeparator) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | if nodeName == "" { |
| 73 | return "", serviceerror.NewInternalf("path contains empty node name: %v", path) |
| 74 | } |
| 75 | |
| 76 | for _, r := range nodeName { |
| 77 | if r == utf8.RuneError { |
| 78 | return "", serviceerror.NewInvalidArgumentf("node name contains invalid UTF-8 code point: %v", nodeName) |
| 79 | } |
| 80 | |
| 81 | if r == escapeChar || |
| 82 | r == nameSeparator || |
| 83 | r <= collectionSeparator { |
| 84 | _, _ = b.WriteRune(escapeChar) |
| 85 | } |
| 86 | _, _ = b.WriteRune(r) |
| 87 | } |
| 88 | } |
| 89 | return b.String(), nil |
| 90 | } |
| 91 | |
| 92 | func (e *defaultPathEncoder) Decode( |
| 93 | encodedPath string, |