diff --git a/src/main/java/org/redis/JedisConfig.java b/src/main/java/org/redis/JedisConfig.java new file mode 100644 index 0000000..e995dc4 --- /dev/null +++ b/src/main/java/org/redis/JedisConfig.java @@ -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; + } + +} diff --git a/src/main/java/org/redis/Main.java b/src/main/java/org/redis/Main.java index 68b3db6..cb7e92f 100644 --- a/src/main/java/org/redis/Main.java +++ b/src/main/java/org/redis/Main.java @@ -2,6 +2,8 @@ public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + MapRedisImpl mapRedis = new MapRedisImpl<>(String.class, String.class); + mapRedis.put("Key", "Value"); + System.out.println(mapRedis.containsKey("Key")); } } \ No newline at end of file diff --git a/src/main/java/org/redis/MapRedisImpl.java b/src/main/java/org/redis/MapRedisImpl.java new file mode 100644 index 0000000..eb9b133 --- /dev/null +++ b/src/main/java/org/redis/MapRedisImpl.java @@ -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 implements Map { + private final JedisConfig jedisConfig; + private final JedisPool jedisPool; + private final Gson gson; + private final Class valueType; + private final Class keyType; + private String cursor = ScanParams.SCAN_POINTER_START; + private final ScanParams scanParams = new ScanParams().count(100); + + public MapRedisImpl(Class keyType, Class 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 scanResult = jedis.scan(cursor, scanParams); + List 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 m) { + try (Jedis jedis = getJedis()) { + for (Entry 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 keySet() { + Set keysSet = new HashSet<>(); + try (Jedis jedis = getJedis()) { + do { + ScanResult scanResult = jedis.scan(cursor, scanParams); + List 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 values() { + ArrayList values = new ArrayList<>(); + try (Jedis jedis = getJedis()) { + do { + ScanResult scanResult = jedis.scan(cursor, scanParams); + List 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> entrySet() { + Set> entrySet = new HashSet<>(); + try (Jedis jedis = getJedis()) { + do { + ScanResult scanResult = jedis.scan(cursor, scanParams); + List 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; + } +} diff --git a/src/test/java/unitTest/MapRedisTest.java b/src/test/java/unitTest/MapRedisTest.java new file mode 100644 index 0000000..6685908 --- /dev/null +++ b/src/test/java/unitTest/MapRedisTest.java @@ -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 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 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 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 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> 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"))); + } + + +}