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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
package data
import (
"context"
"database/sql"
"time"
"github.com/jpappel/atlas/pkg/index"
)
// TODO: rename struct
//
// Use to build a document from a database connection
type Fill struct {
Path string
Db *sql.DB
id int
doc *index.Document
}
// TODO: rename struct
//
// Use to build documents and aliases from a database connection
type FillMany struct {
docs map[string]*index.Document
ids map[string]int
Db *sql.DB
}
func (f Fill) Get(ctx context.Context) (*index.Document, error) {
f.doc = &index.Document{Path: f.Path}
if err := f.document(ctx); err != nil {
return nil, err
}
if err := f.tags(ctx); err != nil {
return nil, err
}
if err := f.authors(ctx); err != nil {
return nil, err
}
if err := f.links(ctx); err != nil {
return nil, err
}
return f.doc, nil
}
func (f FillMany) Get(ctx context.Context) (map[string]*index.Document, error) {
f.docs = make(map[string]*index.Document)
f.ids = make(map[string]int)
if err := f.documents(ctx); err != nil {
return nil, err
}
if err := f.tags(ctx); err != nil {
return nil, err
}
if err := f.links(ctx); err != nil {
return nil, err
}
if err := f.authors(ctx); err != nil {
return nil, err
}
return f.docs, nil
}
func (f *Fill) document(ctx context.Context) error {
var title sql.NullString
var dateEpoch sql.NullInt64
var fileTimeEpoch sql.NullInt64
var meta sql.NullString
row := f.Db.QueryRowContext(ctx, `
SELECT id, title, date, fileTime, meta
FROM Documents
WHERE path = ?
`, f.Path)
if err := row.Scan(&f.id, &title, &dateEpoch, &fileTimeEpoch, &meta); err != nil {
return err
}
if title.Valid {
f.doc.Title = title.String
}
if dateEpoch.Valid {
f.doc.Date = time.Unix(dateEpoch.Int64, 0)
}
if fileTimeEpoch.Valid {
f.doc.FileTime = time.Unix(fileTimeEpoch.Int64, 0)
}
if meta.Valid {
f.doc.OtherMeta = meta.String
}
return nil
}
func (f *FillMany) documents(ctx context.Context) error {
rows, err := f.Db.QueryContext(ctx, `
SELECT id, path, title, date, fileTime, meta
FROM Documents
`)
if err != nil {
return err
}
defer rows.Close()
var id int
var docPath string
var title, meta sql.NullString
var dateEpoch, filetimeEpoch sql.NullInt64
for rows.Next() {
if err := rows.Scan(&id, &docPath, &title, &dateEpoch, &filetimeEpoch, &meta); err != nil {
return err
}
doc := &index.Document{
Path: docPath,
}
if title.Valid {
doc.Title = title.String
}
if dateEpoch.Valid {
doc.Date = time.Unix(dateEpoch.Int64, 0)
}
if filetimeEpoch.Valid {
doc.FileTime = time.Unix(filetimeEpoch.Int64, 0)
}
if meta.Valid {
doc.OtherMeta = meta.String
}
f.docs[docPath] = doc
f.ids[docPath] = id
}
return nil
}
func (f Fill) authors(ctx context.Context) error {
rows, err := f.Db.QueryContext(ctx, `
SELECT name
FROM Authors
JOIN DocumentAuthors
ON Authors.id = DocumentAuthors.authorId
WHERE docId = ?
`, f.id)
if err != nil {
return err
}
defer rows.Close()
var name string
authors := make([]string, 0, 4)
for rows.Next() {
if err := rows.Scan(&name); err != nil {
return err
}
authors = append(authors, name)
}
f.doc.Authors = authors
return nil
}
func (f FillMany) authors(ctx context.Context) error {
stmt, err := f.Db.PrepareContext(ctx, `
SELECT name
FROM Authors
JOIN DocumentAuthors
ON Authors.id = DocumentAuthors.authorId
WHERE docId = ?
`)
if err != nil {
return err
}
defer stmt.Close()
// PERF: parallelize
var name string
for path, id := range f.ids {
rows, err := stmt.QueryContext(ctx, id)
if err != nil {
return err
}
doc := f.docs[path]
for rows.Next() {
if err := rows.Scan(&name); err != nil {
rows.Close()
return err
}
doc.Authors = append(doc.Authors, name)
}
rows.Close()
}
return nil
}
func (f Fill) tags(ctx context.Context) error {
rows, err := f.Db.QueryContext(ctx, `
SELECT name
FROM Tags
JOIN DocumentTags
ON Tags.id = DocumentTags.tagId
WHERE docId = ?
`, f.id)
if err != nil {
panic(err)
}
defer rows.Close()
var tag string
tags := make([]string, 0, 2)
for rows.Next() {
if err := rows.Scan(&tag); err != nil {
return err
}
tags = append(tags, tag)
}
f.doc.Tags = tags
return nil
}
func (f FillMany) tags(ctx context.Context) error {
stmt, err := f.Db.PrepareContext(ctx, `
SELECT name
FROM Tags
JOIN DocumentTags
ON Tags.id = DocumentTags.tagId
WHERE docId = ?
`)
if err != nil {
return err
}
defer stmt.Close()
// PERF: parallelize
var tag string
for docPath, id := range f.ids {
rows, err := stmt.QueryContext(ctx, id)
if err != nil {
return err
}
doc := f.docs[docPath]
for rows.Next() {
if err := rows.Scan(&tag); err != nil {
rows.Close()
return err
}
doc.Tags = append(doc.Tags, tag)
}
rows.Close()
}
return nil
}
func (f Fill) links(ctx context.Context) error {
rows, err := f.Db.QueryContext(ctx, `
SELECT path
FROM Documents
JOIN Links
ON Links.referencedId = Documents.id
WHERE Links.refererId = ?
`, f.id)
if err != nil {
return err
}
defer rows.Close()
var link string
links := make([]string, 0)
for rows.Next() {
if err := rows.Scan(&link); err != nil {
return err
}
links = append(links, link)
}
f.doc.Links = links
return nil
}
func (f FillMany) links(ctx context.Context) error {
stmt, err := f.Db.PrepareContext(ctx, `
SELECT path
FROM Documents
JOIN Links
ON Links.referencedId = Documents.id
WHERE Links.refererId = ?
`)
if err != nil {
return err
}
defer stmt.Close()
// PERF: parallelize
var linkPath string
for path, id := range f.ids {
rows, err := stmt.QueryContext(ctx, id)
if err != nil {
return err
}
doc := f.docs[path]
for rows.Next() {
if err := rows.Scan(&linkPath); err != nil {
rows.Close()
return err
}
doc.Links = append(doc.Links, linkPath)
}
rows.Close()
}
return nil
}
|