-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.go
69 lines (61 loc) · 1.44 KB
/
util.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
package anystore
import (
"slices"
"github.com/valyala/fastjson"
"zombiezen.com/go/sqlite"
"github.com/anyproto/any-store/internal/syncpool"
)
func readIndexInfo(buf *syncpool.DocBuffer, stmt *sqlite.Stmt) (result []IndexInfo, err error) {
var p = &fastjson.Parser{}
for {
hasRow, stepErr := stmt.Step()
if stepErr != nil {
return nil, stepErr
}
if !hasRow {
return
}
fields, err := jsonToStringArray(p, stmt.ColumnText(1))
if err != nil {
return nil, err
}
result = append(result, IndexInfo{
Name: stmt.ColumnText(0),
Fields: fields,
Sparse: stmt.ColumnInt(2) != 0,
Unique: stmt.ColumnInt(2) != 0,
})
}
}
func readBytes(stmt *sqlite.Stmt, buf []byte) []byte {
l := stmt.ColumnLen(0)
buf = slices.Grow(buf, l)[:l]
stmt.ColumnBytes(0, buf)
return buf
}
func stringArrayToJson(a *fastjson.Arena, array []string) string {
jArr := a.NewArray()
for i, s := range array {
jArr.SetArrayItem(i, a.NewString(s))
}
return jArr.String()
}
func jsonToStringArray(p *fastjson.Parser, j string) ([]string, error) {
jVal, err := p.Parse(j)
if err != nil {
return nil, err
}
jVals, err := jVal.Array()
if err != nil {
return nil, err
}
result := make([]string, len(jVals))
for i, jArrV := range jVals {
result[i] = string(jArrV.GetStringBytes())
}
return result, nil
}
func copyItem(buf *syncpool.DocBuffer, it item) item {
res, _ := buf.Parser.Parse(it.val.MarshalTo(buf.DocBuf[:0]))
return item{val: res}
}