using batch to optimize load dump

This commit is contained in:
siddontang 2014-11-10 15:35:57 +08:00
parent f47b916828
commit 660998f1fd
1 changed files with 19 additions and 2 deletions

View File

@ -155,6 +155,11 @@ func (l *Ledis) LoadDump(r io.Reader) (*DumpHead, error) {
var key, value []byte var key, value []byte
wb := l.ldb.NewWriteBatch()
defer wb.Close()
n := 0
for { for {
if err = binary.Read(rb, binary.BigEndian, &keyLen); err != nil && err != io.EOF { if err = binary.Read(rb, binary.BigEndian, &keyLen); err != nil && err != io.EOF {
return nil, err return nil, err
@ -182,14 +187,26 @@ func (l *Ledis) LoadDump(r io.Reader) (*DumpHead, error) {
return nil, err return nil, err
} }
if err = l.ldb.Put(key, value); err != nil { wb.Put(key, value)
return nil, err n++
if n%1024 == 0 {
if err = wb.Commit(); err != nil {
return nil, err
}
} }
// if err = l.ldb.Put(key, value); err != nil {
// return nil, err
// }
keyBuf.Reset() keyBuf.Reset()
valueBuf.Reset() valueBuf.Reset()
} }
if err = wb.Commit(); err != nil {
return nil, err
}
deKeyBuf = nil deKeyBuf = nil
deValueBuf = nil deValueBuf = nil