feat(M2): content-addressed BlobStore (disk CAS) 🧊

- internal/hub/blobstore: Store interface (Has/Put/Get/Delete) + Disk impl
- sharded root/<first2>/<hash>, atomic put (temp+rename, never over-existing),
  idempotent, ErrNotFound sentinel — the open-core seam (S3/R2 later)
- tests: roundtrip, idempotent, missing-errors, no temp leftovers
This commit is contained in:
shoemoney 2026-06-22 23:16:51 -05:00
parent 546270d4f4
commit 9788442f32
2 changed files with 201 additions and 0 deletions

View file

@ -0,0 +1,114 @@
// Package blobstore is devbox's content-addressed blob storage abstraction.
//
// Blobs (chunks and manifests) are keyed by their caller-supplied content hash.
// The disk implementation (v1) stores each blob at root/<first2>/<hash>; an
// S3/R2 implementation can satisfy the same Store interface for the hosted tier.
// The store is "dumb": it trusts the key is the content hash and does not verify
// — the hub layer verifies on ingest.
package blobstore
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
)
// ErrNotFound is returned by Get/Delete for a missing blob.
var ErrNotFound = errors.New("blobstore: blob not found")
// Store is a content-addressed blob store.
type Store interface {
Has(hash string) (bool, error) // does a blob with this hash exist?
Put(hash string, data []byte) error // idempotent store
Get(hash string) ([]byte, error) // ErrNotFound if absent
Delete(hash string) error // ErrNotFound if absent; used by GC
}
// Disk is a filesystem-backed Store rooted at a directory.
type Disk struct{ root string }
// NewDisk opens (creating if needed) a disk blob store at root.
func NewDisk(root string) (*Disk, error) {
if root == "" {
return nil, fmt.Errorf("blobstore: empty root")
}
if err := os.MkdirAll(root, 0o755); err != nil {
return nil, err
}
return &Disk{root: root}, nil
}
func (d *Disk) shardDir(hash string) string {
if len(hash) < 2 {
return filepath.Join(d.root, "_short")
}
return filepath.Join(d.root, hash[:2])
}
func (d *Disk) path(hash string) string {
return filepath.Join(d.shardDir(hash), hash)
}
// Has reports whether a blob with the given hash exists.
func (d *Disk) Has(hash string) (bool, error) {
_, err := os.Stat(d.path(hash))
if err == nil {
return true, nil
}
if errors.Is(err, fs.ErrNotExist) {
return false, nil
}
return false, err
}
// Put stores data under hash. It is idempotent (a no-op if the blob exists) and
// writes atomically via a temp file + rename, so a partial write never appears
// as a complete blob.
func (d *Disk) Put(hash string, data []byte) error {
if hash == "" {
return fmt.Errorf("blobstore: empty hash")
}
if ok, err := d.Has(hash); err != nil || ok {
return err
}
dir := d.shardDir(hash)
if err := os.MkdirAll(dir, 0o755); err != nil {
return err
}
tmp, err := os.CreateTemp(dir, ".tmp-*")
if err != nil {
return err
}
tmpName := tmp.Name()
defer os.Remove(tmpName) // no-op once renamed
if _, err := tmp.Write(data); err != nil {
tmp.Close()
return err
}
if err := tmp.Close(); err != nil {
return err
}
// Rename to a non-existent final path: atomic on POSIX, and on Windows too
// since we never rename over an existing file (Put is a no-op when present).
return os.Rename(tmpName, d.path(hash))
}
// Get returns the blob bytes for hash, or ErrNotFound.
func (d *Disk) Get(hash string) ([]byte, error) {
b, err := os.ReadFile(d.path(hash))
if errors.Is(err, fs.ErrNotExist) {
return nil, ErrNotFound
}
return b, err
}
// Delete removes a blob, returning ErrNotFound if it was absent.
func (d *Disk) Delete(hash string) error {
err := os.Remove(d.path(hash))
if errors.Is(err, fs.ErrNotExist) {
return ErrNotFound
}
return err
}

View file

@ -0,0 +1,87 @@
package blobstore
import (
"bytes"
"errors"
"os"
"path/filepath"
"testing"
)
// compile-time check: Disk satisfies Store.
var _ Store = (*Disk)(nil)
func TestPutHasGetDelete(t *testing.T) {
d, err := NewDisk(t.TempDir())
if err != nil {
t.Fatal(err)
}
const hash = "abcd1234"
data := []byte("hello chunk")
if ok, _ := d.Has(hash); ok {
t.Fatal("blob should not exist yet")
}
if err := d.Put(hash, data); err != nil {
t.Fatal(err)
}
if ok, err := d.Has(hash); err != nil || !ok {
t.Fatalf("expected blob to exist (ok=%v err=%v)", ok, err)
}
got, err := d.Get(hash)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, data) {
t.Fatalf("got %q, want %q", got, data)
}
// stored under 2-char shard
if _, err := os.Stat(filepath.Join(d.root, "ab", hash)); err != nil {
t.Fatalf("expected sharded path ab/%s: %v", hash, err)
}
if err := d.Delete(hash); err != nil {
t.Fatal(err)
}
if ok, _ := d.Has(hash); ok {
t.Fatal("blob should be gone after delete")
}
}
func TestPutIdempotent(t *testing.T) {
d, _ := NewDisk(t.TempDir())
if err := d.Put("ff00", []byte("v1")); err != nil {
t.Fatal(err)
}
// Put again with same key is a no-op (content-addressed: same key == same bytes).
if err := d.Put("ff00", []byte("v1")); err != nil {
t.Fatal(err)
}
got, _ := d.Get("ff00")
if string(got) != "v1" {
t.Fatalf("got %q", got)
}
}
func TestMissingErrors(t *testing.T) {
d, _ := NewDisk(t.TempDir())
if _, err := d.Get("nope"); !errors.Is(err, ErrNotFound) {
t.Fatalf("Get missing: want ErrNotFound, got %v", err)
}
if err := d.Delete("nope"); !errors.Is(err, ErrNotFound) {
t.Fatalf("Delete missing: want ErrNotFound, got %v", err)
}
}
func TestNoTempLeftBehind(t *testing.T) {
root := t.TempDir()
d, _ := NewDisk(root)
if err := d.Put("dead", []byte("x")); err != nil {
t.Fatal(err)
}
// the .tmp-* file must not survive a successful Put
matches, _ := filepath.Glob(filepath.Join(root, "de", ".tmp-*"))
if len(matches) != 0 {
t.Fatalf("temp files left behind: %v", matches)
}
}