Files
seaweedfs/weed-fs/src/pkg/storage/needle_map.go
T
chris.lu@gmail.com b39d1a77b4 cleaner
git-svn-id: https://weed-fs.googlecode.com/svn/trunk@16 282b0af5-e82d-9cf1-ede4-77906d7719d0
2011-12-19 05:59:37 +00:00

54 lines
1.2 KiB
Go

package storage
import (
"os"
)
type NeedleKey struct {
Key uint64 "file id"
AlternateKey uint32 "supplemental id"
}
func (k *NeedleKey) String() string {
var tmp [12]byte
for i := uint(0); i < 8; i++ {
tmp[i] = byte(k.Key >> (8 * i))
}
for i := uint(0); i < 4; i++ {
tmp[i+8] = byte(k.AlternateKey >> (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[string]*NeedleValue //mapping NeedleKey(Key,AlternateKey) to NeedleValue
}
func NewNeedleMap() *NeedleMap {
return &NeedleMap{m: make(map[string]*NeedleValue)}
}
func (nm *NeedleMap) load(file *os.File) {
}
func makeKey(key uint64, altKey uint32) string {
var tmp [12]byte
for i := uint(0); i < 8; i++ {
tmp[i] = byte(key >> (8 * i))
}
for i := uint(0); i < 4; i++ {
tmp[i+8] = byte(altKey >> (8 * i))
}
return string(tmp[:])
}
func (nm *NeedleMap) put(key uint64, altKey uint32, offset uint32, size uint32) {
nm.m[makeKey(key, altKey)] = &NeedleValue{Offset: offset, Size: size}
}
func (nm *NeedleMap) get(key uint64, altKey uint32) (element *NeedleValue, ok bool) {
element, ok = nm.m[makeKey(key, altKey)]
return
}