Search in sources :

Example 36 with Pipeline

use of redis.clients.jedis.Pipeline in project storm by apache.

the class TestRedisDataSourcesProvider method testRedisSink.

@SuppressWarnings("unchecked")
@Test
public void testRedisSink() {
    ISqlTridentDataSource ds = DataSourcesRegistry.constructTridentDataSource(URI.create("redis://:foobared@localhost:6380/2"), null, null, TBL_PROPERTIES, FIELDS);
    Assert.assertNotNull(ds);
    ISqlTridentDataSource.SqlTridentConsumer consumer = ds.getConsumer();
    Assert.assertEquals(RedisState.Factory.class, consumer.getStateFactory().getClass());
    Assert.assertEquals(RedisStateUpdater.class, consumer.getStateUpdater().getClass());
    RedisState state = (RedisState) consumer.getStateFactory().makeState(Collections.emptyMap(), null, 0, 1);
    StateUpdater stateUpdater = consumer.getStateUpdater();
    JedisPool mockJedisPool = mock(JedisPool.class);
    Jedis mockJedis = mock(Jedis.class);
    Pipeline mockPipeline = mock(Pipeline.class);
    Whitebox.setInternalState(state, "jedisPool", mockJedisPool);
    when(mockJedisPool.getResource()).thenReturn(mockJedis);
    when(mockJedis.pipelined()).thenReturn(mockPipeline);
    List<TridentTuple> tupleList = mockTupleList();
    stateUpdater.updateState(state, tupleList, null);
    for (TridentTuple t : tupleList) {
        // PK goes to the key
        String id = String.valueOf(t.getValueByField("ID"));
        String serializedValue = new String(SERIALIZER.write(t.getValues(), null).array());
        verify(mockPipeline).hset(eq(ADDITIONAL_KEY), eq(id), eq(serializedValue));
    }
    verify(mockPipeline).sync();
    verify(mockJedis).close();
}
Also used : Jedis(redis.clients.jedis.Jedis) RedisState(org.apache.storm.redis.trident.state.RedisState) ISqlTridentDataSource(org.apache.storm.sql.runtime.ISqlTridentDataSource) JedisPool(redis.clients.jedis.JedisPool) StateUpdater(org.apache.storm.trident.state.StateUpdater) RedisStateUpdater(org.apache.storm.redis.trident.state.RedisStateUpdater) RedisClusterStateUpdater(org.apache.storm.redis.trident.state.RedisClusterStateUpdater) Pipeline(redis.clients.jedis.Pipeline) TridentTuple(org.apache.storm.trident.tuple.TridentTuple) Test(org.junit.Test)

Example 37 with Pipeline

use of redis.clients.jedis.Pipeline in project storm by apache.

the class RedisStateUpdater method updateStatesToRedis.

/**
     * {@inheritDoc}
     */
