From 660998f1fdc65ee453809df429e441df01f9f860 Mon Sep 17 00:00:00 2001 From: siddontang Date: Mon, 10 Nov 2014 15:35:57 +0800 Subject: [PATCH] using batch to optimize load dump --- ledis/dump.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/ledis/dump.go b/ledis/dump.go index 1f6d5e2..863255c 100644 --- a/ledis/dump.go +++ b/ledis/dump.go @@ -155,6 +155,11 @@ func (l *Ledis) LoadDump(r io.Reader) (*DumpHead, error) { var key, value []byte + wb := l.ldb.NewWriteBatch() + defer wb.Close() + + n := 0 + for { if err = binary.Read(rb, binary.BigEndian, &keyLen); err != nil && err != io.EOF { return nil, err @@ -182,14 +187,26 @@ func (l *Ledis) LoadDump(r io.Reader) (*DumpHead, error) { return nil, err } - if err = l.ldb.Put(key, value); err != nil { - return nil, err + wb.Put(key, value) + 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() valueBuf.Reset() } + if err = wb.Commit(); err != nil { + return nil, err + } + deKeyBuf = nil deValueBuf = nil