store

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ObjectPath

func ObjectPath(bucketID, objectID string) string

ObjectPath returns the relative location_ref for a given bucket and object ID. Uses two-level hash directory: <bucket-id>/objects/<id[0:2]>/<id[2:4]>/<id>

func PartPath

func PartPath(uploadID string, partNumber int) string

PartPath returns the relative path for a multipart part.

Types

type Store

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

Store manages physical object files on the filesystem.

func New

func New(dataDir string) (*Store, error)

New creates a Store and ensures the required directory structure exists.

func (*Store) AssembleParts

func (s *Store) AssembleParts(bucketID, objectID string, partLocations []string) (checksum string, size int64, locationRef string, err error)

AssembleParts concatenates parts into a final object file. Returns the SHA-256 checksum, total size, and location ref of the assembled object.

func (*Store) BucketObjectsDir

func (s *Store) BucketObjectsDir(bucketID string) string

BucketObjectsDir returns the absolute path to a bucket's objects directory. Callers that only need to walk a bucket's directory tree should use this instead of constructing paths manually.

func (*Store) CleanTmp

func (s *Store) CleanTmp() (int, error)

CleanTmp removes all files in the tmp directory. Best-effort: an error on one entry is logged and the remaining entries are still attempted. Returns the count of files actually removed and the first error observed (if any).

func (*Store) Cleanup

func (s *Store) Cleanup(locationRef string)

Cleanup safely removes the file at locationRef, logging but not propagating errors. It is intended for best-effort cleanup when a subsequent operation (e.g. metadata commit) fails after a successful write.

func (*Store) CleanupUploadParts

func (s *Store) CleanupUploadParts(uploadID string) error

CleanupUploadParts removes all part files for an upload.

func (*Store) DataDir

func (s *Store) DataDir() string

DataDir returns the root data directory.

func (*Store) DeleteObject

func (s *Store) DeleteObject(locationRef string) error

DeleteObject removes the physical file at locationRef.

func (*Store) EnsureBucketDir

func (s *Store) EnsureBucketDir(bucketID string) error

EnsureBucketDir creates the objects directory for a bucket and fsyncs the parent to ensure the directory entry is durable.

func (*Store) ListBucketFiles

func (s *Store) ListBucketFiles(bucketID string) ([]string, error)

ListBucketFiles walks the bucket's objects directory and returns all object file paths (relative).

func (*Store) ObjectExists

func (s *Store) ObjectExists(obj *meta.Object) bool

ObjectExists checks if a physical object file exists at the location ref within this store.

func (*Store) Quarantine

func (s *Store) Quarantine(locationRef string) error

Quarantine moves a file from its location to the quarantine directory.

func (*Store) ReadObject

func (s *Store) ReadObject(locationRef string) (*os.File, error)

ReadObject opens the file at locationRef for reading. Caller must close it.

func (*Store) RemoveBucketDir

func (s *Store) RemoveBucketDir(bucketID string) error

RemoveBucketDir removes the bucket's directory tree. Only call after confirming no objects remain.

func (*Store) SafePath

func (s *Store) SafePath(locationRef string) (string, error)

SafePath validates locationRef and returns the absolute path. Use this whenever locationRef originates from untrusted or externally-stored input.

func (*Store) SetFsyncErrorHook

func (s *Store) SetFsyncErrorHook(fn func(err error))

SetFsyncErrorHook registers a callback invoked after any fsync failure in the store. Safe to call with nil to clear. Callers typically wire this to a metrics counter.

func (*Store) VerifyChecksum

func (s *Store) VerifyChecksum(locationRef, expected string) (bool, string, error)

VerifyChecksum reads the file at locationRef and returns whether its SHA-256 matches expected. Uses a 1 MiB buffered reader to reduce syscall overhead on large objects.

func (*Store) VerifyChecksumRateLimited

func (s *Store) VerifyChecksumRateLimited(locationRef, expected string, limiter *rate.Limiter) (bool, string, error)

VerifyChecksumRateLimited behaves like VerifyChecksum but throttles read bandwidth via limiter. If limiter is nil the read is unbounded. The limiter's burst size must be >= the chunk size (1 MiB); callers that construct the limiter should size the burst accordingly.

func (*Store) WriteObject

func (s *Store) WriteObject(bucketID, objectID string, body io.Reader) (checksum string, size int64, locationRef string, err error)

WriteObject streams body to a temp file, computes SHA-256, then atomically moves it to its final location. Returns checksum, size, and locationRef.

The sequence ensures durability:

  1. Write to temp file (.writing suffix so GC can identify in-flight writes)
  2. fsync temp file
  3. Rename directly to final path
  4. fsync parent directory

func (*Store) WritePart

func (s *Store) WritePart(uploadID string, partNumber int, body io.Reader) (checksum string, size int64, locationRef string, err error)

WritePart writes a multipart part to disk with fsync. Returns the SHA-256 checksum, size, and location ref.

Jump to

Keyboard shortcuts

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