-
Notifications
You must be signed in to change notification settings - Fork 0
/
vault_scanner.go
412 lines (313 loc) · 9.01 KB
/
vault_scanner.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
// Vault scanner
package main
import (
"sort"
)
const MAX_TAGS_FILTER_COUNT = 16
type TagFilterMode int
const (
TagFilterAllOf TagFilterMode = 0
TagFilterAnyOf TagFilterMode = 1
TagFilterNoneOf TagFilterMode = 2
)
// Entry to store a tag id within its media count
type TagIndexMetadata struct {
// Tag ID
id uint64
// Size of the index
count int64
// Index manager
index *VaultMainIndex
// Index file
indexFile *IndexedListFile
}
// Vault scanner
type VaultScanner struct {
// Tag filter mode
tagFilterMode TagFilterMode
// Indexes of tags
tagIndexes []*TagIndexMetadata
// Main index of the vault
mainIndexFile *IndexedListFile
// Index being scanned
scanningIndex *IndexedListFile
// Count pre-calc if the scanning index
scanningIndexCount int64
// True for reverse order
reversed bool
// Current index
currentIndex int64
}
// Setups a vault scanner
// tagMode - Tag filtering mode
// reversed - True if reverse order
// tagNames - List of tag names
// continueRef - Reference to continue
// key - Vault decryption key
// Returns a reference to the created VaultScanner
// May return null if an error occurs, or if some tags are not found, meaning the list is empty
func NewVaultScanner(tagMode TagFilterMode, reversed bool, tagNames []string, continueRef int64, key []byte) (*VaultScanner, error) {
tagList, err := GetVault().tags.ReadList(key)
if err != nil {
return nil, err
}
// Find tags
if len(tagNames) > MAX_TAGS_FILTER_COUNT {
tagNames = tagNames[:MAX_TAGS_FILTER_COUNT]
}
tags := make([]uint64, 0)
for _, tagName := range tagNames {
found, tagId := tagList.FindTag(tagName)
if !found {
if tagMode == TagFilterAllOf {
// If we require all tags, but one does not exists, the list is empty
return nil, nil
}
continue
}
tags = append(tags, tagId)
}
if tagMode == TagFilterAnyOf && len(tags) == 0 {
// If we require at least one tag, but no tags are provided, the list is empty
return nil, nil
}
// Create vault scanner
vaultScanner := &VaultScanner{
tagFilterMode: tagMode,
reversed: reversed,
}
// Load main index if necessary
if tagMode == TagFilterNoneOf || (tagMode == TagFilterAllOf && len(tags) == 0) || (tagMode == TagFilterAnyOf && len(tags) > 1) {
indexFile, err := GetVault().index.StartRead()
if err != nil {
return nil, err
}
vaultScanner.mainIndexFile = indexFile
vaultScanner.scanningIndex = indexFile
count, err := indexFile.Count()
if err != nil {
GetVault().index.EndRead(vaultScanner.mainIndexFile)
return nil, err
}
vaultScanner.scanningIndexCount = count
}
// Load and sort tag indexes
sortedTags := make([]*TagIndexMetadata, len(tags))
for i := 0; i < len(tags); i++ {
meta := &TagIndexMetadata{
id: tags[i],
}
tagIndex, err := GetVault().tags.AcquireIndexFile(meta.id)
if err != nil {
// Release all acquired tags
for j := i - 1; j >= 0; j-- {
sortedTags[j].index.EndRead(sortedTags[j].indexFile)
GetVault().tags.ReleaseIndexFile(sortedTags[j].id, false, key)
}
// Release main index if acquired
if vaultScanner.mainIndexFile != nil {
GetVault().index.EndRead(vaultScanner.mainIndexFile)
}
return nil, err
}
meta.index = tagIndex
tagIndexFile, err := tagIndex.StartRead()
if err != nil {
// Release current tag
GetVault().tags.ReleaseIndexFile(meta.id, false, key)
// Release all acquired tags
for j := i - 1; j >= 0; j-- {
sortedTags[j].index.EndRead(sortedTags[j].indexFile)
GetVault().tags.ReleaseIndexFile(sortedTags[j].id, false, key)
}
// Release main index if acquired
if vaultScanner.mainIndexFile != nil {
GetVault().index.EndRead(vaultScanner.mainIndexFile)
}
return nil, err
}
meta.indexFile = tagIndexFile
count, err := tagIndexFile.Count()
if err != nil {
// Release current tag
tagIndex.EndRead(tagIndexFile)
GetVault().tags.ReleaseIndexFile(meta.id, false, key)
// Release all acquired tags
for j := i - 1; j >= 0; j-- {
sortedTags[j].index.EndRead(sortedTags[j].indexFile)
GetVault().tags.ReleaseIndexFile(sortedTags[j].id, false, key)
}
// Release main index if acquired
if vaultScanner.mainIndexFile != nil {
GetVault().index.EndRead(vaultScanner.mainIndexFile)
}
return nil, err
}
meta.count = count
sortedTags[i] = meta
}
if tagMode == TagFilterAllOf {
sort.Slice(sortedTags, func(i, j int) bool {
return sortedTags[i].count < sortedTags[j].count
})
} else {
sort.Slice(sortedTags, func(i, j int) bool {
return sortedTags[i].count > sortedTags[j].count
})
}
vaultScanner.tagIndexes = sortedTags
if tagMode == TagFilterAllOf && len(sortedTags) > 0 {
vaultScanner.scanningIndex = sortedTags[0].indexFile
vaultScanner.scanningIndexCount = sortedTags[0].count
} else if tagMode == TagFilterAnyOf && len(sortedTags) == 1 {
vaultScanner.scanningIndex = sortedTags[len(sortedTags)-1].indexFile
vaultScanner.scanningIndexCount = sortedTags[len(sortedTags)-1].count
}
// Position the scanner properly based on the continue value
if vaultScanner.scanningIndex == nil {
vaultScanner.Release(key)
return nil, nil
}
if continueRef >= 0 {
// Find continue value in the index file
found, foundIndex, err := vaultScanner.scanningIndex.BinarySearchWithCountPreCalc(uint64(continueRef), vaultScanner.scanningIndexCount)
if err != nil {
vaultScanner.Release(key)
return nil, err
}
if reversed {
if found {
vaultScanner.currentIndex = foundIndex - 1
} else {
vaultScanner.currentIndex = foundIndex
}
} else {
vaultScanner.currentIndex = foundIndex + 1
}
} else if reversed {
vaultScanner.currentIndex = vaultScanner.scanningIndexCount - 1
} else {
vaultScanner.currentIndex = 0
}
return vaultScanner, nil
}
// Releases all the resources acquired by this scanner
func (scanner *VaultScanner) Release(key []byte) {
if scanner.mainIndexFile != nil {
GetVault().index.EndRead(scanner.mainIndexFile)
}
for _, tie := range scanner.tagIndexes {
tie.index.EndRead(tie.indexFile)
GetVault().tags.ReleaseIndexFile(tie.id, false, key)
}
}
// Finds the next value
// Returns:
// - found: true only if the scanner has a value to return, false means the scanner has reached the end
// - id: The id of the next media element
// - err: Error
func (scanner *VaultScanner) Next() (found bool, id uint64, err error) {
for (!scanner.reversed && scanner.currentIndex < scanner.scanningIndexCount) || (scanner.reversed && scanner.currentIndex >= 0) {
// Read value
current, err := scanner.scanningIndex.ReadValue(scanner.currentIndex)
if err != nil {
return false, 0, err
}
// Increment index
if scanner.reversed {
scanner.currentIndex--
} else {
scanner.currentIndex++
}
// Filter
switch scanner.tagFilterMode {
case TagFilterAllOf:
p, err := scanner.checkAllTags(current)
if err != nil {
return false, 0, err
}
if !p {
continue
}
case TagFilterAnyOf:
p, err := scanner.checkAnyTag(current)
if err != nil {
return false, 0, err
}
if !p {
continue
}
case TagFilterNoneOf:
p, err := scanner.checkNoneTag(current)
if err != nil {
return false, 0, err
}
if !p {
continue
}
}
// Return
return true, current, nil
}
// End reached
return false, 0, nil
}
// Checks all tags
// Returns true only if the media ID is present in all the indexed lists
func (scanner *VaultScanner) checkAllTags(id uint64) (passes bool, err error) {
if len(scanner.tagIndexes) < 2 {
return true, nil
}
for _, ie := range scanner.tagIndexes[1:] {
found, _, err := ie.indexFile.BinarySearchWithCountPreCalc(id, ie.count)
if err != nil {
return false, err
}
if !found {
return false, nil
}
}
return true, nil
}
// Checks any tag
// Returns true only if the media ID is present in at least one of the indexed lists
func (scanner *VaultScanner) checkAnyTag(id uint64) (passes bool, err error) {
for _, ie := range scanner.tagIndexes {
found, _, err := ie.indexFile.BinarySearchWithCountPreCalc(id, ie.count)
if err != nil {
return false, err
}
if found {
return true, nil
}
}
return false, nil
}
// Checks none of the tags contain the media
// Returns true only if the media ID is not present in any of the indexed lists
func (scanner *VaultScanner) checkNoneTag(id uint64) (passes bool, err error) {
for _, ie := range scanner.tagIndexes {
found, _, err := ie.indexFile.BinarySearchWithCountPreCalc(id, ie.count)
if err != nil {
return false, err
}
if found {
return false, nil
}
}
return true, nil
}
// Gets current scanner progress
func (scanner *VaultScanner) GetProgress() (scanned int64, total int64) {
if scanner.reversed {
if scanner.currentIndex < 0 {
return scanner.scanningIndexCount, scanner.scanningIndexCount
}
return scanner.scanningIndexCount - scanner.currentIndex - 1, scanner.scanningIndexCount
} else {
if scanner.currentIndex >= scanner.scanningIndexCount {
return scanner.scanningIndexCount, scanner.scanningIndexCount
}
return scanner.currentIndex, scanner.scanningIndexCount
}
}