public List<Map<String, Object>> execute(String columnNames[]) { List<Map<String, Object>> combinedRowList = null; Set<NoSqlPredicate> inefficientPredicates = new HashSet<NoSqlPredicate>(); for (NoSqlPredicate predicate : predicateList) { if (predicate.canExecuteEfficiently()) { List<Map<String, Object>> rowList = predicate.execute(columnNames); if (combinedRowList != null) { String primaryKeyName = storageSource.getTablePrimaryKeyName(tableName); combinedRowList = combineRowLists(primaryKeyName, combinedRowList, rowList, operator); } else { combinedRowList = rowList; } } else { inefficientPredicates.add(predicate); } } if (inefficientPredicates.isEmpty()) return combinedRowList; List<Map<String, Object>> filteredRowList = new ArrayList<Map<String, Object>>(); for (Map<String, Object> row : combinedRowList) { for (NoSqlPredicate predicate : inefficientPredicates) { if (predicate.matchesRow(row)) { filteredRowList.add(row); } } } return filteredRowList; }
public boolean matchesRow(Map<String, Object> row) { if (operator == CompoundPredicate.Operator.AND) { for (NoSqlPredicate predicate : predicateList) { if (!predicate.matchesRow(row)) { return false; } } return true; } else { for (NoSqlPredicate predicate : predicateList) { if (predicate.matchesRow(row)) { return true; } } return false; } }
public boolean canExecuteEfficiently() { if (operator == CompoundPredicate.Operator.AND) { for (NoSqlPredicate predicate : predicateList) { if (predicate.canExecuteEfficiently()) { return true; } } return false; } else { for (NoSqlPredicate predicate : predicateList) { if (!predicate.canExecuteEfficiently()) { return false; } } return true; } }
private NoSqlResultSet executeParameterizedQuery( String tableName, String[] columnNameList, IPredicate predicate, RowOrdering rowOrdering, Map<String, Comparable<?>> parameterMap) { NoSqlPredicate noSqlPredicate = convertPredicate(predicate, tableName, parameterMap); List<Map<String, Object>> rowList; if ((noSqlPredicate != null) && noSqlPredicate.canExecuteEfficiently()) { rowList = noSqlPredicate.execute(columnNameList); } else { rowList = new ArrayList<Map<String, Object>>(); Collection<Map<String, Object>> allRowList = getAllRows(tableName, columnNameList); for (Map<String, Object> row : allRowList) { if ((noSqlPredicate == null) || noSqlPredicate.matchesRow(row)) { rowList.add(row); } } } if (rowOrdering != null) Collections.sort(rowList, new RowComparator(rowOrdering)); return new NoSqlResultSet(this, tableName, rowList); }
NoSqlPredicate convertPredicate( IPredicate predicate, String tableName, Map<String, Comparable<?>> parameterMap) { if (predicate == null) return null; NoSqlPredicate convertedPredicate = null; if (predicate instanceof CompoundPredicate) { CompoundPredicate compoundPredicate = (CompoundPredicate) predicate; ArrayList<NoSqlPredicate> noSqlPredicateList = new ArrayList<NoSqlPredicate>(); for (IPredicate childPredicate : compoundPredicate.getPredicateList()) { boolean incorporated = false; if (childPredicate instanceof OperatorPredicate) { OperatorPredicate childOperatorPredicate = (OperatorPredicate) childPredicate; for (NoSqlPredicate childNoSqlPredicate : noSqlPredicateList) { incorporated = childNoSqlPredicate.incorporateComparison( childOperatorPredicate.getColumnName(), childOperatorPredicate.getOperator(), getOperatorPredicateValue(childOperatorPredicate, parameterMap), compoundPredicate.getOperator()); if (incorporated) break; } } if (!incorporated) { NoSqlPredicate noSqlPredicate = convertPredicate(childPredicate, tableName, parameterMap); noSqlPredicateList.add(noSqlPredicate); } } convertedPredicate = new NoSqlCompoundPredicate( this, tableName, compoundPredicate.getOperator(), compoundPredicate.isNegated(), noSqlPredicateList); } else if (predicate instanceof OperatorPredicate) { OperatorPredicate operatorPredicate = (OperatorPredicate) predicate; Comparable<?> value = getOperatorPredicateValue(operatorPredicate, parameterMap); switch (operatorPredicate.getOperator()) { case EQ: convertedPredicate = new NoSqlRangePredicate( this, tableName, operatorPredicate.getColumnName(), value, true, value, true); break; case LT: convertedPredicate = new NoSqlRangePredicate( this, tableName, operatorPredicate.getColumnName(), null, false, value, false); break; case LTE: convertedPredicate = new NoSqlRangePredicate( this, tableName, operatorPredicate.getColumnName(), null, false, value, true); break; case GT: convertedPredicate = new NoSqlRangePredicate( this, tableName, operatorPredicate.getColumnName(), value, false, null, false); break; case GTE: convertedPredicate = new NoSqlRangePredicate( this, tableName, operatorPredicate.getColumnName(), value, true, null, false); break; default: convertedPredicate = new NoSqlOperatorPredicate( this, operatorPredicate.getColumnName(), operatorPredicate.getOperator(), value); } } else { throw new StorageException("Unknown predicate type"); } return convertedPredicate; }