diff --git a/main.go b/main.go index 17ffa9b..6787803 100644 --- a/main.go +++ b/main.go @@ -25,12 +25,13 @@ const ( func main() { var ( - port int - host string - driver string - cacheSize int - version bool - gcDuration string + port int + host string + driver string + cacheSize int + version bool + interleaved bool + gcDuration string ) flag.IntVar(&port, "port", 6379, "port to bind") @@ -38,6 +39,7 @@ func main() { flag.StringVar(&host, "host", "", "host to bind") flag.IntVar(&cacheSize, "cache", 32, "cache size in MB") flag.BoolVar(&version, "version", false, "Print version and exit.") + flag.BoolVar(&interleaved, "interleaved", false, "Backend stores key in interleaved form.") flag.StringVar(&gcDuration, "gc-duration", GCDuration, "Duration between forced GCs.") flag.Parse() @@ -63,10 +65,7 @@ func main() { } if driver == "sqlite" { - if backend, err = NewSqliteBackend( - args[0], - common.EncodeStringToBytes, - common.DecodeStringFromBytes); err != nil { + if backend, err = NewSqliteBackend(args[0], interleaved); err != nil { log.Fatal(err) } } else { diff --git a/sqlite.go b/sqlite.go index 43f74ac..aab5d35 100644 --- a/sqlite.go +++ b/sqlite.go @@ -26,15 +26,16 @@ const ( ) type SqliteBackend struct { - db *sql.DB - encoder common.KeyEncoder - decoder common.KeyDecoder - existsStmt *sql.Stmt - fetchStmt *sql.Stmt - insertStmt *sql.Stmt - updateStmt *sql.Stmt - countStmt *sql.Stmt - keysStmt *sql.Stmt + db *sql.DB + encoder common.KeyEncoder + decoder common.KeyDecoder + interleaved bool + existsStmt *sql.Stmt + fetchStmt *sql.Stmt + insertStmt *sql.Stmt + updateStmt *sql.Stmt + countStmt *sql.Stmt + keysStmt *sql.Stmt } type SqliteSession struct { @@ -55,12 +56,9 @@ func (ss *SqliteSession) Close() error { return nil } -func NewSqliteBackend( - path string, - encoder common.KeyEncoder, - decoder common.KeyDecoder) (sqlb *SqliteBackend, err error) { +func NewSqliteBackend(path string, interleaved bool) (sqlb *SqliteBackend, err error) { - res := SqliteBackend{} + res := SqliteBackend{interleaved: interleaved} if res.db, err = sql.Open("sqlite3", path); err != nil { return @@ -96,8 +94,13 @@ func NewSqliteBackend( return } - res.encoder = encoder - res.decoder = decoder + if interleaved { + res.encoder = common.EncodeStringToBytes + res.decoder = common.DecodeStringFromBytes + } else { + res.encoder = common.EncodeStringToBytesFromInterleaved + res.decoder = common.DecodeStringFromBytesToInterleaved + } sqlb = &res return