HCOLL-277 HCOLL-278 HCOLL-289 Fixed two bugs in VanillaChronicleMap.putValue()

This commit is contained in:
leventov
2015-01-03 01:56:49 +07:00
parent cbf727ae7a
commit db594bfb64
7 changed files with 27 additions and 13 deletions
@@ -239,9 +239,12 @@ class Int24Int24MultiMap implements MultiMap {
@Override
public void replacePrevPos(SearchState searchState, long newValue) {
checkValueForPut(newValue);
long prevPos = stepBack(searchState.searchPos);
long prevPos = searchState.searchPos;
if (!searchState.putAfterFailedSearch)
prevPos = stepBack(prevPos);
long oldEntry = bytes.readInt48(prevPos);
long oldValue = value(oldEntry);
checkValueForRemove(oldValue);
positions.clear(oldValue);
positions.set(newValue);
bytes.writeInt48(prevPos, entry(searchState.searchHash, newValue));
@@ -240,9 +240,12 @@ class IntIntMultiMap implements MultiMap {
@Override
public void replacePrevPos(SearchState searchState, long newValue) {
checkValueForPut(newValue);
long prevPos = stepBack(searchState.searchPos);
long prevPos = searchState.searchPos;
if (!searchState.putAfterFailedSearch)
prevPos = stepBack(prevPos);
long oldEntry = bytes.readLong(prevPos);
long oldValue = value(oldEntry);
checkValueForRemove(oldValue);
positions.clear(oldValue);
positions.set(newValue);
bytes.writeLong(prevPos, entry(searchState.searchHash, newValue));
@@ -19,4 +19,5 @@ package net.openhft.chronicle.map;
final class SearchState {
long searchHash = -1L;
long searchPos = -1L;
boolean putAfterFailedSearch = false;
}
@@ -206,6 +206,7 @@ class ShortShortMultiMap implements MultiMap {
key = maskUnsetKey(key);
searchStateToReuse.searchPos = pos(key);
searchStateToReuse.searchHash = key;
searchStateToReuse.putAfterFailedSearch = false;
}
@Override
@@ -237,9 +238,12 @@ class ShortShortMultiMap implements MultiMap {
@Override
public void replacePrevPos(SearchState searchState, long newValue) {
checkValueForPut(newValue);
long prevPos = stepBack(searchState.searchPos);
long prevPos = searchState.searchPos;
if (!searchState.putAfterFailedSearch)
prevPos = stepBack(prevPos);
int oldEntry = bytes.readInt(prevPos);
long oldValue = value(oldEntry);
checkValueForRemove(oldValue);
positions.clear(oldValue);
positions.set(newValue);
bytes.writeInt(prevPos, entry(searchState.searchHash, newValue));
@@ -250,6 +254,7 @@ class ShortShortMultiMap implements MultiMap {
checkValueForPut(value);
positions.set(value);
bytes.writeInt(searchState.searchPos, entry(searchState.searchHash, value));
searchState.putAfterFailedSearch = true;
}
@Override
@@ -2348,26 +2348,26 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
onRelocation(this, pos);
int allocatedChunks =
inChunks(innerEntrySize(sizeOfEverythingBeforeValue, newElemSize));
pos = alloc(allocatedChunks);
long newPos = alloc(allocatedChunks);
// putValue() is called from put() and replace()
// after successful search by key
searchedHashLookup.replacePrevPos(segmentState.searchState, pos);
offset = offsetFromPos(pos);
searchedHashLookup.replacePrevPos(segmentState.searchState, newPos);
long newOffset = offsetFromPos(newPos);
reuse(entry, newOffset);
// Moving metadata, key size and key.
// Don't want to fiddle with pseudo-buffers for this,
// since we already have all absolute addresses.
long newEntryStartAddr = entry.address();
NativeBytes.UNSAFE.copyMemory(entryStartAddr,
newEntryStartAddr, valueSizeAddr - entryStartAddr);
entry = reuse(entry, offset);
entry.position(valueSizePos);
valueSizeMarshaller.writeSize(entry, newElemSize);
alignment.alignPositionAddr(entry);
metaElemWriter.write(elemWriter, entry, newElem);
freeExtraAllocatedChunks(pos, allocatedChunks, entry);
freeExtraAllocatedChunks(newPos, allocatedChunks, entry);
segmentState.pos = pos;
segmentState.pos = newPos;
return offset;
// END OF RELOCATION
} else if (newSizeInChunks < oldSizeInChunks) {