Enabaled interleaving in SQLite backend.

This commit is contained in:
Sascha L. Teichmann 2014-08-23 18:05:40 +02:00
parent 8cd23d257f
commit 5094f05414
2 changed files with 28 additions and 26 deletions

19
main.go
View File

@ -25,12 +25,13 @@ const (
func main() { func main() {
var ( var (
port int port int
host string host string
driver string driver string
cacheSize int cacheSize int
version bool version bool
gcDuration string interleaved bool
gcDuration string
) )
flag.IntVar(&port, "port", 6379, "port to bind") flag.IntVar(&port, "port", 6379, "port to bind")
@ -38,6 +39,7 @@ func main() {
flag.StringVar(&host, "host", "", "host to bind") flag.StringVar(&host, "host", "", "host to bind")
flag.IntVar(&cacheSize, "cache", 32, "cache size in MB") flag.IntVar(&cacheSize, "cache", 32, "cache size in MB")
flag.BoolVar(&version, "version", false, "Print version and exit.") 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.StringVar(&gcDuration, "gc-duration", GCDuration, "Duration between forced GCs.")
flag.Parse() flag.Parse()
@ -63,10 +65,7 @@ func main() {
} }
if driver == "sqlite" { if driver == "sqlite" {
if backend, err = NewSqliteBackend( if backend, err = NewSqliteBackend(args[0], interleaved); err != nil {
args[0],
common.EncodeStringToBytes,
common.DecodeStringFromBytes); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} else { } else {

View File

@ -26,15 +26,16 @@ const (
) )
type SqliteBackend struct { type SqliteBackend struct {
db *sql.DB db *sql.DB
encoder common.KeyEncoder encoder common.KeyEncoder
decoder common.KeyDecoder decoder common.KeyDecoder
existsStmt *sql.Stmt interleaved bool
fetchStmt *sql.Stmt existsStmt *sql.Stmt
insertStmt *sql.Stmt fetchStmt *sql.Stmt
updateStmt *sql.Stmt insertStmt *sql.Stmt
countStmt *sql.Stmt updateStmt *sql.Stmt
keysStmt *sql.Stmt countStmt *sql.Stmt
keysStmt *sql.Stmt
} }
type SqliteSession struct { type SqliteSession struct {
@ -55,12 +56,9 @@ func (ss *SqliteSession) Close() error {
return nil return nil
} }
func NewSqliteBackend( func NewSqliteBackend(path string, interleaved bool) (sqlb *SqliteBackend, err error) {
path string,
encoder common.KeyEncoder,
decoder common.KeyDecoder) (sqlb *SqliteBackend, err error) {
res := SqliteBackend{} res := SqliteBackend{interleaved: interleaved}
if res.db, err = sql.Open("sqlite3", path); err != nil { if res.db, err = sql.Open("sqlite3", path); err != nil {
return return
@ -96,8 +94,13 @@ func NewSqliteBackend(
return return
} }
res.encoder = encoder if interleaved {
res.decoder = decoder res.encoder = common.EncodeStringToBytes
res.decoder = common.DecodeStringFromBytes
} else {
res.encoder = common.EncodeStringToBytesFromInterleaved
res.decoder = common.DecodeStringFromBytesToInterleaved
}
sqlb = &res sqlb = &res
return return