-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindices.go
65 lines (56 loc) · 1.87 KB
/
indices.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
package hermes
// When you delete a number of keys from the cache, the index remains
// the same. Over time, this number will grow to be very large, and will
// cause the cache to use a lot of memory. This function resets the indices
// to be sequential, starting from 0.
// This function is thread-safe.
func (c *Cache) FTSequenceIndices() {
c.mutex.Lock()
defer c.mutex.Unlock()
c.ft.sequenceIndices()
}
// When you delete a number of keys from the cache, the index remains
// the same. Over time, this number will grow to be very large, and will
// cause the cache to use a lot of memory. This function resets the indices
// to be sequential, starting from 0.
// This function is not thread-safe, and should only be called from
// an exported function.
func (ft *FullText) sequenceIndices() {
// Store the temp variables
var (
tempIndices map[int]string = make(map[int]string)
tempindex int = 0
tempKeys map[string]int = make(map[string]int)
)
// Fill the temp indices by iterating over the current
// indices and adding them to the tempIndices map
for _, value := range ft.indices {
tempIndices[tempindex] = value
tempindex++
}
// Fill the temp keys with the opposites of ft.indices
for key, value := range tempIndices {
tempKeys[value] = key
}
// Iterate over the ft storage
for word, data := range ft.storage {
// Check if the data is []int or int
if v, ok := data.(int); ok {
ft.storage[word] = tempKeys[ft.indices[v]]
continue
}
// If the data is []int, loop through the slice
if keys, ok := data.([]int); !ok {
for i := 0; i < len(keys); i++ {
var index int = keys[i]
// Get the key from the old indices
var key string = ft.indices[index]
// Set the new index
ft.storage[word].([]int)[i] = tempKeys[key]
}
}
}
// Set the old variables to the new variables
ft.indices = tempIndices
ft.index = tempindex
}