Skip to content
Open
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
23 changes: 23 additions & 0 deletions src/main/java/org/redis/JedisConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.redis;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisConfig {
private final JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
private final JedisPool jedisPool;

public JedisConfig() {
jedisPool = new JedisPool(jedisPoolConfig, "localhost", 6379);
}

public JedisPoolConfig getJedisPoolConfig() {
return jedisPoolConfig;
}


public JedisPool getJedisPool() {
return jedisPool;
}

}
4 changes: 3 additions & 1 deletion src/main/java/org/redis/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
MapRedisImpl<String, String> mapRedis = new MapRedisImpl<>(String.class, String.class);
mapRedis.put("Key", "Value");
System.out.println(mapRedis.containsKey("Key"));
}
}
176 changes: 176 additions & 0 deletions src/main/java/org/redis/MapRedisImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package org.redis;

import com.google.gson.Gson;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.ScanParams;
import redis.clients.jedis.resps.ScanResult;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class MapRedisImpl <K,V> implements Map<K,V> {
private final JedisConfig jedisConfig;
private final JedisPool jedisPool;
private final Gson gson;
private final Class<V> valueType;
private final Class<K> keyType;
private String cursor = ScanParams.SCAN_POINTER_START;
private final ScanParams scanParams = new ScanParams().count(100);

public MapRedisImpl(Class<K> keyType, Class<V> valueType) {
jedisConfig = new JedisConfig();
jedisPool = jedisConfig.getJedisPool();
gson = new Gson();
this.valueType = valueType;
this.keyType = keyType;
}

private Jedis getJedis() {
return jedisPool.getResource();
}

@Override
public int size() {
try (Jedis jedis = getJedis()) {
return (int)jedis.dbSize();
}
}

@Override
public boolean isEmpty() {
try (Jedis jedis = getJedis()) {
return jedis.dbSize() == 0;
}
}

@Override
public boolean containsKey(Object key) {
try (Jedis jedis = getJedis()) {
return jedis.exists(gson.toJson(key));
}
}

@Override
public boolean containsValue(Object value) {
try (Jedis jedis = getJedis()) {
do {
ScanResult<String> scanResult = jedis.scan(cursor, scanParams);
List<String> keys = scanResult.getResult();
for (String key : keys) {
String valueRedis = jedis.get(key);
if (valueRedis != null && gson.fromJson(valueRedis, valueType).equals(value)) {
return true;
}
}
cursor = scanResult.getCursor();
} while (!cursor.equals(ScanParams.SCAN_POINTER_START));
}
return false;
}

@Override
public V get(Object key) {
try(Jedis jedis = getJedis()) {
String value = jedis.get(gson.toJson(key));
return value == null? null: gson.fromJson(value, valueType);
}
}

@Override
public V put(K key, V value) {
try(Jedis jedis = getJedis()) {
jedis.set(gson.toJson(key), gson.toJson(value));
return gson.fromJson(jedis.get(gson.toJson(key)), valueType);
}
}

@Override
public V remove(Object key) {
try(Jedis jedis = getJedis()) {
String value = jedis.get(gson.toJson(key));
if (value == null) {
return null;
} else {
jedis.del(gson.toJson(key));
return gson.fromJson(value, valueType);
}
}
}

@Override
public void putAll(Map<? extends K, ? extends V> m) {
try (Jedis jedis = getJedis()) {
for (Entry<? extends K, ? extends V> kvEntry : m.entrySet()) {
jedis.set(gson.toJson(kvEntry.getKey()), gson.toJson(kvEntry.getValue()));
}
}
}

@Override
public void clear() {
try(Jedis jedis = getJedis()) {
jedis.flushDB();
}
}

@Override
public Set<K> keySet() {
Set<K> keysSet = new HashSet<>();
try (Jedis jedis = getJedis()) {
do {
ScanResult<String> scanResult = jedis.scan(cursor, scanParams);
List<String> keys = scanResult.getResult();
for (String key : keys) {
K k = gson.fromJson(key, keyType);
keysSet.add(k);
}
cursor = scanResult.getCursor();
} while (!cursor.equals(ScanParams.SCAN_POINTER_START));
}
return keysSet;
}

@Override
public Collection<V> values() {
ArrayList<V> values = new ArrayList<>();
try (Jedis jedis = getJedis()) {
do {
ScanResult<String> scanResult = jedis.scan(cursor, scanParams);
List<String> keys = scanResult.getResult();
for (String key : keys) {
String valueRedis = jedis.get(key);
if (valueRedis != null) {
values.add(gson.fromJson(valueRedis, valueType));
}
}
cursor = scanResult.getCursor();
} while (!cursor.equals(ScanParams.SCAN_POINTER_START));
}
return values;
}

@Override
public Set<Entry<K, V>> entrySet() {
Set<Entry<K, V>> entrySet = new HashSet<>();
try (Jedis jedis = getJedis()) {
do {
ScanResult<String> scanResult = jedis.scan(cursor, scanParams);
List<String> keys = scanResult.getResult();
for (String key : keys) {
String valueRedis = jedis.get(key);
if (valueRedis != null) {
entrySet.add(new AbstractMap.SimpleEntry<>(gson.fromJson(key, keyType), gson.fromJson(valueRedis, valueType)));
}
}
cursor = scanResult.getCursor();
} while (!cursor.equals(ScanParams.SCAN_POINTER_START));
}
return entrySet;
}
}
181 changes: 181 additions & 0 deletions src/test/java/unitTest/MapRedisTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package unitTest;

import com.google.gson.Gson;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.redis.JedisConfig;
import org.redis.MapRedisImpl;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

class MapRedisTest {

private final JedisConfig jedisConfig = new JedisConfig();
private final JedisPool jedisPool = jedisConfig.getJedisPool();
private Jedis resource;
private final MapRedisImpl<String, String> mapRedis = new MapRedisImpl<>(String.class, String.class);
private final Gson gson = new Gson();


@BeforeEach
void setUp() {
resource = jedisPool.getResource();
}

@AfterEach
void tearDown() {
resource.flushDB();
resource.close();
}

@Test
void validSizeTest() {
resource.set("Key", "Value");
Assertions.assertEquals(1, mapRedis.size());
}

@Test
void emptySizeTest() {
Assertions.assertEquals(0, mapRedis.size());
}

@Test
void validIsNotEmptyTest() {
resource.set("Key", "Value");
boolean empty = mapRedis.isEmpty();
Assertions.assertFalse(empty);
}

@Test
void validIsEmptyTest() {
boolean empty = mapRedis.isEmpty();
Assertions.assertTrue(empty);
}

@Test
void validContainsKeyTest() {
resource.set(gson.toJson("Key"), gson.toJson("Value"));
boolean key = mapRedis.containsKey("Key");
Assertions.assertTrue(key);
}

@Test
void invalidContainsKeyTest() {
resource.set(gson.toJson("Key"), gson.toJson("Value"));
boolean key = mapRedis.containsKey("NotKey");
Assertions.assertFalse(key);
}

@Test
void validContainsValueTest() {
resource.set(gson.toJson("Key"), gson.toJson("Value"));
boolean value = mapRedis.containsValue("Value");
Assertions.assertTrue(value);
}

@Test
void invalidContainsValueTest() {
resource.set(gson.toJson("Key"), gson.toJson("Value"));
boolean value = mapRedis.containsValue("NotValue");
Assertions.assertFalse(value);
}

@Test
void validGetTest() {
resource.set(gson.toJson("Key"), gson.toJson("Value"));
String value = mapRedis.get("Key");
Assertions.assertEquals("Value", value);
}

@Test
void invalidGetTest() {
resource.set(gson.toJson("Key"), gson.toJson("Value"));
String value = mapRedis.get("Key1");
Assertions.assertNull(value);
}

@Test
void validPutTest() {
String put = mapRedis.put("Key", "Value");
System.out.println(put);
Assertions.assertEquals("Value", put);
}

@Test
void validRemoveTest() {
resource.set(gson.toJson("Key"), gson.toJson("Value"));
String value = mapRedis.remove("Key");
Assertions.assertNotNull(value);
Assertions.assertEquals("Value", value);
}

@Test
void invalidRemoveTest() {
String value = mapRedis.remove("Key");
Assertions.assertNull(value);
}

@Test
void validPutAllTest() {
Map<String, String> map = new HashMap<>();
map.put("Key", "Value");
map.put("Key1", "Value1");
mapRedis.putAll(map);
String value = resource.get(gson.toJson("Key"));
String value1 = resource.get(gson.toJson("Key1"));
Assertions.assertEquals("\"Value\"", value);
Assertions.assertEquals("\"Value1\"", value1);
Assertions.assertEquals(2, resource.dbSize());
}

@Test
void validClearTest() {
resource.set(gson.toJson("Key"), gson.toJson("Value"));
mapRedis.clear();
String value = resource.get(gson.toJson("Key"));
Assertions.assertNull(value);
Assertions.assertEquals(0, resource.dbSize());
}

@Test
void validKeySetTest() {
resource.set(gson.toJson("Key"), gson.toJson("Value"));
resource.set(gson.toJson("Key1"), gson.toJson("Value2"));
Set<String> keys = mapRedis.keySet();
Assertions.assertEquals(2, keys.size());
Assertions.assertTrue(keys.contains("Key"));
Assertions.assertTrue(keys.contains("Key1"));
Assertions.assertFalse(keys.contains("Key2"));
}

@Test
void validValuesTest() {
resource.set(gson.toJson("Key"), gson.toJson("Value"));
resource.set(gson.toJson("Key1"), gson.toJson("Value2"));
Collection<String> values = mapRedis.values();
Assertions.assertEquals(2, values.size());
Assertions.assertTrue(values.contains("Value"));
Assertions.assertTrue(values.contains("Value2"));
Assertions.assertFalse(values.contains("Value3"));
}

@Test
void validEntrySetTest() {
resource.set(gson.toJson("Key"), gson.toJson("Value"));
resource.set(gson.toJson("Key1"), gson.toJson("Value2"));
Set<Map.Entry<String, String>> entries = mapRedis.entrySet();
Assertions.assertEquals(2, entries.size());
Assertions.assertTrue(entries.contains(Map.entry("Key", "Value")));
Assertions.assertTrue(entries.contains(Map.entry("Key1", "Value2")));
Assertions.assertFalse(entries.contains(Map.entry("Key", "Value3")));
}


}