forked from dgraph-io/badger
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
load backup with restricted range #4
Open
fridgei
wants to merge
1
commit into
master
Choose a base branch
from
ian_backup_load_for_range
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,10 @@ import ( | |
"bytes" | ||
"context" | ||
"encoding/binary" | ||
"fmt" | ||
"io" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/dgraph-io/badger/v2/pb" | ||
"github.com/dgraph-io/badger/v2/y" | ||
|
@@ -199,6 +202,25 @@ func (l *KVLoader) Finish() error { | |
return l.throttle.Finish() | ||
} | ||
|
||
// IDFromString returns the ID corresponding to the input string. | ||
// Requires that s was produced by a previous call to ID.String. | ||
func IDFromString(s string) (uint64, error) { | ||
s = strings.TrimPrefix(s, "0x") | ||
id, err := strconv.ParseUint(s, 16, 64) | ||
return id, err | ||
} | ||
|
||
const badgerKeySep = "/" | ||
|
||
func decodeBadgerKeyForDocID(key []byte) (uint64, error) { | ||
components := strings.SplitN(string(key), badgerKeySep, 4) | ||
if id, err := IDFromString(components[0]); err != nil { | ||
return 0, fmt.Errorf("parsing docid failed: %v %w", components[0], err) | ||
} else { | ||
return id, nil | ||
} | ||
} | ||
|
||
// Load reads a protobuf-encoded list of all entries from a reader and writes | ||
// them to the database. This can be used to restore the database from a backup | ||
// made by calling DB.Backup(). If more complex logic is needed to restore a badger | ||
|
@@ -252,3 +274,62 @@ func (db *DB) Load(r io.Reader, maxPendingWrites int) error { | |
db.orc.txnMark.Done(db.orc.nextTxnTs - 1) | ||
return nil | ||
} | ||
|
||
// LoadForRange reads a protobuf-encoded list of all entries from a reader and writes | ||
// them to the database if the docID is within the appropriate range . This can be used | ||
// to restore the database from a backup // made by calling DB.Backup(). If more complex logic is needed to restore a badger | ||
// backup, the KVLoader interface should be used instead. | ||
// | ||
// DB.LoadForRange() should be called on a database that is not running any other | ||
// concurrent transactions while it is running. | ||
func (db *DB) LoadForRange(r io.Reader, maxPendingWrites int, minID uint64, maxID uint64) error { | ||
br := bufio.NewReaderSize(r, 16<<10) | ||
unmarshalBuf := make([]byte, 1<<10) | ||
|
||
ldr := db.NewKVLoader(maxPendingWrites) | ||
for { | ||
var sz uint64 | ||
err := binary.Read(br, binary.LittleEndian, &sz) | ||
if err == io.EOF { | ||
break | ||
} else if err != nil { | ||
return err | ||
} | ||
|
||
if cap(unmarshalBuf) < int(sz) { | ||
unmarshalBuf = make([]byte, sz) | ||
} | ||
|
||
if _, err = io.ReadFull(br, unmarshalBuf[:sz]); err != nil { | ||
return err | ||
} | ||
|
||
list := &pb.KVList{} | ||
if err := proto.Unmarshal(unmarshalBuf[:sz], list); err != nil { | ||
return err | ||
} | ||
|
||
for _, kv := range list.Kv { | ||
if id, err := decodeBadgerKeyForDocID(kv.Key); err != nil { | ||
return err | ||
} else if id < minID || id > maxID { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar comment, can we use |
||
continue | ||
} | ||
if err := ldr.Set(kv); err != nil { | ||
return err | ||
} | ||
|
||
// Update nextTxnTs, memtable stores this | ||
// timestamp in badger head when flushed. | ||
if kv.Version >= db.orc.nextTxnTs { | ||
db.orc.nextTxnTs = kv.Version + 1 | ||
} | ||
} | ||
} | ||
|
||
if err := ldr.Finish(); err != nil { | ||
return err | ||
} | ||
db.orc.txnMark.Done(db.orc.nextTxnTs - 1) | ||
return nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we pass in configDocRanges (docs.RangeSlice) instead of minID and maxID?