|
@ -1,6 +1,7 @@ |
|
|
package storage |
|
|
package storage |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
|
|
|
"errors" |
|
|
"io" |
|
|
"io" |
|
|
"log" |
|
|
"log" |
|
|
"os" |
|
|
"os" |
|
@ -60,10 +61,16 @@ func NewFrozenNeedleMap(file *os.File) (*NeedleMap, error) { |
|
|
}, nil |
|
|
}, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (nm NeedleMap) IsFrozen() bool { |
|
|
|
|
|
return nm.m == nil && nm.fm != nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
const ( |
|
|
const ( |
|
|
RowsToRead = 1024 |
|
|
RowsToRead = 1024 |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
var MapIsFrozen = errors.New("Map is frozen!") |
|
|
|
|
|
|
|
|
func LoadNeedleMap(file *os.File) (*NeedleMap, error) { |
|
|
func LoadNeedleMap(file *os.File) (*NeedleMap, error) { |
|
|
nm := NewNeedleMap(file) |
|
|
nm := NewNeedleMap(file) |
|
|
|
|
|
|
|
@ -124,6 +131,9 @@ func readIndexFile(indexFile *os.File, iterFun func([]byte) error) error { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (nm *NeedleMap) Put(key uint64, offset uint32, size uint32) (int, error) { |
|
|
func (nm *NeedleMap) Put(key uint64, offset uint32, size uint32) (int, error) { |
|
|
|
|
|
if nm.IsFrozen() { |
|
|
|
|
|
return 0, MapIsFrozen |
|
|
|
|
|
} |
|
|
oldSize := nm.m.Set(Key(key), offset, size) |
|
|
oldSize := nm.m.Set(Key(key), offset, size) |
|
|
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) |
|
@ -140,13 +150,17 @@ func (nm *NeedleMap) Get(key uint64) (element *NeedleValue, ok bool) { |
|
|
element, ok = nm.m.Get(Key(key)) |
|
|
element, ok = nm.m.Get(Key(key)) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
func (nm *NeedleMap) Delete(key uint64) { |
|
|
|
|
|
|
|
|
func (nm *NeedleMap) Delete(key uint64) error { |
|
|
|
|
|
if nm.IsFrozen() { |
|
|
|
|
|
return MapIsFrozen |
|
|
|
|
|
} |
|
|
nm.deletionByteCounter = nm.deletionByteCounter + uint64(nm.m.Delete(Key(key))) |
|
|
nm.deletionByteCounter = nm.deletionByteCounter + uint64(nm.m.Delete(Key(key))) |
|
|
util.Uint64toBytes(nm.bytes[0:8], key) |
|
|
util.Uint64toBytes(nm.bytes[0:8], key) |
|
|
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++ |
|
|
nm.deletionCounter++ |
|
|
|
|
|
return nil |
|
|
} |
|
|
} |
|
|
func (nm *NeedleMap) Close() { |
|
|
func (nm *NeedleMap) Close() { |
|
|
nm.indexFile.Close() |
|
|
nm.indexFile.Close() |
|
|