Browse Source

change name: needle id instead of needle key

pull/2/head
Chris Lu 13 years ago
parent
commit
b820609088
  1. 4
      weed-fs/src/cmd/weed/fix.go
  2. 12
      weed-fs/src/pkg/storage/needle.go
  3. 12
      weed-fs/src/pkg/storage/volume.go

4
weed-fs/src/cmd/weed/fix.go

@ -52,10 +52,10 @@ func runFix(cmd *Command, args []string) bool {
offset := uint32(storage.SuperBlockSize)
for n != nil {
if *IsDebug {
log.Println("key", n.Key, "volume offset", offset, "data_size", n.Size, "length", length)
log.Println("key", n.Id, "volume offset", offset, "data_size", n.Size, "length", length)
}
if n.Size > 0 {
count, pe := nm.Put(n.Key, offset/8, n.Size)
count, pe := nm.Put(n.Id, offset/8, n.Size)
if *IsDebug {
log.Println("saved", count, "with error", pe)
}

12
weed-fs/src/pkg/storage/needle.go

@ -15,7 +15,7 @@ import (
type Needle struct {
Cookie uint32 "random number to mitigate brute force lookups"
Key uint64 "file id"
Id uint64 "needle id"
Size uint32 "Data size"
Data []byte "The actual file data"
Checksum int32 "CRC32 to check integrity"
@ -69,18 +69,18 @@ func (n *Needle) ParsePath(fid string) {
if deltaIndex > 0 {
fid, delta = fid[0:deltaIndex], fid[deltaIndex+1:]
}
n.Key, n.Cookie = ParseKeyHash(fid)
n.Id, n.Cookie = ParseKeyHash(fid)
if delta != "" {
d, e := strconv.ParseUint(delta, 10, 64)
if e == nil {
n.Key += d
n.Id += d
}
}
}
func (n *Needle) Append(w io.Writer) uint32 {
header := make([]byte, 16)
util.Uint32toBytes(header[0:4], n.Cookie)
util.Uint64toBytes(header[4:12], n.Key)
util.Uint64toBytes(header[4:12], n.Id)
n.Size = uint32(len(n.Data))
util.Uint32toBytes(header[12:16], n.Size)
w.Write(header)
@ -94,7 +94,7 @@ func (n *Needle) Read(r io.Reader, size uint32) (int, error) {
bytes := make([]byte, size+16+4)
ret, e := r.Read(bytes)
n.Cookie = util.BytesToUint32(bytes[0:4])
n.Key = util.BytesToUint64(bytes[4:12])
n.Id = util.BytesToUint64(bytes[4:12])
n.Size = util.BytesToUint32(bytes[12:16])
n.Data = bytes[16 : 16+size]
n.Checksum = int32(util.BytesToUint32(bytes[16+size : 16+size+4]))
@ -108,7 +108,7 @@ func ReadNeedle(r *os.File) (*Needle, uint32) {
return nil, 0
}
n.Cookie = util.BytesToUint32(bytes[0:4])
n.Key = util.BytesToUint64(bytes[4:12])
n.Id = util.BytesToUint64(bytes[4:12])
n.Size = util.BytesToUint32(bytes[12:16])
rest := 8 - ((n.Size + 16 + 4) % 8)
r.Seek(int64(n.Size+4+rest), 1)

12
weed-fs/src/pkg/storage/volume.go

@ -76,19 +76,19 @@ func (v *Volume) write(n *Needle) uint32 {
defer v.accessLock.Unlock()
offset, _ := v.dataFile.Seek(0, 2)
ret := n.Append(v.dataFile)
nv, ok := v.nm.Get(n.Key)
nv, ok := v.nm.Get(n.Id)
if !ok || int64(nv.Offset)*8 < offset {
v.nm.Put(n.Key, uint32(offset/8), n.Size)
v.nm.Put(n.Id, uint32(offset/8), n.Size)
}
return ret
}
func (v *Volume) delete(n *Needle) uint32 {
v.accessLock.Lock()
defer v.accessLock.Unlock()
nv, ok := v.nm.Get(n.Key)
//log.Println("key", n.Key, "volume offset", nv.Offset, "data_size", n.Size, "cached size", nv.Size)
nv, ok := v.nm.Get(n.Id)
//log.Println("key", n.Id, "volume offset", nv.Offset, "data_size", n.Size, "cached size", nv.Size)
if ok {
v.nm.Delete(n.Key)
v.nm.Delete(n.Id)
v.dataFile.Seek(int64(nv.Offset*8), 0)
n.Append(v.dataFile)
return nv.Size
@ -98,7 +98,7 @@ func (v *Volume) delete(n *Needle) uint32 {
func (v *Volume) read(n *Needle) (int, error) {
v.accessLock.Lock()
defer v.accessLock.Unlock()
nv, ok := v.nm.Get(n.Key)
nv, ok := v.nm.Get(n.Id)
if ok && nv.Offset > 0 {
v.dataFile.Seek(int64(nv.Offset)*8, 0)
return n.Read(v.dataFile, nv.Size)

Loading…
Cancel
Save