From d4a362668ca9c360dfef2ca6107696335fa805cc Mon Sep 17 00:00:00 2001 From: NatureFreshMilk Date: Thu, 17 Jan 2019 14:42:02 +0100 Subject: [PATCH] sqlite.GetBlocks() --- db/accessor.go | 1 + db/sqlite.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++ db/sqlite_test.go | 31 ++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) diff --git a/db/accessor.go b/db/accessor.go index c861d11..d1e87a5 100644 --- a/db/accessor.go +++ b/db/accessor.go @@ -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) } diff --git a/db/sqlite.go b/db/sqlite.go index d34b0c7..f352bb3 100644 --- a/db/sqlite.go +++ b/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 { diff --git a/db/sqlite_test.go b/db/sqlite_test.go index df985d9..a38f32d 100644 --- a/db/sqlite_test.go +++ b/db/sqlite_test.go @@ -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") + } + +}