mirror of
https://bitbucket.org/s_l_teichmann/mtsatellite
synced 2024-11-08 11:10:27 +01:00
121 lines
3.0 KiB
Go
121 lines
3.0 KiB
Go
// Copyright 2014 by Sascha L. Teichmann
|
|
// Use of this source code is governed by the MIT license
|
|
// that can be found in the LICENSE file.
|
|
|
|
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
|
|
"bitbucket.org/s_l_teichmann/mtredisalize/common"
|
|
)
|
|
|
|
func usage() {
|
|
fmt.Fprintf(os.Stderr,
|
|
"Usage: %s [<options>] <source database> <dest database>\n", os.Args[0])
|
|
fmt.Fprintln(os.Stderr, "Options:")
|
|
flag.PrintDefaults()
|
|
}
|
|
|
|
func copyProducerToConsumer(producer BlockProducer, consumer BlockConsumer) (err error) {
|
|
var block Block
|
|
i := 0
|
|
for {
|
|
if err = producer.Next(&block); err != nil {
|
|
if err == ErrNoMoreBlocks {
|
|
err = nil
|
|
break
|
|
}
|
|
return
|
|
}
|
|
if err = consumer.Consume(&block); err != nil {
|
|
return
|
|
}
|
|
i++
|
|
if i%1000 == 0 {
|
|
log.Printf("%i blocks transferred.\n", i)
|
|
}
|
|
}
|
|
log.Printf("%i blocks transferred in total.\n", i)
|
|
|
|
return
|
|
}
|
|
|
|
func main() {
|
|
var (
|
|
srcBackend string
|
|
dstBackend string
|
|
srcInterleaved bool
|
|
dstInterleaved bool
|
|
)
|
|
|
|
flag.Usage = usage
|
|
|
|
flag.StringVar(&srcBackend, "source-backend", "sqlite",
|
|
"type of source database (leveldb, sqlite)")
|
|
flag.StringVar(&srcBackend, "sb", "sqlite",
|
|
"type of source database (leveldb, sqlite). Shorthand")
|
|
flag.StringVar(&dstBackend, "dest-backend", "leveldb",
|
|
"type of destination database (leveldb, sqlite)")
|
|
flag.StringVar(&dstBackend, "db", "leveldb",
|
|
"type of destination database (leveldb, sqlite). Shorthand")
|
|
flag.BoolVar(&srcInterleaved, "source-interleaved", false,
|
|
"Is source database interleaved?")
|
|
flag.BoolVar(&srcInterleaved, "si", false,
|
|
"Is source database interleaved? Shorthand")
|
|
flag.BoolVar(&dstInterleaved, "dest-interleaved", true,
|
|
"Should dest database be interleaved?")
|
|
flag.BoolVar(&dstInterleaved, "di", true,
|
|
"Should source database be interleaved? Shorthand")
|
|
|
|
flag.Parse()
|
|
|
|
if flag.NArg() < 2 {
|
|
log.Fatal("Missing source and/or destination database.")
|
|
}
|
|
|
|
var (
|
|
producer BlockProducer
|
|
consumer BlockConsumer
|
|
err error
|
|
)
|
|
|
|
if srcBackend == "sqlite" {
|
|
if producer, err = NewSQLiteBlockProducer(
|
|
flag.Arg(0),
|
|
common.SelectKeySplitter(srcInterleaved)); err != nil {
|
|
log.Fatalf("Cannot open '%s': %s", flag.Arg(0), err)
|
|
}
|
|
} else { // LevelDB
|
|
if producer, err = NewLevelDBBlockProducer(
|
|
flag.Arg(0),
|
|
common.SelectKeySplitter(srcInterleaved),
|
|
common.SelectKeyDecoder(srcInterleaved)); err != nil {
|
|
log.Fatalf("Cannot open '%s': %s", flag.Arg(0), err)
|
|
}
|
|
}
|
|
defer producer.Close()
|
|
|
|
if dstBackend == "sqlite" {
|
|
if consumer, err = NewSQLiteBlockConsumer(
|
|
flag.Arg(1), common.SelectKeyJoiner(dstInterleaved)); err != nil {
|
|
log.Fatalf("Cannot open '%s': %s", flag.Arg(1), err)
|
|
}
|
|
} else { // LevelDB
|
|
if consumer, err = NewLevelDBBlockConsumer(
|
|
flag.Arg(1),
|
|
common.SelectKeyJoiner(dstInterleaved),
|
|
common.SelectKeyEncoder(dstInterleaved)); err != nil {
|
|
log.Fatalf("Cannot open '%s': %s", flag.Arg(1), err)
|
|
}
|
|
}
|
|
defer consumer.Close()
|
|
|
|
if err = copyProducerToConsumer(producer, consumer); err != nil {
|
|
log.Fatalf("Database transfer failed: %s\n", err)
|
|
}
|
|
}
|