config use bytes for all size

This commit is contained in:
siddontang 2014-04-30 08:56:51 +08:00
parent eb13bf9e9b
commit 2f02d5d6e2
2 changed files with 19 additions and 16 deletions

View File

@ -33,13 +33,7 @@ type DB struct {
filter *levigo.FilterPolicy filter *levigo.FilterPolicy
} }
func Open(configJson json.RawMessage) (*DB, error) { func OpenWithConfig(cfg *Config) (*DB, error) {
cfg := new(Config)
err := json.Unmarshal(configJson, cfg)
if err != nil {
return nil, err
}
db := new(DB) db := new(DB)
db.cfg = cfg db.cfg = cfg
@ -50,10 +44,21 @@ func Open(configJson json.RawMessage) (*DB, error) {
db.iteratorOpts = levigo.NewReadOptions() db.iteratorOpts = levigo.NewReadOptions()
db.iteratorOpts.SetFillCache(false) db.iteratorOpts.SetFillCache(false)
var err error
db.db, err = levigo.Open(cfg.Path, db.opts) db.db, err = levigo.Open(cfg.Path, db.opts)
return db, err return db, err
} }
func Open(configJson json.RawMessage) (*DB, error) {
cfg := new(Config)
err := json.Unmarshal(configJson, cfg)
if err != nil {
return nil, err
}
return OpenWithConfig(cfg)
}
func (db *DB) initOptions(cfg *Config) *levigo.Options { func (db *DB) initOptions(cfg *Config) *levigo.Options {
opts := levigo.NewOptions() opts := levigo.NewOptions()
@ -72,14 +77,12 @@ func (db *DB) initOptions(cfg *Config) *levigo.Options {
opts.SetCompression(levigo.NoCompression) opts.SetCompression(levigo.NoCompression)
} }
blockSize := cfg.BlockSize * 1024 if cfg.BlockSize > 0 {
if blockSize > 0 { opts.SetBlockSize(cfg.BlockSize)
opts.SetBlockSize(blockSize)
} }
writeBufferSize := cfg.WriteBufferSize * 1024 * 1024 if cfg.WriteBufferSize > 0 {
if writeBufferSize > 0 { opts.SetWriteBufferSize(cfg.WriteBufferSize)
opts.SetWriteBufferSize(writeBufferSize)
} }
return opts return opts

View File

@ -12,9 +12,9 @@ var testConfigJson = []byte(`
{ {
"path" : "./testdb", "path" : "./testdb",
"compression":true, "compression":true,
"block_size" : 32, "block_size" : 32768,
"write_buffer_size" : 2, "write_buffer_size" : 2097152,
"cache_size" : 20 "cache_size" : 20971520
} }
`) `)