cas

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 15 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Examples

Constants

This section is empty.

Variables

View Source
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

func ParseDigest(value string) (Digest, error)

ParseDigest parses the canonical SHA-256 representation.

func (Digest) String

func (digest Digest) String() string

String returns the canonical algorithm-prefixed 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.

func (*Store) Put

func (store *Store) Put(ctx context.Context, reader io.Reader) (digest Digest, object objectstore.Object, returnErr error)

Put streams content through an owner-only temporary file and stores it once.

func (*Store) Stat

func (store *Store) Stat(ctx context.Context, digest Digest) (objectstore.Object, error)

Stat returns cached or authoritative metadata for a digest.

Jump to

Keyboard shortcuts

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