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 (
|
2016-04-24 12:41:58 +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
|
|
|
)
|
|
|
|
|
2016-04-24 18:37:08 +02:00
|
|
|
// ErrDatabaseNotExists indicates that the database does not exist.
|
2014-08-21 14:46:34 +02:00
|
|
|
var ErrDatabaseNotExists = errors.New("Database does not exists.")
|
2014-08-20 21:05:36 +02:00
|
|
|
|
2016-04-24 18:37:08 +02:00
|
|
|
const blocksPerTx = 128 // Number of blocks copied in a transaction.
|
2014-08-24 23:47:43 +02:00
|
|
|
|
2014-08-20 21:05:36 +02:00
|
|
|
type (
|
2016-04-24 18:37:08 +02:00
|
|
|
// SQLiteBlockProducer helps getting blocks from a SQLite database.
|
2014-08-20 21:05:36 +02:00
|
|
|
SQLiteBlockProducer struct {
|
|
|
|
db *sql.DB
|
|
|
|
rows *sql.Rows
|
|
|
|
splitter common.KeySplitter
|
|
|
|
}
|
|
|
|
|
2016-04-24 18:37:08 +02:00
|
|
|
// SQLiteBlockConsumer helps storing blocks into a SQLite database.
|
2014-08-20 21:05:36 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-04-24 18:37:08 +02:00
|
|
|
// NewSQLiteBlockConsumer returns a storage helper for SQLite databases.
|
2014-08-20 21:05:36 +02:00
|
|
|
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 {
|
2016-04-24 12:41:58 +02:00
|
|
|
if _, err = db.Exec(createSQL); err != nil {
|
2014-08-20 21:05:36 +02:00
|
|
|
db.Close()
|
|
|
|
return
|
|
|
|
}
|
2014-08-24 23:47:43 +02:00
|
|
|
} else {
|
2016-04-24 12:41:58 +02:00
|
|
|
if _, err = db.Exec(deleteSQL); err != nil {
|
2014-08-24 23:47:43 +02:00
|
|
|
db.Close()
|
|
|
|
return
|
|
|
|
}
|
2014-08-20 21:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var insertStmt *sql.Stmt
|
2016-04-24 12:41:58 +02:00
|
|
|
if insertStmt, err = db.Prepare(insertSQL); err != nil {
|
2014-08-20 21:05:36 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-04-24 18:37:08 +02:00
|
|
|
// Close closes a SQLite storage helper.
|
2014-08-20 21:05:36 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-04-24 18:37:08 +02:00
|
|
|
// Consume stores a block in an SQLite database.
|
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
|
|
|
}
|
|
|
|
|
2016-04-24 18:37:08 +02:00
|
|
|
// NewSQLiteBlockProducer returns a new producer to fetch blocks from a
|
|
|
|
// SQLite database.
|
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
|
2016-04-24 12:41:58 +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
|
|
|
|
}
|
|
|
|
|
2016-04-24 18:37:08 +02:00
|
|
|
// Next fetches the next block from a SQLite database.
|
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
|
|
|
|
}
|
|
|
|
|
2016-04-24 18:37:08 +02:00
|
|
|
// Close closes a block producer from a SQLite database.
|
2014-08-19 12:07:57 +02:00
|
|
|
func (sbp *SQLiteBlockProducer) Close() error {
|
|
|
|
if sbp.rows != nil {
|
|
|
|
sbp.rows.Close()
|
|
|
|
}
|
|
|
|
return sbp.db.Close()
|
|
|
|
}
|