handle databases config

This commit is contained in:
siddontang 2015-03-16 13:39:15 +08:00
parent 7c88ae4fa4
commit 2ec83580ac
2 changed files with 9 additions and 4 deletions

View File

@ -26,6 +26,7 @@ var readonly = flag.Bool("readonly", false, "set readonly mode, slave server is
var rpl = flag.Bool("rpl", false, "enable replication or not, slave server is always enabled")
var rplSync = flag.Bool("rpl_sync", false, "enable sync replication or not")
var ttlCheck = flag.Int("ttl_check", 0, "TTL check interval")
var databases = flag.Int("databases", 0, "ledisdb maximum database number")
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
@ -59,6 +60,10 @@ func main() {
cfg.DBName = *dbName
}
if *databases > 0 {
cfg.Databases = *databases
}
if len(*slaveof) > 0 {
cfg.SlaveOf = *slaveof
cfg.Readonly = true

View File

@ -48,8 +48,8 @@ func Open(cfg *config.Config) (*Ledis, error) {
if cfg.Databases == 0 {
cfg.Databases = 16
} else if cfg.Databases > 16 {
cfg.Databases = 16
} else if cfg.Databases > MaxDatabases {
cfg.Databases = MaxDatabases
}
os.MkdirAll(cfg.DataDir, 0755)
@ -112,8 +112,8 @@ func (l *Ledis) Close() {
}
func (l *Ledis) Select(index int) (*DB, error) {
if index < 0 || index >= MaxDatabases {
return nil, fmt.Errorf("invalid db index %d, must in [0, %d]", index, MaxDatabases-1)
if index < 0 || index >= l.cfg.Databases {
return nil, fmt.Errorf("invalid db index %d, must in [0, %d]", index, l.cfg.Databases-1)
}
l.dbLock.Lock()