go.temporal.io/server/chasm/path_encoder.go
132 LOC · 50 covered · 82 uncovered · 24 ranges · 492 concepts · 9 introducers · 230 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
package chasm
import (
"strings"
"unicode/utf8"
"go.temporal.io/api/serviceerror"
)
var _ NodePathEncoder = (*defaultPathEncoder)(nil)
var DefaultPathEncoder NodePathEncoder = &defaultPathEncoder{}
type defaultPathEncoder struct{}
const (
nameSeparator = '$'
collectionSeparator = '#'
escapeChar = '\\'
)
var (
rootPath = []string{}
)
// 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.
// Additionally, it allows getting all ancestor nodes of a given node.
//
// It does so by using a different separator for a node which is a direct child of a Collection node.
// The two separators used ('$' and '#') are next to each other in terms of values, which ensures all
// children for a node are grouped together. Additionally, all immediate children of a Collection node
// are grouped together as well.
//
// To get a sub-tree (say a node with name "foo"), we want to use a query look like the following:
//
// path >= "foo" AND path < "foo[something]"
//
// and we need to know the minimal value of [something] that can possibly be in the encoded path.
// This is achieved by escaping '\', '$', '#', and also every code point less than '#'.
// Since the escaped character itself is larger than '#', the minimal value is '%' and our query becomes:
//
// path >= "foo" AND path < "foo%"
func (e *defaultPathEncoder) Encode(
node *Node,
path []string,
if path == nil {
path = node.path()
}
return "", nil
}
lastIdx := len(path) - 1
for i, nodeName := range path {
if i > 0 {
node.parent != nil &&
node.parent.serializedNode.GetMetadata().GetCollectionAttributes() != nil {
}
}
return "", serviceerror.NewInternalf("path contains empty node name: %v", path)
}
if r == utf8.RuneError {
return "", serviceerror.NewInvalidArgumentf("node name contains invalid UTF-8 code point: %v", nodeName)
}
r == nameSeparator ||
r <= collectionSeparator {
}
}
}
}
func (e *defaultPathEncoder) Decode(
encodedPath string,
if encodedPath == "" {
return rootPath, nil
}
var b strings.Builder
escaped := false
for _, r := range encodedPath {
if r == utf8.RuneError {
return nil, serviceerror.NewInvalidArgumentf("encodedPath contains invalid UTF-8 code point: %v", encodedPath)
}
escaped = false
continue
}
continue
}
b.Reset()
continue
}
}
return nil, serviceerror.NewInternalf("encoded path ends with escape character: %v", encodedPath)
}
return path, nil
}