Class RewriteOnUpdate

java.lang.Object
org.apache.sis.internal.storage.xml.stream.RewriteOnUpdate
All Implemented Interfaces:
AutoCloseable
Direct Known Subclasses:
Updater

public abstract class RewriteOnUpdate extends Object implements AutoCloseable
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 Details

    • source

      protected final FeatureSet source
      The set of features to update. This is the set specified at construction time.
    • location

      private final Path location
      The main file, or null if unknown.
    • isSourceEmpty

      private boolean isSourceEmpty
      Whether the store is initially empty. It may be the underlying file does not exist or has a length of zero.
    • filtered

      private Stream<? extends AbstractFeature> filtered
      The features to write, fetched when first needed.
      See Also:
    • modified

      private boolean modified
      A flag telling whether removeIf(Predicate) removed at least one feature.
  • Constructor Details

    • RewriteOnUpdate

      public RewriteOnUpdate(FeatureSet source, Path location) throws IOException
      Creates an updater for the given source of features.
      Parameters:
      source - the set of features to update.
      location - the main file, or null if unknown.
      Throws:
      IOException - if an error occurred while determining whether the file is empty.
  • Method Details

    • getLocale

      protected final Locale getLocale()
      Returns the locale to use for locale-sensitive data, or null if unspecified. This is not for logging or warning messages.
      Returns:
      the data locale, or null.
    • isEmpty

      private boolean isEmpty() throws ReadOnlyStorageException
      Returns true if there is currently no data.
      Throws:
      ReadOnlyStorageException
    • filtered

      private Stream<? extends AbstractFeature> filtered() throws DataStoreException
      Returns the features to write.
      Throws:
      DataStoreException - if the feature stream cannot be obtained.
    • features

      protected Stream<? extends AbstractFeature> features() throws DataStoreException
      Returns the stream of features to copy. The default implementation delegates to FeatureSet.features(boolean).
      Returns:
      all features contained in the dataset.
      Throws:
      DataStoreException - if an error occurred while fetching the features.
    • add

      public void add(Iterator<? extends AbstractFeature> features) throws DataStoreException
      Appends new feature instances in the FeatureSet. Any feature already present in the FeatureSet will remain unmodified.
      Parameters:
      features - feature instances to append in the FeatureSet.
      Throws:
      DataStoreException - if the feature stream cannot be obtained or updated.
    • removeIf

      public boolean removeIf(Predicate<? super AbstractFeature> filter) throws DataStoreException
      Removes all feature instances from the FeatureSet which matches the given predicate.
      Parameters:
      filter - a predicate which returns true 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 the FeatureSet which match the given predicate. If the given operator returns null, then the filtered feature is removed.
      Parameters:
      filter - a predicate which returns true for feature instances to be updated.
      updater - operation called for each matching Feature instance. May return null.
      Throws:
      DataStoreException - if the feature stream cannot be obtained or updated.
    • createTemporaryFile

      protected abstract Path createTemporaryFile() throws IOException
      Creates an initially empty temporary file.
      Returns:
      the temporary file.
      Throws:
      IOException - if an error occurred while creating the temporary file.
    • createWriter

      protected abstract StaxStreamWriter createWriter(OutputStream temporary) throws Exception
      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, or null 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 be DataStoreException, IOException, RuntimeException, etc.
    • flush

      public void flush() throws DataStoreException
      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. If flush() has not been invoked, data may be lost. This method is useful in try-with-resource in case something fails before flush() invocation.
      Specified by:
      close in interface AutoCloseable