raw

package
v0.23.2 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package raw is the explicit trusted Redis command boundary. Normal application features use Foundry's typed cache, data, lease and pub/sub APIs. Commands here require scoped keys and typed decoders, but arbitrary arguments and Lua source are trusted code: Foundry cannot prove that they only refer to declared keys.

Index

Constants

View Source
const (
	MaxArguments     = 256
	MaxCommands      = 256
	MaxRequestBytes  = 16 << 20
	MaxArgumentBytes = 1 << 20
	MaxScriptBytes   = 64 << 10
	MaxReplyBytes    = 16 << 20
	MaxReplyNodes    = 65536
	MaxReplyDepth    = 64
)

Variables

This section is empty.

Functions

func Ignore

func Ignore[R any](p *Pipeline, c Command[R]) error

Ignore still validates the typed decoder and every server/protocol error; it discards only the successful decoded value, never a failed command.

Types

type Argument

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

Argument snapshots one explicit raw value. Zero is invalid; empty Text/Bytes arguments are valid. Use Command.Key for additional keys, not Text(key.String()).

func Bytes

func Bytes(data []byte) Argument

Bytes copies data into an argument, returning an invalid argument if it exceeds MaxArgumentBytes.

func Int64

func Int64(value int64) Argument

Int64 constructs a signed decimal argument.

func Text

func Text(text string) Argument

Text snapshots text as an argument, returning an invalid argument if it exceeds MaxArgumentBytes.

func Uint64

func Uint64(value uint64) Argument

Uint64 constructs an unsigned decimal argument.

type Backend

type Backend interface {
	// ExecuteRaw executes one validated request and returns a bounded owned reply without retrying.
	ExecuteRaw(context.Context, Request, Limits) (Reply, error)
	// ExecuteRawBatch executes the batch in the selected mode and bounds its combined replies.
	ExecuteRawBatch(context.Context, []Request, Mode, Limits) ([]Reply, error)
	// ExpireRaw atomically changes an existing key's expiry while preserving its contents.
	ExpireRaw(context.Context, Key, cache.TTL) (bool, error)
}

Backend executes validated immutable requests without automatic retries. Batch errors may follow partial execution; Transaction errors do not roll back earlier successful commands. Capture and bound every reply, including ignored results. ExpireRaw uses one atomic native operation, preserving contents and returning true for an existing key even when Forever is already set. The caller owns the adapter lifecycle. A lost acknowledgement leaves the outcome unknown.

type Builder

type Builder[R any] struct {
	// contains filtered or unexported fields
}

Builder cannot execute until Key produces a Command. Prefix arguments support commands such as XGROUP CREATE whose key is not the first argument.

func NewCommand

func NewCommand[R any](name string, decoder Decoder[R]) Builder[R]

NewCommand constructs a builder with an uppercase command name and typed decoder. Invalid names and connection-state commands produce an invalid request.

func (Builder[R]) Arg

func (b Builder[R]) Arg(arg Argument) Builder[R]

Arg returns a builder with the explicit argument appended.

func (Builder[R]) Key

func (b Builder[R]) Key(key Key) Command[R]

Key appends an explicit scoped key and returns an executable command.

type Command

type Command[R any] struct {
	// contains filtered or unexported fields
}

Command owns immutable arguments and a concrete result decoder. Lua source and raw arguments are trusted; declared key checks are not a sandbox for that code.

func NewScript

func NewScript[R any](source string, key Key, decoder Decoder[R]) Command[R]

NewScript uses a single EVAL with typed KEYS and explicit ARGV, without script cache fallback. It can also be queued in a Pipeline like any other command.

func (Command[R]) Arg

func (c Command[R]) Arg(arg Argument) Command[R]

Arg returns a command with the explicit argument appended.

func (Command[R]) Key

func (c Command[R]) Key(key Key) Command[R]

Key returns a command with an additional explicit scoped key appended.

func (Command[R]) Run

func (c Command[R]) Run(ctx context.Context, s *Store) (R, error)

Run executes and decodes one command under the store's bounds, returning a zero result on failure.

type Config

type Config struct {
	Namespace                                   keyspace.Namespace
	MaxDeclarations, MaxKeyBytes, MaxConcurrent int
	Timeout                                     time.Duration
	Limits                                      Limits
}

Config sets the raw store namespace, declaration capacity, concurrency, timeout, and request limits.

func DefaultConfig

func DefaultConfig(ns keyspace.Namespace) Config

DefaultConfig returns bounded raw store defaults for the supplied namespace.

func (Config) Validate

func (c Config) Validate() error

Validate checks the namespace, store bounds, timeout, and request limits.

type Declaration

type Declaration[K any] struct {
	// contains filtered or unexported fields
}

Declaration retains a reusable raw key family identity and concrete key codec. Its zero value cannot bind.

