rdb

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package rdb serializes and deserializes values in Redis RDB wire form. It backs DUMP and RESTORE, whose payloads are RDB value bytes plus a short version and checksum footer, and it is the same codec a full snapshot file would reuse.

Index

Constants

View Source
const (
	Version = 11
)

Version is the RDB payload version aki writes into the DUMP footer. Redis 7.x uses 11; we accept up to maxVersion on load so a payload from a slightly newer peer still restores.

Variables

View Source
var ErrUnsupported = errors.New("rdb: unsupported value type")

ErrUnsupported means the payload used a type aki does not deserialize yet, such as a module value. RESTORE maps it to the generic bad-payload reply.

Functions

func Marshal

func Marshal(v Value) ([]byte, error)

Marshal serializes v into a DUMP payload: the type byte and value bytes, then a 2-byte little-endian version, then an 8-byte little-endian CRC-64 over all the preceding bytes.

func MarshalFile

func MarshalFile(snap Snapshot) ([]byte, error)

MarshalFile serializes a snapshot to a complete RDB file: the REDIS header, the aux records, then each non-empty database as a selector, a resize hint, and its keys, then the EOF marker and the CRC-64 over everything before the checksum.

func SortMembers

func SortMembers(members []Member)

SortMembers orders sorted-set members by score then member bytes, the order aki's stored body expects. RESTORE calls it before re-encoding a decoded set.

Types

type DBData

type DBData struct {
	Index   int
	Entries []Entry
}

DBData is the contents of one logical database in a snapshot.

type Entry

type Entry struct {
	Key      []byte
	Value    Value
	ExpireMS int64 // -1 for no expiry
	Idle     uint32
	HasIdle  bool
	Freq     uint8
	HasFreq  bool
}

Entry is one key in a snapshot: its name, value, optional expiry, and the access metadata that seeds the LRU and LFU state on load.

type Field

type Field struct {
	Field []byte
	Value []byte
}

Field is one hash entry.

type Kind

type Kind int

Kind tags the logical type a Value carries.

const (
	KindString Kind = iota
	KindList
	KindSet
	KindHash
	KindZSet
	KindStream
)

type Member

type Member struct {
	Member []byte
	Score  float64
}

Member is one sorted-set entry.

type Snapshot

type Snapshot struct {
	DBs       []DBData
	Aux       map[string]string // auxiliary header fields such as redis-ver
	Functions []string          // function library sources, one per library
}

Snapshot is a whole dataset across its databases, the unit SAVE writes and load reads.

func UnmarshalFile

func UnmarshalFile(data []byte) (Snapshot, error)

UnmarshalFile parses a complete RDB file into a snapshot. It checks the header magic and version, verifies the trailing CRC-64 unless it is zero, and reads the opcode stream until EOF.

type StreamConsumer

type StreamConsumer struct {
	Name       []byte
	SeenTime   uint64
	ActiveTime uint64
	PendingIDs []StreamID
}

StreamConsumer is one named reader in a group, with its seen and active times and the IDs it currently holds pending.

type StreamData

type StreamData struct {
	Entries      []StreamEntry
	LastMS       uint64
	LastSeq      uint64
	FirstMS      uint64
	FirstSeq     uint64
	MaxDelMS     uint64
	MaxDelSeq    uint64
	EntriesAdded uint64
	Groups       []StreamGroup
}

StreamData is the neutral carrier for a stream value between aki's keyspace and the RDB byte form, the stream counterpart of the List, Set, Hash and ZSet fields on Value.

type StreamEntry

type StreamEntry struct {
	MS     uint64
	Seq    uint64
	Fields [][]byte
}

StreamEntry is one log entry: its ID and a flat field/value list where even indices are field names and odd indices are the matching values.

type StreamGroup

type StreamGroup struct {
	Name        []byte
	LastMS      uint64
	LastSeq     uint64
	EntriesRead uint64
	PEL         []StreamPEL
	Consumers   []StreamConsumer
}

StreamGroup is a consumer group: its name, last delivered ID, entries-read counter, the group PEL, and the consumers.

type StreamID

type StreamID struct {
	MS  uint64
	Seq uint64
}

StreamID is a 128-bit stream entry ID inside an RDB stream value: a millisecond timestamp and a sequence number.

type StreamPEL

type StreamPEL struct {
	MS            uint64
	Seq           uint64
	DeliveryTime  uint64
	DeliveryCount uint64
}

StreamPEL is one Pending Entries List record of a consumer group: an entry that was delivered to a consumer but not yet acknowledged.

type Value

type Value struct {
	Kind   Kind
	Str    []byte
	List   [][]byte
	Set    [][]byte
	Hash   []Field
	ZSet   []Member
	Stream *StreamData
}

Value is a decoded logical value, the bridge between aki's keyspace and the RDB byte form. The command layer fills the field matching Kind and hands it to Marshal, or reads it back from Unmarshal.

func Unmarshal

func Unmarshal(payload []byte) (Value, error)

Unmarshal validates a DUMP payload and decodes its value. It checks the CRC-64 (unless the footer is all zeros, the rdbchecksum-off convention) and rejects a version above what aki supports, both reported by RESTORE as the same wrong payload error.

Jump to

Keyboard shortcuts

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