Skip to content

refactor: reduce code duplication across storage, concatenation, and utility classes - #146

Merged
tomdesair merged 1 commit into
masterfrom
feature/pre-release-improvements
Oct 24, 2026
Merged

tomdesair merged 1 commit into
masterfrom
feature/pre-release-improvements

Conversation

@tomdesair

Copy link
Copy Markdown
Owner

Summary

This PR addresses the SonarCloud code duplication warnings across tus-java-server by extracting common utilities, introducing shared abstract base classes, and consolidating redundant logic across storage, concatenation, and checksum index classes.

Changes

  1. Thread Pool Lifecycle Consolidation:

    • Extracted Utils.createUploadExecutor, Utils.resizeThreadPoolExecutor, and Utils.shutdownExecutor in me.desair.tus.server.util.Utils.
    • Unified thread pool creation and dynamic resizing across AsyncChunkUploader, AbstractUploadStorageService, and test suites.
  2. Unified Prefix Sanitization:

    • Extracted Utils.sanitizePrefix and removed duplicate copies in S3Utils and AzureUtils.
    • Redirected callers in S3, Azure, and unit tests to invoke Utils.sanitizePrefix directly.
  3. Abstract Upload Storage Service (AbstractUploadStorageService):

    • Consolidated shared configuration fields, getters/setters, getUploadInfo(url, ownerKey), copyUploadTo, deduplication lookups (deduplicate, getUploadInfoByChecksum), and locking service replacement cleanup into AbstractUploadStorageService.
    • Added a default void copyUploadTo method on UploadStorageService.
    • Refactored S3StorageService and AzureBlobStorageService to extend AbstractUploadStorageService.
  4. Abstract Concatenation Service (AbstractConcatenationService):

    • Extracted shared partial upload retrieval (getPartialUploads), cumulative length calculation (calculateTotalLength), and completion validation (checkAllCompleted).
    • Added a default Long calculateTotalLength(List<UploadInfo>) method on UploadConcatenationService.
    • Refactored VirtualConcatenationService, S3ConcatenationService, and AzureBlobConcatenationService to extend AbstractConcatenationService.
  5. Checksum Key Index Parsing (ChecksumKey):

    • Added ChecksumKey.parseFromIndexRelativePath(String relativePath, String parentSuffix).
    • Centralized relative path splitting and key extraction across S3ChecksumIndex and AzureBlobChecksumIndex.
  6. Lock Wrapping & Stream Helpers:

    • Extracted wrapStreamWithLock and closeLockQuietly in TusFileUploadService.
    • Deduplicated supports(...) in download extension request handlers.

Verification & Metrics

  • SonarCloud / CPD Duplication: CPD duplication blocks ($\ge 50$ tokens) reduced to only 17 residual blocks across the codebase (total 282 lines, primarily unavoidable HTTP dispatch signatures).
  • Unit Tests: 100% passing (1,519+ test assertions).
  • Diff Line Coverage: 97.91% against master (exceeds the 95.00% threshold).
  • Backward Compatibility: Fully preserved; UploadInfo.serialVersionUID = -8751200491586638308L is unchanged.
  • Code Style: 100% compliant with Google Java Style (mvn -P codestyle com.spotify.fmt:fmt-maven-plugin:format).

…utility classes

- Extract thread pool executor creation and lifecycle management (createUploadExecutor, resizeThreadPoolExecutor, shutdownExecutor) and prefix sanitization (sanitizePrefix) to Utils
- Introduce AbstractUploadStorageService providing shared configuration, accessors, deduplication retrieval, executor management, and locking lifecycle handling for S3StorageService and AzureBlobStorageService
- Introduce AbstractConcatenationService providing shared partial upload validation, sorting, and length calculation for VirtualConcatenationService, S3ConcatenationService, and AzureBlobConcatenationService
- Centralize checksum index relative path parsing in ChecksumKey.parseFromIndexRelativePath
- Streamline lock wrapping in TusFileUploadService and deduplicate handler checks
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 94.911% (+0.2%) from 94.69% — feature/pre-release-improvements into master

@tomdesair
tomdesair merged commit 3119590 into master Oct 24, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants