-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathset.go
82 lines (71 loc) · 2.34 KB
/
set.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
package hermes
import "fmt"
// Set is a method of the Cache struct that sets a value in the cache for the specified key.
// This function is thread-safe.
//
// Parameters:
// - key: A string representing the key to set the value for.
// - value: A map[string]any representing the value to set.
//
// Returns:
// - Error
func (c *Cache) Set(key string, value map[string]any) error {
c.mutex.Lock()
defer c.mutex.Unlock()
return c.set(key, value)
}
// set is a method of the Cache struct that sets a value in the cache for the specified key.
// This function is not thread-safe, and should only be called from an exported function.
// If fullText is true, set the value in the full-text cache as well.
//
// Parameters:
// - key: A string representing the key to set the value for.
// - value: A map[string]any representing the value to set.
//
// Returns:
// - An error if the full-text cache key already exists. Otherwise, nil.
func (c *Cache) set(key string, value map[string]any) error {
if _, ok := c.data[key]; ok {
return fmt.Errorf("full-text cache key already exists (%s). delete it before setting it another value", key)
}
// Update the value in the FT cache
if c.ft != nil {
if err := c.ftSet(key, value); err != nil {
return err
}
}
// Update the value in the cache
c.data[key] = value
// Return nil for no error
return nil
}
// ftSet is a method of the Cache struct that sets a value in the full-text cache for the specified key.
// This function is not thread-safe, and should only be called from an exported function.
//
// Parameters:
// - key: A string representing the key to set the value for.
// - value: A map[string]any representing the value to set.
//
// Returns:
// - An error if the full-text storage limit or byte-size limit is reached. Otherwise, nil.
func (c *Cache) ftSet(key string, value map[string]any) error {
var ts *TempStorage = NewTempStorage(c.ft)
for k, v := range value {
if ftv := WFTGetValue(v); len(ftv) == 0 {
continue
} else {
// Update the value
value[k] = ftv
// Insert the value in the temp storage
if err := ts.insert(c.ft, key, ftv); err != nil {
return err
}
}
}
// Iterate over the temp storage and set the values with len 1 to int
ts.cleanSingleArrays()
// Set the full-text cache to the temp map
ts.updateFullText(c.ft)
// Return nil for no errors
return nil
}