-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.go
430 lines (380 loc) · 10.1 KB
/
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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package fs
import (
"encoding/gob"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"sync"
"github.com/RoaringBitmap/roaring/roaring64"
vocab "github.com/go-ap/activitypub"
"github.com/go-ap/errors"
"github.com/go-ap/filters"
"github.com/go-ap/filters/index"
)
type bitmaps struct {
w sync.RWMutex
ref map[uint64]string
all map[index.Type]index.Indexable
}
var genericIndexTypes = []index.Type{
index.ByID, index.ByType,
index.ByRecipients, index.ByAttributedTo,
index.ByName, index.BySummary, index.ByContent,
}
var allIndexTypes = append(genericIndexTypes,
index.ByPreferredUsername, index.ByActor, index.ByObject /*, index.ByCollection*/)
func newBitmap(typ ...index.Type) *bitmaps {
if len(typ) == 0 {
typ = allIndexTypes
}
b := bitmaps{
ref: make(map[uint64]string),
all: make(map[index.Type]index.Indexable),
}
for _, tt := range typ {
switch tt {
case index.ByID:
b.all[tt] = index.All()
case index.ByType:
b.all[tt] = index.NewTokenIndex(index.ExtractType)
case index.ByName:
b.all[tt] = index.NewTokenIndex(index.ExtractName)
case index.ByPreferredUsername:
b.all[tt] = index.NewTokenIndex(index.ExtractPreferredUsername)
case index.BySummary:
b.all[tt] = index.NewTokenIndex(index.ExtractSummary)
case index.ByContent:
b.all[tt] = index.NewTokenIndex(index.ExtractContent)
case index.ByActor:
b.all[tt] = index.NewTokenIndex(index.ExtractActor)
case index.ByObject:
b.all[tt] = index.NewTokenIndex(index.ExtractObject)
case index.ByRecipients:
b.all[tt] = index.NewTokenIndex(index.ExtractRecipients)
case index.ByAttributedTo:
b.all[tt] = index.NewTokenIndex(index.ExtractAttributedTo)
}
}
return &b
}
// searchIndex does a fast search for the received filters.
func (r *repo) searchIndex(col vocab.Item, ff ...filters.Check) (vocab.ItemCollection, error) {
if r.index == nil {
return nil, cacheDisabled
}
if len(ff) == 0 {
return nil, errors.Errorf("nil filters for index search")
}
i := r.index
i.w.RLock()
defer i.w.RUnlock()
idxPath := r.collectionIndexStoragePath(col.GetLink())
bmp := filters.Checks(ff).IndexMatch(i.all)
colBmp := roaring64.New()
_ = r.loadBinFromFile(idxPath, colBmp)
bmp.And(colBmp)
if bmp.IsEmpty() {
return nil, nil
}
result := make(vocab.ItemCollection, 0, bmp.GetCardinality())
it := bmp.Iterator()
for it.HasNext() {
x := it.Next()
if ip, ok := i.ref[x]; ok {
prefix := r.root.Name()
if !strings.Contains(ip, prefix) {
ip = filepath.Join(prefix, ip)
}
ob, err := loadRawFromPath(r.root, getObjectKey(ip))
if err != nil {
continue
}
result = append(result, ob)
}
}
return result, nil
}
const _indexDirName = ".index"
func (r *repo) indexStoragePath() string {
return filepath.Join(_indexDirName)
}
func (r *repo) collectionIndexStoragePath(col vocab.IRI) string {
return filepath.Join(iriPath(col), _indexDirName)
}
func getIndexKey(typ index.Type) string {
switch typ {
case index.ByID:
return ".all.gob"
case index.ByType:
return ".type.gob"
case index.ByName:
return ".name.gob"
case index.ByPreferredUsername:
return ".preferredUsername.gob"
case index.BySummary:
return ".summary.gob"
case index.ByContent:
return ".content.gob"
case index.ByActor:
return ".actor.gob"
case index.ByObject:
return ".object.gob"
case index.ByRecipients:
return ".recipients.gob"
case index.ByAttributedTo:
return ".attributedTo.gob"
}
return ""
}
const _refName = ".ref.gob"
func (r *repo) writeBinFile(path string, bmp any) error {
f, err := r.root.OpenFile(path, defaultNewFileFlags, defaultFilePerm)
if err != nil {
r.logger.Warnf("%s not found", path)
return errors.NewNotFound(asPathErr(err, r.path), "not found")
}
defer func() {
if err := f.Close(); err != nil {
r.logger.Warnf("Unable to close file: %s", asPathErr(err, r.path))
}
}()
return gob.NewEncoder(f).Encode(bmp)
}
func saveIndex(r *repo) error {
if r.index == nil {
return nil
}
idxPath := r.indexStoragePath()
r.root.Mkdir(idxPath, defaultDirPerm)
_ = mkDirIfNotExists(r.root, idxPath)
r.index.w.RLock()
defer r.index.w.RUnlock()
errs := make([]error, 0, len(r.index.all))
for typ, bmp := range r.index.all {
if err := r.writeBinFile(filepath.Join(idxPath, getIndexKey(typ)), bmp); err != nil {
errs = append(errs, err)
}
}
if err := r.writeBinFile(filepath.Join(idxPath, _refName), r.index.ref); err != nil {
errs = append(errs, err)
}
return errors.Join(errs...)
}
func (r *repo) loadBinFromFile(path string, bmp any) (err error) {
f, err := r.root.OpenFile(path, os.O_RDONLY, defaultFilePerm)
if err != nil {
return err
}
defer func() {
err = f.Close()
}()
if err = gob.NewDecoder(f).Decode(bmp); err != nil {
return err
}
return nil
}
func loadIndex(r *repo) error {
if r.index == nil {
return nil
}
r.index.w.Lock()
defer r.index.w.Unlock()
errs := make([]error, 0, len(r.index.all))
idxPath := r.indexStoragePath()
for typ, bmp := range r.index.all {
if err := r.loadBinFromFile(filepath.Join(idxPath, getIndexKey(typ)), bmp); err != nil {
errs = append(errs, err)
}
}
if err := r.loadBinFromFile(filepath.Join(idxPath, _refName), &r.index.ref); err != nil {
errs = append(errs, err)
}
return errors.Join(errs...)
}
var cacheDisabled = errors.NotImplementedf("index is disabled")
func onCollectionBitmap(bmp *roaring64.Bitmap, it vocab.Item, fn func(*roaring64.Bitmap, uint64)) error {
if bmp == nil {
return cacheDisabled
}
hashFn := index.HashFn
if hashFn == nil {
return cacheDisabled
}
fn(bmp, hashFn(it.GetLink()))
return nil
}
func (r *repo) removeFromIndex(it vocab.Item, path string) error {
if r.index == nil {
return cacheDisabled
}
if vocab.IsNil(it) {
return errors.NotFoundf("nil item")
}
in := r.index
errs := make([]error, 0)
switch {
case vocab.ActivityTypes.Contains(it.GetType()):
if iact, ok := in.all[index.ByActor]; ok {
_ = iact.Add(it)
}
if iob, ok := in.all[index.ByObject]; ok {
_ = iob.Add(it)
}
case vocab.IntransitiveActivityTypes.Contains(it.GetType()):
if iact, ok := in.all[index.ByActor]; ok {
_ = iact.Add(it)
}
case vocab.ActorTypes.Contains(it.GetType()):
if ipu, ok := in.all[index.ByPreferredUsername]; ok {
_ = ipu.Add(it)
}
}
type remover interface {
Remove(vocab.LinkOrIRI) error
}
// NOTE(marius): all objects should get added to these indexes
for _, gi := range allIndexTypes {
i, ok := in.all[gi]
if !ok {
continue
}
rem, ok := i.(remover)
if !ok {
continue
}
if err := rem.Remove(it); err != nil {
errs = append(errs, err)
continue
}
}
return errors.Join(errs...)
}
func (r *repo) addToIndex(it vocab.Item, path string) error {
if r.index == nil {
return cacheDisabled
}
if vocab.IsNil(it) {
return errors.NotFoundf("nil item")
}
in := r.index
in.w.Lock()
defer in.w.Unlock()
switch {
case vocab.ActivityTypes.Contains(it.GetType()):
if iact, ok := in.all[index.ByActor]; ok {
_ = iact.Add(it)
}
if iob, ok := in.all[index.ByObject]; ok {
_ = iob.Add(it)
}
case vocab.IntransitiveActivityTypes.Contains(it.GetType()):
if iact, ok := in.all[index.ByActor]; ok {
_ = iact.Add(it)
}
case vocab.ActorTypes.Contains(it.GetType()):
if ipu, ok := in.all[index.ByPreferredUsername]; ok {
_ = ipu.Add(it)
}
}
var itemRef uint64
// NOTE(marius): all objects should get added to these indexes
for _, gi := range genericIndexTypes {
if ig, ok := in.all[gi]; ok {
itemRef = ig.Add(it)
}
}
in.ref[itemRef] = path
return nil
}
func (r *repo) iriFromPath(p string) vocab.IRI {
p = strings.Trim(strings.TrimSuffix(strings.Replace(p, r.root.Name(), "", 1), objectKey), "/")
return vocab.IRI(fmt.Sprintf("https://%s", p))
}
func (r *repo) collectionBitmapOp(fn func(*roaring64.Bitmap, uint64), items ...vocab.Item) func(col vocab.CollectionInterface) error {
return func(col vocab.CollectionInterface) error {
iri := col.GetLink()
idxPath := r.collectionIndexStoragePath(iri)
bmp := roaring64.New()
if err := r.loadBinFromFile(idxPath, bmp); err != nil {
//r.logger.Warnf("Unable to load collection index %s: %s", iri, err)
}
wasEmpty := bmp.GetCardinality() == 0
// NOTE(marius): this is terrible, we're using the same function for indexing a full collection
// but also to add a single item to the collection index.
if len(items) == 0 {
items = col.Collection()
}
for _, ob := range items {
if err := onCollectionBitmap(bmp, ob, fn); err != nil {
if errors.IsNotImplemented(err) {
return fs.SkipAll
}
r.logger.Warnf("Unable to add item %s to index: %s", iri, err)
}
}
// NOTE(marius): if there was nothing in the bitmap, and we didn't add
// anything either, we don't save the collection file.
if isEmpty := bmp.GetCardinality() == 0; isEmpty {
if wasEmpty {
return nil
}
// NOTE(marius): if the collection wasn't empty and we removed the last item from it,
// we can remove the collection index file.
return os.RemoveAll(idxPath)
}
return r.writeBinFile(idxPath, bmp)
}
}
func (r *repo) Reindex() (err error) {
//if err = r.Open(); err != nil {
// return err
//}
//defer r.Close()
if err = loadIndex(r); err != nil {
//r.logger.Warnf("Unable to load indexes: %s", err)
}
defer func() {
err = saveIndex(r)
}()
root := r.root.FS()
err = fs.WalkDir(root, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.Type().IsDir() {
return nil
}
if d.Name() != objectKey {
return nil
}
var it vocab.Item
dir := filepath.Dir(path)
maybeCol := filepath.Base(dir)
iri := r.iriFromPath(dir)
if storageCollectionPaths.Contains(vocab.CollectionPath(maybeCol)) {
it, err = r.loadCollectionFromPath(filepath.Join(path), iri)
if err == nil {
err = vocab.OnCollectionIntf(it, r.collectionBitmapOp((*roaring64.Bitmap).Add))
}
} else {
it, err = r.loadItemFromPath(filepath.Join(path))
}
if err != nil || vocab.IsNil(it) {
return nil
}
if err = r.addToIndex(it, dir); err != nil {
if errors.IsNotImplemented(err) {
return fs.SkipAll
}
r.logger.Warnf("Unable to add item %s to index: %s", iri, err)
}
r.logger.Debugf("Indexed: %s", it.GetLink())
return nil
})
if err != nil {
return err
}
return nil
}