|
| 1 | +# rust-librdb |
| 2 | + |
| 3 | +Rust wrapper for [librdb](https://github.com/redis/librdb), the Redis RDB file parser. |
| 4 | + |
| 5 | +Parses RDB dump files and delivers Redis data types (strings, lists, hashes, |
| 6 | +sets, sorted sets, streams) through a callback trait, without loading the |
| 7 | +entire file into memory. |
| 8 | + |
| 9 | +| Crate | Description | |
| 10 | +|-------|-------------| |
| 11 | +| [`librdb`](librdb/) | Safe, high-level wrapper | |
| 12 | +| [`librdb-sys`](librdb-sys/) | Raw FFI bindings | |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```toml |
| 17 | +[dependencies] |
| 18 | +librdb = "0.1" |
| 19 | +``` |
| 20 | + |
| 21 | +By default, librdb is compiled from the vendored source (git submodule). |
| 22 | +A C compiler is required. |
| 23 | + |
| 24 | +```sh |
| 25 | +git clone --recurse-submodules https://github.com/funcpp/rust-librdb |
| 26 | +cd rust-librdb |
| 27 | +cargo build |
| 28 | +``` |
| 29 | + |
| 30 | +### Linking options |
| 31 | + |
| 32 | +| Method | How | |
| 33 | +|--------|-----| |
| 34 | +| Source build (default) | No extra steps | |
| 35 | +| System shared library | `--features librdb-sys/dynamic-linking` | |
| 36 | +| Pre-built static library | Set `DEP_LIBRDB_STATIC_ROOT`, use `--features librdb-sys/static-linking` | |
| 37 | + |
| 38 | +## Example |
| 39 | + |
| 40 | +```rust |
| 41 | +use librdb::{Parser, RdbHandlers, KeyInfo, Result}; |
| 42 | + |
| 43 | +struct MyHandler { |
| 44 | + keys: Vec<String>, |
| 45 | +} |
| 46 | + |
| 47 | +impl RdbHandlers for MyHandler { |
| 48 | + fn handle_new_key(&mut self, key: &[u8], _info: &KeyInfo) -> Result<()> { |
| 49 | + self.keys.push(String::from_utf8_lossy(key).into_owned()); |
| 50 | + Ok(()) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +fn main() -> librdb::Result<()> { |
| 55 | + let mut parser = Parser::new(MyHandler { keys: vec![] })?; |
| 56 | + parser.parse_file("dump.rdb")?; |
| 57 | + let handler = parser.into_handler(); |
| 58 | + println!("{} keys parsed", handler.keys.len()); |
| 59 | + Ok(()) |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +`&[u8]` callback parameters borrow directly from librdb's internal buffer and |
| 64 | +are only valid for the duration of the callback. Clone if you need to retain |
| 65 | +the data. |
| 66 | + |
| 67 | +## License |
| 68 | + |
| 69 | +MIT |
0 commit comments