func DefineKeys

func DefineKeys[K any](name Name, version Version, codec keyspace.Codec[K]) Declaration[K]

DefineKeys creates a reusable declaration without validating or binding it.

func (Declaration[K]) Bind

func (d Declaration[K]) Bind(s *Store) (Keys[K], error)

Bind validates the declaration and claims its name and version in the store without I/O.

type Decoder

type Decoder[R any] struct {
	// contains filtered or unexported fields
}

Decoder preserves the command's concrete result type. Custom callbacks must be deterministic, concurrency-safe and return owned values. Execution owns their lifetime and catches panic/Goexit without abandoning uncooperative callbacks.

func DecodeArray

func DecodeArray[R any](element Decoder[R]) Decoder[[]R]

DecodeArray decodes each array element in order and returns no partial result on failure.

func DecodeBool

func DecodeBool() Decoder[bool]

DecodeBool accepts integer replies of zero or one as false or true.

func DecodeBytes

func DecodeBytes() Decoder[[]byte]

DecodeBytes accepts a text reply and returns an owned byte slice.

func DecodeInt64

func DecodeInt64() Decoder[int64]

DecodeInt64 accepts an integer reply.

func DecodeNullable

func DecodeNullable[R any](element Decoder[R]) Decoder[value.Nullable[R]]

DecodeNullable maps a nil reply to null and delegates every other reply to element.

func DecodeString

func DecodeString() Decoder[string]

DecodeString accepts a text reply.

func DecodeUint64

func DecodeUint64() Decoder[uint64]

DecodeUint64 accepts a nonnegative integer reply.

func DecodeWith

func DecodeWith[R any](fn func(context.Context, Reply) (R, error)) Decoder[R]

DecodeWith retains a custom typed reply decoder without invoking it.

type Key

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

Key is an explicit adapter address. Obtain it from Keys.For or FromDataKey. Possessing a key does not statically verify arbitrary command/script arguments.

func FromDataKey

func FromDataKey(key data.Key) (Key, error)

FromDataKey validates and preserves a typed data address for explicit raw operations.

func NewKey

func NewKey(ns keyspace.Namespace, name Name, version Version, logical string) (Key, error)

NewKey validates and constructs a versioned raw address for adapter use.

func (Key) Namespace

func (k Key) Namespace() keyspace.Namespace

Namespace returns the key's application and environment namespace.

func (Key) String

func (k Key) String() string

String returns the physical address, or an empty string for the zero key.

func (Key) Validate

func (k Key) Validate() error

Validate checks that the key has address text and a valid namespace.

type Keys

type Keys[K any] struct {
	// contains filtered or unexported fields
}

Keys preserves the domain resource type while resolving explicit raw addresses.

func (Keys[K]) Delete

func (k Keys[K]) Delete(ctx context.Context, input K) (bool, error)

Delete removes one resource and reports whether it existed.

func (Keys[K]) DeleteMany

func (k Keys[K]) DeleteMany(ctx context.Context, inputs ...K) (uint64, error)

DeleteMany encodes all input keys before one atomic DEL and returns a count of distinct existing keys. It never enumerates namespaces or retries an uncertain DEL.

func (Keys[K]) Exists

func (k Keys[K]) Exists(ctx context.Context, input K) (bool, error)

Exists checks a resource of any native Redis data type through its typed key.

func (Keys[K]) Expire

func (k Keys[K]) Expire(ctx context.Context, input K, ttl cache.TTL) (bool, error)

Expire changes an existing resource's TTL without replacing its contents. Forever removes expiry.

func (Keys[K]) For

func (k Keys[K]) For(ctx context.Context, input K) (Key, error)

For encodes a resource key under the store's timeout and admission bounds without backend I/O.

func (Keys[K]) Name

func (k Keys[K]) Name() Name

Name returns the declared family name, or an empty name for an uninitialized handle.

func (Keys[K]) Version

func (k Keys[K]) Version() Version

Version returns the declared schema version, or zero for an uninitialized handle.

type Limits

type Limits struct {
	Arguments, Commands, RequestBytes int
	Reply                             ReplyLimits
}

Limits bounds request arguments, batch commands, request bytes, and captured replies.

func DefaultLimits

func DefaultLimits() Limits

DefaultLimits returns bounded request and reply defaults with at most 64 batch commands.

func (Limits) Validate

func (l Limits) Validate() error

Validate checks request bounds and the nested reply limits.

type Mode

type Mode uint8

Mode selects ordinary pipelining or a Redis transaction. Zero is invalid.

const (
	Pipelined Mode = iota + 1
	Transaction
)

func (Mode) Validate

func (m Mode) Validate() error

Validate returns fault.Invalid unless the mode is Pipelined or Transaction.

type Name

type Name string

