/src/com/google/appengine/datanucleus/query/StreamingQueryResult.java

http://datanucleus-appengine.googlecode.com/ · Java · 157 lines · 88 code · 26 blank · 43 comment · 11 complexity · b1d075aa942f8df4d916ded60bdb2793 MD5 · raw file

  1. /**********************************************************************
  2. Copyright (c) 2009 Google Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. **********************************************************************/
  13. package com.google.appengine.datanucleus.query;
  14. import com.google.appengine.api.datastore.Cursor;
  15. import com.google.appengine.api.datastore.Entity;
  16. import org.datanucleus.exceptions.NucleusUserException;
  17. import com.google.appengine.datanucleus.Utils.Function;
  18. import org.datanucleus.store.query.AbstractQueryResult;
  19. import org.datanucleus.store.query.Query;
  20. import org.datanucleus.util.NucleusLogger;
  21. import java.util.Iterator;
  22. import java.util.ListIterator;
  23. import java.util.NoSuchElementException;
  24. import javax.jdo.JDOUserException;
  25. /**
  26. * An {@link AbstractQueryResult} implementation that streams results, converting
  27. * from {@link Entity Entities} to POJOs as clients access the data.
  28. *
  29. * @author Max Ross <maxr@google.com>
  30. */
  31. class StreamingQueryResult extends AbstractQueryResult {
  32. /** Delegate for lazy loading of results. */
  33. private final LazyResult<Object> lazyResult;
  34. private boolean loadResultsAtCommit = true;
  35. private final Cursor endCursor;
  36. private boolean hasError;
  37. private RuntimeExceptionWrappingIterable inputIterable;
  38. /**
  39. * Constructs a StreamingQueryResult.
  40. * @param query The query which yields the results.
  41. * @param lazyEntities The result of the query.
  42. * @param entityToPojoFunc A function that can convert a {@link Entity} into a pojo.
  43. * @param endCursor Provides a cursor that points to the end of the result set. Can be null.
  44. */
  45. public StreamingQueryResult(Query query, Iterable<Entity> lazyEntities,
  46. Function<Entity, Object> entityToPojoFunc, Cursor endCursor) {
  47. super(query);
  48. if (lazyEntities instanceof RuntimeExceptionWrappingIterable) {
  49. this.inputIterable = (RuntimeExceptionWrappingIterable) lazyEntities;
  50. }
  51. this.lazyResult = new LazyResult<Object>(lazyEntities, entityToPojoFunc, query.useResultsCaching());
  52. this.endCursor = endCursor;
  53. }
  54. @Override
  55. public void disconnect() {
  56. if (inputIterable != null) {
  57. // Relay the iterable error out
  58. this.hasError = inputIterable.hasError();
  59. }
  60. super.disconnect();
  61. }
  62. @Override
  63. protected void closingConnection() {
  64. // Connection is being closed so last chance to grab any results not yet loaded
  65. if (loadResultsAtCommit && isOpen()) {
  66. if (hasError) {
  67. NucleusLogger.QUERY.info("Skipping resolution of remaining results due to earlier error.");
  68. } else {
  69. try {
  70. // If we are still open, force consumption of the rest of the results
  71. lazyResult.resolveAll();
  72. // TODO Get rid of this selective exception swallowing. Makes no sense
  73. } catch (NucleusUserException jpue) {
  74. // Log any exception - can get exceptions when maybe the user has specified an invalid result class etc
  75. NucleusLogger.QUERY.warn("Exception thrown while loading remaining rows of query : " + jpue.getMessage());
  76. } catch (JDOUserException ue) {
  77. // Log any exception - can get exceptions when maybe the user has specified an invalid result class etc
  78. NucleusLogger.QUERY.warn("Exception thrown while loading remaining rows of query : " + ue.getMessage());
  79. }
  80. }
  81. // Cache the query results (if required)
  82. cacheQueryResults();
  83. }
  84. }
  85. @Override
  86. protected void closeResults() {
  87. // Cache the query results (if required)
  88. cacheQueryResults();
  89. }
  90. /**
  91. * Method to cache the results (List of the Entity keys) if it has been requested.
  92. */
  93. protected void cacheQueryResults() {
  94. if (query != null && query.useResultsCaching()) {
  95. lazyResult.resolveAll();
  96. query.getQueryManager().addDatastoreQueryResult(query, query.getInputParameters(), lazyResult.getEntityKeys());
  97. }
  98. }
  99. @Override
  100. public boolean equals(Object o) {
  101. return this == o;
  102. }
  103. @Override
  104. public Object get(int index) {
  105. return lazyResult.get(index);
  106. }
  107. /**
  108. * @throws NoSuchElementException if there are no more elements to resolve.
  109. */
  110. void resolveNext() {
  111. lazyResult.resolveNext();
  112. }
  113. @Override
  114. public Iterator<Object> iterator() {
  115. return lazyResult.listIterator();
  116. }
  117. @Override
  118. public ListIterator<Object> listIterator() {
  119. return lazyResult.listIterator();
  120. }
  121. @Override
  122. public int size() {
  123. return lazyResult.size();
  124. }
  125. Cursor getEndCursor() {
  126. return endCursor;
  127. }
  128. }