Ejemplo n.º 1
0
 @Test
 public void shouldReturnCursorThatAccessesTuples() {
   results = new QueryResults(columns, statistics, tuples, context.getProblems(), null);
   Cursor cursor = results.getCursor();
   Iterator<Object[]> expectedIter = tuples.iterator();
   int rowNumber = 0;
   while (cursor.hasNext() && expectedIter.hasNext()) {
     cursor.next();
     Object[] tuple = expectedIter.next();
     // Check the column values by column name and index ...
     for (Column column : results.getColumns().getColumns()) {
       String columnName = column.getColumnName();
       int columnIndex = columns.getColumnIndexForName(columnName);
       assertThat(cursor.getValue(columnName), is(tuple[columnIndex]));
       assertThat(cursor.getValue(columnIndex), is(tuple[columnIndex]));
       // Get the location for this column ...
       int locationIndex = columns.getLocationIndex(column.selectorName().name());
       Location location = (Location) tuple[locationIndex];
       assertThat(cursor.getLocation(columnIndex), is(location));
     }
     // Check the locations by selector name and index ...
     for (String selectorName : results.getColumns().getSelectorNames()) {
       int locationIndex = columns.getLocationIndex(selectorName);
       Location location = (Location) tuple[locationIndex];
       assertThat(cursor.getLocation(selectorName), is(location));
       assertThat(location.getPath(), is(notNullValue()));
     }
     // Check the row index ...
     assertThat(cursor.getRowIndex(), is(rowNumber++));
   }
   assertThat(cursor.hasNext(), is(false));
   assertThat(expectedIter.hasNext(), is(false));
 }
