Made key encoder/decoder variable in SQLite backend. TODO: Do the same in LevelDB backend.

This commit is contained in:
Sascha L. Teichmann 2014-08-22 21:54:22 +02:00
parent 83c160eb90
commit 24b9236e41
2 changed files with 18 additions and 5 deletions

View File

@ -13,6 +13,8 @@ import (
"os/signal" "os/signal"
"runtime" "runtime"
"time" "time"
"bitbucket.org/s_l_teichmann/mtredisalize/common"
) )
const ( const (
@ -61,7 +63,10 @@ func main() {
} }
if driver == "sqlite" { if driver == "sqlite" {
if backend, err = NewSqliteBackend(args[0]); err != nil { if backend, err = NewSqliteBackend(
args[0],
common.EncodeStringToBytes,
common.DecodeStringFromBytes); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} else { } else {

View File

@ -27,6 +27,8 @@ const (
type SqliteBackend struct { type SqliteBackend struct {
db *sql.DB db *sql.DB
encoder common.KeyEncoder
decoder common.KeyDecoder
existsStmt *sql.Stmt existsStmt *sql.Stmt
fetchStmt *sql.Stmt fetchStmt *sql.Stmt
insertStmt *sql.Stmt insertStmt *sql.Stmt
@ -53,7 +55,10 @@ func (ss *SqliteSession) Close() error {
return nil return nil
} }
func NewSqliteBackend(path string) (sqlb *SqliteBackend, err error) { func NewSqliteBackend(
path string,
encoder common.KeyEncoder,
decoder common.KeyDecoder) (sqlb *SqliteBackend, err error) {
res := SqliteBackend{} res := SqliteBackend{}
@ -91,6 +96,9 @@ func NewSqliteBackend(path string) (sqlb *SqliteBackend, err error) {
return return
} }
res.encoder = encoder
res.decoder = decoder
sqlb = &res sqlb = &res
return return
} }
@ -139,7 +147,7 @@ func (ss *SqliteSession) txStmt(stmt *sql.Stmt) *sql.Stmt {
func (ss *SqliteSession) Fetch(hash, key []byte) (data []byte, err error) { func (ss *SqliteSession) Fetch(hash, key []byte) (data []byte, err error) {
var pos int64 var pos int64
if pos, err = common.DecodeStringFromBytes(key); err != nil { if pos, err = ss.backend.decoder(key); err != nil {
return return
} }
@ -161,7 +169,7 @@ func (ss *SqliteSession) InTransaction() bool {
func (ss *SqliteSession) Store(hash, key, value []byte) (exists bool, err error) { func (ss *SqliteSession) Store(hash, key, value []byte) (exists bool, err error) {
var pos int64 var pos int64
if pos, err = common.DecodeStringFromBytes(key); err != nil { if pos, err = ss.backend.decoder(key); err != nil {
return return
} }
@ -249,7 +257,7 @@ func (ss *SqliteSession) AllKeys(hash []byte) (keys chan []byte, n int, err erro
break break
} }
var encoded []byte var encoded []byte
if encoded, err = common.EncodeStringToBytes(key); err != nil { if encoded, err = ss.backend.encoder(key); err != nil {
log.Printf("Cannot encode key: %d %s\n", key, err) log.Printf("Cannot encode key: %d %s\n", key, err)
break break
} }