@Override
protected void updateStatesToRedis(RedisState redisState, Map<String, String> keyToValue) {
    Jedis jedis = null;
    try {
        jedis = redisState.getJedis();
        Pipeline pipeline = jedis.pipelined();
        for (Map.Entry<String, String> kvEntry : keyToValue.entrySet()) {
            String key = kvEntry.getKey();
            String value = kvEntry.getValue();
            switch(dataType) {
                case STRING:
                    if (this.expireIntervalSec > 0) {
                        pipeline.setex(key, expireIntervalSec, value);
                    } else {
                        pipeline.set(key, value);
                    }
                    break;
                case HASH:
                    pipeline.hset(additionalKey, key, value);
                    break;
                default:
                    throw new IllegalArgumentException("Cannot process such data type: " + dataType);
            }
        }
        // it expires key itself entirely, so use it with caution
        if (dataType == RedisDataTypeDescription.RedisDataType.HASH && this.expireIntervalSec > 0) {
            pipeline.expire(additionalKey, expireIntervalSec);
        }
        pipeline.sync();
    } finally {
        if (jedis != null) {
            redisState.returnJedis(jedis);
        }
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) Map(java.util.Map) Pipeline(redis.clients.jedis.Pipeline)

Example 38 with Pipeline

use of redis.clients.jedis.Pipeline in project cachecloud by sohutv.

the class TransactionCommandsTest method testResetStateWhenInMultiWithinPipeline.

@Test
public void testResetStateWhenInMultiWithinPipeline() {
    jedis.auth("foobared");
    Pipeline p = jedis.pipelined();
    p.multi();
    p.set("foooo", "barrr");
    jedis.resetState();
    assertEquals(null, jedis.get("foooo"));
}
Also used : Pipeline(redis.clients.jedis.Pipeline) Test(org.junit.Test)

Example 39 with Pipeline

use of redis.clients.jedis.Pipeline in project cachecloud by sohutv.

the class PipeliningTest method multi.

@Test
public void multi() {
    Pipeline p = jedis.pipelined();
    p.multi();
    Response<Long> r1 = p.hincrBy("a", "f1", -1);
    Response<Long> r2 = p.hincrBy("a", "f1", -2);
    Response<List<Object>> r3 = p.exec();
    List<Object> result = p.syncAndReturnAll();
    assertEquals(new Long(-1), r1.get());
    assertEquals(new Long(-3), r2.get());
    assertEquals(4, result.size());
    assertEquals("OK", result.get(0));
    assertEquals("QUEUED", result.get(1));
    assertEquals("QUEUED", result.get(2));
    // 4th result is a list with the results from the multi
    @SuppressWarnings("unchecked") List<Object> multiResult = (List<Object>) result.get(3);
    assertEquals(new Long(-1), multiResult.get(0));
    assertEquals(new Long(-3), multiResult.get(1));
    assertEquals(new Long(-1), r3.get().get(0));
    assertEquals(new Long(-3), r3.get().get(1));
}
Also used : Pipeline(redis.clients.jedis.Pipeline) Test(org.junit.Test)

Example 40 with Pipeline

use of redis.clients.jedis.Pipeline in project cachecloud by sohutv.

the class PipeliningTest method pipelineResponse.

@Test
public void pipelineResponse() {
    jedis.set("string", "foo");
    jedis.lpush("list", "foo");
    jedis.hset("hash", "foo", "bar");
    jedis.zadd("zset", 1, "foo");
    jedis.sadd("set", "foo");
    jedis.setrange("setrange", 0, "0123456789");
    byte[] bytesForSetRange = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    jedis.setrange("setrangebytes".getBytes(), 0, bytesForSetRange);
    Pipeline p = jedis.pipelined();
    Response<String> string = p.get("string");
    Response<String> list = p.lpop("list");
    Response<String> hash = p.hget("hash", "foo");
    Response<Set<String>> zset = p.zrange("zset", 0, -1);
    Response<String> set = p.spop("set");
    Response<Boolean> blist = p.exists("list");
    Response<Double> zincrby = p.zincrby("zset", 1, "foo");
    Response<Long> zcard = p.zcard("zset");
    p.lpush("list", "bar");
    Response<List<String>> lrange = p.lrange("list", 0, -1);
    Response<Map<String, String>> hgetAll = p.hgetAll("hash");
    p.sadd("set", "foo");
    Response<Set<String>> smembers = p.smembers("set");
    Response<Set<Tuple>> zrangeWithScores = p.zrangeWithScores("zset", 0, -1);
    Response<String> getrange = p.getrange("setrange", 1, 3);
    Response<byte[]> getrangeBytes = p.getrange("setrangebytes".getBytes(), 6, 8);
    p.sync();
    assertEquals("foo", string.get());
    assertEquals("foo", list.get());
    assertEquals("bar", hash.get());
    assertEquals("foo", zset.get().iterator().next());
    assertEquals("foo", set.get());
    assertEquals(false, blist.get());
    assertEquals(Double.valueOf(2), zincrby.get());
    assertEquals(Long.valueOf(1), zcard.get());
    assertEquals(1, lrange.get().size());
    assertNotNull(hgetAll.get().get("foo"));
    assertEquals(1, smembers.get().size());
    assertEquals(1, zrangeWithScores.get().size());
    assertEquals("123", getrange.get());
    byte[] expectedGetRangeBytes = { 6, 7, 8 };
    assertArrayEquals(expectedGetRangeBytes, getrangeBytes.get());
}
Also used : Pipeline(redis.clients.jedis.Pipeline) Test(org.junit.Test)

Aggregations

Pipeline (redis.clients.jedis.Pipeline)77 Test (org.junit.Test)72 Jedis (redis.clients.jedis.Jedis)12 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Map (java.util.Map)6 JedisDataException (redis.clients.jedis.exceptions.JedisDataException)6 HashMap (java.util.HashMap)5 Set (java.util.Set)3 Response (redis.clients.jedis.Response)3 LinkedHashMap (java.util.LinkedHashMap)2 SpanEventBo (com.navercorp.pinpoint.common.server.bo.SpanEventBo)1 BasePinpointTest (com.navercorp.pinpoint.test.junit4.BasePinpointTest)1 RedisDataTypeDescription (org.apache.storm.redis.common.mapper.RedisDataTypeDescription)1 RedisClusterStateUpdater (org.apache.storm.redis.trident.state.RedisClusterStateUpdater)1 RedisState (org.apache.storm.redis.trident.state.RedisState)1 RedisStateUpdater (org.apache.storm.redis.trident.state.RedisStateUpdater)1 ISqlTridentDataSource (org.apache.storm.sql.runtime.ISqlTridentDataSource)1 StateUpdater (org.apache.storm.trident.state.StateUpdater)1 TridentTuple (org.apache.storm.trident.tuple.TridentTuple)1