backup.go
backup.go - Overview
This file provides functionality for backing up and restoring a Badger database. It includes functions for creating full and incremental backups, as well as loading data from a backup file.
Detailed Documentation
Backup
(DB method)
Purpose: Dumps a protobuf-encoded list of all entries in the database into the given writer, that are newer than or equal to the specified version. It's a wrapper over Stream.Backup
.
Parameters:
w
(io.Writer): The writer to which the backup data will be written.since
(uint64): The version of the last entry that was dumped. Only entries newer than or equal to this version will be included in the backup.
Returns:
uint64
: The timestamp (version) of the last entry that was dumped.error
: An error, if any.
Backup
(Stream method)
Purpose: Dumps a protobuf-encoded list of all entries in the database into the given writer, that are newer than or equal to the specified version.
Parameters:
w
(io.Writer): The writer to which the backup data will be written.since
(uint64): The version of the last entry that was dumped. Only entries newer than or equal to this version will be included in the backup.
Returns:
uint64
: The timestamp (version) of the last entry that was dumped.error
: An error, if any.
writeTo
Purpose: Writes a pb.KVList
to an io.Writer
.
Parameters:
list
(*pb.KVList): The list of key-value pairs to write.w
(io.Writer): The writer to write to.
Returns:
error
: An error, if any.
KVLoader
Purpose: Used to write KVList
objects into Badger. It can be used to restore a backup.
Fields:
db
(*DB): A pointer to the Badger database.throttle
(*y.Throttle): A throttle to limit the number of pending writes.entries
([]*Entry): A slice of entries to be written.entriesSize
(int64): The total size of the entries in theentries
slice.totalSize
(int64): The total size of all entries processed by the loader.
NewKVLoader
Purpose: Returns a new instance of KVLoader
.
Parameters:
maxPendingWrites
(int): The maximum number of pending writes allowed.
Returns:
*KVLoader
: A newKVLoader
instance.
Set
Purpose: Writes the key-value pair to the database.
Parameters:
kv
(*pb.KV): The key-value pair to write.
Returns:
error
: An error, if any.
send
Purpose: Sends the current batch of entries to the database.
Returns:
error
: An error, if any.
Finish
Purpose: Meant to be called after all the key-value pairs have been loaded. It flushes any remaining entries and waits for all pending writes to complete.
Returns:
error
: An error, if any.
Load
Purpose: Reads a protobuf-encoded list of all entries from a reader and writes them to the database.
Parameters:
r
(io.Reader): The reader to read from.maxPendingWrites
(int): The maximum number of pending writes allowed.
Returns:
error
: An error, if any.
Code Examples
Not applicable.