diff --git a/node/main.go b/node/main.go index 0bc5a87..a2bafa9 100644 --- a/node/main.go +++ b/node/main.go @@ -97,6 +97,11 @@ var ( false, "print node related information", ) + migrate = flag.String( + "migrate", + "", + "migrate from Pebble to MDBX from specified path (e.g. /home/user/backup/.config/store)", + ) debug = flag.Bool( "debug", false, @@ -375,6 +380,60 @@ func main() { console.Run() return } + if *migrate != "" { + fmt.Printf("Migrating from Pebble to MDBX from %s\n", *migrate) + d, err := os.Stat(filepath.Join(*migrate, "LOCK")) + if err != nil || d == nil { + fmt.Printf("%s does not look like a pebble db! Double check your path", *migrate) + return + } + dbConfig := &config.DBConfig{ + Path: *migrate, + } + pebbleInput := store.NewPebbleDB(dbConfig) + mdbxOutput := store.NewMDBXDB(nodeConfig.DB) + + allIter, err := pebbleInput.NewIter(nil, nil) + if err != nil { + panic(err) + } + batch := mdbxOutput.NewBatch(false) + total := 0 + for allIter.First(); allIter.Valid(); allIter.Next() { + err := batch.Set(allIter.Key(), allIter.Value()) + if err != nil { + panic(err) + } + total++ + if total%10_000 == 0 { + err := batch.Commit() + if err != nil { + panic(err) + } + fmt.Printf("Commit. Total: %d", total) + } + } + err = batch.Commit() + if err != nil { + panic(err) + } + fmt.Printf("Commit. Total: %d", total) + err = allIter.Close() + if err != nil { + panic(err) + } + err = pebbleInput.Close() + if err != nil { + panic(err) + } + err = mdbxOutput.Close() + if err != nil { + panic(err) + } + fmt.Println("Done.") + + return + } if *dhtOnly { done := make(chan os.Signal, 1) diff --git a/node/store/mdbx.go b/node/store/mdbx.go index 5e71eda..b7b99c6 100644 --- a/node/store/mdbx.go +++ b/node/store/mdbx.go @@ -553,6 +553,9 @@ func (m *MDBXBatch) Commit() error { return err } key, _, err := cursor.Get(op.operand1, nil, mdbx.SetRange) + if err != nil { + return err + } for bytes.Compare(key, op.operand1) >= 0 && bytes.Compare(key, op.operand2) < 0 { err = cursor.Del(mdbx.Current) if err != nil {