Class Model

java.lang.Object
de.spiritscorp.datasync.model.Model

public class Model extends Object
Core controller class responsible for managing high-performance file synchronization, directory scanning, and backup operations.

This class orchestrates the synchronization pipeline by utilizing multi-threaded file traversals to process source and destination structures simultaneously. It tracks file attributes, evaluates state deltas to isolate unique changes, detects duplicate files based on sizes or checksum configurations, and handles safe file transfers.

To ensure data integrity, structural backups are handled via a strict two-phase execution model: clearing obsolete files first (with optional local trashbin staging) before transferring new payloads. Internal storage maps are wrapped in synchronized structures to maintain thread safety during parallel operations.

  • Constructor Details

    • Model

      public Model(Logger logger, Map<Path, FileAttributes> sourceMap, Map<Path, FileAttributes> destMap)
      Constructs a new Model controller instance and sets up the central tracking components.

      Initializes the internal system logger for transaction auditing and binds the reference maps used for storing file attribute states on the source and destination sides.

      Parameters:
      logger - the active system logger instance used for operational and debug tracking
      sourceMap - the tracking map used to store and evaluate source file attributes
      destMap - the tracking map used to store and evaluate destination file attributes
  • Method Details

    • createMap

      public static final <K,V> Map<K,V> createMap()
      Creates a thread-safe, synchronized sorted map backed by a standard TreeMap.

      This helper method is crucial for concurrent environments where multiple threads need to read and write to the file mapping without risking memory corruption.

      Type Parameters:
      K - the type of keys maintained by this map (typically java.nio.file.Path)
      V - the type of mapped values (typically FileAttributes)
      Returns:
      a synchronized, thread-safe view of a newly instantiated TreeMap
    • scanSyncFiles

      public Map<Path, FileAttributes> scanSyncFiles(ArrayList<Path> sourcePathes, ArrayList<Path> destPathes, Long[] stats, ScanType deepScan, boolean subDir, boolean trashbin)
      Lists all files in both source and destination directories concurrently using dedicated threads.

      To maximize performance on multi-core systems, this method spawns two parallel threads: One for the source path scanning and one for the destination path scanning.

      After both threads have finished execution, the provided statistics array is populated with the size and byte metrics of both maps.

      Parameters:
      sourcePathes - an ArrayList containing the base directories of the source side
      destPathes - an ArrayList containing the base directories of the destination side
      stats - a Long array with a minimum length of 4 used to store the tracking results: index 0: Total number of files found in source, index 1: Total number of files found in destination, index 2: Total aggregated size of source files in bytes, index 3: Total aggregated size of destination files in bytes
      deepScan - the configuration determining the type and depth of the file parsing
      subDir - true to recursively scan all subdirectories, false to only process the root level
      trashbin - true to enable trashbin retention logic, false to bypass it
      Returns:
      a Map containing all paths where failures, permission issues, or structural conflicts occurred
    • getEqualsFiles

      public void getEqualsFiles()
      Compares the pre-loaded source and destination maps to isolate identical files.

      This method triggers the internal handlers to filter out matching files from both maps. After execution, both maps will only contain unique entries that require synchronization actions like copy, update, or delete.

    • getSyncFiles

      public ArrayList<Map<Path, FileAttributes>> getSyncFiles(Map<Path, FileAttributes> syncMap, Path sourcePath, Path destPath)
      Analyzes the file state differentials to categorize synchronization requirements.

      Evaluates file modification dates, sizes, or checksums between the source and destination targets. The results are split into three structural hitlists returned as an indexed list.

      Parameters:
      syncMap - the map tracking current synchronization history states
      sourcePath - the absolute base path of the source directory
      destPath - the absolute base path of the destination directory
      Returns:
      an ArrayList containing exactly three separate maps: index 0 (copySourceHitList): Files to be copied from source to destination, index 1 (copyDestHitList): Files to be copied back from destination to source, index 2 (delHitList): Files marked for deletion from the target directory
    • backupFiles

      public boolean backupFiles(int del, boolean logOn, Path destPath, boolean trashbin, Path trashbinPath)
      Executes the physical file backup sequence on the local storage system.

      To ensure a clean and predictable operation, this method enforces a strict two-phase execution order: Phase 1 (Purge) clears obsolete files from the target directory first, and Phase 2 (Transfer) physically copies new or updated files into the destination path.

      Parameters:
      del - the mode flag determining deletions (processed exclusively if set to 0)
      logOn - true to output detailed file paths and transaction logs to the system logger
      destPath - the absolute path to the target directory where files will be transferred to
      trashbin - true to move deleted files safely into a local trash bin structure
      trashbinPath - the absolute directory path representing the safe retention folder
      Returns:
      true if all file entries inside the tracking maps were successfully processed and cleared, false if unprocessed files remain due to operational faults or file system errors
    • syncFiles

      public boolean syncFiles(SyncJobContext ctx, ArrayList<Map<Path, FileAttributes>> result, Map<Path, FileAttributes> syncMap, Path sourcePath, Path destPath, boolean testOn)
      Synchronizes files bi-directionally between the configured directories.

      This function consumes the pre-calculated multi-hitlist results, transfers the newest file states, and structurally synchronizes both directories to reach an identical file state.

      Parameters:
      result - the calculated synchronization tracking lists containing the hit maps
      syncMap - the map tracking current synchronization history states
      sourcePath - the absolute base path of the source directory
      destPath - the absolute base path of the destination directory
      testOn - true to run a dry-run simulation which skips real I/O operations
      Returns:
      true if the entire synchronization pipeline completed without unexpected exceptions, false if errors occurred during file interaction
    • scanDublicates

      public Map<Path, FileAttributes> scanDublicates(ArrayList<Path> paths, Long... stats)
      Scans the selected target paths to locate and extract duplicate file structures.

      Utilizes a specialized duplicate scan handler that matches files based on identical parameters like sizing blocks or checksums. Any errors encountered during the filesystem traversal are collected and merged into the final state mapping.

      Parameters:
      paths - an ArrayList containing the directory paths that should be inspected for file duplicates
      Returns:
      a Map containing the duplicate paths mapped to their attributes, combined with failure reports