2014-07-25 13:58:00 +04:00
|
|
|
package mdb
|
|
|
|
|
2014-07-29 13:29:51 +04:00
|
|
|
type batchPut interface {
|
|
|
|
BatchPut([]Write) error
|
|
|
|
}
|
|
|
|
|
2014-07-25 13:58:00 +04:00
|
|
|
type Write struct {
|
|
|
|
Key []byte
|
|
|
|
Value []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type WriteBatch struct {
|
2014-07-29 13:29:51 +04:00
|
|
|
batch batchPut
|
|
|
|
wb []Write
|
2014-07-25 13:58:00 +04:00
|
|
|
}
|
|
|
|
|
2014-07-25 15:41:04 +04:00
|
|
|
func (w *WriteBatch) Put(key, value []byte) {
|
2014-07-25 13:58:00 +04:00
|
|
|
w.wb = append(w.wb, Write{key, value})
|
|
|
|
}
|
|
|
|
|
2014-07-25 15:41:04 +04:00
|
|
|
func (w *WriteBatch) Delete(key []byte) {
|
2014-07-25 13:58:00 +04:00
|
|
|
w.wb = append(w.wb, Write{key, nil})
|
|
|
|
}
|
|
|
|
|
2014-07-25 15:41:04 +04:00
|
|
|
func (w *WriteBatch) Commit() error {
|
2014-07-29 13:29:51 +04:00
|
|
|
return w.batch.BatchPut(w.wb)
|
2014-07-25 13:58:00 +04:00
|
|
|
}
|
|
|
|
|
2014-07-25 15:41:04 +04:00
|
|
|
func (w *WriteBatch) Rollback() error {
|
2014-07-25 19:40:10 +04:00
|
|
|
w.wb = w.wb[0:0]
|
2014-07-25 13:58:00 +04:00
|
|
|
return nil
|
|
|
|
}
|