2014-09-22 13:50:51 +04:00
|
|
|
package rpl
|
2014-09-15 18:42:25 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2014-09-17 13:54:04 +04:00
|
|
|
"fmt"
|
2014-09-15 18:42:25 +04:00
|
|
|
"github.com/siddontang/go/num"
|
|
|
|
"github.com/siddontang/ledisdb/config"
|
|
|
|
"github.com/siddontang/ledisdb/store"
|
|
|
|
"os"
|
|
|
|
"sync"
|
2014-09-17 13:54:04 +04:00
|
|
|
"time"
|
2014-09-15 18:42:25 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
type GoLevelDBStore struct {
|
2014-09-22 13:50:51 +04:00
|
|
|
LogStore
|
|
|
|
|
2014-09-15 18:42:25 +04:00
|
|
|
m sync.Mutex
|
|
|
|
db *store.DB
|
|
|
|
|
|
|
|
cfg *config.Config
|
|
|
|
|
|
|
|
first uint64
|
|
|
|
last uint64
|
2014-10-31 10:40:47 +03:00
|
|
|
|
|
|
|
buf bytes.Buffer
|
2014-09-15 18:42:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GoLevelDBStore) FirstID() (uint64, error) {
|
|
|
|
s.m.Lock()
|
2014-09-22 13:50:51 +04:00
|
|
|
id, err := s.firstID()
|
|
|
|
s.m.Unlock()
|
|
|
|
|
|
|
|
return id, err
|
2014-09-15 18:42:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GoLevelDBStore) LastID() (uint64, error) {
|
|
|
|
s.m.Lock()
|
2014-09-22 13:50:51 +04:00
|
|
|
id, err := s.lastID()
|
|
|
|
s.m.Unlock()
|
|
|
|
|
|
|
|
return id, err
|
2014-09-15 18:42:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GoLevelDBStore) firstID() (uint64, error) {
|
|
|
|
if s.first != InvalidLogID {
|
|
|
|
return s.first, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
it := s.db.NewIterator()
|
|
|
|
defer it.Close()
|
|
|
|
|
|
|
|
it.SeekToFirst()
|
|
|
|
|
|
|
|
if it.Valid() {
|
|
|
|
s.first = num.BytesToUint64(it.RawKey())
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.first, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GoLevelDBStore) lastID() (uint64, error) {
|
|
|
|
if s.last != InvalidLogID {
|
|
|
|
return s.last, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
it := s.db.NewIterator()
|
|
|
|
defer it.Close()
|
|
|
|
|
|
|
|
it.SeekToLast()
|
|
|
|
|
|
|
|
if it.Valid() {
|
|
|
|
s.last = num.BytesToUint64(it.RawKey())
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.last, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GoLevelDBStore) GetLog(id uint64, log *Log) error {
|
|
|
|
v, err := s.db.Get(num.Uint64ToBytes(id))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if v == nil {
|
|
|
|
return ErrLogNotFound
|
|
|
|
} else {
|
|
|
|
return log.Decode(bytes.NewBuffer(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GoLevelDBStore) StoreLog(log *Log) error {
|
|
|
|
s.m.Lock()
|
|
|
|
defer s.m.Unlock()
|
|
|
|
|
2014-09-16 04:39:52 +04:00
|
|
|
last, err := s.lastID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-09-15 18:42:25 +04:00
|
|
|
|
2014-09-16 04:39:52 +04:00
|
|
|
s.last = InvalidLogID
|
2014-09-15 18:42:25 +04:00
|
|
|
|
2014-10-31 10:40:47 +03:00
|
|
|
s.buf.Reset()
|
2014-09-15 18:42:25 +04:00
|
|
|
|
2014-10-31 10:40:47 +03:00
|
|
|
if log.ID != last+1 {
|
|
|
|
return ErrStoreLogID
|
|
|
|
}
|
2014-09-15 18:42:25 +04:00
|
|
|
|
2014-10-31 10:40:47 +03:00
|
|
|
last = log.ID
|
|
|
|
key := num.Uint64ToBytes(log.ID)
|
2014-09-15 18:42:25 +04:00
|
|
|
|
2014-10-31 10:40:47 +03:00
|
|
|
if err := log.Encode(&s.buf); err != nil {
|
|
|
|
return err
|
2014-09-15 18:42:25 +04:00
|
|
|
}
|
|
|
|
|
2014-10-31 10:40:47 +03:00
|
|
|
if err = s.db.Put(key, s.buf.Bytes()); err != nil {
|
2014-09-16 04:39:52 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.last = last
|
|
|
|
return nil
|
2014-09-15 18:42:25 +04:00
|
|
|
}
|
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
func (s *GoLevelDBStore) PurgeExpired(n int64) error {
|
2014-09-17 13:54:04 +04:00
|
|
|
if n <= 0 {
|
|
|
|
return fmt.Errorf("invalid expired time %d", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
t := uint32(time.Now().Unix() - int64(n))
|
|
|
|
|
|
|
|
s.m.Lock()
|
|
|
|
defer s.m.Unlock()
|
|
|
|
|
|
|
|
s.reset()
|
|
|
|
|
|
|
|
it := s.db.NewIterator()
|
|
|
|
it.SeekToFirst()
|
|
|
|
|
|
|
|
w := s.db.NewWriteBatch()
|
|
|
|
defer w.Rollback()
|
|
|
|
|
|
|
|
l := new(Log)
|
|
|
|
for ; it.Valid(); it.Next() {
|
|
|
|
v := it.RawValue()
|
|
|
|
|
|
|
|
if err := l.Unmarshal(v); err != nil {
|
|
|
|
return err
|
|
|
|
} else if l.CreateTime > t {
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
w.Delete(it.RawKey())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := w.Commit(); err != nil {
|
2014-09-15 18:42:25 +04:00
|
|
|
return err
|
|
|
|
}
|
2014-09-16 04:39:52 +04:00
|
|
|
|
2014-09-15 18:42:25 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-06 16:52:18 +03:00
|
|
|
func (s *GoLevelDBStore) reset() {
|
|
|
|
s.first = InvalidLogID
|
|
|
|
s.last = InvalidLogID
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:42:25 +04:00
|
|
|
func (s *GoLevelDBStore) Clear() error {
|
|
|
|
s.m.Lock()
|
|
|
|
defer s.m.Unlock()
|
|
|
|
|
|
|
|
if s.db != nil {
|
|
|
|
s.db.Close()
|
|
|
|
}
|
|
|
|
|
2014-09-16 04:39:52 +04:00
|
|
|
s.reset()
|
2014-09-15 18:42:25 +04:00
|
|
|
os.RemoveAll(s.cfg.DBPath)
|
|
|
|
|
|
|
|
return s.open()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GoLevelDBStore) Close() error {
|
|
|
|
s.m.Lock()
|
|
|
|
defer s.m.Unlock()
|
|
|
|
|
|
|
|
if s.db == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := s.db.Close()
|
|
|
|
s.db = nil
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GoLevelDBStore) open() error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
s.first = InvalidLogID
|
|
|
|
s.last = InvalidLogID
|
|
|
|
|
|
|
|
s.db, err = store.Open(s.cfg)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-10-16 05:35:35 +04:00
|
|
|
func NewGoLevelDBStore(base string, syncLog int) (*GoLevelDBStore, error) {
|
2014-10-11 12:00:59 +04:00
|
|
|
cfg := config.NewConfigDefault()
|
2014-09-15 18:42:25 +04:00
|
|
|
cfg.DBName = "goleveldb"
|
|
|
|
cfg.DBPath = base
|
2014-10-11 12:00:59 +04:00
|
|
|
cfg.LevelDB.BlockSize = 16 * 1024 * 1024
|
|
|
|
cfg.LevelDB.CacheSize = 64 * 1024 * 1024
|
|
|
|
cfg.LevelDB.WriteBufferSize = 64 * 1024 * 1024
|
2014-09-15 18:42:25 +04:00
|
|
|
cfg.LevelDB.Compression = false
|
2014-10-16 05:35:35 +04:00
|
|
|
cfg.DBSyncCommit = syncLog
|
2014-09-15 18:42:25 +04:00
|
|
|
|
|
|
|
s := new(GoLevelDBStore)
|
|
|
|
s.cfg = cfg
|
|
|
|
|
|
|
|
if err := s.open(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|