-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdelete.go
75 lines (68 loc) · 1.91 KB
/
delete.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
package hermes
// Delete is a method of the Cache struct that removes a key from the cache.
// If the full-text index is initialized, it is also removed from there.
// This method is thread-safe.
//
// Parameters:
// - key: A string representing the key to remove from the cache.
//
// Returns:
// - None
func (c *Cache) Delete(key string) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.delete(key)
}
// delete is a method of the Cache struct that removes a key from the cache.
// If the full-text index is initialized, it is also removed from there.
// This method is not thread-safe and should only be called from an exported function.
//
// Parameters:
// - key: A string representing the key to remove from the cache.
//
// Returns:
// - None
func (c *Cache) delete(key string) {
// Delete the key from the FT cache
if c.ft != nil {
c.ft.delete(key)
}
// Delete the key from the cache
delete(c.data, key)
}
// delete is a method of the FullText struct that removes a key from the full-text storage.
// This function is not thread-safe and should only be called from an exported function.
//
// Parameters:
// - key: A string representing the key to remove from the full-text storage.
//
// Returns:
// - None
func (ft *FullText) delete(key string) {
// Remove the key from the ft.storage
for word, data := range ft.storage {
// Check if the data is []int or int
if _, ok := data.(int); ok {
delete(ft.storage, word)
continue
}
// If the data is []int, loop through the slice
if keys, ok := data.([]int); !ok {
for i := 0; i < len(keys); i++ {
if key != ft.indices[keys[i]] {
continue
}
// Remove the key from the ft.storage slice
keys = append(keys[:i], keys[i+1:]...)
ft.storage[word] = keys
break
}
// If keys is empty, remove it from the storage
if len(keys) == 0 {
delete(ft.storage, word)
} else if len(keys) == 1 {
ft.storage[word] = keys[0]
}
}
}
}