Class FeatureStream
java.lang.Object
org.apache.sis.internal.stream.BaseStreamWrapper<T,Stream<T>>
org.apache.sis.internal.stream.StreamWrapper<T>
org.apache.sis.internal.stream.DeferredStream<AbstractFeature>
org.apache.sis.internal.sql.feature.FeatureStream
- All Implemented Interfaces:
AutoCloseable,BaseStream<AbstractFeature,,Stream<AbstractFeature>> Stream<AbstractFeature>
A stream of
Feature instances from a table. This implementation intercepts some Stream
method calls such as count(), distinct(), skip(long) and limit(long)
in order to delegate the operation to the underlying SQL database.
Optimization strategies are also propagated to streams obtained using map(Function) and
StreamWrapper.mapToDouble(ToDoubleFunction). However, for result consistency, no optimization is stacked
anymore after either filter(Predicate) or StreamWrapper.flatMap(Function) operations are called,
because they modify volumetry (the count of stream elements is not bound 1 to 1 to query result rows).
- Since:
- 1.1
- Version:
- 1.1
-
Nested Class Summary
Nested classes/interfaces inherited from interface java.util.stream.Stream
Stream.Builder<T extends Object> -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate longMaximum number of rows to return, or 0 for no limit.private booleanWhether all returned feature instances should be unique.private SelectionClauseWriterThe visitor to use for converting filters/expressions to SQL statements.private booleantrueif at least one comparator given tosorted(Comparator)is implemented using Java code instead of using SQL statements.private booleantrueif at least one predicate given tofilter(Predicate)is implemented using Java code instead of using SQL statements.private longNumber of rows to skip in underlying SQL query, or 0 for none.private SelectionClauseThe SQL fragment on the right side of theWHEREkeyword.private SortBy<? super AbstractFeature>TheORDER BYclauses, ornullif none.private final TableThe table which is the source of features. -
Constructor Summary
ConstructorsConstructorDescriptionFeatureStream(Table table, boolean parallel) Creates a new stream of features. -
Method Summary
Modifier and TypeMethodDescriptionlongcount()Counts the number of elements in the table.protected Spliterator<AbstractFeature>Creates the iterator which will provide the actual feature instances.distinct()Requests this stream to return distinct feature instances.private Stream<AbstractFeature>empty()Marks this stream as inactive and returns an empty stream.filter(Predicate<? super AbstractFeature> predicate) Returns a stream with features of this stream that match the given predicate.private ConnectionAcquires a connection to the database.private booleanlimit(long maxSize) Truncates this stream to the given number of elements.private voidmakeReadOnly(Connection connection) Makes the given connection read-only and apply some configuration for better performances.<R> Stream<R>map(Function<? super AbstractFeature, ? extends R> mapper) Returns a stream with results of applying the given function to the elements of this stream.skip(long n) Discards the specified number of elements.sorted()Returns an equivalent stream that is sorted by feature natural order.sorted(Comparator<? super AbstractFeature> comparator) Returns a stream with features of this stream sorted using the given comparator.toString()Returns a string representation of this stream for debugging purposes.Returns an equivalent stream that is unordered.Methods inherited from class org.apache.sis.internal.stream.DeferredStream
setCloseHandlerMethods inherited from class org.apache.sis.internal.stream.StreamWrapper
allMatch, anyMatch, collect, collect, delegate, findAny, findFirst, flatMap, flatMapToDouble, flatMapToInt, flatMapToLong, forEach, forEachOrdered, iterator, mapToDouble, mapToInt, mapToLong, max, min, noneMatch, onClose, parallel, peek, reduce, reduce, reduce, sequential, source, spliterator, toArray, toArrayMethods inherited from class org.apache.sis.internal.stream.BaseStreamWrapper
close, isParallelMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.stream.BaseStream
close, isParallel
-
Field Details
-
table
The table which is the source of features. -
filterToSQL
The visitor to use for converting filters/expressions to SQL statements. This is used for writing the content of theSelectionClause. It is usually a singleton instance shared by all databases. It is fetched when first needed. -
selection
The SQL fragment on the right side of theWHEREkeyword. This buffer does not including theWHEREkeyword. It is created when first needed and discarded after the iterator is created. -
hasPredicates
private boolean hasPredicatestrueif at least one predicate given tofilter(Predicate)is implemented using Java code instead of using SQL statements. -
hasComparator
private boolean hasComparatortrueif at least one comparator given tosorted(Comparator)is implemented using Java code instead of using SQL statements. -
distinct
private boolean distinctWhether all returned feature instances should be unique. -
sort
TheORDER BYclauses, ornullif none. -
offset
private long offsetNumber of rows to skip in underlying SQL query, or 0 for none.- See Also:
-
count
private long countMaximum number of rows to return, or 0 for no limit. Note that 0 is a valid value for the limit, but when this value is reached theempty()stream should be immediately returned.- See Also:
-
-
Constructor Details
-
FeatureStream
FeatureStream(Table table, boolean parallel) Creates a new stream of features.- Parameters:
table- the source table.parallel- whether the stream should be initially parallel.
-
-
Method Details
-
empty
Marks this stream as inactive and returns an empty stream. This method is invoked when an operation resulted in an empty stream. -
isPagined
private boolean isPagined()Returnstrueif eithercountoroffsetis set. In such case, we cannot continue to build the SQL statement because theOFFSET ... FETCH NEXTclauses in SQL are executed last. Consequently, in order to have consistent results, the#offset(long)andlimit(long)methods need to be the last methods invoked on this stream. -
filter
Returns a stream with features of this stream that match the given predicate. If the given predicate is an instance ofFilter, then this method tries to express the filter using SQL statements.- Specified by:
filterin interfaceStream<AbstractFeature>- Overrides:
filterin classStreamWrapper<AbstractFeature>
-
distinct
Requests this stream to return distinct feature instances. This operation will be done with a SQLDISTINCTclause if possible.- Specified by:
distinctin interfaceStream<AbstractFeature>- Overrides:
distinctin classStreamWrapper<AbstractFeature>
-
unordered
Returns an equivalent stream that is unordered.- Specified by:
unorderedin interfaceBaseStream<AbstractFeature,Stream<AbstractFeature>> - Overrides:
unorderedin classStreamWrapper<AbstractFeature>
-
sorted
Returns an equivalent stream that is sorted by feature natural order. This is defined as a matter of principle, but will cause aClassCastExceptionto be thrown when a terminal operation will be executed becauseFeatureinstances are not comparable.- Specified by:
sortedin interfaceStream<AbstractFeature>- Overrides:
sortedin classStreamWrapper<AbstractFeature>
-
sorted
Returns a stream with features of this stream sorted using the given comparator.- Specified by:
sortedin interfaceStream<AbstractFeature>- Overrides:
sortedin classStreamWrapper<AbstractFeature>
-
skip
Discards the specified number of elements. This operation will be done with a SQLOFFSETclause.- Specified by:
skipin interfaceStream<AbstractFeature>- Overrides:
skipin classStreamWrapper<AbstractFeature>
-
limit
Truncates this stream to the given number of elements. This operation will be done with a SQLFETCH NEXTclause.- Specified by:
limitin interfaceStream<AbstractFeature>- Overrides:
limitin classStreamWrapper<AbstractFeature>
-
map
Returns a stream with results of applying the given function to the elements of this stream. Theskipandlimitoperations applied on the returned stream may continue to be optimized.- Specified by:
mapin interfaceStream<AbstractFeature>- Overrides:
mapin classStreamWrapper<AbstractFeature>
-
count
public long count()Counts the number of elements in the table. This method uses a simpler SQL statement than the one associated to the table. For example if a property is an association to another feature, the SQL statement will contain only the foreigner key values, not an inner join to the other feature table.- Specified by:
countin interfaceStream<AbstractFeature>- Overrides:
countin classStreamWrapper<AbstractFeature>
-
getConnection
Acquires a connection to the database. ThemakeReadOnly(Connection)method should be invoked after this method. Those two methods are separated for allowing the immediate use of the connection in atry ... finallyblock.- Returns:
- a new connection to the database.
- Throws:
SQLException- if we cannot create a new connection. SeeDataSource.getConnection()for details.
-
makeReadOnly
Makes the given connection read-only and apply some configuration for better performances. Current configurations are:- Parameters:
connection- the connection to configure.- Throws:
SQLException
-
createSourceIterator
Creates the iterator which will provide the actual feature instances. The characteristics of the returned iterator must be the characteristics declared in theFeatureStreamconstructor.This method is invoked at most once, generally when a stream terminal operation is invoked. After this method is invoked, this stream will not be active anymore.
- Specified by:
createSourceIteratorin classDeferredStream<AbstractFeature>- Returns:
- an iterator over the feature elements.
- Throws:
DataStoreException- if a data model dependent error occurs.SQLException- if an error occurs while executing the SQL statement.Exception- if the iterator cannot be created.
-
toString
Returns a string representation of this stream for debugging purposes. The returned string tells whether filtering and sorting are done using SQL statement, Java code, or a mix of both.
-