Class RewriteOnUpdate
java.lang.Object
org.apache.sis.internal.storage.xml.stream.RewriteOnUpdate
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
Updater
Helper class for updating an existing XML file, with no feature type change permitted.
The implementation strategy is to rewrite fully the updated features in a temporary file,
then replaces the source file by the temporary file when ready.
The flush()
method should always been invoked before a RewriteOnUpdate
reference is lost, otherwise data may be lost.
Multi-threading
This class is not synchronized for multi-threading. Synchronization is caller's responsibility, because the caller usually needs to take in account other data store operations such as reads.- Since:
- 1.3
- Version:
- 1.3
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Stream
<? extends AbstractFeature> The features to write, fetched when first needed.private boolean
Whether the store is initially empty.private final Path
The main file, ornull
if unknown.private boolean
A flag telling whetherremoveIf(Predicate)
removed at least one feature.protected final FeatureSet
The set of features to update. -
Constructor Summary
ConstructorsConstructorDescriptionRewriteOnUpdate
(FeatureSet source, Path location) Creates an updater for the given source of features. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(Iterator<? extends AbstractFeature> features) Appends new feature instances in theFeatureSet
.void
close()
Releases resources used by this updater.protected abstract Path
Creates an initially empty temporary file.protected abstract StaxStreamWriter
createWriter
(OutputStream temporary) Creates a new XML document writer for an output in the specified temporary file.protected Stream
<? extends AbstractFeature> features()
Returns the stream of features to copy.private Stream
<? extends AbstractFeature> filtered()
Returns the features to write.void
flush()
Writes immediately all feature instances.protected final Locale
Returns the locale to use for locale-sensitive data, ornull
if unspecified.private boolean
isEmpty()
Returnstrue
if there is currently no data.boolean
removeIf
(Predicate<? super AbstractFeature> filter) Removes all feature instances from theFeatureSet
which matches the given predicate.void
replaceIf
(Predicate<? super AbstractFeature> filter, UnaryOperator<AbstractFeature> updater) Updates all feature instances from theFeatureSet
which match the given predicate.
-
Field Details
-
source
The set of features to update. This is the set specified at construction time. -
location
The main file, ornull
if unknown. -
isSourceEmpty
private boolean isSourceEmptyWhether the store is initially empty. It may be the underlying file does not exist or has a length of zero. -
filtered
The features to write, fetched when first needed.- See Also:
-
modified
private boolean modifiedA flag telling whetherremoveIf(Predicate)
removed at least one feature.
-
-
Constructor Details
-
RewriteOnUpdate
Creates an updater for the given source of features.- Parameters:
source
- the set of features to update.location
- the main file, ornull
if unknown.- Throws:
IOException
- if an error occurred while determining whether the file is empty.
-
-
Method Details
-
getLocale
Returns the locale to use for locale-sensitive data, ornull
if unspecified. This is not for logging or warning messages.- Returns:
- the data locale, or
null
.
-
isEmpty
Returnstrue
if there is currently no data.- Throws:
ReadOnlyStorageException
-
filtered
Returns the features to write.- Throws:
DataStoreException
- if the feature stream cannot be obtained.
-
features
Returns the stream of features to copy. The default implementation delegates toFeatureSet.features(boolean)
.- Returns:
- all features contained in the dataset.
- Throws:
DataStoreException
- if an error occurred while fetching the features.
-
add
Appends new feature instances in theFeatureSet
. Any feature already present in theFeatureSet
will remain unmodified.- Parameters:
features
- feature instances to append in theFeatureSet
.- Throws:
DataStoreException
- if the feature stream cannot be obtained or updated.
-
removeIf
Removes all feature instances from theFeatureSet
which matches the given predicate.- Parameters:
filter
- a predicate which returnstrue
for feature instances to be removed.- Returns:
true
if any elements were removed.- Throws:
DataStoreException
- if the feature stream cannot be obtained or updated.
-
replaceIf
public void replaceIf(Predicate<? super AbstractFeature> filter, UnaryOperator<AbstractFeature> updater) throws DataStoreException Updates all feature instances from theFeatureSet
which match the given predicate. If the given operator returnsnull
, then the filtered feature is removed.- Parameters:
filter
- a predicate which returnstrue
for feature instances to be updated.updater
- operation called for each matchingFeature
instance. May returnnull
.- Throws:
DataStoreException
- if the feature stream cannot be obtained or updated.
-
createTemporaryFile
Creates an initially empty temporary file.- Returns:
- the temporary file.
- Throws:
IOException
- if an error occurred while creating the temporary file.
-
createWriter
Creates a new XML document writer for an output in the specified temporary file. Caller is responsible for closing the writer.- Parameters:
temporary
- the temporary stream where to write, ornull
for writing directly in the store file.- Returns:
- the writer where to copy updated features.
- Throws:
Exception
- if an error occurred while creating the writer. May beDataStoreException
,IOException
,RuntimeException
, etc.
-
flush
Writes immediately all feature instances. This method does nothing if there is no data to write.- Throws:
DataStoreException
- if an error occurred.
-
close
public void close()Releases resources used by this updater. Ifflush()
has not been invoked, data may be lost. This method is useful in try-with-resource in case something fails beforeflush()
invocation.- Specified by:
close
in interfaceAutoCloseable
-