From 51f47198288d745d434c0b91452583a30f1a7a8e Mon Sep 17 00:00:00 2001 From: Thierno Diallo Date: Mon, 14 Nov 2022 14:29:31 -0500 Subject: [PATCH] Programming Challenge #6 --- .../RU102J/dao/FeedDaoRedisImpl.java | 23 ++++++++++++++----- .../RU102J/dao/FeedDaoRedisImplTest.java | 1 - 2 files changed, 17 insertions(+), 7 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..c125f66 100644 --- a/src/main/java/com/redislabs/university/RU102J/dao/FeedDaoRedisImpl.java +++ b/src/main/java/com/redislabs/university/RU102J/dao/FeedDaoRedisImpl.java @@ -1,12 +1,15 @@ package com.redislabs.university.RU102J.dao; 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; + +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; +import redis.clients.jedis.Pipeline; +import redis.clients.jedis.StreamEntry; +import redis.clients.jedis.StreamEntryID; public class FeedDaoRedisImpl implements FeedDao { @@ -20,9 +23,17 @@ public FeedDaoRedisImpl(JedisPool jedisPool) { // Challenge #6 @Override - public void insert(MeterReading meterReading) { - // START Challenge #6 - // END Challenge #6 + public void insert( MeterReading meterReading ) { + // START Challenge #6 + try ( Jedis jedis = jedisPool.getResource() ) { + Pipeline pipeline = jedis.pipelined(); + + pipeline.xadd( RedisSchema.getGlobalFeedKey(), StreamEntryID.NEW_ENTRY, meterReading.toMap() ); + pipeline.xadd( RedisSchema.getFeedKey( meterReading.getSiteId() ), StreamEntryID.NEW_ENTRY, meterReading.toMap() ); + + pipeline.sync(); + } + // END Challenge #6 } @Override 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);