Skip to content

Commit

Permalink
feature: support hkeys protocol (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
funky-eyes committed Apr 22, 2024
1 parent 192c9cf commit 1c74290
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import icu.funkye.redispike.handler.process.impl.hash.HGetRequestProcessor;
import icu.funkye.redispike.handler.process.impl.hash.HIncrbyRequestProcessor;
import icu.funkye.redispike.handler.process.impl.hash.HIncrbyfloatRequestProcessor;
import icu.funkye.redispike.handler.process.impl.hash.HKeysRequestProcessor;
import icu.funkye.redispike.handler.process.impl.hash.HLenRequestProcessor;
import icu.funkye.redispike.handler.process.impl.hash.HMgetRequestProcessor;
import icu.funkye.redispike.handler.process.impl.hash.HSetRequestProcessor;
Expand Down Expand Up @@ -100,6 +101,8 @@ public RedisCommandHandler() {
processorMap.put(hIncrbyfloatRequestProcessor.getCmdCode().value(), hIncrbyfloatRequestProcessor);
HLenRequestProcessor hLenRequestProcessor = new HLenRequestProcessor();
processorMap.put(hLenRequestProcessor.getCmdCode().value(), hLenRequestProcessor);
HKeysRequestProcessor hKeysRequestProcessor = new HKeysRequestProcessor();
processorMap.put(hKeysRequestProcessor.getCmdCode().value(), hKeysRequestProcessor);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.hash;

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.hash.HKeysRequest;
import icu.funkye.redispike.util.IntegerUtils;

public class HKeysRequestProcessor extends AbstractRedisRequestProcessor<HKeysRequest> {

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

@Override
public void handle(RemotingContext ctx, HKeysRequest 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.keySet().forEach(request::setResponse));
write(ctx, request);
}

@Override
public void onFailure(AerospikeException exception) {

}
}, client.getReadPolicyDefault(), key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import icu.funkye.redispike.protocol.request.hash.HGetRequest;
import icu.funkye.redispike.protocol.request.hash.HIncrbyRequest;
import icu.funkye.redispike.protocol.request.hash.HIncrbyfloatRequest;
import icu.funkye.redispike.protocol.request.hash.HKeysRequest;
import icu.funkye.redispike.protocol.request.hash.HLenRequest;
import icu.funkye.redispike.protocol.request.hash.HMgetRequest;
import icu.funkye.redispike.protocol.request.hash.HSetRequest;
Expand Down Expand Up @@ -116,6 +117,9 @@ private AbstractRedisRequest<?> convert2RedisRequest(List<String> params, boolea
case "del":
params.remove(0);
return new DelRequest(params, flush);
case "hkeys":
params.remove(0);
return new HKeysRequest(params, flush);
case "hget":
return new HGetRequest(params.get(1), params.size() > 2 ? params.get(2) : null, flush);
case "hincrby":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.hash;

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 HKeysRequest extends AbstractRedisRequest<String> {

String key;

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

public HKeysRequest(List<String> params, boolean flush) {
this.flush = flush;
this.key = params.remove(0);
}

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

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

public String getKey() {
return key;
}

}
8 changes: 8 additions & 0 deletions src/test/java/icu/funkye/redispike/ServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,19 @@
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import icu.funkye.redispike.factory.AeroSpikeClientFactory;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.condition.DisabledIfSystemProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.params.SetParams;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ServerTest {
static Server server;
static IAerospikeClient aspClient;
Expand All @@ -57,6 +61,7 @@ public static void init() throws ParseException {
}

@Test
@Order(value = Integer.MIN_VALUE)
public void TestPippline() {
List<String> keys = new ArrayList<>();
String key = String.valueOf(ThreadLocalRandom.current().nextInt(RandomValue));
Expand Down Expand Up @@ -150,6 +155,7 @@ public void testKeys() {
}

@Test
@Order(value = Integer.MAX_VALUE)
public void testhHash() {
String key = String.valueOf(ThreadLocalRandom.current().nextInt(RandomValue));
try (Jedis jedis = JedisPooledFactory.getJedisInstance()) {
Expand All @@ -163,6 +169,8 @@ public void testhHash() {
map.put("d", "e");
result = jedis.hset(key, map);
Assertions.assertEquals(result, 2);
Set<String> set = jedis.hkeys(key);
Assertions.assertTrue(set.containsAll(map.keySet()));
List<String> list = jedis.hmget(key, "b", "d");
Assertions.assertEquals(list.size(), 2);
list = jedis.hvals(key);
Expand Down

0 comments on commit 1c74290

Please sign in to comment.