Skip to content

Commit

Permalink
feature: support hmget protocol (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
funky-eyes authored Apr 11, 2024
1 parent 4b588f0 commit dc0bad9
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ target/
*.bak
*.log*
data/
*.json
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<HMgetRequest> {

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]));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -88,6 +89,9 @@ private AbstractRedisRequest<?> convert2RedisRequest(List<String> 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":
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> {

final String key;

final List<String> field;

BulkResponse response = new BulkResponse(new ArrayList<>());

public HMgetRequest(String key, List<String> 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<String> getResponse() {
return response;
}

public List<String> 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 + '}';
}
}
2 changes: 2 additions & 0 deletions src/test/java/icu/funkye/redispike/ServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ public void testhHash() {
map.put("d", "e");
result = jedis.hset(key, map);
Assertions.assertEquals(result, 2);
List<String> 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));
Expand Down

0 comments on commit dc0bad9

Please sign in to comment.