forked from objectify/objectify
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ignore no-op load/save/delete actions properly
* the goal is to avoid the small, but potentially frequently executed calls to save CPU * removal of proxy creation in favor of forwarding list/map
- Loading branch information
Showing
14 changed files
with
244 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ objectify.iml | |
.svn | ||
.gwt | ||
.gradle | ||
.factorypath |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 38 additions & 22 deletions
60
src/main/java/com/googlecode/objectify/util/ResultProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,57 @@ | ||
package com.googlecode.objectify.util; | ||
|
||
import com.google.common.collect.ForwardingList; | ||
import com.google.common.collect.ForwardingMap; | ||
import com.googlecode.objectify.Result; | ||
|
||
import java.io.ObjectStreamException; | ||
import java.io.Serializable; | ||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Proxy; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
/** | ||
* A dynamic proxy that wraps a Result<?> value. For example, if you had a Result<List<String>>, the | ||
* A utility class that wraps a Result<?> value. For example, if you had a Result<List<String>>, the | ||
* proxy would implement List<String> and call through to the inner object. | ||
*/ | ||
public class ResultProxy<T> implements InvocationHandler, Serializable | ||
public class ResultProxy<T> | ||
{ | ||
/** | ||
* Create a ResultProxy for the given interface. | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static <S> S create(Class<? super S> interf, Result<S> result) { | ||
return (S)Proxy.newProxyInstance(result.getClass().getClassLoader(), new Class[] { interf }, new ResultProxy<>(result)); | ||
public static <K, V> Map<K, V> createMap(final Result<Map<K, V>> result) { | ||
return new ResultProxyMap<K, V>(result); | ||
} | ||
|
||
Result<T> result; | ||
public static <E> List<E> createList(final Result<List<E>> result) { | ||
return new ResultProxyList<E>(result); | ||
} | ||
|
||
@RequiredArgsConstructor | ||
private static class ResultProxyList<E> extends ForwardingList<E> implements Serializable { | ||
|
||
private final Result<List<E>> result; | ||
|
||
@Override | ||
protected List<E> delegate() { | ||
return result.now(); | ||
} | ||
|
||
private ResultProxy(Result<T> result) { | ||
this.result = result; | ||
private Object writeReplace() throws ObjectStreamException { | ||
return delegate(); | ||
} | ||
} | ||
|
||
@Override | ||
public Object invoke(Object obj, Method meth, Object[] params) throws Throwable { | ||
return meth.invoke(result.now(), params); | ||
@RequiredArgsConstructor | ||
private static class ResultProxyMap<K, V> extends ForwardingMap<K, V> implements Serializable { | ||
|
||
private final Result<Map<K, V>> result; | ||
|
||
@Override | ||
protected Map<K, V> delegate() { | ||
return result.now(); | ||
} | ||
|
||
private Object writeReplace() throws ObjectStreamException { | ||
return delegate(); | ||
} | ||
} | ||
|
||
private Object writeReplace() throws ObjectStreamException { | ||
return new NowProxy<>(result.now()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/test/java/com/googlecode/objectify/test/IgnoreEmptyOperationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.googlecode.objectify.test; | ||
|
||
import static com.googlecode.objectify.ObjectifyService.factory; | ||
import static com.googlecode.objectify.ObjectifyService.ofy; | ||
|
||
import java.util.Arrays; | ||
|
||
import org.mockito.Mockito; | ||
import org.testng.annotations.Test; | ||
|
||
import com.google.appengine.api.datastore.DatastoreServiceConfig; | ||
import com.google.common.collect.Lists; | ||
import com.googlecode.objectify.Key; | ||
import com.googlecode.objectify.test.entity.Trivial; | ||
import com.googlecode.objectify.test.util.TestBase; | ||
import com.googlecode.objectify.test.util.TestObjectifyFactory; | ||
|
||
public class IgnoreEmptyOperationTests extends TestBase { | ||
|
||
@Override | ||
protected void setUpObjectifyFactory(TestObjectifyFactory factory) { | ||
factory.register(Trivial.class); | ||
super.setUpObjectifyFactory(Mockito.spy(factory)); | ||
Mockito.verify(factory(), Mockito.times(1)).begin(); | ||
} | ||
|
||
// engines are responsible for datastore actions, and because all of them requires an AsyncDatastoreService | ||
// it seems like a good idea to verify if it has been created or not | ||
private void verifyNoRemoteCall() { | ||
Mockito.verify(factory(), Mockito.never()) | ||
.createAsyncDatastoreService(Mockito.any(DatastoreServiceConfig.class), Mockito.anyBoolean()); | ||
Mockito.verifyNoMoreInteractions(factory()); | ||
} | ||
|
||
private void verifyThatRemoteCallWasExecuted(int times) { | ||
Mockito.verify(factory(), Mockito.times(times)) | ||
.createAsyncDatastoreService(Mockito.any(DatastoreServiceConfig.class), Mockito.anyBoolean()); | ||
} | ||
|
||
@Test | ||
public void testEmptySave() { | ||
ofy().save().entities(Lists.newArrayList()).now().size(); | ||
|
||
verifyNoRemoteCall(); | ||
} | ||
|
||
@Test | ||
public void testNotEmptySave() { | ||
ofy().save().entities(Arrays.asList(new Trivial())).now().size(); | ||
|
||
verifyThatRemoteCallWasExecuted(1); | ||
} | ||
|
||
@Test | ||
public void testEmptyDelete() { | ||
ofy().delete().entities(Lists.newArrayList()).now(); | ||
ofy().delete().keys(Lists.<Key<Trivial>> newArrayList()).now(); | ||
ofy().delete().type(Trivial.class).ids(Lists.newArrayList()).now(); | ||
|
||
verifyNoRemoteCall(); | ||
} | ||
|
||
@Test | ||
public void testNotEmptyDelete() { | ||
final Trivial trivial = new Trivial(1L, "", 1L); | ||
ofy().delete().entities(Arrays.asList(trivial)).now(); | ||
ofy().delete().keys(Arrays.asList(Key.create(trivial))).now(); | ||
ofy().delete().type(Trivial.class).ids(Arrays.asList(trivial.getId())).now(); | ||
|
||
verifyThatRemoteCallWasExecuted(3); | ||
} | ||
|
||
@Test | ||
public void testEmptyLoad() { | ||
ofy().load().entities(Lists.newArrayList()).size(); | ||
ofy().load().keys(Lists.<Key<Trivial>> newArrayList()).size(); | ||
ofy().load().type(Trivial.class).ids(Lists.newArrayList()).size(); | ||
|
||
verifyNoRemoteCall(); | ||
} | ||
|
||
@Test | ||
public void testNotEmptyLoad() { | ||
final Trivial trivial = new Trivial(1L, "", 1L); | ||
ofy().load().entities(Arrays.asList(trivial)).size(); | ||
ofy().load().keys(Arrays.asList(Key.<Trivial> create(trivial))).size(); | ||
ofy().load().type(Trivial.class).ids(Arrays.asList(trivial.getId())).size(); | ||
|
||
verifyThatRemoteCallWasExecuted(3); | ||
} | ||
} |
Oops, something went wrong.