filecopy

package
v0.0.0-...-50df253 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package filecopy provides a high-performance file copying service with persistent caching. It creates temporary copies of files that can be reused across application restarts, significantly reducing I/O overhead for large files.

Key features:

  • Instance-based isolation: Different instance IDs maintain separate cache namespaces
  • Persistent caching: Temporary files survive application restarts
  • Automatic cleanup: Removes orphaned files and manages cache lifecycle
  • Thread-safe operations: Concurrent access is fully supported
  • Version management: Only keeps the latest version of each cached file

Index

Constants

View Source
const (
	// CleanupInterval defines how often to run unified cleanup (1 minute).
	// First run is delayed by CleanupInterval after manager initialization.
	CleanupInterval = 1 * time.Minute

	// OrphanFileCleanupThreshold defines when orphaned files should be cleaned up (10 minutes).
	OrphanFileCleanupThreshold = 10 * time.Minute

	// MaxCacheEntries defines the maximum number of files to keep in the cache to prevent memory leaks.
	MaxCacheEntries = 10000 // Reasonable limit for most use cases

	// RecentFileProtectionWindow prevents deletion of recently modified/accessed files.
	RecentFileProtectionWindow = 2 * CleanupInterval

	// DedupSkipWindow skips version deduplication for very recent files during periodic cleanup.
	DedupSkipWindow = CleanupInterval

	// PathHashHexLen limits path-hash length in filenames (increase to lower collision risk).
	PathHashHexLen = 12

	// DataHashHexLen limits data-hash length in filenames.
	DataHashHexLen = 16

	// MaxBaseNameLen limits base filename length in temp file naming.
	MaxBaseNameLen = 100
)

Configuration constants for cache management and behavior tuning.

View Source
const VersionDetection = VersionDetectContentHash

VersionDetection controls how data hash is computed. Adjust as needed.

Variables

This section is empty.

Functions

func GetTempCopy

func GetTempCopy(instanceID, originalPath string) (string, error)

GetTempCopy creates or retrieves a temporary copy of the specified file. It provides persistent caching with instance-based isolation.

Parameters:

  • instanceID: Unique identifier for the application instance (e.g., "app_v1.0", "service_name")
  • originalPath: Absolute path to the original file to copy

Returns:

  • string: Path to the temporary copy
  • error: Any error encountered during the operation

The function performs these operations:

  1. Checks in-memory cache for existing valid copy
  2. Scans disk for existing cached file that can be reused
  3. Creates new copy if none found, cleaning up old versions

Thread-safe for concurrent use.

func Shutdown

func Shutdown()

Shutdown performs graceful shutdown and cleanup of all resources (Public API). This cleans up all manager instances and allows for re-initialization if needed.

Types

type FileCopyManager

type FileCopyManager struct {
	// contains filtered or unexported fields
}

FileCopyManager manages temporary file copies with persistent caching capabilities. It provides thread-safe operations for creating, accessing, and cleaning up temporary files.

func (*FileCopyManager) GetTempCopy

func (fm *FileCopyManager) GetTempCopy(originalPath string) (string, error)

GetTempCopy implements optimized file copying with intelligent index-based lookup. This eliminates repeated directory scanning and provides O(1) lookup performance. Old file versions are cleaned up by the periodic cleanup worker, not here.

func (*FileCopyManager) Shutdown

func (fm *FileCopyManager) Shutdown()

Shutdown performs complete cleanup by removing all temporary files and cache entries. This method ensures clean resource deallocation with proper goroutine lifecycle management.

type FileIndexEntry

type FileIndexEntry struct {
	TempPath     string    // Path to the temporary file copy (immutable after creation)
	OriginalPath string    // Original source file path (protected by mu)
	Size         int64     // Size of the original file in bytes (immutable after creation)
	ModTime      time.Time // Modification time of the original file (immutable after creation)

	PathHash  string // Path hash for collision detection (immutable after creation)
	DataHash  string // Content hash for file integrity verification (immutable after creation)
	BaseName  string // Base name for multi-version cleanup (immutable after creation)
	Extension string // File extension (normalized, without leading dot) (immutable after creation)
	// contains filtered or unexported fields
}

FileIndexEntry represents an indexed temporary file with comprehensive metadata. It provides O(1) lookup and intelligent file lifecycle management. Thread-safe for concurrent access through atomic operations and mutex protection.

func (*FileIndexEntry) GetLastAccess

func (e *FileIndexEntry) GetLastAccess() time.Time

GetLastAccess returns the last access time in a thread-safe manner

func (*FileIndexEntry) GetOriginalPath

func (e *FileIndexEntry) GetOriginalPath() string

GetOriginalPath returns the original path in a thread-safe manner

func (*FileIndexEntry) SetLastAccess

func (e *FileIndexEntry) SetLastAccess(t time.Time)

SetLastAccess updates the last access time atomically

func (*FileIndexEntry) SetOriginalPath

func (e *FileIndexEntry) SetOriginalPath(path string)

SetOriginalPath updates the original path in a thread-safe manner

type VersionDetectionMode

type VersionDetectionMode int

Version detection policy for generating data hash in file naming and cache keys.

const (
	// VersionDetectContentHash computes data hash from entire file content (strong consistency).
	VersionDetectContentHash VersionDetectionMode = iota
	// VersionDetectSizeModTime computes data hash from size+modtime only (faster, weaker consistency).
	VersionDetectSizeModTime
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL