mirror of
https://bitbucket.org/s_l_teichmann/mtsatellite
synced 2025-01-11 17:30:18 +01:00
Added first stubs of SQLite producer for interleaver.
This commit is contained in:
parent
8eca3a6e45
commit
c14e7c3dd0
@ -4,7 +4,12 @@
|
|||||||
|
|
||||||
package main
|
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 (
|
type (
|
||||||
Block struct {
|
Block struct {
|
||||||
@ -13,8 +18,9 @@ type (
|
|||||||
}
|
}
|
||||||
|
|
||||||
BlocKProducer interface {
|
BlocKProducer interface {
|
||||||
|
// Returns next block.
|
||||||
|
// error is NoMoreBlocksErr if it run out of blocks.
|
||||||
Next() (Block, error)
|
Next() (Block, error)
|
||||||
HasNext() bool
|
|
||||||
Close() error
|
Close() error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
9
interleaver/main.go
Normal file
9
interleaver/main.go
Normal 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
75
interleaver/sqlite.go
Normal 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()
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user