support configuring databases

This commit is contained in:
siddontang 2015-03-04 09:15:28 +08:00
parent 18362ffba4
commit c69f803f37
13 changed files with 59 additions and 42 deletions

View File

@ -1,4 +1,4 @@
//This file was generated by .tools/generate_commands.py on Tue Mar 03 2015 08:58:38 +0800
//This file was generated by .tools/generate_commands.py on Wed Mar 04 2015 09:09:49 +0800
package main
var helpCommands = [][]string{
@ -10,6 +10,7 @@ var helpCommands = [][]string{
{"BLPOP", "key [key ...] timeout", "List"},
{"BRPOP", "key [key ...] timeout", "List"},
{"COMMIT", "-", "Transaction"},
{"CONFIG GET", "parameter", "Server"},
{"CONFIG REWRITE", "-", "Server"},
{"DECR", "key", "KV"},
{"DECRBY", "key decrement", "KV"},

View File

@ -102,6 +102,8 @@ type Config struct {
DataDir string `toml:"data_dir"`
Databases uint8 `toml:"databases"`
DBName string `toml:"db_name"`
DBPath string `toml:"db_path"`
DBSyncCommit int `toml:"db_sync_commit"`
@ -165,6 +167,9 @@ func NewConfigDefault() *Config {
cfg.SlaveOf = ""
cfg.Readonly = false
// default databases number
cfg.Databases = 16
// disable access log
cfg.AccessLog = ""
@ -209,7 +214,6 @@ func (cfg *Config) adjust() {
cfg.ConnReadBufferSize = getDefault(4*KB, cfg.ConnReadBufferSize)
cfg.ConnWriteBufferSize = getDefault(4*KB, cfg.ConnWriteBufferSize)
cfg.TTLCheckInterval = getDefault(1, cfg.TTLCheckInterval)
}
func (cfg *LevelDBConfig) adjust() {

View File

@ -9,6 +9,11 @@ http_addr = "127.0.0.1:11181"
# Data store path, all ledisdb's data will be saved here
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
# Log server command, set empty to disable
access_log = ""

View File

@ -566,6 +566,12 @@
"readonly": false
},
"CONFIG GET": {
"arguments" : "parameter",
"group": "Server",
"readonly": true
},
"DUMP": {
"arguments" : "key",
"group": "KV",

View File

@ -9,6 +9,11 @@ http_addr = "127.0.0.1:11181"
# Data store path, all ledisdb's data will be saved here
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
# Log server command, set empty to disable
access_log = ""

View File

@ -104,9 +104,6 @@ var (
)
const (
//we don't support too many databases
MaxDBNumber uint8 = 16
//max key size
MaxKeySize int = 1024

View File

@ -1,26 +0,0 @@
package ledis
import ()
// todo, add info
// type Keyspace struct {
// Kvs int `json:"kvs"`
// KvExpires int `json:"kv_expires"`
// Lists int `json:"lists"`
// ListExpires int `json:"list_expires"`
// Bitmaps int `json:"bitmaps"`
// BitmapExpires int `json:"bitmap_expires"`
// ZSets int `json:"zsets"`
// ZSetExpires int `json:"zset_expires"`
// Hashes int `json:"hashes"`
// HashExpires int `json:"hahsh_expires"`
// }
// type Info struct {
// KeySpaces [MaxDBNumber]Keyspace
// }

View File

@ -18,7 +18,7 @@ type Ledis struct {
cfg *config.Config
ldb *store.DB
dbs [MaxDBNumber]*DB
dbs []*DB
quit chan struct{}
wg sync.WaitGroup
@ -35,7 +35,7 @@ type Ledis struct {
lock io.Closer
tcs [MaxDBNumber]*ttlChecker
tcs []*ttlChecker
}
func Open(cfg *config.Config) (*Ledis, error) {
@ -43,6 +43,10 @@ func Open(cfg *config.Config) (*Ledis, error) {
cfg.DataDir = config.DefaultDataDir
}
if cfg.Databases == 0 {
cfg.Databases = 16
}
os.MkdirAll(cfg.DataDir, 0755)
var err error
@ -78,7 +82,8 @@ func Open(cfg *config.Config) (*Ledis, error) {
l.r = nil
}
for i := uint8(0); i < MaxDBNumber; i++ {
l.dbs = make([]*DB, cfg.Databases)
for i := uint8(0); i < cfg.Databases; i++ {
l.dbs[i] = l.newDB(i)
}
@ -105,7 +110,7 @@ func (l *Ledis) Close() {
}
func (l *Ledis) Select(index int) (*DB, error) {
if index < 0 || index >= int(MaxDBNumber) {
if index < 0 || index >= len(l.dbs) {
return nil, fmt.Errorf("invalid db index %d", index)
}
@ -167,6 +172,7 @@ func (l *Ledis) IsReadOnly() bool {
}
func (l *Ledis) checkTTL() {
l.tcs = make([]*ttlChecker, len(l.dbs))
for i, db := range l.dbs {
c := newTTLChecker(db)

View File

@ -66,7 +66,7 @@ func (m *Multi) Close() error {
}
func (m *Multi) Select(index int) error {
if index < 0 || index >= int(MaxDBNumber) {
if index < 0 || index >= int(m.l.cfg.Databases) {
return fmt.Errorf("invalid db index %d", index)
}

View File

@ -102,7 +102,7 @@ func (tx *Tx) newBatch() *batch {
}
func (tx *Tx) Select(index int) error {
if index < 0 || index >= int(MaxDBNumber) {
if index < 0 || index >= int(tx.l.cfg.Databases) {
return fmt.Errorf("invalid db index %d", index)
}

View File

@ -239,8 +239,8 @@ func xmigratedbCommand(c *client) error {
db, err := ledis.StrUint64(args[4], nil)
if err != nil {
return err
} else if db >= uint64(ledis.MaxDBNumber) {
return fmt.Errorf("invalid db index %d, must < %d", db, ledis.MaxDBNumber)
} else if db >= uint64(c.app.cfg.Databases) {
return fmt.Errorf("invalid db index %d, must < %d", db, c.app.cfg.Databases)
}
timeout, err := ledis.StrInt64(args[5], nil)
@ -328,8 +328,8 @@ func xmigrateCommand(c *client) error {
db, err := ledis.StrUint64(args[4], nil)
if err != nil {
return err
} else if db >= uint64(ledis.MaxDBNumber) {
return fmt.Errorf("invalid db index %d, must < %d", db, ledis.MaxDBNumber)
} else if db >= uint64(c.app.cfg.Databases) {
return fmt.Errorf("invalid db index %d, must < %d", db, c.app.cfg.Databases)
}
timeout, err := ledis.StrInt64(args[5], nil)

View File

@ -111,6 +111,23 @@ func timeCommand(c *client) error {
return nil
}
func configGetCommand(c *client) error {
args := c.args
if len(args) != 2 {
return ErrCmdParams
}
ay := make([][]byte, 0, 2)
key := hack.String(args[1])
switch key {
case "databases":
ay = append(ay, []byte("databases"), num.FormatUint8ToSlice(c.app.cfg.Databases))
}
c.resp.writeSliceArray(ay)
return nil
}
func configCommand(c *client) error {
if len(c.args) < 1 {
return ErrCmdParams
@ -124,6 +141,8 @@ func configCommand(c *client) error {
c.resp.writeStatus(OK)
return nil
}
case "get":
return configGetCommand(c)
default:
return ErrCmdParams
}

View File

@ -45,7 +45,7 @@ func main() {
wb := db.NewWriteBatch()
for i := uint8(0); i < ledis.MaxDBNumber; i++ {
for i := uint8(0); i < cfg.Databases; i++ {
minK, maxK := oldKeyPair(i)
it := db.RangeIterator(minK, maxK, store.RangeROpen)