2015-07-26 16:33:29 +02:00
|
|
|
// Copyright 2014, 2015 by Sascha L. Teichmann
|
2014-08-19 12:07:57 +02:00
|
|
|
// Use of this source code is governed by the MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2014-08-20 21:05:36 +02:00
|
|
|
"errors"
|
2014-08-19 12:07:57 +02:00
|
|
|
"os"
|
|
|
|
|
2014-10-03 18:59:14 +02:00
|
|
|
"bitbucket.org/s_l_teichmann/mtsatellite/common"
|
2014-08-19 12:07:57 +02:00
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-08-20 21:05:36 +02:00
|
|
|
createSql = "CREATE TABLE blocks (pos INT NOT NULL PRIMARY KEY, data BLOB)"
|
|
|
|
insertSql = "INSERT INTO blocks (pos, data) VALUES (?, ?)"
|
|
|
|
deleteSql = "DELETE FROM blocks"
|
|
|
|
selectSql = "SELECT pos, data FROM blocks"
|
2014-08-19 12:07:57 +02:00
|
|
|
)
|
|
|
|
|
2014-08-21 14:46:34 +02:00
|
|
|
var ErrDatabaseNotExists = errors.New("Database does not exists.")
|
2014-08-20 21:05:36 +02:00
|
|
|
|
2014-08-24 23:47:43 +02:00
|
|
|
const blocksPerTx = 128
|
|
|
|
|
2014-08-20 21:05:36 +02:00
|
|
|
type (
|
|
|
|
SQLiteBlockProducer struct {
|
|
|
|
db *sql.DB
|
|
|
|
rows *sql.Rows
|
|
|
|
splitter common.KeySplitter
|
|
|
|
}
|
|
|
|
|
|
|
|
SQLiteBlockConsumer struct {
|
|
|
|
db *sql.DB
|
|
|
|
insertStmt *sql.Stmt
|
2014-08-24 23:47:43 +02:00
|
|
|
tx *sql.Tx
|
|
|
|
txCounter int
|
2014-08-20 21:05:36 +02:00
|
|
|
joiner common.KeyJoiner
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func fileExists(path string) bool {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
return !os.IsNotExist(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSQLiteBlockConsumer(
|
|
|
|
path string,
|
|
|
|
joiner common.KeyJoiner) (sbc *SQLiteBlockConsumer, err error) {
|
|
|
|
|
|
|
|
createNew := !fileExists(path)
|
|
|
|
|
|
|
|
var db *sql.DB
|
|
|
|
if db, err = sql.Open("sqlite3", path); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if createNew {
|
|
|
|
if _, err = db.Exec(createSql); err != nil {
|
|
|
|
db.Close()
|
|
|
|
return
|
|
|
|
}
|
2014-08-24 23:47:43 +02:00
|
|
|
} else {
|
|
|
|
if _, err = db.Exec(deleteSql); err != nil {
|
|
|
|
db.Close()
|
|
|
|
return
|
|
|
|
}
|
2014-08-20 21:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var insertStmt *sql.Stmt
|
|
|
|
if insertStmt, err = db.Prepare(insertSql); err != nil {
|
|
|
|
db.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-24 23:47:43 +02:00
|
|
|
var tx *sql.Tx
|
|
|
|
if tx, err = db.Begin(); err != nil {
|
|
|
|
insertStmt.Close()
|
|
|
|
db.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-20 21:05:36 +02:00
|
|
|
sbc = &SQLiteBlockConsumer{
|
|
|
|
db: db,
|
|
|
|
insertStmt: insertStmt,
|
2014-08-24 23:47:43 +02:00
|
|
|
tx: tx,
|
2014-08-20 21:05:36 +02:00
|
|
|
joiner: joiner}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sbc *SQLiteBlockConsumer) Close() error {
|
2014-08-24 23:47:43 +02:00
|
|
|
sbc.tx.Commit()
|
2014-08-20 21:05:36 +02:00
|
|
|
sbc.insertStmt.Close()
|
|
|
|
return sbc.db.Close()
|
|
|
|
}
|
|
|
|
|
2014-08-24 23:47:43 +02:00
|
|
|
func (sbc *SQLiteBlockConsumer) getTx() (tx *sql.Tx, err error) {
|
|
|
|
if sbc.txCounter >= blocksPerTx {
|
|
|
|
sbc.txCounter = 0
|
|
|
|
if err = sbc.tx.Commit(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if sbc.tx, err = sbc.db.Begin(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sbc.txCounter++
|
|
|
|
tx = sbc.tx
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-07 15:57:25 +02:00
|
|
|
func (sbc *SQLiteBlockConsumer) Consume(block *common.Block) (err error) {
|
2014-08-24 23:47:43 +02:00
|
|
|
var tx *sql.Tx
|
|
|
|
if tx, err = sbc.getTx(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = tx.Stmt(sbc.insertStmt).Exec(sbc.joiner(block.Coord), block.Data)
|
2014-08-20 21:05:36 +02:00
|
|
|
return
|
2014-08-19 12:07:57 +02:00
|
|
|
}
|
|
|
|
|
2014-08-20 21:05:36 +02:00
|
|
|
func NewSQLiteBlockProducer(
|
|
|
|
path string,
|
|
|
|
splitter common.KeySplitter) (sbp *SQLiteBlockProducer, err error) {
|
2014-08-19 12:07:57 +02:00
|
|
|
|
2014-08-20 21:05:36 +02:00
|
|
|
if !fileExists(path) {
|
2014-08-21 14:46:34 +02:00
|
|
|
err = ErrDatabaseNotExists
|
2014-08-19 12:07:57 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var db *sql.DB
|
|
|
|
if db, err = sql.Open("sqlite3", path); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var rows *sql.Rows
|
2014-08-20 21:05:36 +02:00
|
|
|
if rows, err = db.Query(selectSql); err != nil {
|
2014-08-19 12:07:57 +02:00
|
|
|
db.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sbp = &SQLiteBlockProducer{
|
|
|
|
db: db,
|
|
|
|
rows: rows,
|
|
|
|
splitter: splitter}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-07 15:57:25 +02:00
|
|
|
func (sbp *SQLiteBlockProducer) Next(block *common.Block) (err error) {
|
2014-08-19 12:07:57 +02:00
|
|
|
if sbp.rows == nil {
|
2014-09-07 16:12:18 +02:00
|
|
|
err = common.ErrNoMoreBlocks
|
2014-08-19 12:07:57 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if sbp.rows.Next() {
|
|
|
|
var key int64
|
|
|
|
if err = sbp.rows.Scan(&key, &block.Data); err == nil {
|
|
|
|
block.Coord = sbp.splitter(key)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sbp.rows.Close()
|
|
|
|
sbp.rows = nil
|
2014-09-07 16:12:18 +02:00
|
|
|
err = common.ErrNoMoreBlocks
|
2014-08-19 12:07:57 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sbp *SQLiteBlockProducer) Close() error {
|
|
|
|
if sbp.rows != nil {
|
|
|
|
sbp.rows.Close()
|
|
|
|
}
|
|
|
|
return sbp.db.Close()
|
|
|
|
}
|