Browse Source
works now!
works now!
git-svn-id: https://weed-fs.googlecode.com/svn/trunk@20 282b0af5-e82d-9cf1-ede4-77906d7719d0pull/2/head
chris.lu@gmail.com
13 years ago
7 changed files with 151 additions and 77 deletions
-
59weed-fs/src/cmd/weedc.go
-
24weed-fs/src/cmd/weeds.go
-
15weed-fs/src/pkg/directory/file_id.go
-
31weed-fs/src/pkg/storage/needle.go
-
58weed-fs/src/pkg/storage/needle_map.go
-
9weed-fs/src/pkg/storage/store.go
-
32weed-fs/src/pkg/storage/volume.go
@ -1,42 +1,60 @@ |
|||||
package storage |
package storage |
||||
|
|
||||
import ( |
import ( |
||||
|
"log" |
||||
"os" |
"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 { |
type NeedleValue struct { |
||||
Offset uint32 "Volume offset" //since aligned to 8 bytes, range is 4G*8=32G
|
Offset uint32 "Volume offset" //since aligned to 8 bytes, range is 4G*8=32G
|
||||
Size uint32 "Size of the data portion" |
Size uint32 "Size of the data portion" |
||||
} |
} |
||||
|
|
||||
type NeedleMap struct { |
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 { |
func NewNeedleMap() *NeedleMap { |
||||
return &NeedleMap{m: make(map[uint64]*NeedleValue)} |
|
||||
} |
|
||||
func (nm *NeedleMap) load(file *os.File) { |
|
||||
|
return &NeedleMap{m: make(map[uint64]*NeedleValue), bytes: make([]byte, 16)} |
||||
} |
} |
||||
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) { |
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) { |
func (nm *NeedleMap) get(key uint64) (element *NeedleValue, ok bool) { |
||||
element, ok = nm.m[makeKey(key)] |
|
||||
|
element, ok = nm.m[key] |
||||
return |
return |
||||
} |
} |
||||
|
func (nm *NeedleMap) Close() { |
||||
|
nm.indexFile.Close() |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue