From 24b9236e41fc5528c4b3281b15c584700fe0a7f7 Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Fri, 22 Aug 2014 21:54:22 +0200 Subject: [PATCH] Made key encoder/decoder variable in SQLite backend. TODO: Do the same in LevelDB backend. --- main.go | 7 ++++++- sqlite.go | 16 ++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index f27f4e3..bdf38df 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,8 @@ import ( "os/signal" "runtime" "time" + + "bitbucket.org/s_l_teichmann/mtredisalize/common" ) const ( @@ -61,7 +63,10 @@ func main() { } 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) } } else { diff --git a/sqlite.go b/sqlite.go index 4d9a0ea..43f74ac 100644 --- a/sqlite.go +++ b/sqlite.go @@ -27,6 +27,8 @@ const ( type SqliteBackend struct { db *sql.DB + encoder common.KeyEncoder + decoder common.KeyDecoder existsStmt *sql.Stmt fetchStmt *sql.Stmt insertStmt *sql.Stmt @@ -53,7 +55,10 @@ func (ss *SqliteSession) Close() error { 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{} @@ -91,6 +96,9 @@ func NewSqliteBackend(path string) (sqlb *SqliteBackend, err error) { return } + res.encoder = encoder + res.decoder = decoder + sqlb = &res 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) { var pos int64 - if pos, err = common.DecodeStringFromBytes(key); err != nil { + if pos, err = ss.backend.decoder(key); err != nil { return } @@ -161,7 +169,7 @@ func (ss *SqliteSession) InTransaction() bool { func (ss *SqliteSession) Store(hash, key, value []byte) (exists bool, err error) { var pos int64 - if pos, err = common.DecodeStringFromBytes(key); err != nil { + if pos, err = ss.backend.decoder(key); err != nil { return } @@ -249,7 +257,7 @@ func (ss *SqliteSession) AllKeys(hash []byte) (keys chan []byte, n int, err erro break } 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) break }