diff --git a/cmd/mtredisalize/sqlite.go b/cmd/mtredisalize/sqlite.go index 4929f29..e72b890 100644 --- a/cmd/mtredisalize/sqlite.go +++ b/cmd/mtredisalize/sqlite.go @@ -17,18 +17,13 @@ import ( var globalLock sync.RWMutex const ( - fetchSQL = "SELECT data FROM blocks WHERE pos = ?" - existsSQL = "SELECT 1 FROM blocks WHERE pos = ?" - updateSQL = "UPDATE blocks SET data = ? WHERE pos = ?" - insertSQL = "INSERT INTO blocks (pos, data) VALUES (?, ?)" - countSQL = "SELECT count(*) FROM blocks" - keysSQL = "SELECT pos FROM blocks" - rangeSQL = "SELECT pos, data FROM blocks WHERE pos BETWEEN ? AND ? ORDER BY pos" - rangeDuffSQL = "SELECT pos, data FROM blocks WHERE " + - "pos BETWEEN ? AND ? OR " + - "pos BETWEEN ? AND ? OR " + - "pos BETWEEN ? AND ? OR " + - "pos BETWEEN ? AND ?" + fetchSQL = "SELECT data FROM blocks WHERE pos = ?" + existsSQL = "SELECT 1 FROM blocks WHERE pos = ?" + updateSQL = "UPDATE blocks SET data = ? WHERE pos = ?" + insertSQL = "INSERT INTO blocks (pos, data) VALUES (?, ?)" + countSQL = "SELECT count(*) FROM blocks" + keysSQL = "SELECT pos FROM blocks" + rangeSQL = "SELECT pos, data FROM blocks WHERE pos BETWEEN ? AND ? ORDER BY pos" ) type SqliteBackend struct { @@ -105,14 +100,7 @@ func NewSqliteBackend( return } - var rS string - if interleaved { - rS = rangeSQL - } else { - rS = rangeDuffSQL - } - - if res.rangeStmt, err = res.db.Prepare(rS); err != nil { + if res.rangeStmt, err = res.db.Prepare(rangeSQL); err != nil { res.closeAll() return } @@ -421,52 +409,6 @@ func (ss *SqliteSession) interleavedSpatialQuery(first, second []byte, done chan return } -type duffStmt struct { - stmt *sql.Stmt - counter int - params [8]int64 -} - -func (ds *duffStmt) push(a, b int64) bool { - ds.params[ds.counter] = a - ds.counter++ - ds.params[ds.counter] = b - ds.counter++ - return ds.counter > 7 -} - -func (ds *duffStmt) Query() (*sql.Rows, error) { - c := ds.counter - ds.counter = 0 - switch c { - case 8: - return ds.stmt.Query( - ds.params[0], ds.params[1], - ds.params[2], ds.params[3], - ds.params[4], ds.params[5], - ds.params[0], ds.params[1]) - case 6: - return ds.stmt.Query( - ds.params[0], ds.params[1], - ds.params[2], ds.params[3], - ds.params[4], ds.params[5], - ds.params[0], ds.params[1]) - case 4: - return ds.stmt.Query( - ds.params[0], ds.params[1], - ds.params[2], ds.params[3], - ds.params[0], ds.params[1], - ds.params[0], ds.params[1]) - case 2: - return ds.stmt.Query( - ds.params[0], ds.params[1], - ds.params[0], ds.params[1], - ds.params[0], ds.params[1], - ds.params[0], ds.params[1]) - } - return nil, nil -} - func (ss *SqliteSession) plainSpatialQuery(first, second []byte, done chan struct{}) (blocks chan Block, err error) { var ( @@ -490,18 +432,17 @@ func (ss *SqliteSession) plainSpatialQuery(first, second []byte, done chan struc go func() { defer globalLock.RUnlock() defer close(blocks) - rangeStmt := duffStmt{stmt: ss.txStmt(ss.backend.rangeStmt)} + rangeStmt := ss.txStmt(ss.backend.rangeStmt) send := func(rows *sql.Rows, err error) bool { - if rows == nil { - return true - } if err != nil { log.Printf("Error in range query: %s\n", err) return false } defer rows.Close() + count := 0 + for rows.Next() { var key int64 var data []byte @@ -519,6 +460,7 @@ func (ss *SqliteSession) plainSpatialQuery(first, second []byte, done chan struc case <-done: return false } + count++ } if err = rows.Err(); err != nil { log.Printf("Error in range query: %s\n", err) @@ -527,37 +469,27 @@ func (ss *SqliteSession) plainSpatialQuery(first, second []byte, done chan struc return true } - if ss.backend.coverage == nil { - a, b := common.Coord{X: c1.X}, common.Coord{X: c2.X} - for a.Y = c2.Y; a.Y >= c1.Y; a.Y-- { - b.Y = a.Y - for a.Z = c1.Z; a.Z <= c2.Z; a.Z++ { - b.Z = a.Z - // Ordering should not be necessary. - from, to := order(common.CoordToPlain(a), common.CoordToPlain(b)) - if rangeStmt.push(from, to) && !send(rangeStmt.Query()) { - return - } - } - } - } else { - var a, b common.Coord + var a, b common.Coord - for _, r := range ss.backend.coverage.Query(c1, c2) { - a.Z, b.Z = int16(r.Z), int16(r.Z) - a.X, b.X = int16(r.X1), int16(r.X2) - // log.Printf("y1 y2 x1 x2 z: %d %d, %d %d, %d\n", r.Y1, r.Y2, r.X1, r.X2, r.Z) - for y := r.Y2; y >= r.Y1; y-- { - a.Y, b.Y = int16(y), int16(y) - from, to := order(common.CoordToPlain(a), common.CoordToPlain(b)) - if rangeStmt.push(from, to) && !send(rangeStmt.Query()) { - return - } + for _, r := range ss.backend.coverage.Query(c1, c2) { + a.Z, b.Z = int16(r.Z), int16(r.Z) + a.X, b.X = int16(r.X1), int16(r.X2) + // log.Printf("y1 y2 x1 x2 z: %d %d, %d %d, %d\n", r.Y1, r.Y2, r.X1, r.X2, r.Z) + for a.Y = r.Y2; a.Y >= r.Y1; a.Y-- { + b.Y = a.Y + from, to := common.CoordToPlain(a), common.CoordToPlain(b) + if !send(rangeStmt.Query(from, to)) { + return } } } - send(rangeStmt.Query()) }() return } + +func clone(a []byte) []byte { + b := make([]byte, len(a)) + copy(b, a) + return b +} diff --git a/common/basetilecreator.go b/common/basetilecreator.go index 84e2003..5517580 100644 --- a/common/basetilecreator.go +++ b/common/basetilecreator.go @@ -132,7 +132,7 @@ func (btc *BaseTileCreator) CreateTile(x, z int16, i, j int) error { return err } } - log.Printf("Writing empty (%d, %d) to file %s\n", x, z, path) + //log.Printf("Writing empty (%d, %d) to file %s\n", x, z, path) return ioutil.WriteFile(path, btc.emptyImage, 0666) }