Module: LaunchDarkly::Interfaces::FeatureStore

Overview

Mixin that defines the required methods of a feature store implementation. The LaunchDarkly client uses the feature store to persist feature flags and related objects received from the LaunchDarkly service. Implementations must support concurrent access and updates. For more about how feature stores can be used, see: Using a persistent feature store.

An entity that can be stored in a feature store is a hash that can be converted to and from JSON, and that has at a minimum the following properties: :key, a string that is unique among entities of the same kind; :version, an integer that is higher for newer data; :deleted, a boolean (optional, defaults to false) that if true means this is a placeholder for a deleted entity.

To represent the different kinds of objects that can be stored, such as feature flags and segments, the SDK will provide a "kind" object; this is a hash with a single property, :namespace, which is a short string unique to that kind. This string can be used as a collection name or a key prefix.

The default implementation is LaunchDarkly::InMemoryFeatureStore. Several implementations that use databases can be found in LaunchDarkly::Integrations. If you want to write a new implementation, see LaunchDarkly::Integrations::Util for tools that can make this task simpler.

Instance Method Summary collapse

Instance Method Details

#all(kind) ⇒ Hash

Returns all stored entities of the specified kind, not including deleted entities.

Parameters:

  • kind (Object)

    the kind of entity to get

Returns:

  • (Hash)

    a hash where each key is the entity's :key property and each value is the entity



67
68
# File 'lib/ldclient-rb/interfaces.rb', line 67

def all(kind)
end

#delete(kind, key, version) ⇒ void

This method returns an undefined value.

Attempt to delete an entity if it exists. Deletion should only succeed if the version parameter is greater than the existing entity's :version; otherwise, the method should do nothing.

Parameters:

  • kind (Object)

    the kind of entity to delete

  • key (String)

    the unique key of the entity

  • version (Integer)

    the entity must have a lower version than this to be deleted



92
93
# File 'lib/ldclient-rb/interfaces.rb', line 92

def delete(kind, key, version)
end

#get(kind, key) ⇒ Hash

Returns the entity to which the specified key is mapped, if any.

Parameters:

  • kind (Object)

    the kind of entity to get

  • key (String)

    the unique key of the entity to get

Returns:

  • (Hash)

    the entity; nil if the key was not found, or if the stored entity's :deleted property was true



57
58
# File 'lib/ldclient-rb/interfaces.rb', line 57

def get(kind, key)
end

#init(all_data) ⇒ void

This method returns an undefined value.

Initializes (or re-initializes) the store with the specified set of entities. Any existing entries will be removed. Implementations can assume that this data set is up to date-- there is no need to perform individual version comparisons between the existing objects and the supplied features.

If possible, the store should update the entire data set atomically. If that is not possible, it should iterate through the outer hash and then the inner hash using the existing iteration order of those hashes (the SDK will ensure that the items were inserted into the hashes in the correct order), storing each item, and then delete any leftover items at the very end.

Parameters:

  • all_data (Hash)

    a hash where each key is one of the data kind objects, and each value is in turn a hash of string keys to entities



46
47
# File 'lib/ldclient-rb/interfaces.rb', line 46

def init(all_data)
end

#initialized?Boolean

Checks whether this store has been initialized. That means that init has been called either by this process, or (if the store can be shared) by another process. This method will be called frequently, so it should be efficient. You can assume that if it has returned true once, it can continue to return true, i.e. a store cannot become uninitialized again.

Returns:

  • (Boolean)

    true if the store is in an initialized state



104
105
# File 'lib/ldclient-rb/interfaces.rb', line 104

def initialized?
end

#stopvoid

This method returns an undefined value.

Performs any necessary cleanup to shut down the store when the client is being shut down.



112
113
# File 'lib/ldclient-rb/interfaces.rb', line 112

def stop
end

#upsert(kind, item) ⇒ void

This method returns an undefined value.

Attempt to add an entity, or update an existing entity with the same key. An update should only succeed if the new item's :version is greater than the old one; otherwise, the method should do nothing.

Parameters:

  • kind (Object)

    the kind of entity to add or update

  • item (Hash)

    the entity to add or update



79
80
# File 'lib/ldclient-rb/interfaces.rb', line 79

def upsert(kind, item)
end