-
Notifications
You must be signed in to change notification settings - Fork 6
/
ancient.go
104 lines (99 loc) · 2.08 KB
/
ancient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"fmt"
"iter"
"strings"
"zombiezen.com/go/sqlite"
"zombiezen.com/go/sqlite/sqlitex"
)
func ancientOpen(file string) (*sqlite.Conn, int, error) {
conn, err := sqlite.OpenConn(file, sqlite.OpenReadOnly, sqlite.OpenURI)
if err != nil {
return nil, 0, err
}
st, _, err := conn.PrepareTransient(`SELECT pfix FROM config`)
if err != nil {
return nil, 0, err
}
defer st.Finalize()
order, err := sqlitex.ResultInt(st)
return conn, order, err
}
type ancientMessage struct {
tag string
text string
}
func ancientMessages(conn *sqlite.Conn, order int) iter.Seq2[ancientMessage, error] {
// don't look at this code
return func(yield func(ancientMessage, error) bool) {
st, _, err := conn.PrepareTransient(fmt.Sprintf(`SELECT id, tag, IFNULL(p%d, ''), IFNULL(suffix, '') FROM tuples%d`, order-1, order))
if err != nil {
yield(ancientMessage{}, err)
return
}
defer st.Finalize()
var (
toks []string
id int64
tag, pre, suf string
)
for {
// Get the initial values for the current message.
for {
ok, err := st.Step()
if err != nil {
yield(ancientMessage{}, err)
return
}
if !ok {
return
}
pre = st.ColumnText(2)
if pre != "" {
continue
}
id = st.ColumnInt64(0)
tag = st.ColumnText(1)
suf = st.ColumnText(3)
break
}
toks = toks[:0]
for {
if suf == "" {
if !yield(ancientMessage{tag: tag, text: strings.Join(toks, " ")}, nil) {
return
}
break
}
toks = append(toks, suf)
ok, err := st.Step()
if err != nil {
yield(ancientMessage{}, err)
return
}
if !ok {
yield(ancientMessage{tag: tag, text: strings.Join(toks, " ")}, nil)
return
}
nid := st.ColumnInt64(0)
if nid != id+1 {
// Gap in IDs. Skip to the next message.
for suf != "" {
ok, err := st.Step()
if err != nil {
yield(ancientMessage{}, err)
return
}
if !ok {
return
}
suf = st.ColumnText(3)
}
break
}
id = nid
suf = st.ColumnText(3)
}
}
}
}