Skip to content

RBS Storage provider #742

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 12 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,8 @@ public enum MatcherType {
@SerializedName("BETWEEN_SEMVER")
BETWEEN_SEMVER,
@SerializedName("IN_LIST_SEMVER")
IN_LIST_SEMVER
IN_LIST_SEMVER,

@SerializedName("IN_RULE_BASED_SEGMENT")
IN_RULE_BASED_SEGMENT,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.split.android.client.localhost;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.Set;

import io.split.android.client.dtos.RuleBasedSegment;
import io.split.android.client.storage.rbs.RuleBasedSegmentStorage;

public class LocalhostRuleBasedSegmentsStorage implements RuleBasedSegmentStorage {

@Nullable
@Override
public RuleBasedSegment get(String segmentName) {
return null;
}

@Override
public boolean update(@NonNull Set<RuleBasedSegment> toAdd, @NonNull Set<RuleBasedSegment> toRemove, long changeNumber) {
return false;
}

@Override
public long getChangeNumber() {
return -1;
}

@Override
public boolean contains(@NonNull Set<String> segmentNames) {
return false;
}

@Override
public void loadLocal() {
// no-op
}

@Override
public void clear() {
// no-op
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.split.android.client.localhost;

import androidx.annotation.NonNull;

import io.split.android.client.storage.rbs.RuleBasedSegmentStorage;
import io.split.android.client.storage.rbs.RuleBasedSegmentStorageProvider;

class LocalhostRuleBasedSegmentsStorageProvider implements RuleBasedSegmentStorageProvider {

private final RuleBasedSegmentStorage mRuleBasedSegmentStorage;

LocalhostRuleBasedSegmentsStorageProvider(@NonNull RuleBasedSegmentStorage ruleBasedSegmentStorage) {
mRuleBasedSegmentStorage = ruleBasedSegmentStorage;
}

@Override
public RuleBasedSegmentStorage get() {
return mRuleBasedSegmentStorage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
import io.split.android.client.storage.mysegments.MySegmentsStorageContainer;
import io.split.android.client.storage.mysegments.MySegmentsStorageContainerImpl;
import io.split.android.client.storage.mysegments.SqLitePersistentMySegmentsStorage;
import io.split.android.client.storage.rbs.LazyRuleBasedSegmentStorageProvider;
import io.split.android.client.storage.rbs.PersistentRuleBasedSegmentStorage;
import io.split.android.client.storage.rbs.RuleBasedSegmentStorageProvider;
import io.split.android.client.storage.rbs.SqLitePersistentRuleBasedSegmentStorageProvider;
import io.split.android.client.storage.splits.PersistentSplitsStorage;
import io.split.android.client.storage.splits.SplitsStorage;
import io.split.android.client.storage.splits.SplitsStorageImpl;
Expand Down Expand Up @@ -154,4 +158,12 @@ public static PersistentImpressionsObserverCacheStorage getImpressionsObserverCa
public static GeneralInfoStorage getGeneralInfoStorage(SplitRoomDatabase splitRoomDatabase) {
return new GeneralInfoStorageImpl(splitRoomDatabase.generalInfoDao());
}

public static PersistentRuleBasedSegmentStorage getPersistentRuleBasedSegmentStorage(SplitRoomDatabase splitRoomDatabase, SplitCipher splitCipher, GeneralInfoStorage generalInfoStorage) {
return new SqLitePersistentRuleBasedSegmentStorageProvider(splitCipher, splitRoomDatabase, generalInfoStorage).get();
}

public static RuleBasedSegmentStorageProvider getRuleBasedSegmentStorageProvider() {
return new LazyRuleBasedSegmentStorageProvider();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.split.android.client.storage.rbs;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.concurrent.atomic.AtomicReference;

import io.split.android.client.utils.logger.Logger;

public class LazyRuleBasedSegmentStorageProvider implements RuleBasedSegmentStorageProvider {

private final AtomicReference<RuleBasedSegmentStorage> mRuleBasedSegmentStorageRef = new AtomicReference<>();

public void set(@NonNull RuleBasedSegmentStorage ruleBasedSegmentStorage) {
if (!mRuleBasedSegmentStorageRef.compareAndSet(null, ruleBasedSegmentStorage)) {
Logger.w("RuleBasedSegmentStorage already set in LazyRuleBasedSegmentStorageProvider");
}
}

@Nullable
@Override
public RuleBasedSegmentStorage get() {
return mRuleBasedSegmentStorageRef.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.split.android.client.storage.rbs;

import androidx.annotation.Nullable;

public interface RuleBasedSegmentStorageProvider {

@Nullable
RuleBasedSegmentStorage get();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.split.android.client.storage.rbs;

import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.mock;

import org.junit.Test;

public class LazyRuleBasedSegmentStorageProviderTest {

@Test
public void refCanOnlyBeSetOnce() {
LazyRuleBasedSegmentStorageProvider provider = new LazyRuleBasedSegmentStorageProvider();
RuleBasedSegmentStorage firstInstance = mock(RuleBasedSegmentStorage.class);
RuleBasedSegmentStorage secondInstance = mock(RuleBasedSegmentStorage.class);
provider.set(firstInstance);
provider.set(secondInstance);

assertSame(firstInstance, provider.get());
assertNotSame(secondInstance, provider.get());
}

@Test
public void getReturnsNullWhenSetHasNotBeenCalled() {
LazyRuleBasedSegmentStorageProvider provider = new LazyRuleBasedSegmentStorageProvider();
assertNull(provider.get());
}
}
Loading