aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/index/index.go
blob: 15a5e869d349b7df0f0664760da9beeb2294dde6 (plain) (blame)
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
package index

import (
	"errors"
	"fmt"
	"io"
	"log/slog"
	"os"
	"slices"
	"strings"
	"sync"
	"time"

	"github.com/goccy/go-yaml"
	"github.com/goccy/go-yaml/ast"
	"github.com/jpappel/atlas/pkg/util"
)

var ErrHeaderParse error = errors.New("Unable to parse YAML header")

type Document struct {
	Path      string    `yaml:"-" json:"path"`
	Title     string    `yaml:"title" json:"title"`
	Date      time.Time `yaml:"-" json:"date"`
	FileTime  time.Time `yaml:"-" json:"filetime"`
	Authors   []string  `yaml:"-" json:"authors"`
	Tags      []string  `yaml:"tags,omitempty" json:"tags"`
	Links     []string  `yaml:"-" json:"links"`
	OtherMeta string    `yaml:"-" json:"meta"`
}

type infoPath struct {
	path string
	info os.FileInfo
}

type Index struct {
	Root      string // root directory for searching
	Documents map[string]*Document
	Filters   []DocFilter
}

func (idx Index) String() string {
	b := strings.Builder{}
	fmt.Fprintf(&b, "%s Documents[%d]\n", idx.Root, len(idx.Documents))
	fmt.Fprintf(&b, "Filters[%d]: ", len(idx.Filters))

	for i, docFilter := range idx.Filters {
		b.WriteString(docFilter.Name)
		if i != len(idx.Filters) {
			b.WriteByte(',')
		}
	}

	return b.String()
}

var _ yaml.NodeUnmarshaler = (*Document)(nil)

func (doc *Document) UnmarshalYAML(node ast.Node) error {
	// parse top level fields
	type alias Document
	var temp alias
	if err := yaml.NodeToValue(node, &temp); err != nil {
		return err
	}
	doc.Title = temp.Title
	doc.Tags = temp.Tags

	mapnode, ok := node.(*ast.MappingNode)
	if !ok {
		return ErrHeaderParse
	}

	ignored_keyPaths := map[string]bool{
		"$.title": true,
		"$.tags":  true,
	}

	buf := strings.Builder{}
	for _, kv := range mapnode.Values {
		k, v := kv.Key, kv.Value
		keyPath := k.GetPath()

		if ignored_keyPaths[keyPath] {
			continue
		}

		if keyPath == "$.date" {
			if err := doc.parseDateNode(v); err != nil {
				return err
			}
		} else if keyPath == "$.author" {
			if err := doc.parseAuthor(v); err != nil {
				return err
			}
		} else {
			field, err := kv.MarshalYAML()
			if err != nil {
				return err
			}
			buf.Write(field)
			buf.WriteByte('\n')
		}
	}

	doc.OtherMeta = buf.String()

	return nil
}

func (doc *Document) parseDateNode(node ast.Node) error {
	dateNode, ok := node.(*ast.StringNode)
	if !ok {
		return ErrHeaderParse
	}
	dateStr := dateNode.Value

	if dateStr == "" {
		return nil
	}

	var err error
	if doc.Date, err = util.ParseDateTime(dateStr); err != nil {
		return fmt.Errorf("Unable to parse date: %s", dateNode.Value)
	}

	return nil
}

func (doc *Document) parseAuthor(node ast.Node) error {
	authorsNode, ok := node.(*ast.SequenceNode)
	if ok {
		doc.Authors = make([]string, 0, len(authorsNode.Values))
		for _, authorNode := range authorsNode.Values {
			authorStrNode, ok := authorNode.(*ast.StringNode)
			if !ok {
				return ErrHeaderParse
			}
			doc.Authors = append(doc.Authors, authorStrNode.Value)
		}
	} else {
		authorNode, ok := node.(*ast.StringNode)
		if ok {
			doc.Authors = []string{authorNode.Value}
		} else {
			return ErrHeaderParse
		}
	}

	return nil
}

func (doc Document) Equal(other Document) bool {
	if len(doc.Authors) != len(other.Authors) || len(doc.Tags) != len(other.Tags) || len(doc.Links) != len(other.Links) || doc.Path != other.Path || doc.Title != other.Title || doc.OtherMeta != other.OtherMeta || !doc.Date.Equal(other.Date) {
		return false
	}

	if !slices.Equal(doc.Authors, other.Authors) {
		return false
	}

	slices.Sort(doc.Tags)
	slices.Sort(other.Tags)
	for i := range doc.Tags {
		if doc.Tags[i] != other.Tags[i] {
			return false
		}
	}

	slices.Sort(doc.Links)
	slices.Sort(other.Links)
	for i := range doc.Links {
		if doc.Links[i] != other.Links[i] {
			return false
		}
	}

	return true
}

