DBAccessor.getTimestamp()

This commit is contained in:
NatureFreshMilk 2019-04-03 08:15:32 +02:00
parent 70ab8ee90a
commit 33d30bc91d
6 changed files with 81 additions and 0 deletions

View File

@ -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)

View File

@ -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 {

View File

@ -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

View File

@ -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')
`

View File

@ -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)

View File

@ -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")
}
}