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 booleanWhether the store is initially empty.private final PathThe main file, ornullif unknown.private booleanA flag telling whetherremoveIf(Predicate)removed at least one feature.protected final FeatureSetThe 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 TypeMethodDescriptionvoidadd(Iterator<? extends AbstractFeature> features) Appends new feature instances in theFeatureSet.voidclose()Releases resources used by this updater.protected abstract PathCreates an initially empty temporary file.protected abstract StaxStreamWritercreateWriter(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.voidflush()Writes immediately all feature instances.protected final LocaleReturns the locale to use for locale-sensitive data, ornullif unspecified.private booleanisEmpty()Returnstrueif there is currently no data.booleanremoveIf(Predicate<? super AbstractFeature> filter) Removes all feature instances from theFeatureSetwhich matches the given predicate.voidreplaceIf(Predicate<? super AbstractFeature> filter, UnaryOperator<AbstractFeature> updater) Updates all feature instances from theFeatureSetwhich 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, ornullif 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, ornullif 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, ornullif unspecified. This is not for logging or warning messages.- Returns:
- the data locale, or
null.
-
isEmpty
Returnstrueif 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 theFeatureSetwill 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 theFeatureSetwhich matches the given predicate.- Parameters:
filter- a predicate which returnstruefor feature instances to be removed.- Returns:
trueif 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 theFeatureSetwhich match the given predicate. If the given operator returnsnull, then the filtered feature is removed.- Parameters:
filter- a predicate which returnstruefor feature instances to be updated.updater- operation called for each matchingFeatureinstance. 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, ornullfor 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:
closein interfaceAutoCloseable
-