Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: support hincrbyfloat protocol #27

Merged
merged 4 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import icu.funkye.redispike.handler.process.impl.HGetAllRequestProcessor;
import icu.funkye.redispike.handler.process.impl.HGetRequestProcessor;
import icu.funkye.redispike.handler.process.impl.HIncrbyRequestProcessor;
import icu.funkye.redispike.handler.process.impl.HIncrbyfloatRequestProcessor;
import icu.funkye.redispike.handler.process.impl.HMgetRequestProcessor;
import icu.funkye.redispike.handler.process.impl.HSetRequestProcessor;
import icu.funkye.redispike.handler.process.impl.HValsRequestProcessor;
Expand Down Expand Up @@ -97,6 +98,8 @@ public RedisCommandHandler() {
processorMap.put(hValsRequestProcessor.getCmdCode().value(), hValsRequestProcessor);
HIncrbyRequestProcessor hIncrbyRequestProcessor = new HIncrbyRequestProcessor();
processorMap.put(hIncrbyRequestProcessor.getCmdCode().value(), hIncrbyRequestProcessor);
HIncrbyfloatRequestProcessor hIncrbyfloatRequestProcessor = new HIncrbyfloatRequestProcessor();
processorMap.put(hIncrbyfloatRequestProcessor.getCmdCode().value(), hIncrbyfloatRequestProcessor);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,7 @@ public HIncrbyRequestProcessor() {
@Override
public void handle(RemotingContext ctx, HIncrbyRequest request) {
Key key = new Key(AeroSpikeClientFactory.namespace, AeroSpikeClientFactory.set, request.getKey());
Object value;
if (request.getValue().contains(".")) {
value = Double.parseDouble(request.getValue());
} else {
value = Long.parseLong(request.getValue());
}
Bin bin = new Bin(request.getField(), value);
Bin bin = new Bin(request.getField(), Long.parseLong(request.getValue()));
client.operate(AeroSpikeClientFactory.eventLoops.next(), new RecordListener() {
@Override
public void onSuccess(Key key, Record record) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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 com.aerospike.client.AerospikeException;
import com.aerospike.client.Bin;
import com.aerospike.client.Key;
import com.aerospike.client.Operation;
import com.aerospike.client.Record;
import com.aerospike.client.listener.RecordListener;
import com.aerospike.client.policy.WritePolicy;
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.HIncrbyRequest;
import icu.funkye.redispike.protocol.request.HIncrbyfloatRequest;
import icu.funkye.redispike.util.IntegerUtils;

public class HIncrbyfloatRequestProcessor extends AbstractRedisRequestProcessor<HIncrbyfloatRequest> {
WritePolicy defaultWritePolicy;

public HIncrbyfloatRequestProcessor() {
this.cmdCode = new RedisRequestCommandCode(IntegerUtils.hashCodeToShort(HIncrbyfloatRequest.class.hashCode()));
this.defaultWritePolicy = client.getWritePolicyDefault();
this.defaultWritePolicy.sendKey = true;
}

@Override
public void handle(RemotingContext ctx, HIncrbyfloatRequest request) {
Key key = new Key(AeroSpikeClientFactory.namespace, AeroSpikeClientFactory.set, request.getKey());
Bin bin = new Bin(request.getField(), Double.parseDouble(request.getValue()));
client.operate(AeroSpikeClientFactory.eventLoops.next(), new RecordListener() {
@Override
public void onSuccess(Key key, Record record) {
Object value = record.getValue(request.getField());
if (value != null) {
request.setResponse(String.valueOf(value));
}
write(ctx, request);
}

@Override
public void onFailure(AerospikeException exception) {
logger.error(exception.getMessage(), exception);
write(ctx, request);
}
}, defaultWritePolicy, key, Operation.add(bin), Operation.get(request.getField()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,17 @@ public HSetRequestProcessor() {
public void handle(RemotingContext ctx, HSetRequest request) {
Key key = new Key(AeroSpikeClientFactory.namespace, AeroSpikeClientFactory.set, request.getKey());
List<Bin> list = new ArrayList<>();
request.getKv().forEach((k, v) -> list.add(new Bin(k, StringUtils.isNumeric(v)?Long.parseLong(v):v)));
request.getKv().forEach((k, v) -> {
Object value;
if (StringUtils.isNumeric(v)) {
value = Long.parseLong(v);
} else if (v.matches("-?\\d+(\\.\\d+)?")) {
value = Double.parseDouble(v);
} else {
value = v;
}
list.add(new Bin(k, value));
});
WritePolicy writePolicy;
if (request.getOperate() != null && request.getOperate() == Operate.NX) {
writePolicy = new WritePolicy(defaultWritePolicy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import icu.funkye.redispike.protocol.request.HGetAllRequest;
import icu.funkye.redispike.protocol.request.HGetRequest;
import icu.funkye.redispike.protocol.request.HIncrbyRequest;
import icu.funkye.redispike.protocol.request.HIncrbyfloatRequest;
import icu.funkye.redispike.protocol.request.HMgetRequest;
import icu.funkye.redispike.protocol.request.HSetRequest;
import icu.funkye.redispike.protocol.request.HValsRequest;
Expand Down Expand Up @@ -118,6 +119,8 @@ private AbstractRedisRequest<?> convert2RedisRequest(List<String> params, boolea
return new HGetRequest(params.get(1), params.size() > 2 ? params.get(2) : null, flush);
case "hincrby":
return new HIncrbyRequest(params.get(1), params.get(2), params.get(3), flush);
case "hincrbyfloat":
return new HIncrbyfloatRequest(params.get(1), params.get(2), params.get(3), flush);
case "hgetall":
return new HGetAllRequest(params.get(1), flush);
case "hvals":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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 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 HIncrbyfloatRequest extends AbstractRedisRequest<String> {

final String key;

final String field;

final String value;

BulkResponse response = new BulkResponse();

public HIncrbyfloatRequest(String key, String field, String value, boolean flush) {
this.flush = flush;
this.key = key;
this.value = value;
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 String getValue() {
return value;
}

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

}
7 changes: 7 additions & 0 deletions src/test/java/icu/funkye/redispike/ServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ public void testhHash() {
Assertions.assertEquals(result, 1);
result = jedis.hincrBy(key, "t", 5);
Assertions.assertEquals(result, 6);
result = jedis.hincrBy(key, "t", -1);
Assertions.assertEquals(result, 5);
jedis.del(key);
result = jedis.hset(key, "t", "0.0");
Assertions.assertEquals(result, 1);
Double res = jedis.hincrByFloat(key, "t", 5.1);
Assertions.assertEquals(res, 5.1);
jedis.del(key);
}
}
Expand Down