Browse Source
simplify the file id format
simplify the file id format
git-svn-id: https://weed-fs.googlecode.com/svn/trunk@19 282b0af5-e82d-9cf1-ede4-77906d7719d0pull/2/head
chris.lu@gmail.com
13 years ago
8 changed files with 110 additions and 51 deletions
-
7weed-fs/src/cmd/weeds.go
-
44weed-fs/src/pkg/directory/file_id.go
-
16weed-fs/src/pkg/directory/file_id_test.go
-
18weed-fs/src/pkg/directory/volume_mapping.go
-
40weed-fs/src/pkg/storage/needle.go
-
8weed-fs/src/pkg/storage/store.go
-
22weed-fs/src/pkg/storage/util.go
-
6weed-fs/src/pkg/storage/volume.go
@ -0,0 +1,44 @@ |
|||||
|
package directory |
||||
|
|
||||
|
import ( |
||||
|
"encoding/hex" |
||||
|
"storage" |
||||
|
"strconv" |
||||
|
"strings" |
||||
|
) |
||||
|
|
||||
|
type FileId struct { |
||||
|
VolumeId uint32 |
||||
|
Key uint64 |
||||
|
Hashcode uint32 |
||||
|
} |
||||
|
|
||||
|
func NewFileId(VolumeId uint32, Key uint64, Hashcode uint32) *FileId { |
||||
|
return &FileId{VolumeId: VolumeId, Key: Key, Hashcode: Hashcode} |
||||
|
} |
||||
|
|
||||
|
func ParseFileId(path string) *FileId { |
||||
|
a := strings.Split(path, ",") |
||||
|
if len(a) != 2 { |
||||
|
return nil |
||||
|
} |
||||
|
vid_string, key_hash_string := a[0], a[1] |
||||
|
key_hash_bytes, khe := hex.DecodeString(key_hash_string) |
||||
|
key_hash_len := len(key_hash_bytes) |
||||
|
if khe != nil || key_hash_len <= 4 { |
||||
|
return nil |
||||
|
} |
||||
|
vid, _ := strconv.Atoui64(vid_string) |
||||
|
key := storage.BytesToUint64(key_hash_bytes[0 : key_hash_len-4]) |
||||
|
hash := storage.BytesToUint32(key_hash_bytes[key_hash_len-4 : key_hash_len]) |
||||
|
return &FileId{VolumeId: uint32(vid), Key: key, Hashcode: hash} |
||||
|
} |
||||
|
func (n *FileId) String() string { |
||||
|
bytes := make([]byte, 12) |
||||
|
storage.Uint64toBytes(bytes[0:8], n.Key) |
||||
|
storage.Uint32toBytes(bytes[8:12], n.Hashcode) |
||||
|
nonzero_index := 0 |
||||
|
for ; bytes[nonzero_index] == 0; nonzero_index++ { |
||||
|
} |
||||
|
return strconv.Uitoa64(uint64(n.VolumeId)) + "," + hex.EncodeToString(bytes[nonzero_index:]) |
||||
|
} |
@ -0,0 +1,16 @@ |
|||||
|
package directory |
||||
|
|
||||
|
import { |
||||
|
"testing" |
||||
|
"log" |
||||
|
} |
||||
|
func TestSerialDeserialization(t *testing.T) { |
||||
|
f1 := &FileId{VolumeId: 345, Key:8698, Hashcode: 23849095} |
||||
|
log.Println("vid", f1.VolumeId, "key", f1.Key, "hash", f1.Hashcode) |
||||
|
|
||||
|
f2 := ParseFileId(t.String()) |
||||
|
|
||||
|
log.Println("vvid", f2.VolumeId, "vkey", f2.Key, "vhash", f2.Hashcode) |
||||
|
|
||||
|
t. |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue