mirror of
https://bitbucket.org/s_l_teichmann/mtsatellite
synced 2025-01-11 09:20:17 +01:00
Moved Block as type to common structures.
This commit is contained in:
parent
70e8d5a548
commit
d75fe4f102
7
common/block.go
Normal file
7
common/block.go
Normal file
@ -0,0 +1,7 @@
|
||||
package common
|
||||
|
||||
// Block data from Minetest database.
|
||||
type Block struct {
|
||||
Coord Coord
|
||||
Data []byte
|
||||
}
|
@ -14,24 +14,18 @@ import (
|
||||
var ErrNoMoreBlocks = errors.New("No more blocks.")
|
||||
|
||||
type (
|
||||
// Block data from Minetest database.
|
||||
Block struct {
|
||||
Coord common.Coord
|
||||
Data []byte
|
||||
}
|
||||
|
||||
// BlockProducer is used to over a existing Minetest database
|
||||
// and return its content block by block.
|
||||
BlockProducer interface {
|
||||
// error is ErrNoMoreBlocks if it run out of blocks.
|
||||
Next(*Block) error
|
||||
Next(*common.Block) error
|
||||
// Closes the open database connections.
|
||||
Close() error
|
||||
}
|
||||
|
||||
// BlockConsumer is used to store blocks in a new Minetest database.
|
||||
BlockConsumer interface {
|
||||
Consume(*Block) error
|
||||
Consume(*common.Block) error
|
||||
// Closes the open database connections.
|
||||
Close() error
|
||||
}
|
||||
|
@ -75,7 +75,7 @@ func (ldbp *LevelDBBlockProducer) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ldbp *LevelDBBlockProducer) Next(block *Block) (err error) {
|
||||
func (ldbp *LevelDBBlockProducer) Next(block *common.Block) (err error) {
|
||||
if ldbp.iterator == nil {
|
||||
err = ErrNoMoreBlocks
|
||||
return
|
||||
@ -127,7 +127,7 @@ func (ldbc *LevelDBBlockConsumer) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ldbc *LevelDBBlockConsumer) Consume(block *Block) (err error) {
|
||||
func (ldbc *LevelDBBlockConsumer) Consume(block *common.Block) (err error) {
|
||||
var encodedKey []byte
|
||||
if encodedKey, err = ldbc.encoder(ldbc.joiner(block.Coord)); err != nil {
|
||||
return
|
||||
|
@ -51,16 +51,16 @@ func selectKeyEncoder(interleaved bool) common.KeyEncoder {
|
||||
|
||||
func copyProducerToConsumer(producer BlockProducer, consumer BlockConsumer) error {
|
||||
|
||||
blocks := make(chan *Block)
|
||||
blocks := make(chan *common.Block)
|
||||
done := make(chan struct{})
|
||||
defer close(done)
|
||||
|
||||
pool := sync.Pool{New: func() interface{} { return new(Block) }}
|
||||
pool := sync.Pool{New: func() interface{} { return new(common.Block) }}
|
||||
|
||||
go func() {
|
||||
defer close(blocks)
|
||||
for {
|
||||
block := pool.Get().(*Block)
|
||||
block := pool.Get().(*common.Block)
|
||||
if err := producer.Next(block); err != nil {
|
||||
if err != ErrNoMoreBlocks {
|
||||
log.Printf("Reading failed: %s\n", err)
|
||||
|
@ -112,7 +112,7 @@ func (sbc *SQLiteBlockConsumer) getTx() (tx *sql.Tx, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (sbc *SQLiteBlockConsumer) Consume(block *Block) (err error) {
|
||||
func (sbc *SQLiteBlockConsumer) Consume(block *common.Block) (err error) {
|
||||
var tx *sql.Tx
|
||||
if tx, err = sbc.getTx(); err != nil {
|
||||
return
|
||||
@ -149,7 +149,7 @@ func NewSQLiteBlockProducer(
|
||||
return
|
||||
}
|
||||
|
||||
func (sbp *SQLiteBlockProducer) Next(block *Block) (err error) {
|
||||
func (sbp *SQLiteBlockProducer) Next(block *common.Block) (err error) {
|
||||
if sbp.rows == nil {
|
||||
err = ErrNoMoreBlocks
|
||||
return
|
||||
|
Loading…
Reference in New Issue
Block a user