From 72bd7346c91fd4506ecc9cc08ae0b60be05fec10 Mon Sep 17 00:00:00 2001 From: Thomas Rudin Date: Thu, 14 Feb 2019 22:20:56 +0100 Subject: [PATCH] half working --- server/db/postgres/initialblocks.go | 54 +++++++++++++++++++++++------ server/db/postgres/util.go | 22 ++++++++++++ 2 files changed, 65 insertions(+), 11 deletions(-) create mode 100644 server/db/postgres/util.go diff --git a/server/db/postgres/initialblocks.go b/server/db/postgres/initialblocks.go index 7994ff2..3221f35 100644 --- a/server/db/postgres/initialblocks.go +++ b/server/db/postgres/initialblocks.go @@ -68,7 +68,7 @@ func (this *PostgresAccessor) FindNextInitialBlocks(s settings.Settings, layers "lastxblock": lastxblock, "lastyblock": lastyblock, } - log.WithFields(fields).Info("Initial-Query") + log.WithFields(fields).Debug("Initial-Query") minX := math.Min(float64(tcr.Pos1.X), float64(tcr.Pos2.X)) maxX := math.Max(float64(tcr.Pos1.X), float64(tcr.Pos2.X)) @@ -77,6 +77,33 @@ func (this *PostgresAccessor) FindNextInitialBlocks(s settings.Settings, layers minZ := math.Min(float64(tcr.Pos1.Z), float64(tcr.Pos2.Z)) maxZ := math.Max(float64(tcr.Pos1.Z), float64(tcr.Pos2.Z)) + stridecount := this.intQuery(` + select count(*) from blocks + where posx >= $1 and posx <= $2 + and posy >= $3 and posy <= $4 + and mtime = 0`, + minX, maxX, + minY, maxY, + ) + + if stridecount == 0 { + fields = logrus.Fields{ + "minX": minX, + "maxX": maxX, + "minY": minY, + "maxY": maxY, + } + log.WithFields(fields).Info("Skipping stride") + + s.SetInt(SETTING_LAST_LAYER, lastlayer) + s.SetInt(SETTING_LAST_X_BLOCK, lastxblock) + s.SetInt(SETTING_LAST_Y_BLOCK, lastyblock) + + result := &db.InitialBlocksResult{} + result.HasMore = true + return result, nil + } + rows, err := this.db.Query(getBlocksByInitialTileQuery, minX, minY, minZ, maxX, maxY, maxZ, ) @@ -88,18 +115,23 @@ func (this *PostgresAccessor) FindNextInitialBlocks(s settings.Settings, layers defer rows.Close() blocks := make([]*db.Block, 0) - for rows.Next() { - var posx, posy, posz int - var data []byte - var mtime int64 + for { + for rows.Next() { + var posx, posy, posz int + var data []byte + var mtime int64 - err = rows.Scan(&posx, &posy, &posz, &data, &mtime) - if err != nil { - return nil, err + err = rows.Scan(&posx, &posy, &posz, &data, &mtime) + if err != nil { + return nil, err + } + + mb := convertRows(posx, posy, posz, data, mtime) + blocks = append(blocks, mb) + } + if !rows.NextResultSet() { + break } - - mb := convertRows(posx, posy, posz, data, mtime) - blocks = append(blocks, mb) } s.SetInt(SETTING_LAST_LAYER, lastlayer) diff --git a/server/db/postgres/util.go b/server/db/postgres/util.go new file mode 100644 index 0000000..1cdc6d8 --- /dev/null +++ b/server/db/postgres/util.go @@ -0,0 +1,22 @@ +package postgres + +func (this *PostgresAccessor) intQuery(q string, params ...interface{}) int { + rows, err := this.db.Query(q, params...) + if err != nil { + panic(err) + } + + defer rows.Close() + + if rows.Next() { + var result int + err = rows.Scan(&result) + if err != nil { + panic(err) + } + + return result + } + + panic("no result!") +}