-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add precomputed configuration storage #239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8a45491
feat: add obfuscation utilities and precomputed DTOs
leoromanovsky a933cd2
perf: optimize MD5 hex conversion with lookup table
leoromanovsky 5970e9f
perf: add md5HexPrefix for efficient partial hash
leoromanovsky 5d2f360
refactor: extract BaseCacheFile for reuse
leoromanovsky 5ff6524
feat: add precomputed configuration storage
leoromanovsky dd5309f
style: apply spotless formatting
leoromanovsky 4741881
fix: address PR #239 feedback
leoromanovsky 75dcdc4
Merge branch 'main' into precomputed/3-storage-layer
leoromanovsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
15 changes: 15 additions & 0 deletions
15
eppo/src/main/java/cloud/eppo/android/PrecomputedCacheFile.java
This file contains hidden or 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,15 @@ | ||
| package cloud.eppo.android; | ||
|
|
||
| import android.app.Application; | ||
|
|
||
| /** Disk cache file for precomputed configuration. */ | ||
| public class PrecomputedCacheFile extends BaseCacheFile { | ||
|
|
||
| public PrecomputedCacheFile(Application application, String fileNameSuffix) { | ||
| super(application, cacheFileName(fileNameSuffix)); | ||
| } | ||
|
|
||
| public static String cacheFileName(String suffix) { | ||
| return "eppo-sdk-precomputed-" + suffix + ".json"; | ||
| } | ||
| } |
138 changes: 138 additions & 0 deletions
138
eppo/src/main/java/cloud/eppo/android/PrecomputedConfigurationStore.java
This file contains hidden or 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,138 @@ | ||
| package cloud.eppo.android; | ||
|
|
||
| import static cloud.eppo.android.util.Utils.logTag; | ||
|
|
||
| import android.app.Application; | ||
| import android.util.Log; | ||
| import androidx.annotation.NonNull; | ||
| import androidx.annotation.Nullable; | ||
| import cloud.eppo.android.dto.PrecomputedBandit; | ||
| import cloud.eppo.android.dto.PrecomputedConfigurationResponse; | ||
| import cloud.eppo.android.dto.PrecomputedFlag; | ||
| import cloud.eppo.android.util.Utils; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.util.Map; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| /** Storage for precomputed flags/bandits with disk caching. */ | ||
| public class PrecomputedConfigurationStore { | ||
|
|
||
| private static final String TAG = logTag(PrecomputedConfigurationStore.class); | ||
| private final PrecomputedCacheFile cacheFile; | ||
| private final Object cacheLock = new Object(); | ||
|
|
||
| private volatile PrecomputedConfigurationResponse configuration = | ||
| PrecomputedConfigurationResponse.empty(); | ||
| private final Object cacheLoadLock = new Object(); | ||
| private CompletableFuture<PrecomputedConfigurationResponse> cacheLoadFuture = null; | ||
|
|
||
| public PrecomputedConfigurationStore(Application application, String cacheFileNameSuffix) { | ||
| cacheFile = new PrecomputedCacheFile(application, cacheFileNameSuffix); | ||
| } | ||
|
|
||
| /** Returns the current configuration. */ | ||
| @NonNull public PrecomputedConfigurationResponse getConfiguration() { | ||
| return configuration; | ||
| } | ||
|
|
||
| /** Returns the salt from the current configuration, or null if not set. */ | ||
| @Nullable public String getSalt() { | ||
| String salt = configuration.getSalt(); | ||
| return (salt != null && !salt.isEmpty()) ? salt : null; | ||
| } | ||
|
|
||
| /** Returns the format from the current configuration, or null if not set. */ | ||
| @Nullable public String getFormat() { | ||
| String format = configuration.getFormat(); | ||
| return (format != null && !format.isEmpty()) ? format : null; | ||
| } | ||
|
|
||
| /** Returns a flag by its MD5-hashed key, or null if not found. */ | ||
| @Nullable public PrecomputedFlag getFlag(String hashedKey) { | ||
| return configuration.getFlags().get(hashedKey); | ||
| } | ||
|
|
||
| /** Returns a bandit by its MD5-hashed key, or null if not found. */ | ||
| @Nullable public PrecomputedBandit getBandit(String hashedKey) { | ||
| return configuration.getBandits().get(hashedKey); | ||
| } | ||
|
|
||
| /** Returns the flags map. */ | ||
| @NonNull public Map<String, PrecomputedFlag> getFlags() { | ||
| return configuration.getFlags(); | ||
| } | ||
|
|
||
| /** Returns the bandits map. */ | ||
| @NonNull public Map<String, PrecomputedBandit> getBandits() { | ||
| return configuration.getBandits(); | ||
| } | ||
|
|
||
| /** Updates the configuration with a new response. */ | ||
| public void setConfiguration(@NonNull PrecomputedConfigurationResponse newConfiguration) { | ||
| this.configuration = newConfiguration; | ||
| } | ||
|
|
||
| /** Loads configuration from the cache file asynchronously. */ | ||
| public CompletableFuture<PrecomputedConfigurationResponse> loadConfigFromCache() { | ||
| synchronized (cacheLoadLock) { | ||
| if (cacheLoadFuture != null) { | ||
| return cacheLoadFuture; | ||
| } | ||
| if (!cacheFile.exists()) { | ||
| Log.d(TAG, "Not loading from cache (file does not exist)"); | ||
| return CompletableFuture.completedFuture(null); | ||
| } | ||
| return cacheLoadFuture = | ||
| CompletableFuture.supplyAsync( | ||
| () -> { | ||
| Log.d(TAG, "Loading precomputed config from cache"); | ||
| return readCacheFile(); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| /** Reads the cache file and returns the configuration, or null if reading fails. */ | ||
| @Nullable protected PrecomputedConfigurationResponse readCacheFile() { | ||
| synchronized (cacheLock) { | ||
| try (InputStream inputStream = cacheFile.getInputStream()) { | ||
| Log.d(TAG, "Attempting to inflate precomputed config"); | ||
| byte[] bytes = Utils.toByteArray(inputStream); | ||
| PrecomputedConfigurationResponse config = PrecomputedConfigurationResponse.fromBytes(bytes); | ||
| Log.d(TAG, "Precomputed cache load complete"); | ||
| return config; | ||
| } catch (IOException e) { | ||
| Log.e(TAG, "Error loading precomputed config from the cache: " + e.getMessage()); | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** Saves the configuration to the cache file asynchronously. */ | ||
| public CompletableFuture<Void> saveConfiguration( | ||
| @NonNull PrecomputedConfigurationResponse newConfiguration) { | ||
| return CompletableFuture.supplyAsync( | ||
| () -> { | ||
| synchronized (cacheLock) { | ||
| // Always update in-memory configuration, even if disk write fails | ||
| this.configuration = newConfiguration; | ||
|
|
||
| Log.d(TAG, "Saving precomputed configuration to cache file"); | ||
| try (OutputStream outputStream = cacheFile.getOutputStream()) { | ||
| outputStream.write(newConfiguration.toBytes()); | ||
| Log.d(TAG, "Updated precomputed cache file"); | ||
| } catch (IOException e) { | ||
| Log.e(TAG, "Unable to write precomputed config to file (in-memory updated)", e); | ||
| // Don't throw - in-memory config is already updated | ||
| } | ||
| return null; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /** Deletes the cache file. */ | ||
| public void deleteCache() { | ||
| cacheFile.delete(); | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing to fix here, but just noting that there's a race condition due to a sync check and async read. You've handled it by ensuring that exceptions are caught so it's not dangerous.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea absolutely thanks for mentioning; I'm being kind of lazy and copying the same pattern as exists in the local-eval configuration store.