-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompression.go
More file actions
503 lines (440 loc) · 12.9 KB
/
compression.go
File metadata and controls
503 lines (440 loc) · 12.9 KB
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package gencache
import (
"bytes"
"compress/gzip"
"compress/lzw"
"compress/zlib"
"context"
"encoding/gob"
"io"
"sync"
"sync/atomic"
"time"
"github.com/gozephyr/gencache/errors"
)
// CompressionAlgorithm represents the compression algorithm to use
type CompressionAlgorithm int
const (
// GzipCompression uses gzip compression
GzipCompression CompressionAlgorithm = iota
// ZlibCompression uses zlib compression
ZlibCompression
// LZWCompression uses LZW compression
LZWCompression
)
// CompressionConfig represents configuration for compression
type CompressionConfig struct {
Algorithm CompressionAlgorithm
Level int
MinSize int
MaxSize int
StatsEnabled bool
StatsInterval time.Duration
}
// DefaultCompressionConfig returns the default compression configuration
func DefaultCompressionConfig() CompressionConfig {
return CompressionConfig{
Algorithm: GzipCompression,
Level: gzip.DefaultCompression,
MinSize: 1024, // 1KB
MaxSize: 10 * 1024 * 1024, // 10MB
StatsEnabled: true,
StatsInterval: 5 * time.Minute,
}
}
// CompressionStats represents statistics for compression
type CompressionStats struct {
TotalCompressed atomic.Int64
TotalDecompressed atomic.Int64
TotalBytesIn atomic.Int64
TotalBytesOut atomic.Int64
CompressionRatio atomic.Value // float64
LastCompressionTime atomic.Value // time.Time
LastStatsReset atomic.Value // time.Time
}
// CompressedCache extends Cache with compression
type CompressedCache[K comparable, V any] interface {
Cache[K, V]
// GetCompressionStats returns the current compression statistics
GetCompressionStats() *CompressionStats
// ResetCompressionStats resets the compression statistics
ResetCompressionStats()
// SetCompressionConfig updates the compression configuration
SetCompressionConfig(config CompressionConfig)
// GetCompressionConfig returns the current compression configuration
GetCompressionConfig() CompressionConfig
// OnEvent registers a callback for cache events
OnEvent(cb CacheCallback[K, V])
}
// CompressedValue is a wrapper for compressed data
// Used internally to mark values as compressed
// and to store the compressed bytes and algorithm
// so that Get can decompress them.
type CompressedValue struct {
Data []byte
Algorithm CompressionAlgorithm
}
// compressedCache implements CompressedCache
type compressedCache[K comparable, V any] struct {
Cache[K, any]
config CompressionConfig
stats CompressionStats
mu sync.RWMutex
stop chan struct{}
}
// NewCompressedCache creates a new cache with compression
func NewCompressedCache[K comparable, V any](c Cache[K, any], config CompressionConfig) CompressedCache[K, V] {
cc := &compressedCache[K, V]{
Cache: c,
config: config,
stop: make(chan struct{}),
}
if config.StatsEnabled {
go cc.statsLoop()
}
return cc
}
// extractCompressedValue tries to extract a CompressedValue from an interface{}, handling interface wrapping
func extractCompressedValue(val any) (CompressedValue, bool) {
const maxDepth = 10
depth := 0
for depth < maxDepth {
depth++
switch v := val.(type) {
case CompressedValue:
return v, true
case *CompressedValue:
if v != nil {
return *v, true
}
return CompressedValue{}, false
case interface{ Unwrap() any }:
val = v.Unwrap()
continue
case any:
// Unwrap one layer of interface{}
val = v
continue
default:
return CompressedValue{}, false
}
}
return CompressedValue{}, false
}
// Get retrieves a value from the cache
func (cc *compressedCache[K, V]) Get(key K) (V, error) {
raw, err := cc.Cache.Get(key)
var zero V
if err != nil {
return zero, err
}
// If value is CompressedValue, decompress and decode
if cv, isCompressed := extractCompressedValue(raw); isCompressed {
cc.mu.Lock()
cc.stats.TotalDecompressed.Add(1)
cc.stats.TotalBytesIn.Add(int64(len(cv.Data)))
cc.mu.Unlock()
decompressed, err := cc.decompressWithAlgorithm(cv.Data, cv.Algorithm)
if err != nil {
return zero, err
}
cc.mu.Lock()
cc.stats.TotalBytesOut.Add(int64(len(decompressed)))
cc.mu.Unlock()
// If V is interface{} or []byte, return decompressed as []byte
if _, ok := any(*new(V)).([]byte); ok || isInterfaceType[V]() {
if decompressed == nil {
return zero, errors.ErrInvalidValue
}
return any(decompressed).(V), nil
}
// Otherwise, gob decode
var result V
if err := gob.NewDecoder(bytes.NewReader(decompressed)).Decode(&result); err != nil {
return zero, err
}
return result, nil
}
// If value is []byte, try to decompress as fallback (for corrupted data test)
if b, ok := raw.([]byte); ok {
for _, algo := range []CompressionAlgorithm{GzipCompression, ZlibCompression, LZWCompression} {
decompressed, err := cc.decompressWithAlgorithm(b, algo)
if err != nil {
continue
}
cc.mu.Lock()
cc.stats.TotalDecompressed.Add(1)
cc.stats.TotalBytesIn.Add(int64(len(b)))
cc.stats.TotalBytesOut.Add(int64(len(decompressed)))
cc.mu.Unlock()
if _, ok := any(*new(V)).([]byte); ok || isInterfaceType[V]() {
return any(decompressed).(V), nil
}
var result V
if err := gob.NewDecoder(bytes.NewReader(decompressed)).Decode(&result); err == nil {
return result, nil
}
}
// Fallback: if V is []byte, return as is
if _, ok := any(*new(V)).([]byte); ok {
return any(b).(V), nil
}
return zero, errors.ErrInvalidValue
}
// Not compressed, try to cast to V
if v, ok := raw.(V); ok {
return v, nil
}
// Fallback: if V is []byte and raw is []byte
if b, ok := raw.([]byte); ok {
if _, ok2 := any(*new(V)).([]byte); ok2 {
return any(b).(V), nil
}
}
return zero, errors.ErrInvalidValue
}
// Set stores a value in the cache
func (cc *compressedCache[K, V]) Set(key K, value V, ttl time.Duration) error {
cc.mu.Lock()
config := cc.config
cc.mu.Unlock()
// Convert value to []byte
var data []byte
switch v := any(value).(type) {
case []byte:
data = v
default:
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(value); err != nil {
return cc.Cache.Set(key, value, ttl)
}
data = buf.Bytes()
}
// Check if data should be compressed
if len(data) >= config.MinSize && len(data) <= config.MaxSize {
compressed, err := cc.compress(data)
if err == nil {
cc.mu.Lock()
cc.stats.TotalCompressed.Add(1)
cc.stats.TotalBytesIn.Add(int64(len(data)))
cc.stats.TotalBytesOut.Add(int64(len(compressed)))
cc.stats.LastCompressionTime.Store(time.Now())
cc.mu.Unlock()
return cc.Cache.Set(key, any(CompressedValue{
Data: compressed,
Algorithm: config.Algorithm,
}), ttl)
}
}
// If compression fails or data is outside size limits, store uncompressed
return cc.Cache.Set(key, value, ttl)
}
// Delete removes a value from the cache
func (cc *compressedCache[K, V]) Delete(key K) error {
return cc.Cache.Delete(key)
}
// GetCompressionStats returns the current compression statistics
func (cc *compressedCache[K, V]) GetCompressionStats() *CompressionStats {
cc.mu.RLock()
defer cc.mu.RUnlock()
// Create a new stats struct to avoid copying locks
stats := &CompressionStats{
TotalCompressed: atomic.Int64{},
TotalDecompressed: atomic.Int64{},
TotalBytesIn: atomic.Int64{},
TotalBytesOut: atomic.Int64{},
CompressionRatio: atomic.Value{},
LastCompressionTime: atomic.Value{},
LastStatsReset: atomic.Value{},
}
// Copy values atomically
stats.TotalCompressed.Store(cc.stats.TotalCompressed.Load())
stats.TotalDecompressed.Store(cc.stats.TotalDecompressed.Load())
stats.TotalBytesIn.Store(cc.stats.TotalBytesIn.Load())
stats.TotalBytesOut.Store(cc.stats.TotalBytesOut.Load())
if ratio := cc.stats.CompressionRatio.Load(); ratio != nil {
stats.CompressionRatio.Store(ratio)
} else {
stats.CompressionRatio.Store(float64(0))
}
if lastComp := cc.stats.LastCompressionTime.Load(); lastComp != nil {
stats.LastCompressionTime.Store(lastComp)
}
if lastReset := cc.stats.LastStatsReset.Load(); lastReset != nil {
stats.LastStatsReset.Store(lastReset)
}
return stats
}
// ResetCompressionStats resets the compression statistics
func (cc *compressedCache[K, V]) ResetCompressionStats() {
cc.mu.Lock()
defer cc.mu.Unlock()
cc.stats = CompressionStats{
LastStatsReset: atomic.Value{},
}
cc.stats.LastStatsReset.Store(time.Now())
}
// SetCompressionConfig updates the compression configuration
func (cc *compressedCache[K, V]) SetCompressionConfig(config CompressionConfig) {
cc.mu.Lock()
defer cc.mu.Unlock()
cc.config = config
}
// compress compresses data using the configured algorithm
func (cc *compressedCache[K, V]) compress(data []byte) ([]byte, error) {
cc.mu.RLock()
config := cc.config
cc.mu.RUnlock()
var buf bytes.Buffer
var writer io.WriteCloser
var err error
switch config.Algorithm {
case GzipCompression:
writer, err = gzip.NewWriterLevel(&buf, config.Level)
case ZlibCompression:
writer, err = zlib.NewWriterLevel(&buf, config.Level)
case LZWCompression:
writer = lzw.NewWriter(&buf, lzw.LSB, 8)
default:
return nil, errors.WrapError("compress", nil, errors.ErrInvalidOperation)
}
if err != nil {
return nil, errors.WrapError("compress", nil, errors.ErrCompression)
}
if _, err := writer.Write(data); err != nil {
writer.Close()
return nil, errors.WrapError("compress", nil, errors.ErrCompression)
}
if err := writer.Close(); err != nil {
return nil, errors.WrapError("compress", nil, errors.ErrCompression)
}
return buf.Bytes(), nil
}
// decompressWithAlgorithm decompresses data using the specified algorithm
func (cc *compressedCache[K, V]) decompressWithAlgorithm(data []byte, algo CompressionAlgorithm) ([]byte, error) {
var reader io.ReadCloser
var err error
switch algo {
case GzipCompression:
reader, err = gzip.NewReader(bytes.NewReader(data))
case ZlibCompression:
reader, err = zlib.NewReader(bytes.NewReader(data))
case LZWCompression:
reader = lzw.NewReader(bytes.NewReader(data), lzw.LSB, 8)
default:
return nil, errors.WrapError("decompressWithAlgorithm", nil, errors.ErrInvalidOperation)
}
if err != nil {
return nil, errors.WrapError("decompressWithAlgorithm", nil, errors.ErrDecompression)
}
defer reader.Close()
decompressed, err := io.ReadAll(reader)
if err != nil {
return nil, errors.WrapError("decompressWithAlgorithm", nil, errors.ErrDecompression)
}
return decompressed, nil
}
// statsLoop periodically updates compression statistics
func (cc *compressedCache[K, V]) statsLoop() {
cc.mu.RLock()
interval := cc.config.StatsInterval
cc.mu.RUnlock()
// If interval is not positive, use a default of 5 minutes
if interval <= 0 {
interval = 5 * time.Minute
}
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
cc.mu.Lock()
if cc.stats.TotalBytesIn.Load() > 0 {
cc.stats.CompressionRatio.Store(float64(cc.stats.TotalBytesOut.Load()) / float64(cc.stats.TotalBytesIn.Load()))
}
cc.mu.Unlock()
case <-cc.stop:
return
}
}
}
// GetMany retrieves multiple values from the cache
func (cc *compressedCache[K, V]) GetMany(ctx context.Context, keys []K) map[K]V {
result := make(map[K]V, len(keys))
for _, key := range keys {
select {
case <-ctx.Done():
return result
default:
value, err := cc.Get(key)
if err == nil {
result[key] = value
}
}
}
return result
}
// GetWithContext retrieves a value from the cache with context support
func (cc *compressedCache[K, V]) GetWithContext(ctx context.Context, key K) (V, error) {
select {
case <-ctx.Done():
var zero V
return zero, errors.ErrContextCanceled
default:
value, err := cc.Get(key)
if err != nil {
var zero V
return zero, err
}
return value, nil
}
}
// SetMany sets multiple values in the cache with context support
func (cc *compressedCache[K, V]) SetMany(ctx context.Context, values map[K]V, ttl time.Duration) error {
for k, v := range values {
select {
case <-ctx.Done():
return errors.ErrContextCanceled
default:
if err := cc.Set(k, v, ttl); err != nil {
return err
}
}
}
return nil
}
// SetWithContext sets a value in the cache with context support
func (cc *compressedCache[K, V]) SetWithContext(ctx context.Context, key K, value V, ttl time.Duration) error {
select {
case <-ctx.Done():
return errors.ErrContextCanceled
default:
return cc.Set(key, value, ttl)
}
}
// isInterfaceType returns true if V is interface{}
func isInterfaceType[V any]() bool {
var v V
return any(v) == nil
}
// GetCompressionConfig returns the current compression configuration
func (cc *compressedCache[K, V]) GetCompressionConfig() CompressionConfig {
cc.mu.RLock()
defer cc.mu.RUnlock()
return cc.config
}
// OnEvent registers a callback for cache events
func (cc *compressedCache[K, V]) OnEvent(cb CacheCallback[K, V]) {
cc.Cache.OnEvent(func(evt CacheEvent[K, any]) {
var v V
if val, ok := evt.Value.(V); ok {
v = val
}
cb(CacheEvent[K, V]{
Type: evt.Type,
Key: evt.Key,
Value: v,
Timestamp: evt.Timestamp,
})
})
}