mirror of
https://bitbucket.org/s_l_teichmann/mtsatellite
synced 2025-01-11 17:30:18 +01:00
Moved all the block producer/consumer interfaces to common.
This commit is contained in:
parent
d75fe4f102
commit
13bb251f35
@ -1,7 +1,31 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Error returned if a Producer has run to its end.
|
||||||
|
var ErrNoMoreBlocks = errors.New("No more blocks.")
|
||||||
|
|
||||||
|
type (
|
||||||
// Block data from Minetest database.
|
// Block data from Minetest database.
|
||||||
type Block struct {
|
Block struct {
|
||||||
Coord Coord
|
Coord Coord
|
||||||
Data []byte
|
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
|
||||||
|
// Closes the open database connections.
|
||||||
|
Close() error
|
||||||
|
}
|
||||||
|
|
||||||
|
// BlockConsumer is used to store blocks in a new Minetest database.
|
||||||
|
BlockConsumer interface {
|
||||||
|
Consume(*Block) error
|
||||||
|
// Closes the open database connections.
|
||||||
|
Close() error
|
||||||
|
}
|
||||||
|
)
|
||||||
|
@ -1,32 +0,0 @@
|
|||||||
// 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 (
|
|
||||||
"errors"
|
|
||||||
|
|
||||||
"bitbucket.org/s_l_teichmann/mtredisalize/common"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Error returned if a Producer has run to its end.
|
|
||||||
var ErrNoMoreBlocks = errors.New("No more blocks.")
|
|
||||||
|
|
||||||
type (
|
|
||||||
// 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(*common.Block) error
|
|
||||||
// Closes the open database connections.
|
|
||||||
Close() error
|
|
||||||
}
|
|
||||||
|
|
||||||
// BlockConsumer is used to store blocks in a new Minetest database.
|
|
||||||
BlockConsumer interface {
|
|
||||||
Consume(*common.Block) error
|
|
||||||
// Closes the open database connections.
|
|
||||||
Close() error
|
|
||||||
}
|
|
||||||
)
|
|
@ -77,12 +77,12 @@ func (ldbp *LevelDBBlockProducer) Close() error {
|
|||||||
|
|
||||||
func (ldbp *LevelDBBlockProducer) Next(block *common.Block) (err error) {
|
func (ldbp *LevelDBBlockProducer) Next(block *common.Block) (err error) {
|
||||||
if ldbp.iterator == nil {
|
if ldbp.iterator == nil {
|
||||||
err = ErrNoMoreBlocks
|
err = common.ErrNoMoreBlocks
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !ldbp.iterator.Valid() {
|
if !ldbp.iterator.Valid() {
|
||||||
if err = ldbp.iterator.GetError(); err == nil {
|
if err = ldbp.iterator.GetError(); err == nil {
|
||||||
err = ErrNoMoreBlocks
|
err = common.ErrNoMoreBlocks
|
||||||
}
|
}
|
||||||
ldbp.iterator.Close()
|
ldbp.iterator.Close()
|
||||||
ldbp.iterator = nil
|
ldbp.iterator = nil
|
||||||
|
@ -49,7 +49,7 @@ func selectKeyEncoder(interleaved bool) common.KeyEncoder {
|
|||||||
return common.EncodeStringToBytes
|
return common.EncodeStringToBytes
|
||||||
}
|
}
|
||||||
|
|
||||||
func copyProducerToConsumer(producer BlockProducer, consumer BlockConsumer) error {
|
func copyProducerToConsumer(producer common.BlockProducer, consumer common.BlockConsumer) error {
|
||||||
|
|
||||||
blocks := make(chan *common.Block)
|
blocks := make(chan *common.Block)
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
@ -62,7 +62,7 @@ func copyProducerToConsumer(producer BlockProducer, consumer BlockConsumer) erro
|
|||||||
for {
|
for {
|
||||||
block := pool.Get().(*common.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 != common.ErrNoMoreBlocks {
|
||||||
log.Printf("Reading failed: %s\n", err)
|
log.Printf("Reading failed: %s\n", err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -126,8 +126,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
producer BlockProducer
|
producer common.BlockProducer
|
||||||
consumer BlockConsumer
|
consumer common.BlockConsumer
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -151,7 +151,7 @@ func NewSQLiteBlockProducer(
|
|||||||
|
|
||||||
func (sbp *SQLiteBlockProducer) Next(block *common.Block) (err error) {
|
func (sbp *SQLiteBlockProducer) Next(block *common.Block) (err error) {
|
||||||
if sbp.rows == nil {
|
if sbp.rows == nil {
|
||||||
err = ErrNoMoreBlocks
|
err = common.ErrNoMoreBlocks
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if sbp.rows.Next() {
|
if sbp.rows.Next() {
|
||||||
@ -162,7 +162,7 @@ func (sbp *SQLiteBlockProducer) Next(block *common.Block) (err error) {
|
|||||||
} else {
|
} else {
|
||||||
sbp.rows.Close()
|
sbp.rows.Close()
|
||||||
sbp.rows = nil
|
sbp.rows = nil
|
||||||
err = ErrNoMoreBlocks
|
err = common.ErrNoMoreBlocks
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user