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.

1 package chasm
2
3 import (
4 "strings"
5 "unicode/utf8"
6
7 "go.temporal.io/api/serviceerror"
8 )
9
10 var _ NodePathEncoder = (*defaultPathEncoder)(nil)
11
12 var DefaultPathEncoder NodePathEncoder = &defaultPathEncoder{}
13
14 type defaultPathEncoder struct{}
15
16 const (
17 nameSeparator = '$'
18 collectionSeparator = '#'
19 escapeChar = '\\'
20 )
21
22 var (
23 rootPath = []string{}
24 )
25
26 // The Encode method encodes node path in a way that the following uses cases can be
27 // achieved by doing a simple a range query in DB based on prefixes of the encoded path:
28 // 1. Getting all nodes for a chasm tree.
29 // 2. Getting all nodes for a sub-tree.
30 // 3. Getting all immediate children of a Collection node.
31 // Additionally, it allows getting all ancestor nodes of a given node.
32 //
33 // It does so by using a different separator for a node which is a direct child of a Collection node.
34 // The two separators used ('$' and '#') are next to each other in terms of values, which ensures all
35 // children for a node are grouped together. Additionally, all immediate children of a Collection node
36 // are grouped together as well.
37 //
38 // To get a sub-tree (say a node with name "foo"), we want to use a query look like the following:
39 //
40 // path >= "foo" AND path < "foo[something]"
41 //
42 // and we need to know the minimal value of [something] that can possibly be in the encoded path.
43 // This is achieved by escaping '\', '$', '#', and also every code point less than '#'.
44 // Since the escaped character itself is larger than '#', the minimal value is '%' and our query becomes:
45 //
46 // path >= "foo" AND path < "foo%"
47 func (e *defaultPathEncoder) Encode(
48 node *Node,
49 path []string,
50 > ) (string, error) { path_encoder.go ×2
51 > if path == nil {
52 path = node.path()
53 }
54
55 > if len(path) == 0 { path_encoder.go ×2
56 > return "", nil
57 > }
58
59 > var b strings.Builder path_encoder.go ×6
60 > lastIdx := len(path) - 1
61 > for i, nodeName := range path {
62 > if i > 0 {
63 > if i == lastIdx && path_encoder.go ×2
64 > node.parent != nil &&
65 > node.parent.serializedNode.GetMetadata().GetCollectionAttributes() != nil {
66 > _, _ = b.WriteRune(collectionSeparator) path_encoder.go ×1
67 > } else { path_encoder.go ×2
68 > _, _ = b.WriteRune(nameSeparator) path_encoder.go ×1
69 > }
70 }
71
72 > if nodeName == "" { path_encoder.go ×6
73 return "", serviceerror.NewInternalf("path contains empty node name: %v", path)
74 }
75
76 > for _, r := range nodeName { path_encoder.go ×6
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 || path_encoder.go ×6
82 > r == nameSeparator ||
83 > r <= collectionSeparator {
84 > _, _ = b.WriteRune(escapeChar) path_encoder.go ×3
85 > }
86 > _, _ = b.WriteRune(r) path_encoder.go ×6
87 }
88 }
89 > return b.String(), nil path_encoder.go ×6
90 }
91
92 func (e *defaultPathEncoder) Decode(
93 encodedPath string,
94 > ) ([]string, error) { path_encoder.go ×1
95 > if encodedPath == "" {
96 > return rootPath, nil
97 > }
98
99 > path := make([]string, 0, 3) path_encoder.go ×7
100 > var b strings.Builder
101 > escaped := false
102 > for _, r := range encodedPath {
103 > if r == utf8.RuneError {
104 return nil, serviceerror.NewInvalidArgumentf("encodedPath contains invalid UTF-8 code point: %v", encodedPath)
105 }
106
107 > if escaped { path_encoder.go ×7
108 > _, _ = b.WriteRune(r) path_encoder.go ×3
109 > escaped = false
110 > continue
111 }
112
113 > if r == '\\' { path_encoder.go ×7
114 > escaped = true path_encoder.go ×3
115 > continue
116 }
117
118 > if r == '$' || r == '#' { path_encoder.go ×7
119 > path = append(path, b.String()) path_encoder.go ×1
120 > b.Reset()
121 > continue
122 }
123
124 > _, _ = b.WriteRune(r) path_encoder.go ×7
125 }
126 > if escaped { path_encoder.go ×7
127 return nil, serviceerror.NewInternalf("encoded path ends with escape character: %v", encodedPath)
128 }
129
130 > path = append(path, b.String()) path_encoder.go ×7
131 > return path, nil
132 }