memory

package
v0.1.29 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrEvicted error = errors.New("the blob has been evicted or deleted from the store") // TODO - rename to ErrGone

ErrEvicted is returned when a user tries operating on a blob's *File after the blob has been evicted or deleted.

View Source
var ErrNoSpace error = errors.New("cannot free enough memory for new entry")

ErrNoSpace means the store could not free enough space for a new entry.

Functions

This section is empty.

Types

type Config

type Config struct {
	// GOMEMLIMITBytes and GOGC tune the Go GC. In short, it is advised to set GOMEMLIMIT to 90-95% of the container's reserved memory
	// and to turn GOGC off, assuming the container has reserved memory. More info on tuning the Go GC: https://go.dev/doc/gc-guide.
	//
	// If set, they overwrite any previous configurations for GOMEMLIMIT and GOGC.
	// GOMEMLIMIT *must* be configured either through [Config] or another way (e.g. an env var).
	// GOGC defaults to the Go default (100), if not set and is turned off if a negative value is provided.
	GOMEMLIMITBytes int64 `yaml:"gomemlimit_bytes"`
	// Check the comment at [Config.GOMEMLIMITBytes].
	GOGC int `yaml:"gogc"`
	// CapacityBytes sets a *hard* limit on the memory [Store] can keep reachable (i.e. unreclaimable by the GC) to store blobs.
	// Any other memory that [Store] uses (e.g. storing metadata) is NOT capped by CapacityBytes.
	CapacityBytes uint64 `yaml:"capacity_bytes"`
}

Config configures Store.

type File

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

File represents an open handle to a blob in Store, similar to how an os.File is an open file descriptor to a file on disk. As soon as the blob is evicted, File's APIs starts returning ErrEvicted, as File no longer has a reference to its data, ensuring GC can clean it.

func (*File) Cancel

func (f *File) Cancel() error

func (*File) Close

func (f *File) Close() error

func (*File) Commit

func (f *File) Commit() error

func (*File) Off

func (f *File) Off() int64

Off returns the offset for the next Read or Write operation. Thread-safe with other File APIs ONLY after File's blob is evicted.

func (*File) Read

func (f *File) Read(p []byte) (n int, err error)

func (*File) ReadAt

func (f *File) ReadAt(p []byte, off int64) (n int, err error)

ReadAt implements io.ReaderAt. Thread-safe.

func (*File) Seek

func (f *File) Seek(off int64, whence int) (int64, error)

Seek implements io.Seeker. Not thread-safe.

func (*File) Size

func (f *File) Size() int64

Stat returns the blob's actual size, even if it differs from the size reported during Create. A return value of -1 represents ErrEvicted.

func (*File) Write

func (f *File) Write(p []byte) (n int, err error)

Write implements io.Writer.

func (*File) WriteAt

func (f *File) WriteAt(p []byte, off int64) (n int, err error)

WriteAt implements io.WriterAt. It is fully thread-safe.

type Store

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

Store is an in-memory, thread-safe, LRU cache for blobs and their metadata.Metadata.

  • Supports pagination of blobs during reading/writing, such that blobs don't need to be fully loaded into memory.

  • New blobs are considered 'incomplete', which unlists them from LRU eviction. The store can be scoped to work on only (in-)complete blobs.

  • The store prioritizes writing new blobs over reading existing ones. Therefore, blobs may get evicted while clients hold a *File to them. In such cases, ErrEvicted is returned.

  • All APIs are thread-safe. Parallel access to a single blob is allowed but clients must ensure they don't intervene with one another.

  • Supports (un-)marking blobs as non-evictable (needed when we want to ensure an entry does not get evicted before the client flushes it to disk).

func NewStore

func NewStore(config *Config, stats tally.Scope) (*Store, error)

NewStore initializes a new, empty *Store.

func (*Store) BanEviction

func (s *Store) BanEviction(key string) error

BanEviction marks a blob as unevictable by LRU eviction. It is idempotent. Usually used by clients to ensure a blob is not evicted before being flushed to disk.

func (*Store) Create

func (s *Store) Create(key string, sizeBytes uint64) (*File, error)

Create initializes a new, incomplete blob, reserves space for it, and returns a *File pointing to it. Incomplete entries cannot be automatically evicted. MarkComplete must be called once the blob is complete. The store uses `sizeBytes` for its eviction logic even if the blob's real size differs (which is tolerated).

func (*Store) Delete

func (s *Store) Delete(key string) error

Delete removes a blob and its metadata from the store. Returns os.ErrNotExist on missing entry.

func (*Store) DeleteMetadata

func (s *Store) DeleteMetadata(key string, mdSuffix string) error

DeleteMetadata removes a blob's metadata. No error returned if the metadata is not present.

func (*Store) GetMetadata

func (s *Store) GetMetadata(key string, md metadata.Metadata) (ok bool, err error)

GetMetadata populates `md` if the metadata is present. Returns os.ErrNotExist if key is not in store.

func (*Store) Has

func (s *Store) Has(key string) (inStore bool, inScope bool)

Has checks if the blob is in the store.

func (*Store) List

func (s *Store) List() []string

List returns the keys of all blobs (except those out of scope).

func (*Store) ListMetadata

func (s *Store) ListMetadata(key string) ([]metadata.Metadata, error)

ListMetadata returns all metadata.Metadata of key.

func (*Store) MarkComplete

func (s *Store) MarkComplete(key string) error

MarkComplete marks the blob as fully written, which enlists it for LRU eviction (unless BanEviction has been called). It is idempotent. Additionally, other store APIs may filter blobs based on completeness.

func (*Store) Open

func (s *Store) Open(key string) (*File, error)

Open returns a *File pointing to the blob. *File APIs returns ErrEvicted once the blob gets evicted.

func (*Store) ScopeComplete

func (s *Store) ScopeComplete() *Store

ScopeComplete scopes Store's APIs such that they can only operate on complete blobs. storelib.ErrOutOfScope is returned if the user tries to operate on an incomplete blob.

func (*Store) ScopeIncomplete

func (s *Store) ScopeIncomplete() *Store

ScopeIncomplete scopes Store's APIs such that they can only operate on incomplete blobs. storelib.ErrOutOfScope is returned if the user tries to operate on a complete blob.

func (*Store) Scoped

func (s *Store) Scoped(scope storelib.BlobScope) *Store

Scoped scopes Store's APIs, such that storelib.ErrOutOfScope is returned upon attempting to operate on blobs out of scope.

func (*Store) SetMetadata

func (s *Store) SetMetadata(key string, md metadata.Metadata) error

SetMetadata sets the respective metadata of the blob.

func (*Store) Stat

func (s *Store) Stat(key string) (size int64, err error)

Stat returns the blob's actual size, even if it differs from the size reported during Create.

func (*Store) UnbanEviction

func (s *Store) UnbanEviction(key string) error

UnbanEviction removes the effect of BanEviction for a blob. It is idempotent.

Jump to

Keyboard shortcuts

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