keys

package
v1.17.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package keys provides deterministic, collision-resistant cache key construction for OpenFGA's storage layer. It encodes heterogeneous fields into a compact TLV (type-length-value) byte sequence that is then hex-encoded for use as a cache key string.

Index

Constants

This section is empty.

Variables

View Source
var Seed uint64

Seed is the per-process random seed applied to all Digest instances. Randomizing prevents external observers from predicting or deliberately colliding hash outputs (hash-flooding mitigation).

Exported solely so tests can pin it to a known value for deterministic hash output. Production code must not modify Seed: Digest.Reset re-reads it on every call, so changing Seed at runtime invalidates the continuity of any in-flight or already-emitted digests.

Functions

This section is empty.

Types

type Array

type Array []Serializable

func (Array) WriteTo

func (a Array) WriteTo(kb *Builder)

type Bool

type Bool bool

func (Bool) WriteTo

func (b Bool) WriteTo(kb *Builder)

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder accumulates a TLV-encoded byte sequence from which a cache key can be derived. Each field is self-describing (tagged with type and length where applicable), so keys are unambiguous without delimiters.

func (*Builder) Bytes

func (kb *Builder) Bytes() []byte

Bytes returns the accumulated buffer contents. The returned slice is valid only until the next mutating call on the Builder.

func (*Builder) EncodeArray

func (kb *Builder) EncodeArray(a []Serializable)

