PageRenderTime 54ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/gemfire-core/src/main/java/com/gemstone/gemfire/cache/client/internal/QueryOp.java

https://gitlab.com/kidaa/incubator-geode
Java | 194 lines | 148 code | 7 blank | 39 comment | 28 complexity | 5fd4ec80f961e08633b84b1a06452b30 MD5 | raw file
  1. /*=========================================================================
  2. * Copyright (c) 2002-2014 Pivotal Software, Inc. All Rights Reserved.
  3. * This product is protected by U.S. and international copyright
  4. * and intellectual property laws. Pivotal products are covered by
  5. * more patents listed at http://www.pivotal.io/patents.
  6. *=========================================================================
  7. */
  8. package com.gemstone.gemfire.cache.client.internal;
  9. import java.util.Arrays;
  10. import com.gemstone.gemfire.internal.Version;
  11. import com.gemstone.gemfire.internal.cache.tier.MessageType;
  12. import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
  13. import com.gemstone.gemfire.internal.cache.tier.sockets.ChunkedMessage;
  14. import com.gemstone.gemfire.internal.cache.tier.sockets.ObjectPartList;
  15. import com.gemstone.gemfire.internal.cache.tier.sockets.Part;
  16. import com.gemstone.gemfire.cache.query.SelectResults;
  17. import com.gemstone.gemfire.cache.query.types.CollectionType;
  18. import com.gemstone.gemfire.cache.query.types.ObjectType;
  19. import com.gemstone.gemfire.cache.query.internal.QueryUtils;
  20. import com.gemstone.gemfire.cache.query.internal.StructImpl;
  21. import com.gemstone.gemfire.cache.query.internal.types.StructTypeImpl;
  22. import com.gemstone.gemfire.cache.query.internal.types.TypeUtils;
  23. import com.gemstone.gemfire.SerializationException;
  24. import com.gemstone.gemfire.cache.client.ServerOperationException;
  25. /**
  26. * Does a region query on a server
  27. * @author darrel
  28. * @since 5.7
  29. */
  30. public class QueryOp {
  31. /**
  32. * Does a region query on a server using connections from the given pool
  33. * to communicate with the server.
  34. * @param pool the pool to use to communicate with the server.
  35. * @param queryPredicate A query language boolean query predicate
  36. * @return A <code>SelectResults</code> containing the values
  37. * that match the <code>queryPredicate</code>.
  38. */
  39. public static SelectResults execute(ExecutablePool pool, String queryPredicate,
  40. Object[] queryParams)
  41. {
  42. AbstractOp op = null;
  43. if (queryParams != null && queryParams.length > 0) {
  44. op = new QueryOpImpl(queryPredicate, queryParams);
  45. } else {
  46. op = new QueryOpImpl(queryPredicate);
  47. }
  48. return (SelectResults)pool.execute(op);
  49. }
  50. private QueryOp() {
  51. // no instances allowed
  52. }
  53. /**
  54. * Note: this class is extended by CreateCQWithIROpImpl.
  55. */
  56. protected static class QueryOpImpl extends AbstractOp {
  57. /**
  58. * @throws com.gemstone.gemfire.SerializationException if serialization fails
  59. */
  60. public QueryOpImpl(String queryPredicate) {
  61. super(MessageType.QUERY, 1);
  62. getMessage().addStringPart(queryPredicate);
  63. }
  64. /**
  65. * @throws com.gemstone.gemfire.SerializationException if serialization fails
  66. */
  67. public QueryOpImpl(String queryPredicate, Object[] queryParams) {
  68. super(MessageType.QUERY_WITH_PARAMETERS, 2 + queryParams.length);
  69. getMessage().addStringPart(queryPredicate);
  70. getMessage().addIntPart(queryParams.length);
  71. for (Object param : queryParams){
  72. getMessage().addObjPart(param);
  73. }
  74. }
  75. /**
  76. * This constructor is used by our subclass CreateCQWithIROpImpl
  77. * @throws com.gemstone.gemfire.SerializationException if serialization fails
  78. */
  79. protected QueryOpImpl(int msgType, int numParts) {
  80. super(msgType, numParts);
  81. }
  82. @Override
  83. protected Message createResponseMessage() {
  84. return new ChunkedMessage(2, Version.CURRENT);
  85. }
  86. @Override
  87. protected Object processResponse(Message msg) throws Exception {
  88. final SelectResults[] resultRef = new SelectResults[1];
  89. final Exception[] exceptionRef = new Exception[1];
  90. ChunkHandler ch = new ChunkHandler() {
  91. public void handle(ChunkedMessage cm) throws Exception {
  92. Part collectionTypePart = cm.getPart(0);
  93. Object o = collectionTypePart.getObject();
  94. if (o instanceof Throwable) {
  95. String s = "While performing a remote " + getOpName();
  96. exceptionRef[0] = new ServerOperationException(s, (Throwable)o);
  97. return;
  98. }
  99. CollectionType collectionType = (CollectionType)o;
  100. Part resultPart = cm.getPart(1);
  101. Object queryResult = null;
  102. try {
  103. queryResult = resultPart.getObject();
  104. } catch (Exception e) {
  105. String s = "While deserializing " + getOpName() + " result";
  106. exceptionRef[0] = new SerializationException(s, e);
  107. return;
  108. }
  109. if (queryResult instanceof Throwable) {
  110. String s = "While performing a remote " + getOpName();
  111. exceptionRef[0] = new ServerOperationException(s, (Throwable)queryResult);
  112. return;
  113. } else if (queryResult instanceof Integer) {
  114. // Create the appropriate SelectResults instance if necessary
  115. if (resultRef[0] == null) {
  116. resultRef[0] = QueryUtils.
  117. getEmptySelectResults(TypeUtils.OBJECT_TYPE,
  118. null);
  119. }
  120. resultRef[0].add(queryResult);
  121. } else { // typical query result
  122. // Create the appropriate SelectResults instance if necessary
  123. if (resultRef[0] == null) {
  124. resultRef[0] = QueryUtils.getEmptySelectResults(collectionType,
  125. null);
  126. }
  127. SelectResults selectResults = resultRef[0];
  128. ObjectType objectType = collectionType.getElementType();
  129. Object[] resultArray;
  130. // for select * queries, the serialized object byte arrays are
  131. // returned as part of ObjectPartList
  132. boolean isObjectPartList = false;
  133. if (queryResult instanceof ObjectPartList) {
  134. isObjectPartList = true;
  135. resultArray = ((ObjectPartList) queryResult).getObjects().toArray();
  136. } else{
  137. // Add the results to the SelectResults
  138. resultArray = (Object[]) queryResult;
  139. }
  140. if (objectType.isStructType()) {
  141. for (int i = 0; i < resultArray.length; i++) {
  142. if (isObjectPartList) {
  143. selectResults
  144. .add(new StructImpl((StructTypeImpl) objectType,
  145. ((ObjectPartList) resultArray[i]).getObjects()
  146. .toArray()));
  147. } else {
  148. selectResults.add(new StructImpl((StructTypeImpl) objectType,
  149. (Object[]) resultArray[i]));
  150. }
  151. }
  152. } else {
  153. selectResults.addAll(Arrays.asList(resultArray));
  154. }
  155. }
  156. }
  157. };
  158. processChunkedResponse((ChunkedMessage)msg, getOpName(), ch);
  159. if (exceptionRef[0] != null) {
  160. throw exceptionRef[0];
  161. } else {
  162. return resultRef[0];
  163. }
  164. }
  165. protected String getOpName() {
  166. return "query";
  167. }
  168. @Override
  169. protected boolean isErrorResponse(int msgType) {
  170. return msgType == MessageType.QUERY_DATA_ERROR
  171. || msgType == MessageType.CQDATAERROR_MSG_TYPE
  172. || msgType == MessageType.CQ_EXCEPTION_TYPE;
  173. }
  174. @Override
  175. protected long startAttempt(ConnectionStats stats) {
  176. return stats.startQuery();
  177. }
  178. @Override
  179. protected void endSendAttempt(ConnectionStats stats, long start) {
  180. stats.endQuerySend(start, hasFailed());
  181. }
  182. @Override
  183. protected void endAttempt(ConnectionStats stats, long start) {
  184. stats.endQuery(start, hasTimedOut(), hasFailed());
  185. }
  186. }
  187. }