rdb

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2023 License: MIT Imports: 14 Imported by: 0

README

RDB Parser

This library is capable of parsing RDB files upto version 11.

It can also be used to parse and dump single RDB values, and verify RDB files and values.

API

Parsing a file

The following code demonstrates how to parse an RDB file.

Note that, only the database with the number 0 is parsed, and the rest is skipped or an error is raised, depending on the handler implementation.

The same holds true for some types of metadata or function definition in the RDB file.

import (
	"log"

	"github.com/upstash/rdb"
)

type fileHandler struct {
}

// Implement rdb.FileHandler methods, which will be called
// as the file is parsed.

func main() {
	err := rdb.ReadFile("/path/to/dump.rdb", &fileHandler{})
	if err != nil {
		log.Fatal(err)
	}
}
Parsing a value

The following code demonstrates how to parse a single RDB value.

import (
	"log"

	"github.com/upstash/rdb"
)

type valueHandler struct {
}

// Implement rdb.ValueHandler methods, in which one of them will
// called depending on the type of the value in the given payload.

func main() {
	payload := []byte{ /*RDB value payload*/ }
	err := rdb.ReadValue("key", payload, &valueHandler{})
	if err != nil {
		log.Fatal(err)
	}
}
Dumping a value

The following code demonsrates how to dump a single RDB value as a payload that can be parsed with rdb.ReadValue.

import (
	"github.com/upstash/rdb"
)

func main() {
	writer := rdb.NewWriter()
	writer.WriteType(rdb.TypeString)
	writer.WriteString("foo")
	writer.WriteChecksum(rdb.Version)
	writer.GetBuffer() // payload
}
Verifying a file

The following code demonstrates how to verify an RDB file is not corrupt, and does not exceed the defined limits of the total data, max entry, and max key sizes.

import (
	"log"

	"github.com/upstash/rdb"
)

func main() {
	opts := rdb.VerifyFileOptions{
		MaxDataSize:  256 << 20, // 256 MB
		MaxEntrySize: 100 << 20, // 100 MB
		MaxStreamPELSize: 1000,
	}
	err := rdb.VerifyFile("/path/to/dump.rdb", opts)
	if err != nil {
		log.Fatal(err)
	}
}
Verifying a value

The following code demonstrates how to verify an RDB value is not corrupt, and does not exceed the defined limits of the max entry size.

import (
	"log"

	"github.com/upstash/rdb"
)

func main() {
	opts := rdb.VerifyValueOptions{
		MaxEntrySize: 100 << 20, // 100 MB
		MaxStreamPELSize: 1000,
	}
	payload := []byte{ /*RDB value payload*/ }
	err := rdb.VerifyValue(payload, opts)
	if err != nil {
		log.Fatal(err)
	}
}

Documentation

Index

Constants

View Source
const ValueChecksumSize = 10

Size of the checksum block. 2 (RDB version) + 8 (CRC-64) bytes in total.

View Source
const Version uint16 = 11

Variables

This section is empty.

Functions

func ReadFile

func ReadFile(path string, handler FileHandler) error

ReadFile reads the RDB file in the given path, and calls the appropriate methods of the handler for the objects read. It also allows partial read of the file which means the function will skip some parts of the file, which is not compatible with Upstash yet. These parts include function data, multiple databases(any database other than 0), and unsupported modules.

func ReadValue

func ReadValue(key string, payload []byte, handler ValueHandler) error

ReadValue reads the single RDB value given in the payload into the handler. The given key is passed into the handler methods directly.

func VerifyFile

func VerifyFile(path string, opts VerifyFileOptions) error

VerifyFile verifies that the given RDB file is not corrupt, or does not exceed the limits in the given options.

func VerifyValue

func VerifyValue(payload []byte, opts VerifyValueOptions) error

VerifyValue verifies that the given RDB value is not corrupt, or does not exceed the limits in the given options.

func VerifyValueChecksum

func VerifyValueChecksum(payload []byte) error

VerifyValueChecksum uses the checksum block at the end of the payload(last 10 bytes) to verify that the CRC64 of the payload matches with the CRC value encoded at the last 8 bytes of the payload. It also verifies that the RDB version encoded in the checksum block is less than or equal to the RDB version supported by Upstash.

Types

type FileHandler

type FileHandler interface {
	ValueHandler
	HandleExpireTime(key string, expireTime time.Duration)
}

FileHandler is an extension of ValueHandler, which can handle RDB objects and their expiration information from RDB files.

type ModuleMarker

type ModuleMarker string
const (
	EmptyModuleMarker ModuleMarker = ""
	JSONModuleMarker  ModuleMarker = "json"
)

type Stream

type Stream struct {
	LastID  StreamID
	Entries []StreamEntry
	Length  uint64
	Groups  []StreamConsumerGroup
}

type StreamConsumer

type StreamConsumer struct {
	Name           string
	SeenTime       int64
	ActiveTime     int64
	PendingEntries []*StreamPendingEntry
}

type StreamConsumerGroup

type StreamConsumerGroup struct {
	Name        string
	LastID      StreamID
	EntriesRead int64
	Consumers   []StreamConsumer
}

