-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbloom.go
More file actions
1096 lines (912 loc) · 31.8 KB
/
bloom.go
File metadata and controls
1096 lines (912 loc) · 31.8 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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package patternx
import (
"context"
"crypto/md5"
"crypto/sha256"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"hash/fnv"
"math"
"sync"
"sync/atomic"
"time"
"github.com/seasbee/go-logx"
)
// Bloom Filter Types
type BloomStore interface {
Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
Get(ctx context.Context, key string) ([]byte, error)
Del(ctx context.Context, key string) error
Exists(ctx context.Context, key string) (bool, error)
}
type BloomConfig struct {
ExpectedItems uint64 // Expected number of items
FalsePositiveRate float64 // Desired false positive rate (0.01 = 1%)
Store BloomStore // Optional store for persistence
KeyPrefix string // Key prefix for store operations
TTL time.Duration // Time-to-live for stored data
EnableMetrics bool // Enable performance metrics
}
type BloomMetrics struct {
AddOperations atomic.Uint64
ContainsOperations atomic.Uint64
AddBatchOperations atomic.Uint64
ContainsBatchOperations atomic.Uint64
FalsePositives atomic.Uint64
TrueNegatives atomic.Uint64
StoreOperations atomic.Uint64
StoreErrors atomic.Uint64
mu sync.RWMutex // For complex metrics that can't be atomic
LastAddTime time.Time
LastContainsTime time.Time
AverageAddLatency time.Duration
AverageContainsLatency time.Duration
}
type BloomFilter struct {
mu sync.RWMutex
storeMu sync.Mutex // Dedicated mutex for store operations
bitset []bool
size uint64
hashCount int
itemCount uint64
maxItems uint64
falsePositiveRate float64
store BloomStore
keyPrefix string
ttl time.Duration
closed int32 // Atomic flag for closed state
metrics *BloomMetrics
}
// Constants for production constraints
const (
MaxItemLengthBloom = 1024 * 1024 // 1MB max item length
MaxExpectedItemsBloom = 1_000_000_000 // 1 billion max expected items
MinExpectedItemsBloom = 1 // Minimum expected items
MaxFalsePositiveBloom = 0.5 // 50% max false positive rate
MinFalsePositiveBloom = 1e-6 // 0.0001% min false positive rate
MaxHashCountBloom = 50 // Maximum number of hash functions
MinHashCountBloom = 1 // Minimum number of hash functions
MaxBitsetSizeBloom = 1e10 // 10 billion max bitset size
DefaultTTLBloom = 24 * time.Hour
DefaultKeyPrefixBloom = "bloom"
MaxRetryAttemptsBloom = 3
RetryDelayBloom = 100 * time.Millisecond
)
// BloomFilterState represents the serializable state of a Bloom filter
type BloomFilterState struct {
ItemCount uint64 `json:"item_count"`
Size uint64 `json:"size"`
HashCount int `json:"hash_count"`
Bitset []bool `json:"bitset"`
Version string `json:"version"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// NewBloomFilter creates a new production-ready Bloom filter with comprehensive validation
func NewBloomFilter(config *BloomConfig) (*BloomFilter, error) {
// Validate configuration
if err := validateBloomConfig(config); err != nil {
return nil, fmt.Errorf("%w: %v", ErrInvalidConfig, err)
}
// Apply defaults
applyBloomDefaults(config)
// Calculate optimal size and hash count with validation
size, hashCount, err := calculateOptimalParameters(config.ExpectedItems, config.FalsePositiveRate)
if err != nil {
return nil, fmt.Errorf("failed to calculate optimal parameters: %w", err)
}
// Create Bloom filter
bf := &BloomFilter{
bitset: make([]bool, size),
size: size,
hashCount: hashCount,
maxItems: config.ExpectedItems,
falsePositiveRate: config.FalsePositiveRate,
store: config.Store,
keyPrefix: config.KeyPrefix,
ttl: config.TTL,
metrics: &BloomMetrics{},
}
// Load existing state if store is provided
if config.Store != nil {
if err := bf.loadFromStoreWithRetry(context.Background()); err != nil {
logx.Warn("Failed to load bloom filter from store, starting fresh",
logx.ErrorField(err),
logx.String("key_prefix", config.KeyPrefix))
}
}
logx.Info("Bloom filter created successfully",
logx.String("size", fmt.Sprintf("%d", size)),
logx.Int("hash_count", hashCount),
logx.String("expected_items", fmt.Sprintf("%d", config.ExpectedItems)),
logx.Float64("false_positive_rate", config.FalsePositiveRate),
logx.String("key_prefix", config.KeyPrefix))
return bf, nil
}
// Add adds an item to the Bloom filter with comprehensive validation and improved context handling
func (bf *BloomFilter) Add(ctx context.Context, item string) error {
// Check if filter is closed
if atomic.LoadInt32(&bf.closed) == 1 {
return ErrFilterClosed
}
// Validate input
if err := validateItem(item); err != nil {
return fmt.Errorf("invalid item: %w", err)
}
// Check context cancellation before starting operation
if err := ctx.Err(); err != nil {
return fmt.Errorf("%w: %v", ErrContextCancelled, err)
}
start := time.Now()
defer func() {
bf.updateAddMetrics(time.Since(start))
}()
// Use a timeout context to prevent indefinite blocking
opCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
bf.mu.Lock()
defer bf.mu.Unlock()
// Check context cancellation after acquiring lock with timeout context
if err := opCtx.Err(); err != nil {
return fmt.Errorf("%w: %v", ErrContextCancelled, err)
}
// Check capacity
if bf.itemCount >= bf.maxItems {
logx.Warn("Bloom filter at capacity, item not added",
logx.String("item_count", fmt.Sprintf("%d", bf.itemCount)),
logx.String("max_items", fmt.Sprintf("%d", bf.maxItems)),
logx.String("item", truncateString(item, 100)))
return fmt.Errorf("%w: current=%d, max=%d", ErrCapacityExceeded, bf.itemCount, bf.maxItems)
}
// Get hash positions
positions := bf.getHashPositions(item)
// Set bits
for _, pos := range positions {
bf.bitset[pos] = true
}
bf.itemCount++
bf.metrics.AddOperations.Add(1)
// Persist to store if available with dedicated store mutex
if bf.store != nil {
if err := bf.saveToStoreWithRetry(opCtx); err != nil {
logx.Error("Failed to persist bloom filter state",
logx.ErrorField(err),
logx.String("item", truncateString(item, 100)))
bf.metrics.StoreErrors.Add(1)
// Don't fail the operation if persistence fails
}
}
return nil
}
// Contains checks if an item might be in the Bloom filter with validation and improved context handling
func (bf *BloomFilter) Contains(ctx context.Context, item string) (bool, error) {
// Check if filter is closed
if atomic.LoadInt32(&bf.closed) == 1 {
return false, ErrFilterClosed
}
// Validate input
if err := validateItem(item); err != nil {
return false, fmt.Errorf("invalid item: %w", err)
}
// Check context cancellation before starting operation
if err := ctx.Err(); err != nil {
return false, fmt.Errorf("%w: %v", ErrContextCancelled, err)
}
start := time.Now()
defer func() {
bf.updateContainsMetrics(time.Since(start))
}()
// Use a timeout context to prevent indefinite blocking
opCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
bf.mu.RLock()
defer bf.mu.RUnlock()
// Check context cancellation after acquiring lock with timeout context
if err := opCtx.Err(); err != nil {
return false, fmt.Errorf("%w: %v", ErrContextCancelled, err)
}
// Get hash positions
positions := bf.getHashPositions(item)
// Check if all bits are set
for _, pos := range positions {
if !bf.bitset[pos] {
bf.metrics.TrueNegatives.Add(1)
return false, nil
}
}
bf.metrics.ContainsOperations.Add(1)
// Note: We can't distinguish between true positives and false positives
// without external verification, so we don't update false positive metrics here
return true, nil
}
// AddBatch adds multiple items to the Bloom filter with validation and improved context handling
func (bf *BloomFilter) AddBatch(ctx context.Context, items []string) error {
// Check if filter is closed
if atomic.LoadInt32(&bf.closed) == 1 {
return ErrFilterClosed
}
// Validate input
if err := validateBatch(items); err != nil {
return fmt.Errorf("invalid batch: %w", err)
}
// Check context cancellation before starting operation
if err := ctx.Err(); err != nil {
return fmt.Errorf("%w: %v", ErrContextCancelled, err)
}
start := time.Now()
defer func() {
bf.updateAddBatchMetrics(time.Since(start), len(items))
}()
// Use a timeout context to prevent indefinite blocking
opCtx, cancel := context.WithTimeout(ctx, 60*time.Second) // Longer timeout for batch operations
defer cancel()
bf.mu.Lock()
defer bf.mu.Unlock()
// Check context cancellation after acquiring lock with timeout context
if err := opCtx.Err(); err != nil {
return fmt.Errorf("%w: %v", ErrContextCancelled, err)
}
addedCount := 0
for i, item := range items {
// Check context cancellation periodically during batch processing with timeout context
if i%100 == 0 && opCtx.Err() != nil {
return fmt.Errorf("%w: %v", ErrContextCancelled, opCtx.Err())
}
// Check capacity
if bf.itemCount >= bf.maxItems {
logx.Warn("Bloom filter at capacity during batch add",
logx.String("item_count", fmt.Sprintf("%d", bf.itemCount)),
logx.String("max_items", fmt.Sprintf("%d", bf.maxItems)),
logx.Int("items_processed", addedCount),
logx.Int("total_items", len(items)))
break
}
// Get hash positions
positions := bf.getHashPositions(item)
// Set bits
for _, pos := range positions {
bf.bitset[pos] = true
}
bf.itemCount++
addedCount++
}
// Update metrics accurately for batch operations with atomic operations
bf.metrics.AddBatchOperations.Add(1)
bf.metrics.AddOperations.Add(uint64(addedCount)) // Count individual adds
// Persist to store if available with dedicated store mutex
if bf.store != nil && addedCount > 0 {
if err := bf.saveToStoreWithRetry(opCtx); err != nil {
logx.Error("Failed to persist bloom filter state after batch add",
logx.ErrorField(err),
logx.Int("items_added", addedCount))
bf.metrics.StoreErrors.Add(1)
// Don't fail the operation if persistence fails
}
}
return nil
}
// ContainsBatch checks if multiple items might be in the Bloom filter with improved context handling
func (bf *BloomFilter) ContainsBatch(ctx context.Context, items []string) (map[string]bool, error) {
// Check if filter is closed
if atomic.LoadInt32(&bf.closed) == 1 {
return nil, ErrFilterClosed
}
// Validate input
if err := validateBatch(items); err != nil {
return nil, fmt.Errorf("invalid batch: %w", err)
}
// Check context cancellation before starting operation
if err := ctx.Err(); err != nil {
return nil, fmt.Errorf("%w: %v", ErrContextCancelled, err)
}
start := time.Now()
defer func() {
bf.updateContainsBatchMetrics(time.Since(start), len(items))
}()
// Use a timeout context to prevent indefinite blocking
opCtx, cancel := context.WithTimeout(ctx, 60*time.Second) // Longer timeout for batch operations
defer cancel()
bf.mu.RLock()
defer bf.mu.RUnlock()
// Check context cancellation after acquiring lock with timeout context
if err := opCtx.Err(); err != nil {
return nil, fmt.Errorf("%w: %v", ErrContextCancelled, err)
}
results := make(map[string]bool, len(items))
for i, item := range items {
// Check context cancellation periodically during batch processing with timeout context
if i%100 == 0 && opCtx.Err() != nil {
return nil, fmt.Errorf("%w: %v", ErrContextCancelled, opCtx.Err())
}
// Get hash positions
positions := bf.getHashPositions(item)
// Check if all bits are set
contains := true
for _, pos := range positions {
if !bf.bitset[pos] {
contains = false
break
}
}
results[item] = contains
}
// Update metrics accurately for batch operations with atomic operations
bf.metrics.ContainsBatchOperations.Add(1)
bf.metrics.ContainsOperations.Add(uint64(len(items))) // Count individual contains
return results, nil
}
// Clear clears the Bloom filter
func (bf *BloomFilter) Clear(ctx context.Context) error {
// Check if filter is closed
if atomic.LoadInt32(&bf.closed) == 1 {
return errors.New("bloom filter is closed")
}
// Check context cancellation
if err := ctx.Err(); err != nil {
return fmt.Errorf("%w: %v", ErrContextCancelled, err)
}
bf.mu.Lock()
defer bf.mu.Unlock()
// Clear bitset
for i := range bf.bitset {
bf.bitset[i] = false
}
bf.itemCount = 0
// Clear from store if available
if bf.store != nil {
if err := bf.store.Del(ctx, bf.getStoreKey()); err != nil {
logx.Error("Failed to clear bloom filter from store", logx.ErrorField(err))
bf.metrics.StoreErrors.Add(1)
return fmt.Errorf("%w: %v", ErrStoreOperationFailed, err)
}
}
logx.Info("Bloom filter cleared successfully")
return nil
}
// GetStats returns comprehensive Bloom filter statistics with improved thread safety
func (bf *BloomFilter) GetStats() map[string]interface{} {
bf.mu.RLock()
defer bf.mu.RUnlock()
// Calculate current false positive rate
currentFPR := bf.calculateCurrentFalsePositiveRate()
bitsSet := bf.countSetBits()
stats := map[string]interface{}{
"size": bf.size,
"hash_count": bf.hashCount,
"item_count": bf.itemCount,
"max_items": bf.maxItems,
"desired_false_positive_rate": bf.falsePositiveRate,
"current_false_positive_rate": currentFPR,
"load_factor": float64(bf.itemCount) / float64(bf.maxItems),
"bits_set": bitsSet,
"bits_set_percentage": float64(bitsSet) / float64(bf.size) * 100,
"is_at_capacity": bf.itemCount >= bf.maxItems,
"available_capacity": bf.maxItems - bf.itemCount,
}
// Add metrics with atomic operations for thread safety
if bf.metrics != nil {
stats["add_operations"] = bf.metrics.AddOperations.Load()
stats["contains_operations"] = bf.metrics.ContainsOperations.Load()
stats["add_batch_operations"] = bf.metrics.AddBatchOperations.Load()
stats["contains_batch_operations"] = bf.metrics.ContainsBatchOperations.Load()
stats["false_positives"] = bf.metrics.FalsePositives.Load()
stats["true_negatives"] = bf.metrics.TrueNegatives.Load()
stats["store_operations"] = bf.metrics.StoreOperations.Load()
stats["store_errors"] = bf.metrics.StoreErrors.Load()
// Use mutex only for complex metrics that can't be atomic
bf.metrics.mu.RLock()
defer bf.metrics.mu.RUnlock()
stats["average_add_latency_ns"] = bf.metrics.AverageAddLatency.Nanoseconds()
stats["average_contains_latency_ns"] = bf.metrics.AverageContainsLatency.Nanoseconds()
}
return stats
}
// Close closes the Bloom filter and releases resources
func (bf *BloomFilter) Close() error {
if !atomic.CompareAndSwapInt32(&bf.closed, 0, 1) {
return errors.New("bloom filter already closed")
}
logx.Info("Bloom filter closed successfully")
return nil
}
// IsClosed returns true if the Bloom filter is closed
func (bf *BloomFilter) IsClosed() bool {
return atomic.LoadInt32(&bf.closed) == 1
}
// getHashPositions calculates hash positions for an item using multiple hash functions
func (bf *BloomFilter) getHashPositions(item string) []uint64 {
positions := make([]uint64, bf.hashCount)
// Use multiple hash functions for better distribution
h1 := fnv.New64a()
h1.Write([]byte(item))
hash1 := h1.Sum64()
h2 := md5.New()
h2.Write([]byte(item))
hash2 := binary.BigEndian.Uint64(h2.Sum(nil))
h3 := sha256.New()
h3.Write([]byte(item))
hash3 := binary.BigEndian.Uint64(h3.Sum(nil))
for i := 0; i < bf.hashCount; i++ {
// Combine hashes using triple hashing for better distribution
hash := hash1 + uint64(i)*hash2 + uint64(i*i)*hash3
positions[i] = hash % bf.size
}
return positions
}
// countSetBits counts the number of set bits in the bitset efficiently
func (bf *BloomFilter) countSetBits() uint64 {
count := uint64(0)
for _, bit := range bf.bitset {
if bit {
count++
}
}
return count
}
// calculateCurrentFalsePositiveRate calculates the current false positive rate
func (bf *BloomFilter) calculateCurrentFalsePositiveRate() float64 {
if bf.itemCount == 0 {
return 0.0
}
// Calculate probability of a bit being set
p := float64(bf.countSetBits()) / float64(bf.size)
// False positive rate = p^hashCount
return math.Pow(p, float64(bf.hashCount))
}
// getStoreKey returns the key for storing bloom filter state
func (bf *BloomFilter) getStoreKey() string {
return fmt.Sprintf("%s:filter", bf.keyPrefix)
}
// saveToStoreWithRetry saves the bloom filter state to the store with retry logic and store synchronization
func (bf *BloomFilter) saveToStoreWithRetry(ctx context.Context) error {
if bf.store == nil {
return ErrStoreUnavailable
}
var lastErr error
for attempt := 0; attempt < MaxRetryAttemptsBloom; attempt++ {
if err := ctx.Err(); err != nil {
return fmt.Errorf("%w: %v", ErrContextCancelled, err)
}
// Use dedicated store mutex to prevent concurrent store operations
// but release it between retries to prevent deadlocks
bf.storeMu.Lock()
err := bf.saveToStore(ctx)
bf.storeMu.Unlock()
if err != nil {
lastErr = err
if attempt < MaxRetryAttemptsBloom-1 {
// Sleep outside of the mutex to prevent deadlocks
select {
case <-ctx.Done():
return fmt.Errorf("%w: %v", ErrContextCancelled, ctx.Err())
case <-time.After(RetryDelayBloom * time.Duration(attempt+1)):
continue
}
}
} else {
bf.metrics.StoreOperations.Add(1)
return nil
}
}
return fmt.Errorf("%w: %v", ErrStoreOperationFailed, lastErr)
}
// saveToStore saves the bloom filter state to the store with enhanced serialization
func (bf *BloomFilter) saveToStore(ctx context.Context) error {
// Check context cancellation before starting store operation
if err := ctx.Err(); err != nil {
return fmt.Errorf("%w: %v", ErrContextCancelled, err)
}
state := &BloomFilterState{
ItemCount: bf.itemCount,
Size: bf.size,
HashCount: bf.hashCount,
Bitset: make([]bool, len(bf.bitset)),
Version: "1.1", // Enhanced version for production
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
// Copy bitset
copy(state.Bitset, bf.bitset)
// Enhanced serialization with compression and validation
data, err := bf.serializeState(state)
if err != nil {
return fmt.Errorf("%w: %v", ErrSerializationFailed, err)
}
// Check context cancellation before store operation
if err := ctx.Err(); err != nil {
return fmt.Errorf("%w: %v", ErrContextCancelled, err)
}
return bf.store.Set(ctx, bf.getStoreKey(), data, bf.ttl)
}
// serializeState provides enhanced serialization for production
func (bf *BloomFilter) serializeState(state *BloomFilterState) ([]byte, error) {
// Create enhanced serialization structure
enhancedState := map[string]interface{}{
"version": state.Version,
"item_count": state.ItemCount,
"size": state.Size,
"hash_count": state.HashCount,
"created_at": state.CreatedAt.Format(time.RFC3339),
"updated_at": state.UpdatedAt.Format(time.RFC3339),
"metadata": map[string]interface{}{
"false_positive_rate": bf.falsePositiveRate,
"max_items": bf.maxItems,
"key_prefix": bf.keyPrefix,
},
}
// Convert bitset to compact binary format for efficiency
bitsetBytes := bf.bitsetToBytes(state.Bitset)
enhancedState["bitset"] = bitsetBytes
// Serialize to JSON with proper error handling
data, err := json.Marshal(enhancedState)
if err != nil {
return nil, fmt.Errorf("failed to marshal enhanced state: %w", err)
}
return data, nil
}
// bitsetToBytes converts bitset to compact binary format
func (bf *BloomFilter) bitsetToBytes(bitset []bool) []byte {
// Calculate required bytes (8 bits per byte)
byteCount := (len(bitset) + 7) / 8
bytes := make([]byte, byteCount)
for i, bit := range bitset {
if bit {
byteIndex := i / 8
bitIndex := i % 8
bytes[byteIndex] |= 1 << bitIndex
}
}
return bytes
}
// loadFromStoreWithRetry loads the bloom filter state from the store with retry logic and store synchronization
func (bf *BloomFilter) loadFromStoreWithRetry(ctx context.Context) error {
if bf.store == nil {
return ErrStoreUnavailable
}
var lastErr error
for attempt := 0; attempt < MaxRetryAttemptsBloom; attempt++ {
if err := ctx.Err(); err != nil {
return fmt.Errorf("%w: %v", ErrContextCancelled, err)
}
// Use dedicated store mutex to prevent concurrent store operations
// but release it between retries to prevent deadlocks
bf.storeMu.Lock()
err := bf.loadFromStore(ctx)
bf.storeMu.Unlock()
if err != nil {
lastErr = err
if attempt < MaxRetryAttemptsBloom-1 {
// Sleep outside of the mutex to prevent deadlocks
select {
case <-ctx.Done():
return fmt.Errorf("%w: %v", ErrContextCancelled, ctx.Err())
case <-time.After(RetryDelayBloom * time.Duration(attempt+1)):
continue
}
}
} else {
bf.metrics.StoreOperations.Add(1)
return nil
}
}
return fmt.Errorf("%w: %v", ErrStoreOperationFailed, lastErr)
}
// loadFromStore loads the bloom filter state from the store with enhanced deserialization
func (bf *BloomFilter) loadFromStore(ctx context.Context) error {
// Check context cancellation before starting store operation
if err := ctx.Err(); err != nil {
return fmt.Errorf("%w: %v", ErrContextCancelled, err)
}
data, err := bf.store.Get(ctx, bf.getStoreKey())
if err != nil {
return err
}
// Check context cancellation after store operation
if err := ctx.Err(); err != nil {
return fmt.Errorf("%w: %v", ErrContextCancelled, err)
}
state, err := bf.deserializeState(data)
if err != nil {
return fmt.Errorf("%w: %v", ErrDeserializationFailed, err)
}
// Validate loaded state
if err := validateBloomFilterState(state, bf.size, bf.hashCount); err != nil {
return fmt.Errorf("invalid loaded state: %w", err)
}
// Apply loaded state
bf.mu.Lock()
defer bf.mu.Unlock()
bf.itemCount = state.ItemCount
copy(bf.bitset, state.Bitset)
logx.Info("Bloom filter state loaded from store",
logx.String("item_count", fmt.Sprintf("%d", state.ItemCount)),
logx.String("version", state.Version),
logx.String("updated_at", state.UpdatedAt.Format(time.RFC3339)))
return nil
}
// deserializeState provides enhanced deserialization for production
func (bf *BloomFilter) deserializeState(data []byte) (*BloomFilterState, error) {
// Try enhanced format first
var enhancedState map[string]interface{}
if err := json.Unmarshal(data, &enhancedState); err != nil {
// Fallback to legacy format
return bf.deserializeLegacyState(data)
}
// Extract version and validate
version, ok := enhancedState["version"].(string)
if !ok {
return nil, errors.New("invalid version in enhanced state")
}
// Extract basic fields
itemCount, ok := enhancedState["item_count"].(float64)
if !ok {
return nil, errors.New("invalid item_count in enhanced state")
}
size, ok := enhancedState["size"].(float64)
if !ok {
return nil, errors.New("invalid size in enhanced state")
}
hashCount, ok := enhancedState["hash_count"].(float64)
if !ok {
return nil, errors.New("invalid hash_count in enhanced state")
}
// Extract bitset
bitsetBytes, ok := enhancedState["bitset"].([]interface{})
if !ok {
return nil, errors.New("invalid bitset in enhanced state")
}
// Convert bitset bytes back to bool slice
bitset := bf.bytesToBitset(bitsetBytes, int(size))
// Parse timestamps
createdAtStr, ok := enhancedState["created_at"].(string)
if !ok {
createdAtStr = time.Now().Format(time.RFC3339)
}
updatedAtStr, ok := enhancedState["updated_at"].(string)
if !ok {
updatedAtStr = time.Now().Format(time.RFC3339)
}
createdAt, _ := time.Parse(time.RFC3339, createdAtStr)
updatedAt, _ := time.Parse(time.RFC3339, updatedAtStr)
state := &BloomFilterState{
ItemCount: uint64(itemCount),
Size: uint64(size),
HashCount: int(hashCount),
Bitset: bitset,
Version: version,
CreatedAt: createdAt,
UpdatedAt: updatedAt,
}
return state, nil
}
// deserializeLegacyState handles legacy serialization format
func (bf *BloomFilter) deserializeLegacyState(data []byte) (*BloomFilterState, error) {
var state BloomFilterState
if err := json.Unmarshal(data, &state); err != nil {
return nil, fmt.Errorf("failed to unmarshal legacy state: %w", err)
}
// Set default values for missing fields
if state.Version == "" {
state.Version = "1.0"
}
if state.CreatedAt.IsZero() {
state.CreatedAt = time.Now()
}
if state.UpdatedAt.IsZero() {
state.UpdatedAt = time.Now()
}
return &state, nil
}
// bytesToBitset converts compact binary format back to bool slice
func (bf *BloomFilter) bytesToBitset(bytes []interface{}, size int) []bool {
bitset := make([]bool, size)
for i := 0; i < size; i++ {
byteIndex := i / 8
bitIndex := i % 8
if byteIndex < len(bytes) {
if byteVal, ok := bytes[byteIndex].(float64); ok {
bitset[i] = (uint8(byteVal) & (1 << bitIndex)) != 0
}
}
}
return bitset
}
// updateAddMetrics updates add operation metrics with improved thread safety
func (bf *BloomFilter) updateAddMetrics(duration time.Duration) {
if bf.metrics == nil {
return
}
// Use mutex only for complex metrics that can't be atomic
bf.metrics.mu.Lock()
defer bf.metrics.mu.Unlock()
bf.metrics.LastAddTime = time.Now()
if bf.metrics.AverageAddLatency == 0 {
bf.metrics.AverageAddLatency = duration
} else {
bf.metrics.AverageAddLatency = (bf.metrics.AverageAddLatency + duration) / 2
}
}
// updateContainsMetrics updates contains operation metrics with improved thread safety
func (bf *BloomFilter) updateContainsMetrics(duration time.Duration) {
if bf.metrics == nil {
return
}
// Use mutex only for complex metrics that can't be atomic
bf.metrics.mu.Lock()
defer bf.metrics.mu.Unlock()
bf.metrics.LastContainsTime = time.Now()
if bf.metrics.AverageContainsLatency == 0 {
bf.metrics.AverageContainsLatency = duration
} else {
bf.metrics.AverageContainsLatency = (bf.metrics.AverageContainsLatency + duration) / 2
}
}
// updateAddBatchMetrics updates batch add operation metrics with accurate counting and improved thread safety
func (bf *BloomFilter) updateAddBatchMetrics(duration time.Duration, itemCount int) {
if bf.metrics == nil {
return
}
// Use mutex only for complex metrics that can't be atomic
bf.metrics.mu.Lock()
defer bf.metrics.mu.Unlock()
bf.metrics.LastAddTime = time.Now()
// Calculate average latency per item for batch operations
if itemCount > 0 {
avgPerItem := duration / time.Duration(itemCount)
if bf.metrics.AverageAddLatency == 0 {
bf.metrics.AverageAddLatency = avgPerItem
} else {
// Weighted average to account for batch operations
bf.metrics.AverageAddLatency = (bf.metrics.AverageAddLatency + avgPerItem) / 2
}
}
}
// updateContainsBatchMetrics updates batch contains operation metrics with accurate counting and improved thread safety
func (bf *BloomFilter) updateContainsBatchMetrics(duration time.Duration, itemCount int) {
if bf.metrics == nil {
return
}
// Use mutex only for complex metrics that can't be atomic
bf.metrics.mu.Lock()
defer bf.metrics.mu.Unlock()
bf.metrics.LastContainsTime = time.Now()
// Calculate average latency per item for batch operations
if itemCount > 0 {
avgPerItem := duration / time.Duration(itemCount)
if bf.metrics.AverageContainsLatency == 0 {
bf.metrics.AverageContainsLatency = avgPerItem
} else {
// Weighted average to account for batch operations
bf.metrics.AverageContainsLatency = (bf.metrics.AverageContainsLatency + avgPerItem) / 2
}
}
}
// validateBloomConfig validates Bloom filter configuration
func validateBloomConfig(config *BloomConfig) error {
if config == nil {
return errors.New("config cannot be nil")
}
if config.ExpectedItems < MinExpectedItemsBloom || config.ExpectedItems > MaxExpectedItemsBloom {
return fmt.Errorf("%w: expected items must be between %d and %d, got %d",
ErrInvalidExpectedItems, MinExpectedItemsBloom, MaxExpectedItemsBloom, config.ExpectedItems)
}
if config.FalsePositiveRate < MinFalsePositiveBloom || config.FalsePositiveRate > MaxFalsePositiveBloom {
return fmt.Errorf("%w: false positive rate must be between %f and %f, got %f",
ErrInvalidFalsePositive, MinFalsePositiveBloom, MaxFalsePositiveBloom, config.FalsePositiveRate)
}
return nil
}
// validateItem validates a single item
func validateItem(item string) error {
if item == "" {
return ErrEmptyItem
}
if len(item) > MaxItemLengthBloom {
return fmt.Errorf("%w: item length %d exceeds maximum %d",
ErrItemTooLong, len(item), MaxItemLengthBloom)
}
return nil
}
// validateBatch validates a slice of items
func validateBatch(items []string) error {
if len(items) == 0 {
return errors.New("items slice cannot be empty")
}
for i, item := range items {
if err := validateItem(item); err != nil {
return fmt.Errorf("invalid item at index %d: %w", i, err)
}
}
return nil
}
// validateBloomFilterState validates the loaded Bloom filter state
func validateBloomFilterState(state *BloomFilterState, expectedSize uint64, expectedHashCount int) error {
if state == nil {
return errors.New("state cannot be nil")
}
if state.Size != expectedSize {
return fmt.Errorf("loaded size %d does not match expected %d", state.Size, expectedSize)