works now!

git-svn-id: https://weed-fs.googlecode.com/svn/trunk@20 282b0af5-e82d-9cf1-ede4-77906d7719d0
This commit is contained in:
chris.lu@gmail.com committed 2011-12-22 04:04:47 +00:00
1 parent 9c6a9bf518
commit ea75165e85
7 files changed
+151 -77

No files matched your search

+38 -20
View File
@@ -1,42 +1,60 @@
package storage
import (
"log"
"os"
)
type NeedleKey struct {
Key uint64 "file id"
}
func (k *NeedleKey) String() string {
var tmp [12]byte
for i := uint(0); i < 8; i++ {
tmp[i] = byte(k.Key >> (8 * i))
}
return string(tmp[:])
}
type NeedleValue struct {
Offset uint32 "Volume offset" //since aligned to 8 bytes, range is 4G*8=32G
Size uint32 "Size of the data portion"
}
type NeedleMap struct {
m map[uint64]*NeedleValue //mapping NeedleKey(Key,AlternateKey) to NeedleValue
indexFile *os.File
m map[uint64]*NeedleValue //mapping needle key(uint64) to NeedleValue
bytes []byte
}
func NewNeedleMap() *NeedleMap {
return &NeedleMap{m: make(map[uint64]*NeedleValue)}
return &NeedleMap{m: make(map[uint64]*NeedleValue), bytes: make([]byte, 16)}
}
func (nm *NeedleMap) load(file *os.File) {
}
func makeKey(key uint64) uint64 {
return key
const (
RowsToRead = 1024
)
func LoadNeedleMap(file *os.File) *NeedleMap {
nm := NewNeedleMap()
nm.indexFile = file
bytes := make([]byte, 16*RowsToRead)
count, e := nm.indexFile.Read(bytes)
if count > 0 {
fstat, _ := file.Stat()
log.Println("Loading index file", fstat.Name, "size", fstat.Size)
}
for count > 0 && e == nil {
for i := 0; i < count; i += 16 {
key := BytesToUint64(bytes[i : i+8])
offset := BytesToUint32(bytes[i+8 : i+12])
size := BytesToUint32(bytes[i+12 : i+16])
nm.m[key] = &NeedleValue{Offset: offset, Size: size}
}
count, e = nm.indexFile.Read(bytes)
}
return nm
}
func (nm *NeedleMap) put(key uint64, offset uint32, size uint32) {
nm.m[makeKey(key)] = &NeedleValue{Offset: offset, Size: size}
nm.m[key] = &NeedleValue{Offset: offset, Size: size}
Uint64toBytes(nm.bytes[0:8], key)
Uint32toBytes(nm.bytes[8:12], offset)
Uint32toBytes(nm.bytes[12:16], size)
nm.indexFile.Write(nm.bytes)
}
func (nm *NeedleMap) get(key uint64) (element *NeedleValue, ok bool) {
element, ok = nm.m[makeKey(key)]
element, ok = nm.m[key]
return
}
func (nm *NeedleMap) Close() {
nm.indexFile.Close()
}