ledisdb/ledis/ledis.go

184 lines
2.8 KiB
Go
Raw Normal View History

2014-05-20 04:41:24 +04:00
package ledis
import (
"encoding/json"
"fmt"
"github.com/siddontang/go-log/log"
2014-06-19 13:19:40 +04:00
"github.com/siddontang/ledisdb/leveldb"
"path"
2014-05-29 11:07:14 +04:00
"sync"
"time"
2014-05-20 04:41:24 +04:00
)
type Config struct {
DataDir string `json:"data_dir"`
2014-06-06 07:25:13 +04:00
//if you not set leveldb path, use data_dir/data
2014-05-20 04:41:24 +04:00
DataDB leveldb.Config `json:"data_db"`
2014-05-27 12:05:24 +04:00
UseBinLog bool `json:"use_bin_log"`
2014-06-06 07:25:13 +04:00
//if you not set bin log path, use data_dir/bin_log
BinLog BinLogConfig `json:"bin_log"`
2014-05-20 04:41:24 +04:00
}
type DB struct {
2014-06-05 11:59:10 +04:00
l *Ledis
2014-05-20 04:41:24 +04:00
db *leveldb.DB
index uint8
kvTx *tx
listTx *tx
hashTx *tx
zsetTx *tx
2014-06-23 16:09:05 +04:00
binTx *tx
2014-05-20 04:41:24 +04:00
}
type Ledis struct {
2014-05-29 11:07:14 +04:00
sync.Mutex
2014-05-20 04:41:24 +04:00
cfg *Config
ldb *leveldb.DB
dbs [MaxDBNumber]*DB
2014-05-27 12:05:24 +04:00
binlog *BinLog
2014-06-05 11:59:10 +04:00
quit chan struct{}
jobs *sync.WaitGroup
2014-05-20 04:41:24 +04:00
}
func OpenWithJsonConfig(configJson json.RawMessage) (*Ledis, error) {
2014-05-20 04:41:24 +04:00
var cfg Config
if err := json.Unmarshal(configJson, &cfg); err != nil {
return nil, err
}
return Open(&cfg)
2014-05-20 04:41:24 +04:00
}
func Open(cfg *Config) (*Ledis, error) {
if len(cfg.DataDir) == 0 {
return nil, fmt.Errorf("must set correct data_dir")
}
2014-06-06 07:25:13 +04:00
if len(cfg.DataDB.Path) == 0 {
cfg.DataDB.Path = path.Join(cfg.DataDir, "data")
}
ldb, err := leveldb.Open(&cfg.DataDB)
2014-05-20 04:41:24 +04:00
if err != nil {
return nil, err
}
l := new(Ledis)
2014-06-05 11:59:10 +04:00
l.quit = make(chan struct{})
l.jobs = new(sync.WaitGroup)
2014-06-05 11:59:10 +04:00
2014-05-20 04:41:24 +04:00
l.ldb = ldb
if cfg.UseBinLog {
2014-06-06 07:25:13 +04:00
if len(cfg.BinLog.Path) == 0 {
cfg.BinLog.Path = path.Join(cfg.DataDir, "bin_log")
}
l.binlog, err = NewBinLog(&cfg.BinLog)
2014-05-27 12:05:24 +04:00
if err != nil {
return nil, err
}
} else {
l.binlog = nil
}
2014-05-20 04:41:24 +04:00
for i := uint8(0); i < MaxDBNumber; i++ {
l.dbs[i] = newDB(l, i)
}
l.activeExpireCycle()
2014-05-20 04:41:24 +04:00
return l, nil
}
func newDB(l *Ledis, index uint8) *DB {
d := new(DB)
2014-06-05 11:59:10 +04:00
d.l = l
2014-05-20 04:41:24 +04:00
d.db = l.ldb
d.index = index
2014-05-27 12:05:24 +04:00
d.kvTx = newTx(l)
d.listTx = newTx(l)
d.hashTx = newTx(l)
d.zsetTx = newTx(l)
2014-06-23 16:09:05 +04:00
d.binTx = newTx(l)
2014-05-20 04:41:24 +04:00
return d
}
func (l *Ledis) Close() {
2014-06-05 11:59:10 +04:00
close(l.quit)
l.jobs.Wait()
2014-06-05 11:59:10 +04:00
2014-05-20 04:41:24 +04:00
l.ldb.Close()
2014-06-05 11:59:10 +04:00
if l.binlog != nil {
l.binlog.Close()
l.binlog = nil
}
2014-05-20 04:41:24 +04:00
}
func (l *Ledis) Select(index int) (*DB, error) {
if index < 0 || index >= int(MaxDBNumber) {
return nil, fmt.Errorf("invalid db index %d", index)
}
return l.dbs[index], nil
}
2014-06-08 12:43:59 +04:00
func (l *Ledis) FlushAll() error {
for index, db := range l.dbs {
if _, err := db.FlushAll(); err != nil {
log.Error("flush db %d error %s", index, err.Error())
}
}
return nil
}
2014-06-10 06:41:50 +04:00
//very dangerous to use
func (l *Ledis) DataDB() *leveldb.DB {
return l.ldb
}
func (l *Ledis) activeExpireCycle() {
var executors []*elimination = make([]*elimination, len(l.dbs))
for i, db := range l.dbs {
executors[i] = db.newEliminator()
}
l.jobs.Add(1)
go func() {
tick := time.NewTicker(1 * time.Second)
end := false
for !end {
select {
case <-tick.C:
for _, eli := range executors {
eli.active()
}
case <-l.quit:
end = true
break
}
}
tick.Stop()
l.jobs.Done()
}()
}