diff --git a/.gitignore b/.gitignore index f4857a0..67caddb 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ target/ *.bak *.log* data/ +*.json \ No newline at end of file diff --git a/src/main/java/icu/funkye/redispike/handler/RedisCommandHandler.java b/src/main/java/icu/funkye/redispike/handler/RedisCommandHandler.java index cd92335..1a581a3 100644 --- a/src/main/java/icu/funkye/redispike/handler/RedisCommandHandler.java +++ b/src/main/java/icu/funkye/redispike/handler/RedisCommandHandler.java @@ -32,6 +32,7 @@ import icu.funkye.redispike.handler.process.impl.HDelRequestProcessor; import icu.funkye.redispike.handler.process.impl.HGetAllRequestProcessor; 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.KeysRequestProcessor; import icu.funkye.redispike.handler.process.impl.SCardRequestProcessor; @@ -73,6 +74,8 @@ public RedisCommandHandler() { processorMap.put(hGetAllRequestProcessor.getCmdCode().value(), hGetAllRequestProcessor); HGetRequestProcessor hGetRequestProcessor = new HGetRequestProcessor(); processorMap.put(hGetRequestProcessor.getCmdCode().value(), hGetRequestProcessor); + HMgetRequestProcessor hMgetRequestProcessor = new HMgetRequestProcessor(); + processorMap.put(hMgetRequestProcessor.getCmdCode().value(), hMgetRequestProcessor); SMembersRequestProcessor smembersRequestProcessor = new SMembersRequestProcessor(); processorMap.put(smembersRequestProcessor.getCmdCode().value(), smembersRequestProcessor); SAddRequestProcessor sAddRequestProcessor = new SAddRequestProcessor(); diff --git a/src/main/java/icu/funkye/redispike/handler/process/impl/HMgetRequestProcessor.java b/src/main/java/icu/funkye/redispike/handler/process/impl/HMgetRequestProcessor.java new file mode 100644 index 0000000..cf59682 --- /dev/null +++ b/src/main/java/icu/funkye/redispike/handler/process/impl/HMgetRequestProcessor.java @@ -0,0 +1,63 @@ +/* + * 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.ArrayList; +import com.aerospike.client.AerospikeException; +import com.aerospike.client.Bin; +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.HGetAllRequest; +import icu.funkye.redispike.protocol.request.HMgetRequest; +import icu.funkye.redispike.util.IntegerUtils; + +public class HMgetRequestProcessor extends AbstractRedisRequestProcessor { + + public HMgetRequestProcessor() { + this.cmdCode = new RedisRequestCommandCode(IntegerUtils.hashCodeToShort(HMgetRequest.class.hashCode())); + } + + @Override + public void handle(RemotingContext ctx, HMgetRequest 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; + } + record.bins.forEach((k,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,request.getField().toArray(new String[0])); + } +} diff --git a/src/main/java/icu/funkye/redispike/protocol/RedisCommandDecoder.java b/src/main/java/icu/funkye/redispike/protocol/RedisCommandDecoder.java index 82c3086..e8dfd29 100644 --- a/src/main/java/icu/funkye/redispike/protocol/RedisCommandDecoder.java +++ b/src/main/java/icu/funkye/redispike/protocol/RedisCommandDecoder.java @@ -25,6 +25,7 @@ import icu.funkye.redispike.protocol.request.HDelRequest; import icu.funkye.redispike.protocol.request.HGetAllRequest; 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.KeysRequest; import icu.funkye.redispike.protocol.request.SAddRequest; @@ -88,6 +89,9 @@ private AbstractRedisRequest convert2RedisRequest(List params, boolea LOGGER.debug("cmd: {}", params); } switch (cmd) { + case "hmget": + params.remove(0); + return new HMgetRequest(params.remove(0), params, flush); case "hdel": return new HDelRequest(params, flush); case "get": diff --git a/src/main/java/icu/funkye/redispike/protocol/request/HMgetRequest.java b/src/main/java/icu/funkye/redispike/protocol/request/HMgetRequest.java new file mode 100644 index 0000000..2d0e50b --- /dev/null +++ b/src/main/java/icu/funkye/redispike/protocol/request/HMgetRequest.java @@ -0,0 +1,73 @@ +/* + * 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 java.util.List; + +import icu.funkye.redispike.protocol.AbstractRedisRequest; +import icu.funkye.redispike.protocol.RedisResponse; +import icu.funkye.redispike.protocol.response.BulkResponse; + +public class HMgetRequest extends AbstractRedisRequest { + + final String key; + + final List field; + + BulkResponse response = new BulkResponse(new ArrayList<>()); + + public HMgetRequest(String key, List field, boolean flush) { + this.flush = flush; + this.key = key; + if (field.isEmpty()) { + response.setError("ERR wrong number of arguments for 'hget' command"); + } + this.field = field; + } + + public String getKey() { + return key; + } + + @Override + public void setResponse(String data) { + this.response.appender(data); + } + + @Override + public RedisResponse getResponse() { + return response; + } + + public List getField() { + return field; + } + + public void setResponse(BulkResponse response) { + this.response = response; + } + + public void setError(String errorMsg) { + this.response.setError(errorMsg); + } + + @Override + public String toString() { + return "HGetRequest{" + "key='" + key + '\'' + ", field='" + field + '\'' + ", response=" + response + '}'; + } +} diff --git a/src/test/java/icu/funkye/redispike/ServerTest.java b/src/test/java/icu/funkye/redispike/ServerTest.java index e25c61d..7e2e41f 100644 --- a/src/test/java/icu/funkye/redispike/ServerTest.java +++ b/src/test/java/icu/funkye/redispike/ServerTest.java @@ -160,6 +160,8 @@ public void testhHash() { map.put("d", "e"); result = jedis.hset(key, map); Assertions.assertEquals(result, 2); + List list = jedis.hmget(key, "b", "d"); + 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));