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
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.24.2</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
52 changes: 51 additions & 1 deletion src/main/java/org/redis/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,57 @@
package org.redis;

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

import java.util.HashMap;
import java.util.Map;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
JedisPoolConfig poolConfig = new JedisPoolConfig();
try (JedisPool pool = new JedisPool(poolConfig, "localhost", 6379)) {
RedisMap map = new RedisMap(pool, "demo");

System.out.println("\n--- put / get / size ---");
System.out.println("put(a, 1) returned: " + map.put("a", "1"));
System.out.println("get(a) = " + map.get("a"));
System.out.println("size = " + map.size());

System.out.println("\n--- overwrite ---");
System.out.println("put(a, 100) returned previous: " + map.put("a", "100"));
System.out.println("get(a) = " + map.get("a"));

System.out.println("\n--- add more keys ---");
map.put("b", "2");
map.put("c", "3");
System.out.println("size = " + map.size());
System.out.println("containsKey(\"b\") = " + map.containsKey("b"));
System.out.println("containsValue(\"2\") = " + map.containsValue("2"));

System.out.println("\n--- keySet / values / entrySet ---");
System.out.println("keySet = " + map.keySet());
System.out.println("values = " + map.values());
System.out.println("entrySet = " + map.entrySet());

System.out.println("\n--- putAll ---");
Map<String, String> toPut = new HashMap<>();
toPut.put("x", "10");
toPut.put("y", "20");
map.putAll(toPut);
System.out.println("After putAll, size = " + map.size());
System.out.println("entrySet = " + map.entrySet());

System.out.println("\n--- remove ---");
System.out.println("remove(\"b\") returned: " + map.remove("b"));
System.out.println("containsKey(\"b\") = " + map.containsKey("b"));
System.out.println("keySet = " + map.keySet());

System.out.println("\n--- clear ---");
map.clear();
System.out.println("After clear, size = " + map.size());
System.out.println("keySet = " + map.keySet());
} catch (Exception e) {
System.err.println("Ошибка в работе RedisMap: " + e.getMessage());
}
}
}
136 changes: 136 additions & 0 deletions src/main/java/org/redis/RedisMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package org.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.*;
import java.util.stream.Collectors;

public class RedisMap implements Map<String, String> {

private final JedisPool pool;
private final String redisHashKey;

public RedisMap(JedisPool pool, String mapName) {
Objects.requireNonNull(pool, "JedisPool пустой");
Objects.requireNonNull(mapName, "mapName пустое");
this.pool = pool;
this.redisHashKey = "map:" + mapName;
}

@Override
public int size() {
try (Jedis jedis = pool.getResource()) {
long len = jedis.hlen(redisHashKey);
return (int) len;
}
}

@Override
public boolean isEmpty() {
return size() == 0;
}

@Override
public boolean containsKey(Object key) {
if (key == null) {
return false;
}
try (Jedis jedis = pool.getResource()) {
return jedis.hexists(redisHashKey, key.toString());
}
}

@Override
public boolean containsValue(Object value) {
if (value == null) {
return false;
}
try (Jedis jedis = pool.getResource()) {
List<String> values = jedis.hvals(redisHashKey);
return values.contains(value.toString());
}
}

@Override
public String get(Object key) {
if (key == null) {
return null;
}
try (Jedis jedis = pool.getResource()) {
return jedis.hget(redisHashKey, key.toString());
}
}

@Override
public String put(String key, String value) {
Objects.requireNonNull(key, "Пустой ключ");
Objects.requireNonNull(key, "Пустое значение");
try (Jedis jedis = pool.getResource()) {
String prev = jedis.hget(redisHashKey, key);
jedis.hset(redisHashKey, key, value);
return prev;
}
}

@Override
public String remove(Object key) {
if (key == null) {
return null;
}
try (Jedis jedis = pool.getResource()) {
String prev = jedis.hget(redisHashKey, key.toString());
jedis.hdel(redisHashKey, key.toString());
return prev;
}
}

@Override
public void putAll(Map<? extends String, ? extends String> m) {
if (m == null || m.isEmpty()) {
return;
}
try (Jedis jedis = pool.getResource()) {
Map<String, String> toStore = new HashMap<>();
for (Entry<? extends String, ? extends String> e : m.entrySet()) {
if (e.getKey() == null) {
throw new NullPointerException("Словарь содержит пустой ключ");
}
toStore.put(e.getKey(), e.getValue());
}
jedis.hset(redisHashKey, toStore);
}
}

@Override
public void clear() {
try (Jedis jedis = pool.getResource()) {
jedis.del(redisHashKey);
}
}

@Override
public Set<String> keySet() {
try (Jedis jedis = pool.getResource()) {
return new HashSet<>(jedis.hkeys(redisHashKey));
}
}

@Override
public Collection<String> values() {
try (Jedis jedis = pool.getResource()) {
return new ArrayList<>(jedis.hvals(redisHashKey));
}
}

@Override
public Set<Entry<String, String>> entrySet() {
try (Jedis jedis = pool.getResource()) {
Map<String, String> all = jedis.hgetAll(redisHashKey);
return all.entrySet().stream()
.map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), e.getValue()))
.collect(Collectors.toUnmodifiableSet());
}
}

}
111 changes: 111 additions & 0 deletions src/test/java/org/redis/RedisMapTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package org.redis;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.*;

import static org.junit.jupiter.api.Assertions.*;

public class RedisMapTest {

private JedisPool pool;
private RedisMap map;

@BeforeEach
public void beforeEach() {
pool = new JedisPool(new JedisPoolConfig(), "localhost", 6379);
String MAP_NAME = "testMap";
map = new RedisMap(pool, MAP_NAME);
map.clear();
}

private void closePool() {
if (pool != null) {
pool.close();
}
}

@Test
public void testPutAndGet() {
assertNull(map.put("a", "1"));
assertEquals("1", map.get("a"));
assertEquals(1, map.size());
assertTrue(map.containsKey("a"));
assertTrue(map.containsValue("1"));

closePool();
}

@Test
public void testRemove() {
map.put("k", "v");

assertEquals("v", map.remove("k"));
assertNull(map.get("k"));
assertFalse(map.containsKey("k"));

closePool();
}

@Test
public void testPutAllAndSize() {
Map<String, String> m = new HashMap<>();
m.put("x", "10");
m.put("y", "20");

map.putAll(m);

assertEquals(2, map.size());
assertEquals("10", map.get("x"));
assertEquals("20", map.get("y"));

closePool();
}

@Test
public void testClearAndIsEmpty() {
map.put("one", "1");
map.clear();

assertTrue(map.isEmpty());
assertEquals(0, map.size());

closePool();
}

@Test
public void testKeySetValuesEntrySet() {
map.put("k1", "v1");
map.put("k2", "v2");

Set<String> keys = map.keySet();

assertEquals(new HashSet<>(Arrays.asList("k1", "k2")), keys);
Collection<String> values = map.values();
assertTrue(values.containsAll(Arrays.asList("v1", "v2")));
assertEquals(2, values.size());
Set<Map.Entry<String, String>> entries = map.entrySet();
Map<String, String> snapshot = new HashMap<>();
for (Map.Entry<String, String> e : entries) {
snapshot.put(e.getKey(), e.getValue());
}
assertEquals(2, snapshot.size());
assertEquals("v1", snapshot.get("k1"));

closePool();
}

@Test
public void testOverwritePutReturnsPrevious() {
map.put("a", "1");

assertEquals("1", map.put("a", "2"));
assertEquals("2", map.get("a"));

closePool();
}

}