-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexer.go
397 lines (329 loc) · 8.86 KB
/
indexer.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/meilisearch/meilisearch-go"
gitignore "github.com/sabhiram/go-gitignore"
"gopkg.in/yaml.v3"
"github.com/alsosee/finder/structs"
)
var errNotFound = fmt.Errorf("not found")
// Indexer reads files and writes them to a MeiliSearch index.
type Indexer struct {
client meilisearch.ServiceManager
ignore *gitignore.GitIgnore
state map[string]string
// toUpdateThumb is a map of paths that need to be updated additionally.
// Processing a single document can trigger processing of another
// if they share the same thumbnail path
toUpdateThumb map[string]interface{} // path -> nil
infoDir string
mediaAbsPath string
}
// NewIndexer creates a new Indexer.
func NewIndexer(
client meilisearch.ServiceManager,
ignore *gitignore.GitIgnore,
infoDir string,
mediaDir string,
state map[string]string,
) (*Indexer, error) {
mediaAbsPath, err := filepath.Abs(mediaDir)
if err != nil {
return nil, fmt.Errorf("getting absolute path: %w", err)
}
return &Indexer{
client: client,
ignore: ignore,
state: state,
toUpdateThumb: make(map[string]interface{}),
infoDir: infoDir,
mediaAbsPath: mediaAbsPath,
}, nil
}
// Index reads files from the info directory and writes them to the MeiliSearch.
func (i *Indexer) Index(stateFile, index, force string) error {
state, err := readStateFromFile(stateFile)
if err != nil {
return fmt.Errorf("reading state file: %w", err)
}
log.Printf("State file contains %d entries", len(state))
if err := i.updateIndex(state, index, force); err != nil {
return fmt.Errorf("updating index: %w", err)
}
if err := writeStateToFile(stateFile, i.state); err != nil {
return fmt.Errorf("writing state file: %w", err)
}
return nil
}
func (i *Indexer) updateIndex(oldState map[string]string, index, force string) error {
if force == "all" {
return i.addToIndexAll(index)
}
if force != "" {
var forceList []string
if strings.HasPrefix(force, "[") {
// force is a JSON array
if err := json.Unmarshal([]byte(force), &forceList); err != nil {
return fmt.Errorf("parsing force list: %w", err)
}
} else {
// split force string by comma
forceList = strings.Split(force, ",")
}
return i.addToIndex(forceList, index)
}
// find deleted files
var toDelete []string
for path := range oldState {
if _, ok := i.state[path]; !ok {
toDelete = append(toDelete, path)
}
}
// find new and changed files
var toUpdate []string
for path, hash := range i.state {
if oldHash, ok := oldState[path]; !ok || oldHash != hash {
toUpdate = append(toUpdate, path)
}
}
if err := i.deleteFromIndex(toDelete, index); err != nil {
return fmt.Errorf("deleting documents: %w", err)
}
if err := i.addToIndex(toUpdate, index); err != nil {
return fmt.Errorf("adding documents: %w", err)
}
return nil
}
func (i *Indexer) deleteFromIndex(paths []string, index string) error {
if len(paths) == 0 {
return nil
}
log.Printf("Deleting %d documents from index", len(paths))
// todo fix IDs
task, err := i.client.Index(index).DeleteDocuments(paths)
if err != nil {
return err
}
err = i.waitForTask(task.TaskUID, time.Minute*2)
if err != nil {
return fmt.Errorf("waiting for task %q: %w", task.TaskUID, err)
}
return nil
}
func (i *Indexer) addToIndex(paths []string, index string) error {
documents := []*structs.Content{}
for _, path := range paths {
document, err := i.processFile(path)
if err != nil {
if err == errNotFound {
log.Printf("File %q not found, skipping", path)
continue
}
if errors.Is(err, io.EOF) {
continue
}
return fmt.Errorf("processing file %q: %w", path, err)
}
documents = append(documents, document)
}
for path := range i.toUpdateThumb {
if in(path, paths...) {
continue
}
document, err := i.processFile(path)
if err != nil {
return fmt.Errorf("processing file %q: %w", path, err)
}
documents = append(documents, document)
}
return i.addDocumentsToIndex(documents, index)
}
func (i *Indexer) addToIndexAll(index string) error {
documents := []*structs.Content{}
for path := range i.state {
document, err := i.processFile(path)
if err != nil {
return fmt.Errorf("processing file %q: %w", path, err)
}
documents = append(documents, document)
}
return i.addDocumentsToIndex(documents, index)
}
func (i *Indexer) addDocumentsToIndex(documents []*structs.Content, index string) error {
if len(documents) == 0 {
log.Printf("No documents to add to index %q", index)
return nil
}
log.Printf("Adding %d documents to index %q", len(documents), index)
for _, document := range documents {
log.Printf(" %s", document.Source)
}
tasks, err := i.client.Index(index).AddDocumentsInBatches(documents, 100, "ID")
if err != nil {
return err
}
for _, task := range tasks {
err := i.waitForTask(task.TaskUID, time.Minute*2)
if err != nil {
return fmt.Errorf("waiting for task %q: %w", task.TaskUID, err)
}
}
return err
}
func (i *Indexer) processFile(path string) (*structs.Content, error) {
switch filepath.Ext(path) {
case ".yml", ".yaml":
return i.processYAMLFile(path)
// case ".md":
// return i.processMarkdownFile(path)
default:
return nil, fmt.Errorf("unknown file type: %s", path)
}
}
func (i *Indexer) processYAMLFile(path string) (*structs.Content, error) {
file, err := os.Open(filepath.Join(i.infoDir, path))
if err != nil {
if os.IsNotExist(err) {
return nil, errNotFound
}
return nil, fmt.Errorf("opening file: %w", err)
}
defer file.Close()
var content structs.Content
if err := yaml.NewDecoder(file).Decode(&content); err != nil {
return nil, fmt.Errorf("decoding file: %w", err)
}
content.Source = path
content.GenerateID()
content.AddMedia(i.getImageForPath)
return &content, nil
}
func (i *Indexer) getImageForPath(path string) *structs.Media {
dir := filepath.Dir(path)
if dir == "." {
dir = ""
}
// read .thumb.yml file in media directory
thumbFile := filepath.Join(i.mediaAbsPath, dir, ".thumbs.yml")
if _, err := os.Stat(thumbFile); os.IsNotExist(err) {
return nil
}
media, err := structs.ParseMediaFile(thumbFile)
if err != nil {
log.Printf("Error parsing media file %q: %v", thumbFile, err)
return nil
}
if len(media) == 0 {
return nil
}
var image *structs.Media
for _, m := range media {
mediaImage := m
mediaPath := filepath.Join(dir, removeFileExtention(m.Path))
if mediaPath == path {
image = &mediaImage
break
}
}
if image != nil {
// Add other media that share the same ThumbPath for updating the index,
// because data in the index is used to display the thumb.
for _, m := range media {
if m.ThumbPath == image.ThumbPath && m.Path != image.Path {
newPath := filepath.Join(dir, removeFileExtention(m.Path)+".yml")
if _, ok := i.toUpdateThumb[newPath]; !ok {
// if file is existing
if _, err := os.Stat(filepath.Join(i.infoDir, newPath)); err == nil {
log.Printf("Adding to update list: %s", newPath)
i.toUpdateThumb[newPath] = nil
}
}
}
}
}
return image
}
func (i *Indexer) waitForTask(taskID int64, timeout time.Duration) error {
var (
task *meilisearch.Task
err error
delay = time.Second
)
for {
task, err = i.client.GetTask(taskID)
if err != nil {
return err
}
if task.Status == "succeeded" || task.Status == "failed" {
break
}
time.Sleep(delay)
// check timeout
timeout -= delay
if timeout <= 0 {
return fmt.Errorf("task timeout")
}
log.Printf("Task %d status: %s", taskID, task.Status)
}
if task.Status == "failed" {
return fmt.Errorf("task failed: %s", task.Error)
}
return nil
}
func readStateFromFile(stateFile string) (map[string]string, error) {
state := make(map[string]string)
f, err := os.Open(stateFile)
if err != nil {
if os.IsNotExist(err) {
return state, nil
}
return nil, fmt.Errorf("opening state file: %w", err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if line == "" {
continue
}
parts := strings.Split(line, "\t")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid state file format")
}
relPath := parts[0]
hash := parts[1]
state[relPath] = hash
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("reading state file: %w", err)
}
return state, nil
}
func writeStateToFile(stateFile string, state map[string]string) error {
f, err := os.Create(stateFile)
if err != nil {
return fmt.Errorf("creating state file: %w", err)
}
defer f.Close()
stateSlice := make([]string, 0, len(state))
for relPath, hash := range state {
stateSlice = append(stateSlice, fmt.Sprintf("%s\t%s", relPath, hash))
}
sort.Strings(stateSlice)
for _, line := range stateSlice {
if _, err := f.WriteString(line + "\n"); err != nil {
return fmt.Errorf("writing state file: %w", err)
}
}
return nil
}