-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from launchdarkly/eb/ch29197/dependency-order
implement dependency ordering for feature store data
- Loading branch information
Showing
7 changed files
with
213 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require "ldclient-rb/interfaces" | ||
require "ldclient-rb/impl/store_data_set_sorter" | ||
|
||
module LaunchDarkly | ||
module Impl | ||
# | ||
# Provides additional behavior that the client requires before or after feature store operations. | ||
# Currently this just means sorting the data set for init(). In the future we may also use this | ||
# to provide an update listener capability. | ||
# | ||
class FeatureStoreClientWrapper | ||
include Interfaces::FeatureStore | ||
|
||
def initialize(store) | ||
@store = store | ||
end | ||
|
||
def init(all_data) | ||
@store.init(FeatureStoreDataSetSorter.sort_all_collections(all_data)) | ||
end | ||
|
||
def get(kind, key) | ||
@store.get(kind, key) | ||
end | ||
|
||
def all(kind) | ||
@store.all(kind) | ||
end | ||
|
||
def upsert(kind, item) | ||
@store.upsert(kind, item) | ||
end | ||
|
||
def delete(kind, key, version) | ||
@store.delete(kind, key, version) | ||
end | ||
|
||
def initialized? | ||
@store.initialized? | ||
end | ||
|
||
def stop | ||
@store.stop | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
|
||
module LaunchDarkly | ||
module Impl | ||
# | ||
# Implements a dependency graph ordering for data to be stored in a feature store. We must use this | ||
# on every data set that will be passed to the feature store's init() method. | ||
# | ||
class FeatureStoreDataSetSorter | ||
# | ||
# Returns a copy of the input hash that has the following guarantees: the iteration order of the outer | ||
# hash will be in ascending order by the VersionDataKind's :priority property (if any), and for each | ||
# data kind that has a :get_dependency_keys function, the inner hash will have an iteration order | ||
# where B is before A if A has a dependency on B. | ||
# | ||
# This implementation relies on the fact that hashes in Ruby have an iteration order that is the same | ||
# as the insertion order. Also, due to the way we deserialize JSON received from LaunchDarkly, the | ||
# keys in the inner hash will always be symbols. | ||
# | ||
def self.sort_all_collections(all_data) | ||
outer_hash = {} | ||
kinds = all_data.keys.sort_by { |k| | ||
k[:priority].nil? ? k[:namespace].length : k[:priority] # arbitrary order if priority is unknown | ||
} | ||
kinds.each do |kind| | ||
items = all_data[kind] | ||
outer_hash[kind] = self.sort_collection(kind, items) | ||
end | ||
outer_hash | ||
end | ||
|
||
def self.sort_collection(kind, input) | ||
dependency_fn = kind[:get_dependency_keys] | ||
return input if dependency_fn.nil? || input.empty? | ||
remaining_items = input.clone | ||
items_out = {} | ||
while !remaining_items.empty? | ||
# pick a random item that hasn't been updated yet | ||
key, item = remaining_items.first | ||
self.add_with_dependencies_first(item, dependency_fn, remaining_items, items_out) | ||
end | ||
items_out | ||
end | ||
|
||
def self.add_with_dependencies_first(item, dependency_fn, remaining_items, items_out) | ||
item_key = item[:key].to_sym | ||
remaining_items.delete(item_key) # we won't need to visit this item again | ||
dependency_fn.call(item).each do |dep_key| | ||
dep_item = remaining_items[dep_key.to_sym] | ||
self.add_with_dependencies_first(dep_item, dependency_fn, remaining_items, items_out) if !dep_item.nil? | ||
end | ||
items_out[item_key] = item | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters