com.googlecode.cqengine.query.Query Java Examples

The following examples show how to use com.googlecode.cqengine.query.Query. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DBQueries.java    From cqengine with Apache License 2.0 6 votes vote down vote up
public static <O> int count(final Query<O> query, final String tableName, final Connection connection) {

        final String selectSql = String.format("SELECT COUNT(objectKey) FROM cqtbl_%s", tableName);
        PreparedStatement statement = null;
        try {
            statement = createAndBindSelectPreparedStatement(selectSql, "", Collections.<WhereClause>emptyList(), query, connection);
            java.sql.ResultSet resultSet = statement.executeQuery();

            if (!resultSet.next()) {
                throw new IllegalStateException("Unable to execute count. The ResultSet returned no row. Query: " + query);
            }

            return resultSet.getInt(1);
        }
        catch (Exception e) {
            throw new IllegalStateException("Unable to execute count. Query: " + query, e);
        }
        finally {
            DBUtils.closeQuietly(statement);
        }
    }
 
Example #2
Source File: DeduplicationTest.java    From cqengine with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeduplication_Logical() {
    IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>();
    cars.add(new Car(1, "Ford", "Focus", BLUE, 5, 1000.0, Collections.<String>emptyList(), Collections.emptyList()));
    cars.addIndex(HashIndex.onAttribute(Car.COLOR));
    cars.addIndex(HashIndex.onAttribute(Car.MANUFACTURER));

    Query<Car> query = or(
            equal(COLOR, BLUE),
            equal(MANUFACTURER, "Ford")
    );
    ResultSet<Car> results;
    results = cars.retrieve(query);
    assertEquals(2, results.size());

    DeduplicationOption deduplicate = deduplicate(DeduplicationStrategy.LOGICAL_ELIMINATION);
    results = cars.retrieve(query, queryOptions(deduplicate));
    assertEquals(1, results.size());
}
 
Example #3
Source File: SQLiteIndex.java    From cqengine with Apache License 2.0 6 votes vote down vote up
Query<O> getKeyRangeRestriction(A lowerBound, boolean lowerInclusive, A upperBound, boolean upperInclusive) {
    final Query<O> query;
    if (lowerBound != null && upperBound != null) {
        query = between(attribute, lowerBound, lowerInclusive, upperBound, upperInclusive);
    }
    else if (lowerBound != null) {
        query = lowerInclusive ? greaterThanOrEqualTo(attribute, lowerBound) : greaterThan(attribute, lowerBound);
    }
    else if (upperBound != null) {
        query = upperInclusive ? lessThanOrEqualTo(attribute, upperBound) : lessThan(attribute, upperBound);
    }
    else {
        query = has(attribute);
    }
    return query;
}
 
Example #4
Source File: CollectionQueryEngineTest.java    From cqengine with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings({"MismatchedQueryAndUpdateOfCollection", "StatementWithEmptyBody"})
public void testOrQueryCollectionScan() {
    IterationCountingSet<Car> iterationCountingSet = new IterationCountingSet<Car>(CarFactory.createCollectionOfCars(10));
    IndexedCollection<Car> collection = new ConcurrentIndexedCollection<Car>(WrappingPersistence.aroundCollection(iterationCountingSet));

    Query<Car> query = or(equal(Car.COLOR, Car.Color.BLUE), equal(Car.MANUFACTURER, "Honda"));
    ResultSet<Car> resultSet = collection.retrieve(query);

    for (Car car : resultSet) {
        // No op
    }

    // The two-branch or() query should have been evaluated by scanning the collection only once...
    Assert.assertEquals(iterationCountingSet.size(), iterationCountingSet.getItemsIteratedCount());
}
 
Example #5
Source File: InvertedRadixTreeIndex.java    From cqengine with Apache License 2.0 6 votes vote down vote up
/**
 * Package-private constructor, used by static factory methods.
 */
protected InvertedRadixTreeIndex(Attribute<O, A> attribute, NodeFactory nodeFactory) {
    super(attribute, new HashSet<Class<? extends Query>>() {/**
         * 
         */
        private static final long serialVersionUID = 1L;

    {
        add(Equal.class);
        add(In.class);
        add(StringIsContainedIn.class);
        add(LongestPrefix.class);
        add(StringIsPrefixOf.class);
    }});
    this.nodeFactory = nodeFactory;
    this.tree = new ConcurrentInvertedRadixTree<StoredResultSet<O>>(nodeFactory);
}
 
Example #6
Source File: JoinTest.java    From cqengine with Apache License 2.0 6 votes vote down vote up
/**
 * Find cars which are convertible or which have a sunroof, which can be serviced in Dublin.
 */
@Test
public void testSqlExistsWithForeignRestriction() {
    Query<Car> carsQuery = and(
            in(Car.FEATURES, "sunroof", "convertible"),
            existsIn(garages,
                    Car.NAME,
                    Garage.BRANDS_SERVICED,
                    equal(Garage.LOCATION, "Dublin")
            )
    );

    Set<Car> results = asSet(cars.retrieve(carsQuery));

    assertEquals("should have 2 results", 2, results.size());
    assertTrue("results should contain car1", results.contains(car1));
    assertTrue("results should contain car4", results.contains(car4));
}
 
Example #7
Source File: ReversedRadixTreeIndex.java    From cqengine with Apache License 2.0 6 votes vote down vote up
/**
 * If a query option specifying logical deduplication was supplied, wrap the given result sets in
 * {@link ResultSetUnion}, otherwise wrap in {@link ResultSetUnionAll}.
 * <p/>
 * An exception is if the index is built on a SimpleAttribute, we can avoid deduplication and always use
 * {@link ResultSetUnionAll}, because the same object could not exist in more than one {@link StoredResultSet}.
 *
 * @param results Provides the result sets to union
 * @param query The query for which the union is being constructed
 * @param queryOptions Specifies whether or not logical deduplication is required
 * @return A union view over the given result sets
 */
ResultSet<O> unionResultSets(Iterable<? extends ResultSet<O>> results, Query<O> query, QueryOptions queryOptions) {
    if (DeduplicationOption.isLogicalElimination(queryOptions)
            && !(getAttribute() instanceof SimpleAttribute || getAttribute() instanceof SimpleNullableAttribute)) {
        return new ResultSetUnion<O>(results, query, queryOptions) {
            @Override
            public int getRetrievalCost() {
                return INDEX_RETRIEVAL_COST;
            }
        };
    }
    else {
        return new ResultSetUnionAll<O>(results, query, queryOptions) {
            @Override
            public int getRetrievalCost() {
                return INDEX_RETRIEVAL_COST;
            }
        };
    }
}
 
Example #8
Source File: CollectionQueryEngine.java    From cqengine with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the entire collection wrapped as a {@link ResultSet}, with retrieval cost {@link Integer#MAX_VALUE}.
 * <p/>
 * Merge cost is the size of the collection.
 *
 * @return The entire collection wrapped as a {@link ResultSet}, with retrieval cost {@link Integer#MAX_VALUE}
 */
ResultSet<O> getEntireCollectionAsResultSet(final Query<O> query, final QueryOptions queryOptions) {
    return new ObjectStoreResultSet<O>(objectStore, query, queryOptions, Integer.MAX_VALUE) {
        // Override getMergeCost() to avoid calling size(),
        // which may be expensive for custom implementations of lazy backing sets...
        @Override
        public int getMergeCost() {
            return Integer.MAX_VALUE;
        }

        @Override
        public Query<O> getQuery() {
            return query;
        }

        @Override
        public QueryOptions getQueryOptions() {
            return queryOptions;
        }
    };
}
 
Example #9
Source File: DeduplicationTest.java    From cqengine with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeduplication_Materialize() {
    IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>();
    cars.add(new Car(1, "Ford", "Focus", BLUE, 5, 1000.0, Collections.<String>emptyList(), Collections.emptyList()));
    cars.addIndex(HashIndex.onAttribute(Car.COLOR));
    cars.addIndex(HashIndex.onAttribute(Car.MANUFACTURER));

    Query<Car> query = or(
            equal(COLOR, BLUE),
            equal(MANUFACTURER, "Ford")
    );
    ResultSet<Car> results;
    results = cars.retrieve(query);
    assertEquals(2, results.size());

    DeduplicationOption deduplicate = deduplicate(DeduplicationStrategy.MATERIALIZE);
    results = cars.retrieve(query, queryOptions(deduplicate));
    assertEquals(1, results.size());
}
 
Example #10
Source File: NavigableIndexTest.java    From cqengine with Apache License 2.0 6 votes vote down vote up
@Test
public void testIndexQuantization_ComplexQuery() {
    IndexedCollection<Car> collection = new ConcurrentIndexedCollection<Car>();
    collection.addIndex(NavigableIndex.withQuantizerOnAttribute(IntegerQuantizer.withCompressionFactor(10), Car.CAR_ID));
    collection.addAll(CarFactory.createCollectionOfCars(100));
    Query<Car> query = and(between(Car.CAR_ID, 96, 98), greaterThan(Car.CAR_ID, 95));

    // Merge cost should be 10, because objects matching this query are in a single bucket...
    assertEquals(10, collection.retrieve(query).getMergeCost());

    // 3 objects match the query...
    assertEquals(3, collection.retrieve(query).size());

    List<Integer> carIdsFound = retrieveCarIds(collection, query);
    assertEquals(asList(96, 97, 98), carIdsFound);
}
 
Example #11
Source File: LogicalQuery.java    From cqengine with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new {@link LogicalQuery} initialized to logically connect the supplied set of child queries.
 *
 * @param childQueries The child queries which this {@code LogicalQuery} is to logically connect
 */
public LogicalQuery(Collection<Query<O>> childQueries) {
    Objects.requireNonNull(childQueries, "The child queries supplied to a logical query cannot be null");
    for (Query<O> query : childQueries) {
        if (query instanceof LogicalQuery) {
            logicalQueries.add((LogicalQuery<O>) query);
        }
        else if (query instanceof SimpleQuery) {
            simpleQueries.add((SimpleQuery<O, ?>) query);
        }
        else if (query instanceof ComparativeQuery) {
            comparativeQueries.add((ComparativeQuery<O, ?>) query);
        }
        else {
            throw new IllegalStateException("Unexpected type of query: " + (query == null ? null : query + ", " + query.getClass()));
        }
    }
    this.hasLogicalQueries = !logicalQueries.isEmpty();
    this.hasSimpleQueries = !simpleQueries.isEmpty();
    this.hasComparativeQueries = !comparativeQueries.isEmpty();
    this.size = childQueries.size();
    this.childQueries = childQueries;
}
 
Example #12
Source File: JoinTest.java    From cqengine with Apache License 2.0 6 votes vote down vote up
/**
 * Find cars which are convertible or which have a sunroof,
 * which can be serviced by any garage which we have on file.
 */
@Test
public void testSqlExistsNoForeignRestriction() {
    Query<Car> carsQuery = and(
            in(Car.FEATURES, "sunroof", "convertible"),
            existsIn(garages,
                    Car.NAME,
                    Garage.BRANDS_SERVICED
            )
    );

    Set<Car> results = asSet(cars.retrieve(carsQuery));

    assertEquals("should have 3 results", 3, results.size());
    assertTrue("results should contain car1", results.contains(car1));
    assertTrue("results should contain car4", results.contains(car4));
    assertTrue("results should contain car5", results.contains(car5));
}
 
Example #13
Source File: InvertedRadixTreeIndex.java    From cqengine with Apache License 2.0 6 votes vote down vote up
/**
 * If a query option specifying logical deduplication was supplied, wrap the given result sets in
 * {@link ResultSetUnion}, otherwise wrap in {@link ResultSetUnionAll}.
 * <p/>
 * An exception is if the index is built on a SimpleAttribute, we can avoid deduplication and always use
 * {@link ResultSetUnionAll}, because the same object could not exist in more than one {@link StoredResultSet}.
 *
 * @param results Provides the result sets to union
 * @param query The query for which the union is being constructed
 * @param queryOptions Specifies whether or not logical deduplication is required
 * @return A union view over the given result sets
 */
ResultSet<O> unionResultSets(Iterable<? extends ResultSet<O>> results, Query<O> query, QueryOptions queryOptions) {
    if (DeduplicationOption.isLogicalElimination(queryOptions)
            && !(getAttribute() instanceof SimpleAttribute || getAttribute() instanceof SimpleNullableAttribute)) {
        return new ResultSetUnion<O>(results, query, queryOptions) {
            @Override
            public int getRetrievalCost() {
                return INDEX_RETRIEVAL_COST;
            }
        };
    }
    else {
        return new ResultSetUnionAll<O>(results, query, queryOptions) {
            @Override
            public int getRetrievalCost() {
                return INDEX_RETRIEVAL_COST;
            }
        };
    }
}
 
Example #14
Source File: CollectionQueryEngine.java    From cqengine with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieve results and then sort them afterwards (if sorting is required).
 */
ResultSet<O> retrieveWithoutIndexOrdering(Query<O> query, QueryOptions queryOptions, OrderByOption<O> orderByOption) {
    ResultSet<O> resultSet;
    resultSet = retrieveRecursive(query, queryOptions);

    // Check if we need to wrap ResultSet to order and/or deduplicate results (deduplicate using MATERIAIZE rather
    // than LOGICAL_ELIMINATION strategy)...
    final boolean applyMaterializedDeduplication = DeduplicationOption.isMaterialize(queryOptions);
    if (orderByOption != null) {
        // An OrderByOption was specified, wrap the results in an MaterializedOrderedResultSet.
        // -> This will implicitly sort AND deduplicate the results returned by the ResultSet.iterator() method.
        // -> However note this does not mean we will also deduplicate the count returned by ResultSet.size()!
        // -> Deduplicating the count returned by size() is expensive, so we only do this if the client
        //    requested both ordering AND deduplication explicitly (hence we pass applyMaterializeDeduplication)...
        Comparator<O> comparator = new AttributeOrdersComparator<O>(orderByOption.getAttributeOrders(), queryOptions);
        resultSet = new MaterializedOrderedResultSet<O>(resultSet, comparator, applyMaterializedDeduplication);
    }
    else if (applyMaterializedDeduplication) {
        // A DeduplicationOption was specified, wrap the results in an MaterializedDeduplicatedResultSet,
        // which will deduplicate (but not sort) results. O(n) time complexity to subsequently iterate...
        resultSet = new MaterializedDeduplicatedResultSet<O>(resultSet);
    }
    return resultSet;
}
 
Example #15
Source File: DBQueries.java    From cqengine with Apache License 2.0 5 votes vote down vote up
public static <O> java.sql.ResultSet search(final Query<O> query, final String tableName, final Connection connection){
    final String selectSql = String.format("SELECT DISTINCT objectKey FROM cqtbl_%s",tableName);
    PreparedStatement statement = null;
    try{
        statement = createAndBindSelectPreparedStatement(selectSql, "", Collections.<WhereClause>emptyList(), query, connection);
        return statement.executeQuery();
    }catch(Exception e){
        DBUtils.closeQuietly(statement);
        throw new IllegalStateException("Unable to execute search. Query: " + query, e);
    }
    // In case of success we leave the statement and result-set open because the iteration of an Index ResultSet is lazy.

}
 
Example #16
Source File: SQLAntlrListener.java    From cqengine with Apache License 2.0 5 votes vote down vote up
/**
 * Can be called when parsing has finished, to retrieve the parsed query.
 */
public Query<O> getParsedQuery() {
    Collection<Query<O>> rootQuery = childQueries.get(null);
    if (rootQuery == null) {
        // There was no WHERE clause...
        return QueryFactory.all(this.queryParser.getObjectType());
    }
    validateExpectedNumberOfChildQueries(1, rootQuery.size());
    return rootQuery.iterator().next();
}
 
Example #17
Source File: CollectionQueryEngine.java    From cqengine with Apache License 2.0 5 votes vote down vote up
/**
 * Adds either a {@link StandingQueryIndex} or a regular index build on a {@link StandingQueryAttribute}.
 * @param standingQueryIndex The index to add
 * @param standingQuery The query on which the index is based
 */
void addStandingQueryIndex(Index<O> standingQueryIndex, Query<O> standingQuery, QueryOptions queryOptions) {
    Index<O> existingIndex = standingQueryIndexes.putIfAbsent(standingQuery, standingQueryIndex);
    if (existingIndex != null) {
        throw new IllegalStateException("An index has already been added for standing query: " + standingQuery);
    }
    queryOptions.put(QueryEngine.class, this);
    queryOptions.put(Persistence.class, persistence);
    standingQueryIndex.init(objectStore, queryOptions);
}
 
Example #18
Source File: ObjectStoreResultSet.java    From cqengine with Apache License 2.0 5 votes vote down vote up
public ObjectStoreResultSet(ObjectStore<O> objectStore, Query<O> query, QueryOptions queryOptions, int retrievalCost) {
    this.objectStore = objectStore;
    this.query = query;
    this.queryOptions = queryOptions;
    this.retrievalCost = retrievalCost;
    this.objectSet = ObjectSet.fromObjectStore(objectStore, queryOptions);
}
 
Example #19
Source File: CollectionQueryEngine.java    From cqengine with Apache License 2.0 5 votes vote down vote up
Iterator<O> retrieveWithIndexOrderingMissingResults(final Query<O> query, QueryOptions queryOptions, Attribute<O, Comparable> primarySortAttribute, List<AttributeOrder<O>> allSortOrders, boolean attributeCanHaveMoreThanOneValue) {
    // Ensure that at the end of processing the request, that we close any resources we opened...
    final CloseableResourceGroup closeableResourceGroup = CloseableRequestResources.forQueryOptions(queryOptions).addGroup();

    // Retrieve missing objects from the secondary index on objects which don't have a value for the primary sort attribute...
    Not<O> missingValuesQuery = not(has(primarySortAttribute));
    ResultSet<O> missingResults = retrieveRecursive(missingValuesQuery, queryOptions);

    // Ensure that this is closed...
    closeableResourceGroup.add(missingResults);

    Iterator<O> missingResultsIterator = missingResults.iterator();
    // Filter the objects from the secondary index, to ensure they match the query...
    missingResultsIterator = filterIndexOrderingCandidateResults(missingResultsIterator, query, queryOptions);

    // Determine if we need to sort the missing objects...
    Index<O> indexForMissingObjects = standingQueryIndexes.get(missingValuesQuery);
    final List<AttributeOrder<O>> sortOrdersForBucket = determineAdditionalSortOrdersForIndexOrdering(allSortOrders, attributeCanHaveMoreThanOneValue, indexForMissingObjects, queryOptions);

    if (!sortOrdersForBucket.isEmpty()) {
        // We do need to sort the missing objects...
        Comparator<O> comparator = new AttributeOrdersComparator<O>(sortOrdersForBucket, queryOptions);
        missingResultsIterator = IteratorUtil.materializedSort(missingResultsIterator, comparator);
    }

    return missingResultsIterator;
}
 
Example #20
Source File: NoneTest.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Test
public void testNone() {
    IndexedCollection<Integer> indexedCollection = new ConcurrentIndexedCollection<Integer>();
    indexedCollection.addAll(asList(1, 2, 3, 4, 5));
    IndexedCollection<Integer> collection = indexedCollection;
    Query<Integer> query = none(Integer.class);
    ResultSet<Integer> results = collection.retrieve(query);
    assertEquals(0, results.size());
    assertFalse(results.iterator().hasNext());
}
 
Example #21
Source File: CollectionQueryEngine.java    From cqengine with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if the given query can be answered from a standing query index, and if so returns
 * a {@link ResultSet} which does so.
 * If the query cannot be answered from a standing query index, returns null.
 *
 * @param query The query to evaluate
 * @param queryOptions Query options supplied for the query
 * @return A {@link ResultSet} which answers the query from a standing query index, or null if no such index
 * is available
 */
ResultSet<O> retrieveFromStandingQueryIndexIfAvailable(Query<O> query, final QueryOptions queryOptions) {
    // Check if we can process this query from a standing query index...
    Index<O> standingQueryIndex = standingQueryIndexes.get(query);
    if (standingQueryIndex != null) {
        // No deduplication required for standing queries.
        if (standingQueryIndex instanceof StandingQueryIndex) {
            return standingQueryIndex.retrieve(query, queryOptions);
        }
        else {
            return standingQueryIndex.retrieve(equal(forStandingQuery(query), Boolean.TRUE), queryOptions);
        }
    } // else no suitable standing query index exists, process the query normally...
    return null;
}
 
Example #22
Source File: AbstractAttributeIndex.java    From cqengine with Apache License 2.0 5 votes vote down vote up
/**
 * Protected constructor, called by subclasses.
 *
 * @param attribute The attribute on which the index will be built
 * @param supportedQueries The set of {@link Query} types which the subclass implementation supports
 */
protected AbstractAttributeIndex(Attribute<O, A> attribute, Set<Class<? extends Query>> supportedQueries) {
    this.attribute = attribute;
    // Note: Ideally supportedQueries would be varargs to simplify subclasses, but varargs causes generic array
    // creation warnings.
    this.supportedQueries = Collections.unmodifiableSet(supportedQueries);
}
 
Example #23
Source File: CollectionQueryEngineTest.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testUnexpectedQueryTye() {
    CollectionQueryEngine<Car> queryEngine = new CollectionQueryEngine<Car>();
    queryEngine.init(emptyObjectStore(), queryOptionsWithOnHeapPersistence());

    queryEngine.retrieveRecursive(new Query<Car>() {
        @Override
        public boolean matches(Car object, QueryOptions queryOptions) {
            return false;
        }
    }, noQueryOptions());
}
 
Example #24
Source File: DiskIndex.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Override
public ResultSet<O> retrieve(Query<O> query, QueryOptions queryOptions) {
    return new WrappedResultSet<O>(super.retrieve(query, queryOptions)) {
        @Override
        public int getRetrievalCost() {
            return super.getRetrievalCost() + INDEX_RETRIEVAL_COST_DELTA;
        }
    };
}
 
Example #25
Source File: UniqueIndex.java    From cqengine with Apache License 2.0 5 votes vote down vote up
/**
 * Package-private constructor, used by static factory methods. Creates a new UniqueIndex initialized to index the
 * supplied attribute.
 *
 * @param indexMapFactory A factory used to create the main map-based data structure used by the index
 * @param attribute The attribute on which the index will be built
 */
protected UniqueIndex(Factory<ConcurrentMap<A,O>> indexMapFactory, Attribute<O, A> attribute)	{
    super(attribute, new HashSet<Class<? extends Query>>() {{
        add(Equal.class);
        add(In.class);
    }});
    this.indexMapFactory = indexMapFactory;
    this.indexMap = indexMapFactory.create();
}
 
Example #26
Source File: ReversedRadixTreeIndex.java    From cqengine with Apache License 2.0 5 votes vote down vote up
/**
 * Package-private constructor, used by static factory methods.
 */
protected ReversedRadixTreeIndex(Attribute<O, A> attribute, NodeFactory nodeFactory) {
    super(attribute, new HashSet<Class<? extends Query>>() {{
        add(Equal.class);
        add(In.class);
        add(StringEndsWith.class);
    }});
    this.nodeFactory = nodeFactory;
    this.tree = new ConcurrentReversedRadixTree<StoredResultSet<O>>(nodeFactory);
}
 
Example #27
Source File: PartialIndex.java    From cqengine with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true if this partial index can answer this branch of the query.
 * <p/>
 * See the class JavaDoc for the conditions which must be satisfied for this method to return true.
 *
 * @param query The query supplied by the query engine, which is typically a branch of the overall query being
 *              evaluated.
 */
@Override
public boolean supportsQuery(Query<O> query, QueryOptions queryOptions) {
    // Extract the root query from the query options, and check if it contains the filter query...
    @SuppressWarnings("unchecked")
    Query<O> rootQuery = (Query<O>) queryOptions.get(CollectionQueryEngine.ROOT_QUERY);

    return supportsQueryInternal(backingIndex(), filterQuery, rootQuery, query, queryOptions);
}
 
Example #28
Source File: ResultSetUnion.java    From cqengine with Apache License 2.0 5 votes vote down vote up
public ResultSetUnion(Iterable<? extends ResultSet<O>> resultSets, Query<O> query, QueryOptions queryOptions, boolean useIndexMergeStrategy) {
    List<ResultSet<O>> costCachingResultSets = ResultSets.wrapWithCostCachingIfNecessary(resultSets);
    this.resultSets = costCachingResultSets;
    this.query = query;
    this.queryOptions = queryOptions;
    this.useIndexMergeStrategy = useIndexMergeStrategy;
}
 
Example #29
Source File: SQLiteIndex.java    From cqengine with Apache License 2.0 5 votes vote down vote up
CloseableIterable<A> getDistinctKeysInRange(A lowerBound, boolean lowerInclusive, A upperBound, boolean upperInclusive, final boolean descending, final QueryOptions queryOptions) {
    final Query<O> query = getKeyRangeRestriction(lowerBound, lowerInclusive, upperBound, upperInclusive);

    final CloseableResourceGroup closeableResourceGroup = CloseableRequestResources.forQueryOptions(queryOptions).addGroup();

    return new CloseableIterable<A>() {
        @Override
        public CloseableIterator<A> iterator() {
            final ConnectionManager connectionManager = getConnectionManager(queryOptions);
            final Connection searchConnection = connectionManager.getConnection(SQLiteIndex.this, queryOptions);

            final java.sql.ResultSet searchResultSet = DBQueries.getDistinctKeys(query, descending, tableName, searchConnection);
            closeableResourceGroup.add(DBUtils.wrapAsCloseable(searchResultSet));

            return new LazyCloseableIterator<A>() {
                @Override
                protected A computeNext() {
                    try {
                        if (!searchResultSet.next()) {
                            close();
                            return endOfData();
                        }
                        return DBUtils.getValueFromResultSet(1, searchResultSet, attribute.getAttributeType());
                    }
                    catch (Exception e) {
                        endOfData();
                        close();
                        throw new IllegalStateException("Unable to retrieve the ResultSet item.", e);
                    }
                }

                @Override
                public void close() {
                    closeableResourceGroup.close();
                }
            };
        }
    };
}
 
Example #30
Source File: CQNAntlrListener.java    From cqengine with Apache License 2.0 5 votes vote down vote up
/**
 * Adds the given query to a list of child queries which have not yet been wrapped in a parent query.
 */
void addParsedQuery(ParserRuleContext currentContext, Query<O> parsedQuery) {
    // Retrieve the possibly null parent query...
    ParserRuleContext parentContext = getParentContextOfType(currentContext, getAndOrNotContextClasses());
    Collection<Query<O>> childrenOfParent = this.childQueries.get(parentContext);
    if (childrenOfParent == null) {
        childrenOfParent = new ArrayList<Query<O>>();
        this.childQueries.put(parentContext, childrenOfParent); // parentContext will be null if this is root query
    }
    childrenOfParent.add(parsedQuery);
    numQueriesParsed++;
}