mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2025-10-15 20:06:19 +08:00
simplify the file id format
git-svn-id: https://weed-fs.googlecode.com/svn/trunk@19 282b0af5-e82d-9cf1-ede4-77906d7719d0
This commit is contained in:
@@ -5,17 +5,16 @@ import (
|
||||
"io/ioutil"
|
||||
"http"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Needle struct {
|
||||
Cookie uint32 "random number to mitigate brute force lookups"
|
||||
Key uint64 "file id"
|
||||
Size uint32 "Data size"
|
||||
Data []byte "The actual file data"
|
||||
Checksum int32 "CRC32 to check integrity"
|
||||
Padding []byte "Aligned to 8 bytes"
|
||||
Cookie uint32 "random number to mitigate brute force lookups"
|
||||
Key uint64 "file id"
|
||||
Size uint32 "Data size"
|
||||
Data []byte "The actual file data"
|
||||
Checksum int32 "CRC32 to check integrity"
|
||||
Padding []byte "Aligned to 8 bytes"
|
||||
}
|
||||
|
||||
func NewNeedle(r *http.Request) (n *Needle) {
|
||||
@@ -33,30 +32,31 @@ func NewNeedle(r *http.Request) (n *Needle) {
|
||||
return
|
||||
}
|
||||
func (n *Needle) ParsePath(path string) {
|
||||
a := strings.Split(path, "_")
|
||||
log.Println("cookie", a[0], "key", a[1], "altKey", a[2])
|
||||
cookie, _ := strconv.Atoui(a[0])
|
||||
n.Cookie = uint32(cookie)
|
||||
n.Key, _ = strconv.Atoui64(a[1])
|
||||
if len(path) != 16 {
|
||||
return
|
||||
}
|
||||
bytes := []byte(path)
|
||||
n.Cookie = BytesToUint32(bytes[12:16])
|
||||
n.Key = BytesToUint64(bytes[4:12])
|
||||
}
|
||||
func (n *Needle) Append(w io.Writer) {
|
||||
header := make([]byte, 16)
|
||||
uint32toBytes(header[0:4], n.Cookie)
|
||||
uint64toBytes(header[4:12], n.Key)
|
||||
Uint32toBytes(header[0:4], n.Cookie)
|
||||
Uint64toBytes(header[4:12], n.Key)
|
||||
n.Size = uint32(len(n.Data))
|
||||
uint32toBytes(header[12:16], n.Size)
|
||||
Uint32toBytes(header[12:16], n.Size)
|
||||
w.Write(header)
|
||||
w.Write(n.Data)
|
||||
rest := 8 - ((n.Size + 16 + 4) % 8)
|
||||
uint32toBytes(header[0:4], uint32(n.Checksum))
|
||||
Uint32toBytes(header[0:4], uint32(n.Checksum))
|
||||
w.Write(header[0 : rest+4])
|
||||
}
|
||||
func (n *Needle) Read(r io.Reader, size uint32) {
|
||||
bytes := make([]byte, size+16+4)
|
||||
r.Read(bytes)
|
||||
n.Cookie = bytesToUint32(bytes[0:4])
|
||||
n.Key = bytesToUint64(bytes[4:12])
|
||||
n.Size = bytesToUint32(bytes[12:16])
|
||||
n.Cookie = BytesToUint32(bytes[0:4])
|
||||
n.Key = BytesToUint64(bytes[4:12])
|
||||
n.Size = BytesToUint32(bytes[12:16])
|
||||
n.Data = bytes[16 : 16+size]
|
||||
n.Checksum = int32(bytesToUint32(bytes[16+size : 16+size+4]))
|
||||
n.Checksum = int32(BytesToUint32(bytes[16+size : 16+size+4]))
|
||||
}
|
||||
|
@@ -17,7 +17,7 @@ type Store struct {
|
||||
PublicServer string
|
||||
}
|
||||
type VolumeInfo struct {
|
||||
Id uint64
|
||||
Id uint32
|
||||
Size int64
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ func NewStore(port int, publicServer, dirname string, chunkSize, capacity int) (
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
s.volumes[id] = NewVolume(s.dir, id)
|
||||
s.volumes[id] = NewVolume(s.dir, uint32(id))
|
||||
}
|
||||
log.Println("Store started on dir:", dirname, "with", len(s.volumes), "existing volumes")
|
||||
log.Println("Expected capacity=", s.capacity, "volumes")
|
||||
@@ -46,7 +46,7 @@ func (s *Store) Join(mserver string) {
|
||||
stats := new([]*VolumeInfo)
|
||||
for k, v := range s.volumes {
|
||||
s := new(VolumeInfo)
|
||||
s.Id, s.Size = k, v.Size()
|
||||
s.Id, s.Size = uint32(k), v.Size()
|
||||
*stats = append(*stats, s)
|
||||
}
|
||||
bytes, _ := json.Marshal(stats)
|
||||
@@ -63,7 +63,7 @@ func (s *Store) Join(mserver string) {
|
||||
e := json.Unmarshal(retString, newVids)
|
||||
if e == nil {
|
||||
for _, vid := range *newVids {
|
||||
s.volumes[uint64(vid)] = NewVolume(s.dir, uint64(vid))
|
||||
s.volumes[uint64(vid)] = NewVolume(s.dir, uint32(vid))
|
||||
log.Println("Adding volume", vid)
|
||||
}
|
||||
}
|
||||
|
@@ -7,30 +7,32 @@ import (
|
||||
"log"
|
||||
)
|
||||
|
||||
func bytesToUint64(b []byte)(v uint64){
|
||||
for i :=uint(7);i>0;i-- {
|
||||
func BytesToUint64(b []byte)(v uint64){
|
||||
length := uint(len(b))
|
||||
for i :=uint(0);i<length-1;i++ {
|
||||
v += uint64(b[i])
|
||||
v <<= 8
|
||||
}
|
||||
v+=uint64(b[0])
|
||||
v+=uint64(b[length-1])
|
||||
return
|
||||
}
|
||||
func bytesToUint32(b []byte)(v uint32){
|
||||
for i :=uint(3);i>0;i-- {
|
||||
func BytesToUint32(b []byte)(v uint32){
|
||||
length := uint(len(b))
|
||||
for i :=uint(0);i<length-1;i++ {
|
||||
v += uint32(b[i])
|
||||
v <<= 8
|
||||
}
|
||||
v+=uint32(b[0])
|
||||
v+=uint32(b[length-1])
|
||||
return
|
||||
}
|
||||
func uint64toBytes(b []byte, v uint64){
|
||||
func Uint64toBytes(b []byte, v uint64){
|
||||
for i :=uint(0);i<8;i++ {
|
||||
b[i] = byte(v>>(i*8))
|
||||
b[7-i] = byte(v>>(i*8))
|
||||
}
|
||||
}
|
||||
func uint32toBytes(b []byte, v uint32){
|
||||
func Uint32toBytes(b []byte, v uint32){
|
||||
for i :=uint(0);i<4;i++ {
|
||||
b[i] = byte(v>>(i*8))
|
||||
b[3-i] = byte(v>>(i*8))
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
type Volume struct {
|
||||
Id uint64
|
||||
Id uint32
|
||||
dir string
|
||||
dataFile, indexFile *os.File
|
||||
nm *NeedleMap
|
||||
@@ -16,10 +16,10 @@ type Volume struct {
|
||||
accessChannel chan int
|
||||
}
|
||||
|
||||
func NewVolume(dirname string, id uint64) (v *Volume) {
|
||||
func NewVolume(dirname string, id uint32) (v *Volume) {
|
||||
var e os.Error
|
||||
v = &Volume{dir:dirname,Id:id, nm:NewNeedleMap()}
|
||||
fileName := strconv.Uitoa64(v.Id)
|
||||
fileName := strconv.Uitoa64(uint64(v.Id))
|
||||
v.dataFile, e = os.OpenFile(path.Join(v.dir,fileName+".dat"), os.O_RDWR|os.O_CREATE, 0644)
|
||||
if e != nil {
|
||||
log.Fatalf("New Volume [ERROR] %s\n", e)
|
||||
|
Reference in New Issue
Block a user