class InMemoryFeatureStore(object): def __init__(self): self._lock = ReadWriteLock() self._initialized = False self._features = {} def get(self, key): try: self._lock.rlock() f = self._features.get(key) if f is None or 'deleted' in f and f['deleted']: return None return f finally: self._lock.runlock() def all(self): try: self._lock.rlock() return dict((k,f) for k,f in self._features.iteritems() 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 finally: self._lock.unlock() 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] = f finally: self._lock.unlock() def initialized(self): try: self._lock.rlock() return self._initialized finally: self._lock.runlock()
class InMemoryFeatureStore(FeatureStore): """ In-memory implementation of a store that holds feature flags and related data received from the streaming API. """ def __init__(self): self._lock = ReadWriteLock() self._initialized = False self._items = defaultdict(dict) def get(self, kind, key, callback): try: self._lock.rlock() itemsOfKind = self._items[kind] item = itemsOfKind.get(key) if item is None: log.debug( "Attempted to get missing key %s in '%s', returning None", key, kind.namespace) return callback(None) if 'deleted' in item and item['deleted']: log.debug( "Attempted to get deleted key %s in '%s', returning None", key, kind.namespace) return callback(None) return callback(item) finally: self._lock.runlock() def all(self, kind, callback): try: self._lock.rlock() itemsOfKind = self._items[kind] return callback( dict((k, i) for k, i in itemsOfKind.items() if ('deleted' not in i) or not i['deleted'])) finally: self._lock.runlock() def init(self, all_data): try: self._lock.rlock() self._items.clear() self._items.update(all_data) self._initialized = True for k in all_data: log.debug("Initialized '%s' store with %d items", k.namespace, len(all_data[k])) finally: self._lock.runlock() # noinspection PyShadowingNames def delete(self, kind, key, version): try: self._lock.rlock() itemsOfKind = self._items[kind] i = itemsOfKind.get(key) if i is None or i['version'] < version: i = {'deleted': True, 'version': version} itemsOfKind[key] = i finally: self._lock.runlock() def upsert(self, kind, item): key = item['key'] try: self._lock.rlock() itemsOfKind = self._items[kind] i = itemsOfKind.get(key) if i is None or i['version'] < item['version']: itemsOfKind[key] = item log.debug("Updated %s in '%s' to version %d", key, kind.namespace, item['version']) finally: self._lock.runlock() @property def initialized(self): try: self._lock.rlock() return self._initialized finally: self._lock.runlock()
class InMemoryFeatureStore(FeatureStore): def __init__(self): self._lock = ReadWriteLock() self._initialized = False self._features = {} def get(self, key): 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 None if 'deleted' in f and f['deleted']: log.debug("Attempted to get deleted feature: " + str(key) + " Returning None") return None return f finally: self._lock.runlock() def all(self): try: self._lock.rlock() return 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 {} to version {}".format( key, feature['version'])) finally: self._lock.unlock() @property def initialized(self): try: self._lock.rlock() return self._initialized finally: self._lock.runlock()
class InMemoryFeatureStore(FeatureStore, DiagnosticDescription): """The default feature store implementation, which holds all data in a thread-safe data structure in memory. """ def __init__(self): """Constructs an instance of InMemoryFeatureStore. """ self._lock = ReadWriteLock() self._initialized = False self._items = defaultdict(dict) def get(self, kind, key, callback): """ """ try: self._lock.rlock() itemsOfKind = self._items[kind] item = itemsOfKind.get(key) if item is None: log.debug( "Attempted to get missing key %s in '%s', returning None", key, kind.namespace) return callback(None) if 'deleted' in item and item['deleted']: log.debug( "Attempted to get deleted key %s in '%s', returning None", key, kind.namespace) return callback(None) return callback(item) finally: self._lock.runlock() def all(self, kind, callback): """ """ try: self._lock.rlock() itemsOfKind = self._items[kind] return callback( dict((k, i) for k, i in itemsOfKind.items() if ('deleted' not in i) or not i['deleted'])) finally: self._lock.runlock() def init(self, all_data): """ """ try: self._lock.rlock() self._items.clear() self._items.update(all_data) self._initialized = True for k in all_data: log.debug("Initialized '%s' store with %d items", k.namespace, len(all_data[k])) finally: self._lock.runlock() # noinspection PyShadowingNames def delete(self, kind, key, version): """ """ try: self._lock.rlock() itemsOfKind = self._items[kind] i = itemsOfKind.get(key) if i is None or i['version'] < version: i = {'deleted': True, 'version': version} itemsOfKind[key] = i finally: self._lock.runlock() def upsert(self, kind, item): """ """ key = item['key'] try: self._lock.rlock() itemsOfKind = self._items[kind] i = itemsOfKind.get(key) if i is None or i['version'] < item['version']: itemsOfKind[key] = item log.debug("Updated %s in '%s' to version %d", key, kind.namespace, item['version']) finally: self._lock.runlock() @property def initialized(self): """ """ try: self._lock.rlock() return self._initialized finally: self._lock.runlock() def describe_configuration(self, config): return 'memory'
class InMemoryFeatureStore(FeatureStore): def __init__(self): self._lock = ReadWriteLock() self._initialized = False self._features = {} def get(self, key): try: self._lock.rlock() f = self._features.get(key) if f is None or "deleted" in f and f["deleted"]: return None return f finally: self._lock.runlock() def all(self): try: self._lock.rlock() return 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 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] = f finally: self._lock.unlock() @property def initialized(self): try: self._lock.rlock() return self._initialized finally: self._lock.runlock()
class InMemoryFeatureStore(FeatureStore): def __init__(self): self._lock = ReadWriteLock() self._initialized = False self._features = {} def get(self, key): try: self._lock.rlock() f = self._features.get(key) if f is None or 'deleted' in f and f['deleted']: return None return f finally: self._lock.runlock() def all(self): try: self._lock.rlock() return 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 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] = f finally: self._lock.unlock() @property def initialized(self): try: self._lock.rlock() return self._initialized finally: self._lock.runlock()
class InMemoryFeatureStore(FeatureStore): """The default feature store implementation, which holds all data in a thread-safe data structure in memory. """ def __init__(self): """Constructs an instance of InMemoryFeatureStore. """ self._lock = ReadWriteLock() self._initialized = False self._items = defaultdict(dict) def get(self, kind, key, callback): """ """ try: self._lock.rlock() itemsOfKind = self._items[kind] item = itemsOfKind.get(key) if item is None: log.debug("Attempted to get missing key %s in '%s', returning None", key, kind.namespace) return callback(None) if 'deleted' in item and item['deleted']: log.debug("Attempted to get deleted key %s in '%s', returning None", key, kind.namespace) return callback(None) return callback(item) finally: self._lock.runlock() def all(self, kind, callback): """ """ try: self._lock.rlock() itemsOfKind = self._items[kind] return callback(dict((k, i) for k, i in itemsOfKind.items() if ('deleted' not in i) or not i['deleted'])) finally: self._lock.runlock() def init(self, all_data): """ """ try: self._lock.rlock() self._items.clear() self._items.update(all_data) self._initialized = True for k in all_data: log.debug("Initialized '%s' store with %d items", k.namespace, len(all_data[k])) finally: self._lock.runlock() # noinspection PyShadowingNames def delete(self, kind, key, version): """ """ try: self._lock.rlock() itemsOfKind = self._items[kind] i = itemsOfKind.get(key) if i is None or i['version'] < version: i = {'deleted': True, 'version': version} itemsOfKind[key] = i finally: self._lock.runlock() def upsert(self, kind, item): """ """ key = item['key'] try: self._lock.rlock() itemsOfKind = self._items[kind] i = itemsOfKind.get(key) if i is None or i['version'] < item['version']: itemsOfKind[key] = item log.debug("Updated %s in '%s' to version %d", key, kind.namespace, item['version']) finally: self._lock.runlock() @property def initialized(self): """ """ try: self._lock.rlock() return self._initialized finally: self._lock.runlock()