This repository was archived by the owner on May 5, 2022. It is now read-only.
forked from launchdarkly/python-server-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeature_store.py
More file actions
73 lines (64 loc) · 2.32 KB
/
Copy pathfeature_store.py
File metadata and controls
73 lines (64 loc) · 2.32 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
from ldclient.util import log
from ldclient.interfaces import FeatureStore
from ldclient.rwlock import ReadWriteLock
class InMemoryFeatureStore(FeatureStore):
def __init__(self):
self._lock = ReadWriteLock()
self._initialized = False
self._features = {}
def get(self, key, callback):
try:
self._lock.rlock()
f = self._features.get(key)
if f is None:
log.debug("Attempted to get missing feature: " + str(key) + " Returning None")
return callback(None)
if 'deleted' in f and f['deleted']:
log.debug("Attempted to get deleted feature: " + str(key) + " Returning None")
return callback(None)
return callback(f)
finally:
self._lock.runlock()
def all(self, callback):
try:
self._lock.rlock()
return callback(dict((k, f) for k, f in self._features.items() if ('deleted' not in f) or not f['deleted']))
finally:
self._lock.runlock()
def init(self, features):
try:
self._lock.lock()
self._features = dict(features)
self._initialized = True
log.debug("Initialized feature store with " + str(len(features)) + " features")
finally:
self._lock.unlock()
# noinspection PyShadowingNames
def delete(self, key, version):
try:
self._lock.lock()
f = self._features.get(key)
if f is not None and f['version'] < version:
f['deleted'] = True
f['version'] = version
elif f is None:
f = {'deleted': True, 'version': version}
self._features[key] = f
finally:
self._lock.unlock()
def upsert(self, key, feature):
try:
self._lock.lock()
f = self._features.get(key)
if f is None or f['version'] < feature['version']:
self._features[key] = feature
log.debug("Updated feature {0} to version {1}".format(key, feature['version']))
finally:
self._lock.unlock()
@property
def initialized(self):
try:
self._lock.rlock()
return self._initialized
finally:
self._lock.runlock()