diff options
| author | Chris Lu <chris.lu@gmail.com> | 2016-04-10 01:42:17 -0700 |
|---|---|---|
| committer | Chris Lu <chris.lu@gmail.com> | 2016-04-10 01:42:17 -0700 |
| commit | 62f9ce180de18cd34a02d6ddecccd145b0636d05 (patch) | |
| tree | 73eb7c3d75caf0cfa199e00b2f2d285088a72c50 /go/storage/needle.go | |
| parent | b39c384d6dfa63133990a1e190525ef3133f1530 (diff) | |
| parent | eec72b51362773cc5ef7f585bd1d8ccf4061ac7a (diff) | |
| download | seaweedfs-62f9ce180de18cd34a02d6ddecccd145b0636d05.tar.xz seaweedfs-62f9ce180de18cd34a02d6ddecccd145b0636d05.zip | |
Merge pull request #286 from thinxer/parseKeyHash
faster ParseKeyHash
Diffstat (limited to 'go/storage/needle.go')
| -rw-r--r-- | go/storage/needle.go | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/go/storage/needle.go b/go/storage/needle.go index e49368820..66cf5b22a 100644 --- a/go/storage/needle.go +++ b/go/storage/needle.go @@ -1,7 +1,6 @@ package storage import ( - "encoding/hex" "errors" "fmt" "io/ioutil" @@ -15,7 +14,6 @@ import ( "github.com/chrislusf/seaweedfs/go/glog" "github.com/chrislusf/seaweedfs/go/images" "github.com/chrislusf/seaweedfs/go/operation" - "github.com/chrislusf/seaweedfs/go/util" ) const ( @@ -213,16 +211,25 @@ func (n *Needle) ParsePath(fid string) (err error) { } func ParseKeyHash(key_hash_string string) (uint64, uint32, error) { - if len(key_hash_string)%2 == 1 { - key_hash_string = "0" + key_hash_string - } - key_hash_bytes, khe := hex.DecodeString(key_hash_string) - key_hash_len := len(key_hash_bytes) - if khe != nil || key_hash_len <= 4 { - glog.V(0).Infoln("Invalid key_hash", key_hash_string, "length:", key_hash_len, "error", khe) + key, hash, ok := parseKeyHash(key_hash_string) + if !ok { return 0, 0, errors.New("Invalid key and hash:" + key_hash_string) } - key := util.BytesToUint64(key_hash_bytes[0 : key_hash_len-4]) - hash := util.BytesToUint32(key_hash_bytes[key_hash_len-4 : key_hash_len]) return key, hash, nil } + +func parseKeyHash(keyhash string) (uint64, uint32, bool) { + if len(keyhash) <= 8 || len(keyhash) > 24 { + return 0, 0, false + } + split := len(keyhash) - 8 + key, err := strconv.ParseUint(keyhash[:split], 16, 64) + if err != nil { + return 0, 0, false + } + hash, err := strconv.ParseUint(keyhash[split:], 16, 32) + if err != nil { + return 0, 0, false + } + return key, uint32(hash), true +} |
