From 5b9d311f5dc06d447fd6019eb6845a77c1d09d88 Mon Sep 17 00:00:00 2001 From: siddontang Date: Mon, 5 May 2014 17:00:11 +0800 Subject: [PATCH] add helper function, batch add explicit Close --- leveldb/batch.go | 11 ++++---- leveldb/db.go | 57 +++++++++++++++++++++++++++++++++++++++++ leveldb/leveldb_test.go | 3 ++- leveldb/snapshot.go | 57 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 6 deletions(-) diff --git a/leveldb/batch.go b/leveldb/batch.go index 945531c..f65226a 100644 --- a/leveldb/batch.go +++ b/leveldb/batch.go @@ -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 } diff --git a/leveldb/db.go b/leveldb/db.go index 6da4160..5acd187 100644 --- a/leveldb/db.go +++ b/leveldb/db.go @@ -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 +} diff --git a/leveldb/leveldb_test.go b/leveldb/leveldb_test.go index a31e5b4..06b94f8 100644 --- a/leveldb/leveldb_test.go +++ b/leveldb/leveldb_test.go @@ -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() diff --git a/leveldb/snapshot.go b/leveldb/snapshot.go index 0663b5c..1801bcc 100644 --- a/leveldb/snapshot.go +++ b/leveldb/snapshot.go @@ -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 +}