Giter Site home page Giter Site logo

rdb's Introduction

rdb Build Status GoDoc

WARNING: This project is not production ready.

Package rdb is a Redis RDB File parser written in Go.

Feature

  • Concurrent parsing.
  • Built in memory report.
  • Simple filter support.
  • Layered skipping strategies.

Install

go get -u github.com/matthewjhe/rdb

Usage

Basic Example

The basic use case is very simple, just implements Filter interface and pass it to Parse:

package main

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

func (f filter) Key(k rdb.Key) bool { return false }
func (f filter) Type(t rdb.Type) bool   { return false }
func (f filter) Database(db rdb.DB) bool { return false }
func (f filter) Set(v *rdb.Set) { }
func (f filter) List(v *rdb.List) { }
func (f filter) Hash(v *rdb.Hash) { }
func (f filter) String(v *rdb.String) { }
func (f filter) SortedSet(v *rdb.SortedSet) { }

func main() {
    const file = "/tmp/dump.rdb"
    reader, err := rdb.NewBufferReader(file, 0)
    if err != nil {
        panic(err)
    }

    if err := rdb.Parse(reader, rdb.WithFilter(filter{})); err != nil {
        panic(err)
    }
}

Skipping

NOTE: RDB file is read sequentially, when we say skips a database, we also need to parse this database's every single key, we just simply skips actions like decompress, unzip ziplist, etc. It is impossible to skips a key without reading its metadata.

  • Global Skip Strategy

Global skip strategy applies to the whole parsing lifetime, it can be overwritten by Filter's Database method, it will be restored when parsing a database completely.

Global skip strategy is set by a ParseOption when rdb.Parse is called:

    // reads memory report only
    strategy := rdb.WithStrategy(rdb.SkipExpiry | rdb.SkipMeta | rdb.SkipValue)
    err := rdb.Parse(reader, strategy, rdb.WithFilter(filter{}))
    // ...
  • Database Skip Strategy

Database skip strategy applies to a database lifetime, it is set by Filter's Database method, and it can be overwritten by Filter's Key or Type method. Database skip strategy is set to global skip strategy by default.

func (f filter) Database(db rdb.DB) bool {
    // skips this database
    db.Skip(rdb.SkipAll)

    // skips database's metadata and key's expiry
    // db.Skip(rdb.SkipMeta | rdb.SkipExpiry)
    return false
}
  • Type and Key Skip Strategy

Type and key skip strategy overwrite database skip strategy. It only applies the key and value which will read next.

func (f filter) Type(t rdb.Type) bool {
    // skips this type
    t.Skip(rdb.SkipAll)
    return false
}

func (f filter) Key(key rdb.Key) bool {
    // skips this key
    // key.Skip(rdb.SkipAll)

    // skips value
    key.Skip(rdb.SkipValue)
    return false
}

Syncing

By default, rdb uses multiple goroutines to parse keys which would cause keys disorder. If the order of keys is important, use EnableSync ParseOption.

    rdb.Parse(reader, rdb.WithFilter(filter{}), rdb.EnableSync())

Aborting

Sometimes we want to abort the parsing process, this can be done by return true when one of the following methods is called: Note that the abort action is taking effect immediately when the function returns.

func (f filter) Key(k rdb.Key) bool {
    return true
}
func (f filter) Type(t rdb.Type) bool   {
    return true
}
func (f filter) Database(db rdb.DB) bool {
    return true
}

References

License

MIT

rdb's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.