Vdr.RedisStream.Parser (veidrodelis v0.1.8)

Copy Markdown View Source

Redis replication stream parser implemented in Rust.

This module provides a streaming parser for Redis replication protocol. (except for few greeting commands) The parser handles RDB snapshot transfer and command subsequent online command streaming.

The parser main states are:

  • WaitingRdb: Initial state, waiting for RDB data
  • ReadingRdb: Parsing RDB snapshot
  • Streaming: Processing command stream after RDB

This parser is designed to be used internally by Vdr.RedisStream.Replica

Summary

Types

Flags returned from the parser along with commands.

Functions

Create a new streaming replica parser.

Feed a chunk of binary data to the replica parser.

Types

flags()

@type flags() :: %{ping: boolean(), replconf_getack: boolean()}

Flags returned from the parser along with commands.

  • :ping - true if a PING command was encountered during parsing
  • :replconf_getack - true if a REPLCONF GETACK command was encountered during parsing

Functions

create(opts \\ [])

@spec create(keyword()) :: reference()

Create a new streaming replica parser.

Options

  • :rdb - Boolean, default true. If false, the parser starts in streaming mode without expecting RDB data. Use rdb: false for partial resync scenarios where no RDB snapshot will be transferred.

Returns

A parser resource that can be used with data/2.

data(parser, chunk)

@spec data(reference(), binary()) ::
  {:ok, list(), reference(), flags()} | {:error, term()}

Feed a chunk of binary data to the replica parser.

The parser will process the data according to its current state:

  • In WaitingRdb state: waits for RDB bulk string header
  • In ReadingRdb state: parses RDB snapshot and returns commands
  • In Streaming state: parses RESP commands from the stream

Parameters

  • parser - Parser resource from create/0 or previous data/2 call
  • chunk - Binary chunk of replication data

Returns

  • {:ok, commands, new_parser, flags} - Successfully processed chunk, returns parsed commands (may be empty)
  • {:error, reason} - Parsing failed

Commands are tuples: {db, command_tuple, raw_command, affected_keys} where:

  • db is the database number
  • command_tuple is a parsed command tuple (e.g., {:set, key, value})
  • raw_command is the raw command tuple {:generic, args} for logging/debugging
  • affected_keys is a list of keys affected by this command

The flags map contains:

  • :ping - true if a PING command was encountered during this chunk processing
  • :replconf_getack - true if a REPLCONF GETACK command was encountered during this chunk processing

Note: PING and REPLCONF commands are not returned as commands - they are signaled via flags only.