]> git.giacomolli.uk - onechat/blob - view.go
Separate multiline Go statements
[onechat] / view.go
1 package main
2
3 import (
4 "fmt"
5 "math"
6 "math/bits"
7 "strconv"
8 "strings"
9 )
10
11 const (
12 // viewChatHigh and viewChatLow are the limits of the chat view in
13 // bytes.
14 viewChatHigh = 128000
15 viewChatLow = 64000
16
17 // viewCompactionHigh and viewCompactionLow are the limits of the
18 // compaction view in bytes.
19 viewCompactionHigh = 32000
20 viewCompactionLow = 16000
21
22 // viewPending is the text of a line that is not built yet.
23 viewPending = "(not summarized yet: zoom it)"
24 )
25
26 // View is a list of tree nodes that covers messages 0 to end-1 without a gap,
27 // oldest first. Each new message appends its leaf as a line. Once the size of
28 // the view goes above high, one batch merges the most due sibling pairs until
29 // the size is at most low. A batch merges only pairs whose parent is built. If
30 // the batch cannot get to low, it continues at each new message until it does.
31 //
32 // The view keeps the nodes of its built lines in memory, so its size and its
33 // text need no read of the tree. The size of a line is the size of its node
34 // text, or the size of the pending text when the node is not built.
35 //
36 // For the sibling pair (l, i) and (l, i+1), due is (T - last) / 2^l, with T the
37 // number of messages and last the ID of the last message of the pair. That is
38 // how long ago the pair ended, measured in its own line size. With a budget of
39 // the length of the rollback push list of the reference design, this order
40 // makes the same merges as the push. The oldest pair wins a tie.
41 type View struct {
42 high int
43 low int
44 lines []NodeKey
45 nodes map[NodeKey]Node
46 end int
47 merging bool
48 }
49
50 // ViewState is the saved form of a View. The limits are not part of it.
51 type ViewState struct {
52 Lines [][2]int `json:"lines"`
53 Merging bool `json:"merging"`
54 }
55
56 // ViewNodeReader gives the built nodes of a tree.
57 type ViewNodeReader interface {
58 Built(level, index int) (bool, error)
59 Node(level, index int) (Node, bool, error)
60 }
61
62 // newView returns an empty view with the limits high and low.
63 func newView(high, low int) *View {
64 return &View{
65 high: high,
66 low: low,
67 nodes: make(map[NodeKey]Node),
68 }
69 }
70
71 // State returns the saved form of the view.
72 func (v *View) State() ViewState {
73 state := ViewState{
74 Lines: make([][2]int, len(v.lines)),
75 Merging: v.merging,
76 }
77
78 for n, line := range v.lines {
79 state.Lines[n] = [2]int{line.level, line.index}
80 }
81
82 return state
83 }
84
85 // Append adds the leaf of the next message as a line and merges when the view
86 // is too large. It tells if a merge changed the view.
87 func (v *View) Append(tree ViewNodeReader) (bool, error) {
88 line := NodeKey{0, v.end}
89 _, err := v.load(tree, line)
90 if err != nil {
91 return false, err
92 }
93
94 v.lines = append(v.lines, line)
95 v.end++
96
97 return v.fit(tree)
98 }
99
100 // Coarsen takes each merge of other, a view of the same messages, and then
101 // merges in one batch until the size is at most low. Each line of the result is
102 // the coarser of the two lines that start at its first message. Dyadic ranges
103 // either nest or do not overlap, so the result covers the messages without a
104 // gap.
105 func (v *View) Coarsen(other *View, tree ViewNodeReader) error {
106 lines := make([]NodeKey, 0, len(v.lines))
107 nodes := make(map[NodeKey]Node, len(v.nodes))
108 mine, theirs := 0, 0
109 for start := 0; start < v.end; {
110 line := v.lines[mine]
111 node, built := v.nodes[line]
112 if other.lines[theirs].level > line.level {
113 line = other.lines[theirs]
114 node, built = other.nodes[line]
115 }
116
117 lines = append(lines, line)
118 if built {
119 nodes[line] = node
120 }
121
122 start = nodeEnd(line.level, line.index)
123 for mine < len(v.lines) && v.lines[mine].index<<v.lines[mine].level < start {
124 mine++
125 }
126
127 for theirs < len(other.lines) && other.lines[theirs].index<<other.lines[theirs].level < start {
128 theirs++
129 }
130 }
131
132 v.lines = lines
133 v.nodes = nodes
134 v.merging = true
135 _, err := v.fit(tree)
136
137 return err
138 }
139
140 // Refresh keeps node when it is the node of a pending line.
141 func (v *View) Refresh(node Node) {
142 key := NodeKey{node.Level, node.Index}
143 for n := len(v.lines) - 1; n >= 0; n-- {
144 line := v.lines[n]
145 if line == key {
146 v.nodes[key] = node
147 return
148 }
149
150 if line.index<<line.level <= key.index<<key.level {
151 return
152 }
153 }
154 }
155
156 // Pending tells if a line of the view is not built yet.
157 func (v *View) Pending() bool {
158 for _, line := range v.lines {
159 _, built := v.nodes[line]
160 if !built {
161 return true
162 }
163 }
164
165 return false
166 }
167
168 // Render returns the view as the model reads it: one line "id+n|text" for each
169 // node inside <chat> tags, with the newlines of the text as spaces.
170 func (v *View) Render() string {
171 return "<chat>\n" + strings.Join(v.LinesWithout(nil), "") + "</chat>"
172 }
173
174 // LinesWithout returns the lines of the view as the model reads them, each with
175 // its newline, without the leaf lines of the messages in skip. A line that
176 // merges such a message with others stays.
177 func (v *View) LinesWithout(skip map[int]bool) []string {
178 lines := make([]string, 0, len(v.lines))
179 for _, line := range v.lines {
180 if line.level == 0 && skip[line.index] {
181 continue
182 }
183
184 var text strings.Builder
185
186 v.renderLine(&text, line)
187
188 lines = append(lines, text.String())
189 }
190
191 return lines
192 }
193
194 // Context returns the lines of the view before message end as the model reads
195 // them. It stops at the first line that is not built, so a compaction never
196 // sees a pending mark or a part of a message.
197 func (v *View) Context(end int) string {
198 var text strings.Builder
199
200 text.WriteString("<chat>\n")
201
202 for _, line := range v.lines {
203 _, built := v.nodes[line]
204 if !built || nodeEnd(line.level, line.index) > end {
205 break
206 }
207
208 v.renderLine(&text, line)
209 }
210
211 text.WriteString("</chat>")
212
213 return text.String()
214 }
215
216 // load keeps the node of line when it is built, and tells if it is.
217 func (v *View) load(tree ViewNodeReader, line NodeKey) (bool, error) {
218 node, built, err := tree.Node(line.level, line.index)
219 if err != nil || !built {
220 return false, err
221 }
222
223 v.nodes[line] = node
224
225 return true, nil
226 }
227
228 // fit merges the most due pairs while a batch runs, and tells if a merge
229 // changed the view.
230 func (v *View) fit(tree ViewNodeReader) (bool, error) {
231 size := v.Size()
232 if size > v.high {
233 v.merging = true
234 }
235
236 merged := false
237 for v.merging && size > v.low {
238 best, err := v.mostDue(tree)
239 if err != nil || best < 0 {
240 return merged, err
241 }
242
243 left, right := v.lines[best], v.lines[best+1]
244 parent := NodeKey{left.level + 1, left.index / 2}
245 _, err = v.load(tree, parent)
246 if err != nil {
247 return merged, err
248 }
249
250 size += v.lineSize(parent) - v.lineSize(left) - v.lineSize(right)
251 delete(v.nodes, left)
252 delete(v.nodes, right)
253
254 v.lines[best] = parent
255 v.lines = append(v.lines[:best+1], v.lines[best+2:]...)
256 merged = true
257 }
258
259 v.merging = false
260
261 return merged, nil
262 }
263
264 // renderLine writes one view line, "id+n|text", and a newline.
265 func (v *View) renderLine(text *strings.Builder, line NodeKey) {
266 body := viewPending
267 node, built := v.nodes[line]
268 if built {
269 body = strings.ReplaceAll(node.Text, "\n", " ")
270 }
271
272 text.WriteString(strconv.Itoa(line.index << line.level))
273 text.WriteByte('+')
274 text.WriteString(strconv.Itoa(1 << line.level))
275 text.WriteByte('|')
276 text.WriteString(body)
277 text.WriteByte('\n')
278 }
279
280 // Size returns the sum of the line sizes.
281 func (v *View) Size() int {
282 size := 0
283 for _, line := range v.lines {
284 size += v.lineSize(line)
285 }
286
287 return size
288 }
289
290 // mostDue returns the position of the left line of the most due sibling pair
291 // whose parent is built, or -1 when there is no such pair.
292 func (v *View) mostDue(tree ViewNodeReader) (int, error) {
293 best := -1
294 bestDue := 0.0
295 for n := 0; n+1 < len(v.lines); n++ {
296 left, right := v.lines[n], v.lines[n+1]
297 if left.index%2 != 0 || right.level != left.level || right.index != left.index+1 {
298 continue
299 }
300
301 last := nodeEnd(right.level, right.index) - 1
302 due := math.Ldexp(float64(v.end-last), -left.level)
303 if best >= 0 && due <= bestDue {
304 continue
305 }
306
307 built, err := tree.Built(left.level+1, left.index/2)
308 if err != nil {
309 return -1, fmt.Errorf("check parent node: %w", err)
310 }
311
312 if built {
313 best = n
314 bestDue = due
315 }
316 }
317
318 return best, nil
319 }
320
321 // lineSize returns the size of a view line.
322 func (v *View) lineSize(line NodeKey) int {
323 node, built := v.nodes[line]
324 if !built {
325 return len(viewPending)
326 }
327
328 return node.Size
329 }
330
331 // restoreView returns the view of state with the limits high and low. Each line
332 // must continue the one before it, and each line above level 0 must be built.
333 func restoreView(state ViewState, high, low int, tree ViewNodeReader) (*View, error) {
334 v := newView(high, low)
335 v.merging = state.Merging
336 for _, pair := range state.Lines {
337 line := NodeKey{pair[0], pair[1]}
338 if line.level < 0 || line.level >= bits.UintSize-2 || line.index < 0 || line.index<<line.level != v.end {
339 return nil, fmt.Errorf("view line (%d, %d) does not start at message %d", line.level, line.index, v.end)
340 }
341
342 built, err := v.load(tree, line)
343 if err != nil {
344 return nil, err
345 }
346
347 if line.level > 0 && !built {
348 return nil, fmt.Errorf("view line (%d, %d) is not built", line.level, line.index)
349 }
350
351 v.lines = append(v.lines, line)
352 v.end = nodeEnd(line.level, line.index)
353 }
354
355 return v, nil
356 }