ledisdb/store/driver/batch.go

40 lines
633 B
Go
Raw Normal View History

2014-08-02 11:16:16 +04:00
package driver
2014-07-25 13:58:00 +04:00
2014-08-02 11:16:16 +04:00
type BatchPuter interface {
2014-07-29 13:29:51 +04:00
BatchPut([]Write) error
}
2014-07-25 13:58:00 +04:00
type Write struct {
Key []byte
Value []byte
}
type WriteBatch struct {
2014-08-02 11:16:16 +04:00
batch BatchPuter
2014-07-29 13:29:51 +04:00
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-29 04:58:13 +04:00
if value == nil {
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
}
2014-08-02 11:16:16 +04:00
func NewWriteBatch(puter BatchPuter) IWriteBatch {
return &WriteBatch{puter, []Write{}}
}