Class StoredSetBasedResultSet<O>

  • All Implemented Interfaces:
    Closeable, AutoCloseable, Iterable<O>

    public class StoredSetBasedResultSet<O>
    extends StoredResultSet<O>
    A ResultSet which is stored directly in an index, and supports additional methods to add and remove objects.

    This implementation wraps a backing Set.

    Author:
    Niall Gallagher
    • Constructor Detail

      • StoredSetBasedResultSet

        public StoredSetBasedResultSet​(Set<O> backingSet)
        Constructor. Initialises the object with retrieval cost 0.
        Parameters:
        backingSet - A Set to which methods in this wrapper class will delegate
      • StoredSetBasedResultSet

        public StoredSetBasedResultSet​(Set<O> backingSet,
                                       int retrievalCost)
        Constructor.
        Parameters:
        backingSet - A Set to which methods in this wrapper class will delegate
        retrievalCost - The retrieval cost to subsequently return
    • Method Detail

      • contains

        public boolean contains​(O o)
        Description copied from class: ResultSet
        Returns true if this ResultSet contains the given object, false if it does not.

        Note that the cost of calling this method will most likely be cheaper than iterating all results to check if an object is contained. If indexes are available to support the query, this method will query indexes to check if the object is contained, instead of actually retrieving any data.

        See also the ResultSet.matches(Object) method, which provides a similar function but often with better performance than this method.

        Specified by:
        contains in class ResultSet<O>
        Parameters:
        o - The object to check for containment in this ResultSet
        Returns:
        True if this ResultSet contains the given object, false if it does not
      • matches

        public boolean matches​(O object)
        Similar to the ResultSet.contains(Object) method, but checks for logical containment in the ResultSet as opposed to physical containment in the ResultSet. Determines if the given object would be contained in the ResultSet, by testing if the given object matches the query for which this ResultSet was generated, instead of actually checking if the object is contained in appropriate indexes.

        This method will typically make the determination by evaluating the query on the given object on-the-fly without accessing indexes, however in some cases this method might delegate to the ResultSet.contains(Object) method to make the determination.

        This method will perform better than ResultSet.contains(Object) in cases where querying indexes is more expensive than querying attributes, which is usually the case.

        This implementation delegates to contains(Object).

        Specified by:
        matches in class ResultSet<O>
        Parameters:
        object - The object to check for logical containment in this ResultSet
        Returns:
        True if this ResultSet logically contains the given object, false if it does not
      • isEmpty

        public boolean isEmpty()
        Description copied from class: ResultSet
        Checks if this ResultSet if iterated would not return any objects (i.e. the query does not match any objects).

        This method can be more efficient than calling #size() to check simply if no objects would be returned.

        Specified by:
        isEmpty in class StoredResultSet<O>
        Returns:
        True if this ResultSet if iterated would not return any objects; false if the ResultSet would return objects
      • isNotEmpty

        public boolean isNotEmpty()
        Description copied from class: ResultSet
        Checks if this ResultSet if iterated would return some objects (i.e. the query matches some objects).

        This method can be more efficient than calling #size() to check simply if some objects would be returned.

        Specified by:
        isNotEmpty in class StoredResultSet<O>
        Returns:
        True if this ResultSet if iterated would return some objects; false if the ResultSet would not return any objects
      • size

        public int size()
        Description copied from class: ResultSet
        Returns the number of objects which would be returned by this ResultSet if iterated.

        Note that the cost of calling this method depends on the query for which it was constructed.

        For simple queries where a single query is supplied and a matching index exists, or where several such simple queries are supplied and are connected using a simple Or query, calculating the size via this method will be cheaper than iterating through the ResultSet and counting the number of objects individually.

        For more complex queries, where intersections must be performed or where no suitable indexes exist, calling this method can be non-trivial, but it will always be at least as cheap as iterating through the ResultSet and counting the number of objects individually.

        Specified by:
        size in class ResultSet<O>
        Returns:
        The number of objects which would be returned by this ResultSet if iterated
      • getRetrievalCost

        public int getRetrievalCost()
        Description copied from class: ResultSet
        Returns an estimate of the cost of looking up objects matching the query underlying this ResultSet in the index.

        The query engine will use this to select the index with the lowest cost, when more than one index supporting the query exists for the same attribute.

        An example: a single-level hash index will typically have a lower retrieval cost than a tree-based index. Of course a hash index only supports equality-based retrieval whereas a sorted tree-based index might support equality/less than/greater than or range based retrieval. But for an equality-based query, supported by both indexes, retrieval cost allows the query engine to prefer the hash index.

        Specified by:
        getRetrievalCost in class ResultSet<O>
        Returns:
        An estimate of the cost of looking up a particular query in the index
      • getMergeCost

        public int getMergeCost()
        Returns the size of the backing set.
        Specified by:
        getMergeCost in class ResultSet<O>
        Returns:
        the size of the backing set
      • close

        public void close()
        Description copied from class: ResultSet
        Releases any resources or closes the transaction which was opened for this ResultSet.

        Whether or not it is necessary to close the ResultSet depends on which implementation of IndexedCollection is in use and the types of indexes added to it.

        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface Closeable
        Specified by:
        close in class ResultSet<O>
      • getQuery

        public Query<O> getQuery()
        Description copied from class: ResultSet
        Returns the query for which this ResultSet provides results.
        Specified by:
        getQuery in class ResultSet<O>
        Returns:
        The query for which this ResultSet provides results.
      • getQueryOptions

        public QueryOptions getQueryOptions()
        Description copied from class: ResultSet
        Returns the query options associated with the query.
        Specified by:
        getQueryOptions in class ResultSet<O>
        Returns:
        The query options associated with the query.