update helper function

This commit is contained in:
siddontang 2014-05-05 17:09:05 +08:00
parent 5b9d311f5d
commit 8dbf848f49
4 changed files with 86 additions and 84 deletions

View File

@ -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))
}

View File

@ -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)
}

View File

@ -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))
}

56
leveldb/util.go Normal file
View File

@ -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
}