-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtempstorage.go
209 lines (191 loc) · 5.69 KB
/
tempstorage.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
package hermes
import (
"fmt"
"strings"
utils "github.com/realTristan/hermes/utils"
)
// NewTempStorage is a function that creates a new TempStorage object for a given FullText object.
// Parameters:
// - ft (*FullText): A pointer to the FullText object to create the TempStorage object for.
//
// Returns:
// - (*TempStorage): A pointer to the newly created TempStorage object.
type TempStorage struct {
data map[string]any
indices map[int]string
index int
keys map[string]int
}
// NewTempStorage is a function that creates a new TempStorage object for a given FullText object.
// Parameters:
// - ft (*FullText): A pointer to the FullText object to create the TempStorage object for.
//
// Returns:
// - (*TempStorage): A pointer to the newly created TempStorage object.
func NewTempStorage(ft *FullText) *TempStorage {
var ts = &TempStorage{
data: ft.storage,
indices: ft.indices,
index: ft.index,
keys: make(map[string]int),
}
// Loop through the data
for k, v := range ts.indices {
ts.keys[v] = k
}
return ts
}
// updateFullText is a method of the TempStorage struct that updates the FullText object with the data in the TempStorage object.
// Parameters:
// - ft (*FullText): A pointer to the FullText object to update.
//
// Returns:
// - None.
func (ts *TempStorage) updateFullText(ft *FullText) {
ft.storage = ts.data
ft.indices = ts.indices
ft.index = ts.index
}
// cleanSingleArrays is a method of the TempStorage struct that replaces single-element integer arrays with their single integer value.
// Parameters:
// - None.
//
// Returns:
// - None.
func (ts *TempStorage) cleanSingleArrays() {
for k, v := range ts.data {
if v, ok := v.([]int); ok && len(v) == 1 {
ts.data[k] = v[0]
}
}
}
// error is a method of the TempStorage struct that checks if the storage limit has been reached and returns an error if it has.
// Parameters:
// - ft (*FullText): A pointer to the FullText object to check the storage limit against.
//
// Returns:
// - (error): An error if the storage limit has been reached, nil otherwise.
func (ts *TempStorage) error(ft *FullText) error {
// Check if the storage limit has been reached
if ft.maxSize > 0 {
if len(ts.data) > ft.maxSize {
return fmt.Errorf("full-text storage limit reached (%d/%d keys). load cancelled", len(ts.data), ft.maxSize)
}
}
if ft.maxBytes > 0 {
if cacheSize, err := utils.Size(ts.data); err != nil {
return err
} else if cacheSize > ft.maxBytes {
return fmt.Errorf("full-text byte-size limit reached (%d/%d bytes). load cancelled", cacheSize, ft.maxBytes)
}
}
return nil
}
// update is a method of the TempStorage struct that updates the TempStorage object with the given words and cache key.
// Parameters:
// - ft (*FullText): A pointer to the FullText object to update.
// - words ([]string): A slice of strings representing the words to update.
// - cacheKey (string): A string representing the cache key to update.
//
// Returns:
// - None.
func (ts *TempStorage) update(ft *FullText, words []string, cacheKey string) {
// Loop through the words
for i := 0; i < len(words); i++ {
var word string = words[i]
// Check if the word is valid
if len(word) < ft.minWordLength {
continue
}
if temp, ok := ts.data[word]; !ok {
ts.data[word] = []int{ts.index}
} else if v, ok := temp.([]int); !ok {
ts.data[word] = []int{temp.(int), ts.keys[cacheKey]}
} else {
if utils.SliceContains(v, ts.keys[cacheKey]) {
continue
}
ts.data[word] = append(v, ts.keys[cacheKey])
}
}
}
// updateKeys is a method of the TempStorage struct that sets the given cache key in the temp storage keys
// Parameters:
// - cacheKey (string): A string representing the cache key to set.
//
// Returns:
// - None.
func (ts *TempStorage) updateKeys(cacheKey string) {
if _, ok := ts.keys[cacheKey]; !ok {
ts.index++
ts.indices[ts.index] = cacheKey
ts.keys[cacheKey] = ts.index
}
}
// mergeKeys is a method of the TempStorage struct that merges all keys that contain subkeys into a single key.
// Parameters:
// - None.
//
// Returns:
// - None.
func (ts *TempStorage) mergeKeys() {
// Loop through the keys
for k1, v1 := range ts.data {
for k2, v2 := range ts.data {
if k1 == k2 {
continue
}
if strings.Contains(k1, k2) {
if _, ok := v1.(string); ok {
continue
}
if _, ok := v2.(string); ok {
continue
}
var v1, v2 = v1.([]int), v2.([]int)
for v := range v2 {
if !utils.SliceContains(v1, v) {
v1 = append(v1, v)
}
}
ts.data[k1] = v1
ts.data[k2] = k1
// delete(ts.data, k2)
break
}
}
}
}
// insertWords is a method of the TempStorage struct that inserts data into the temp storage.
// Parameters:
// - ft (*FullText): A pointer to the FullText object to check the storage limit against.
// - cacheKey (string): A string representing the cache key to insert.
// - ftv (string): A string representing the value to insert.
//
// Returns:
// - (error): An error if the storage limit has been reached, nil otherwise.
func (ts *TempStorage) insert(ft *FullText, cacheKey string, ftv string) error {
// Set the cache key in the temp storage keys
ts.updateKeys(cacheKey)
// Clean the string value
ftv = strings.TrimSpace(ftv)
ftv = utils.RemoveDoubleSpaces(ftv)
ftv = strings.ToLower(ftv)
// Loop through the words
for _, word := range strings.Split(ftv, " ") {
if len(word) == 0 {
continue
} else if len(word) < ft.minWordLength {
continue
} else if err := ts.error(ft); err != nil {
return err
}
// Trim the word
word = utils.TrimNonAlphaNum(word)
var words []string = utils.SplitByAlphaNum(word)
// Update the temp storage
ts.update(ft, words, cacheKey)
}
// Return no error
return nil
}