|
@ -9,7 +9,11 @@ import ( |
|
|
type NeedleMap struct { |
|
|
type NeedleMap struct { |
|
|
indexFile *os.File |
|
|
indexFile *os.File |
|
|
m CompactMap |
|
|
m CompactMap |
|
|
|
|
|
|
|
|
|
|
|
//transient
|
|
|
bytes []byte |
|
|
bytes []byte |
|
|
|
|
|
deletionCounter int |
|
|
|
|
|
fileCounter int |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func NewNeedleMap(file *os.File) *NeedleMap { |
|
|
func NewNeedleMap(file *os.File) *NeedleMap { |
|
@ -40,8 +44,10 @@ func LoadNeedleMap(file *os.File) *NeedleMap { |
|
|
size := util.BytesToUint32(bytes[i+12 : i+16]) |
|
|
size := util.BytesToUint32(bytes[i+12 : i+16]) |
|
|
if offset > 0 { |
|
|
if offset > 0 { |
|
|
nm.m.Set(Key(key), offset, size) |
|
|
nm.m.Set(Key(key), offset, size) |
|
|
|
|
|
nm.fileCounter++ |
|
|
} else { |
|
|
} else { |
|
|
nm.m.Delete(Key(key)) |
|
|
nm.m.Delete(Key(key)) |
|
|
|
|
|
nm.deletionCounter++ |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -55,6 +61,7 @@ func (nm *NeedleMap) Put(key uint64, offset uint32, size uint32) (int, error) { |
|
|
util.Uint64toBytes(nm.bytes[0:8], key) |
|
|
util.Uint64toBytes(nm.bytes[0:8], key) |
|
|
util.Uint32toBytes(nm.bytes[8:12], offset) |
|
|
util.Uint32toBytes(nm.bytes[8:12], offset) |
|
|
util.Uint32toBytes(nm.bytes[12:16], size) |
|
|
util.Uint32toBytes(nm.bytes[12:16], size) |
|
|
|
|
|
nm.fileCounter++ |
|
|
return nm.indexFile.Write(nm.bytes) |
|
|
return 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) { |
|
@ -67,6 +74,7 @@ func (nm *NeedleMap) Delete(key uint64) { |
|
|
util.Uint32toBytes(nm.bytes[8:12], 0) |
|
|
util.Uint32toBytes(nm.bytes[8:12], 0) |
|
|
util.Uint32toBytes(nm.bytes[12:16], 0) |
|
|
util.Uint32toBytes(nm.bytes[12:16], 0) |
|
|
nm.indexFile.Write(nm.bytes) |
|
|
nm.indexFile.Write(nm.bytes) |
|
|
|
|
|
nm.deletionCounter++ |
|
|
} |
|
|
} |
|
|
func (nm *NeedleMap) Close() { |
|
|
func (nm *NeedleMap) Close() { |
|
|
nm.indexFile.Close() |
|
|
nm.indexFile.Close() |
|
|