Ejemplo n.º 2
0
  /**
   * Create the constraint evaluator that is used by the {@link SelectComponent} to evaluate the
   * supplied {@link Constraint criteria}.
   *
   * @param types the type system; may not be null
   * @param schemata the schemata; may not be null
   * @param columns the definition of the result columns and the tuples; may not be null
   * @param constraint the criteria that this {@link SelectComponent} is to evaluate
   * @param variables the variables that are to be substituted for the various {@link
   *     BindVariableName} {@link StaticOperand operands}; may not be null
   * @return the constraint evaluator; never null
   */
  protected ConstraintChecker createChecker(
      final TypeSystem types,
      Schemata schemata,
      Columns columns,
      Constraint constraint,
      Map<String, Object> variables) {
    if (constraint instanceof Or) {
      Or orConstraint = (Or) constraint;
      final ConstraintChecker left =
          createChecker(types, schemata, columns, orConstraint.left(), variables);
      final ConstraintChecker right =
          createChecker(types, schemata, columns, orConstraint.right(), variables);
      return new ConstraintChecker() {
        @Override
        public boolean satisfiesConstraints(Object[] tuple) {
          return left.satisfiesConstraints(tuple) || right.satisfiesConstraints(tuple);
        }
      };
    }
    if (constraint instanceof Not) {
      Not notConstraint = (Not) constraint;
      final ConstraintChecker original =
          createChecker(types, schemata, columns, notConstraint.getConstraint(), variables);
      return new ConstraintChecker() {
        @Override
        public boolean satisfiesConstraints(Object[] tuple) {
          return !original.satisfiesConstraints(tuple);
        }
      };
    }
    if (constraint instanceof And) {
      And andConstraint = (And) constraint;
      final ConstraintChecker left =
          createChecker(types, schemata, columns, andConstraint.left(), variables);
      final ConstraintChecker right =
          createChecker(types, schemata, columns, andConstraint.right(), variables);
      return new ConstraintChecker() {
        @Override
        public boolean satisfiesConstraints(Object[] tuple) {
          return left.satisfiesConstraints(tuple) && right.satisfiesConstraints(tuple);
        }
      };
    }
    if (constraint instanceof ChildNode) {
      ChildNode childConstraint = (ChildNode) constraint;
      final int locationIndex = columns.getLocationIndex(childConstraint.selectorName().name());
      final Path parentPath = (Path) types.getPathFactory().create(childConstraint.getParentPath());
      return new ConstraintChecker() {
        @Override
        public boolean satisfiesConstraints(Object[] tuple) {
          Location location = (Location) tuple[locationIndex];
          return location.getPath().getParent().equals(parentPath);
        }
      };
    }
    if (constraint instanceof DescendantNode) {
      DescendantNode descendantNode = (DescendantNode) constraint;
      final int locationIndex = columns.getLocationIndex(descendantNode.selectorName().name());
      final Path ancestorPath =
          (Path) types.getPathFactory().create(descendantNode.getAncestorPath());
      return new ConstraintChecker() {
        @Override
        public boolean satisfiesConstraints(Object[] tuple) {
          Location location = (Location) tuple[locationIndex];
          return location.getPath().isDescendantOf(ancestorPath);
        }
      };
    }
    if (constraint instanceof SameNode) {
      SameNode sameNode = (SameNode) constraint;
      final int locationIndex = columns.getLocationIndex(sameNode.selectorName().name());
      final String path = sameNode.getPath();
      return new ConstraintChecker() {
        @Override
        public boolean satisfiesConstraints(Object[] tuple) {
          Location location = (Location) tuple[locationIndex];
          return location.toString().equals(path);
        }
      };
    }
    if (constraint instanceof PropertyExistence) {
      PropertyExistence propertyExistance = (PropertyExistence) constraint;
      String selectorName = propertyExistance.selectorName().name();
      final String propertyName = propertyExistance.getPropertyName();
      final int columnIndex = columns.getColumnIndexForProperty(selectorName, propertyName);
      return new ConstraintChecker() {
        @Override
        public boolean satisfiesConstraints(Object[] tuple) {
          return tuple[columnIndex] != null;
        }
      };
    }
    if (constraint instanceof FullTextSearch) {
      return new ConstraintChecker() {
        @Override
        public boolean satisfiesConstraints(Object[] tuple) {
          return true;
        }
      };
    }
    if (constraint instanceof Relike) {
      return new ConstraintChecker() {
        @Override
        public boolean satisfiesConstraints(Object[] tuple) {
          return true;
        }
      };
    }
    if (constraint instanceof Comparison) {
      Comparison comparison = (Comparison) constraint;

      // Create the correct dynamic operation ...
      DynamicOperation dynamicOperation =
          createDynamicOperation(types, schemata, columns, comparison.getOperand1());
      Operator operator = comparison.operator();
      StaticOperand staticOperand = comparison.getOperand2();
      return createChecker(types, schemata, columns, dynamicOperation, operator, staticOperand);
    }
    if (constraint instanceof SetCriteria) {
      SetCriteria setCriteria = (SetCriteria) constraint;
      DynamicOperation dynamicOperation =
          createDynamicOperation(types, schemata, columns, setCriteria.leftOperand());
      Operator operator = Operator.EQUAL_TO;
      final List<ConstraintChecker> checkers = new LinkedList<ConstraintChecker>();
      for (StaticOperand setValue : setCriteria.rightOperands()) {
        ConstraintChecker rightChecker =
            createChecker(types, schemata, columns, dynamicOperation, operator, setValue);
        assert rightChecker != null;
        checkers.add(rightChecker);
      }
      if (checkers.isEmpty()) {
        // Nothing will satisfy these constraints ...
        return new ConstraintChecker() {
          @Override
          public boolean satisfiesConstraints(Object[] tuple) {
            return false;
          }
        };
      }
      return new ConstraintChecker() {
        @Override
        public boolean satisfiesConstraints(Object[] tuple) {
          for (ConstraintChecker checker : checkers) {
            if (checker.satisfiesConstraints(tuple)) return true;
          }
          return false;
        }
      };
    }
    assert false;
    return null;
  }