type StreamEntry

type StreamEntry struct {
	ID    StreamID
	Value []string
}

type StreamID

type StreamID struct {
	Millis uint64
	Seq    uint64
}

type StreamPendingEntry

type StreamPendingEntry struct {
	Entry         StreamEntry
	DeliveryTime  int64
	DeliveryCount uint64
}

type StreamWriter

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

func (*StreamWriter) WriteConsumerGroups

func (sw *StreamWriter) WriteConsumerGroups(groups []StreamConsumerGroup) error

func (*StreamWriter) WriteEntries

func (sw *StreamWriter) WriteEntries(entries []StreamEntry) error

Instead of delta encoding entries, we write each entry as a seperate listpack, to make the implementation simpler.

func (*StreamWriter) WriteMetadata

func (sw *StreamWriter) WriteMetadata(length uint64, lastID StreamID) error

type Type

type Type uint8
const (
	TypeString           Type = 0
	TypeList             Type = 1
	TypeSet              Type = 2
	TypeZset             Type = 3
	TypeHash             Type = 4
	TypeZset2            Type = 5
	TypeModule2          Type = 7 // module pre ga with type 6 is not supported
	TypeHashZipmap       Type = 9 // type 8 seems unused by Redis
	TypeListZiplist      Type = 10
	TypeSetIntset        Type = 11
	TypeZsetZiplist      Type = 12
	TypeHashZiplist      Type = 13
	TypeListQuicklist    Type = 14
	TypeStreamListpacks  Type = 15
	TypeHashListpack     Type = 16
	TypeZsetListpack     Type = 17
	TypeListQuicklist2   Type = 18
	TypeStreamListpacks2 Type = 19
	TypeSetListpack      Type = 20
	TypeStreamListpacks3 Type = 21
)

type ValueHandler

type ValueHandler interface {
	// whether the handler can skip known but not yet supported types or not.
	AllowPartialRead() bool

	// called when a string value is read for the key.
	HandleString(key, value string) error

	// returned function is called for the each enty read for the key.
	ListEntryHandler(key string) func(elem string) error

	// called when the list is read completely, with the name and the number of entries read.
	HandleListEnding(key string, entriesRead uint64)

	// returned function is called for the each enty read for the key.
	SetEntryHandler(key string) func(elem string) error

	// returned function is called for the each enty read for the key.
	ZsetEntryHandler(key string) func(elem string, score float64) error

	// called when the zset is read completely, with the name and the number of entries read.
	HandleZsetEnding(key string, entriesRead uint64)

	// returned function is called for the each enty read for the key.
	HashEntryHandler(key string) func(field, value string) error

	// called when a module value is read for the key.
	HandleModule(key, value string, marker ModuleMarker) error

	// returned function is called for the each stream enty read for the key.
	StreamEntryHandler(key string) func(entry StreamEntry) error

	// returned function is called for the each stream group read for the key.
	StreamGroupHandler(key string) func(group StreamConsumerGroup) error

	// called when the stream entries and groups are read completely,
	// with the name and the number of entries read.
	HandleStreamEnding(key string, entriesRead uint64)
}

ValueHandler is used to handle RDB objects while reading a file.

type VerifyFileOptions

type VerifyFileOptions struct {
	MaxDataSize      int
	MaxEntrySize     int
	MaxKeySize       int
	MaxStreamPELSize int
}

type VerifyValueOptions

type VerifyValueOptions struct {
	MaxEntrySize     int
	MaxStreamPELSize int
}

type Writer

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

func NewWriter

func NewWriter() *Writer

func (*Writer) GetBuffer

func (w *Writer) GetBuffer() []byte

GetBuffer returns the payload written to the writer.

func (*Writer) WriteChecksum

func (w *Writer) WriteChecksum(version uint16) error

WriteChecksum writes the given RDB version and the CRC64 for the payload.

func (*Writer) WriteHash

func (w *Writer) WriteHash(hash map[string]string) error

WriteHash writes the given hash as the ObjectTypeHash.

func (*Writer) WriteJSON

func (w *Writer) WriteJSON(json string) error

WriteJSON writes the JSON string as the ObjectTypeModule2, with the JSON module type with version 3.

func (*Writer) WriteList

func (w *Writer) WriteList(list []string) error

WriteList writes the given list as the ObjectTypeList.

func (*Writer) WriteSet

func (w *Writer) WriteSet(set []string) error

WriteSet writes the given set as the ObjectTypeSet.

func (*Writer) WriteStream

func (w *Writer) WriteStream(stream *Stream) error

WriteStream writes the stream as the ObjectTypeStreamListpacks.

func (*Writer) WriteString

func (w *Writer) WriteString(str string) error

WriteString writes the given string as the ObjectTypeString.

func (*Writer) WriteType

func (w *Writer) WriteType(objType Type) error

WriteType writes the given object type.

func (*Writer) WriteZset

func (w *Writer) WriteZset(elements []string, scores []float64) error

WriteZset writes the given sorted set as the ObjectTypeZset2.

Jump to

Keyboard shortcuts

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