Example #1
0
 def __init__(self):
     self._lock = ReadWriteLock()
     self._initialized = False
     self._items = defaultdict(dict)
Example #2
0
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()
Example #3
0
 def __init__(self):
     self._lock = ReadWriteLock()
     self._initialized = False
     self._features = {}
Example #4
0
from .util import log

__version__ = VERSION

__LONG_SCALE__ = float(0xFFFFFFFFFFFFFFF)

__BUILTINS__ = [
    "key", "ip", "country", "email", "firstName", "lastName", "avatar", "name",
    "anonymous"
]
"""Settings."""
start_wait = 5

__client = None
__config = Config()
__lock = ReadWriteLock()


def set_config(config):
    """Sets the configuration for the shared SDK client instance.

    If this is called prior to :func:`ldclient.get()`, it stores the configuration that will be used when the
    client is initialized. If it is called after the client has already been initialized, the client will be
    re-initialized with the new configuration (this will result in the next call to :func:`ldclient.get()`
    returning a new client instance).

    :param ldclient.config.Config config: the client configuration
    """
    global __config
    global __client
    global __lock
Example #5
0
 def __init__(self):
     """Constructs an instance of InMemoryFeatureStore.
     """
     self._lock = ReadWriteLock()
     self._initialized = False
     self._items = defaultdict(dict)
Example #6
0
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()
Example #7
0
 def __init__(self):
     self._lock = ReadWriteLock()
     self._initialized = False
     self._features = {}
Example #8
0
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'
Example #9
0
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()
Example #10
0
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()
 def __init__(self):
     """Constructs an instance of InMemoryFeatureStore.
     """
     self._lock = ReadWriteLock()
     self._initialized = False
     self._items = defaultdict(dict)
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()
Example #13
0
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()