-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinfo.go
101 lines (89 loc) · 3.01 KB
/
info.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
package hermes
import (
"errors"
utils "github.com/realTristan/hermes/utils"
)
// Info is a method of the Cache struct that returns a map with the cache and full-text info.
// This method is thread-safe.
// An error is returned if the full-text index is not initialized.
//
// Returns:
// - A map[string]any representing the cache and full-text info.
// - An error if the full-text index is not initialized.
func (c *Cache) Info() (map[string]any, error) {
c.mutex.RLock()
defer c.mutex.RUnlock()
return c.info()
}
// info is a method of the Cache struct that returns a map with the cache and full-text info.
// This method is not thread-safe, and should only be called from an exported function.
// An error is returned if the full-text index is not initialized.
//
// Returns:
// - A map[string]any representing the cache and full-text info.
// - An error if the full-text index is not initialized.
func (c *Cache) info() (map[string]any, error) {
var info map[string]any = map[string]any{
"keys": len(c.data),
}
// Check if the cache full-text has been initialized
if c.ft == nil {
return info, errors.New("full-text is not initialized")
}
// Add the full-text info to the map
if size, err := utils.Size(c.ft.storage); err != nil {
return info, err
} else {
// Add the full-text info to the map
info["full-text"] = map[string]any{
"keys": len(c.ft.storage),
"index": c.ft.index,
"size": size,
}
}
// Return the info map
return info, nil
}
// InfoForTesting is a method of the Cache struct that returns a map with the cache and full-text info for testing purposes.
// This method is thread-safe.
// An error is returned if the full-text index is not initialized.
//
// Returns:
// - A map[string]any representing the cache and full-text info for testing purposes.
// - An error if the full-text index is not initialized.
func (c *Cache) InfoForTesting() (map[string]any, error) {
c.mutex.RLock()
defer c.mutex.RUnlock()
return c.infoForTesting()
}
// infoForTesting is a method of the Cache struct that returns a map with the cache and full-text info for testing purposes.
// This method is not thread-safe, and should only be called from an exported function.
// An error is returned if the full-text index is not initialized.
//
// Returns:
// - A map[string]any representing the cache and full-text info for testing purposes.
// - An error if the full-text index is not initialized.
func (c *Cache) infoForTesting() (map[string]any, error) {
var info map[string]any = map[string]any{
"keys": len(c.data),
"data": c.data,
}
// Check if the cache full-text has been initialized
if c.ft == nil {
return info, errors.New("full-text is not initialized")
}
// Add the full-text info to the map
if size, err := utils.Size(c.ft.storage); err != nil {
return info, err
} else {
info["full-text"] = map[string]any{
"keys": len(c.ft.storage),
"index": c.ft.index,
"size": size,
"storage": c.ft.storage,
"indices": c.ft.indices,
}
}
// Return the info map
return info, nil
}