From aaf1fb092d28c1971bc3c3b209d23975fa9523dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20A=2E=20I=2E=20L=C3=B3pez?= <17472377+J-A-I-L@users.noreply.github.com> Date: Sat, 9 Jul 2022 02:24:46 +0200 Subject: [PATCH] Challenge #6 Streams for Feeds Implement `insert()` in `FeedDaoRedisImpl.java`. --- .../university/RU102J/dao/FeedDaoRedisImpl.java | 14 ++++++++++++-- .../RU102J/dao/FeedDaoRedisImplTest.java | 1 - 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/redislabs/university/RU102J/dao/FeedDaoRedisImpl.java b/src/main/java/com/redislabs/university/RU102J/dao/FeedDaoRedisImpl.java index 4d34dd3..09d1438 100644 --- a/src/main/java/com/redislabs/university/RU102J/dao/FeedDaoRedisImpl.java +++ b/src/main/java/com/redislabs/university/RU102J/dao/FeedDaoRedisImpl.java @@ -3,10 +3,8 @@ import com.redislabs.university.RU102J.api.MeterReading; import redis.clients.jedis.*; -import java.nio.channels.Pipe; import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; public class FeedDaoRedisImpl implements FeedDao { @@ -22,9 +20,21 @@ public FeedDaoRedisImpl(JedisPool jedisPool) { @Override public void insert(MeterReading meterReading) { // START Challenge #6 + try (Jedis jedis = jedisPool.getResource()) { + final Pipeline pipelined = jedis.pipelined(); + pipelined.xadd(RedisSchema.getGlobalFeedKey(), StreamEntryID.NEW_ENTRY, meterReading.toMap(), + globalMaxFeedLength, true); + pipelined.xadd(RedisSchema.getFeedKey(meterReading.getSiteId()), StreamEntryID.NEW_ENTRY, meterReading.toMap(), + siteMaxFeedLength, true); + pipelined.sync(); + } // END Challenge #6 } + private void insertForKey(String globalFeedKey, MeterReading meterReading, Long maxGlobalDays) { + + } + @Override public List getRecentGlobal(int limit) { return getRecent(RedisSchema.getGlobalFeedKey(), limit); diff --git a/src/test/java/com/redislabs/university/RU102J/dao/FeedDaoRedisImplTest.java b/src/test/java/com/redislabs/university/RU102J/dao/FeedDaoRedisImplTest.java index bb95831..678bec2 100644 --- a/src/test/java/com/redislabs/university/RU102J/dao/FeedDaoRedisImplTest.java +++ b/src/test/java/com/redislabs/university/RU102J/dao/FeedDaoRedisImplTest.java @@ -53,7 +53,6 @@ public void flush() { } // Challenge #6 - @Ignore @Test public void testBasicInsertReturnsRecent() { FeedDao dao = new FeedDaoRedisImpl(jedisPool);