Merge branch 'develop'

This commit is contained in:
siddontang 2015-03-04 17:47:22 +08:00
commit b9d10ace23
5 changed files with 16 additions and 10 deletions

View File

@ -102,7 +102,7 @@ type Config struct {
DataDir string `toml:"data_dir"` DataDir string `toml:"data_dir"`
Databases uint8 `toml:"databases"` Databases int `toml:"databases"`
DBName string `toml:"db_name"` DBName string `toml:"db_name"`
DBPath string `toml:"db_path"` DBPath string `toml:"db_path"`
@ -214,6 +214,10 @@ func (cfg *Config) adjust() {
cfg.ConnReadBufferSize = getDefault(4*KB, cfg.ConnReadBufferSize) cfg.ConnReadBufferSize = getDefault(4*KB, cfg.ConnReadBufferSize)
cfg.ConnWriteBufferSize = getDefault(4*KB, cfg.ConnWriteBufferSize) cfg.ConnWriteBufferSize = getDefault(4*KB, cfg.ConnWriteBufferSize)
cfg.TTLCheckInterval = getDefault(1, cfg.TTLCheckInterval) cfg.TTLCheckInterval = getDefault(1, cfg.TTLCheckInterval)
cfg.Databases = getDefault(0, cfg.Databases)
if cfg.Databases > 256 {
cfg.Databases = 256
}
} }
func (cfg *LevelDBConfig) adjust() { func (cfg *LevelDBConfig) adjust() {

View File

@ -12,7 +12,7 @@ data_dir = "/tmp/ledis_server"
# Set the number of databases. You can use `select dbindex` to choose a db. # Set the number of databases. You can use `select dbindex` to choose a db.
# dbindex must be in [0, databases - 1]. # dbindex must be in [0, databases - 1].
# Maximum databases is 256. # Maximum databases is 256.
databases = 16 databases = 256
# Log server command, set empty to disable # Log server command, set empty to disable
access_log = "" access_log = ""

View File

@ -45,6 +45,8 @@ func Open(cfg *config.Config) (*Ledis, error) {
if cfg.Databases == 0 { if cfg.Databases == 0 {
cfg.Databases = 16 cfg.Databases = 16
} else if cfg.Databases > 256 {
cfg.Databases = 256
} }
os.MkdirAll(cfg.DataDir, 0755) os.MkdirAll(cfg.DataDir, 0755)
@ -83,8 +85,8 @@ func Open(cfg *config.Config) (*Ledis, error) {
} }
l.dbs = make([]*DB, cfg.Databases) l.dbs = make([]*DB, cfg.Databases)
for i := uint8(0); i < cfg.Databases; i++ { for i := 0; i < cfg.Databases; i++ {
l.dbs[i] = l.newDB(i) l.dbs[i] = l.newDB(uint8(i))
} }
l.checkTTL() l.checkTTL()
@ -111,7 +113,7 @@ func (l *Ledis) Close() {
func (l *Ledis) Select(index int) (*DB, error) { func (l *Ledis) Select(index int) (*DB, error) {
if index < 0 || index >= len(l.dbs) { 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 return l.dbs[index], nil

View File

@ -121,7 +121,7 @@ func configGetCommand(c *client) error {
key := hack.String(args[1]) key := hack.String(args[1])
switch key { switch key {
case "databases": 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) c.resp.writeSliceArray(ay)

View File

@ -45,18 +45,18 @@ func main() {
wb := db.NewWriteBatch() wb := db.NewWriteBatch()
for i := uint8(0); i < cfg.Databases; i++ { for i := 0; i < cfg.Databases; i++ {
minK, maxK := oldKeyPair(i) minK, maxK := oldKeyPair(uint8(i))
it := db.RangeIterator(minK, maxK, store.RangeROpen) it := db.RangeIterator(minK, maxK, store.RangeROpen)
num := 0 num := 0
for ; it.Valid(); it.Next() { 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 { if err != nil {
continue continue
} }
newKey := encodeNewKey(i, dt, k, t) newKey := encodeNewKey(uint8(i), dt, k, t)
wb.Put(newKey, it.RawValue()) wb.Put(newKey, it.RawValue())
wb.Delete(it.RawKey()) wb.Delete(it.RawKey())