diff --git a/config/config.go b/config/config.go index 7c1346b..61d4519 100644 --- a/config/config.go +++ b/config/config.go @@ -102,7 +102,7 @@ type Config struct { DataDir string `toml:"data_dir"` - Databases uint8 `toml:"databases"` + Databases int `toml:"databases"` DBName string `toml:"db_name"` DBPath string `toml:"db_path"` @@ -214,6 +214,10 @@ func (cfg *Config) adjust() { cfg.ConnReadBufferSize = getDefault(4*KB, cfg.ConnReadBufferSize) cfg.ConnWriteBufferSize = getDefault(4*KB, cfg.ConnWriteBufferSize) cfg.TTLCheckInterval = getDefault(1, cfg.TTLCheckInterval) + cfg.Databases = getDefault(0, cfg.Databases) + if cfg.Databases > 256 { + cfg.Databases = 256 + } } func (cfg *LevelDBConfig) adjust() { diff --git a/etc/ledis.conf b/etc/ledis.conf index bcc3820..9c34182 100644 --- a/etc/ledis.conf +++ b/etc/ledis.conf @@ -12,7 +12,7 @@ data_dir = "/tmp/ledis_server" # Set the number of databases. You can use `select dbindex` to choose a db. # dbindex must be in [0, databases - 1]. # Maximum databases is 256. -databases = 16 +databases = 256 # Log server command, set empty to disable access_log = "" diff --git a/ledis/ledis.go b/ledis/ledis.go index bda8f5a..c28c29b 100644 --- a/ledis/ledis.go +++ b/ledis/ledis.go @@ -45,6 +45,8 @@ func Open(cfg *config.Config) (*Ledis, error) { if cfg.Databases == 0 { cfg.Databases = 16 + } else if cfg.Databases > 256 { + cfg.Databases = 256 } os.MkdirAll(cfg.DataDir, 0755) @@ -83,8 +85,8 @@ func Open(cfg *config.Config) (*Ledis, error) { } l.dbs = make([]*DB, cfg.Databases) - for i := uint8(0); i < cfg.Databases; i++ { - l.dbs[i] = l.newDB(i) + for i := 0; i < cfg.Databases; i++ { + l.dbs[i] = l.newDB(uint8(i)) } l.checkTTL() @@ -111,7 +113,7 @@ func (l *Ledis) Close() { func (l *Ledis) Select(index int) (*DB, error) { if index < 0 || index >= len(l.dbs) { - return nil, fmt.Errorf("invalid db index %d", index) + return nil, fmt.Errorf("invalid db index %d, must in [0, %d]", index, len(l.dbs)-1) } return l.dbs[index], nil diff --git a/server/cmd_server.go b/server/cmd_server.go index b402fca..83f705c 100644 --- a/server/cmd_server.go +++ b/server/cmd_server.go @@ -121,7 +121,7 @@ func configGetCommand(c *client) error { key := hack.String(args[1]) switch key { case "databases": - ay = append(ay, []byte("databases"), num.FormatUint8ToSlice(c.app.cfg.Databases)) + ay = append(ay, []byte("databases"), num.FormatIntToSlice(c.app.cfg.Databases)) } c.resp.writeSliceArray(ay) diff --git a/upgrade/ledis-upgrade-ttl/main.go b/upgrade/ledis-upgrade-ttl/main.go index d7ddbed..b450b14 100644 --- a/upgrade/ledis-upgrade-ttl/main.go +++ b/upgrade/ledis-upgrade-ttl/main.go @@ -45,18 +45,18 @@ func main() { wb := db.NewWriteBatch() - for i := uint8(0); i < cfg.Databases; i++ { - minK, maxK := oldKeyPair(i) + for i := 0; i < cfg.Databases; i++ { + minK, maxK := oldKeyPair(uint8(i)) it := db.RangeIterator(minK, maxK, store.RangeROpen) num := 0 for ; it.Valid(); it.Next() { - dt, k, t, err := decodeOldKey(i, it.RawKey()) + dt, k, t, err := decodeOldKey(uint8(i), it.RawKey()) if err != nil { continue } - newKey := encodeNewKey(i, dt, k, t) + newKey := encodeNewKey(uint8(i), dt, k, t) wb.Put(newKey, it.RawValue()) wb.Delete(it.RawKey())