diff --git a/server/db/accessor.go b/server/db/accessor.go index 9d8d126..4db6e1b 100644 --- a/server/db/accessor.go +++ b/server/db/accessor.go @@ -23,6 +23,7 @@ type InitialBlocksResult struct { type DBAccessor interface { Migrate() error + GetTimestamp() (int64, error) FindBlocksByMtime(gtmtime int64, limit int) ([]*Block, error) FindNextInitialBlocks(s settings.Settings, layers []*layer.Layer, limit int) (*InitialBlocksResult, error) GetBlock(pos *coords.MapBlockCoords) (*Block, error) diff --git a/server/db/postgres/postgres.go b/server/db/postgres/postgres.go index 5ef374e..0066f36 100644 --- a/server/db/postgres/postgres.go +++ b/server/db/postgres/postgres.go @@ -91,6 +91,28 @@ func (this *PostgresAccessor) CountBlocks(frommtime, tomtime int64) (int, error) return 0, nil } +func (db *PostgresAccessor) GetTimestamp() (int64, error) { + rows, err := db.db.Query(getTimestampQuery) + if err != nil { + return 0, err + } + + defer rows.Close() + + if rows.Next() { + var ts int64 + + err = rows.Scan(&ts) + if err != nil { + return 0, err + } + + return ts, nil + } + + return 0, nil +} + func (this *PostgresAccessor) GetBlock(pos *coords.MapBlockCoords) (*db.Block, error) { rows, err := this.db.Query(getBlockQuery, pos.X, pos.Y, pos.Z) if err != nil { diff --git a/server/db/postgres/sql.go b/server/db/postgres/sql.go index e3509cf..9cc3147 100644 --- a/server/db/postgres/sql.go +++ b/server/db/postgres/sql.go @@ -34,6 +34,10 @@ const countBlocksQuery = ` select count(*) from blocks where mtime >= $1 and mtime <= $2 ` +const getTimestampQuery = ` +select floor(EXTRACT(EPOCH from now()) * 1000) +` + const getBlockQuery = ` select posx,posy,posz,data,mtime from blocks b where b.posx = $1 diff --git a/server/db/sqlite/sql.go b/server/db/sqlite/sql.go index a923fd2..02105dc 100644 --- a/server/db/sqlite/sql.go +++ b/server/db/sqlite/sql.go @@ -15,3 +15,7 @@ select count(*) from blocks b const getBlockQuery = ` select pos,data,mtime from blocks b where b.pos = ? ` + +const getTimestampQuery = ` +select strftime('%s', 'now') +` diff --git a/server/db/sqlite/sqlite.go b/server/db/sqlite/sqlite.go index 3259169..98cd9c7 100644 --- a/server/db/sqlite/sqlite.go +++ b/server/db/sqlite/sqlite.go @@ -106,6 +106,28 @@ func (db *Sqlite3Accessor) CountBlocks() (int, error) { return 0, nil } +func (db *Sqlite3Accessor) GetTimestamp() (int64, error) { + rows, err := db.db.Query(getTimestampQuery) + if err != nil { + return 0, err + } + + defer rows.Close() + + if rows.Next() { + var ts int64 + + err = rows.Scan(&ts) + if err != nil { + return 0, err + } + + return ts, nil + } + + return 0, nil +} + func (db *Sqlite3Accessor) GetBlock(pos *coords.MapBlockCoords) (*db.Block, error) { ppos := coords.CoordToPlain(pos) diff --git a/server/db/sqlite/sqlite_test.go b/server/db/sqlite/sqlite_test.go index e253956..79683b7 100644 --- a/server/db/sqlite/sqlite_test.go +++ b/server/db/sqlite/sqlite_test.go @@ -103,3 +103,31 @@ func TestMigrateAndQueryCount(t *testing.T) { t.Fatal("zero count") } } + +func TestMigrateAndQueryTimestamp(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 := New(tmpfile.Name()) + if err != nil { + panic(err) + } + + err = a.Migrate() + if err != nil { + panic(err) + } + + count, err := a.GetTimestamp() + if err != nil { + panic(err) + } + + if count <= 0 { + t.Fatal("zero count") + } +}