func visit(file infoPath, visitQueue chan<- infoPath, filterQueue chan<- infoPath, wg *sync.WaitGroup) {
	// TODO: check if symlink, and handle appropriately
	// TODO: extract error out of function

	if file.info.IsDir() {
		entries, err := os.ReadDir(file.path)
		if err != nil {
			panic(err)
		}

		wg.Add(len(entries))
		for _, entry := range entries {
			entryInfo, err := entry.Info()
			if err != nil {
				panic(err)
			}
			// PERF: prevents deadlock but introduces an additional goroutine overhead per file
			go func(path string) {
				visitQueue <- infoPath{path: path, info: entryInfo}
			}(file.path + "/" + entry.Name())
		}
	} else if file.info.Mode().IsRegular() {
		filterQueue <- file
	}

	wg.Done()
}

func workerTraverse(wg *sync.WaitGroup, visitQueue chan infoPath, filterQueue chan<- infoPath) {
	for work := range visitQueue {
		visit(work, visitQueue, filterQueue, wg)
	}
}

func (idx Index) Traverse(numWorkers uint) []string {
	if numWorkers <= 1 {
		panic(fmt.Sprint("Invalid number of workers: ", numWorkers))
	}
	docs := make([]string, 0)

	rootInfo, err := os.Stat(idx.Root)
	if err != nil {
		panic(err)
	}

	jobs := make(chan infoPath, numWorkers)
	filterQueue := make(chan infoPath, numWorkers)

	activeJobs := &sync.WaitGroup{}

	// start workers
	for range numWorkers {
		go workerTraverse(activeJobs, jobs, filterQueue)
	}

	// init send
	activeJobs.Add(1)
	jobs <- infoPath{path: idx.Root, info: rootInfo}

	// close jobs queue
	go func() {
		activeJobs.Wait()
		close(jobs)
		close(filterQueue)
	}()

	// gather
	for doc := range filterQueue {
		docs = append(docs, doc.path)
	}

	return docs
}

func (idx Index) FilterOne(path string) bool {
	info, err := os.Stat(string(path))
	if err != nil {
		return false
	}

	f, err := os.Open(string(path))
	if err != nil {
		return false
	}
	defer f.Close()

	for _, docFilter := range idx.Filters {
		if !docFilter.Filter(infoPath{string(path), info}, f) {
			return false
		}
		if _, err := f.Seek(0, io.SeekStart); err != nil {
			return false
		}
	}
	return true
}

func (idx Index) Filter(paths []string, numWorkers uint) []string {
	fPaths := make([]string, 0, len(paths))
	jobs := make(chan string, numWorkers)
	accepted := make(chan string, numWorkers)
	wg := &sync.WaitGroup{}

	wg.Add(int(numWorkers))
	for range numWorkers {
		go func(jobs <-chan string, accepted chan<- string, wg *sync.WaitGroup) {
			for path := range jobs {
				if idx.FilterOne(path) {
					accepted <- path
				}
			}
			wg.Done()
		}(jobs, accepted, wg)
	}

	go func(jobs chan<- string) {
		for _, path := range paths {
			jobs <- path
		}
		close(jobs)
	}(jobs)

	go func() {
		wg.Wait()
		close(accepted)
	}()

	for path := range accepted {
		fPaths = append(fPaths, path)
	}

	return fPaths
}

func ParseDoc(path string) (*Document, error) {
	doc := &Document{}
	doc.Path = path

	f, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	defer f.Close()

	info, err := f.Stat()
	if err != nil {
		return nil, err
	}
	doc.FileTime = info.ModTime()

	if err := yaml.NewDecoder(f).Decode(doc); err != nil {
		return nil, errors.Join(ErrHeaderParse, err)
	}

	// TODO: read the rest of the file to find links
	return doc, nil
}

func ParseDocs(paths []string, numWorkers uint) map[string]*Document {
	jobs := make(chan string, numWorkers)
	results := make(chan Document, numWorkers)
	docs := make(map[string]*Document, len(paths))
	wg := &sync.WaitGroup{}

	wg.Add(int(numWorkers))
	for range numWorkers {
		go func(jobs <-chan string, results chan<- Document, wg *sync.WaitGroup) {
			for path := range jobs {
				doc, err := ParseDoc(path)
				if err != nil {
					// TODO: propagate error
					slog.Error("Error occured while parsing file",
						slog.String("path", path), slog.String("err", err.Error()),
					)
					continue
				}

				results <- *doc
			}
			wg.Done()
		}(jobs, results, wg)
	}

	go func(jobs chan<- string, paths []string) {
		for _, path := range paths {
			jobs <- path
		}
		close(jobs)
	}(jobs, paths)

	go func(results chan Document, wg *sync.WaitGroup) {
		wg.Wait()
		close(results)
	}(results, wg)

	for doc := range results {
		docs[doc.Path] = &doc
	}

	return docs
}