key

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: GPL-3.0, LGPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package key implements the 32-byte Amber-Store lookup key: a content address that encodes a CAS object type, a logical payload length, and a truncated BLAKE3 hash of the payload's serialized bytes. See architecture/keys.md.

Index

Constants

View Source
const Size = 32

Size is the fixed byte length of every key.

Variables

View Source
var (
	// ErrBadKeyLength means the input to Parse was not exactly Size bytes.
	ErrBadKeyLength = errors.New("key: data is not 32 bytes")
	// ErrReservedBitSet means the header's reserved bit (bit 3) is set.
	ErrReservedBitSet = errors.New("key: reserved header bit is set")
	// ErrReservedType means the object type is reserved (5..15) or out of range.
	ErrReservedType = errors.New("key: reserved object type")
	// ErrNonCanonicalLength means the length field has leading-zero padding.
	ErrNonCanonicalLength = errors.New("key: non-canonical length encoding")
)

Sentinel errors returned by Parse, Validate, and the constructors. Wrap them with %w and match with errors.Is.

Functions

This section is empty.

Types

type Key

type Key [Size]byte

Key is a 32-byte lookup key. It is a value type and is directly comparable, so it can be used as a Go map key. Accessors assume the key is canonical (produced by New, NewFromHash, or Parse).

func New

func New(t Type, length uint64, serialized []byte) (Key, error)

New computes the BLAKE3-256 digest of serialized, then assembles a canonical key via NewFromHash. length is the logical payload length and is taken as given (it need not equal len(serialized) — see NewFromHash).

func NewFromHash

func NewFromHash(t Type, length uint64, fullHash [Size]byte) (Key, error)

NewFromHash assembles a canonical key from a CAS object type, a logical payload length, and a precomputed full 256-bit BLAKE3 digest. The digest is truncated to its leading bytes to fill the key. length is used verbatim: for Blob/XattrSet it is the serialized byte length; for FileNode/DirLeaf/DirNode it is the logical size (see architecture/types.md). Returns ErrReservedType if t is not a defined type.

func Parse

func Parse(b []byte) (Key, error)

Parse copies b into a Key and validates its canonical form. b must be exactly Size bytes.

func (Key) Hash

func (k Key) Hash() []byte

Hash returns the truncated payload hash bytes (len == Size-1-LengthSize).

func (Key) Length

func (k Key) Length() uint64

Length decodes the big-endian payload-length field.

func (Key) LengthSize

func (k Key) LengthSize() int

LengthSize returns the number of bytes the payload-length field occupies (1..8).

func (Key) String

func (k Key) String() string

String returns the lowercase hex encoding of the key, for logs and errors.

func (Key) Type

func (k Key) Type() Type

Type returns the CAS object type from the header's high nibble.

func (Key) Validate

func (k Key) Validate() error

Validate reports whether k is canonical: the reserved bit is clear, the type is defined (0..4), and the length field is minimally encoded (its first byte is non-zero, except for the single 0x00 byte that encodes a zero length).

type Type

type Type uint8

Type is the 4-bit CAS object type carried in the high nibble of a key's header byte (architecture/types.md).

const (
	Blob     Type = 0 // raw file-content byte chunk (a CDC leaf)
	FileNode Type = 1 // file chunk-index node
	DirLeaf  Type = 2 // a contiguous run of directory entries
	DirNode  Type = 3 // directory index node
	XattrSet Type = 4 // spilled extended attributes
)

func (Type) IsValid

func (t Type) IsValid() bool

IsValid reports whether t is a defined CAS object type (0..4). Types 5..15 are reserved and must not be emitted; values above 15 do not fit the 4-bit field.

func (Type) String

func (t Type) String() string

String returns the type name, or "Type(n)" for reserved/unknown values.

Jump to

Keyboard shortcuts

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