Name identifies a declared raw Redis key family.

type Pipeline

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

Pipeline is built once and executed once. Queue operations and Run are serialized; whichever obtains the lock first determines inclusion. Execution consumes the pipeline even on failure. Construct another pipeline for a deliberate new attempt.

func NewPipeline

func NewPipeline(mode Mode) (*Pipeline, error)

NewPipeline validates mode and creates an empty pipeline for one execution.

func (*Pipeline) IsEmpty

func (p *Pipeline) IsEmpty() bool

IsEmpty reports whether no commands remain queued, including for a nil pipeline.

func (*Pipeline) Len

func (p *Pipeline) Len() int

Len reports queued commands. Run claims and clears that queue before I/O.

func (*Pipeline) Mode

func (p *Pipeline) Mode() Mode

Mode returns the selected execution mode, or zero for a nil pipeline.

func (*Pipeline) Run

func (p *Pipeline) Run(ctx context.Context, s *Store) error

Run consumes the pipeline once and publishes results only after every command and decoder succeeds. A failed execution can still have applied remote mutations.

type Reply

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

Reply is an immutable, owned RESP2 value for explicit decoders and adapters. Its zero value is Redis nil. Array returns a new slice of immutable child values.

func CaptureReplies

func CaptureReplies(ctx context.Context, input []any, l ReplyLimits) ([]Reply, error)

CaptureReplies shares one budget across a complete pipeline, including its synthetic array root. Errors return no partial batch and do not expose payloads.

func CaptureReply

func CaptureReply(ctx context.Context, input any, l ReplyLimits) (Reply, error)

CaptureReply is the adapter boundary for driver-decoded RESP2 (nil, int64, string, or []any). No driver types escape. Bounds apply after RESP parsing.

func (Reply) Array

func (r Reply) Array() ([]Reply, error)

Array returns an owned slice of immutable children or fault.Invalid for another reply kind.

func (Reply) Integer

func (r Reply) Integer() (int64, error)

Integer returns the integer value or fault.Invalid for another reply kind.

func (Reply) IsNull

func (r Reply) IsNull() bool

IsNull reports whether the reply is Redis nil, including the zero reply.

func (Reply) Text

func (r Reply) Text() (string, error)

Text returns the text value or fault.Invalid for another reply kind.

type ReplyLimits

type ReplyLimits struct{ Bytes, Nodes, Depth int }

ReplyLimits apply to the complete reply, or the combined batch. They bound retained/captured replies after the driver parses RESP, not malicious server parser allocations. Depth counts the root as zero.

func DefaultReplyLimits

func DefaultReplyLimits() ReplyLimits

DefaultReplyLimits returns reply bounds of 1 MiB, 8,192 nodes, and depth 32.

func (ReplyLimits) Validate

func (l ReplyLimits) Validate() error

Validate checks byte, node, and depth bounds against the package maxima.

type Request

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

Request is an immutable adapter invocation. Only a command containing a typed key, or a script constructor requiring a key, can produce one.

func (Request) Arguments

func (r Request) Arguments() []string

Arguments returns owned command arguments (including its name) or script ARGV.

func (Request) Bytes

func (r Request) Bytes() int

Bytes returns the combined byte length of the command name, script source, and arguments.

func (Request) IsScript

func (r Request) IsScript() bool

IsScript reports whether the request carries Lua source.

func (Request) Keys

func (r Request) Keys() []Key

Keys returns an owned slice of explicitly declared keys in argument order.

func (Request) Source

func (r Request) Source() string

Source returns the Lua source, or an empty string for an ordinary command.

func (Request) Validate

func (r Request) Validate(ns keyspace.Namespace, l Limits) error

Validate checks request limits and requires explicit keys within the supplied namespace.

type Result

type Result[R any] struct {
	// contains filtered or unexported fields
}

Result is a typed pipeline receipt. Value is available only after the entire execution and all decoders succeed. Repeated Value calls share the application's decoded value; callers own synchronization of any maps/slices it contains.

func Queue

func Queue[R any](p *Pipeline, c Command[R]) (Result[R], error)

Queue appends an immutable command and returns its typed result receipt.

func (Result[R]) Value

func (r Result[R]) Value() (R, error)

Value returns the decoded result after the entire pipeline succeeds, or fault.Conflict while it is pending.

type Store

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

Store borrows an adapter and bounds raw operations by namespace, timeout, and admission capacity.

func NewStore

func NewStore(backend Backend, c Config) (*Store, error)

NewStore validates configuration and borrows the backend without performing I/O.

func (*Store) Namespace

func (s *Store) Namespace() keyspace.Namespace

Namespace returns the configured namespace, or a zero namespace for a nil store.

type Version

type Version uint32

Version identifies a raw key family schema. Zero is invalid.

Jump to

Keyboard shortcuts

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