2021-04-14 18:14:01 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
2022-04-23 16:46:54 +00:00
|
|
|
"github.com/bmatcuk/doublestar/v4"
|
2021-04-14 18:14:01 +00:00
|
|
|
"github.com/mickael-menu/zk/internal/util"
|
|
|
|
"github.com/mickael-menu/zk/internal/util/errors"
|
|
|
|
"github.com/mickael-menu/zk/internal/util/paths"
|
|
|
|
strutil "github.com/mickael-menu/zk/internal/util/strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NoteIndex persists and grants access to indexed information about the notes.
|
|
|
|
type NoteIndex interface {
|
|
|
|
// Find retrieves the notes matching the given filtering and sorting criteria.
|
|
|
|
Find(opts NoteFindOpts) ([]ContextualNote, error)
|
|
|
|
// FindMinimal retrieves lightweight metadata for the notes matching the
|
|
|
|
// given filtering and sorting criteria.
|
|
|
|
FindMinimal(opts NoteFindOpts) ([]MinimalNote, error)
|
|
|
|
|
2022-05-22 14:39:13 +00:00
|
|
|
// Find link match returns the best note match for a given link href,
|
|
|
|
// relative to baseDir.
|
|
|
|
FindLinkMatch(baseDir string, href string, linkType LinkType) (NoteID, error)
|
|
|
|
|
2021-11-14 08:50:13 +00:00
|
|
|
// FindLinksBetweenNotes retrieves the links between the given notes.
|
|
|
|
FindLinksBetweenNotes(ids []NoteID) ([]ResolvedLink, error)
|
|
|
|
|
2021-04-14 18:14:01 +00:00
|
|
|
// FindCollections retrieves all the collections of the given kind.
|
2021-10-03 16:36:59 +00:00
|
|
|
FindCollections(kind CollectionKind, sorters []CollectionSorter) ([]Collection, error)
|
2021-04-14 18:14:01 +00:00
|
|
|
|
|
|
|
// Indexed returns the list of indexed note file metadata.
|
|
|
|
IndexedPaths() (<-chan paths.Metadata, error)
|
2021-09-25 17:28:29 +00:00
|
|
|
// Add indexes a new note.
|
2021-04-14 18:14:01 +00:00
|
|
|
Add(note Note) (NoteID, error)
|
|
|
|
// Update resets the metadata of an already indexed note.
|
|
|
|
Update(note Note) error
|
|
|
|
// Remove deletes a note from the index.
|
|
|
|
Remove(path string) error
|
|
|
|
|
|
|
|
// Commit performs a set of operations atomically.
|
|
|
|
Commit(transaction func(idx NoteIndex) error) error
|
|
|
|
|
|
|
|
// NeedsReindexing returns whether all notes should be reindexed.
|
|
|
|
NeedsReindexing() (bool, error)
|
|
|
|
// SetNeedsReindexing indicates whether all notes should be reindexed.
|
|
|
|
SetNeedsReindexing(needsReindexing bool) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// NoteIndexingStats holds statistics about a notebook indexing process.
|
|
|
|
type NoteIndexingStats struct {
|
|
|
|
// Number of notes in the source.
|
2021-05-11 19:53:19 +00:00
|
|
|
SourceCount int `json:"sourceCount"`
|
2021-04-14 18:14:01 +00:00
|
|
|
// Number of newly indexed notes.
|
2021-05-11 19:53:19 +00:00
|
|
|
AddedCount int `json:"addedCount"`
|
2021-04-14 18:14:01 +00:00
|
|
|
// Number of notes modified since last indexing.
|
2021-05-11 19:53:19 +00:00
|
|
|
ModifiedCount int `json:"modifiedCount"`
|
2021-04-14 18:14:01 +00:00
|
|
|
// Number of notes removed since last indexing.
|
2021-05-11 19:53:19 +00:00
|
|
|
RemovedCount int `json:"removedCount"`
|
2021-04-14 18:14:01 +00:00
|
|
|
// Duration of the indexing process.
|
2021-05-11 19:53:19 +00:00
|
|
|
Duration time.Duration `json:"duration"`
|
2021-04-14 18:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// String implements Stringer
|
|
|
|
func (s NoteIndexingStats) String() string {
|
|
|
|
return fmt.Sprintf(`Indexed %d %v in %v
|
|
|
|
+ %d added
|
|
|
|
~ %d modified
|
|
|
|
- %d removed`,
|
|
|
|
s.SourceCount,
|
|
|
|
strutil.Pluralize("note", s.SourceCount),
|
|
|
|
s.Duration.Round(500*time.Millisecond),
|
|
|
|
s.AddedCount, s.ModifiedCount, s.RemovedCount,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-11-13 15:56:32 +00:00
|
|
|
// NoteIndexOpts holds the options for the indexing process.
|
|
|
|
type NoteIndexOpts struct {
|
|
|
|
// When true, existing notes will be reindexed.
|
|
|
|
Force bool
|
|
|
|
Verbose bool
|
|
|
|
}
|
|
|
|
|
2021-04-14 18:14:01 +00:00
|
|
|
// indexTask indexes the notes in the given directory with the NoteIndex.
|
|
|
|
type indexTask struct {
|
2021-11-13 15:56:32 +00:00
|
|
|
path string
|
|
|
|
config Config
|
|
|
|
force bool
|
|
|
|
verbose bool
|
|
|
|
index NoteIndex
|
|
|
|
parser NoteParser
|
|
|
|
logger util.Logger
|
2021-04-14 18:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *indexTask) execute(callback func(change paths.DiffChange)) (NoteIndexingStats, error) {
|
|
|
|
wrap := errors.Wrapper("indexing failed")
|
|
|
|
|
|
|
|
stats := NoteIndexingStats{}
|
|
|
|
startTime := time.Now()
|
|
|
|
|
|
|
|
needsReindexing, err := t.index.NeedsReindexing()
|
|
|
|
if err != nil {
|
|
|
|
return stats, wrap(err)
|
|
|
|
}
|
|
|
|
|
2021-11-13 15:56:32 +00:00
|
|
|
print := func(message string) {
|
|
|
|
if t.verbose {
|
|
|
|
fmt.Println(message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 18:14:01 +00:00
|
|
|
force := t.force || needsReindexing
|
|
|
|
|
2021-11-13 15:56:32 +00:00
|
|
|
type IgnoredFile struct {
|
|
|
|
Path string
|
|
|
|
Reason string
|
|
|
|
}
|
|
|
|
ignoredFiles := []IgnoredFile{}
|
|
|
|
|
2021-07-10 10:21:21 +00:00
|
|
|
shouldIgnorePath := func(path string) (bool, error) {
|
2021-11-13 15:56:32 +00:00
|
|
|
notifyIgnored := func(reason string) {
|
|
|
|
ignoredFiles = append(ignoredFiles, IgnoredFile{
|
|
|
|
Path: path,
|
|
|
|
Reason: reason,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-25 17:28:29 +00:00
|
|
|
group, err := t.config.GroupConfigForPath(path)
|
2021-07-10 10:21:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if filepath.Ext(path) != "."+group.Note.Extension {
|
2021-11-13 15:56:32 +00:00
|
|
|
notifyIgnored("expected extension \"" + group.Note.Extension + "\"")
|
2021-07-10 10:21:21 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2023-05-20 12:10:18 +00:00
|
|
|
for _, ignoreGlob := range group.ExcludeGlobs() {
|
2022-04-23 16:46:54 +00:00
|
|
|
matches, err := doublestar.PathMatch(ignoreGlob, path)
|
2021-07-10 10:21:21 +00:00
|
|
|
if err != nil {
|
2023-05-20 12:10:18 +00:00
|
|
|
return true, errors.Wrapf(err, "failed to match exclude glob %s to %s", ignoreGlob, path)
|
2021-07-10 10:21:21 +00:00
|
|
|
}
|
|
|
|
if matches {
|
2023-05-20 12:10:18 +00:00
|
|
|
notifyIgnored("matched exclude glob \"" + ignoreGlob + "\"")
|
2021-07-10 10:21:21 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-09-25 17:28:29 +00:00
|
|
|
source := paths.Walk(t.path, t.logger, shouldIgnorePath)
|
2021-07-10 10:21:21 +00:00
|
|
|
|
2021-04-14 18:14:01 +00:00
|
|
|
target, err := t.index.IndexedPaths()
|
|
|
|
if err != nil {
|
|
|
|
return stats, wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Use the FS?
|
|
|
|
count, err := paths.Diff(source, target, force, func(change paths.DiffChange) error {
|
|
|
|
callback(change)
|
2021-11-13 15:56:32 +00:00
|
|
|
print("- " + change.Kind.String() + " " + change.Path)
|
2021-10-19 08:57:25 +00:00
|
|
|
absPath := filepath.Join(t.path, change.Path)
|
2021-04-14 18:14:01 +00:00
|
|
|
|
|
|
|
switch change.Kind {
|
|
|
|
case paths.DiffAdded:
|
|
|
|
stats.AddedCount += 1
|
2021-09-25 17:28:29 +00:00
|
|
|
note, err := t.parser.ParseNoteAt(absPath)
|
|
|
|
if note != nil {
|
|
|
|
_, err = t.index.Add(*note)
|
2021-04-14 18:14:01 +00:00
|
|
|
}
|
|
|
|
t.logger.Err(err)
|
|
|
|
|
|
|
|
case paths.DiffModified:
|
|
|
|
stats.ModifiedCount += 1
|
2021-09-25 17:28:29 +00:00
|
|
|
note, err := t.parser.ParseNoteAt(absPath)
|
|
|
|
if note != nil {
|
|
|
|
err = t.index.Update(*note)
|
2021-04-14 18:14:01 +00:00
|
|
|
}
|
|
|
|
t.logger.Err(err)
|
|
|
|
|
|
|
|
case paths.DiffRemoved:
|
|
|
|
stats.RemovedCount += 1
|
|
|
|
err := t.index.Remove(change.Path)
|
|
|
|
t.logger.Err(err)
|
|
|
|
}
|
2021-11-13 15:56:32 +00:00
|
|
|
|
2021-04-14 18:14:01 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2021-11-13 15:56:32 +00:00
|
|
|
for _, ignored := range ignoredFiles {
|
|
|
|
print("- ignored " + ignored.Path + ": " + ignored.Reason)
|
|
|
|
}
|
|
|
|
|
2021-04-14 18:14:01 +00:00
|
|
|
stats.SourceCount = count
|
|
|
|
stats.Duration = time.Since(startTime)
|
|
|
|
|
|
|
|
if needsReindexing {
|
|
|
|
err = t.index.SetNeedsReindexing(false)
|
|
|
|
}
|
|
|
|
|
2021-11-13 15:56:32 +00:00
|
|
|
print("")
|
2021-04-14 18:14:01 +00:00
|
|
|
return stats, wrap(err)
|
|
|
|
}
|