Skip to content

Commit

Permalink
feature: support hexists protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
funky-eyes committed Apr 12, 2024
1 parent 4f859d8 commit cab3381
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.alipay.remoting.RemotingProcessor;
import icu.funkye.redispike.handler.process.impl.GetRequestProcessor;
import icu.funkye.redispike.handler.process.impl.HDelRequestProcessor;
import icu.funkye.redispike.handler.process.impl.HExistsRequestProcessor;
import icu.funkye.redispike.handler.process.impl.HGetAllRequestProcessor;
import icu.funkye.redispike.handler.process.impl.HGetRequestProcessor;
import icu.funkye.redispike.handler.process.impl.HMgetRequestProcessor;
Expand Down Expand Up @@ -88,6 +89,8 @@ public RedisCommandHandler() {
processorMap.put(sRemRequestProcessor.getCmdCode().value(), sRemRequestProcessor);
SCardRequestProcessor sCardRequestProcessor = new SCardRequestProcessor();
processorMap.put(sCardRequestProcessor.getCmdCode().value(), sCardRequestProcessor);
HExistsRequestProcessor hExistsRequestProcessor = new HExistsRequestProcessor();
processorMap.put(hExistsRequestProcessor.getCmdCode().value(), hExistsRequestProcessor);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.HExistsRequest;
import icu.funkye.redispike.util.IntegerUtils;

public class HExistsRequestProcessor extends AbstractRedisRequestProcessor<HExistsRequest> {

public HExistsRequestProcessor() {
this.cmdCode = new RedisRequestCommandCode(IntegerUtils.hashCodeToShort(HExistsRequest.class.hashCode()));
}

@Override
public void handle(RemotingContext ctx, HExistsRequest 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;
}
if (record.bins != null) {
request.setResponse("1");
} else {
request.setResponse("0");
}
write(ctx, request);
}

@Override
public void onFailure(AerospikeException ae) {
logger.error(ae.getMessage(), ae);
write(ctx,request);
}
}, client.getReadPolicyDefault(), key,request.getField());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package icu.funkye.redispike.handler.process.impl;

import java.util.ArrayList;
import java.util.Optional;
import com.aerospike.client.AerospikeException;
import com.aerospike.client.Bin;
import com.aerospike.client.Key;
Expand Down Expand Up @@ -47,10 +48,9 @@ public void onSuccess(Key key, Record record) {
write(ctx,request);
return;
}
record.bins.forEach((k,v)-> {
request.setResponse(v.toString());
});
write(ctx,request);
Optional.ofNullable(record.bins)
.ifPresent(bins -> bins.values().forEach(v -> request.setResponse(v.toString())));
write(ctx, request);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.concurrent.CountDownLatch;
import com.alipay.remoting.CommandDecoder;
import icu.funkye.redispike.protocol.request.HDelRequest;
import icu.funkye.redispike.protocol.request.HExistsRequest;
import icu.funkye.redispike.protocol.request.HGetAllRequest;
import icu.funkye.redispike.protocol.request.HGetRequest;
import icu.funkye.redispike.protocol.request.HMgetRequest;
Expand Down Expand Up @@ -115,6 +116,8 @@ private AbstractRedisRequest<?> convert2RedisRequest(List<String> 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 "hexists":
return new HExistsRequest(params.get(1), params.get(2), flush);
case "scard":
return new SCardRequest(params.get(1), flush);
case "sadd":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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 com.alipay.remoting.util.StringUtils;

import icu.funkye.redispike.protocol.AbstractRedisRequest;
import icu.funkye.redispike.protocol.RedisResponse;
import icu.funkye.redispike.protocol.response.BulkResponse;
import icu.funkye.redispike.protocol.response.IntegerResponse;

public class HExistsRequest extends AbstractRedisRequest<String> {

final String key;

final String field;

RedisResponse<String> response;

public HExistsRequest(String key, String field, boolean flush) {
this.flush = flush;
this.key = key;
if (StringUtils.isBlank(field)) {
BulkResponse response = new BulkResponse();
response.setError("ERR wrong number of arguments for 'hget' command");
this.response = response;
}else {
this.response = new IntegerResponse();
}
this.field = field;
}

public String getKey() {
return key;
}

@Override
public void setResponse(String data) {
this.response.setData(data);
}

@Override
public RedisResponse<String> getResponse() {
return response;
}

public String getField() {
return field;
}

public void setResponse(BulkResponse response) {
this.response = response;
}

}
10 changes: 9 additions & 1 deletion src/test/java/icu/funkye/redispike/ServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class ServerTest {
@BeforeAll
public static void init() throws ParseException {
server = new Server();
server.start("-p 6789".split(" "));
server.start("-th 10.58.10.103 -tp 3000 -n test -s tdkv-test -TU tongdun-admin1 -TP xxxzzz123 -p 6789".split(" "));
JedisPooledFactory.getJedisPoolInstance("127.0.0.1", 6789);
aspClient = AeroSpikeClientFactory.getClient();
}
Expand Down Expand Up @@ -171,6 +171,14 @@ public void testhHash() {
Assertions.assertEquals(result, 0);
String value = jedis.hget(key, "f");
Assertions.assertEquals(value, "g");
value = jedis.hget(key, "l");
Assertions.assertNull(value);
list = jedis.hmget(key,"l","p");
Assertions.assertTrue(list.isEmpty());
boolean hexists =jedis.hexists(key,"i");
Assertions.assertFalse(hexists);
hexists =jedis.hexists(key,"f");
Assertions.assertTrue(hexists);
map = jedis.hgetAll(key);
Assertions.assertEquals(map.size(), 1);
result = jedis.del(key);
Expand Down

0 comments on commit cab3381

Please sign in to comment.