EncodeArray writes a as a tagged sequence (tag + element count + each element's own TLV representation).

func (*Builder) EncodeArrayHeader

func (kb *Builder) EncodeArrayHeader(n int)

EncodeArrayHeader writes the tagArray framing (tag + element count) only. The caller must then write exactly n element encodings into the Builder. Mismatched counts produce an unparseable key — this is a discipline contract, not a checked invariant. Prefer EncodeArray when a []Serializable is already in hand; use the header when streaming elements directly to avoid materializing the slice.

func (*Builder) EncodeBool

func (kb *Builder) EncodeBool(b bool)

EncodeBool writes b as a tagged boolean (0x00 for false, 0x01 for true).

func (*Builder) EncodeByte

func (kb *Builder) EncodeByte(b byte)

EncodeByte writes b as a tagged single-byte value.

func (*Builder) EncodeBytes

func (kb *Builder) EncodeBytes(b []byte)

EncodeBytes writes b as a tagged, length-prefixed byte slice.

func (*Builder) EncodeMap

func (kb *Builder) EncodeMap(entries []MapEntry)

EncodeMap writes entries as a tagged map (tag + entry count + flat key/value TLVs). The tagMap framing distinguishes maps from arrays, preventing collision between a dictionary and a positional sequence that happen to contain the same values. Use Pair only as a standalone Serializable; map entries are unframed inside tagMap.

func (*Builder) EncodeMapHeader

func (kb *Builder) EncodeMapHeader(n int)

EncodeMapHeader writes the tagMap framing (tag + entry count) only. The caller must then write exactly 2*n TLVs into the Builder, alternating key and value (flat layout — no per-entry tagPair framing inside tagMap). Mismatched counts or interleaving produce an unparseable key — this is a discipline contract, not a checked invariant.

func (*Builder) EncodeNull

func (kb *Builder) EncodeNull()

EncodeNull emits a bare tagNull marker. Distinct from EncodeBool(false) and EncodeByte(0) so that JSON-style null, boolean false, and the byte 0x00 never collide in the encoded key.

func (*Builder) EncodePair

func (kb *Builder) EncodePair(key Serializable, value Serializable)

EncodePair writes a key-value pair with structural markers separating the key and value portions.

func (*Builder) EncodeString

func (kb *Builder) EncodeString(s string)

EncodeString writes s as a tagged, length-prefixed UTF-8 string.

func (*Builder) EncodeUint64

func (kb *Builder) EncodeUint64(i uint64)

EncodeUint64 writes i as a tagged 8-byte little-endian integer.

func (*Builder) EncodeUnset

func (kb *Builder) EncodeUnset()

EncodeUnset emits a bare tagUnset marker. Distinct from EncodeNull so that "value absent / kind unset" (e.g., a structpb.Value with no Kind set) and an explicit JSON-style null cannot collide in the encoded key.

func (*Builder) Grow

func (kb *Builder) Grow(n int)

Grow increases the capacity of the internal buffer by at least n bytes, guaranteeing space for n more bytes of writes without reallocation.

func (*Builder) Key

func (kb *Builder) Key() Key

Key snapshots the current buffer contents into an immutable Key value.

func (*Builder) Reset

func (kb *Builder) Reset()

Reset discards all accumulated content, returning the buffer to empty while retaining its underlying storage for reuse.

func (*Builder) Serialize

func (kb *Builder) Serialize(value Serializable)

Serialize delegates encoding to the value's own WriteTo implementation.

func (*Builder) Write

func (kb *Builder) Write(b []byte) (int, error)

Write appends raw bytes to the buffer without type-tagging or length-prefixing. Use this for pre-encoded data or fixed prefixes that do not require TLV framing.

func (*Builder) WriteByte

func (kb *Builder) WriteByte(b byte) error

WriteByte appends a single raw byte to the buffer without type-tagging.

func (*Builder) WriteString

func (kb *Builder) WriteString(s string) (int, error)

WriteString appends the contents of s to the buffer without type-tagging or length-prefixing. It avoids the allocation that Write([]byte(s)) would incur for string-to-byte conversion.

type Byte

type Byte byte

func (Byte) WriteTo

func (b Byte) WriteTo(kb *Builder)

type Bytes

type Bytes []byte

func (Bytes) WriteTo

func (b Bytes) WriteTo(kb *Builder)

type Digest

type Digest struct {
	// contains filtered or unexported fields
}

Digest wraps xxhash.Digest with the package-level Seed applied automatically on construction and reset.

func NewDigest

func NewDigest() *Digest

NewDigest returns a new Digest initialized with the package-level Seed.

func (*Digest) BlockSize

func (hasher *Digest) BlockSize() int

func (*Digest) Reset

func (hasher *Digest) Reset()

Reset re-initializes the digest while preserving the package-level Seed.

func (*Digest) Size

func (hasher *Digest) Size() int

func (*Digest) Sum

func (hasher *Digest) Sum(b []byte) []byte

func (*Digest) Sum64

func (hasher *Digest) Sum64() uint64

func (*Digest) Write

func (hasher *Digest) Write(b []byte) (int, error)

func (*Digest) WriteString

func (hasher *Digest) WriteString(s string) (int, error)

type Key

type Key struct {
	// contains filtered or unexported fields
}

Key is an opaque, immutable cache key derived from TLV-encoded fields. It is comparable and safe for use as a map key or in concurrent data structures.

func (Key) Bytes

func (k Key) Bytes() []byte

Bytes returns the key contents. The returned slice must not be mutated. If mutation is necessary, copy the bytes into another slice.

func (Key) String

func (k Key) String() string

String returns the key contents as a hex-encoded string suitable for display. The hex encoding ensures the output is printable and safe for any backend that restricts key character sets.

type Map

type Map []MapEntry

Map is an ordered collection of key-value entries that serializes with the tagMap framing (tagMap + entry count + alternating key/value TLVs). Use Map (rather than Array of Pairs) when the source data is a dictionary or struct-like object whose entries must be distinguishable from a positional sequence.

func (Map) WriteTo

func (m Map) WriteTo(kb *Builder)

type MapEntry

type MapEntry struct {
	Key   Serializable
	Value Serializable
}

MapEntry is one entry inside a Map. Map entries use a flat key/value layout inside tagMap framing — no per-entry tagPair/tagKey/tagValue markers — so MapEntry is a plain value struct rather than a Serializable.

type Null

type Null struct{}

Null is a singleton sentinel that serializes as a bare tagNull marker. Use it (instead of Byte(0) or Bool(false)) when the source value is semantically "absence" rather than the boolean false or the byte 0x00.

func (Null) WriteTo

func (Null) WriteTo(kb *Builder)

type Pair

type Pair struct {
	Key   Serializable
	Value Serializable
}

Pair encodes a standalone key/value with tagPair/tagKey/tagValue framing. It is used when a pair must appear outside a Map (for example, as an element of an Array or as a top-level value) and the pair boundary must remain unambiguous. Inside a Map, use MapEntry instead.

func (Pair) WriteTo

func (p Pair) WriteTo(kb *Builder)

type PbValue

type PbValue structpb.Value

PbValue serializes an arbitrary structpb.Value (including nested structs and lists) into the Builder's TLV format. It uses an explicit stack rather than recursion to bound memory growth on deeply nested inputs.

func (*PbValue) WriteTo

func (pbvalue *PbValue) WriteTo(kb *Builder)

type PooledBuilder

type PooledBuilder struct {
	*Builder
	// contains filtered or unexported fields
}

PooledBuilder is a Builder obtained from a sync.Pool. Callers must call Close when finished to return it to the pool. The typical pattern:

b := keys.GetBuilder()
defer b.Close()

func GetBuilder

func GetBuilder() *PooledBuilder

GetBuilder retrieves a Builder from the pool, ready for use. The returned wrapper must not be copied: each copy carries its own closed flag, so calling Close on more than one copy would return the same Builder to the pool twice.

func (*PooledBuilder) Close

func (b *PooledBuilder) Close()

Close resets the builder and returns it to the pool. Close is idempotent; subsequent calls are no-ops.

type PooledDigest

type PooledDigest struct {
	*Digest
	// contains filtered or unexported fields
}

PooledDigest is a Digest obtained from a sync.Pool. Callers must call Close when finished to return it to the pool.

func GetDigest

func GetDigest() *PooledDigest

GetDigest retrieves a Digest from the pool, ready for use. Reset is called to ensure the digest uses the current package-level Seed. The returned wrapper must not be copied: each copy carries its own closed flag, so calling Close on more than one copy would return the same Digest to the pool twice.

func (*PooledDigest) Close

func (d *PooledDigest) Close()

Close resets the digest and returns it to the pool. Close is idempotent; subsequent calls are no-ops.

type Serializable

type Serializable interface {
	WriteTo(*Builder)
}

Serializable is implemented by types that can encode themselves into the Builder's TLV wire format.

type String

type String string

func (String) WriteTo

func (s String) WriteTo(kb *Builder)

type Tuple

type Tuple openfgav1.TupleKey

func (*Tuple) WriteTo

func (t *Tuple) WriteTo(kb *Builder)

type Uint64

type Uint64 uint64

func (Uint64) WriteTo

func (i Uint64) WriteTo(kb *Builder)

type Unset

type Unset struct{}

Unset is a singleton sentinel that serializes as a bare tagUnset marker. Use it when the source value is structurally absent (e.g., a oneof with no case selected) rather than an explicit JSON-style null, which is represented by Null.

func (Unset) WriteTo

func (Unset) WriteTo(kb *Builder)

Jump to

Keyboard shortcuts

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