2014-08-19 12:07:57 +02:00
|
|
|
// 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
|
|
|
|
|
2014-08-21 14:46:34 +02:00
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2014-08-26 08:42:03 +02:00
|
|
|
"sync"
|
2014-08-21 14:46:34 +02:00
|
|
|
|
|
|
|
"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()
|
|
|
|
}
|
|
|
|
|
2014-08-23 16:01:57 +02:00
|
|
|
func selectKeySplitter(interleaved bool) common.KeySplitter {
|
|
|
|
if interleaved {
|
|
|
|
return common.InterleavedToCoord
|
|
|
|
}
|
|
|
|
return common.PlainToCoord
|
|
|
|
}
|
|
|
|
|
|
|
|
func selectKeyJoiner(interleaved bool) common.KeyJoiner {
|
|
|
|
if interleaved {
|
|
|
|
return common.CoordToInterleaved
|
|
|
|
}
|
|
|
|
return common.CoordToPlain
|
|
|
|
}
|
|
|
|
|
|
|
|
func selectKeyDecoder(interleaved bool) common.KeyDecoder {
|
|
|
|
if interleaved {
|
|
|
|
return common.DecodeFromBigEndian
|
|
|
|
}
|
|
|
|
return common.DecodeStringFromBytes
|
|
|
|
}
|
|
|
|
|
|
|
|
func selectKeyEncoder(interleaved bool) common.KeyEncoder {
|
|
|
|
if interleaved {
|
|
|
|
return common.EncodeToBigEndian
|
|
|
|
}
|
|
|
|
return common.EncodeStringToBytes
|
|
|
|
}
|
|
|
|
|
2014-08-24 19:40:57 +02:00
|
|
|
func copyProducerToConsumer(producer BlockProducer, consumer BlockConsumer) error {
|
2014-08-24 19:27:16 +02:00
|
|
|
|
|
|
|
blocks := make(chan *Block)
|
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
|
2014-08-26 08:42:03 +02:00
|
|
|
pool := sync.Pool{New: func() interface{} { return new(Block) }}
|
|
|
|
|
2014-08-24 19:27:16 +02:00
|
|
|
go func() {
|
|
|
|
defer close(blocks)
|
|
|
|
for {
|
2014-08-26 08:42:03 +02:00
|
|
|
block := pool.Get().(*Block)
|
2014-08-24 19:40:57 +02:00
|
|
|
if err := producer.Next(block); err != nil {
|
2014-08-24 19:27:16 +02:00
|
|
|
if err != ErrNoMoreBlocks {
|
|
|
|
log.Printf("Reading failed: %s\n", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case blocks <- block:
|
|
|
|
case <-done:
|
|
|
|
return
|
2014-08-21 14:46:34 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-24 19:27:16 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for block := range blocks {
|
2014-08-24 19:40:57 +02:00
|
|
|
if err := consumer.Consume(block); err != nil {
|
|
|
|
return err
|
2014-08-21 14:46:34 +02:00
|
|
|
}
|
2014-08-26 08:42:03 +02:00
|
|
|
block.Data = nil
|
|
|
|
pool.Put(block)
|
2014-08-21 14:46:34 +02:00
|
|
|
i++
|
|
|
|
if i%1000 == 0 {
|
2014-08-24 18:50:16 +02:00
|
|
|
log.Printf("%d blocks transferred.\n", i)
|
2014-08-21 14:46:34 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-24 18:50:16 +02:00
|
|
|
log.Printf("%d blocks transferred in total.\n", i)
|
2014-08-21 14:46:34 +02:00
|
|
|
|
2014-08-24 19:40:57 +02:00
|
|
|
return nil
|
2014-08-21 14:46:34 +02:00
|
|
|
}
|
|
|
|
|
2014-08-19 12:07:57 +02:00
|
|
|
func main() {
|
2014-08-21 14:46:34 +02:00
|
|
|
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),
|
2014-08-23 16:01:57 +02:00
|
|
|
selectKeySplitter(srcInterleaved)); err != nil {
|
2014-08-21 14:46:34 +02:00
|
|
|
log.Fatalf("Cannot open '%s': %s", flag.Arg(0), err)
|
|
|
|
}
|
|
|
|
} else { // LevelDB
|
|
|
|
if producer, err = NewLevelDBBlockProducer(
|
|
|
|
flag.Arg(0),
|
2014-08-23 16:01:57 +02:00
|
|
|
selectKeySplitter(srcInterleaved),
|
|
|
|
selectKeyDecoder(srcInterleaved)); err != nil {
|
2014-08-21 14:46:34 +02:00
|
|
|
log.Fatalf("Cannot open '%s': %s", flag.Arg(0), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer producer.Close()
|
|
|
|
|
|
|
|
if dstBackend == "sqlite" {
|
|
|
|
if consumer, err = NewSQLiteBlockConsumer(
|
2014-08-23 16:01:57 +02:00
|
|
|
flag.Arg(1),
|
|
|
|
selectKeyJoiner(dstInterleaved)); err != nil {
|
2014-08-21 14:46:34 +02:00
|
|
|
log.Fatalf("Cannot open '%s': %s", flag.Arg(1), err)
|
|
|
|
}
|
|
|
|
} else { // LevelDB
|
|
|
|
if consumer, err = NewLevelDBBlockConsumer(
|
|
|
|
flag.Arg(1),
|
2014-08-23 16:01:57 +02:00
|
|
|
selectKeyJoiner(dstInterleaved),
|
|
|
|
selectKeyEncoder(dstInterleaved)); err != nil {
|
2014-08-21 14:46:34 +02:00
|
|
|
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)
|
|
|
|
}
|
2014-08-19 12:07:57 +02:00
|
|
|
}
|