storage

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const DeadlockDetectionInterval = 10 * time.Second

DeadlockDetectionInterval is how often the deadlock detector scans for stuck locks.

View Source
const DefaultLockTimeout = 30 * time.Second

DefaultLockTimeout is the default maximum time to wait for lock acquisition.

View Source
const StaleLockerThreshold = 5 * time.Minute

StaleLockerThreshold is how long a lock can be held before it's considered stale.

View Source
const (
	// TempFilePrefix is used to identify temporary files during atomic writes
	TempFilePrefix = ".beamdrop_tmp_"
)

Variables

View Source
var (
	ErrInvalidBucketName = errors.New("invalid bucket name")
	ErrBucketNotFound    = errors.New("bucket not found")
	ErrBucketNotEmpty    = errors.New("bucket is not empty")
	ErrBucketExists      = errors.New("bucket already exists")
	ErrObjectNotFound    = errors.New("object not found")
	ErrInvalidKey        = errors.New("invalid object key")
)
View Source
var (
	// ErrLockTimeout is returned when a lock cannot be acquired within the timeout period.
	ErrLockTimeout = errors.New("lock acquisition timed out")
	// ErrDeadlock is returned when a potential deadlock is detected.
	ErrDeadlock = errors.New("potential deadlock detected")
)

Functions

func AtomicWriteFile

func AtomicWriteFile(targetPath string, r io.Reader) (int64, error)

AtomicWriteFile is a convenience function for simple atomic writes

func CleanupOrphanedTempFiles

func CleanupOrphanedTempFiles(rootDir string) error

CleanupOrphanedTempFiles removes any temporary files left from interrupted writes. Call this on application startup.

func ValidateBucketName

func ValidateBucketName(name string) error

ValidateBucketName checks if a bucket name is valid

func ValidateMaxStorage added in v0.2.0

func ValidateMaxStorage(dir string, maxBytes int64) error

ValidateMaxStorage checks that maxBytes does not exceed the filesystem's total capacity.

func ValidateObjectKey

func ValidateObjectKey(key string) error

ValidateObjectKey checks if an object key is valid

Types

type AtomicWriter

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

AtomicWriter provides atomic file write operations using temp file + fsync + rename

func NewAtomicWriter

func NewAtomicWriter(targetPath string) (*AtomicWriter, error)

NewAtomicWriter creates a new atomic writer for the target path. The file is written to a temporary location first, then atomically renamed.

func (*AtomicWriter) Abort

func (aw *AtomicWriter) Abort() error

Abort cancels the write and cleans up the temp file

func (*AtomicWriter) Commit

func (aw *AtomicWriter) Commit() error

Commit finalizes the atomic write: fsync + rename This is the critical method that ensures atomicity

func (*AtomicWriter) ReadFrom

func (aw *AtomicWriter) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom implements io.ReaderFrom for efficient copying

func (*AtomicWriter) TempPath

func (aw *AtomicWriter) TempPath() string

TempPath returns the path to the temporary file (useful for progress tracking)

func (*AtomicWriter) Write

func (aw *AtomicWriter) Write(p []byte) (n int, err error)

Write implements io.Writer

type BucketInfo

type BucketInfo struct {
	Name      string    `json:"name"`
	CreatedAt time.Time `json:"createdAt"`
}

BucketInfo contains bucket metadata

type BucketManager

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

BucketManager handles bucket filesystem operations

func NewBucketManager

func NewBucketManager(sharedDir string) *BucketManager

NewBucketManager creates a new bucket manager

func (*BucketManager) BucketExists

func (bm *BucketManager) BucketExists(name string) bool

BucketExists checks if a bucket exists

func (*BucketManager) CreateBucket

func (bm *BucketManager) CreateBucket(name string) error

CreateBucket creates a new bucket directory

func (*BucketManager) CreateBucketIfNotExists added in v0.2.0

func (bm *BucketManager) CreateBucketIfNotExists(name string) (created bool, err error)

CreateBucketIfNotExists creates a new bucket directory if it doesn't already exist. Returns (true, nil) if the bucket was newly created, (false, nil) if it already existed.

func (*BucketManager) DeleteBucket

func (bm *BucketManager) DeleteBucket(name string) error

DeleteBucket deletes a bucket if it's empty

func (*BucketManager) EnsureBucketsDir

