sqlite.GetBlocks()
This commit is contained in:
parent
d44aa103d4
commit
d4a362668c
@ -13,5 +13,6 @@ type Block struct {
|
||||
type DBAccessor interface {
|
||||
Migrate() error
|
||||
FindLatestBlocks(mintime int64, limit int) ([]Block, error)
|
||||
GetBlocks(pos1 coords.MapBlockCoords, pos2 coords.MapBlockCoords) ([]Block, error)
|
||||
GetBlock(pos coords.MapBlockCoords) (*Block, error)
|
||||
}
|
||||
|
52
db/sqlite.go
52
db/sqlite.go
@ -6,6 +6,8 @@ import (
|
||||
"github.com/sirupsen/logrus"
|
||||
"mapserver/coords"
|
||||
"time"
|
||||
"strings"
|
||||
"errors"
|
||||
)
|
||||
|
||||
const migrateScript = `
|
||||
@ -100,6 +102,56 @@ func (db *Sqlite3Accessor) GetBlock(pos coords.MapBlockCoords) (*Block, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
|
||||
func (db *Sqlite3Accessor) GetBlocks(pos1 coords.MapBlockCoords, pos2 coords.MapBlockCoords) ([]Block, error){
|
||||
|
||||
poslist := make([]interface{}, 0)
|
||||
|
||||
if pos1.X != pos2.X {
|
||||
return nil, errors.New("x does not line up")
|
||||
}
|
||||
|
||||
if pos1.Z != pos2.Z {
|
||||
return nil, errors.New("z does not line up")
|
||||
}
|
||||
|
||||
minY := pos1.Y
|
||||
maxY := pos2.Y
|
||||
|
||||
if minY > maxY {
|
||||
minY, maxY = maxY, minY
|
||||
}
|
||||
|
||||
for y := minY; y <= maxY; y++ {
|
||||
poslist = append(poslist, coords.CoordToPlain(coords.NewMapBlockCoords(pos1.X, y, pos1.Z)))
|
||||
}
|
||||
|
||||
getBlocksQuery := "select pos,data,mtime from blocks b where b.pos in (?" + strings.Repeat(",?", len(poslist)-1) + ")"
|
||||
|
||||
rows, err := db.db.Query(getBlocksQuery, poslist...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
blocklist := make([]Block, 0)
|
||||
|
||||
for rows.Next() {
|
||||
var pos int64
|
||||
var data []byte
|
||||
var mtime int64
|
||||
|
||||
err = rows.Scan(&pos, &data, &mtime)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
mb := convertRows(pos, data, mtime)
|
||||
blocklist = append(blocklist, mb)
|
||||
}
|
||||
|
||||
return blocklist, nil
|
||||
}
|
||||
|
||||
func NewSqliteAccessor(filename string) (*Sqlite3Accessor, error) {
|
||||
db, err := sql.Open("sqlite3", filename+"?mode=ro")
|
||||
if err != nil {
|
||||
|
@ -74,3 +74,34 @@ func TestMigrateAndQuery(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
func TestMigrateAndQueryStride(t *testing.T) {
|
||||
tmpfile, err := ioutil.TempFile("", "TestMigrateAndQueryStride.*.sqlite")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer os.Remove(tmpfile.Name())
|
||||
|
||||
testutils.CreateTestDatabase(tmpfile.Name())
|
||||
a, err := NewSqliteAccessor(tmpfile.Name())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = a.Migrate()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
blocks, err := a.GetBlocks(coords.NewMapBlockCoords(0, -1, 0), coords.NewMapBlockCoords(0, 10, 0))
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if blocks == nil {
|
||||
t.Fatal("no data")
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user