rdb

package module
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2023 License: MIT Imports: 11 Imported by: 1

README

rdb-go

Parse Redis RDB dump files. This library is based on redis-rdb-tools.

GitHub tag (latest SemVer) go.dev reference Test codecov

Install

This library can be used as a package.

go get github.com/tommy351/rdb-go

Or as a command line tool.

go get github.com/tommy351/rdb-go/cmd/rdb
rdb path/to/dump.rdb

Usage

Use Parser to iterate over a RDB dump file.

import (
  rdb "github.com/tommy351/rdb-go"
)

parser := NewParser(file)

for {
  data, err := parser.Next()

  if err == io.EOF {
    break
  }

  if err != nil {
    panic(err)
  }

  // ...
}

See examples in the documentation or cmd/rdb/main.go for more details.

License

MIT

Documentation

Overview

Package rdb parses RDB dump files.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrInvalidMagicString = errors.New("invalid magic string")

ErrInvalidMagicString is returned when a RDB dump file is not started with the magic string "REDIS".

Functions

This section is empty.

Types

type Aux

type Aux struct {
	Key   string
	Value string
}

type BloomFilter added in v0.6.0

type BloomFilter struct {
	DataKey
}

BloomFilter represents a bloom filter data structure implemented by RedisBloom. At present, It only contains data key, but does not store the actual data values.

type CuckooFilter added in v0.6.0

type CuckooFilter struct {
	DataKey
}

CuckooFilter represents a cuckoo filter data structure implemented by RedisBloom. At present, It only contains data key, but does not store the actual data values.

type DataKey

type DataKey struct {
	Database int
	Key      string
	Expiry   *time.Time
}

DataKey contains the database, the key and the expiry of data.

func (DataKey) Expired added in v0.2.0

func (d DataKey) Expired() bool

Expired returns true if the key is expired.

type DatabaseSize added in v0.1.1

type DatabaseSize struct {
	Size   int
	Expire int
}

type HashData

type HashData struct {
	DataKey
	Value map[string]string
}

HashData is returned when all entries in a hash are all read.

type HashEntry

type HashEntry struct {
	DataKey
	HashValue
	Length int
}

HashEntry is returned when a new hash entry is read.

type HashHead

type HashHead struct {
	DataKey
	Length int
}

HashHead contains the key and the length of a hash. It is returned when a hash is read first time.

type HashValue

type HashValue struct {
	Index string
	Value string
}

HashValue contains a key-value pair of a hash entry.

type IntSetEncodingError

type IntSetEncodingError struct {
	Encoding uint32
}

func (IntSetEncodingError) Error

func (i IntSetEncodingError) Error() string

type LengthEncodingError

type LengthEncodingError struct {
	Encoding byte
}

func (LengthEncodingError) Error

func (l LengthEncodingError) Error() string

type ListData

type ListData struct {
	DataKey
	Value []string
}

ListData is returned when all entries in a list are all read.

type ListEntry

type ListEntry struct {
	DataKey
	Index  int
	Length int
	Value  string
}

ListEntry is returned when a new list entry is read.

type ListHead

type ListHead struct {
	DataKey
	Length int
}

ListHead contains the key and the length of a list. It is returned when a list is read first time. The length may be incorrect when the list is backed by a quicklist data structure.

type ModuleOpcodeError added in v0.6.0

type ModuleOpcodeError struct {
	Expected int
	Actual   int
}

func (ModuleOpcodeError) Error added in v0.6.0

func (r ModuleOpcodeError) Error() string

type Parser

type Parser struct {
	KeyFilter func(key *DataKey) bool
	// contains filtered or unexported fields
}

Parser parses a RDB dump file.

Example (Basic)
file, err := os.Open("dump.rdb")
if err != nil {
	panic(err)
}

defer file.Close()

parser := NewParser(file)

for {
	data, err := parser.Next()
	if errors.Is(err, io.EOF) {
		break
	}

	if err != nil {
		panic(err)
	}

	switch data := data.(type) {
	case *StringData:
		fmt.Println(data.Key, data.Value)

	case *ListData:
		for i, value := range data.Value {
			fmt.Println(data.Key, i, value)
		}
	}
}
Example (KeyFilter)
file, err := os.Open("dump.rdb")
if err != nil {
	panic(err)
}

defer file.Close()

parser := NewParser(file)

parser.KeyFilter = func(key *DataKey) bool {
	return key.Database == 0 && strings.HasPrefix(key.Key, "foo:") && !key.Expired()
}

func NewParser

func NewParser(r io.Reader) *Parser

NewParser returns a new Parser to read from r.

func (*Parser) Next

func (p *Parser) Next() (interface{}, error)

Next reads data from the reader until the next token and returns one of the following types:

*Aux
*DatabaseSize
*StringData
*ListHead, *ListEntry, *ListData
*SetHead, *SetEntry, *SetData
*SortedSetHead, *SortedSetEntry, *SortedSetData
*MapHead, *MapEntry, *MapData

Next returns a io.EOF error when a EOF token is read.

type SetData

type SetData struct {
	DataKey
	Value []string
}

SetData is returned when all entries in a set are all read.

type SetEntry

type SetEntry struct {
	DataKey
	Index  int
	Length int
	Value  string
}

SetEntry is returned when a new set entry is read.

type SetHead

type SetHead struct {
	DataKey
	Length int
}

SetHead contains the key and the length of a set. It is returned when a set is read first time.

type SortedSetData

type SortedSetData struct {
	DataKey
	Value []SortedSetValue
}

SortedSetData is returned when all entries in a sorted set are all read.

type SortedSetEntry

type SortedSetEntry struct {
	DataKey
	SortedSetValue
	Index  int
	Length int
}

SortedSetEntry is returned when a new sorted set entry is read.

type SortedSetHead

type SortedSetHead struct {
	DataKey
	Length int
}

SortedSetHead contains the key and the length of a sorted set. It is returned when a sorted set is read first time.

type SortedSetValue

type SortedSetValue struct {
	Value string
	Score float64
}

SortedSetValue contains the value and its score of a sorted set entry.

type StringData

type StringData struct {
	DataKey
	Value string
}

StringData contains the key and the value of string data.

type StringEncodingError

type StringEncodingError struct {
	Encoding int
}

func (StringEncodingError) Error

func (s StringEncodingError) Error() string

type UnexpectedZipMapEndError

type UnexpectedZipMapEndError struct {
	Key string
}

func (UnexpectedZipMapEndError) Error

func (u UnexpectedZipMapEndError) Error() string

type UnsupportedDataTypeError

type UnsupportedDataTypeError struct {
	DataType byte
}

func (UnsupportedDataTypeError) Error

func (u UnsupportedDataTypeError) Error() string

type UnsupportedVersionError

type UnsupportedVersionError struct {
	Version int
}

func (UnsupportedVersionError) Error

func (u UnsupportedVersionError) Error() string

type ZipListEndError added in v0.1.1

type ZipListEndError struct {
	Value byte
}

func (ZipListEndError) Error added in v0.1.1

func (z ZipListEndError) Error() string

type ZipListHeaderError

type ZipListHeaderError struct {
	Header byte
}

func (ZipListHeaderError) Error

func (z ZipListHeaderError) Error() string

type ZipListLengthError added in v0.1.1

type ZipListLengthError struct {
	Length      int
	ValueLength int
}

func (ZipListLengthError) Error added in v0.1.1

func (z ZipListLengthError) Error() string

Directories

Path Synopsis
cmd
rdb
hack
internal

Jump to

Keyboard shortcuts

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