func (bm *BucketManager) EnsureBucketsDir() error

EnsureBucketsDir ensures the buckets directory exists

func (*BucketManager) GetBucketPath

func (bm *BucketManager) GetBucketPath(name string) (string, error)

GetBucketPath returns the filesystem path for a bucket

func (*BucketManager) ListBuckets

func (bm *BucketManager) ListBuckets() ([]BucketInfo, error)

ListBuckets returns all bucket names

type DirUsage added in v0.2.0

type DirUsage struct {
	UsedBytes    uint64    `json:"usedBytes"`
	TotalBytes   uint64    `json:"totalBytes,omitempty"`
	FreeBytes    uint64    `json:"freeBytes,omitempty"`
	UsagePercent float64   `json:"usagePercent"`
	LastUpdated  time.Time `json:"lastUpdated"`
}

DirUsage represents storage usage for a directory.

func GetDirStorageUsage added in v0.2.0

func GetDirStorageUsage(dir string) (DirUsage, error)

GetDirStorageUsage returns cached or freshly computed usage for dir.

type ListObjectsResult

type ListObjectsResult struct {
	Prefix         string       `json:"prefix"`
	Delimiter      string       `json:"delimiter,omitempty"`
	MaxKeys        int          `json:"maxKeys"`
	IsTruncated    bool         `json:"isTruncated"`
	Contents       []ObjectInfo `json:"contents"`
	CommonPrefixes []string     `json:"commonPrefixes,omitempty"`
}

ListObjectsResult contains the result of a list objects operation

type LockManager

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

LockManager provides per-object read/write locking with timeout and deadlock detection.

func NewLockManager

func NewLockManager(timeout time.Duration) *LockManager

NewLockManager creates a new LockManager with the given lock acquisition timeout. Pass 0 to use DefaultLockTimeout.

func (*LockManager) Close

func (lm *LockManager) Close()

Close shuts down the deadlock detector goroutine.

func (*LockManager) Lock

func (lm *LockManager) Lock(bucket, key string) (unlock func(), err error)

Lock acquires a write lock for the given bucket/key with timeout. Returns an unlock function that MUST be called when the write is done.

func (*LockManager) RLock

func (lm *LockManager) RLock(bucket, key string) (unlock func(), err error)

RLock acquires a read lock for the given bucket/key with timeout. Returns an unlock function that MUST be called when the read is done.

func (*LockManager) Stats

func (lm *LockManager) Stats() LockStats

Stats returns a snapshot of current lock statistics.

type LockStats

type LockStats struct {
	ActiveLocks int `json:"activeLocks"`
	WriteLocks  int `json:"writeLocks"`
	ReadLocks   int `json:"readLocks"`
}

LockStats contains a snapshot of lock manager state.

type ObjectInfo

type ObjectInfo struct {
	Key          string    `json:"key"`
	Size         int64     `json:"size"`
	LastModified time.Time `json:"lastModified"`
	ETag         string    `json:"etag"`
	ContentType  string    `json:"contentType,omitempty"`
}

ObjectInfo contains object metadata

type ObjectManager

type ObjectManager struct {
	LockManager *LockManager
	// contains filtered or unexported fields
}

ObjectManager handles object filesystem operations

func NewObjectManager

func NewObjectManager(sharedDir string) *ObjectManager

NewObjectManager creates a new object manager

func (*ObjectManager) DeleteObject

func (om *ObjectManager) DeleteObject(bucket, key string) error

DeleteObject removes an object from storage

func (*ObjectManager) GetObject

func (om *ObjectManager) GetObject(bucket, key string) (*os.File, *ObjectInfo, func(), error)

GetObject retrieves an object from storage. The caller receives an unlock function that MUST be called after the file is fully consumed.

func (*ObjectManager) HeadObject

func (om *ObjectManager) HeadObject(bucket, key string) (*ObjectInfo, error)

HeadObject returns object metadata without the content

func (*ObjectManager) ListObjects

func (om *ObjectManager) ListObjects(bucket, prefix, delimiter string, maxKeys int) (*ListObjectsResult, error)

ListObjects lists objects in a bucket with optional prefix filtering

func (*ObjectManager) PutObject

func (om *ObjectManager) PutObject(bucket, key string, reader io.Reader) (*ObjectInfo, error)

PutObject writes an object to storage

Jump to

Keyboard shortcuts

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