-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexed_list.go
526 lines (397 loc) · 9.06 KB
/
indexed_list.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// Indexed list of identifiers, for the main index and tags
// This kind of file is a sorted list of Media IDs
// Header:
// - List size (uint64) (Big endian) (8 bytes)
// List (always sorted):
// - Each item is an uint64 (Big endian) (8 bytes)
package main
import (
"encoding/binary"
"math/rand"
"os"
)
// Indexed list file
type IndexedListFile struct {
f *os.File // File descriptor
}
// Opens index file for writing
// file - Path to the file to open
func OpenIndexedListForWriting(file string) (*IndexedListFile, error) {
f, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE, FILE_PERMISSION)
if err != nil {
return nil, err
}
i := IndexedListFile{
f: f,
}
return &i, nil
}
// Opens index file for reading
// file - Path to the file to open
func OpenIndexedListForReading(file string) (*IndexedListFile, error) {
f, err := os.OpenFile(file, os.O_RDONLY, FILE_PERMISSION)
if err != nil {
return nil, err
}
i := IndexedListFile{
f: f,
}
return &i, nil
}
// Closes the file
func (file *IndexedListFile) Close() {
file.f.Close()
}
// Returns the number of items in the index
func (file *IndexedListFile) Count() (int64, error) {
// Rewind to the start of the file
_, err := file.f.Seek(0, 0)
if err != nil {
return 0, err
}
b := make([]byte, 8)
_, err = file.f.Read(b)
if err != nil {
return 0, err
}
return int64(binary.BigEndian.Uint64(b)), nil
}
// Initializes the index. Call once when the index file does not exists
func (file *IndexedListFile) Initialize() error {
// Set the size of the file
err := file.f.Truncate(8)
if err != nil {
return err
}
// Rewind to the start of the file
_, err = file.f.Seek(0, 0)
if err != nil {
return err
}
// Write size
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, 0)
_, err = file.f.Write(b)
if err != nil {
return err
}
return nil
}
// Reads a value given an index
// index - Index in the sorted list
func (file *IndexedListFile) ReadValue(index int64) (uint64, error) {
_, err := file.f.Seek(8+(index*8), 0)
if err != nil {
return 0, err
}
b := make([]byte, 8)
_, err = file.f.Read(b)
if err != nil {
return 0, err
}
return binary.BigEndian.Uint64(b), nil
}
// Writes a value
// index - Index in the sorted list
// value - Value to write
func (file *IndexedListFile) WriteValue(index int64, value uint64) error {
_, err := file.f.Seek(8+(index*8), 0)
if err != nil {
return err
}
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, value)
_, err = file.f.Write(b)
if err != nil {
return err
}
return nil
}
// Searches in the file for a value
// val - Value to search in the list
// Returns:
//
// 1 - A boolean value = true if the exact value was found
// 2 - The closest index to that value
func (file *IndexedListFile) BinarySearch(val uint64) (bool, int64, error) {
count, err := file.Count()
if err != nil {
return false, 0, err
}
if count == 0 {
return false, 0, nil
}
var low int64
var high int64
var mVal uint64
low = 0
high = int64(count - 1)
for low <= high {
m := (low + high) / 2
mVal, err = file.ReadValue(m)
if err != nil {
return false, 0, err
}
if mVal < val {
low = m + 1
} else if mVal > val {
high = m - 1
} else {
low = m
high = m - 1
}
}
return mVal == val, low, nil
}
// Searches in the file for a value
// This method requires you to provide the list length.
// A wrong value will result in undefined behavior
// val - Value to search in the list
// count - List size
// Returns:
//
// 1 - A boolean value = true if the exact value was found
// 2 - The closest index to that value
func (file *IndexedListFile) BinarySearchWithCountPreCalc(val uint64, count int64) (bool, int64, error) {
if count == 0 {
return false, 0, nil
}
var low int64
var high int64
var mVal uint64
var err error
low = 0
high = count - 1
for low <= high {
m := (low + high) / 2
mVal, err = file.ReadValue(m)
if err != nil {
return false, 0, err
}
if mVal < val {
low = m + 1
} else if mVal > val {
high = m - 1
} else {
low = m
high = m - 1
}
}
return mVal == val, low, nil
}
// Adds a value
// val - Value to add
// Returns
//
// 1 - true if it was added, false if it was already in the file
// 2 - the index where the value was added
func (file *IndexedListFile) AddValue(val uint64) (bool, int64, error) {
count, err := file.Count()
if err != nil {
return false, 0, err
}
found, index, err := file.BinarySearchWithCountPreCalc(val, count)
if err != nil {
return false, 0, err
}
if found {
return false, index, nil
}
// Resize file to make space for the new value
err = file.f.Truncate(8 + 8*(count+1))
if err != nil {
return false, 0, err
}
// Move down 1 all items below
tempVal := val
for i := index; i < count+1; i++ {
rVal, err := file.ReadValue(i)
if err != nil {
return false, 0, err
}
err = file.WriteValue(i, tempVal)
if err != nil {
return false, 0, err
}
tempVal = rVal
}
// Write the count value
// Rewind to the start of the file
_, err = file.f.Seek(0, 0)
if err != nil {
return false, 0, err
}
// Write size
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(count+1))
_, err = file.f.Write(b)
if err != nil {
return false, 0, err
}
return true, index, nil
}
// Removes a value
// val - Value to remove
// Returns
//
// 1 - true if it was removed, false if it was not present in the index
// 2 - The new count value
func (file *IndexedListFile) RemoveValue(val uint64) (bool, int64, error) {
count, err := file.Count()
if err != nil {
return false, 0, err
}
found, index, err := file.BinarySearchWithCountPreCalc(val, count)
if err != nil {
return false, 0, err
}
if !found {
return false, count, nil
}
new_count, err := file.RemoveIndex(index, count)
if err != nil {
return false, 0, err
}
return true, new_count, nil
}
// Removes a value given the index and the count
// index - Index to remove
// count - List size
// Returns the new count value
func (file *IndexedListFile) RemoveIndex(index int64, count int64) (int64, error) {
// Move instances 1 above
lastIndex := int64(count - 1)
for i := index; i < lastIndex; i++ {
val, err := file.ReadValue(i + 1)
if err != nil {
return 0, err
}
err = file.WriteValue(i, val)
if err != nil {
return 0, err
}
}
// Change file size
err := file.f.Truncate(int64(8 + 8*(count-1)))
if err != nil {
return 0, err
}
// Write the count value
// Rewind to the start of the file
_, err = file.f.Seek(0, 0)
if err != nil {
return 0, err
}
// Write size
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(count-1))
_, err = file.f.Write(b)
if err != nil {
return 0, err
}
return count - 1, err
}
// List values inside the index in order
// skip - Number of items to skip
// limit - Max number of items to return
func (file *IndexedListFile) ListValues(skip int64, limit int64) ([]uint64, error) {
count, err := file.Count()
if err != nil {
return nil, err
}
if count <= 0 || limit <= 0 || skip >= count {
return make([]uint64, 0), nil
}
resultSize := count - skip
if resultSize > limit {
resultSize = limit
}
result := make([]uint64, resultSize)
for i := int64(0); i < resultSize; i++ {
val, err := file.ReadValue(skip + i)
if err != nil {
return nil, err
}
result[i] = val
}
return result, nil
}
// List values inside the index in reverse order
// skip - Number of items to skip
// limit - Max number of items to return
func (file *IndexedListFile) ListValuesReverse(skip int64, limit int64) ([]uint64, error) {
count, err := file.Count()
if err != nil {
return nil, err
}
if count <= 0 || limit <= 0 || skip >= count {
return make([]uint64, 0), nil
}
resultSize := count - skip
if resultSize > limit {
resultSize = limit
}
result := make([]uint64, resultSize)
for i := int64(0); i < resultSize; i++ {
val, err := file.ReadValue((count - 1) - skip - i)
if err != nil {
return nil, err
}
result[i] = val
}
return result, nil
}
// Returns a random list of values from the list
// seed - Seed for the P-RNG
// limit - Max number of items to return
func (file *IndexedListFile) RandomValues(seed int64, limit int64) ([]uint64, error) {
count, err := file.Count()
if err != nil {
return nil, err
}
if count <= 0 || limit <= 0 {
return make([]uint64, 0), nil
}
prng := rand.New(rand.NewSource(seed))
result := make([]uint64, limit)
index_set := make(map[int64]struct{})
resultCount := int64(0)
for i := int64(0); i < limit; i++ {
if resultCount >= count {
break
}
index := prng.Int63()
if index < 0 {
index = 0
}
index = index % count
_, repeated := index_set[index]
if repeated {
repeatedIndex := index
index++
if index >= count {
index = 0
}
for index != repeatedIndex && repeated {
_, repeated = index_set[index]
if repeated {
index++
if index >= count {
index = 0
}
}
}
if repeated {
break
}
}
index_set[index] = struct{}{}
val, err := file.ReadValue(index)
if err != nil {
return nil, err
}
result[i] = val
resultCount++
}
return result[0:resultCount], nil
}