@Override public boolean addIndex(String queueID, long index, ResourceItem e) { if (!lockQueue(queueID)) { return false; } remove(queueID, e.getKey()); Jedis jedis = jedisPool.getResource(); try { String poolQueueKey = makePoolQueueKey(queueID); Long length = jedis.llen(poolQueueKey); if (index <= length) { index = length - 1; } String position = jedis.lindex(makePoolQueueKey(queueID), index); if (isNil(position)) { jedis.rpush(poolQueueKey, e.getKey()); } else { jedis.linsert(poolQueueKey, BinaryClient.LIST_POSITION.AFTER, position, e.getKey()); } jedis.hset(makeDataKey(queueID), e.getKey(), JSONObject.toJSONString(e)); } finally { IOUtils.closeQuietly(jedis); unLockQueue(queueID); } return true; }
@Override public boolean addAll(int index, Collection<? extends String> c) { try (Jedis jedis = getJedis()) { String pivot = get(index); for(String item : c){ jedis.linsert(name, BinaryClient.LIST_POSITION.AFTER, pivot, item); } } return true; }
@Override public void add(int index, String element) { try (Jedis jedis = getJedis()) { String pivot = get(index); jedis.linsert(name, BinaryClient.LIST_POSITION.AFTER, pivot, element); } }
@Override public Long linsert(final String key, final BinaryClient.LIST_POSITION where, final String pivot, final String value) { return this.doAction(ClientType.MASTER, new ClientFunction<Long>() { @Override public Long apply(JedisClient client) { return client.linsert(key, where, pivot, value); } }); }
@Override public Long linsert(final byte[] key, final BinaryClient.LIST_POSITION where, final byte[] pivot, final byte[] value) { return this.doAction(ClientType.MASTER, new ClientFunction<Long>() { @Override public Long apply(JedisClient client) { return client.linsert(key, where, pivot, value); } }); }
@Override public boolean addIndex(String queueID, long index, ResourceItem e) { if (!lockQueue(queueID)) { return false; } @Cleanup Jedis jedis = jedisPool.getResource(); try { remove(queueID, e.getKey()); // block 从1开始计数 int block = blockID(index + 1); List<String> sliceQueue = sliceQueue(queueID); String sliceID; if (block - 1 < sliceQueue.size()) { sliceID = sliceQueue.get(block - 1); } else { // create a new slice sliceID = String.valueOf(block); if (!sliceQueue.contains(sliceID)) { Preconditions.checkArgument(index <= size(queueID)); jedis.rpush(makeSliceQueueKey(queueID), sliceID); } else { sliceID = sliceQueue.get(sliceQueue.size() - 1); } } String poolQueueKey = makePoolQueueKey(queueID, sliceID); Long length = jedis.llen(poolQueueKey); long offset = blockOffset(index); if (offset <= length) { offset = length - 1; } String position = jedis.lindex(makePoolQueueKey(queueID, sliceID), offset); if (isNil(position)) { jedis.rpush(poolQueueKey, e.getKey()); } else { jedis.linsert(poolQueueKey, BinaryClient.LIST_POSITION.AFTER, position, e.getKey()); } jedis.hset(makeDataKey(queueID), e.getKey(), JSONObject.toJSONString(e)); } finally { unLockQueue(queueID); } return true; }
@Override public Long linsert(String key, BinaryClient.LIST_POSITION where, String pivot, String value) { throw new FakeJedisNotImplementedException(); }
@Override public Long linsert(byte[] key, BinaryClient.LIST_POSITION where, byte[] pivot, byte[] value) { throw new FakeJedisNotImplementedException(); }
@Override public Response<Long> linsert(String key, BinaryClient.LIST_POSITION where, String pivot, String value) { throw new FakeJedisNotImplementedException(); }
@Override public Response<Long> linsert(byte[] key, BinaryClient.LIST_POSITION where, byte[] pivot, byte[] value) { throw new FakeJedisNotImplementedException(); }
@Override public Long linsert(final String key, final BinaryClient.LIST_POSITION where, final String pivot, final String value) { return this.actions.linsert(this.namespaceKey(key), where, pivot, value); }
/** * @param key list键 * @param pivot 关键的元素,有重复就只看第一个 * @param value 在关键元素之后插入 * @return list长度 */ public long insertAfter(String key, String pivot,String value){ return getJedis().linsert(key, BinaryClient.LIST_POSITION.AFTER,pivot,value); }
/** * @param key list键 * @param pivot 关键的元素,只匹配到第一个 * @param value 之前插入 * @return list 长度 */ public long insertBefore(String key,String pivot,String value){ return getJedis().linsert(key, BinaryClient.LIST_POSITION.BEFORE,pivot,value); }
Response<Long> linsert(byte[] key, BinaryClient.LIST_POSITION where, byte[] pivot, byte[] value);
Long linsert(byte[] key, BinaryClient.LIST_POSITION where, byte[] pivot, byte[] value);
Long linsert(String key, BinaryClient.LIST_POSITION where, String pivot, String value);