Skip to content

Commit

Permalink
FMWK-146 Code format (#92)
Browse files Browse the repository at this point in the history
Co-authored-by: yrizhkov <[email protected]>
  • Loading branch information
agrgr and reugn authored Feb 20, 2023
1 parent 8fa31ba commit 9bcce57
Show file tree
Hide file tree
Showing 20 changed files with 193 additions and 104 deletions.
20 changes: 20 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
charset = utf-8
end_of_line = lf

[*.java]
indent_style = space
indent_size = 4
insert_final_newline = true
max_line_length = 120
ij_continuation_indent_size = 8
ij_java_wrap_long_lines = true
ij_java_wrap_comments = true
ij_java_method_call_chain_wrap = normal
ij_java_blank_lines_after_class_header = 1
ij_java_class_count_to_use_import_on_demand = 10
ij_java_names_count_to_use_import_on_demand = 10
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public static DocumentApiException wrapAerospikeException(AerospikeException ae)
}

/**
* Utility method to wrap an Exception.
* {@link AerospikeException} is processed separately to display its result code name.
* Utility method to wrap an Exception. {@link AerospikeException} is processed separately to display its result
* code name.
*
* @param e the original exception.
* @return a DocumentApiException wrapping the original exception.
Expand All @@ -41,6 +41,7 @@ public static DocumentApiException toDocumentException(Exception e) {
* Exception to be thrown in case of invalid json prefix.
*/
public static class JsonPrefixException extends DocumentApiException {

public JsonPrefixException(String jsonString) {
super(String.format("'%s' should start with either a '$.' or '$['", jsonString));
}
Expand All @@ -50,6 +51,7 @@ public JsonPrefixException(String jsonString) {
* Exception to be thrown in case of invalid json path.
*/
public static class JsonPathException extends DocumentApiException {

public JsonPathException(String jsonString) {
super(String.format("'%s' does not match JSONPath format", jsonString));
}
Expand All @@ -59,6 +61,7 @@ public JsonPathException(String jsonString) {
* Exception to be thrown in case of invalid appending to json.
*/
public static class JsonAppendException extends DocumentApiException {

public JsonAppendException(String jsonString) {
super(String.format("Cannot append to '%s'", jsonString));
}
Expand All @@ -68,6 +71,7 @@ public JsonAppendException(String jsonString) {
* Exception to be thrown in case of invalid json path.
*/
public static class JsonPathParseException extends DocumentApiException {

public JsonPathParseException(String jsonPathPart) {
super(String.format("Unable to parse '%s' as JSONPath token", jsonPathPart));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ public interface IAerospikeDocumentClient {

/**
* Perform batch operations.
*
* <p>Operations order is preserved only for those 1-step operations
* (with JSONPath that contains only array and/or map elements)
* that have unique Aerospike keys within a batch.</p>
* <p>Every 2-step operation (with JSONPath containing wildcards, recursive descent, filters, functions, scripts)
* <p>
* Operations order is preserved only for those 1-step operations (with JSONPath that contains only array and/or map
* elements) that have unique Aerospike keys within a batch.
* <p>
* Every 2-step operation (with JSONPath containing wildcards, recursive descent, filters, functions, scripts)
* should have unique Aerospike key within a batch.
*
* @param batchOperations a list of batch operations to apply.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ Map<String, Object> get(Policy readPolicy, Key key, Collection<String> binNames,

void put(WritePolicy writePolicy, Key key, String binName, Map<?, ?> jsonMap);

void put(WritePolicy writePolicy, Key key, Collection<String> binNames, Object jsonObject, JsonPathObject jsonPathObject);
void put(WritePolicy writePolicy, Key key, Collection<String> binNames, Object jsonObject,
JsonPathObject jsonPathObject);

void put(WritePolicy writePolicy, Key key, Map<String, Object> queryResults, JsonPathObject jsonPathObject);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,16 @@ public BatchRecord setSecondStepRecordAndGet() {
} else {
if (isRequiringJsonPathQuery()) {
// using the original object as the initially parsed one has already been changed within the 1st step
final PathDetails pathDetails = getPathDetails(originalJsonPathObject.getTokensNotRequiringSecondStepQuery(), true);
final PathDetails pathDetails =
getPathDetails(originalJsonPathObject.getTokensNotRequiringSecondStepQuery(), true);
batchOps = firstStepQueryResults().entrySet().stream()
.map(entry -> toPutOperation(entry.getKey(), entry.getValue(), pathDetails))
.filter(Objects::nonNull)
.toArray(Operation[]::new);
} else {
// needs to be treated without modifying
final PathDetails pathDetails = getPathDetails(jsonPathObject.getTokensNotRequiringSecondStepQuery(), false);
final PathDetails pathDetails = getPathDetails(jsonPathObject.getTokensNotRequiringSecondStepQuery(),
false);
batchOps = binNames.stream()
.map(binName -> toAppendOperation(binName, objToAppend, pathDetails))
.filter(Objects::nonNull)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ public BatchRecord setSecondStepRecordAndGet() {
} else {
if (isRequiringJsonPathQuery()) {
// using the original object as the initially parsed one has already been changed within the 1st step
final PathDetails pathDetails = getPathDetails(originalJsonPathObject.getTokensNotRequiringSecondStepQuery(), true);
final PathDetails pathDetails =
getPathDetails(originalJsonPathObject.getTokensNotRequiringSecondStepQuery(), true);
batchOps = firstStepQueryResults().entrySet().stream()
.map(entry -> pathDetails.getFinalToken()
.toAerospikePutOperation(entry.getKey(), entry.getValue(), pathDetails.getCtxArray()))
.toArray(Operation[]::new);
} else {
final PathDetails pathDetails = getPathDetails(jsonPathObject.getTokensNotRequiringSecondStepQuery(), true);
final PathDetails pathDetails = getPathDetails(jsonPathObject.getTokensNotRequiringSecondStepQuery(),
true);
batchOps = binNames.stream()
.map(binName -> pathDetails.getFinalToken()
.toAerospikeDeleteOperation(binName, pathDetails.getCtxArray()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ public BatchRecord setSecondStepRecordAndGet() {
} else {
if (isRequiringJsonPathQuery()) {
// using the original object as the initially parsed one has already been changed within the 1st step
final PathDetails pathDetails = getPathDetails(originalJsonPathObject.getTokensNotRequiringSecondStepQuery(), true);
final PathDetails pathDetails =
getPathDetails(originalJsonPathObject.getTokensNotRequiringSecondStepQuery(), true);
batchOps = firstStepQueryResults().entrySet().stream()
.map(entry -> toPutOperation(entry.getKey(), entry.getValue(), pathDetails))
.filter(Objects::nonNull)
.toArray(Operation[]::new);
} else {
final PathDetails pathDetails = getPathDetails(jsonPathObject.getTokensNotRequiringSecondStepQuery(), true);
final PathDetails pathDetails = getPathDetails(jsonPathObject.getTokensNotRequiringSecondStepQuery(),
true);
batchOps = binNames.stream()
.map(binName -> toPutOperation(binName, objToPut, pathDetails))
.filter(Objects::nonNull)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public static ContextAwareToken extractLastPathPartAndModifyList(List<ContextAwa
}

/**
* Given a list of tokens, convert this to the list of contexts you would need
* to retrieve the JSON path represented by the list of tokens.
* Given a list of tokens, convert this to the list of contexts you would need to retrieve the JSON path represented
* by the list of tokens.
*
* @param tokens tokens list to convert.
* @return an array of contexts (CTXs).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@Value
@RequiredArgsConstructor
public class PathDetails {

ContextAwareToken finalToken;
CTX[] ctxArray;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@Value
@Builder
public class DocumentPolicy {

Policy readPolicy;
WritePolicy writePolicy;
BatchPolicy batchPolicy;
Expand Down
80 changes: 40 additions & 40 deletions src/main/java/com/aerospike/documentapi/token/ListToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,53 @@
*/
public class ListToken extends ContextAwareToken {

static final Pattern PATH_PATTERN = Pattern.compile("^([^\\[^\\]]*)(\\[([\\*\\d]+)\\])*$");
static final Pattern INDEX_PATTERN = Pattern.compile("(\\[([\\*\\d]+)\\])");

private final int listPosition;

public ListToken(int listPosition) {
this.listPosition = listPosition;
setString(OPEN_BRACKET + String.valueOf(listPosition) + CLOSE_BRACKET);
}

static final Pattern PATH_PATTERN = Pattern.compile("^([^\\[^\\]]*)(\\[([\\*\\d]+)\\])*$");
static final Pattern INDEX_PATTERN = Pattern.compile("(\\[([\\*\\d]+)\\])");
// For reading a path part into a list of tokens (map, list or wildcard).
// Appends parsed tokens to a list.
// Expected form of path part is key[index1][index2].
public static List<Token> parseToList(String strPart) {
List<Token> list = new ArrayList<>();
Token token;

Matcher keyMatcher = PATH_PATTERN.matcher(strPart);
if ((!strPart.contains("[")) && (!strPart.contains("]"))) {
// ignoring * wildcard after a dot
if (!strPart.equals(String.valueOf(WILDCARD)) && !strPart.equals(String.valueOf(DOC_ROOT))) {
token = new MapToken(strPart);
list.add(token);
}
} else if (keyMatcher.find()) {
String key = keyMatcher.group(1);
if (!key.equals(String.valueOf(DOC_ROOT))
&& key.length() > 0
&& key.charAt(0) != OPEN_BRACKET && key.charAt(key.length() - 1) != CLOSE_BRACKET) {
token = new MapToken(key);
list.add(token);
}
}

Matcher indexMatcher = INDEX_PATTERN.matcher(strPart);
while (indexMatcher.find()) {
String res = indexMatcher.group(2);
if (res.equals("*")) {
token = new WildcardToken(res, true);
} else {
token = new ListToken(Integer.parseInt(res));
}
list.add(token);
}

return list;
}

public int getListPosition() {
return listPosition;
Expand Down Expand Up @@ -81,42 +119,4 @@ public Operation toAerospikeDeleteOperation(String binName, CTX[] contexts) {
public TokenType getType() {
return TokenType.LIST;
}

// For reading a path part into a list of tokens (map, list or wildcard).
// Appends parsed tokens to a list.
// Expected form of path part is key[index1][index2].
public static List<Token> parseToList(String strPart) {
List<Token> list = new ArrayList<>();
Token token;

Matcher keyMatcher = PATH_PATTERN.matcher(strPart);
if ((!strPart.contains("[")) && (!strPart.contains("]"))) {
// ignoring * wildcard after a dot
if (!strPart.equals(String.valueOf(WILDCARD)) && !strPart.equals(String.valueOf(DOC_ROOT))) {
token = new MapToken(strPart);
list.add(token);
}
} else if (keyMatcher.find()) {
String key = keyMatcher.group(1);
if (!key.equals(String.valueOf(DOC_ROOT))
&& key.length() > 0
&& key.charAt(0) != OPEN_BRACKET && key.charAt(key.length() - 1) != CLOSE_BRACKET) {
token = new MapToken(key);
list.add(token);
}
}

Matcher indexMatcher = INDEX_PATTERN.matcher(strPart);
while (indexMatcher.find()) {
String res = indexMatcher.group(2);
if (res.equals("*")) {
token = new WildcardToken(res, true);
} else {
token = new ListToken(Integer.parseInt(res));
}
list.add(token);
}

return list;
}
}
43 changes: 21 additions & 22 deletions src/main/java/com/aerospike/documentapi/token/MapToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,34 @@
*/
public class MapToken extends ContextAwareToken {

static final Pattern PATH_PATTERN = Pattern.compile("^([^\\[^\\]]*)(\\[(\\d+)\\])*$");

private final String key;

public MapToken(String key) {
this.key = key;
setString(key);
}

static final Pattern PATH_PATTERN = Pattern.compile("^([^\\[^\\]]*)(\\[(\\d+)\\])*$");
public static Optional<Token> match(String strPart) {
Token token;
Matcher keyMatcher = PATH_PATTERN.matcher(strPart);
if ((!strPart.contains("[")) && (!strPart.contains("]"))) {
// ignoring * wildcard after a dot, it's the same as ending with a .path
if (!strPart.equals(String.valueOf(WILDCARD)) && !strPart.equals(String.valueOf(DOC_ROOT))) {
token = new MapToken(strPart);
return Optional.of(token);
}
} else if (keyMatcher.find()) {
String key = keyMatcher.group(1);
if (!key.equals(String.valueOf(DOC_ROOT))) {
token = new MapToken(strPart);
return Optional.of(token);
}
}

return Optional.empty();
}

public String getKey() {
return key;
Expand Down Expand Up @@ -78,25 +98,4 @@ public Operation toAerospikeDeleteOperation(String binName, CTX[] contexts) {
public TokenType getType() {
return TokenType.MAP;
}

public static Optional<Token> match(String strPart) {
Token token;

Matcher keyMatcher = PATH_PATTERN.matcher(strPart);
if ((!strPart.contains("[")) && (!strPart.contains("]"))) {
// ignoring * wildcard after a dot, it's the same as ending with a .path
if (!strPart.equals(String.valueOf(WILDCARD)) && !strPart.equals(String.valueOf(DOC_ROOT))) {
token = new MapToken(strPart);
return Optional.of(token);
}
} else if (keyMatcher.find()) {
String key = keyMatcher.group(1);
if (!key.equals(String.valueOf(DOC_ROOT))) {
token = new MapToken(strPart);
return Optional.of(token);
}
}

return Optional.empty();
}
}
16 changes: 8 additions & 8 deletions src/main/java/com/aerospike/documentapi/token/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ public abstract class Token {
private String string;
private String queryConcatString;

public String getString() {
return string;
}

protected void setString(String string) {
this.string = string;
this.queryConcatString = string;
}

protected void setQueryConcatString(String queryConcatString) {
this.queryConcatString = queryConcatString;
}

public String getString() {
return string;
}

public String getQueryConcatString() {
return queryConcatString;
}

protected void setQueryConcatString(String queryConcatString) {
this.queryConcatString = queryConcatString;
}

public abstract TokenType getType();

public boolean requiresJsonQuery() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public static List<Object> convertJsonNodeToList(JsonNode jsonNode) {
}

/**
* Given an object that represents a list or a map for example an Aerospike database result,
* return its equivalent representation as a Json string.
* Given an object that represents a list or a map for example an Aerospike database result, return its equivalent
* representation as a Json string.
*
* @param object the object to parse as a Json string.
* @return the JSON string representation of the object.
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/aerospike/documentapi/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public static Bin createBin(String binName, Object jsonObject) {

public static void validateNotArray(Object object) throws IllegalArgumentException {
if (object.getClass().isArray()) {
throw new IllegalArgumentException("Putting/appending an array is not allowed, consider providing a Collection");
throw new IllegalArgumentException("Putting/appending an array is not allowed, consider providing a " +
"Collection");
}
}

Expand Down
Loading

0 comments on commit 9bcce57

Please sign in to comment.