/redis-rdb

A set of utilities to handle Redis .rdb files with Ruby.

Primary LanguageRubyMIT LicenseMIT

redis-rdb

This library provides a set of modules and classes that make it easy to handle binary database dumps generated by Redis (.rdb files) in Ruby.

Currently redis-rdb allows developers to read .rdb files in a streamable flashion with RDB::Reader by providing a set of callbacks. Database objects can optionally be filtered by database / key / type using custom filters.

require 'rdb'

class MyCallbacks
  include RDB::ReaderCallbacks

  KEY_SELECTOR = Regexp.compile(/user:\d+/)

  def accept_key?(state)
    state.database == 15 && KEY_SELECTOR.match(state.key)
  end

  def set(key, value, state)
    puts "SET \"#{key}\" \"#{value}\""
  end
end

RDB::Reader.read_file('dump.rdb', callbacks: MyCallbacks.new)

For more details about the supported callbacks you can take a look at the source code in lib/rdb/callbacks.rb.

Data dumpers

The RDB::Dumper module can be used to create classes that dump the data read from an .rdb file into a new file using a different format. An example would be to create an AOF file for Redis or to store the data into JSON or CSV. This is an example of using RDB::Dumpers::AOF:

require 'rdb'

source = 'test/rdb/database_multiple_logical_dbs.rdb'
destination = File.basename(source, '.rdb') + '.aof'

dumper = RDB::Dumpers::AOF.new(source, destination)
dumper.dump

A dumper really is no more than a slightly augmented version of a class defining the callbacks for RDB::Reader with a few additional helper methods. You can find more about how to implement dumpers in lib/rdb/dumper.rb and lib/rdb/dumpers/aof.rb.

RDB file format

Right now there is still no official documentation about the binary format of .rdb files beside the code in rdb.c as the reference implementation.

An unofficial but comprehensive description of the RDB format has been recently made available, but there is still no official documentation about it beside the actual implementation that can be found in rdb.c.

Additional notes and credits

Credit goes to sripathikrishnan for his work on the redis-rdb-tools Python library that proved to be quite an inspiration for the final design of RDB::Reader.We also reused the .rdb files shipped with his project for testing.

Dependencies

  • Ruby >= 1.9.0

Links

Project

Related

Author

License

The code for redis-rdb is distributed under the terms of the MIT license (see LICENSE).