2014-08-03 15:59:56 +02:00
|
|
|
// Copyright 2014 by Sascha L. Teichmann
|
|
|
|
// Use of this source code is governed by the MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2014-08-03 14:52:24 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"log"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2014-08-16 16:06:42 +02:00
|
|
|
|
2014-10-03 19:10:40 +02:00
|
|
|
"bitbucket.org/s_l_teichmann/mtsatellite/common"
|
2014-08-03 14:52:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var globalLock sync.RWMutex
|
|
|
|
|
|
|
|
const (
|
|
|
|
fetchSql = "SELECT data FROM blocks WHERE pos = ?"
|
|
|
|
existsSql = "SELECT 1 FROM blocks WHERE pos = ?"
|
|
|
|
updateSql = "UPDATE blocks SET data = ? WHERE pos = ?"
|
|
|
|
insertSql = "INSERT INTO blocks (pos, data) VALUES (?, ?)"
|
2014-08-11 14:56:01 +02:00
|
|
|
countSql = "SELECT count(*) FROM blocks"
|
|
|
|
keysSql = "SELECT pos FROM blocks"
|
2014-09-01 13:46:23 +02:00
|
|
|
rangeSql = "SELECT pos, data FROM blocks WHERE pos BETWEEN ? AND ? ORDER BY pos"
|
2014-08-03 14:52:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type SqliteBackend struct {
|
2014-09-17 09:51:34 +02:00
|
|
|
db *sql.DB
|
|
|
|
encoder common.KeyEncoder
|
|
|
|
decoder common.KeyDecoder
|
|
|
|
changeTracker *ChangeTracker
|
|
|
|
interleaved bool
|
|
|
|
existsStmt *sql.Stmt
|
|
|
|
fetchStmt *sql.Stmt
|
|
|
|
insertStmt *sql.Stmt
|
|
|
|
updateStmt *sql.Stmt
|
|
|
|
countStmt *sql.Stmt
|
|
|
|
keysStmt *sql.Stmt
|
|
|
|
rangeStmt *sql.Stmt
|
2014-08-03 14:52:24 +02:00
|
|
|
}
|
|
|
|
|
2014-08-06 01:11:41 +02:00
|
|
|
type SqliteSession struct {
|
|
|
|
backend *SqliteBackend
|
|
|
|
tx *sql.Tx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *SqliteBackend) NewSession() (Session, error) {
|
|
|
|
return &SqliteSession{ss, nil}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *SqliteSession) Close() error {
|
|
|
|
t := ss.tx
|
|
|
|
if t != nil {
|
|
|
|
ss.tx = nil
|
|
|
|
return t.Rollback()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-17 09:51:34 +02:00
|
|
|
func NewSqliteBackend(
|
|
|
|
path string,
|
|
|
|
changeTracker *ChangeTracker, interleaved bool) (sqlb *SqliteBackend, err error) {
|
2014-08-03 14:52:24 +02:00
|
|
|
|
2014-09-17 09:51:34 +02:00
|
|
|
res := SqliteBackend{interleaved: interleaved, changeTracker: changeTracker}
|
2014-08-03 14:52:24 +02:00
|
|
|
|
|
|
|
if res.db, err = sql.Open("sqlite3", path); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.existsStmt, err = res.db.Prepare(existsSql); err != nil {
|
|
|
|
res.closeAll()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.fetchStmt, err = res.db.Prepare(fetchSql); err != nil {
|
|
|
|
res.closeAll()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.insertStmt, err = res.db.Prepare(insertSql); err != nil {
|
|
|
|
res.closeAll()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.updateStmt, err = res.db.Prepare(updateSql); err != nil {
|
|
|
|
res.closeAll()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-11 14:56:01 +02:00
|
|
|
if res.countStmt, err = res.db.Prepare(countSql); err != nil {
|
|
|
|
res.closeAll()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.keysStmt, err = res.db.Prepare(keysSql); err != nil {
|
|
|
|
res.closeAll()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-01 12:42:57 +02:00
|
|
|
if res.rangeStmt, err = res.db.Prepare(rangeSql); err != nil {
|
|
|
|
res.closeAll()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-23 18:05:40 +02:00
|
|
|
if interleaved {
|
|
|
|
res.encoder = common.EncodeStringToBytesFromInterleaved
|
|
|
|
res.decoder = common.DecodeStringFromBytesToInterleaved
|
2014-08-23 18:49:14 +02:00
|
|
|
} else {
|
|
|
|
res.encoder = common.EncodeStringToBytes
|
|
|
|
res.decoder = common.DecodeStringFromBytes
|
2014-08-23 18:05:40 +02:00
|
|
|
}
|
2014-08-22 21:54:22 +02:00
|
|
|
|
2014-08-03 14:52:24 +02:00
|
|
|
sqlb = &res
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func closeStmt(stmt **sql.Stmt) error {
|
|
|
|
s := *stmt
|
|
|
|
if s != nil {
|
|
|
|
*stmt = nil
|
|
|
|
return s.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func closeDB(db **sql.DB) error {
|
|
|
|
d := *db
|
|
|
|
if d != nil {
|
|
|
|
*db = nil
|
|
|
|
return d.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sqlb *SqliteBackend) closeAll() error {
|
|
|
|
closeStmt(&sqlb.fetchStmt)
|
|
|
|
closeStmt(&sqlb.insertStmt)
|
|
|
|
closeStmt(&sqlb.updateStmt)
|
|
|
|
closeStmt(&sqlb.existsStmt)
|
2014-08-11 14:56:01 +02:00
|
|
|
closeStmt(&sqlb.countStmt)
|
|
|
|
closeStmt(&sqlb.keysStmt)
|
2014-09-01 12:42:57 +02:00
|
|
|
closeStmt(&sqlb.rangeStmt)
|
2014-08-03 14:52:24 +02:00
|
|
|
return closeDB(&sqlb.db)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sqlb *SqliteBackend) Shutdown() error {
|
|
|
|
globalLock.Lock()
|
|
|
|
defer globalLock.Unlock()
|
|
|
|
|
|
|
|
return sqlb.closeAll()
|
|
|
|
}
|
|
|
|
|
2014-08-06 01:11:41 +02:00
|
|
|
func (ss *SqliteSession) txStmt(stmt *sql.Stmt) *sql.Stmt {
|
|
|
|
if ss.tx != nil {
|
|
|
|
return ss.tx.Stmt(stmt)
|
2014-08-03 14:52:24 +02:00
|
|
|
}
|
|
|
|
return stmt
|
|
|
|
}
|
|
|
|
|
2014-08-06 01:11:41 +02:00
|
|
|
func (ss *SqliteSession) Fetch(hash, key []byte) (data []byte, err error) {
|
2014-08-03 14:52:24 +02:00
|
|
|
var pos int64
|
2014-08-22 21:54:22 +02:00
|
|
|
if pos, err = ss.backend.decoder(key); err != nil {
|
2014-08-03 14:52:24 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
globalLock.RLock()
|
|
|
|
defer globalLock.RUnlock()
|
|
|
|
|
2014-08-06 01:11:41 +02:00
|
|
|
fetchStmt := ss.txStmt(ss.backend.fetchStmt)
|
2014-08-03 14:52:24 +02:00
|
|
|
err2 := fetchStmt.QueryRow(pos).Scan(&data)
|
|
|
|
if err2 == sql.ErrNoRows {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = err2
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-06 01:11:41 +02:00
|
|
|
func (ss *SqliteSession) InTransaction() bool {
|
|
|
|
return ss.tx != nil
|
2014-08-03 14:52:24 +02:00
|
|
|
}
|
|
|
|
|
2014-08-06 01:11:41 +02:00
|
|
|
func (ss *SqliteSession) Store(hash, key, value []byte) (exists bool, err error) {
|
2014-08-03 14:52:24 +02:00
|
|
|
var pos int64
|
2014-08-22 21:54:22 +02:00
|
|
|
if pos, err = ss.backend.decoder(key); err != nil {
|
2014-08-03 14:52:24 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
globalLock.Lock()
|
|
|
|
defer globalLock.Unlock()
|
|
|
|
|
2014-08-06 01:11:41 +02:00
|
|
|
existsStmt := ss.txStmt(ss.backend.existsStmt)
|
2014-08-03 14:52:24 +02:00
|
|
|
var x int
|
|
|
|
err2 := existsStmt.QueryRow(pos).Scan(&x)
|
|
|
|
|
|
|
|
if err2 == sql.ErrNoRows {
|
|
|
|
exists = false
|
|
|
|
} else if err2 != nil {
|
|
|
|
err = err2
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
exists = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists {
|
2014-08-06 01:11:41 +02:00
|
|
|
updateStmt := ss.txStmt(ss.backend.updateStmt)
|
2014-08-03 14:52:24 +02:00
|
|
|
_, err = updateStmt.Exec(value, pos)
|
|
|
|
} else {
|
2014-08-06 01:11:41 +02:00
|
|
|
insertStmt := ss.txStmt(ss.backend.insertStmt)
|
2014-08-03 14:52:24 +02:00
|
|
|
_, err = insertStmt.Exec(pos, value)
|
|
|
|
}
|
2014-09-17 09:51:34 +02:00
|
|
|
// This technically too early because this done in transactions
|
|
|
|
// which are commited (and possible fail) later.
|
|
|
|
if ss.backend.changeTracker != nil {
|
|
|
|
ss.backend.changeTracker.BlockChanged(key)
|
|
|
|
}
|
2014-08-03 14:52:24 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-06 01:11:41 +02:00
|
|
|
func (ss *SqliteSession) BeginTransaction() (err error) {
|
|
|
|
if ss.tx != nil {
|
2014-08-03 14:52:24 +02:00
|
|
|
log.Println("WARN: Already running transaction.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
globalLock.Lock()
|
|
|
|
defer globalLock.Unlock()
|
2014-08-06 01:11:41 +02:00
|
|
|
ss.tx, err = ss.backend.db.Begin()
|
2014-08-03 14:52:24 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-06 01:11:41 +02:00
|
|
|
func (ss *SqliteSession) CommitTransaction() error {
|
2014-08-03 14:52:24 +02:00
|
|
|
|
2014-08-06 01:11:41 +02:00
|
|
|
tx := ss.tx
|
2014-08-03 14:52:24 +02:00
|
|
|
if tx == nil {
|
|
|
|
log.Println("WARN: No transaction running.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
globalLock.Lock()
|
|
|
|
defer globalLock.Unlock()
|
2014-08-06 01:11:41 +02:00
|
|
|
ss.tx = nil
|
2014-08-03 14:52:24 +02:00
|
|
|
return tx.Commit()
|
|
|
|
}
|
2014-08-11 14:56:01 +02:00
|
|
|
|
2014-08-31 19:21:58 +02:00
|
|
|
func (ss *SqliteSession) AllKeys(hash []byte, done chan struct{}) (keys chan []byte, n int, err error) {
|
2014-08-11 15:47:20 +02:00
|
|
|
globalLock.RLock()
|
2014-08-11 14:56:01 +02:00
|
|
|
|
|
|
|
countStmt := ss.txStmt(ss.backend.countStmt)
|
|
|
|
if err = countStmt.QueryRow().Scan(&n); err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
err = nil
|
|
|
|
}
|
2014-08-11 15:47:20 +02:00
|
|
|
globalLock.RUnlock()
|
2014-08-11 14:56:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
keysStmt := ss.txStmt(ss.backend.keysStmt)
|
|
|
|
var rows *sql.Rows
|
|
|
|
if rows, err = keysStmt.Query(); err != nil {
|
2014-08-11 15:47:20 +02:00
|
|
|
globalLock.RUnlock()
|
2014-08-11 14:56:01 +02:00
|
|
|
return
|
|
|
|
}
|
2014-08-11 15:47:20 +02:00
|
|
|
|
2014-08-11 14:56:01 +02:00
|
|
|
keys = make(chan []byte)
|
|
|
|
go func() {
|
2014-08-11 15:47:20 +02:00
|
|
|
defer globalLock.RUnlock()
|
2014-08-11 14:56:01 +02:00
|
|
|
defer rows.Close()
|
|
|
|
defer close(keys)
|
2014-08-18 18:01:34 +02:00
|
|
|
var err error
|
2014-08-11 14:56:01 +02:00
|
|
|
for rows.Next() {
|
2014-08-16 17:41:54 +02:00
|
|
|
var key int64
|
2014-08-11 14:56:01 +02:00
|
|
|
if err := rows.Scan(&key); err != nil {
|
|
|
|
log.Printf("WARN: %s", err)
|
|
|
|
break
|
|
|
|
}
|
2014-08-18 18:01:34 +02:00
|
|
|
var encoded []byte
|
2014-08-22 21:54:22 +02:00
|
|
|
if encoded, err = ss.backend.encoder(key); err != nil {
|
2014-08-18 18:01:34 +02:00
|
|
|
log.Printf("Cannot encode key: %d %s\n", key, err)
|
|
|
|
break
|
|
|
|
}
|
2014-08-31 19:21:58 +02:00
|
|
|
select {
|
|
|
|
case keys <- encoded:
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
2014-08-11 14:56:01 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2014-09-01 00:19:47 +02:00
|
|
|
|
2014-09-01 13:46:23 +02:00
|
|
|
func (ss *SqliteSession) SpatialQuery(hash, first, second []byte, done chan struct{}) (chan Block, error) {
|
2014-09-01 12:42:57 +02:00
|
|
|
|
|
|
|
if ss.backend.interleaved {
|
2014-09-01 13:46:23 +02:00
|
|
|
return ss.interleavedSpatialQuery(first, second, done)
|
2014-09-01 12:42:57 +02:00
|
|
|
}
|
|
|
|
|
2014-09-01 13:46:23 +02:00
|
|
|
return ss.plainSpatialQuery(first, second, done)
|
2014-09-01 12:42:57 +02:00
|
|
|
}
|
|
|
|
|
2014-09-01 13:46:23 +02:00
|
|
|
func (ss *SqliteSession) interleavedSpatialQuery(first, second []byte, done chan struct{}) (blocks chan Block, err error) {
|
2014-09-01 12:42:57 +02:00
|
|
|
var (
|
|
|
|
firstKey int64
|
|
|
|
secondKey int64
|
|
|
|
)
|
|
|
|
if firstKey, err = common.DecodeStringFromBytes(first); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if secondKey, err = common.DecodeStringFromBytes(second); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-09-07 21:36:59 +02:00
|
|
|
c1 := common.ClipCoord(common.PlainToCoord(firstKey))
|
|
|
|
c2 := common.ClipCoord(common.PlainToCoord(secondKey))
|
2014-09-01 12:42:57 +02:00
|
|
|
c1, c2 = common.MinCoord(c1, c2), common.MaxCoord(c1, c2)
|
|
|
|
|
2014-09-01 13:46:23 +02:00
|
|
|
blocks = make(chan Block)
|
|
|
|
|
2014-09-01 12:42:57 +02:00
|
|
|
globalLock.RLock()
|
|
|
|
|
2014-09-01 13:46:23 +02:00
|
|
|
go func() {
|
|
|
|
defer close(blocks)
|
|
|
|
defer globalLock.RUnlock()
|
|
|
|
zmin, zmax := common.CoordToInterleaved(c1), common.CoordToInterleaved(c2)
|
|
|
|
// Should not be necessary.
|
|
|
|
zmin, zmax = order(zmin, zmax)
|
|
|
|
cub := common.Cuboid{P1: c1, P2: c2}
|
|
|
|
rangeStmt := ss.txStmt(ss.backend.rangeStmt)
|
|
|
|
OUTER:
|
|
|
|
for {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
rows *sql.Rows
|
|
|
|
)
|
|
|
|
if rows, err = rangeStmt.Query(zmin, zmax); err != nil {
|
2014-09-01 14:28:12 +02:00
|
|
|
log.Printf("Error in range query: %s", err)
|
2014-09-01 13:46:23 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for rows.Next() {
|
|
|
|
var zcode int64
|
|
|
|
var data []byte
|
|
|
|
if err = rows.Scan(&zcode, &data); err != nil {
|
2014-09-01 14:28:12 +02:00
|
|
|
log.Printf("Error in scanning row: %s", err)
|
|
|
|
rows.Close()
|
|
|
|
return
|
2014-09-01 13:46:23 +02:00
|
|
|
}
|
|
|
|
c := common.InterleavedToCoord(zcode)
|
|
|
|
if cub.Contains(c) {
|
|
|
|
var encodedKey []byte
|
|
|
|
if encodedKey, err = common.EncodeStringToBytes(common.CoordToPlain(c)); err != nil {
|
|
|
|
log.Printf("Key encoding failed: %s", err)
|
2014-09-01 14:28:12 +02:00
|
|
|
rows.Close()
|
|
|
|
return
|
2014-09-01 13:46:23 +02:00
|
|
|
}
|
|
|
|
select {
|
|
|
|
case blocks <- Block{Key: encodedKey, Data: data}:
|
|
|
|
case <-done:
|
|
|
|
rows.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else { // Left the cuboid
|
|
|
|
rows.Close()
|
2014-09-07 10:51:57 +02:00
|
|
|
zmin = common.BigMin(zmin, zmax, zcode)
|
2014-09-01 13:46:23 +02:00
|
|
|
continue OUTER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = rows.Err(); err != nil {
|
|
|
|
log.Printf("Error in range query: %s", err)
|
|
|
|
}
|
|
|
|
rows.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *SqliteSession) plainSpatialQuery(first, second []byte, done chan struct{}) (blocks chan Block, err error) {
|
|
|
|
|
|
|
|
var (
|
|
|
|
firstKey int64
|
|
|
|
secondKey int64
|
|
|
|
)
|
|
|
|
if firstKey, err = common.DecodeStringFromBytes(first); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if secondKey, err = common.DecodeStringFromBytes(second); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c1 := common.PlainToCoord(firstKey)
|
|
|
|
c2 := common.PlainToCoord(secondKey)
|
|
|
|
c1, c2 = common.MinCoord(c1, c2), common.MaxCoord(c1, c2)
|
|
|
|
|
2014-09-01 12:42:57 +02:00
|
|
|
blocks = make(chan Block)
|
|
|
|
|
2014-09-01 13:46:23 +02:00
|
|
|
globalLock.RLock()
|
|
|
|
|
2014-09-01 12:42:57 +02:00
|
|
|
go func() {
|
|
|
|
defer globalLock.RUnlock()
|
|
|
|
defer close(blocks)
|
|
|
|
rangeStmt := ss.txStmt(ss.backend.rangeStmt)
|
|
|
|
|
|
|
|
a, b := common.Coord{X: c1.X}, common.Coord{X: c2.X}
|
|
|
|
|
|
|
|
for a.Z = c1.Z; a.Z <= c2.Z; a.Z++ {
|
|
|
|
b.Z = a.Z
|
|
|
|
for a.Y = c1.Y; a.Y <= c2.Y; a.Y++ {
|
|
|
|
b.Y = a.Y
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
rows *sql.Rows
|
|
|
|
)
|
|
|
|
// Ordering should not be necessary.
|
|
|
|
from, to := order(common.CoordToPlain(a), common.CoordToPlain(b))
|
|
|
|
if rows, err = rangeStmt.Query(from, to); err != nil {
|
2014-09-01 14:28:12 +02:00
|
|
|
log.Printf("Error in range query: %s", err)
|
2014-09-01 12:42:57 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for rows.Next() {
|
|
|
|
var key int64
|
|
|
|
var data []byte
|
|
|
|
if err = rows.Scan(&key, &data); err != nil {
|
2014-09-01 14:28:12 +02:00
|
|
|
log.Printf("Error in scanning row: %s", err)
|
2014-09-01 12:42:57 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
var encodedKey []byte
|
|
|
|
if encodedKey, err = common.EncodeStringToBytes(key); err != nil {
|
|
|
|
log.Printf("Key encoding failed: %s", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case blocks <- Block{Key: encodedKey, Data: data}:
|
|
|
|
case <-done:
|
|
|
|
rows.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = rows.Err(); err != nil {
|
|
|
|
log.Printf("Error in range query: %s", err)
|
|
|
|
}
|
|
|
|
rows.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-09-01 00:19:47 +02:00
|
|
|
return
|
|
|
|
}
|