|
| 1 | +package storage |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/splitio/go-split-commons/v8/dtos" |
| 5 | + "github.com/splitio/go-split-commons/v8/storage" |
| 6 | + "github.com/splitio/go-split-commons/v8/storage/inmemory/mutexmap" |
| 7 | + "github.com/splitio/go-toolkit/v5/datastructures/set" |
| 8 | + "github.com/splitio/go-toolkit/v5/logging" |
| 9 | +) |
| 10 | + |
| 11 | +// ProxyRuleBasedSegmentsStorage defines the interface of a storage that can be used for serving payloads |
| 12 | +// for different requested `since` parameters |
| 13 | +type ProxyRuleBasedSegmentsStorage interface { |
| 14 | + ChangesSince(since int64) (*dtos.RuleBasedSegmentsDTO, error) |
| 15 | +} |
| 16 | + |
| 17 | +// ProxyRuleBasedSegmentsStorageImpl implements the ProxyRuleBasedSegmentsStorage interface and the SplitProducer interface |
| 18 | +type ProxyRuleBasedSegmentsStorageImpl struct { |
| 19 | + snapshot mutexmap.RuleBasedSegmentsStorageImpl |
| 20 | + logger logging.LoggerInterface |
| 21 | + // mtx sync.Mutex |
| 22 | +} |
| 23 | + |
| 24 | +// NewProxyRuleBasedSegmentsStorage instantiates a new proxy storage that wraps an in-memory snapshot of the last known |
| 25 | +// flag configuration |
| 26 | +func NewProxyRuleBasedSegmentsStorage(logger logging.LoggerInterface) *ProxyRuleBasedSegmentsStorageImpl { |
| 27 | + snapshot := mutexmap.NewRuleBasedSegmentsStorage() |
| 28 | + |
| 29 | + return &ProxyRuleBasedSegmentsStorageImpl{ |
| 30 | + snapshot: *snapshot, |
| 31 | + logger: logger, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// ChangesSince retrieves the rule-based segments changes since the given change number |
| 36 | +func (p *ProxyRuleBasedSegmentsStorageImpl) ChangesSince(since int64) (*dtos.RuleBasedSegmentsDTO, error) { |
| 37 | + cn, _ := p.snapshot.ChangeNumber() |
| 38 | + return &dtos.RuleBasedSegmentsDTO{Since: since, Till: cn, RuleBasedSegments: p.snapshot.All()}, nil |
| 39 | +} |
| 40 | + |
| 41 | +// All call is forwarded to the snapshot |
| 42 | +func (p *ProxyRuleBasedSegmentsStorageImpl) All() []dtos.RuleBasedSegmentDTO { |
| 43 | + return p.snapshot.All() |
| 44 | +} |
| 45 | + |
| 46 | +// ChangeNumber returns the current change number |
| 47 | +func (p *ProxyRuleBasedSegmentsStorageImpl) ChangeNumber() (int64, error) { |
| 48 | + return p.snapshot.ChangeNumber() |
| 49 | +} |
| 50 | + |
| 51 | +// Contains checks if the given rule-based segments are present in storage |
| 52 | +func (p *ProxyRuleBasedSegmentsStorageImpl) Contains(rbs []string) bool { |
| 53 | + return p.snapshot.Contains(rbs) |
| 54 | +} |
| 55 | + |
| 56 | +// GetRuleBasedSegmentByName retrieves a rule-based segment by name |
| 57 | +func (p *ProxyRuleBasedSegmentsStorageImpl) GetRuleBasedSegmentByName(name string) (*dtos.RuleBasedSegmentDTO, error) { |
| 58 | + return p.snapshot.GetRuleBasedSegmentByName(name) |
| 59 | +} |
| 60 | + |
| 61 | +// LargeSegments call is forwarded to the snapshot |
| 62 | +func (p *ProxyRuleBasedSegmentsStorageImpl) LargeSegments() *set.ThreadUnsafeSet { |
| 63 | + return p.snapshot.LargeSegments() |
| 64 | +} |
| 65 | + |
| 66 | +// ReplaceAll replaces all rule-based segments in storage |
| 67 | +func (p *ProxyRuleBasedSegmentsStorageImpl) ReplaceAll(rbs []dtos.RuleBasedSegmentDTO, cn int64) error { |
| 68 | + return p.snapshot.ReplaceAll(rbs, cn) |
| 69 | +} |
| 70 | + |
| 71 | +// RuleBasedSegmentNames retrieves the names of all rule-based segments |
| 72 | +func (p *ProxyRuleBasedSegmentsStorageImpl) RuleBasedSegmentNames() []string { |
| 73 | + return p.snapshot.RuleBasedSegmentNames() |
| 74 | +} |
| 75 | + |
| 76 | +// Segments retrieves the names of all segments used in rule-based segments |
| 77 | +func (p *ProxyRuleBasedSegmentsStorageImpl) Segments() *set.ThreadUnsafeSet { |
| 78 | + return p.snapshot.Segments() |
| 79 | +} |
| 80 | + |
| 81 | +// SetChangeNumber sets the change number |
| 82 | +func (p *ProxyRuleBasedSegmentsStorageImpl) SetChangeNumber(cn int64) error { |
| 83 | + return p.snapshot.SetChangeNumber(cn) |
| 84 | +} |
| 85 | + |
| 86 | +// Update |
| 87 | +func (p *ProxyRuleBasedSegmentsStorageImpl) Update(toAdd []dtos.RuleBasedSegmentDTO, toRemove []dtos.RuleBasedSegmentDTO, cn int64) { |
| 88 | + // TODO Add the other logic |
| 89 | + // p.setStartingPoint(changeNumber) // will be executed only the first time this method is called |
| 90 | + |
| 91 | + // if len(toAdd) == 0 && len(toRemove) == 0 { |
| 92 | + // return |
| 93 | + // } |
| 94 | + |
| 95 | + // p.mtx.Lock() |
| 96 | + // p.snapshot.Update(toAdd, toRemove, changeNumber) |
| 97 | + // p.historic.Update(toAdd, toRemove, changeNumber) |
| 98 | + // p.db.Update(toAdd, toRemove, changeNumber) |
| 99 | + // p.mtx.Unlock() |
| 100 | + |
| 101 | + p.snapshot.Update(toAdd, toRemove, cn) |
| 102 | +} |
| 103 | + |
| 104 | +var _ ProxyRuleBasedSegmentsStorage = (*ProxyRuleBasedSegmentsStorageImpl)(nil) |
| 105 | +var _ storage.RuleBasedSegmentsStorage = (*ProxyRuleBasedSegmentsStorageImpl)(nil) |
0 commit comments