From 8dbf848f49b0fa9209404d2c1e5749ac79a42e08 Mon Sep 17 00:00:00 2001 From: siddontang Date: Mon, 5 May 2014 17:09:05 +0800 Subject: [PATCH] update helper function --- leveldb/db.go | 47 ++++--------------------------------- leveldb/iterator.go | 20 ++++++++++++++++ leveldb/snapshot.go | 47 ++++--------------------------------- leveldb/util.go | 56 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 84 deletions(-) create mode 100644 leveldb/util.go diff --git a/leveldb/db.go b/leveldb/db.go index 5acd187..c348ed4 100644 --- a/leveldb/db.go +++ b/leveldb/db.go @@ -3,8 +3,6 @@ package leveldb import ( "encoding/json" "github.com/jmhodges/levigo" - "github.com/siddontang/golib/hack" - "strconv" ) const defaultFilterBits int = 10 @@ -159,56 +157,21 @@ func (db *DB) NewSnapshot() *Snapshot { } 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) + return Int(db.Get(key)) } 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) + return Uint(db.Get(key)) } 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) + return Float(db.Get(key)) } 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 + return String(db.Get(key)) } 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 + return Slice(db.Get(key)) } diff --git a/leveldb/iterator.go b/leveldb/iterator.go index cd8ce50..e8f1e19 100644 --- a/leveldb/iterator.go +++ b/leveldb/iterator.go @@ -99,3 +99,23 @@ func (it *Iterator) Value() []byte { func (it *Iterator) Close() { it.it.Close() } + +func (it *Iterator) IntValue() (int64, error) { + return Int(it.Value(), nil) +} + +func (it *Iterator) UintValue() (uint64, error) { + return Uint(it.Value(), nil) +} + +func (it *Iterator) FloatValue() (float64, error) { + return Float(it.Value(), nil) +} + +func (it *Iterator) StringValue() (string, error) { + return String(it.Value(), nil) +} + +func (it *Iterator) SliceValue() ([]byte, error) { + return Slice(it.Value(), nil) +} diff --git a/leveldb/snapshot.go b/leveldb/snapshot.go index 1801bcc..ab7b37a 100644 --- a/leveldb/snapshot.go +++ b/leveldb/snapshot.go @@ -2,8 +2,6 @@ package leveldb import ( "github.com/jmhodges/levigo" - "github.com/siddontang/golib/hack" - "strconv" ) type Snapshot struct { @@ -49,56 +47,21 @@ func (s *Snapshot) ReverseIterator(rbegin []byte, rend []byte, limit int) *Itera } 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) + return Int(s.Get(key)) } 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) + return Uint(s.Get(key)) } 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) + return Float(s.Get(key)) } 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 + return String(s.Get(key)) } 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 + return Slice(s.Get(key)) } diff --git a/leveldb/util.go b/leveldb/util.go new file mode 100644 index 0000000..66b870b --- /dev/null +++ b/leveldb/util.go @@ -0,0 +1,56 @@ +package leveldb + +import ( + "github.com/siddontang/golib/hack" + "strconv" +) + +func Int(v []byte, err error) (int64, error) { + if err != nil { + return 0, err + } else if v == nil { + return 0, nil + } + + return strconv.ParseInt(hack.String(v), 10, 64) +} + +func Uint(v []byte, err error) (uint64, error) { + if err != nil { + return 0, err + } else if v == nil { + return 0, nil + } + + return strconv.ParseUint(hack.String(v), 10, 64) +} + +func Float(v []byte, err error) (float64, error) { + if err != nil { + return 0, err + } else if v == nil { + return 0, nil + } + + return strconv.ParseFloat(hack.String(v), 64) +} + +func String(v []byte, err error) (string, error) { + if err != nil { + return "", err + } else if v == nil { + return "", nil + } + + return hack.String(v), nil +} + +func Slice(v []byte, err error) ([]byte, error) { + if err != nil { + return nil, err + } else if v == nil { + return []byte{}, nil + } + + return v, nil +}