Skip to content

Commit

Permalink
avoid data loss on update for IndexedMaps + renamed replace => update
Browse files Browse the repository at this point in the history
  • Loading branch information
codingchili committed Nov 11, 2017
1 parent a6e5d36 commit 5eff025
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ public static String getStorageLoaderMissingArgument(String type) {
return "storage loader is missing argument for attribute '" + type + "'.";
}

public static String getNothingToReplaceException(String key) {
public static String getNothingToUpdateException(String key) {
return "Error: nothing to replace for given key '" + key + "'.";
}

Expand Down
4 changes: 2 additions & 2 deletions core/main/java/com/codingchili/core/storage/ElasticMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ public void update(Value value, Handler<AsyncResult<Void>> handler) {
if (response.getResult().ordinal() != 0) {
handler.handle(result());
} else {
handler.handle(error(new NothingToReplaceException(value.id())));
handler.handle(error(new NothingToUpdateException(value.id())));
}
}, exception -> {
if (nested(exception) instanceof DocumentMissingException) {
handler.handle(error(new NothingToReplaceException(value.id())));
handler.handle(error(new NothingToUpdateException(value.id())));
} else {
handler.handle(error(exception));
}
Expand Down
4 changes: 2 additions & 2 deletions core/main/java/com/codingchili/core/storage/HazelMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.codingchili.core.context.FutureHelper;
import com.codingchili.core.context.StorageContext;
import com.codingchili.core.storage.exception.NothingToRemoveException;
import com.codingchili.core.storage.exception.NothingToReplaceException;
import com.codingchili.core.storage.exception.NothingToUpdateException;
import com.codingchili.core.storage.exception.ValueAlreadyPresentException;
import com.codingchili.core.storage.exception.ValueMissingException;
import com.hazelcast.core.Hazelcast;
Expand Down Expand Up @@ -123,7 +123,7 @@ public void update(Value value, Handler<AsyncResult<Void>> handler) {
map.replace(value.id(), value, replace -> {
if (replace.succeeded()) {
if (replace.result() == null) {
handler.handle(error(new NothingToReplaceException(value.id())));
handler.handle(error(new NothingToUpdateException(value.id())));
} else {
handler.handle(FutureHelper.result());
}
Expand Down
10 changes: 5 additions & 5 deletions core/main/java/com/codingchili/core/storage/IndexedMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.codingchili.core.context.StorageContext;
import com.codingchili.core.protocol.Serializer;
import com.codingchili.core.storage.exception.NothingToRemoveException;
import com.codingchili.core.storage.exception.NothingToReplaceException;
import com.codingchili.core.storage.exception.NothingToUpdateException;
import com.codingchili.core.storage.exception.ValueAlreadyPresentException;
import com.codingchili.core.storage.exception.ValueMissingException;
import com.googlecode.cqengine.attribute.Attribute;
Expand Down Expand Up @@ -121,11 +121,11 @@ public void remove(String key, Handler<AsyncResult<Void>> handler) {

@Override
public void update(Value value, Handler<AsyncResult<Void>> handler) {
remove(value.id(), removed -> {
if (removed.succeeded()) {
put(value, handler);
get(value.id(), get -> {
if (get.succeeded() && db.update(Collections.singleton(get.result()), Collections.singleton(value))) {
handler.handle(succeededFuture());
} else {
handler.handle(failedFuture(new NothingToReplaceException(value.id())));
handler.handle(failedFuture(new NothingToUpdateException(value.id())));
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions core/main/java/com/codingchili/core/storage/JsonMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.codingchili.core.files.exception.NoSuchResourceException;
import com.codingchili.core.logging.Logger;
import com.codingchili.core.storage.exception.NothingToRemoveException;
import com.codingchili.core.storage.exception.NothingToReplaceException;
import com.codingchili.core.storage.exception.NothingToUpdateException;
import com.codingchili.core.storage.exception.ValueAlreadyPresentException;
import com.codingchili.core.storage.exception.ValueMissingException;
import io.vertx.core.AsyncResult;
Expand Down Expand Up @@ -135,7 +135,7 @@ public void update(Value value, Handler<AsyncResult<Void>> handler) {
put(value);
handler.handle(FutureHelper.result());
} else {
handler.handle(error(new NothingToReplaceException(value.id())));
handler.handle(error(new NothingToUpdateException(value.id())));
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/main/java/com/codingchili/core/storage/MongoDBMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.codingchili.core.protocol.Serializer;
import com.codingchili.core.security.Validator;
import com.codingchili.core.storage.exception.NothingToRemoveException;
import com.codingchili.core.storage.exception.NothingToReplaceException;
import com.codingchili.core.storage.exception.NothingToUpdateException;
import com.codingchili.core.storage.exception.ValueAlreadyPresentException;
import com.codingchili.core.storage.exception.ValueMissingException;
import io.vertx.core.AsyncResult;
Expand Down Expand Up @@ -134,7 +134,7 @@ public void update(Value value, Handler<AsyncResult<Void>> handler) {
if (replace.result().getDocModified() > 0) {
handler.handle(FutureHelper.result());
} else {
handler.handle(error(new NothingToReplaceException(value.id())));
handler.handle(error(new NothingToUpdateException(value.id())));
}
} else {
handler.handle(error(replace.cause()));
Expand Down
4 changes: 2 additions & 2 deletions core/main/java/com/codingchili/core/storage/PrivateMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.codingchili.core.context.FutureHelper;
import com.codingchili.core.context.StorageContext;
import com.codingchili.core.storage.exception.NothingToRemoveException;
import com.codingchili.core.storage.exception.NothingToReplaceException;
import com.codingchili.core.storage.exception.NothingToUpdateException;
import com.codingchili.core.storage.exception.ValueAlreadyPresentException;
import com.codingchili.core.storage.exception.ValueMissingException;
import io.vertx.core.AsyncResult;
Expand Down Expand Up @@ -91,7 +91,7 @@ public void update(Value value, Handler<AsyncResult<Void>> handler) {
map.put(value.id(), value);
handler.handle(FutureHelper.result());
} else {
handler.handle(error(new NothingToReplaceException(value.id())));
handler.handle(error(new NothingToUpdateException(value.id())));
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/main/java/com/codingchili/core/storage/SharedMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.codingchili.core.context.FutureHelper;
import com.codingchili.core.context.StorageContext;
import com.codingchili.core.storage.exception.NothingToRemoveException;
import com.codingchili.core.storage.exception.NothingToReplaceException;
import com.codingchili.core.storage.exception.NothingToUpdateException;
import com.codingchili.core.storage.exception.ValueAlreadyPresentException;
import com.codingchili.core.storage.exception.ValueMissingException;
import io.vertx.core.AsyncResult;
Expand Down Expand Up @@ -82,7 +82,7 @@ public void update(Value value, Handler<AsyncResult<Void>> handler) {
if (map.replace(value.id(), value) != null) {
handler.handle(FutureHelper.result());
} else {
handler.handle(error(new NothingToReplaceException(value.id())));
handler.handle(error(new NothingToUpdateException(value.id())));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
/**
* @author Robin Duda
* <p>
* Throw when the replace operation cannot complete as there is nothing to replace.
* Throw when the replace operation cannot complete as there is no object stored
* that matches the value of the oject to update.
*/
public class NothingToReplaceException extends CoreException {
public class NothingToUpdateException extends CoreException {

public NothingToReplaceException(Object key) {
super(CoreStrings.getNothingToReplaceException(key.toString()), ResponseStatus.MISSING);
public NothingToUpdateException(String key) {
super(CoreStrings.getNothingToUpdateException(key), ResponseStatus.MISSING);
}
}
14 changes: 7 additions & 7 deletions core/main/java/com/codingchili/core/testing/MapTestCases.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.codingchili.core.storage.SortOrder;
import com.codingchili.core.storage.StorageLoader;
import com.codingchili.core.storage.exception.NothingToRemoveException;
import com.codingchili.core.storage.exception.NothingToReplaceException;
import com.codingchili.core.storage.exception.NothingToUpdateException;
import com.codingchili.core.storage.exception.ValueAlreadyPresentException;
import com.codingchili.core.storage.exception.ValueMissingException;
import io.vertx.core.AsyncResult;
Expand Down Expand Up @@ -237,30 +237,30 @@ public void testRemoveNotPresent(TestContext test) {
}

@Test
public void testReplace(TestContext test) {
public void testUpdate(TestContext test) {
Async async = test.async();
int level = 50;
StorageObject replacement = new StorageObject(OBJECT_TWO.id(), level);
StorageObject updated = new StorageObject(OBJECT_TWO.id(), level);

store.update(replacement, replace -> {
store.update(updated, replace -> {
test.assertTrue(replace.succeeded(), errorText(replace));

store.get(TWO, get -> {
test.assertTrue(get.succeeded(), errorText(get));
test.assertEquals(replacement.getLevel(), get.result().getLevel());
test.assertEquals(updated.getLevel(), get.result().getLevel());
async.complete();
});
});
}

@Test
public void testReplaceIfNotPresent(TestContext test) {
public void testUpdateIfNotePresent(TestContext test) {
Async async = test.async();
StorageObject object = new StorageObject(NAME_MISSING, 0);

store.update(object, replace -> {
test.assertTrue(replace.failed());
test.assertEquals(NothingToReplaceException.class, replace.cause().getClass());
test.assertEquals(NothingToUpdateException.class, replace.cause().getClass());
async.complete();
});
}
Expand Down

0 comments on commit 5eff025

Please sign in to comment.