-
Notifications
You must be signed in to change notification settings - Fork 114
/
note_index.go
231 lines (199 loc) · 5.56 KB
/
note_index.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
package sqlite
import (
"github.com/mickael-menu/zk/internal/core"
"github.com/mickael-menu/zk/internal/util"
"github.com/mickael-menu/zk/internal/util/errors"
"github.com/mickael-menu/zk/internal/util/paths"
)
// NoteIndex persists note indexing results in the SQLite database.
// It implements the port core.NoteIndex and acts as a facade to the DAOs.
type NoteIndex struct {
db *DB
dao *dao
logger util.Logger
}
type dao struct {
notes *NoteDAO
links *LinkDAO
collections *CollectionDAO
metadata *MetadataDAO
}
func NewNoteIndex(db *DB, logger util.Logger) *NoteIndex {
return &NoteIndex{
db: db,
logger: logger,
}
}
// Find implements core.NoteIndex.
func (ni *NoteIndex) Find(opts core.NoteFindOpts) (notes []core.ContextualNote, err error) {
err = ni.commit(func(dao *dao) error {
notes, err = dao.notes.Find(opts)
return err
})
return
}
// FindMinimal implements core.NoteIndex.
func (ni *NoteIndex) FindMinimal(opts core.NoteFindOpts) (notes []core.MinimalNote, err error) {
err = ni.commit(func(dao *dao) error {
notes, err = dao.notes.FindMinimal(opts)
return err
})
return
}
// FindLinksBetweenNotes implements core.NoteIndex.
func (ni *NoteIndex) FindLinksBetweenNotes(ids []core.NoteID) (links []core.ResolvedLink, err error) {
err = ni.commit(func(dao *dao) error {
links, err = dao.links.FindBetweenNotes(ids)
return err
})
return
}
// FindCollections implements core.NoteIndex.
func (ni *NoteIndex) FindCollections(kind core.CollectionKind, sorters []core.CollectionSorter) (collections []core.Collection, err error) {
err = ni.commit(func(dao *dao) error {
collections, err = dao.collections.FindAll(kind, sorters)
return err
})
return
}
// IndexedPaths implements core.NoteIndex.
func (ni *NoteIndex) IndexedPaths() (metadata <-chan paths.Metadata, err error) {
err = ni.commit(func(dao *dao) error {
metadata, err = dao.notes.Indexed()
return err
})
err = errors.Wrap(err, "failed to get indexed notes")
return
}
// Add implements core.NoteIndex.
func (ni *NoteIndex) Add(note core.Note) (id core.NoteID, err error) {
err = ni.commit(func(dao *dao) error {
id, err = dao.notes.Add(note)
if err != nil {
return err
}
err = ni.addLinks(dao, id, note)
if err != nil {
return err
}
return ni.associateTags(dao.collections, id, note.Tags)
})
err = errors.Wrapf(err, "%v: failed to index the note", note.Path)
return
}
// Update implements core.NoteIndex.
func (ni *NoteIndex) Update(note core.Note) error {
err := ni.commit(func(dao *dao) error {
id, err := dao.notes.Update(note)
if err != nil {
return err
}
// Reset links
err = dao.links.RemoveAll(id)
if err != nil {
return err
}
err = ni.addLinks(dao, id, note)
if err != nil {
return err
}
// Reset tags
err = dao.collections.RemoveAssociations(id)
if err != nil {
return err
}
return ni.associateTags(dao.collections, id, note.Tags)
})
return errors.Wrapf(err, "%v: failed to update note index", note.Path)
}
func (ni *NoteIndex) associateTags(collections *CollectionDAO, noteId core.NoteID, tags []string) error {
for _, tag := range tags {
tagId, err := collections.FindOrCreate(core.CollectionKindTag, tag)
if err != nil {
return err
}
_, err = collections.Associate(noteId, tagId)
if err != nil {
return err
}
}
return nil
}
func (ni *NoteIndex) addLinks(dao *dao, id core.NoteID, note core.Note) error {
links, err := ni.resolveLinkNoteIDs(dao, id, note.Links)
if err != nil {
return err
}
err = dao.links.Add(links)
if err != nil {
return err
}
return dao.links.SetTargetID(note.Path, id)
}
func (ni *NoteIndex) resolveLinkNoteIDs(dao *dao, sourceID core.NoteID, links []core.Link) ([]core.ResolvedLink, error) {
resolvedLinks := []core.ResolvedLink{}
for _, link := range links {
allowPartialMatch := (link.Type == core.LinkTypeWikiLink)
targetID, err := dao.notes.FindIdByHref(link.Href, allowPartialMatch)
if err != nil {
return resolvedLinks, err
}
resolvedLinks = append(resolvedLinks, core.ResolvedLink{
Link: link,
SourceID: sourceID,
TargetID: targetID,
})
}
return resolvedLinks, nil
}
// Remove implements core.NoteIndex
func (ni *NoteIndex) Remove(path string) error {
err := ni.commit(func(dao *dao) error {
return dao.notes.Remove(path)
})
return errors.Wrapf(err, "%v: failed to remove note from index", path)
}
// Commit implements core.NoteIndex.
func (ni *NoteIndex) Commit(transaction func(idx core.NoteIndex) error) error {
return ni.commit(func(dao *dao) error {
return transaction(&NoteIndex{
db: ni.db,
dao: dao,
logger: ni.logger,
})
})
}
// NeedsReindexing implements core.NoteIndex.
func (ni *NoteIndex) NeedsReindexing() (needsReindexing bool, err error) {
err = ni.commit(func(dao *dao) error {
res, err := dao.metadata.Get(reindexingRequiredKey)
needsReindexing = (res == "true")
return err
})
return
}
// SetNeedsReindexing implements core.NoteIndex.
func (ni *NoteIndex) SetNeedsReindexing(needsReindexing bool) error {
return ni.commit(func(dao *dao) error {
value := "false"
if needsReindexing {
value = "true"
}
return dao.metadata.Set(reindexingRequiredKey, value)
})
}
func (ni *NoteIndex) commit(transaction func(dao *dao) error) error {
if ni.dao != nil {
return transaction(ni.dao)
} else {
return ni.db.WithTransaction(func(tx Transaction) error {
dao := dao{
notes: NewNoteDAO(tx, ni.logger),
links: NewLinkDAO(tx, ni.logger),
collections: NewCollectionDAO(tx, ni.logger),
metadata: NewMetadataDAO(tx),
}
return transaction(&dao)
})
}
}