Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
buremba committed Aug 11, 2014
1 parent 41e789a commit d81da12
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 5 deletions.
18 changes: 15 additions & 3 deletions hazelcast/src/main/java/com/hazelcast/map/DefaultRecordStore.java
Expand Up @@ -28,7 +28,6 @@
import com.hazelcast.map.record.RecordFactory;
import com.hazelcast.nio.serialization.Data;
import com.hazelcast.nio.serialization.SerializationService;
import com.hazelcast.partition.InternalPartition;
import com.hazelcast.query.impl.IndexService;
import com.hazelcast.query.impl.QueryEntry;
import com.hazelcast.query.impl.QueryableEntry;
Expand All @@ -39,6 +38,7 @@
import com.hazelcast.spi.exception.RetryableHazelcastException;
import com.hazelcast.util.ExceptionUtil;
import com.hazelcast.util.scheduler.EntryTaskScheduler;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -155,6 +155,11 @@ public String getName() {
return name;
}

@Override
public Object remove(Data dataKey) {
return removeInternal(dataKey, true);
}

public void flush() {
Set<Data> keys = new HashSet<Data>();
for (Record record : records.values()) {
Expand Down Expand Up @@ -484,17 +489,24 @@ public void reset() {
resetAccessSequenceNumber();
}

@Override
public Object delete(Data dataKey) {
return removeInternal(dataKey, false);
}

private void resetAccessSequenceNumber() {
lruAccessSequenceNumber = 0L;
}

public Object remove(Data dataKey) {
public Object removeInternal(Data dataKey, boolean loadMissingKeysFromMapStore) {
checkIfLoaded();
Record record = records.get(dataKey);
Object oldValue = null;
if (record == null) {
if (mapContainer.getStore() != null) {
oldValue = mapContainer.getStore().load(mapService.toObject(dataKey));
if (loadMissingKeysFromMapStore) {
oldValue = mapContainer.getStore().load(mapService.toObject(dataKey));
}
if (oldValue != null) {
removeIndex(dataKey);
mapStoreDelete(null, dataKey);
Expand Down
2 changes: 2 additions & 0 deletions hazelcast/src/main/java/com/hazelcast/map/RecordStore.java
Expand Up @@ -103,6 +103,8 @@ public interface RecordStore {

boolean containsValue(Object testValue);

Object delete(Data dataKey);

Object evict(Data key);

Collection<Object> valuesObject();
Expand Down
Expand Up @@ -29,7 +29,7 @@ public DeleteOperation() {
}

public void run() {
success = recordStore.remove(dataKey) != null;
success = recordStore.delete(dataKey) != null;
}

@Override
Expand Down
Expand Up @@ -321,7 +321,7 @@ protected Data removeInternal(Data key) {
}

protected void deleteInternal(Data key) {
RemoveOperation operation = new RemoveOperation(name, key);
DeleteOperation operation = new DeleteOperation(name, key);
invokeOperation(key, operation);
invalidateNearCache(key);
}
Expand Down
Expand Up @@ -1361,6 +1361,44 @@ public void testIssue1085WriteBehindBackupTransactional() throws InterruptedExce
mapStore.awaitStores();
}

@Test
public void testMapStoreLoad_whenCalledDelete() throws Exception {
final FailingLoadMapStore mapStore = new FailingLoadMapStore();

final MapStoreConfig mapStoreConfig = new MapStoreConfig();
mapStoreConfig.setImplementation(mapStore).setWriteDelaySeconds(1);

final Config config = new Config();
config.getMapConfig("test").setBackupCount(0);

final TestHazelcastInstanceFactory instanceFactory = new TestHazelcastInstanceFactory(1);

final IMap<Object, Object> map = instanceFactory.newInstances(config)[0].getMap("test");

try {
map.delete(1);
} catch (IllegalStateException e) {
fail();
}
}

@Test(expected = IllegalStateException.class)
public void testMapStoreLoad_whenCalledRemove() throws Exception {
final FailingLoadMapStore mapStore = new FailingLoadMapStore();

final MapStoreConfig mapStoreConfig = new MapStoreConfig();
mapStoreConfig.setImplementation(mapStore).setWriteDelaySeconds(1);

final Config config = new Config();
config.getMapConfig("test").setBackupCount(0).setMapStoreConfig(mapStoreConfig);

final TestHazelcastInstanceFactory instanceFactory = new TestHazelcastInstanceFactory(1);

final IMap<Object, Object> map = instanceFactory.newInstances(config)[0].getMap("test");

map.remove(1);
}

@Test
public void testWriteBehindSameSecondSameKey() throws Exception {
TestMapStore testMapStore = new TestMapStore(100, 0, 0); // In some cases 2 store operation may happened
Expand Down Expand Up @@ -2209,5 +2247,42 @@ public void storeAll(final Map<K, V> kvMap) {
}
}

class FailingLoadMapStore implements MapStore {
@Override
public void store(Object key, Object value) {

}

@Override
public void storeAll(Map map) {

}

@Override
public void delete(Object key) {

}

@Override
public void deleteAll(Collection keys) {

}

@Override
public Object load(Object key) {
throw new IllegalStateException();
}

@Override
public Map loadAll(Collection keys) {
return null;
}

@Override
public Set loadAllKeys() {
return null;
}
}


}

0 comments on commit d81da12

Please sign in to comment.