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 long
Maximum number of rows to return, or 0 for no limit.private boolean
Whether all returned feature instances should be unique.private SelectionClauseWriter
The visitor to use for converting filters/expressions to SQL statements.private boolean
true
if at least one comparator given tosorted(Comparator)
is implemented using Java code instead of using SQL statements.private boolean
true
if at least one predicate given tofilter(Predicate)
is implemented using Java code instead of using SQL statements.private long
Number of rows to skip in underlying SQL query, or 0 for none.private SelectionClause
The SQL fragment on the right side of theWHERE
keyword.private SortBy<? super AbstractFeature>
TheORDER BY
clauses, ornull
if none.private final Table
The table which is the source of features. -
Constructor Summary
ConstructorsConstructorDescriptionFeatureStream
(Table table, boolean parallel) Creates a new stream of features. -
Method Summary
Modifier and TypeMethodDescriptionlong
count()
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 Connection
Acquires a connection to the database.private boolean
limit
(long maxSize) Truncates this stream to the given number of elements.private void
makeReadOnly
(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
setCloseHandler
Methods 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, toArray
Methods inherited from class org.apache.sis.internal.stream.BaseStreamWrapper
close, isParallel
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.stream.BaseStream
close, isParallel
Methods inherited from interface java.util.stream.Stream
dropWhile, mapMulti, mapMultiToDouble, mapMultiToInt, mapMultiToLong, takeWhile, toList
-
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 theWHERE
keyword. This buffer does not including theWHERE
keyword. It is created when first needed and discarded after the iterator is created. -
hasPredicates
private boolean hasPredicatestrue
if at least one predicate given tofilter(Predicate)
is implemented using Java code instead of using SQL statements. -
hasComparator
private boolean hasComparatortrue
if 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 BY
clauses, ornull
if 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()Returnstrue
if eithercount
oroffset
is set. In such case, we cannot continue to build the SQL statement because theOFFSET ... FETCH NEXT
clauses 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:
filter
in interfaceStream<AbstractFeature>
- Overrides:
filter
in classStreamWrapper<AbstractFeature>
-
distinct
Requests this stream to return distinct feature instances. This operation will be done with a SQLDISTINCT
clause if possible.- Specified by:
distinct
in interfaceStream<AbstractFeature>
- Overrides:
distinct
in classStreamWrapper<AbstractFeature>
-
unordered
Returns an equivalent stream that is unordered.- Specified by:
unordered
in interfaceBaseStream<AbstractFeature,
Stream<AbstractFeature>> - Overrides:
unordered
in 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 aClassCastException
to be thrown when a terminal operation will be executed becauseFeature
instances are not comparable.- Specified by:
sorted
in interfaceStream<AbstractFeature>
- Overrides:
sorted
in classStreamWrapper<AbstractFeature>
-
sorted
Returns a stream with features of this stream sorted using the given comparator.- Specified by:
sorted
in interfaceStream<AbstractFeature>
- Overrides:
sorted
in classStreamWrapper<AbstractFeature>
-
skip
Discards the specified number of elements. This operation will be done with a SQLOFFSET
clause.- Specified by:
skip
in interfaceStream<AbstractFeature>
- Overrides:
skip
in classStreamWrapper<AbstractFeature>
-
limit
Truncates this stream to the given number of elements. This operation will be done with a SQLFETCH NEXT
clause.- Specified by:
limit
in interfaceStream<AbstractFeature>
- Overrides:
limit
in classStreamWrapper<AbstractFeature>
-
map
Returns a stream with results of applying the given function to the elements of this stream. Theskip
andlimit
operations applied on the returned stream may continue to be optimized.- Specified by:
map
in interfaceStream<AbstractFeature>
- Overrides:
map
in 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:
count
in interfaceStream<AbstractFeature>
- Overrides:
count
in 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 ... finally
block.- 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 theFeatureStream
constructor.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:
createSourceIterator
in 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.
-