Skip to content

Commit

Permalink
set userKey parameter as required in batch read operations #31
Browse files Browse the repository at this point in the history
  • Loading branch information
reugn committed Apr 15, 2021
1 parent 027746e commit ed7a2fc
Show file tree
Hide file tree
Showing 6 changed files with 662 additions and 683 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,20 @@
import com.aerospike.client.BatchRead;
import com.aerospike.restclient.util.RestClientErrors;
import com.fasterxml.jackson.annotation.JsonProperty;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel(value="BatchReadRequest")
public class RestClientBatchReadBody {


@ApiModelProperty(value="Key to retrieve a record", required=true)
@ApiModelProperty(value="Key to retrieve a record.", required=true)
@JsonProperty(required=true)
public RestClientKey key;

@ApiModelProperty(value="Whether all bins should be returned with this record")
@ApiModelProperty(value="Whether all bins should be returned with this record.")
public boolean readAllBins;

@ApiModelProperty(value="List of bins to limit the record response to.", required=false)
@ApiModelProperty(value="List of bins to limit the record response to.")
public String[] binNames;

public RestClientBatchReadBody() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@
package com.aerospike.restclient.domain;

import com.aerospike.client.BatchRead;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel(value="BatchRead")
public class RestClientBatchReadResponse {


public RestClientBatchReadResponse() {}

public RestClientBatchReadResponse(BatchRead batchRead) {
Expand All @@ -46,6 +44,4 @@ record = batchRead.record != null ? new RestClientRecord(batchRead.record) : nul
@ApiModelProperty(value="List of bins to limit the record response to.")
public String[] binNames;



}
27 changes: 12 additions & 15 deletions src/main/java/com/aerospike/restclient/domain/RestClientKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
*/
package com.aerospike.restclient.domain;

import java.util.Base64;
import java.util.Base64.Encoder;

import com.aerospike.client.Key;
import com.aerospike.client.Value.BytesValue;
import com.aerospike.client.Value.IntegerValue;
Expand All @@ -27,29 +24,31 @@
import com.aerospike.restclient.util.AerospikeAPIConstants.RecordKeyType;
import com.aerospike.restclient.util.KeyBuilder;
import com.fasterxml.jackson.annotation.JsonProperty;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.Base64;
import java.util.Base64.Encoder;

@ApiModel(value="Key")
public class RestClientKey {

@ApiModelProperty(required=true, example="testNS")
public String namespace;

@JsonProperty(value="setName")
@ApiModelProperty(example="testSet")
public String setName;

@JsonProperty(required=false, value="digest")
@ApiModelProperty(value="URL safe base64 encoded key digest. Returned by the server on batch responses. May be provided by client.",
example="AAAAAAAAAAAAAAAAAAAAAAAAAAA=")
public String digest;

@JsonProperty(value="keytype")
@ApiModelProperty(value="Enum describing the type of the userKey. This field is omitted in MessagePack responses.", example="STRING")
@ApiModelProperty(
value="Enum describing the type of the userKey. This field is omitted in MessagePack responses.",
example="STRING")
public RecordKeyType keytype;

@ApiModelProperty(value="The user key, it may be a string, integer, or URL safe Base64 encoded bytes.", example="userKey")
@ApiModelProperty(required=true,
value="The user key, it may be a string, integer, or URL safe Base64 encoded bytes.",
example="userKey")
public Object userKey;

public RestClientKey() {}
Expand All @@ -58,14 +57,13 @@ public RestClientKey(Key realKey) {
Encoder encoder = Base64.getUrlEncoder();
namespace = realKey.namespace;
setName = realKey.setName;
digest = encoder.encodeToString(realKey.digest);

if (realKey.userKey != null) {
if (realKey.userKey instanceof StringValue) {
userKey = realKey.userKey.toString();
keytype = RecordKeyType.STRING;
} else if (realKey.userKey instanceof IntegerValue || realKey.userKey instanceof LongValue) {
userKey = (long)realKey.userKey.getObject();
userKey = realKey.userKey.getObject();
keytype = RecordKeyType.INTEGER;
} else if(realKey.userKey instanceof BytesValue) {
userKey = encoder.encodeToString((byte[])realKey.userKey.getObject());
Expand All @@ -75,10 +73,9 @@ public RestClientKey(Key realKey) {
userKey = encoder.encodeToString(realKey.digest);
keytype = RecordKeyType.DIGEST;
}
digest = encoder.encodeToString(realKey.digest);
}

public Key toKey() {
return KeyBuilder.buildKey(namespace, setName, userKey.toString(), keytype);
}
}
}
11 changes: 5 additions & 6 deletions src/main/java/com/aerospike/restclient/util/KeyBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
*/
package com.aerospike.restclient.util;

import java.util.Base64;

import com.aerospike.client.Key;
import com.aerospike.restclient.util.AerospikeAPIConstants.RecordKeyType;

import java.util.Base64;

public class KeyBuilder {

public static Key buildKey(String namespace, String set, String strKey, RecordKeyType keyType) {
Expand All @@ -38,13 +38,12 @@ public static Key buildKey(String namespace, String set, String strKey, RecordKe
case DIGEST: {
byte[] digestBytes = getByteArrayFromstring(strKey);
if (digestBytes.length != 20) {
throw new RestClientErrors.InvalidKeyError(String.format("Digest must be 20 bytes long", strKey));
throw new RestClientErrors.InvalidKeyError(String.format("Digest must be 20 bytes long: %s", strKey));
}
return new Key(namespace, digestBytes, set, null);
}
default:
throw new RestClientErrors.InvalidKeyError(String.format("Invalid keytype value: %s", keyType.toString()));

throw new RestClientErrors.InvalidKeyError(String.format("Invalid keyType value: %s", keyType.toString()));
}
}

Expand All @@ -68,7 +67,7 @@ private static byte[] getByteArrayFromstring(String strKey) {
try {
return Base64.getUrlDecoder().decode(strKey);
} catch (IllegalArgumentException nfe) {
throw new RestClientErrors.InvalidKeyError(String.format("Unable to decode bytes", strKey));
throw new RestClientErrors.InvalidKeyError(String.format("Unable to decode bytes: %s", strKey));
}
}

Expand Down
Loading

0 comments on commit ed7a2fc

Please sign in to comment.