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 ( import (
"encoding/json" "encoding/json"
"github.com/jmhodges/levigo" "github.com/jmhodges/levigo"
"github.com/siddontang/golib/hack"
"strconv"
) )
const defaultFilterBits int = 10 const defaultFilterBits int = 10
@ -159,56 +157,21 @@ func (db *DB) NewSnapshot() *Snapshot {
} }
func (db *DB) GetInt(key []byte) (int64, error) { func (db *DB) GetInt(key []byte) (int64, error) {
v, err := db.Get(key) return Int(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) { func (db *DB) GetUInt(key []byte) (uint64, error) {
v, err := db.Get(key) return Uint(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) { func (db *DB) GetFloat(key []byte) (float64, error) {
v, err := db.Get(key) return Float(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) { func (db *DB) GetString(key []byte) (string, error) {
v, err := db.Get(key) return String(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) { func (db *DB) GetSlice(key []byte) ([]byte, error) {
v, err := db.Get(key) return Slice(db.Get(key))
if err != nil {
return nil, err
} else if v == nil {
return []byte{}, nil
}
return v, nil
} }

View File

@ -99,3 +99,23 @@ func (it *Iterator) Value() []byte {
func (it *Iterator) Close() { func (it *Iterator) Close() {
it.it.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 ( import (
"github.com/jmhodges/levigo" "github.com/jmhodges/levigo"
"github.com/siddontang/golib/hack"
"strconv"
) )
type Snapshot struct { 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) { func (s *Snapshot) GetInt(key []byte) (int64, error) {
v, err := s.Get(key) return Int(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) { func (s *Snapshot) GetUInt(key []byte) (uint64, error) {
v, err := s.Get(key) return Uint(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) { func (s *Snapshot) GetFloat(key []byte) (float64, error) {
v, err := s.Get(key) return Float(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) { func (s *Snapshot) GetString(key []byte) (string, error) {
v, err := s.Get(key) return String(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) { func (s *Snapshot) GetSlice(key []byte) ([]byte, error) {
v, err := s.Get(key) return Slice(s.Get(key))
if err != nil {
return nil, err
} else if v == nil {
return []byte{}, nil
}
return v, nil
} }

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
}