Skip to content
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
4 changes: 2 additions & 2 deletions client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
<parent>
<groupId>io.split.client</groupId>
<artifactId>java-client-parent</artifactId>
<version>4.3.0</version>
<version>4.4.0-alpha</version>
</parent>
<artifactId>java-client</artifactId>
<packaging>jar</packaging>
<name>Java Client</name>
<description>Java SDK for Split</description>

<properties>
<pluggable.storage>1.0.0</pluggable.storage>
<pluggable.storage>1.0.0-alpha</pluggable.storage>
</properties>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,17 @@ public Collection<ParsedSplit> getAll() {
@Override
public boolean trafficTypeExists(String trafficTypeName) {
String wrapperResponse = _safeUserStorageWrapper.get(PrefixAdapter.buildTrafficTypeExists(trafficTypeName));
boolean response = false;
if(wrapperResponse == null) {
return response;
return false;
}
try {
response = Json.fromJson(wrapperResponse, Boolean.class);
return response;
Long value = Json.fromJson(wrapperResponse, Long.class);
return value != null && value > 0;
}
catch(Exception e) {
_log.info("Error getting boolean from String.");
}
return response;
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void testGetAllNullOnGetMany() {
@Test
public void testTrafficTypeExists() {
Mockito.when(_safeUserStorageWrapper.get(PrefixAdapter.buildTrafficTypeExists("TrafficType"))).
thenReturn(getBooleanAsJson(true));
thenReturn(getLongAsJson(2));
boolean result = _userCustomSplitAdapterConsumer.trafficTypeExists("TrafficType");
Assert.assertTrue(result);
}
Expand All @@ -146,7 +146,7 @@ public void testTrafficTypeExistsWithWrapperFailing() {
@Test
public void testTrafficTypeExistsWithGsonFailing() {
Mockito.when(_safeUserStorageWrapper.get(PrefixAdapter.buildTrafficTypeExists("TrafficType"))).
thenReturn("2");
thenReturn("true");
boolean result = _userCustomSplitAdapterConsumer.trafficTypeExists("TrafficType");
Assert.assertFalse(result);
}
Expand Down
4 changes: 2 additions & 2 deletions pluggable-storage/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
<parent>
<artifactId>java-client-parent</artifactId>
<groupId>io.split.client</groupId>
<version>4.3.0</version>
<version>4.4.0-alpha</version>
</parent>

<version>1.0.0</version>
<version>1.0.0-alpha</version>
<artifactId>pluggable-storage</artifactId>
<packaging>jar</packaging>
<name>Package for Pluggable Storage</name>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.split.client</groupId>
<artifactId>java-client-parent</artifactId>
<version>4.3.0</version>
<version>4.4.0-alpha</version>
<dependencyManagement>
<dependencies>
<dependency>
Expand Down
12 changes: 9 additions & 3 deletions redis-wrapper/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<parent>
<artifactId>java-client-parent</artifactId>
<groupId>io.split.client</groupId>
<version>4.3.0</version>
<version>4.4.0-alpha</version>
</parent>

<artifactId>redis-wrapper</artifactId>
<version>1.0.0</version>
<version>1.0.0-alpha</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<pluggable.storage>1.0.0</pluggable.storage>
<pluggable.storage>1.0.0-alpha</pluggable.storage>
</properties>
<dependencies>
<dependency>
Expand All @@ -28,6 +28,12 @@
<artifactId>jedis</artifactId>
<version>3.7.0</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>


Expand Down
179 changes: 137 additions & 42 deletions redis-wrapper/src/main/java/redis/RedisImp.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,106 +7,201 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class RedisImp implements CustomStorageWrapper {
class RedisImp implements CustomStorageWrapper {
private static final String TELEMETRY_INIT = "SPLITIO.telemetry.init" ;

private static final String SPLIT_KEY = "SPLITIO.split.*";
private final JedisPool redisPool;
private Jedis jedis;
private String _prefix;
private final JedisPool jedisPool;
private final String prefix;

public RedisImp(JedisPool jedisPool, String prefix) {
redisPool = jedisPool;
jedis = redisPool.getResource();
_prefix = prefix;
this.jedisPool = jedisPool;
this.prefix = prefix;
}

@Override
public String get(String s) throws Exception {
return null;
public String get(String key) throws Exception {
try (Jedis jedis = this.jedisPool.getResource()) {
return jedis.get(buildKeyWithPrefix(key));
} catch (Exception ex) {
throw new Exception(ex);
}
}

@Override
public List<String> getMany(List<String> list) throws Exception {
List<String> keys = new ArrayList<>(this.getKeysByPrefix(""));
List<String> items = jedis.mget(keys.toArray(new String[keys.size()]));
return items;
public List<String> getMany(List<String> keys) throws Exception {
if(keys == null || keys.isEmpty()){
return new ArrayList<>();
}
try (Jedis jedis = this.jedisPool.getResource()) {
keys = keys.stream().map(key -> buildKeyWithPrefix(key)).collect(Collectors.toList());

return jedis.mget(keys.toArray(new String[keys.size()]));
} catch (Exception ex) {
throw new Exception(ex);
}
}

@Override
public void set(String s, String s1) throws Exception {

public void set(String key, String item) throws Exception {
try (Jedis jedis = this.jedisPool.getResource()) {
if(key.contains(TELEMETRY_INIT)) {
String[] splittedKey = key.split("::");
jedis.hset(buildKeyWithPrefix(splittedKey[0]), splittedKey[1], item);
return;
}
jedis.set(buildKeyWithPrefix(key), item);
} catch (Exception ex) {
throw new Exception(ex);
}
}

@Override
public void delete(List<String> list) throws Exception {
public void delete(List<String> keys) throws Exception {
if(keys == null || keys.isEmpty()){
return ;
}
try (Jedis jedis = this.jedisPool.getResource()) {
keys = keys.stream().map(key -> buildKeyWithPrefix(key)).collect(Collectors.toList());

jedis.del(keys.toArray(new String[keys.size()]));
} catch (Exception ex) {
throw new Exception(ex);
}
}

@Override
public String getAndSet(String s, String s1) throws Exception {
return null;
public String getAndSet(String key, String item) throws Exception {
try (Jedis jedis = this.jedisPool.getResource()) {
return jedis.getSet(buildKeyWithPrefix(key), item);
} catch (Exception ex) {
throw new Exception(ex);
}
}

@Override
public Set<String> getKeysByPrefix(String s) throws Exception {
return jedis.keys(_prefix + SPLIT_KEY);
public Set<String> getKeysByPrefix(String prefix) throws Exception {
try (Jedis jedis = this.jedisPool.getResource()) {
return jedis.keys(buildKeyWithPrefix(prefix));
} catch (Exception ex) {
throw new Exception(ex);
}
}

@Override
public long increment(String s, long l) throws Exception {
return 0;
public long increment(String key, long value) throws Exception {
try (Jedis jedis = this.jedisPool.getResource()) {
return jedis.incrBy(buildKeyWithPrefix(key), value);
} catch (Exception ex) {
throw new Exception(ex);
}
}

@Override
public long decrement(String s, long l) throws Exception {
return 0;
public long decrement(String key, long value) throws Exception {
try (Jedis jedis = this.jedisPool.getResource()) {
return jedis.decrBy(buildKeyWithPrefix(key), value);
} catch (Exception ex) {
throw new Exception(ex);
}
}

@Override
public void pushItems(String s, List<String> list) throws Exception {

public void pushItems(String key, List<String> items) throws Exception {
try (Jedis jedis = this.jedisPool.getResource()) {
jedis.rpush(buildKeyWithPrefix(key), items.toArray(new String[items.size()]));
} catch (Exception ex) {
throw new Exception(ex);
}
}

@Override
public List<String> popItems(String s, long l) throws Exception {
return null;
public List<String> popItems(String key, long count) throws Exception {
try (Jedis jedis = this.jedisPool.getResource()) {
return jedis.rpop(buildKeyWithPrefix(key), (int)count);
} catch (Exception ex) {
throw new Exception(ex);
}
}

// Return length of redis set.
@Override
public long getItemsCount(String s) throws Exception {
return 0;
public long getItemsCount(String key) throws Exception {
try (Jedis jedis = this.jedisPool.getResource()) {
return jedis.scard(buildKeyWithPrefix(key));
} catch (Exception ex) {
throw new Exception(ex);
}
}

@Override
public boolean itemContains(String s, String s1) throws Exception {
return false;
public boolean itemContains(String key, String item) throws Exception {
try (Jedis jedis = this.jedisPool.getResource()) {
return jedis.sismember(buildKeyWithPrefix(key), item);
} catch (Exception ex) {
throw new Exception(ex);
}
}

@Override
public void addItems(String s, List<String> list) throws Exception {

public void addItems(String key, List<String> items) throws Exception {
try (Jedis jedis = this.jedisPool.getResource()) {
jedis.sadd(buildKeyWithPrefix(key), items.toArray(new String[items.size()]));
} catch (Exception ex) {
throw new Exception(ex);
}
}

@Override
public void removeItems(String s, List<String> list) throws Exception {

public void removeItems(String key, List<String> items) throws Exception {
try (Jedis jedis = this.jedisPool.getResource()) {
jedis.srem(buildKeyWithPrefix(key), items.toArray(new String[items.size()]));
} catch (Exception ex) {
throw new Exception(ex);
}
}

@Override
public List<String> getItems(List<String> list) throws Exception {
return null;
public List<String> getItems(List<String> keys) throws Exception {
if(keys == null || keys.isEmpty()){
return new ArrayList<>();
}
try (Jedis jedis = this.jedisPool.getResource()) {
keys = keys.stream().map(key -> buildKeyWithPrefix(key)).collect(Collectors.toList());

return jedis.mget(keys.toArray(new String[keys.size()]));
} catch (Exception ex) {
throw new Exception(ex);
}
}

@Override
public boolean connect() throws Exception {
String ping = jedis.ping();
return "PONG".equalsIgnoreCase(ping);
try (Jedis jedis = this.jedisPool.getResource()) {
return "PONG".equalsIgnoreCase(jedis.ping());
} catch (Exception ex) {
throw new Exception(ex);
}
}

@Override
public boolean close() throws Exception {
return false;
try {
jedisPool.close();

return true;
} catch (Exception ex) {
throw new Exception(ex);
}
}

private String buildKeyWithPrefix(String key) {
if (!key.startsWith(this.prefix)) {
key = String.format("%s.%s", prefix, key);
}

return key;
}
}

Loading