forked from splitio/python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.py
More file actions
329 lines (279 loc) · 10.2 KB
/
Copy pathcache.py
File metadata and controls
329 lines (279 loc) · 10.2 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
"""This module contains everything related split and segment caches"""
from __future__ import absolute_import, division, print_function, unicode_literals
from collections import defaultdict
from copy import deepcopy
from threading import RLock
class SplitCache(object): # pragma: no cover
"""
The basic interface for a Split cache. It should be able to store and retrieve Split
instances, as well as keeping track of the change number.
"""
def add_split(self, split_name, split):
"""
Stores a Split under a name.
:param split_name: Name of the split (feature)
:type split_name: str
:param split: The split to store
:type split: Split
"""
pass # Do nothing
def remove_split(self, split_name):
"""
Evicts a Split from the cache.
:param split_name: Name of the split (feature)
:type split_name: str
"""
pass # Do nothing
def get_split(self, split_name):
"""
Retrieves a Split from the cache.
:param split_name: Name of the split (feature)
:type split_name: str
:return: The split under the name if it exists, None otherwise
:rtype: Split
"""
return None
def set_change_number(self, change_number):
"""
Sets the value for the change number
:param change_number: The change number
:type change_number: int
"""
pass # Do nothing
def get_change_number(self):
"""
Retrieves the value of the change number
:return: The current change number value, -1 otherwise
:rtype: int
"""
return -1
class SegmentCache(object): # pragma: no cover
"""
The basic interface for a Segment cache. It should be able to store and retrieve Segment
information, as well as keeping track of the change number.
"""
def add_keys_to_segment(self, segment_name, segment_keys):
"""
Adds a set of keys to a segment
:param segment_name: Name of the segment
:type segment_name: str
:param segment_keys: Keys to add to the segment
:type segment_keys: list
"""
pass # Do nothing
def remove_keys_from_segment(self, segment_name, segment_keys):
"""
Removes a set of keys from a segment
:param segment_name: Name of the segment
:type segment_name: str
:param segment_keys: Keys to remove from the segment
:type segment_keys: list
"""
pass # Do nothing
def is_in_segment(self, segment_name, key):
"""
Checks if a key is in a segment
:param segment_name: Name of the segment
:type segment_name: str
:param key: Key to check
:type key: str
:return: True if the key is in the segment, False otherwise
:rtype: bool
"""
return False
def set_change_number(self, segment_name, change_number):
"""
Sets the value for the change number
:param segment_name: Name of the segment
:type segment_name: str
:param change_number: The change number
:type change_number: int
"""
pass # Do nothing
def get_change_number(self, segment_name):
"""
Retrieves the value of the change number of a segment
:param segment_name: Name of the segment
:type segment_name: str
:return: The current change number value, -1 otherwise
:rtype: int
"""
return -1
class InMemorySplitCache(SplitCache):
def __init__(self, change_number=-1, entries=None):
"""
A SplitCache that stores splits in a dictionary.
:param change_number: Initial value for the change number.
:type change_number: int
:param entries: Initial set of dictionary entries
:type entries: dict
"""
self._change_number = change_number
self._entries = entries if entries is not None else dict()
def add_split(self, split_name, split):
self._entries[split_name] = split
def remove_split(self, split_name):
self._entries.pop(split_name, None)
def get_split(self, split_name):
return self._entries.get(split_name, None)
def set_change_number(self, change_number):
self._change_number = change_number
def get_change_number(self):
return self._change_number
class InMemorySegmentCache(SegmentCache):
def __init__(self):
"""A SegmentCache implementation that stores segments in a dictionary"""
self._entries = defaultdict(lambda: {'change_number': -1, 'key_set': frozenset()})
def add_keys_to_segment(self, segment_name, segment_keys):
segment = self._entries[segment_name]
segment['key_set'] = segment['key_set'] | frozenset(segment_keys)
def remove_keys_from_segment(self, segment_name, segment_keys):
segment = self._entries[segment_name]
segment['key_set'] = segment['key_set'] - frozenset(segment_keys)
def is_in_segment(self, segment_name, key):
return key in self._entries[segment_name]['key_set']
def set_change_number(self, segment_name, change_number):
self._entries[segment_name]['change_number'] = change_number
def get_change_number(self, segment_name):
return self._entries[segment_name]['change_number']
class ImpressionsCache(object): # pragma: no cover
"""The basic interface for an Impressions cache."""
def add_impression(self, impression):
"""Add an impression to a feature
:param impression: An impression
:type impression: Impression
:return: How many impressions have been added so far
:rtype: int
"""
pass # Do nothing
def fetch_all(self):
""" List all impressions.
:return: A list of Impression tuples
:rtype: list
"""
return []
def clear(self):
"""Clears all impressions."""
pass # Do nothing
def fetch_all_and_clear(self):
""" List all impressions and clear the cache.
:return: A list of Impression tuples
:rtype: list
"""
return []
class InMemoryImpressionsCache(ImpressionsCache): # pragma: no cover
def __init__(self, impressions=None):
"""An in memory implementation of an Impressions cache.
:param impressions: Initial set of impressions
:type impressions: dict
"""
self._impressions = defaultdict(list)
if impressions is not None:
self._impressions.update(impressions)
self._rlock = RLock()
def add_impression(self, impression):
"""Add an impression to a feature
:param impression: An impression
:type impression: Impression
"""
with self._rlock:
self._impressions[impression.feature].append(impression)
def fetch_all(self):
""" List all impressions.
:return: A list of Impression tuples
:rtype: dict
"""
return deepcopy(self._impressions)
def clear(self):
"""Clears all impressions."""
with self._rlock:
self._impressions = defaultdict(list)
def fetch_all_and_clear(self):
""" List all impressions and clear the cache.
:return: A list of Impression tuples
:rtype: list
"""
with self._rlock:
impressions = self.fetch_all()
self.clear()
return impressions
class MetricsCache(object): # pragma: no cover
"""A default implementation of a Metrics cache."""
def set_count(self, counter, value):
"""Sets a counter value.
:param counter: Name of the counter
:type counter: str
:param value: Value for the counter
:type value: 1
"""
pass # Do nothing
def increment_count(self, counter, delta=1):
"""Increments the value of a counter by a given value.
:param counter: Name of the counter
:type counter: str
:param delta: The value to be added to the counter
:type delta: int
"""
pass # Do nothing
def get_count(self, counter):
"""
:param counter: Name of the counter
:type counter: str
:return: The current value of the counter
:rtype: int
"""
return 0
def set_gauge(self, gauge, value):
"""Sets the value of a gauge.
:param gauge: The name of the gauge
:type gauge: str
:param value: The value of the gauge
:type value: float
"""
pass # Do nothing
def get_gauge(self, gauge):
"""
:param gauge: The name of the gauge
:type gauge: str
:return: The current value of the gauge
:rtype: float
"""
return 0
def set_latency_bucket_counter(self, operation, bucket_index, value):
"""Sets the value of a bucket of a latency tracker for an operation.
:param operation: The name of the operation
:type operation: str
:param bucket_index: The index for the latency bucket
:type bucket_index: int
:param value: The new value for the bucket
:type value: int
"""
pass # Do nothing
def increment_latency_bucket_counter(self, operation, bucket_index, delta=1):
"""Increments the value of a bucket of a latency tracker for an operation
:param operation: The name of the operation
:type operation: str
:param bucket_index: The index for the latency bucket
:type bucket_index: int
:param delta: The value to add to the bucket
:type delta: int
"""
pass # Do nothing
def get_latency_bucket_counter(self, operation, bucket_index):
"""
:param operation: The name of the operation
:type operation: str
:param bucket_index: The index for the latency bucket
:type bucket_index: int
:return: The current value of a bucket of a latency tracker
:rtype: int
"""
return 0
def get_latency(self, operation):
"""
:param operation: The name of the operation
:type operation: str
:return: All the buckets of a latency tracker
:rtype: list
"""
return [0] * 23