use of org.apache.geode.pdx.internal.PdxString in project geode by apache.
the class CompiledLike method evaluate.
@Override
public Object evaluate(ExecutionContext context) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
// reset the isIndexEvaluated flag here since index is not being used here
context.cachePut(isIndexEvaluatedKey, false);
Pattern pattern = (Pattern) context.cacheGet(this.bindArg);
if (pattern == null) {
// handles both Strings and
String strPattern = this.bindArg.evaluate(context).toString();
// PdxStrings
if (strPattern == null) {
throw new UnsupportedOperationException("Null values are not supported with LIKE predicate.");
}
pattern = Pattern.compile(getRegexPattern(strPattern), Pattern.MULTILINE | Pattern.DOTALL);
context.cachePut(this.bindArg, pattern);
}
Object value = this.var.evaluate(context);
if (value == null) {
return null;
}
if (!((value instanceof String) || (value instanceof PdxString) || (value == QueryService.UNDEFINED))) {
// .toLocalizedString("java.lang.String", value.getClass().getName()));
if (getOperator() == TOK_NE) {
return true;
}
return false;
}
// Check if LIKE clause is negated (_operator == TOK_NE) in query.
boolean isMatched = pattern.matcher(value.toString()).matches();
if (getOperator() == TOK_NE) {
isMatched = !isMatched;
}
return isMatched;
}
use of org.apache.geode.pdx.internal.PdxString in project geode by apache.
the class OrderByComparator method evaluateSortCriteria.
protected int evaluateSortCriteria(Object value1, Object value2) {
int result = -1;
CompiledSortCriterion csc;
if (orderByAttrs != null) {
Iterator orderiter = orderByAttrs.iterator();
while (orderiter.hasNext()) {
csc = (CompiledSortCriterion) orderiter.next();
Object sortCriteriaForValue1 = csc.evaluate(value1, context);
Object sortCriteriaForValue2 = csc.evaluate(value2, context);
if (sortCriteriaForValue1 == null || sortCriteriaForValue2 == null) {
if (sortCriteriaForValue1 == null) {
result = (sortCriteriaForValue2 == null ? 0 : -1);
} else {
result = 1;
}
} else if (sortCriteriaForValue1 == QueryService.UNDEFINED || sortCriteriaForValue2 == QueryService.UNDEFINED) {
if (sortCriteriaForValue1 == QueryService.UNDEFINED) {
result = (sortCriteriaForValue2 == QueryService.UNDEFINED ? 0 : -1);
} else {
result = 1;
}
} else {
if (sortCriteriaForValue1 instanceof Number && sortCriteriaForValue2 instanceof Number) {
double diff = ((Number) sortCriteriaForValue1).doubleValue() - ((Number) sortCriteriaForValue2).doubleValue();
result = diff > 0 ? 1 : diff < 0 ? -1 : 0;
} else {
if (sortCriteriaForValue1 instanceof PdxString && sortCriteriaForValue2 instanceof String) {
sortCriteriaForValue2 = new PdxString((String) sortCriteriaForValue2);
} else if (sortCriteriaForValue2 instanceof PdxString && sortCriteriaForValue1 instanceof String) {
sortCriteriaForValue1 = new PdxString((String) sortCriteriaForValue1);
}
result = ((Comparable) sortCriteriaForValue1).compareTo(sortCriteriaForValue2);
}
}
if (result == 0) {
continue;
} else {
if (Boolean.valueOf(csc.getCriterion())) {
result = (result * (-1));
}
break;
}
}
}
return result;
}
use of org.apache.geode.pdx.internal.PdxString in project geode by apache.
the class OrderByComparator method compare.
/**
* Compares its two arguments for order. Returns a negative integer, zero, or a positive integer
* as the first argument is less than, equal to, or greater than the second.
*
* @param obj1 the first object to be compared.
* @param obj2 the second object to be compared.
* @return a negative integer, zero, or a positive integer as the first argument is less than,
* equal to, or greater than the second.
* @throws ClassCastException if the arguments' types prevent them from being compared by this
* Comparator.
*/
public int compare(Object obj1, Object obj2) {
int result = -1;
if (obj1 == null && obj2 == null) {
return 0;
}
assert !(obj1 instanceof VMCachedDeserializable || obj2 instanceof VMCachedDeserializable);
if ((this.objType.isStructType() && obj1 instanceof Object[] && obj2 instanceof Object[]) || !this.objType.isStructType()) {
// instanceof Object){
if ((result = evaluateSortCriteria(obj1, obj2)) != 0) {
return result;
}
QueryObserver observer = QueryObserverHolder.getInstance();
if (observer != null) {
observer.orderByColumnsEqual();
}
// equal or not
if (this.objType.isStructType()) {
int i = 0;
for (Object o1 : (Object[]) obj1) {
Object o2 = ((Object[]) obj2)[i++];
// Check for null value.
if (o1 == null || o2 == null) {
if (o1 == null) {
if (o2 == null) {
continue;
}
return -1;
} else {
return 1;
}
} else if (o1 == QueryService.UNDEFINED || o2 == QueryService.UNDEFINED) {
if (o1 == QueryService.UNDEFINED) {
if (o2 == QueryService.UNDEFINED) {
continue;
}
return -1;
} else {
return 1;
}
}
if (o1 instanceof Comparable) {
final int rslt;
if (o1 instanceof Number && o2 instanceof Number) {
double diff = ((Number) o1).doubleValue() - ((Number) o2).doubleValue();
rslt = diff > 0 ? 1 : diff < 0 ? -1 : 0;
} else {
if (o1 instanceof PdxString && o2 instanceof String) {
o2 = new PdxString((String) o2);
} else if (o2 instanceof PdxString && o1 instanceof String) {
o1 = new PdxString((String) o1);
}
rslt = ((Comparable) o1).compareTo(o2);
}
if (rslt == 0) {
continue;
} else {
return rslt;
}
} else if (!o1.equals(o2)) {
return -1;
}
}
return 0;
} else {
if (obj1 instanceof PdxString && obj2 instanceof String) {
obj2 = new PdxString((String) obj2);
} else if (obj2 instanceof PdxString && obj1 instanceof String) {
obj1 = new PdxString((String) obj1);
}
if (obj1 instanceof Comparable) {
return ((Comparable) obj1).compareTo(obj2);
} else {
return obj1.equals(obj2) ? 0 : -1;
}
}
}
return -1;
}
use of org.apache.geode.pdx.internal.PdxString in project geode by apache.
the class PdxStringJUnitTest method testCompareTo.
@Test
public void testCompareTo() throws Exception {
PdxInstanceFactory pf = PdxInstanceFactoryImpl.newCreator("Portfolio", false);
pf.writeString("secId", "abc");
PdxInstanceImpl pi = (PdxInstanceImpl) pf.create();
PdxString pdx1 = (PdxString) pi.getRawField("secId");
PdxString pdx2 = new PdxString("abc");
assertEquals(pdx1.compareTo(pdx2), 0);
pdx2 = new PdxString("ABC");
// a - A = 32
assertEquals(pdx1.compareTo(pdx2), 32);
String str1 = new String("A" + "é" + "ñ");
String str2 = new String("A" + "ê" + "ñ");
pf = PdxInstanceFactoryImpl.newCreator("Portfolio", false);
pf.writeString("secId", str1);
pi = (PdxInstanceImpl) pf.create();
pdx1 = (PdxString) pi.getRawField("secId");
pdx2 = new PdxString(str2);
// str1 < str2
assertEquals(-1, pdx1.compareTo(pdx2));
// test compareTo for a huge string and small string
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 200000; i++) {
sb.append("a");
}
str1 = sb.toString();
str2 = "aaa";
pf = PdxInstanceFactoryImpl.newCreator("Portfolio", false);
pf.writeString("secId", str1);
pi = (PdxInstanceImpl) pf.create();
pdx1 = (PdxString) pi.getRawField("secId");
pdx2 = new PdxString(str2);
// str1 > str2 so positive result
assertTrue(pdx1.compareTo(pdx2) > 0);
sb = null;
str1 = null;
// huge utf8 string and compareto
sb = new StringBuilder();
for (int i = 0; i < 65535; i++) {
sb.append("é");
}
str1 = sb.toString();
str2 = "abc";
pf = PdxInstanceFactoryImpl.newCreator("Portfolio", false);
pf.writeString("secId", str1);
pi = (PdxInstanceImpl) pf.create();
pdx1 = new PdxString(str1);
pdx2 = new PdxString(str2);
// str1 > str2 so positive result
assertTrue(pdx1.compareTo(pdx2) > 0);
sb = null;
str1 = null;
}
use of org.apache.geode.pdx.internal.PdxString in project geode by apache.
the class PdxStringJUnitTest method testJSONFieldNameAsPdxString.
/**
* this test verifies that pdxstream with pdxType.Object tyep for string and checks whether we
* create PdxString or not
*
* @throws Exception
*/
@Test
public void testJSONFieldNameAsPdxString() throws Exception {
String verifyString = "ValueExist";
String jsonString = "{name:\"" + verifyString + "\", age:14}";
PdxString pdx = new PdxString(verifyString);
assertEquals(verifyString, pdx.toString());
PdxInstanceImpl pi = (PdxInstanceImpl) JSONFormatter.fromJSON(jsonString);
pdx = (PdxString) pi.getRawField("name");
assertEquals(verifyString, pdx.toString());
}
Aggregations