Moved Block as type to common structures.

This commit is contained in:
Sascha L. Teichmann 2014-09-07 15:57:25 +02:00
parent 70e8d5a548
commit d75fe4f102
5 changed files with 16 additions and 15 deletions

7
common/block.go Normal file
View File

@ -0,0 +1,7 @@
package common
// Block data from Minetest database.
type Block struct {
Coord Coord
Data []byte
}

View File

@ -14,24 +14,18 @@ import (
var ErrNoMoreBlocks = errors.New("No more blocks.") var ErrNoMoreBlocks = errors.New("No more blocks.")
type ( type (
// Block data from Minetest database.
Block struct {
Coord common.Coord
Data []byte
}
// BlockProducer is used to over a existing Minetest database // BlockProducer is used to over a existing Minetest database
// and return its content block by block. // and return its content block by block.
BlockProducer interface { BlockProducer interface {
// error is ErrNoMoreBlocks if it run out of blocks. // error is ErrNoMoreBlocks if it run out of blocks.
Next(*Block) error Next(*common.Block) error
// Closes the open database connections. // Closes the open database connections.
Close() error Close() error
} }
// BlockConsumer is used to store blocks in a new Minetest database. // BlockConsumer is used to store blocks in a new Minetest database.
BlockConsumer interface { BlockConsumer interface {
Consume(*Block) error Consume(*common.Block) error
// Closes the open database connections. // Closes the open database connections.
Close() error Close() error
} }

View File

@ -75,7 +75,7 @@ func (ldbp *LevelDBBlockProducer) Close() error {
return nil return nil
} }
func (ldbp *LevelDBBlockProducer) Next(block *Block) (err error) { func (ldbp *LevelDBBlockProducer) Next(block *common.Block) (err error) {
if ldbp.iterator == nil { if ldbp.iterator == nil {
err = ErrNoMoreBlocks err = ErrNoMoreBlocks
return return
@ -127,7 +127,7 @@ func (ldbc *LevelDBBlockConsumer) Close() error {
return nil return nil
} }
func (ldbc *LevelDBBlockConsumer) Consume(block *Block) (err error) { func (ldbc *LevelDBBlockConsumer) Consume(block *common.Block) (err error) {
var encodedKey []byte var encodedKey []byte
if encodedKey, err = ldbc.encoder(ldbc.joiner(block.Coord)); err != nil { if encodedKey, err = ldbc.encoder(ldbc.joiner(block.Coord)); err != nil {
return return

View File

@ -51,16 +51,16 @@ func selectKeyEncoder(interleaved bool) common.KeyEncoder {
func copyProducerToConsumer(producer BlockProducer, consumer BlockConsumer) error { func copyProducerToConsumer(producer BlockProducer, consumer BlockConsumer) error {
blocks := make(chan *Block) blocks := make(chan *common.Block)
done := make(chan struct{}) done := make(chan struct{})
defer close(done) defer close(done)
pool := sync.Pool{New: func() interface{} { return new(Block) }} pool := sync.Pool{New: func() interface{} { return new(common.Block) }}
go func() { go func() {
defer close(blocks) defer close(blocks)
for { for {
block := pool.Get().(*Block) block := pool.Get().(*common.Block)
if err := producer.Next(block); err != nil { if err := producer.Next(block); err != nil {
if err != ErrNoMoreBlocks { if err != ErrNoMoreBlocks {
log.Printf("Reading failed: %s\n", err) log.Printf("Reading failed: %s\n", err)

View File

@ -112,7 +112,7 @@ func (sbc *SQLiteBlockConsumer) getTx() (tx *sql.Tx, err error) {
return return
} }
func (sbc *SQLiteBlockConsumer) Consume(block *Block) (err error) { func (sbc *SQLiteBlockConsumer) Consume(block *common.Block) (err error) {
var tx *sql.Tx var tx *sql.Tx
if tx, err = sbc.getTx(); err != nil { if tx, err = sbc.getTx(); err != nil {
return return
@ -149,7 +149,7 @@ func NewSQLiteBlockProducer(
return return
} }
func (sbp *SQLiteBlockProducer) Next(block *Block) (err error) { func (sbp *SQLiteBlockProducer) Next(block *common.Block) (err error) {
if sbp.rows == nil { if sbp.rows == nil {
err = ErrNoMoreBlocks err = ErrNoMoreBlocks
return return