Skip to content

Commit

Permalink
FMWK-427 Qualifier builder API redesign (#744)
Browse files Browse the repository at this point in the history
* QualifierBuilder: replace binName, binType, ctx, key with path
* Add @beta annotation to QualifierBuilder
* Refactoring
  • Loading branch information
agrgr authored May 26, 2024
1 parent caa1311 commit dae6842
Show file tree
Hide file tree
Showing 38 changed files with 728 additions and 513 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.springframework.data.aerospike.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Marks public API as potentially volatile, i.e. it may be a subject of incompatible changes in future releases.
*/
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.TYPE, ElementType.CONSTRUCTOR, ElementType.METHOD})
public @interface Beta {

}
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@
* <td> a.55 </td> <td> [mapKey("a"), mapKey(55)] </td>
* </tr>
* <tr>
* <td> a.aa.{=222} </td> <td> [mapKey("a"), mapKey("aa"),mapValue(222)] </td>
* <td> a.aa.{=222} </td> <td> [mapKey("a"), mapKey("aa"), mapValue(222)] </td>
* </tr>
* <tr>
* <td> ab.cd.[-1]."10" </td> <td> [mapKey("ab"), mapKey("cd"),listIndex(-1), mapKey("10")] </td>
* <td> ab.cd.[-1]."10" </td> <td> [mapKey("ab"), mapKey("cd"), listIndex(-1), mapKey("10")] </td>
* </tr>
* </table>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@

import com.aerospike.client.Value;
import com.aerospike.client.cdt.CTX;
import com.aerospike.client.exp.Exp;

public class AerospikeIndexResolverUtils {
import java.util.List;

import static org.springframework.data.aerospike.index.AerospikeContextDslResolverUtils.CtxType.LIST_INDEX;
import static org.springframework.data.aerospike.index.AerospikeContextDslResolverUtils.CtxType.LIST_RANK;
import static org.springframework.data.aerospike.index.AerospikeContextDslResolverUtils.CtxType.LIST_VALUE;
import static org.springframework.data.aerospike.index.AerospikeContextDslResolverUtils.CtxType.MAP_INDEX;
import static org.springframework.data.aerospike.index.AerospikeContextDslResolverUtils.CtxType.MAP_KEY;
import static org.springframework.data.aerospike.index.AerospikeContextDslResolverUtils.CtxType.MAP_RANK;
import static org.springframework.data.aerospike.index.AerospikeContextDslResolverUtils.CtxType.MAP_VALUE;

public class AerospikeContextDslResolverUtils {

public static CTX toCtx(String singleCtx) {
switch (singleCtx.charAt(0)) {
Expand All @@ -24,12 +35,11 @@ public static CTX toCtx(String singleCtx) {
private static CTX processSingleCtx(String singleCtx, AerospikeIndexResolver.CtxType ctxType) {
int length = singleCtx.length();
if (length < 3) {
throw new IllegalArgumentException("@Indexed annotation: context string '" + singleCtx +
"' has no content");
throw new IllegalArgumentException(String.format("Context DSL: string '%s' has no content", singleCtx));
}
if (singleCtx.charAt(length - 1) != ctxType.closingChar) {
throw new IllegalArgumentException("@Indexed annotation: brackets mismatch, " +
"expecting '" + ctxType.closingChar + "', got '" + singleCtx.charAt(length - 1) + "' instead");
throw new IllegalArgumentException(String.format("Context DSL: brackets mismatch, expecting '%s', " +
"got '%s' instead", ctxType.closingChar, singleCtx.charAt(length - 1)));
}

CTX result;
Expand Down Expand Up @@ -75,8 +85,8 @@ private static int parseIntOrFail(String substring, AerospikeIndexResolver.CtxTy
try {
return Integer.parseInt(substring);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("@Indexed annotation " + ctxType + " " + parameterName + ": " +
"expecting only integer values, got '" + substring + "' instead");
throw new IllegalArgumentException(String.format("Context DSL %s %s: expecting only integer values, " +
"got '%s' instead", ctxType, parameterName, substring));
}
}

Expand All @@ -95,4 +105,53 @@ private static boolean isInDoubleOrSingleQuotes(String str) {
return str.length() > 2 && (str.charAt(0) == '"' || str.charAt(0) == '\'')
&& (str.charAt(str.length() - 1) == '"' || str.charAt(str.length() - 1) == '\'');
}

/**
* Check whether context element's id is the same as in {@link CTX#mapKey(Value)}
*/
public static boolean isCtxMapKey(CTX ctx) {
return ctx.id == MAP_KEY.getId();
}

/**
* Check whether context element's id is the same as in {@link CTX#mapValue(Value)}
*/
public static boolean isCtxMapValue(CTX ctx) {
return ctx.id == MAP_VALUE.getId();
}

/**
* Check whether context element's id is the same as in {@link CTX#mapKey(Value)}
*/
public static Exp.Type getCtxType(CTX ctx) {
List<Integer> listIds = List.of(LIST_INDEX.getId(), LIST_RANK.getId(), LIST_VALUE.getId());
List<Integer> mapIds = List.of(MAP_INDEX.getId(), MAP_RANK.getId(), MAP_KEY.getId(), MAP_VALUE.getId());
if (listIds.contains(ctx.id)) {
return Exp.Type.LIST;
} else if (mapIds.contains(ctx.id)) {
return Exp.Type.MAP;
} else {
throw new IllegalStateException("Unexpected CTX element id");
}
}

enum CtxType {
LIST_INDEX(0x10),
LIST_RANK(0x11),
LIST_VALUE(0x13),
MAP_INDEX(0x20),
MAP_RANK(0x21),
MAP_KEY(0x22),
MAP_VALUE(0x23);

private final int id;

CtxType(int id) {
this.id = id;
}

public int getId() {
return id;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private CTX[] toCtxArray(String ctxString) {
String[] ctxTokens = ctxString.split("\\.");
CTX[] ctxArr = Arrays.stream(ctxTokens)
.filter(not(String::isEmpty))
.map(AerospikeIndexResolverUtils::toCtx)
.map(AerospikeContextDslResolverUtils::toCtx)
.filter(Objects::nonNull)
.toArray(CTX[]::new);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class ExpiryQualifier extends Qualifier {

public ExpiryQualifier(FilterOperation op, Value value) {
super(Qualifier.builder()
.setBinName(QueryEngine.Meta.EXPIRATION.toString())
.setPath(QueryEngine.Meta.EXPIRATION.toString())
.setFilterOperation(op)
.setValue(value)
);
Expand Down
Loading

0 comments on commit dae6842

Please sign in to comment.