Skip to content

Commit

Permalink
Closes #310.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdogan committed Apr 26, 2013
1 parent ba89356 commit bb05f57
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
6 changes: 5 additions & 1 deletion hazelcast/src/main/java/com/hazelcast/impl/MProxyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -517,15 +517,19 @@ public void addIndex(final String attribute, final boolean ordered) {

public void addIndex(final Expression expression, final boolean ordered) {
final CountDownLatch latch = new CountDownLatch(1);
final AddMapIndex addMapIndexProcess = new AddMapIndex(name, expression, ordered);
concurrentMapManager.enqueueAndReturn(new Processable() {
public void process() {
AddMapIndex addMapIndexProcess = new AddMapIndex(name, expression, ordered);
concurrentMapManager.sendProcessableToAll(addMapIndexProcess, true);
latch.countDown();
}
});
try {
latch.await();
final Throwable error = addMapIndexProcess.getError();
if (error != null) {
Util.throwUncheckedException(error);
}
} catch (InterruptedException ignored) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.logging.Level;

public class AddMapIndex extends AbstractRemotelyProcessable {

Expand All @@ -31,6 +32,8 @@ public class AddMapIndex extends AbstractRemotelyProcessable {
private boolean ordered;
private int attributeIndex = -1;

private transient Throwable error;

public AddMapIndex() {
}

Expand All @@ -48,8 +51,13 @@ public AddMapIndex(String mapName, Expression expression, boolean ordered, int a
}

public void process() {
CMap cmap = getNode().concurrentMapManager.getOrCreateMap(mapName);
cmap.addIndex(getExpression(), isOrdered(), attributeIndex);
CMap cmap = node.concurrentMapManager.getOrCreateMap(mapName);
try {
cmap.addIndex(getExpression(), isOrdered(), attributeIndex);
} catch (Exception e) {
error = e;
node.getLogger(getClass().getName()).log(Level.WARNING, e.getMessage());
}
}

public void writeData(DataOutput out) throws IOException {
Expand Down Expand Up @@ -101,4 +109,8 @@ public int getAttributeIndex() {
public void setAttributeIndex(int attributeIndex) {
this.attributeIndex = attributeIndex;
}

public Throwable getError() {
return error;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@
package com.hazelcast.impl.concurrentmap;

public class QueryException extends RuntimeException {

public QueryException() {
}

public QueryException(String message) {
super(message);
}

public QueryException(Throwable cause) {
super(cause);
}

@Override
public String getMessage() {
return getCause().getMessage();
final Throwable cause = getCause();
return cause != null ? cause.getMessage() : super.getMessage();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.hazelcast.core.MapEntry;
import com.hazelcast.impl.Record;
import com.hazelcast.impl.concurrentmap.QueryException;
import com.hazelcast.nio.Data;

import java.util.*;
Expand Down Expand Up @@ -133,9 +134,8 @@ public Index addIndex(Expression expression, boolean ordered, int attributeIndex
if (index == null) {
if (size() > 0) {
StringBuilder sb = new StringBuilder("Index can only be added before adding entries!");
sb.append("\n");
sb.append("Add indexes first and only once then put entries.");
throw new RuntimeException(sb.toString());
sb.append(" Add indexes first and only once then put entries.");
throw new QueryException(sb.toString());
}
if (attributeIndex == -1) {
attributeIndex = mapIndexes.size();
Expand Down
14 changes: 14 additions & 0 deletions hazelcast/src/test/java/com/hazelcast/query/QueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.hazelcast.impl.GroupProperties;
import com.hazelcast.impl.NodeType;
import com.hazelcast.impl.TestUtil;
import com.hazelcast.impl.concurrentmap.QueryException;
import com.hazelcast.util.Clock;
import org.junit.After;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -1191,4 +1192,17 @@ public void testOneMemberWithIndexAndOneMemberWithout() {
assertTrue(map1.isEmpty());
assertTrue(map2.isEmpty());
}

@Test(expected = QueryException.class)
public void testAddMapIndexAfterAddingEntry() throws Exception {
HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance();
IMap<String, String> testMap = hazelcast.getMap("testAddMapIndexAfterAddingEntry");
testMap.put("key", "value");
try {
testMap.addIndex("this", false);
} finally {
assertNotNull("Code should reach here!", testMap.get("key"));
}
}

}

0 comments on commit bb05f57

Please sign in to comment.