-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: support hmget protocol (#23)
- Loading branch information
1 parent
4b588f0
commit dc0bad9
Showing
6 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ target/ | |
*.bak | ||
*.log* | ||
data/ | ||
*.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/java/icu/funkye/redispike/handler/process/impl/HMgetRequestProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/main/java/icu/funkye/redispike/protocol/request/HMgetRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + '}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters