2014-05-20 04:41:24 +04:00
|
|
|
package ledis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-07-04 09:27:57 +04:00
|
|
|
"github.com/siddontang/go-log/log"
|
2014-08-07 12:49:48 +04:00
|
|
|
"github.com/siddontang/ledisdb/config"
|
2014-07-25 13:58:00 +04:00
|
|
|
"github.com/siddontang/ledisdb/store"
|
2014-05-29 11:07:14 +04:00
|
|
|
"sync"
|
2014-06-12 06:51:36 +04:00
|
|
|
"time"
|
2014-05-20 04:41:24 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Ledis struct {
|
2014-08-07 12:49:48 +04:00
|
|
|
cfg *config.Config
|
2014-05-20 04:41:24 +04:00
|
|
|
|
2014-07-25 13:58:00 +04:00
|
|
|
ldb *store.DB
|
2014-05-20 04:41:24 +04:00
|
|
|
dbs [MaxDBNumber]*DB
|
2014-05-27 12:05:24 +04:00
|
|
|
|
2014-06-05 11:59:10 +04:00
|
|
|
quit chan struct{}
|
2014-06-18 11:47:26 +04:00
|
|
|
jobs *sync.WaitGroup
|
2014-08-25 10:18:23 +04:00
|
|
|
|
|
|
|
binlog *BinLog
|
2014-08-30 13:39:44 +04:00
|
|
|
|
|
|
|
wLock sync.RWMutex //allow one write at same time
|
|
|
|
commitLock sync.Mutex //allow one write commit at same time
|
2014-05-20 04:41:24 +04:00
|
|
|
}
|
|
|
|
|
2014-08-07 12:49:48 +04:00
|
|
|
func Open(cfg *config.Config) (*Ledis, error) {
|
2014-06-06 06:34:57 +04:00
|
|
|
if len(cfg.DataDir) == 0 {
|
2014-08-07 12:49:48 +04:00
|
|
|
cfg.DataDir = config.DefaultDataDir
|
2014-06-06 06:34:57 +04:00
|
|
|
}
|
|
|
|
|
2014-08-07 12:49:48 +04:00
|
|
|
ldb, err := store.Open(cfg)
|
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{})
|
2014-06-18 11:47:26 +04:00
|
|
|
l.jobs = new(sync.WaitGroup)
|
2014-06-05 11:59:10 +04:00
|
|
|
|
2014-05-20 04:41:24 +04:00
|
|
|
l.ldb = ldb
|
|
|
|
|
2014-09-18 18:30:33 +04:00
|
|
|
if cfg.UseBinLog {
|
2014-09-18 17:54:33 +04:00
|
|
|
println("binlog will be refactored later, use your own risk!!!")
|
2014-08-07 12:49:48 +04:00
|
|
|
l.binlog, err = NewBinLog(cfg)
|
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++ {
|
2014-08-25 10:18:23 +04:00
|
|
|
l.dbs[i] = l.newDB(i)
|
2014-05-20 04:41:24 +04:00
|
|
|
}
|
|
|
|
|
2014-06-12 06:51:36 +04:00
|
|
|
l.activeExpireCycle()
|
|
|
|
|
2014-05-20 04:41:24 +04:00
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Ledis) Close() {
|
2014-06-05 11:59:10 +04:00
|
|
|
close(l.quit)
|
2014-06-18 11:47:26 +04:00
|
|
|
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
|
|
|
|
2014-06-12 06:51:36 +04:00
|
|
|
func (l *Ledis) activeExpireCycle() {
|
|
|
|
var executors []*elimination = make([]*elimination, len(l.dbs))
|
|
|
|
for i, db := range l.dbs {
|
|
|
|
executors[i] = db.newEliminator()
|
|
|
|
}
|
|
|
|
|
2014-06-18 11:47:26 +04:00
|
|
|
l.jobs.Add(1)
|
2014-06-12 06:51:36 +04:00
|
|
|
go func() {
|
|
|
|
tick := time.NewTicker(1 * time.Second)
|
2014-06-18 11:47:26 +04:00
|
|
|
end := false
|
2014-07-07 12:39:36 +04:00
|
|
|
done := make(chan struct{})
|
2014-06-18 11:47:26 +04:00
|
|
|
for !end {
|
2014-06-12 06:51:36 +04:00
|
|
|
select {
|
|
|
|
case <-tick.C:
|
2014-07-07 12:39:36 +04:00
|
|
|
go func() {
|
|
|
|
for _, eli := range executors {
|
|
|
|
eli.active()
|
|
|
|
}
|
|
|
|
done <- struct{}{}
|
|
|
|
}()
|
|
|
|
<-done
|
2014-06-12 06:51:36 +04:00
|
|
|
case <-l.quit:
|
2014-06-18 11:47:26 +04:00
|
|
|
end = true
|
2014-06-12 06:51:36 +04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tick.Stop()
|
2014-06-18 11:47:26 +04:00
|
|
|
l.jobs.Done()
|
2014-06-12 06:51:36 +04:00
|
|
|
}()
|
|
|
|
}
|