Added first stubs of SQLite producer for interleaver.

This commit is contained in:
Sascha L. Teichmann 2014-08-19 12:07:57 +02:00
parent 8eca3a6e45
commit c14e7c3dd0
3 changed files with 92 additions and 2 deletions

View File

@ -4,7 +4,12 @@
package main
import "bitbucket.org/s_l_teichmann/mtredisalize/common"
import (
"fmt"
"bitbucket.org/s_l_teichmann/mtredisalize/common"
)
var NoMoreBlocksErr = fmt.Errorf("No more blocks.")
type (
Block struct {
@ -13,8 +18,9 @@ type (
}
BlocKProducer interface {
// Returns next block.
// error is NoMoreBlocksErr if it run out of blocks.
Next() (Block, error)
HasNext() bool
Close() error
}

9
interleaver/main.go Normal file
View File

@ -0,0 +1,9 @@
// 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.
package main
func main() {
// TODO: Implement me!
}

75
interleaver/sqlite.go Normal file
View File

@ -0,0 +1,75 @@
// 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.
package main
import (
"database/sql"
"os"
"bitbucket.org/s_l_teichmann/mtredisalize/common"
_ "github.com/mattn/go-sqlite3"
)
const (
fetchSql = "SELECT pos, data FROM blocks"
)
type SQLiteBlockProducer struct {
db *sql.DB
rows *sql.Rows
splitter common.KeySplitter
}
func NewSQLiteBlockProducer(path string, splitter common.KeySplitter) (sbp *SQLiteBlockProducer, err error) {
// check if we can stat it -> exists.
if _, err = os.Stat(path); err != nil {
return
}
var db *sql.DB
if db, err = sql.Open("sqlite3", path); err != nil {
return
}
var rows *sql.Rows
if rows, err = db.Query(fetchSql); err != nil {
db.Close()
return
}
sbp = &SQLiteBlockProducer{
db: db,
rows: rows,
splitter: splitter}
return
}
func (sbp *SQLiteBlockProducer) Next() (block Block, err error) {
if sbp.rows == nil {
err = NoMoreBlocksErr
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
err = NoMoreBlocksErr
}
return
}
func (sbp *SQLiteBlockProducer) Close() error {
if sbp.rows != nil {
sbp.rows.Close()
}
return sbp.db.Close()
}