diff --git a/src/main/java/icu/funkye/redispike/handler/RedisCommandHandler.java b/src/main/java/icu/funkye/redispike/handler/RedisCommandHandler.java index 1a2d783..698d244 100644 --- a/src/main/java/icu/funkye/redispike/handler/RedisCommandHandler.java +++ b/src/main/java/icu/funkye/redispike/handler/RedisCommandHandler.java @@ -35,6 +35,7 @@ import icu.funkye.redispike.handler.process.impl.HGetRequestProcessor; import icu.funkye.redispike.handler.process.impl.HMgetRequestProcessor; import icu.funkye.redispike.handler.process.impl.HSetRequestProcessor; +import icu.funkye.redispike.handler.process.impl.HValsRequestProcessor; import icu.funkye.redispike.handler.process.impl.KeysRequestProcessor; import icu.funkye.redispike.handler.process.impl.SCardRequestProcessor; import icu.funkye.redispike.handler.process.impl.SPopRequestProcessor; @@ -52,9 +53,9 @@ public class RedisCommandHandler implements CommandHandler { - private final Logger logger = LoggerFactory.getLogger(getClass()); + private final Logger logger = LoggerFactory.getLogger(getClass()); - Map> processorMap = new HashMap<>(); + Map>> processorMap = new HashMap<>(); public RedisCommandHandler() { CommandRequestProcessor commandRequestProcessor = new CommandRequestProcessor(); @@ -91,6 +92,8 @@ public RedisCommandHandler() { processorMap.put(sCardRequestProcessor.getCmdCode().value(), sCardRequestProcessor); HExistsRequestProcessor hExistsRequestProcessor = new HExistsRequestProcessor(); processorMap.put(hExistsRequestProcessor.getCmdCode().value(), hExistsRequestProcessor); + HValsRequestProcessor hValsRequestProcessor = new HValsRequestProcessor(); + processorMap.put(hValsRequestProcessor.getCmdCode().value(), hValsRequestProcessor); } @Override diff --git a/src/main/java/icu/funkye/redispike/handler/process/impl/HValsRequestProcessor.java b/src/main/java/icu/funkye/redispike/handler/process/impl/HValsRequestProcessor.java new file mode 100644 index 0000000..eebe20c --- /dev/null +++ b/src/main/java/icu/funkye/redispike/handler/process/impl/HValsRequestProcessor.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package icu.funkye.redispike.handler.process.impl; + +import java.util.Optional; +import com.aerospike.client.AerospikeException; +import com.aerospike.client.Key; +import com.aerospike.client.Record; +import com.aerospike.client.listener.RecordListener; +import com.alipay.remoting.RemotingContext; + +import icu.funkye.redispike.factory.AeroSpikeClientFactory; +import icu.funkye.redispike.handler.process.AbstractRedisRequestProcessor; +import icu.funkye.redispike.protocol.RedisRequestCommandCode; +import icu.funkye.redispike.protocol.request.HValsRequest; +import icu.funkye.redispike.util.IntegerUtils; + +public class HValsRequestProcessor extends AbstractRedisRequestProcessor { + + public HValsRequestProcessor() { + this.cmdCode = new RedisRequestCommandCode(IntegerUtils.hashCodeToShort(HValsRequest.class.hashCode())); + } + + @Override + public void handle(RemotingContext ctx, HValsRequest request) { + Key key = new Key(AeroSpikeClientFactory.namespace, AeroSpikeClientFactory.set, request.getKey()); + client.get(AeroSpikeClientFactory.eventLoops.next(), new RecordListener() { + @Override + public void onSuccess(Key key, Record record) { + if (record == null) { + write(ctx,request); + return; + } + Optional.ofNullable(record.bins) + .ifPresent(bins -> bins.values().forEach(v -> + request.setResponse(v.toString()) + )); + write(ctx, request); + } + + @Override + public void onFailure(AerospikeException ae) { + logger.error(ae.getMessage(), ae); + write(ctx,request); + } + }, client.getReadPolicyDefault(), key); + } +} diff --git a/src/main/java/icu/funkye/redispike/protocol/RedisCommandDecoder.java b/src/main/java/icu/funkye/redispike/protocol/RedisCommandDecoder.java index 71bf771..a29f24e 100644 --- a/src/main/java/icu/funkye/redispike/protocol/RedisCommandDecoder.java +++ b/src/main/java/icu/funkye/redispike/protocol/RedisCommandDecoder.java @@ -28,6 +28,7 @@ import icu.funkye.redispike.protocol.request.HGetRequest; import icu.funkye.redispike.protocol.request.HMgetRequest; import icu.funkye.redispike.protocol.request.HSetRequest; +import icu.funkye.redispike.protocol.request.HValsRequest; import icu.funkye.redispike.protocol.request.KeysRequest; import icu.funkye.redispike.protocol.request.SAddRequest; import icu.funkye.redispike.protocol.request.SCardRequest; @@ -116,6 +117,8 @@ private AbstractRedisRequest convert2RedisRequest(List params, boolea return new HGetRequest(params.get(1), params.size() > 2 ? params.get(2) : null, flush); case "hgetall": return new HGetAllRequest(params.get(1), flush); + case "hvals": + return new HValsRequest(params.get(1), flush); case "hexists": return new HExistsRequest(params.get(1), params.get(2), flush); case "scard": diff --git a/src/main/java/icu/funkye/redispike/protocol/request/HExistsRequest.java b/src/main/java/icu/funkye/redispike/protocol/request/HExistsRequest.java index d745599..58ac8f1 100644 --- a/src/main/java/icu/funkye/redispike/protocol/request/HExistsRequest.java +++ b/src/main/java/icu/funkye/redispike/protocol/request/HExistsRequest.java @@ -36,7 +36,7 @@ public HExistsRequest(String key, String field, boolean flush) { this.key = key; if (StringUtils.isBlank(field)) { BulkResponse response = new BulkResponse(); - response.setError("ERR wrong number of arguments for 'hget' command"); + response.setError("ERR wrong number of arguments for 'hexists' command"); this.response = response; } else { this.response = new IntegerResponse(); diff --git a/src/main/java/icu/funkye/redispike/protocol/request/HValsRequest.java b/src/main/java/icu/funkye/redispike/protocol/request/HValsRequest.java new file mode 100644 index 0000000..84e0b41 --- /dev/null +++ b/src/main/java/icu/funkye/redispike/protocol/request/HValsRequest.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package icu.funkye.redispike.protocol.request; + +import java.util.ArrayList; +import com.alipay.remoting.util.StringUtils; + +import icu.funkye.redispike.protocol.AbstractRedisRequest; +import icu.funkye.redispike.protocol.RedisResponse; +import icu.funkye.redispike.protocol.response.BulkResponse; + +public class HValsRequest extends AbstractRedisRequest { + + final String key; + + BulkResponse response = new BulkResponse(new ArrayList<>()); + + public HValsRequest(String key, boolean flush) { + this.flush = flush; + this.key = key; + if (StringUtils.isBlank(key)) { + response.setError("ERR wrong number of arguments for 'hvals' command"); + } + } + + public String getKey() { + return key; + } + + @Override + public void setResponse(String data) { + this.response.appender(data); + } + + @Override + public RedisResponse getResponse() { + return response; + } + + public void setResponse(BulkResponse response) { + this.response = response; + } + + public void setError(String errorMsg) { + this.response.setError(errorMsg); + } + +} diff --git a/src/test/java/icu/funkye/redispike/ServerTest.java b/src/test/java/icu/funkye/redispike/ServerTest.java index e516d26..f4a05d4 100644 --- a/src/test/java/icu/funkye/redispike/ServerTest.java +++ b/src/test/java/icu/funkye/redispike/ServerTest.java @@ -162,6 +162,8 @@ public void testhHash() { Assertions.assertEquals(result, 2); List list = jedis.hmget(key, "b", "d"); Assertions.assertEquals(list.size(), 2); + list = jedis.hvals(key); + Assertions.assertEquals(list.size(), 2); result = jedis.hdel(key, map.keySet().toArray(new String[0])); Assertions.assertEquals(result, 2); key = String.valueOf(ThreadLocalRandom.current().nextInt(RandomValue)); @@ -183,6 +185,8 @@ public void testhHash() { Assertions.assertEquals(map.size(), 1); result = jedis.del(key); Assertions.assertEquals(result, 1); + list = jedis.hvals(key); + Assertions.assertEquals(list.size(), 0); } }