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.
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)
}
}
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)
}
}
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
}
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)
}
}
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)
}
}