add helper function, batch add explicit Close

This commit is contained in:
siddontang 2014-05-05 17:00:11 +08:00
parent 7589d8c9c0
commit 5b9d311f5d
4 changed files with 122 additions and 6 deletions

View File

@ -18,17 +18,18 @@ func (wb *WriteBatch) Delete(key []byte) {
}
func (wb *WriteBatch) Commit() error {
err := wb.db.db.Write(wb.db.writeOpts, wb.wb)
wb.close()
return err
return wb.db.db.Write(wb.db.writeOpts, wb.wb)
}
func (wb *WriteBatch) Rollback() {
wb.wb.Clear()
wb.close()
}
func (wb *WriteBatch) close() {
func (wb *WriteBatch) Close() {
if wb.wb == nil {
return
}
wb.wb.Close()
wb.wb = nil
}

View File

@ -3,6 +3,8 @@ package leveldb
import (
"encoding/json"
"github.com/jmhodges/levigo"
"github.com/siddontang/golib/hack"
"strconv"
)
const defaultFilterBits int = 10
@ -155,3 +157,58 @@ func (db *DB) ReverseIterator(rbegin []byte, rend []byte, limit int) *Iterator {
func (db *DB) NewSnapshot() *Snapshot {
return newSnapshot(db)
}
func (db *DB) GetInt(key []byte) (int64, error) {
v, err := db.Get(key)
if err != nil {
return 0, err
} else if v == nil {
return 0, nil
}
return strconv.ParseInt(hack.String(v), 10, 64)
}
func (db *DB) GetUInt(key []byte) (uint64, error) {
v, err := db.Get(key)
if err != nil {
return 0, err
} else if v == nil {
return 0, nil
}
return strconv.ParseUint(hack.String(v), 10, 64)
}
func (db *DB) GetFloat(key []byte) (float64, error) {
v, err := db.Get(key)
if err != nil {
return 0, err
} else if v == nil {
return 0, nil
}
return strconv.ParseFloat(hack.String(v), 64)
}
func (db *DB) GetString(key []byte) (string, error) {
v, err := db.Get(key)
if err != nil {
return "", err
} else if v == nil {
return "", nil
}
return hack.String(v), nil
}
func (db *DB) GetSlice(key []byte) ([]byte, error) {
v, err := db.Get(key)
if err != nil {
return nil, err
} else if v == nil {
return []byte{}, nil
}
return v, nil
}

View File

@ -73,6 +73,8 @@ func TestBatch(t *testing.T) {
db.Put(key2, value)
wb := db.NewWriteBatch()
defer wb.Close()
wb.Delete(key2)
wb.Put(key1, []byte("hello world2"))
@ -92,7 +94,6 @@ func TestBatch(t *testing.T) {
t.Fatal(string(v))
}
wb = db.NewWriteBatch()
wb.Delete(key1)
wb.Rollback()

View File

@ -2,6 +2,8 @@ package leveldb
import (
"github.com/jmhodges/levigo"
"github.com/siddontang/golib/hack"
"strconv"
)
type Snapshot struct {
@ -45,3 +47,58 @@ func (s *Snapshot) Iterator(begin []byte, end []byte, limit int) *Iterator {
func (s *Snapshot) ReverseIterator(rbegin []byte, rend []byte, limit int) *Iterator {
return newIterator(s.db, s.iteratorOpts, rbegin, rend, limit, backward)
}
func (s *Snapshot) GetInt(key []byte) (int64, error) {
v, err := s.Get(key)
if err != nil {
return 0, err
} else if v == nil {
return 0, nil
}
return strconv.ParseInt(hack.String(v), 10, 64)
}
func (s *Snapshot) GetUInt(key []byte) (uint64, error) {
v, err := s.Get(key)
if err != nil {
return 0, err
} else if v == nil {
return 0, nil
}
return strconv.ParseUint(hack.String(v), 10, 64)
}
func (s *Snapshot) GetFloat(key []byte) (float64, error) {
v, err := s.Get(key)
if err != nil {
return 0, err
} else if v == nil {
return 0, nil
}
return strconv.ParseFloat(hack.String(v), 64)
}
func (s *Snapshot) GetString(key []byte) (string, error) {
v, err := s.Get(key)
if err != nil {
return "", err
} else if v == nil {
return "", nil
}
return hack.String(v), nil
}
func (s *Snapshot) GetSlice(key []byte) ([]byte, error) {
v, err := s.Get(key)
if err != nil {
return nil, err
} else if v == nil {
return []byte{}, nil
}
return v, nil
}