2014-07-25 13:58:00 +04:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
2014-10-16 05:35:35 +04:00
|
|
|
"sync"
|
2014-10-15 06:18:20 +04:00
|
|
|
"time"
|
2015-05-04 17:42:28 +03:00
|
|
|
|
2023-02-15 10:41:45 +03:00
|
|
|
"git.internal/re/ledisdb/config"
|
|
|
|
"git.internal/re/ledisdb/store/driver"
|
2014-07-25 13:58:00 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
type DB struct {
|
2014-10-16 05:35:35 +04:00
|
|
|
db driver.IDB
|
2014-09-12 11:06:36 +04:00
|
|
|
name string
|
2014-10-15 06:18:20 +04:00
|
|
|
|
|
|
|
st *Stat
|
2014-10-16 05:35:35 +04:00
|
|
|
|
|
|
|
cfg *config.Config
|
|
|
|
|
|
|
|
lastCommit time.Time
|
|
|
|
|
|
|
|
m sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) Close() error {
|
|
|
|
return db.db.Close()
|
2014-09-12 11:06:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) String() string {
|
|
|
|
return db.name
|
2014-07-25 13:58:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) NewIterator() *Iterator {
|
2014-10-15 06:18:20 +04:00
|
|
|
db.st.IterNum.Add(1)
|
|
|
|
|
2014-07-25 13:58:00 +04:00
|
|
|
it := new(Iterator)
|
2014-10-16 05:35:35 +04:00
|
|
|
it.it = db.db.NewIterator()
|
2014-10-15 06:18:20 +04:00
|
|
|
it.st = db.st
|
2014-07-25 13:58:00 +04:00
|
|
|
|
|
|
|
return it
|
|
|
|
}
|
|
|
|
|
2014-10-15 06:18:20 +04:00
|
|
|
func (db *DB) Get(key []byte) ([]byte, error) {
|
2014-10-30 11:06:44 +03:00
|
|
|
t := time.Now()
|
2014-10-16 05:35:35 +04:00
|
|
|
v, err := db.db.Get(key)
|
2014-10-15 06:18:20 +04:00
|
|
|
db.st.statGet(v, err)
|
2014-10-30 11:06:44 +03:00
|
|
|
db.st.GetTotalTime.Add(time.Now().Sub(t))
|
2014-10-15 06:18:20 +04:00
|
|
|
return v, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) Put(key []byte, value []byte) error {
|
|
|
|
db.st.PutNum.Add(1)
|
2014-10-16 05:35:35 +04:00
|
|
|
|
|
|
|
if db.needSyncCommit() {
|
|
|
|
return db.db.SyncPut(key, value)
|
|
|
|
}
|
2020-05-10 13:17:39 +03:00
|
|
|
return db.db.Put(key, value)
|
2014-10-15 06:18:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) Delete(key []byte) error {
|
|
|
|
db.st.DeleteNum.Add(1)
|
|
|
|
|
2014-10-16 05:35:35 +04:00
|
|
|
if db.needSyncCommit() {
|
|
|
|
return db.db.SyncDelete(key)
|
|
|
|
}
|
2020-05-10 13:17:39 +03:00
|
|
|
return db.db.Delete(key)
|
2014-10-15 06:18:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) NewWriteBatch() *WriteBatch {
|
|
|
|
db.st.BatchNum.Add(1)
|
|
|
|
wb := new(WriteBatch)
|
2014-10-16 05:35:35 +04:00
|
|
|
wb.wb = db.db.NewWriteBatch()
|
2014-10-15 06:18:20 +04:00
|
|
|
wb.st = db.st
|
2014-10-16 05:35:35 +04:00
|
|
|
wb.db = db
|
2014-10-15 06:18:20 +04:00
|
|
|
return wb
|
2014-07-25 13:58:00 +04:00
|
|
|
}
|
|
|
|
|
2014-08-30 13:19:10 +04:00
|
|
|
func (db *DB) NewSnapshot() (*Snapshot, error) {
|
2014-10-15 06:18:20 +04:00
|
|
|
db.st.SnapshotNum.Add(1)
|
|
|
|
|
2014-08-30 13:19:10 +04:00
|
|
|
var err error
|
|
|
|
s := &Snapshot{}
|
2014-10-16 05:35:35 +04:00
|
|
|
if s.ISnapshot, err = db.db.NewSnapshot(); err != nil {
|
2014-08-30 13:19:10 +04:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-15 06:18:20 +04:00
|
|
|
s.st = db.st
|
2014-08-30 13:19:10 +04:00
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2014-09-12 11:06:36 +04:00
|
|
|
func (db *DB) Compact() error {
|
2014-10-15 06:18:20 +04:00
|
|
|
db.st.CompactNum.Add(1)
|
|
|
|
|
|
|
|
t := time.Now()
|
2014-10-16 05:35:35 +04:00
|
|
|
err := db.db.Compact()
|
2014-10-15 06:18:20 +04:00
|
|
|
|
|
|
|
db.st.CompactTotalTime.Add(time.Now().Sub(t))
|
|
|
|
|
|
|
|
return err
|
2014-09-12 11:06:36 +04:00
|
|
|
}
|
|
|
|
|
2014-07-25 13:58:00 +04:00
|
|
|
func (db *DB) RangeIterator(min []byte, max []byte, rangeType uint8) *RangeLimitIterator {
|
|
|
|
return NewRangeLimitIterator(db.NewIterator(), &Range{min, max, rangeType}, &Limit{0, -1})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) RevRangeIterator(min []byte, max []byte, rangeType uint8) *RangeLimitIterator {
|
|
|
|
return NewRevRangeLimitIterator(db.NewIterator(), &Range{min, max, rangeType}, &Limit{0, -1})
|
|
|
|
}
|
|
|
|
|
2020-05-10 13:17:39 +03:00
|
|
|
//RangeLimitIterator count < 0, unlimit.
|
2014-07-25 13:58:00 +04:00
|
|
|
//
|
|
|
|
//offset must >= 0, if < 0, will get nothing.
|
|
|
|
func (db *DB) RangeLimitIterator(min []byte, max []byte, rangeType uint8, offset int, count int) *RangeLimitIterator {
|
|
|
|
return NewRangeLimitIterator(db.NewIterator(), &Range{min, max, rangeType}, &Limit{offset, count})
|
|
|
|
}
|
|
|
|
|
2020-05-10 13:17:39 +03:00
|
|
|
//RevRangeLimitIterator count < 0, unlimit.
|
2014-07-25 13:58:00 +04:00
|
|
|
//
|
|
|
|
//offset must >= 0, if < 0, will get nothing.
|
|
|
|
func (db *DB) RevRangeLimitIterator(min []byte, max []byte, rangeType uint8, offset int, count int) *RangeLimitIterator {
|
|
|
|
return NewRevRangeLimitIterator(db.NewIterator(), &Range{min, max, rangeType}, &Limit{offset, count})
|
|
|
|
}
|
2014-07-29 13:29:51 +04:00
|
|
|
|
2014-10-15 06:18:20 +04:00
|
|
|
func (db *DB) Stat() *Stat {
|
|
|
|
return db.st
|
2014-07-29 13:29:51 +04:00
|
|
|
}
|
2014-10-16 05:35:35 +04:00
|
|
|
|
|
|
|
func (db *DB) needSyncCommit() bool {
|
|
|
|
if db.cfg.DBSyncCommit == 0 {
|
|
|
|
return false
|
|
|
|
} else if db.cfg.DBSyncCommit == 2 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-05-10 13:17:39 +03:00
|
|
|
n := time.Now()
|
|
|
|
need := false
|
|
|
|
db.m.Lock()
|
|
|
|
|
|
|
|
if n.Sub(db.lastCommit) > time.Second {
|
|
|
|
need = true
|
|
|
|
}
|
|
|
|
db.lastCommit = n
|
|
|
|
|
|
|
|
db.m.Unlock()
|
|
|
|
return need
|
2014-10-16 05:35:35 +04:00
|
|
|
}
|
2014-10-29 05:29:35 +03:00
|
|
|
|
|
|
|
func (db *DB) GetSlice(key []byte) (Slice, error) {
|
|
|
|
if d, ok := db.db.(driver.ISliceGeter); ok {
|
2014-10-30 11:06:44 +03:00
|
|
|
t := time.Now()
|
2014-10-29 05:29:35 +03:00
|
|
|
v, err := d.GetSlice(key)
|
|
|
|
db.st.statGet(v, err)
|
2014-10-30 11:06:44 +03:00
|
|
|
db.st.GetTotalTime.Add(time.Now().Sub(t))
|
2014-10-29 05:29:35 +03:00
|
|
|
return v, err
|
|
|
|
}
|
2020-05-10 13:17:39 +03:00
|
|
|
v, err := db.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if v == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return driver.GoSlice(v), nil
|
2014-10-29 05:29:35 +03:00
|
|
|
}
|