2011-12-16 14:51:26 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2011-12-22 04:04:47 +00:00
|
|
|
"log"
|
2011-12-19 05:59:37 +00:00
|
|
|
"os"
|
2012-06-29 07:53:47 +00:00
|
|
|
"pkg/util"
|
2011-12-16 14:51:26 +00:00
|
|
|
)
|
|
|
|
|
2011-12-19 05:59:37 +00:00
|
|
|
type NeedleValue struct {
|
|
|
|
Offset uint32 "Volume offset" //since aligned to 8 bytes, range is 4G*8=32G
|
|
|
|
Size uint32 "Size of the data portion"
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
|
|
|
|
2011-12-19 05:59:37 +00:00
|
|
|
type NeedleMap struct {
|
2011-12-22 04:04:47 +00:00
|
|
|
indexFile *os.File
|
|
|
|
m map[uint64]*NeedleValue //mapping needle key(uint64) to NeedleValue
|
|
|
|
bytes []byte
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
2011-12-19 05:59:37 +00:00
|
|
|
|
2011-12-24 08:40:56 +00:00
|
|
|
func NewNeedleMap(file *os.File) *NeedleMap {
|
|
|
|
nm := &NeedleMap{
|
|
|
|
m: make(map[uint64]*NeedleValue),
|
|
|
|
bytes: make([]byte, 16),
|
|
|
|
indexFile: file,
|
|
|
|
}
|
|
|
|
return nm
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
2011-12-22 04:04:47 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
RowsToRead = 1024
|
|
|
|
)
|
|
|
|
|
|
|
|
func LoadNeedleMap(file *os.File) *NeedleMap {
|
2011-12-24 08:40:56 +00:00
|
|
|
nm := NewNeedleMap(file)
|
2011-12-22 04:04:47 +00:00
|
|
|
bytes := make([]byte, 16*RowsToRead)
|
|
|
|
count, e := nm.indexFile.Read(bytes)
|
|
|
|
if count > 0 {
|
2011-12-24 08:40:56 +00:00
|
|
|
fstat, _ := file.Stat()
|
2012-06-29 07:53:47 +00:00
|
|
|
log.Println("Loading index file", fstat.Name(), "size", fstat.Size())
|
2011-12-22 04:04:47 +00:00
|
|
|
}
|
|
|
|
for count > 0 && e == nil {
|
|
|
|
for i := 0; i < count; i += 16 {
|
2012-06-29 07:53:47 +00:00
|
|
|
key := util.BytesToUint64(bytes[i : i+8])
|
|
|
|
offset := util.BytesToUint32(bytes[i+8 : i+12])
|
|
|
|
size := util.BytesToUint32(bytes[i+12 : i+16])
|
|
|
|
if offset>0 {
|
|
|
|
nm.m[key] = &NeedleValue{util.Offset: offset, Size: size}
|
|
|
|
}else{
|
|
|
|
delete(nm.m, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-22 04:04:47 +00:00
|
|
|
count, e = nm.indexFile.Read(bytes)
|
2012-06-29 07:53:47 +00:00
|
|
|
}
|
2011-12-22 04:04:47 +00:00
|
|
|
return nm
|
2012-06-29 07:53:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (nm *NeedleMap) Put(key uint64, offset uint32, size uint32) (int, error) {
|
|
|
|
nm.m[key] = &NeedleValue{Offset: offset, Size: size}
|
|
|
|
util.Uint64toBytes(nm.bytes[0:8], key)
|
|
|
|
util.Uint32toBytes(nm.bytes[8:12], offset)
|
|
|
|
util.Uint32toBytes(nm.bytes[12:16], size)
|
2011-12-24 08:40:56 +00:00
|
|
|
return nm.indexFile.Write(nm.bytes)
|
2011-12-19 05:59:37 +00:00
|
|
|
}
|
2011-12-24 08:40:56 +00:00
|
|
|
func (nm *NeedleMap) Get(key uint64) (element *NeedleValue, ok bool) {
|
2011-12-22 04:04:47 +00:00
|
|
|
element, ok = nm.m[key]
|
2011-12-19 05:59:37 +00:00
|
|
|
return
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
2011-12-24 08:40:56 +00:00
|
|
|
func (nm *NeedleMap) Delete(key uint64) {
|
2012-06-29 07:53:47 +00:00
|
|
|
delete(nm.m, key)
|
|
|
|
util.Uint64toBytes(nm.bytes[0:8], key)
|
|
|
|
util.Uint32toBytes(nm.bytes[8:12], 0)
|
|
|
|
util.Uint32toBytes(nm.bytes[12:16], 0)
|
2011-12-24 08:40:56 +00:00
|
|
|
nm.indexFile.Write(nm.bytes)
|
|
|
|
}
|
2011-12-22 04:04:47 +00:00
|
|
|
func (nm *NeedleMap) Close() {
|
|
|
|
nm.indexFile.Close()
|
|
|
|
}
|