-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinit.go
229 lines (199 loc) · 6.53 KB
/
init.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package hermes
import (
"errors"
"fmt"
"sync"
utils "github.com/realTristan/hermes/utils"
)
// InitCache is a function that initializes a new Cache struct and returns a pointer to it.
//
// Returns:
// - A pointer to a new Cache struct.
func InitCache() *Cache {
return &Cache{
data: make(map[string]map[string]any),
mutex: &sync.RWMutex{},
ft: nil,
}
}
// Initialize the full-text for the cache
// This method is thread-safe.
// If the full-text index is already initialized, an error is returned.
//
// Parameters:
// - maxSize: the maximum number of words to store in the full-text index.
// - maxBytes: the maximum size, in bytes, of the full-text index.
//
// Returns:
// - error: If the full-text is already initialized.
func (c *Cache) FTInit(maxSize int, maxBytes int, minWordLength int) error {
c.mutex.Lock()
defer c.mutex.Unlock()
// Verify that the ft has already been initialized
if c.ft != nil {
return errors.New("full-text already initialized")
}
// Initialize the FT
return c.ftInit(maxSize, maxBytes, minWordLength)
}
// Initialize the full-text for the cache.
// This method is not thread-safe, and should only be called from an exported function.
//
// Parameters:
// - maxSize: the maximum number of words to store in the full-text index.
// - maxBytes: the maximum size, in bytes, of the full-text index.
//
// Returns:
// - error: From full-text cache insertion.
func (c *Cache) ftInit(maxSize int, maxBytes int, minWordLength int) error {
// Initialize the FT struct
var ft *FullText = &FullText{
storage: make(map[string]any),
indices: make(map[int]string),
index: 0,
maxSize: maxSize,
maxBytes: maxBytes,
minWordLength: minWordLength,
}
// Load the cache data
if err := ft.insert(&c.data); err != nil {
return err
}
// Update the cache full-text
c.ft = ft
// Return no error
return nil
}
// Initialize the full-text index for the cache with a map.
// This method is thread-safe.
// If the full-text index is already initialized, an error is returned.
//
// Parameters:
// - data: the data to initialize the full-text index with.
// - maxSize: the maximum number of words to store in the full-text index.
// - maxBytes: the maximum size, in bytes, of the full-text index.
//
// Returns:
// - error: If the full-text is already initialized.
func (c *Cache) FTInitWithMap(data map[string]map[string]any, maxSize int, maxBytes int, minWordLength int) error {
c.mutex.Lock()
defer c.mutex.Unlock()
// Verify that the cache is already initialized
if c.ft != nil {
return errors.New("full-text cache already initialized")
}
// Initialize the FT cache
return c.ftInitWithMap(data, maxSize, maxBytes, minWordLength)
}
// Initialize the full-text for the cache with a map.
// This method is not thread-safe, and should only be called from an exported function.
//
// Parameters:
// - data: the data to initialize the full-text index with.
// - maxSize: the maximum number of words to store in the full-text index.
// - maxBytes: the maximum size, in bytes, of the full-text index.
//
// Returns:
// - error
func (c *Cache) ftInitWithMap(data map[string]map[string]any, maxSize int, maxBytes int, minWordLength int) error {
// Initialize the FT struct
var ft *FullText = &FullText{
storage: make(map[string]any),
indices: make(map[int]string),
index: 0,
maxSize: maxSize,
maxBytes: maxBytes,
minWordLength: minWordLength,
}
// Iterate over the cache keys and add them to the data
for k := range c.data {
if _, ok := data[k]; ok {
return fmt.Errorf("key %s already exists in cache", k)
}
data[k] = c.data[k]
}
// Insert the data into the ft storage
if err := ft.insert(&data); err != nil {
return err
}
// Update the cache varoables
c.data = data
c.ft = ft
// Return no error
return nil
}
// Initialize the full-text for the cache with a JSON file.
// This method is thread-safe.
// If the full-text index is already initialized, an error is returned.
//
// Parameters:
// - file: the path to the JSON file to initialize the full-text index with.
// - maxSize: the maximum number of words to store in the full-text index.
// - maxBytes: the maximum size, in bytes, of the full-text index.
//
// Returns:
// - error: If the full-text is already initialized.
func (c *Cache) FTInitWithJson(file string, maxSize int, maxBytes int, minWordLength int) error {
c.mutex.Lock()
defer c.mutex.Unlock()
// Verify that the ft cache is initialized
if c.ft != nil {
return errors.New("full-text cache already initialized")
}
// Initialize the FT
return c.ftInitWithJson(file, maxSize, maxBytes, minWordLength)
}
// Initialize the full-text for the cache with a JSON file.
// This method is not thread-safe, and should only be called from an exported function.
//
// Parameters:
// - file: the path to the JSON file to initialize the full-text index with.
// - maxSize: the maximum number of words to store in the full-text index.
// - maxBytes: the maximum size, in bytes, of the full-text index.
//
// Returns:
// - error: Json file read error, or init with map error.
func (c *Cache) ftInitWithJson(file string, maxSize int, maxBytes int, minWordLength int) error {
if data, err := utils.ReadJson[map[string]map[string]any](file); err != nil {
return err
} else {
return c.ftInitWithMap(data, maxSize, maxBytes, minWordLength)
}
}
// insert is a method of the FullText struct that inserts 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:
// - data: A map of maps containing the data to be inserted.
//
// Returns:
// - An error if the full-text storage limit or byte-size limit is reached.
func (ft *FullText) insert(data *map[string]map[string]any) error {
// Create a new temp storage
var ts *TempStorage = NewTempStorage(ft)
// Loop through the json data
for cacheKey, cacheValue := range *data {
for k, v := range cacheValue {
if ftv := WFTGetValue(v); len(ftv) == 0 {
continue
} else {
// Set the key in the provided value to the fulltext value
(*data)[cacheKey][k] = ftv
// Insert the value in the temp storage
if err := ts.insert(ft, cacheKey, ftv); err != nil {
return err
}
}
}
}
// Merge the keys
// ts.mergeKeys()
// 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(ft)
// Print the size of the full-text cache storage
fmt.Println(utils.Size(ft.storage))
// Return nil for no errors
return nil
}