-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.go
421 lines (339 loc) · 10.5 KB
/
lib.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package pstore
import (
"os"
"path"
"reflect"
"strings"
"sync"
)
type any = interface{}
type memItemCount int
const MEM_ITEMS_UNLIMITED memItemCount = -1
const MEM_ITEMS_DEFAULT memItemCount = 100
func MemoryItemsCount(count int) memItemCount {
if count < 0 {
return MEM_ITEMS_UNLIMITED
}
return memItemCount(count)
}
type PersistentStorage struct {
// The maximum number of items to keep in memory.
MaxMemItems memItemCount
// If true, the cache will be thread-safe.
ThreadSafe bool
// If true, the cache will be saved to disk when a key is set. Default is true.
SaveToDiskOnSet bool
path string
name string
cache map[string]any
inMemory bool
mutex *sync.Mutex
}
// New creates a new PersistentStorage instance.
//
// Parameters:
// - path: The path to the directory where the cache files will be stored.
// - name: The name of the cache.
//
// Returns:
// - A new PersistentStorage instance.
func New(path, name string) *PersistentStorage {
return &PersistentStorage{
path: path,
name: name,
cache: make(map[string]any),
MaxMemItems: MEM_ITEMS_DEFAULT,
inMemory: false,
SaveToDiskOnSet: true,
ThreadSafe: false,
mutex: new(sync.Mutex),
}
}
// NewInMemory creates a new PersistentStorage instance that only stores data in memory.
//
// Important Notes:
// - The cache will not be saved to disk, thus not persistent.
// - The default maximum number of items in memory set to UNLIMITED. Setting it to a value will limit the end up erasing random items when the limit is reached.
// - SaveToDisk method is a no-op.
// - SaveToDiskOnSet is set to false by default as SaveToDisk is a no-op.
// - This constructor only exists for testing purposes and is basically just a glorified map.
//
// Parameters:
// - name: The name of the cache.
//
// Returns:
// - A new PersistentStorage instance.
func NewInMemory(name string) *PersistentStorage {
return &PersistentStorage{
name: name,
cache: make(map[string]any),
MaxMemItems: MEM_ITEMS_UNLIMITED,
inMemory: true,
SaveToDiskOnSet: false,
ThreadSafe: false,
mutex: new(sync.Mutex),
}
}
const error_read_files_failed = "failed to read files"
func IsReadFilesFailed(err error) bool {
return IsPStoreError(err) && strings.Contains(err.Error(), error_read_files_failed)
}
// Returns the number of items in the storage. All items are counted, including those on disk.
//
// Returns:
// - The number of items in the storage.
// - An error if files could not be read.
func (ps *PersistentStorage) Len() (int, error) {
if ps.ThreadSafe {
ps.mutex.Lock()
defer ps.mutex.Unlock()
}
files, err := os.ReadDir(ps.path)
if err != nil {
return -1, ps.errorf("%s: %v", error_read_files_failed, err)
}
count := 0
for _, file := range files {
if file.IsDir() {
continue
}
if strings.HasPrefix(file.Name(), ps.name+"_") && strings.HasSuffix(file.Name(), cache_ext) {
count++
}
}
return count, nil
}
const error_delete_failed = "failed to delete"
// IsDeleteFailed returns true if the error is a delete failure.
func IsDeleteFailed(err error) bool {
return IsPStoreError(err) && strings.Contains(err.Error(), error_delete_failed)
}
// Delete removes the key from the cache and deletes the file from disk.
//
// Parameters:
// - key: The key to delete.
//
// Returns:
// - An error if the key does not exist or if the file could not be deleted.
func (ps *PersistentStorage) Delete(key string) error {
delete(ps.cache, key)
if ps.inMemory {
return nil
}
if err := os.RemoveAll(ps.getCachePath(key)); err != nil {
return ps.errorf("%s %v: %v", error_delete_failed, key, err)
}
return nil
}
// Has returns true if the key exists in the cache.
//
// Parameters:
// - key: The key to check.
//
// Returns:
// - True if the key exists in the cache.
// - An error if the key could not be checked.
func (ps *PersistentStorage) Has(key string) (bool, error) {
if ps.ThreadSafe {
ps.mutex.Lock()
defer ps.mutex.Unlock()
}
_, ok := ps.cache[key]
if ok {
return true, nil
}
files, err := os.ReadDir(ps.path)
if err != nil {
return false, ps.errorf("%s: %v", error_read_files_failed, err)
}
for _, file := range files {
if file.IsDir() {
continue
}
if ps.getCacheFilename(key) == file.Name() {
return true, nil
}
}
return false, nil
}
// Keys returns a list of all keys in the cache. Only the in-memory cache is counted. The keys are not sorted and the order is not guaranteed.
//
// Returns:
// - A list of all keys in the cache.
// - An error if the keys could not be retrieved.
func (ps *PersistentStorage) Keys() ([]string, error) {
if ps.ThreadSafe {
ps.mutex.Lock()
defer ps.mutex.Unlock()
}
keys := []string{}
files, err := os.ReadDir(ps.path)
if err != nil {
return nil, ps.errorf("%s: %v", error_read_files_failed, err)
}
for _, file := range files {
if file.IsDir() {
continue
}
if strings.HasPrefix(file.Name(), ps.name+"_") && strings.HasSuffix(file.Name(), cache_ext) {
keys = append(keys, strings.TrimSuffix(strings.TrimPrefix(file.Name(), ps.name+"_"), cache_ext))
}
}
return keys, nil
}
// SaveToDisk saves the cache to disk. If SingleCacheFile is true, all keys are saved to a single file. Otherwise, each key is saved to a separate file. If SaveToDiskOnSet is true, the cache is saved to disk when a key is set and this method does nothing.
//
// Returns:
// - An error if the cache could not be saved to disk.
func (ps *PersistentStorage) SaveToDisk() error {
if ps.SaveToDiskOnSet {
return nil
}
if ps.ThreadSafe {
ps.mutex.Lock()
defer ps.mutex.Lock()
}
for k, v := range ps.cache {
if err := ps.saveToDisk(k, v); err != nil {
return err
}
}
return nil
}
func (ps *PersistentStorage) set(key string, value any) error {
ps.cache[key] = value
if ps.SaveToDiskOnSet {
if err := ps.saveToDisk(key, value); err != nil {
return err
}
}
if ps.MaxMemItems != MEM_ITEMS_UNLIMITED && len(ps.cache) > int(ps.MaxMemItems) {
for k := range ps.cache {
delete(ps.cache, k)
break
}
}
return nil
}
const cache_ext = ".pcache"
func (ps *PersistentStorage) getCacheFilename(key string) string {
return ps.name + "_" + key + cache_ext
}
func (ps *PersistentStorage) getCachePath(key string) string {
return path.Join(ps.path, ps.getCacheFilename(key))
}
const error_save_to_disk_failed = "failed to save"
// IsSaveToDiskFailed returns true if the error is a save failure.
func IsSaveToDiskFailed(err error) bool {
return IsPStoreError(err) && strings.Contains(err.Error(), error_save_to_disk_failed)
}
const error_serialize_failed = "failed to serialize"
// IsSerializeFailed returns true if the error is a serialization failure.
func IsSerializeFailed(err error) bool {
return IsPStoreError(err) && strings.Contains(err.Error(), error_serialize_failed)
}
const single_cache_filename = "single_full_cache"
func (ps *PersistentStorage) saveToDisk(key string, value any) error {
if ps.inMemory {
return nil
}
if err := os.MkdirAll(ps.path, 0755); err != nil {
return ps.errorf("%s %v: %v", error_save_to_disk_failed, key, err)
}
bytes, err := serialize(value)
if err != nil {
return ps.errorf("%s %v: %v", error_serialize_failed, key, err)
}
if err := os.WriteFile(ps.getCachePath(key), bytes, 0644); err != nil {
return ps.errorf("%s %v: %v", error_save_to_disk_failed, key, err)
}
return nil
}
const error_expected_pointer = "expected pointer"
// IsExpectedPointer returns true if the error is an expected pointer error.
func IsExpectedPointer(err error) bool {
return IsPStoreError(err) && strings.Contains(err.Error(), error_expected_pointer)
}
func (ps *PersistentStorage) get(out any, key string) error {
outReflect := reflect.ValueOf(out)
if outReflect.Kind() != reflect.Ptr {
return ps.errorf("%s but got type %v", error_expected_pointer, outReflect.Type())
}
it, ok := ps.cache[key]
// If the key is not in the cache, read it from disk
if !ok {
err := ps.readFromDisk(out, key)
// If the key is found on disk, cache it
if err != nil {
ps.cache[key] = outReflect.Elem().Interface()
}
return err
}
outReflect.Elem().Set(reflect.ValueOf(it))
return nil
}
const error_key_not_found = "key not found"
// IsKeyNotFound returns true if the error is a key not found error.
func IsKeyNotFound(err error) bool {
return IsPStoreError(err) && strings.Contains(err.Error(), error_key_not_found)
}
const error_read_from_disk_failed = "failed to read from disk"
// IsReadFromDiskFailed returns true if the error is a read from disk failure.
func IsReadFromDiskFailed(err error) bool {
return IsPStoreError(err) && strings.Contains(err.Error(), error_read_from_disk_failed)
}
const error_deserialize_failed = "failed to deserialize"
// IsDeserializeFailed returns true if the error is a deserialization failure.
func IsDeserializeFailed(err error) bool {
return IsPStoreError(err) && strings.Contains(err.Error(), error_deserialize_failed)
}
func (ps *PersistentStorage) readFromDisk(out interface{}, key string) error {
if ps.inMemory {
return ps.errorf("%s %v", error_key_not_found, key)
}
bytes, err := os.ReadFile(ps.getCachePath(key))
if err != nil {
if os.IsNotExist(err) {
return ps.errorf("%s %v", error_key_not_found, key)
}
return ps.errorf("%s %v: %v", error_read_from_disk_failed, key, err)
}
if err := deserialize(bytes, out); err != nil {
return ps.errorf("%s %v: %v", error_deserialize_failed, key, err)
}
return nil
}
// Set sets the value of the key in the cache. If the key already exists, it is overwritten. If the cache is thread-safe, the operation is atomic.
//
// Parameters:
// - key: The key to set.
// - value: The value to set. Should not be a pointer.
//
// Returns:
// - An error if the value could not be set.
func (ps *PersistentStorage) Set(key string, value any) error {
if ps.ThreadSafe {
ps.mutex.Lock()
defer ps.mutex.Lock()
}
var it any = value
for reflect.TypeOf(it).Kind() == reflect.Ptr {
it = reflect.ValueOf(it).Elem().Interface()
}
return ps.set(key, it)
}
// Get gets the value of the key from the cache. If the key does not exist in the cache, it is read from disk. If the key does not exist on disk, an error is returned. If the cache is thread-safe, the operation is atomic.
//
// Parameters:
// - key: The key to get.
// - out: The value to get. Must be a pointer.
//
// Returns:
// - An error if the value could not be retrieved.
func (ps *PersistentStorage) Get(key string, out any) error {
if ps.ThreadSafe {
ps.mutex.Lock()
defer ps.mutex.Lock()
}
return ps.get(out, key)
}