@@ -10,16 +10,15 @@ import (
1010// ErrNotFound is returned when a blob or action result is not in the cache.
1111var ErrNotFound = errors .New ("cache: not found" )
1212
13- // CAS is an on-disk content-addressable store modeled on Bazel's disk cache.
14- // Blobs live at cas/<function>/< xx>/<hash> under the cache root, where <xx>
15- // is the first two hex characters of the hash. Because a blob's name is
16- // derived from its contents, entries never change once written: writers race
17- // benignly and readers can detect corruption by re-hashing.
13+ // CAS is an on-disk content-addressable store keyed by SHA-256, modeled on
14+ // Bazel's disk cache. Blobs live at cas/<xx>/<hash> under the cache root,
15+ // where <xx> is the first two hex characters of the hash. Because a blob's
16+ // name is derived from its contents, entries never change once written:
17+ // writers race benignly and readers can detect corruption by re-hashing.
1818//
19- // Blobs are keyed by BLAKE3 by default. Content with an externally declared
20- // SHA-256 checksum (remotely fetched plugins) is stored keyed by that
21- // checksum instead — see PutSHA256 — so fetches need no action cache entry:
22- // the declared checksum is the address.
19+ // Content with an externally declared checksum (remotely fetched plugins)
20+ // needs no action cache entry: the declared sha256 is the address, so it is
21+ // stored and loaded directly — see SHA256Digest.
2322type CAS struct {
2423 root string
2524 tmp string
@@ -34,25 +33,14 @@ func newCAS(root string) (*CAS, error) {
3433}
3534
3635func (c * CAS ) path (d Digest ) string {
37- return filepath .Join (c .root , "cas" , string ( d . function ()), d .Hash [:2 ], d .Hash )
36+ return filepath .Join (c .root , "cas" , d .Hash [:2 ], d .Hash )
3837}
3938
40- // Put stores a blob keyed by its BLAKE3 hash and returns its digest.
39+ // Put stores a blob and returns its digest. Writing is atomic: the blob is
40+ // staged in a temp file and renamed into place, so concurrent sqlc processes
41+ // never observe partial entries.
4142func (c * CAS ) Put (data []byte ) (Digest , error ) {
42- return c .put (DigestOf (data ), data )
43- }
44-
45- // PutSHA256 stores a blob keyed by its SHA-256 hash and returns its digest.
46- // Use this for content addressed by a checksum declared outside the cache,
47- // so later runs can load it with a bare SHA256Digest lookup.
48- func (c * CAS ) PutSHA256 (data []byte ) (Digest , error ) {
49- hash , _ := sum (SHA256 , data )
50- return c .put (Digest {Function : SHA256 , Hash : hash , SizeBytes : int64 (len (data ))}, data )
51- }
52-
53- // put writes a blob atomically: it is staged in a temp file and renamed into
54- // place, so concurrent sqlc processes never observe partial entries.
55- func (c * CAS ) put (d Digest , data []byte ) (Digest , error ) {
43+ d := DigestOf (data )
5644 path := c .path (d )
5745 // Skip the write only when an entry of the right size already exists; a
5846 // wrong-sized entry is corrupt and is atomically replaced by the rename
@@ -81,10 +69,9 @@ func (c *CAS) put(d Digest, data []byte) (Digest, error) {
8169 return d , nil
8270}
8371
84- // Get returns the blob for a digest. Contents are re-hashed with the
85- // digest's hash function before being returned; a corrupt entry is evicted
86- // and reported as ErrNotFound so callers simply redo the work that produced
87- // it.
72+ // Get returns the blob for a digest. Contents are re-hashed before being
73+ // returned; a corrupt entry is evicted and reported as ErrNotFound so
74+ // callers simply redo the work that produced it.
8875func (c * CAS ) Get (d Digest ) ([]byte , error ) {
8976 if ! d .valid () {
9077 return nil , ErrNotFound
@@ -97,7 +84,7 @@ func (c *CAS) Get(d Digest) ([]byte, error) {
9784 }
9885 return nil , fmt .Errorf ("cache: %w" , err )
9986 }
100- if hash , _ := sum ( d . function (), data ); hash != d .Hash {
87+ if DigestOf ( data ). Hash != d .Hash {
10188 os .Remove (path )
10289 return nil , ErrNotFound
10390 }
0 commit comments