Documentation
¶
Overview ¶
Package storekit owns charly's storage ROOTS and its content-addressed STORE.
DESIGN puts both here: D-SDK-1 lists "the store and its roots (§17)" among the shared mechanisms an sdk package owns, and D-CORE-1 has core build "the store instance (built on the sdk store, §17)" — i.e. the module graph is core → sdk for exactly this. A roots or store implementation living anywhere else is the deviation this package exists to end: before it, the cache root was resolved inside `spec/cache` (under the CONFIG root, contrary to D-STORE-1) and four other packages built their own `.cache` paths by hand.
BUILDING THIS PACKAGE BEFORE `spec`'S TAG EXISTS — the same-module trap: a `go.work` cannot list one module twice, so two worktrees of THIS module (`github.com/opencharly/sdk`) cannot be bridged by a workspace; the error is that a workspace module is listed more than once. A workspace CAN list this module's worktree together with a `spec` worktree (two DIFFERENT modules), which is the legal pre-tag route; the durable route is a branch commit of this package that a caller fetches by path. The pushed form carries a real `require` on the spec tag — never a `replace`, and never a committed `go.work`.
TWO LAYERS, NEVER CONFUSED (the operator's one-store mandate):
- THE STORE is one OCI Image Layout per named store, under the ONE cache root. Every cached item is ONE OCI image manifest: its config blob carries the entry's validity metadata, its layers carry the payload, its annotations carry the cache key (`ai.opencharly.cache.key`) and the tag (`org.opencontainers.image.ref.name`). Blobs are content-addressed by digest, publication is atomic (tmp + rename), and reads take no lock.
- A VIEW is a materialization a consumer actually reads — a tree a loader scans, a binary a loader execs, a disk a hypervisor opens. Views are HARD-LINKED out of the store's own blobs and live INSIDE the store tree, so they are on one filesystem by construction, they add bytes exactly once, and deleting a view never orphans a blob.
D-STORE-6, and the one place it must not be misread: PAYLOAD layers carry no timestamps and no absolute paths; the validity metadata — which necessarily contains a resolution time — lives in the CONFIG blob, and the config blob is deliberately NOT part of the payload digest. Do not "fix" this by stripping the validity record or by putting a timestamp in a payload.
Index ¶
- Constants
- func BlobBytes(blobs []UnreferencedBlob) int64
- func CacheDir() (string, error)
- func DigestOf(data []byte) (string, error)
- func Dir(r Root) (string, error)
- func NamedStores() ([]string, error)
- func SameFile(a, b string) (bool, error)
- func StoreDir(name string) (string, error)
- type Entry
- type Orphan
- type Root
- type Store
- func (s *Store) BlobPath(dgst string) (string, error)
- func (s *Store) Blobs() ([]string, error)
- func (s *Store) DeleteEntry(key string) (bool, error)
- func (s *Store) Dir() string
- func (s *Store) Entries() ([]Entry, error)
- func (s *Store) HasBlob(dgst string) bool
- func (s *Store) LinkBlob(dgst, dest string) error
- func (s *Store) LookupEntry(key string) (*Entry, bool, error)
- func (s *Store) MaterializeTree(entryDigest, dest string) error
- func (s *Store) PutBlob(data []byte) (string, error)
- func (s *Store) PutEntry(e Entry) (string, error)
- func (s *Store) PutTree(dir, mediaType string) (string, error)
- func (s *Store) ReachableBlobs() (map[string]bool, error)
- func (s *Store) ReadBlob(dgst string) ([]byte, error)
- func (s *Store) ReadEntry(key string) (*Entry, error)
- func (s *Store) ReadTree(entryDigest string) (*TreeEntry, error)
- func (s *Store) RemoveBlob(dgst string) error
- func (s *Store) UnreferencedBlobs() ([]UnreferencedBlob, error)
- type TreeEntry
- type TreeFile
- type UnreferencedBlob
Constants ¶
const AnnotationCacheKey = "ai.opencharly.cache.key"
AnnotationCacheKey is the annotation that carries an entry's raw cache key. It is the SAME name `spec/cache` uses, deliberately: one key vocabulary across every store, so a reader that knows one store knows them all.
const AnnotationRefName = "org.opencontainers.image.ref.name"
AnnotationRefName is the OCI index descriptor's ref-name annotation, and it is the SAME name the image spec and `spec/cache` use. Named rather than spelled out at each use site because there are four — LookupEntry's match, indexEntry's write, indexEntry's replace test, and the sweep's delete — and a vocabulary that must agree across three packages may not be four string literals that happen to be equal today.
const LayoutFile = "oci-layout"
LayoutFile is the marker that makes a directory an OCI image layout, and therefore a store.
const MediaTypeStoreEntry = "application/vnd.opencharly.store.entry.v1+json"
MediaTypeStoreEntry is the media type of a store entry manifest.
Variables ¶
This section is empty.
Functions ¶
func BlobBytes ¶ added in v0.2026288.2236
func BlobBytes(blobs []UnreferencedBlob) int64
BlobBytes sums the sizes of a candidate list, for a report line.
func CacheDir ¶
CacheDir resolves the cache root: `$XDG_CACHE_HOME/charly`, or `CHARLY_CACHE_DIR` when set. Every named store lives under it.
func DigestOf ¶
DigestOf renders the sha256 content address of a byte slice, in the `sha256:<hex>` form the store and the artifact annotations use.
func Dir ¶
Dir resolves one of the four roots. It is THE resolver: no caller builds a `.cache` path by hand, and there is exactly one environment override in the whole system (`CHARLY_CACHE_DIR`, on the cache root — TODO 7.2 counts the extra per-cache variables this replaces, and they are being deleted one per migration).
func NamedStores ¶
NamedStores lists every named store under the cache root, sorted. A store is a directory that LOOKS like an OCI image layout (it carries the `oci-layout` marker), so unrelated state under the root — lock directories, transient trees — is never mistaken for a store. A missing root is an empty list, never an error.
Types ¶
type Entry ¶
type Entry struct {
// Key is the caller's cache key: the identity of the INPUTS, not of the bytes. It is
// opaque to the store and is never derived from the payload.
Key string `json:"key"`
// MediaType is what the payload IS (an RFC 6838 type the consumer understands).
MediaType string `json:"media_type"`
// Payload is the single payload blob's digest ("" for a tree entry).
Payload string `json:"payload,omitempty"`
// Tree is the tree manifest's digest ("" for a single-file entry).
Tree string `json:"tree,omitempty"`
// Annotations is anything else the writer wants recorded — a resolved commit, an etag, a
// submodule-completeness flag: the RESOLUTION RECORD D-STORE-5 needs, kept beside the key
// and outside the payload's digest on purpose (a payload must not carry a timestamp).
Annotations map[string]string `json:"annotations,omitempty"`
}
Entry is one keyed cache entry: what it IS, what KEY finds it, and where its payload lives.
Exactly one of Payload or Tree is set:
- Payload — a single content-addressed blob; its VIEW is LinkBlob(payload, path).
- Tree — a tree manifest (see tree.go); its VIEW is MaterializeTree(tree, dir), because a tree has no single payload digest by construction.
type Orphan ¶ added in v0.2026287.2117
Orphan is one discovered layout tree: its directory and the bytes a sweep would reclaim.
Bytes counts EVERY file in the tree — including the `oci-layout` marker that makes it a layout in the first place — because that is what `os.RemoveAll` actually reclaims. A size that omitted the marker under-reported a real sweep by the marker's own bytes (measured while writing the first consumer's assertion).
func OrphanedLayouts ¶ added in v0.2026287.2117
OrphanedLayouts returns the marker-carrying child layout trees of root, each sized, in ReadDir order (sorted by name, so a caller's output is stable).
The predicate is NamedStores() INVERTED, and that is the whole point ¶
NamedStores() calls a marker-carrying child of the cache root a STORE. The same directory, found inside a store's own tree, is junk: a store keeps every byte in `blobs/<alg>/<digest>` and its entries in `index.json`, and never reads a child layout. One marker constant (`LayoutFile`), read in two directions, so the two readings cannot drift apart.
The CALLER names the root, because liveness is policy and this is mechanism ¶
This function does NOT decide which layouts are orphaned, and it cannot: `NamedStores()` answers nothing here either — it returns exactly the marker-carrying children in question. A function that tried to guess "no merged writer still writes this" would be policy wearing a mechanism's name (the boundary law), so the contract is the opposite: the caller passes a root in which a marker-carrying child cannot be live. Two such roots exist, and both are the caller's to justify:
- A STORE'S OWN TREE (`Store.Dir()`), where a child layout is an orphan BY CONSTRUCTION — the shape a retired per-ref writer leaves behind when the store moves into the path it used to write under.
- A CACHE ROOT the migration table has RETIRED (a root no merged writer still writes), where the caller's justification is that retirement, recorded in that migration.
Discovery and sizing only — never deletion ¶
Removing is the caller's, so that a sweep's destructive half stays with the surface that also reports it (`--dry-run` first, then the removal). This function opens nothing for writing.
A root that does not exist yet has no orphans and is never an error — the same shape `NamedStores()` uses for a missing cache root.
type Root ¶
type Root string
Root names one of the four D-STORE-1 roots.
const ( // RootConfig — `$XDG_CONFIG_HOME/charly`: what the USER sets. RootConfig Root = "config" // RootCache — `$XDG_CACHE_HOME/charly`: everything content-addressed and re-derivable. // The single `CHARLY_CACHE_DIR` override relocates this one and therefore every store. RootCache Root = "cache" // RootState — `$XDG_STATE_HOME/charly`: durable state that is NOT cache — install // ledgers, process locks, anything a restart must not lose and a `cache clear` must not // touch. RootState Root = "state" // RootRuntime — `$XDG_RUNTIME_DIR/charly`: per-boot runtime state. RootRuntime Root = "runtime" )
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
A STORE TREE CONTAINS NO LOCKS. Measured on a live store (the wave lead's recon): one store directory carried a `.lock` file and a populated `locks/` tree beside its `oci-layout` and `blobs/`. That is a cache holding a lock, and the homes rule this codebase adopted is explicit: a lock is transient runtime state with no content address — a digest-keyed lock could even be SHARED between two processes that must not share it — so it belongs to the state root (host-global arbitration) or the runtime root (per-boot), never in a store.
This mechanism therefore never places a lock inside a store's tree, and NamedStores()'s layout-marker test exists so a stray `locks/` directory under the cache root is never mistaken for a store. Where the CURRENT `spec/cache` store does place one, that is that store's debt (opencharly/spec#233), not something a consumer should work around.
Store is ONE named OCI image layout under the cache root. Its `blobs/<alg>/<digest>` files ARE the cached bytes — the only copy of them anywhere on the host — and every consumer reads either a blob directly or a VIEW hard-linked from it.
func OpenStoreDir ¶
OpenStoreDir opens the store rooted at dir (an existing layout, or a path a pull will populate).
func (*Store) BlobPath ¶
BlobPath returns the store's path for a content-addressed blob, so a caller can ASSERT that a view shares the store's bytes (`SameFile(store.BlobPath(d), viewPath)`) instead of trusting it.
There is deliberately NO ChmodBlob: a view is a HARDLINK, so chmod-ing the view changes the shared inode — the caller does `os.Chmod(viewPath, mode)` and the store stays out of file modes. Mode is not part of a digest, which is exactly why it is the consumer's concern and not an entry field.
func (*Store) DeleteEntry ¶ added in v0.2026288.2236
DeleteEntry drops an entry's index pointer, returning whether an entry was there.
It removes the POINTER, never the payload: the payload is content-addressed and may be shared with another entry, so freeing it is a decision only a reachability sweep can make. That is why this and UnreferencedBlobs are one change rather than two — a store whose index cannot drop an entry keeps every digest it ever wrote live forever, and a sweep over such a store would be correct about a set that can never shrink.
The read-modify-write is serialized through the SAME lock indexEntry uses, so a delete racing a put cannot lose the other's write. It deliberately does NOT sweep: the caller runs UnreferencedBlobs afterwards if it wants the bytes back.
func (*Store) Entries ¶
Entries lists every keyed entry in the store, sorted by key — the cache surface's inventory.
func (*Store) HasBlob ¶
HasBlob reports whether the store already holds a blob by digest — the primitive every caller needs BEFORE it does any work: a digest that is present is never fetched, copied or rebuilt.
func (*Store) LinkBlob ¶
LinkBlob materializes a VIEW of a stored blob at dest: a HARDLINK, never a copy.
There is deliberately NO fallback. A copy would be a second set of bytes (the operator's mandate is "cached once"), and a cross-device fallback would hide a store that is not one filesystem — the single way the property could silently stop being true. A link failure is therefore a hard error that names the reason.
func (*Store) LookupEntry ¶
LookupEntry resolves an entry BY KEY — one index read, no payload bytes touched and no directory walk, which is what lets a caller decide HIT/MISS before fetching anything.
func (*Store) MaterializeTree ¶
MaterializeTree reconstructs a stored tree at dest as a tree of HARDLINKS to the store's blobs. Nothing is copied, and the file set is created deterministically (paths sorted) so a partial failure is reproducible rather than order-dependent.
func (*Store) PutBlob ¶
PutBlob writes bytes into the store as a content-addressed blob and returns their digest. A blob that is already present is left untouched (the same bytes are the same file), and the write is atomic — tmp + rename — so a concurrent reader never sees a partial blob.
func (*Store) PutEntry ¶
PutEntry stores an entry and indexes it by its key. It returns the entry manifest's digest. A later PutEntry with the same key REPLACES the index pointer; the previous payload stays in the store as the content-addressed blob it is (immutable, and still reachable by digest).
func (*Store) PutTree ¶
PutTree stores a directory as a tree entry and returns the digest of its MANIFEST blob (the entry's identity). Every file is a content-addressed blob, so identical files across trees — and across releases — are stored once; a file already present is not rewritten.
func (*Store) ReachableBlobs ¶ added in v0.2026288.2236
ReachableBlobs returns every blob digest that the store still needs, directly or through a tree manifest.
It is the reference set a sweep subtracts from Blobs(), exposed because a caller may want to reason about liveness without listing the unreferenced side. A store with no entries reaches nothing and returns an empty set, not an error: an unpopulated store is empty, never broken — the same shape NamedStores() and OrphanedLayouts() both use.
Three references, and the one a first draft misses ¶
The index descriptor for each key carries the ENTRY MANIFEST's digest, and that manifest is itself a blob — the thing PutEntry returns. So an entry reaches blobs by three routes:
index descriptor .Digest -> the entry manifest blob entry.Payload -> one payload blob entry.Tree -> the tree's MANIFEST blob -> TreeEntry.Files[*].Digest -> many
The first is easy to miss, because Entry (the decoded manifest) contains no field for it: the pointer lives in `index.json`, one level up. A sweep that reads only Entries() therefore calls every entry manifest unreferenced and deletes the store's own descriptors — measured here by a test that failed on exactly that, naming a 142-byte blob where an 11-byte payload was expected.
func (*Store) ReadBlob ¶
ReadBlob returns a blob's bytes, VERIFIED against the digest they are addressed by. Content addressing is the point: it is what makes a store entry immutable and a cache hit safe.
func (*Store) RemoveBlob ¶ added in v0.2026288.2236
RemoveBlob deletes ONE blob by digest. It is the caller's destructive half, never implicit.
It REFUSES a digest an entry still reaches: a sweep that has not re-read the store since it listed candidates must not be able to free live bytes on a second call. That check is one Entries() walk, and it is the difference between a GC and a delete primitive.
func (*Store) UnreferencedBlobs ¶ added in v0.2026288.2236
func (s *Store) UnreferencedBlobs() ([]UnreferencedBlob, error)
UnreferencedBlobs lists the store's blobs that no entry reaches, with sizes, sorted by digest.
READ-ONLY. It removes nothing — see the header. The caller decides what to do with the list, and RemoveBlob is how it does it.
type TreeEntry ¶
type TreeEntry struct {
// MediaType is the tree's own RFC 6838 media type — never a bare "tar" by convention.
MediaType string `json:"media_type"`
// Files maps a slash-separated RELATIVE path to its blob and mode.
Files map[string]TreeFile `json:"files"`
}
TreeEntry is a stored directory payload: every file by content address, plus the media type that says what the tree IS (e.g. a BuildKit cache export, a repo view).
type TreeFile ¶
type TreeFile struct {
// Digest is the file's content address (its blob in this store).
Digest string `json:"digest"`
// Mode is the file's permission bits. Mode is NOT part of a digest, so it travels here
// and is re-applied on materialization.
Mode uint32 `json:"mode"`
}
TreeFile is one file inside a stored tree.
type UnreferencedBlob ¶ added in v0.2026288.2236
type UnreferencedBlob struct {
// Digest is the blob's content address, as Blobs() reports it (`<alg>:<hex>`).
Digest string
// Size is the blob file's length in bytes.
Size int64
}
UnreferencedBlob is one blob no entry reaches, with the bytes it occupies.
Size is the file's own length, which is what removing it frees: a blob is content-addressed and linked — never copied — into views, so its own bytes are the only bytes attributable to it here. A caller that reports a total should say "up to", for the same reason `/charly-core:clean` says it of `--deep`: a link means the freed space can be less than the sum.