Documentation
¶
Overview ¶
Package cas stores immutable content addressed by its SHA-256 digest.
Store.Put spools a body to an owner-only temporary file while hashing it, then writes it once under prefix/<hex digest> with IfAbsent while holding a fenced distributed-coordination lease for the digest, so concurrent writers of the same content upload it only once. Store.Open verifies every byte of a full read against the digest; ranged reads return exact bytes. An optional cache Cache keeps object metadata for Stat.
Index ¶
- Variables
- type Digest
- type Store
- func (store *Store) Open(ctx context.Context, digest Digest, byteRange *objectstore.ByteRange) (io.ReadCloser, objectstore.Object, error)
- func (store *Store) Put(ctx context.Context, reader io.Reader) (digest Digest, object objectstore.Object, returnErr error)
- func (store *Store) Stat(ctx context.Context, digest Digest) (objectstore.Object, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidDigest identifies a malformed SHA-256 digest. ErrInvalidDigest = errors.New("cas: invalid digest") // ErrDigestMismatch identifies content that does not match its address. ErrDigestMismatch = errors.New("cas: digest mismatch") )
Functions ¶
This section is empty.
Types ¶
type Digest ¶
type Digest [32]byte
Digest is a SHA-256 content address.
func ParseDigest ¶
ParseDigest parses the canonical SHA-256 representation.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store keeps immutable SHA-256-addressed objects. It is safe for concurrent use, also by many processes that share the object store and the coordination backend.
func New ¶
func New(objects objectstore.Store, bucket objectstore.Bucket, prefix string, metadata cache.Cache[string, objectstore.Object], backend coordination.Backend, owner string) (*Store, error)
New constructs a content-addressed store.
Example ¶
package main
import (
"context"
"fmt"
"io"
"strings"
coordinationmemory "github.com/justusschock/distributed-coordination/memory"
"github.com/justusschock/object-storage/backend/memory"
"github.com/justusschock/object-storage/cas"
)
func main() {
// A nil metadata cache disables caching; the coordination backend fences
// concurrent writers of the same content.
store, err := cas.New(memory.New(), "blobs", "sha256", nil, coordinationmemory.NewBackend(nil), "writer-1")
if err != nil {
panic(err)
}
ctx := context.Background()
digest, _, err := store.Put(ctx, strings.NewReader("hello"))
if err != nil {
panic(err)
}
body, _, err := store.Open(ctx, digest, nil)
if err != nil {
panic(err)
}
defer func() { _ = body.Close() }()
data, err := io.ReadAll(body) // verified against digest while streaming
if err != nil {
panic(err)
}
fmt.Println(digest, string(data))
}
Output: sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 hello
func (*Store) Open ¶
func (store *Store) Open(ctx context.Context, digest Digest, byteRange *objectstore.ByteRange) (io.ReadCloser, objectstore.Object, error)
Open opens a digest. Full reads are always verified while streaming; ranged reads are checked against the stored digest when the backend reports one.