filecopy

package
v0.0.27 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 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 (
	// CleanupDelayAfterStart specifies the delay before starting cleanup after manager initialization (1 minute).
	CleanupDelayAfterStart = 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
)

Configuration constants for cache management and cleanup policies.

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 added in v0.0.21

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 added in v0.0.21

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 added in v0.0.21

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.

func (*FileCopyManager) Shutdown added in v0.0.21

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 added in v0.0.21

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 for proper categorization (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 added in v0.0.21

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

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

func (*FileIndexEntry) GetOriginalPath added in v0.0.21

func (e *FileIndexEntry) GetOriginalPath() string

GetOriginalPath returns the original path in a thread-safe manner

func (*FileIndexEntry) SetLastAccess added in v0.0.21

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

SetLastAccess updates the last access time atomically

func (*FileIndexEntry) SetOriginalPath added in v0.0.21

func (e *FileIndexEntry) SetOriginalPath(path string)

SetOriginalPath updates the original path in a thread-safe manner

Jump to

Keyboard shortcuts

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