Skip to content

Commit 75ce574

Browse files
authored
RBS Storage provider (#742)
1 parent 73d3044 commit 75ce574

File tree

7 files changed

+142
-1
lines changed

7 files changed

+142
-1
lines changed

src/main/java/io/split/android/client/dtos/MatcherType.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,8 @@ public enum MatcherType {
6060
@SerializedName("BETWEEN_SEMVER")
6161
BETWEEN_SEMVER,
6262
@SerializedName("IN_LIST_SEMVER")
63-
IN_LIST_SEMVER
63+
IN_LIST_SEMVER,
64+
65+
@SerializedName("IN_RULE_BASED_SEGMENT")
66+
IN_RULE_BASED_SEGMENT,
6467
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.split.android.client.localhost;
2+
3+
import androidx.annotation.NonNull;
4+
import androidx.annotation.Nullable;
5+
6+
import java.util.Set;
7+
8+
import io.split.android.client.dtos.RuleBasedSegment;
9+
import io.split.android.client.storage.rbs.RuleBasedSegmentStorage;
10+
11+
public class LocalhostRuleBasedSegmentsStorage implements RuleBasedSegmentStorage {
12+
13+
@Nullable
14+
@Override
15+
public RuleBasedSegment get(String segmentName) {
16+
return null;
17+
}
18+
19+
@Override
20+
public boolean update(@NonNull Set<RuleBasedSegment> toAdd, @NonNull Set<RuleBasedSegment> toRemove, long changeNumber) {
21+
return false;
22+
}
23+
24+
@Override
25+
public long getChangeNumber() {
26+
return -1;
27+
}
28+
29+
@Override
30+
public boolean contains(@NonNull Set<String> segmentNames) {
31+
return false;
32+
}
33+
34+
@Override
35+
public void loadLocal() {
36+
// no-op
37+
}
38+
39+
@Override
40+
public void clear() {
41+
// no-op
42+
}
43+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.split.android.client.localhost;
2+
3+
import androidx.annotation.NonNull;
4+
5+
import io.split.android.client.storage.rbs.RuleBasedSegmentStorage;
6+
import io.split.android.client.storage.rbs.RuleBasedSegmentStorageProvider;
7+
8+
class LocalhostRuleBasedSegmentsStorageProvider implements RuleBasedSegmentStorageProvider {
9+
10+
private final RuleBasedSegmentStorage mRuleBasedSegmentStorage;
11+
12+
LocalhostRuleBasedSegmentsStorageProvider(@NonNull RuleBasedSegmentStorage ruleBasedSegmentStorage) {
13+
mRuleBasedSegmentStorage = ruleBasedSegmentStorage;
14+
}
15+
16+
@Override
17+
public RuleBasedSegmentStorage get() {
18+
return mRuleBasedSegmentStorage;
19+
}
20+
}

src/main/java/io/split/android/client/storage/db/StorageFactory.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
import io.split.android.client.storage.mysegments.MySegmentsStorageContainer;
3131
import io.split.android.client.storage.mysegments.MySegmentsStorageContainerImpl;
3232
import io.split.android.client.storage.mysegments.SqLitePersistentMySegmentsStorage;
33+
import io.split.android.client.storage.rbs.LazyRuleBasedSegmentStorageProvider;
34+
import io.split.android.client.storage.rbs.PersistentRuleBasedSegmentStorage;
35+
import io.split.android.client.storage.rbs.RuleBasedSegmentStorageProvider;
36+
import io.split.android.client.storage.rbs.SqLitePersistentRuleBasedSegmentStorageProvider;
3337
import io.split.android.client.storage.splits.PersistentSplitsStorage;
3438
import io.split.android.client.storage.splits.SplitsStorage;
3539
import io.split.android.client.storage.splits.SplitsStorageImpl;
@@ -154,4 +158,12 @@ public static PersistentImpressionsObserverCacheStorage getImpressionsObserverCa
154158
public static GeneralInfoStorage getGeneralInfoStorage(SplitRoomDatabase splitRoomDatabase) {
155159
return new GeneralInfoStorageImpl(splitRoomDatabase.generalInfoDao());
156160
}
161+
162+
public static PersistentRuleBasedSegmentStorage getPersistentRuleBasedSegmentStorage(SplitRoomDatabase splitRoomDatabase, SplitCipher splitCipher, GeneralInfoStorage generalInfoStorage) {
163+
return new SqLitePersistentRuleBasedSegmentStorageProvider(splitCipher, splitRoomDatabase, generalInfoStorage).get();
164+
}
165+
166+
public static RuleBasedSegmentStorageProvider getRuleBasedSegmentStorageProvider() {
167+
return new LazyRuleBasedSegmentStorageProvider();
168+
}
157169
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.split.android.client.storage.rbs;
2+
3+
import androidx.annotation.NonNull;
4+
import androidx.annotation.Nullable;
5+
6+
import java.util.concurrent.atomic.AtomicReference;
7+
8+
import io.split.android.client.utils.logger.Logger;
9+
10+
public class LazyRuleBasedSegmentStorageProvider implements RuleBasedSegmentStorageProvider {
11+
12+
private final AtomicReference<RuleBasedSegmentStorage> mRuleBasedSegmentStorageRef = new AtomicReference<>();
13+
14+
public void set(@NonNull RuleBasedSegmentStorage ruleBasedSegmentStorage) {
15+
if (!mRuleBasedSegmentStorageRef.compareAndSet(null, ruleBasedSegmentStorage)) {
16+
Logger.w("RuleBasedSegmentStorage already set in LazyRuleBasedSegmentStorageProvider");
17+
}
18+
}
19+
20+
@Nullable
21+
@Override
22+
public RuleBasedSegmentStorage get() {
23+
return mRuleBasedSegmentStorageRef.get();
24+
}
25+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.split.android.client.storage.rbs;
2+
3+
import androidx.annotation.Nullable;
4+
5+
public interface RuleBasedSegmentStorageProvider {
6+
7+
@Nullable
8+
RuleBasedSegmentStorage get();
9+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.split.android.client.storage.rbs;
2+
3+
import static org.junit.Assert.assertNotSame;
4+
import static org.junit.Assert.assertNull;
5+
import static org.junit.Assert.assertSame;
6+
import static org.mockito.Mockito.mock;
7+
8+
import org.junit.Test;
9+
10+
public class LazyRuleBasedSegmentStorageProviderTest {
11+
12+
@Test
13+
public void refCanOnlyBeSetOnce() {
14+
LazyRuleBasedSegmentStorageProvider provider = new LazyRuleBasedSegmentStorageProvider();
15+
RuleBasedSegmentStorage firstInstance = mock(RuleBasedSegmentStorage.class);
16+
RuleBasedSegmentStorage secondInstance = mock(RuleBasedSegmentStorage.class);
17+
provider.set(firstInstance);
18+
provider.set(secondInstance);
19+
20+
assertSame(firstInstance, provider.get());
21+
assertNotSame(secondInstance, provider.get());
22+
}
23+
24+
@Test
25+
public void getReturnsNullWhenSetHasNotBeenCalled() {
26+
LazyRuleBasedSegmentStorageProvider provider = new LazyRuleBasedSegmentStorageProvider();
27+
assertNull(provider.get());
28+
}
29+
}

0 commit comments

Comments
 (0)