Permalink
Browse files

buffer management refactoring

First phase at improving buffer management and reducing even further buffer copies. Introduce a BytesReference abstraction, allowing to more easily slice and "read/write references" from streams. This is the foundation for later using it to create smarter buffers on top of composite netty channels for example (which http now produces) as well as reducing buffer copies when sending transport/rest responses.
  • Loading branch information...
1 parent 5d93b2b commit 35233564fdc6d455f4492085c18038e76b1ae1b6 @kimchy kimchy committed Jul 6, 2012
Showing with 1,956 additions and 1,503 deletions.
  1. +11 −9 src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java
  2. +9 −0 src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequestBuilder.java
  3. +12 −1 src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java
  4. +9 −0 ...main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequestBuilder.java
  5. +6 −6 src/main/java/org/elasticsearch/action/admin/indices/validate/query/ShardValidateQueryRequest.java
  6. +4 −4 ...main/java/org/elasticsearch/action/admin/indices/validate/query/TransportValidateQueryAction.java
  7. +34 −40 src/main/java/org/elasticsearch/action/admin/indices/validate/query/ValidateQueryRequest.java
  8. +21 −0 src/main/java/org/elasticsearch/action/admin/indices/validate/query/ValidateQueryRequestBuilder.java
  9. +6 −6 src/main/java/org/elasticsearch/action/admin/indices/warmer/put/TransportPutWarmerAction.java
  10. +22 −7 src/main/java/org/elasticsearch/action/bulk/BulkRequest.java
  11. +2 −2 src/main/java/org/elasticsearch/action/bulk/TransportShardBulkAction.java
  12. +29 −43 src/main/java/org/elasticsearch/action/count/CountRequest.java
  13. +21 −0 src/main/java/org/elasticsearch/action/count/CountRequestBuilder.java
  14. +5 −18 src/main/java/org/elasticsearch/action/count/ShardCountRequest.java
  15. +1 −1 src/main/java/org/elasticsearch/action/count/TransportCountAction.java
  16. +33 −38 src/main/java/org/elasticsearch/action/deletebyquery/DeleteByQueryRequest.java
  17. +17 −0 src/main/java/org/elasticsearch/action/deletebyquery/DeleteByQueryRequestBuilder.java
  18. +4 −4 src/main/java/org/elasticsearch/action/deletebyquery/IndexDeleteByQueryRequest.java
  19. +6 −6 src/main/java/org/elasticsearch/action/deletebyquery/ShardDeleteByQueryRequest.java
  20. +3 −3 src/main/java/org/elasticsearch/action/get/GetResponse.java
  21. +34 −67 src/main/java/org/elasticsearch/action/index/IndexRequest.java
  22. +17 −0 src/main/java/org/elasticsearch/action/index/IndexRequestBuilder.java
  23. +2 −2 src/main/java/org/elasticsearch/action/index/TransportIndexAction.java
  24. +22 −54 src/main/java/org/elasticsearch/action/mlt/MoreLikeThisRequest.java
  25. +2 −2 src/main/java/org/elasticsearch/action/mlt/TransportMoreLikeThisAction.java
  26. +17 −55 src/main/java/org/elasticsearch/action/percolate/PercolateRequest.java
  27. +17 −0 src/main/java/org/elasticsearch/action/percolate/PercolateRequestBuilder.java
  28. +1 −1 src/main/java/org/elasticsearch/action/percolate/TransportPercolateAction.java
  29. +14 −5 src/main/java/org/elasticsearch/action/search/MultiSearchRequest.java
  30. +39 −84 src/main/java/org/elasticsearch/action/search/SearchRequest.java
  31. +63 −0 src/main/java/org/elasticsearch/action/search/SearchRequestBuilder.java
  32. +2 −2 src/main/java/org/elasticsearch/action/search/type/TransportSearchHelper.java
  33. +6 −6 src/main/java/org/elasticsearch/action/update/TransportUpdateAction.java
  34. +9 −3 src/main/java/org/elasticsearch/action/update/UpdateRequest.java
  35. +7 −1 src/main/java/org/elasticsearch/action/update/UpdateRequestBuilder.java
  36. +1 −1 src/main/java/org/elasticsearch/cluster/ClusterState.java
  37. +1 −1 src/main/java/org/elasticsearch/cluster/metadata/AliasMetaData.java
  38. +1 −1 src/main/java/org/elasticsearch/cluster/metadata/MappingMetaData.java
  39. +0 −94 src/main/java/org/elasticsearch/common/BytesHolder.java
  40. +0 −62 src/main/java/org/elasticsearch/common/BytesWrap.java
  41. +156 −0 src/main/java/org/elasticsearch/common/bytes/BytesArray.java
  42. +89 −0 src/main/java/org/elasticsearch/common/bytes/BytesReference.java
  43. +107 −0 src/main/java/org/elasticsearch/common/bytes/ChannelBufferBytesReference.java
  44. +130 −0 src/main/java/org/elasticsearch/common/bytes/HashedBytesArray.java
  45. +13 −0 src/main/java/org/elasticsearch/common/compress/CompressedString.java
  46. +3 −0 src/main/java/org/elasticsearch/common/compress/Compressor.java
  47. +29 −5 src/main/java/org/elasticsearch/common/compress/CompressorFactory.java
  48. +9 −0 src/main/java/org/elasticsearch/common/compress/lzf/LZFCompressor.java
  49. +16 −2 src/main/java/org/elasticsearch/common/compress/snappy/SnappyCompressor.java
  50. +3 −5 src/main/java/org/elasticsearch/common/io/BytesStream.java
  51. +6 −16 src/main/java/org/elasticsearch/common/io/FastByteArrayOutputStream.java
  52. +1 −1 src/main/java/org/elasticsearch/common/io/Streams.java
  53. +7 −2 src/main/java/org/elasticsearch/common/io/stream/AdapterStreamInput.java
  54. +3 −8 src/main/java/org/elasticsearch/common/io/stream/AdapterStreamOutput.java
  55. +16 −6 src/main/java/org/elasticsearch/common/io/stream/BytesStreamInput.java
  56. +5 −18 src/main/java/org/elasticsearch/common/io/stream/BytesStreamOutput.java
  57. +1 −1 src/main/java/org/elasticsearch/common/io/stream/CachedStreamOutput.java
  58. +13 −12 src/main/java/org/elasticsearch/common/io/stream/StreamInput.java
  59. +7 −11 src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java
  60. +2 −2 src/main/java/org/elasticsearch/common/xcontent/XContent.java
  61. +40 −33 src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java
  62. +25 −8 src/main/java/org/elasticsearch/common/xcontent/XContentFactory.java
  63. +4 −0 src/main/java/org/elasticsearch/common/xcontent/XContentGenerator.java
  64. +71 −5 src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java
  65. +6 −3 src/main/java/org/elasticsearch/common/xcontent/json/JsonXContent.java
  66. +10 −0 src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java
  67. +6 −3 src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java
  68. +18 −0 src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContentGenerator.java
  69. +4 −2 src/main/java/org/elasticsearch/discovery/zen/ping/multicast/MulticastZenPing.java
  70. +9 −9 src/main/java/org/elasticsearch/discovery/zen/publish/PublishClusterStateAction.java
  71. +1 −2 src/main/java/org/elasticsearch/gateway/blobstore/BlobStoreGateway.java
  72. +5 −2 src/main/java/org/elasticsearch/gateway/local/state/meta/LocalGatewayMetaState.java
  73. +3 −1 src/main/java/org/elasticsearch/gateway/local/state/shards/LocalGatewayShardsState.java
  74. +3 −1 src/main/java/org/elasticsearch/http/netty/NettyHttpChannel.java
  75. +3 −3 src/main/java/org/elasticsearch/index/cache/id/IdReaderCache.java
  76. +3 −3 src/main/java/org/elasticsearch/index/cache/id/IdReaderTypeCache.java
  77. +9 −9 src/main/java/org/elasticsearch/index/cache/id/simple/SimpleIdCache.java
  78. +5 −5 src/main/java/org/elasticsearch/index/cache/id/simple/SimpleIdReaderCache.java
  79. +8 −8 src/main/java/org/elasticsearch/index/cache/id/simple/SimpleIdReaderTypeCache.java
  80. +6 −22 src/main/java/org/elasticsearch/index/engine/Engine.java
  81. +1 −1 src/main/java/org/elasticsearch/index/gateway/CommitPoints.java
  82. +2 −2 src/main/java/org/elasticsearch/index/gateway/blobstore/BlobStoreIndexShardGateway.java
  83. +10 −10 src/main/java/org/elasticsearch/index/get/GetResult.java
  84. +4 −4 src/main/java/org/elasticsearch/index/get/ShardGetService.java
  85. +6 −5 src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java
  86. +4 −17 src/main/java/org/elasticsearch/index/mapper/ParseContext.java
  87. +6 −19 src/main/java/org/elasticsearch/index/mapper/ParsedDocument.java
  88. +11 −33 src/main/java/org/elasticsearch/index/mapper/SourceToParse.java
  89. +3 −3 src/main/java/org/elasticsearch/index/mapper/core/BinaryFieldMapper.java
  90. +1 −1 src/main/java/org/elasticsearch/index/mapper/internal/SizeFieldMapper.java
  91. +24 −33 src/main/java/org/elasticsearch/index/mapper/internal/SourceFieldMapper.java
  92. +10 −31 src/main/java/org/elasticsearch/index/percolator/PercolatorExecutor.java
  93. +6 −5 src/main/java/org/elasticsearch/index/percolator/PercolatorService.java
  94. +4 −4 src/main/java/org/elasticsearch/index/query/BaseQueryBuilder.java
  95. +19 −3 src/main/java/org/elasticsearch/index/query/IndexQueryParserService.java
  96. +3 −3 src/main/java/org/elasticsearch/index/query/QueryBuilder.java
  97. +2 −2 src/main/java/org/elasticsearch/index/search/child/ChildCollector.java
  98. +2 −2 src/main/java/org/elasticsearch/index/search/child/TopChildrenQuery.java
  99. +4 −4 src/main/java/org/elasticsearch/index/search/slowlog/ShardSlowLogSearchService.java
  100. +3 −5 src/main/java/org/elasticsearch/index/shard/service/IndexShard.java
  101. +11 −16 src/main/java/org/elasticsearch/index/shard/service/InternalIndexShard.java
  102. +18 −17 src/main/java/org/elasticsearch/index/translog/Translog.java
  103. +2 −2 src/main/java/org/elasticsearch/index/translog/fs/FsTranslog.java
  104. +6 −5 src/main/java/org/elasticsearch/indices/recovery/RecoveryFileChunkRequest.java
  105. +2 −2 src/main/java/org/elasticsearch/indices/recovery/RecoverySource.java
  106. +6 −1 src/main/java/org/elasticsearch/indices/recovery/RecoveryTarget.java
  107. +2 −2 src/main/java/org/elasticsearch/rest/XContentRestResponse.java
  108. +3 −3 ...main/java/org/elasticsearch/rest/action/admin/indices/validate/query/RestValidateQueryAction.java
  109. +3 −3 src/main/java/org/elasticsearch/rest/action/count/RestCountAction.java
  110. +3 −3 src/main/java/org/elasticsearch/rest/action/deletebyquery/RestDeleteByQueryAction.java
  111. +2 −2 src/main/java/org/elasticsearch/rest/action/support/RestActions.java
  112. +7 −11 src/main/java/org/elasticsearch/rest/action/support/RestXContentBuilder.java
  113. +3 −3 src/main/java/org/elasticsearch/search/SearchHit.java
  114. +8 −7 src/main/java/org/elasticsearch/search/SearchService.java
  115. +43 −54 src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java
  116. +2 −1 src/main/java/org/elasticsearch/search/fetch/FetchPhase.java
  117. +12 −12 src/main/java/org/elasticsearch/search/internal/InternalSearchHit.java
  118. +11 −47 src/main/java/org/elasticsearch/search/internal/InternalSearchRequest.java
  119. +8 −7 src/main/java/org/elasticsearch/search/lookup/SourceLookup.java
  120. +12 −11 src/main/java/org/elasticsearch/search/warmer/IndexWarmersMetaData.java
  121. +1 −1 src/main/java/org/elasticsearch/transport/local/LocalTransport.java
  122. +2 −2 src/main/java/org/elasticsearch/transport/local/LocalTransportChannel.java
  123. +10 −12 src/main/java/org/elasticsearch/transport/netty/ChannelBufferStreamInput.java
  124. +3 −1 src/main/java/org/elasticsearch/transport/netty/NettyTransport.java
  125. +5 −2 src/main/java/org/elasticsearch/transport/netty/NettyTransportChannel.java
  126. +9 −6 src/main/java/org/elasticsearch/transport/support/TransportStreams.java
  127. +1 −1 src/test/java/org/elasticsearch/benchmark/compress/LuceneCompressionBenchmark.java
  128. +6 −6 src/test/java/org/elasticsearch/benchmark/compress/PureCompressionBenchmark.java
  129. +5 −4 src/test/java/org/elasticsearch/benchmark/percolator/EmbeddedPercolatorBenchmarkTest.java
  130. +3 −3 src/test/java/org/elasticsearch/test/integration/search/compress/SearchSourceCompressTests.java
  131. +1 −1 src/test/java/org/elasticsearch/test/integration/search/facet/SimpleFacetsTests.java
  132. +8 −8 src/test/java/org/elasticsearch/test/integration/update/UpdateTests.java
  133. +2 −2 src/test/java/org/elasticsearch/test/stress/compress/LuceneCompressionStressTest.java
  134. +7 −0 src/test/java/org/elasticsearch/test/unit/action/bulk/BulkRequestTests.java
  135. +14 −14 src/test/java/org/elasticsearch/test/unit/cluster/metadata/MappingMetaDataParserTests.java
  136. +1 −1 src/test/java/org/elasticsearch/test/unit/cluster/serialization/ClusterSerializationTests.java
  137. +1 −1 src/test/java/org/elasticsearch/test/unit/common/io/streams/BytesStreamsTests.java
  138. +1 −1 src/test/java/org/elasticsearch/test/unit/common/io/streams/HandlesStreamsTests.java
  139. +2 −2 src/test/java/org/elasticsearch/test/unit/common/xcontent/builder/BuilderRawFieldTests.java
  140. +1 −1 src/test/java/org/elasticsearch/test/unit/common/xcontent/builder/XContentBuilderTests.java
  141. +1 −1 src/test/java/org/elasticsearch/test/unit/common/xcontent/smile/JsonVsSmileTests.java
  142. +1 −1 src/test/java/org/elasticsearch/test/unit/deps/jackson/JacksonLocationTests.java
  143. +1 −1 src/test/java/org/elasticsearch/test/unit/discovery/zen/ping/multicast/MulticastZenPingTests.java
  144. +16 −15 src/test/java/org/elasticsearch/test/unit/index/engine/AbstractSimpleEngineTests.java
  145. +5 −4 src/test/java/org/elasticsearch/test/unit/index/mapper/all/SimpleAllMapperTests.java
  146. +7 −7 src/test/java/org/elasticsearch/test/unit/index/mapper/analyzer/AnalyzerMapperTests.java
  147. +4 −4 src/test/java/org/elasticsearch/test/unit/index/mapper/boost/BoostMappingTests.java
  148. +2 −2 src/test/java/org/elasticsearch/test/unit/index/mapper/boost/CustomBoostMappingTests.java
  149. +2 −2 src/test/java/org/elasticsearch/test/unit/index/mapper/camelcase/CamelCaseFieldNameTests.java
  150. +4 −4 src/test/java/org/elasticsearch/test/unit/index/mapper/compound/CompoundTypesTests.java
  151. +4 −4 src/test/java/org/elasticsearch/test/unit/index/mapper/date/SimpleDateMappingTests.java
  152. +6 −6 src/test/java/org/elasticsearch/test/unit/index/mapper/dynamic/DynamicMappingTests.java
  153. +2 −1 ...csearch/test/unit/index/mapper/dynamictemplate/genericstore/GenericStoreDynamicTemplateTests.java
  154. +2 −1 ...elasticsearch/test/unit/index/mapper/dynamictemplate/pathmatch/PathMatchDynamicTempalteTests.java
  155. +3 −2 .../org/elasticsearch/test/unit/index/mapper/dynamictemplate/simple/SimpleDynamicTemplatesTests.java
  156. +4 −4 src/test/java/org/elasticsearch/test/unit/index/mapper/geopoint/GeohashMappingGeoPointTests.java
  157. +4 −4 .../java/org/elasticsearch/test/unit/index/mapper/geopoint/LatLonAndGeohashMappingGeoPointTests.java
  158. +23 −23 src/test/java/org/elasticsearch/test/unit/index/mapper/geopoint/LatLonMappingGeoPointTests.java
  159. +6 −6 src/test/java/org/elasticsearch/test/unit/index/mapper/id/IdMappingTests.java
  160. +4 −4 src/test/java/org/elasticsearch/test/unit/index/mapper/index/IndexTypeMapperTests.java
  161. +2 −2 src/test/java/org/elasticsearch/test/unit/index/mapper/lucene/DoubleIndexingDocTest.java
  162. +4 −2 src/test/java/org/elasticsearch/test/unit/index/mapper/multifield/MultiFieldTests.java
  163. +4 −2 ...test/java/org/elasticsearch/test/unit/index/mapper/multifield/merge/JavaMultiFieldMergeTests.java
  164. +9 −9 src/test/java/org/elasticsearch/test/unit/index/mapper/nested/NestedMappingTests.java
  165. +3 −3 src/test/java/org/elasticsearch/test/unit/index/mapper/numeric/SimpleNumericTests.java
  166. +4 −4 src/test/java/org/elasticsearch/test/unit/index/mapper/object/NullValueObjectMappingTests.java
  167. +8 −5 src/test/java/org/elasticsearch/test/unit/index/mapper/parent/ParentMappingTests.java
  168. +2 −2 src/test/java/org/elasticsearch/test/unit/index/mapper/routing/RoutingTypeMapperTests.java
  169. +7 −5 src/test/java/org/elasticsearch/test/unit/index/mapper/simple/SimpleMapperTests.java
  170. +10 −9 src/test/java/org/elasticsearch/test/unit/index/mapper/size/SizeMappingTests.java
  171. +4 −4 src/test/java/org/elasticsearch/test/unit/index/mapper/source/CompressSourceMappingTests.java
  172. +15 −16 src/test/java/org/elasticsearch/test/unit/index/mapper/source/DefaultSourceMappingTests.java
  173. +6 −5 src/test/java/org/elasticsearch/test/unit/index/mapper/timestamp/TimestampMappingTests.java
  174. +6 −5 src/test/java/org/elasticsearch/test/unit/index/mapper/ttl/TTLMappingTests.java
  175. +11 −11 src/test/java/org/elasticsearch/test/unit/index/mapper/typelevels/ParseDocumentTypeLevelsTests.java
  176. +3 −2 src/test/java/org/elasticsearch/test/unit/index/percolator/PercolatorExecutorTests.java
  177. +6 −5 src/test/java/org/elasticsearch/test/unit/index/query/SimpleIndexQueryParserTests.java
  178. +14 −14 src/test/java/org/elasticsearch/test/unit/index/translog/AbstractSimpleTranslogTests.java
View
20 src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java
@@ -26,6 +26,8 @@
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.MasterNodeOperationRequest;
import org.elasticsearch.cluster.metadata.IndexMetaData;
+import org.elasticsearch.common.bytes.BytesArray;
+import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
@@ -238,11 +240,7 @@ public CreateIndexRequest source(String source) {
* Sets the settings and mappings as a single source.
*/
public CreateIndexRequest source(XContentBuilder source) {
- try {
- return source(source.underlyingBytes(), 0, source.underlyingBytesLength());
- } catch (IOException e) {
- throw new ElasticSearchParseException("failed to parse source to create index", e);
- }
+ return source(source.bytes());
}
/**
@@ -252,19 +250,23 @@ public CreateIndexRequest source(byte[] source) {
return source(source, 0, source.length);
}
+ public CreateIndexRequest source(byte[] source, int offset, int length) {
+ return source(new BytesArray(source, offset, length));
+ }
+
/**
* Sets the settings and mappings as a single source.
*/
- public CreateIndexRequest source(byte[] source, int offset, int length) {
- XContentType xContentType = XContentFactory.xContentType(source, offset, length);
+ public CreateIndexRequest source(BytesReference source) {
+ XContentType xContentType = XContentFactory.xContentType(source);
if (xContentType != null) {
try {
- source(XContentFactory.xContent(xContentType).createParser(source, offset, length).mapAndClose());
+ source(XContentFactory.xContent(xContentType).createParser(source).mapAndClose());
} catch (IOException e) {
throw new ElasticSearchParseException("failed to parse source for create index", e);
}
} else {
- settings(new String(source, offset, length, Charsets.UTF_8));
+ settings(new String(source.toBytes(), Charsets.UTF_8));
}
return this;
}
View
9 src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequestBuilder.java
@@ -23,6 +23,7 @@
import org.elasticsearch.action.admin.indices.support.BaseIndicesRequestBuilder;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.cluster.metadata.IndexMetaData;
+import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -139,6 +140,14 @@ public CreateIndexRequestBuilder setSource(String source) {
/**
* Sets the settings and mappings as a single source.
*/
+ public CreateIndexRequestBuilder setSource(BytesReference source) {
+ request.source(source);
+ return this;
+ }
+
+ /**
+ * Sets the settings and mappings as a single source.
+ */
public CreateIndexRequestBuilder setSource(byte[] source) {
request.source(source);
return this;
View
13 ...ain/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java
@@ -25,6 +25,7 @@
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.MasterNodeOperationRequest;
import org.elasticsearch.cluster.metadata.IndexMetaData;
+import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
@@ -247,7 +248,7 @@ public PutIndexTemplateRequest mapping(String type, Map<String, Object> source)
*/
public PutIndexTemplateRequest source(XContentBuilder templateBuilder) {
try {
- return source(templateBuilder.underlyingBytes(), 0, templateBuilder.underlyingBytesLength());
+ return source(templateBuilder.bytes());
} catch (Exception e) {
throw new ElasticSearchIllegalArgumentException("Failed to build json for template request", e);
}
@@ -321,6 +322,16 @@ public PutIndexTemplateRequest source(byte[] source, int offset, int length) {
}
}
+ /**
+ * The template source definition.
+ */
+ public PutIndexTemplateRequest source(BytesReference source) {
+ try {
+ return source(XContentFactory.xContent(source).createParser(source).mapOrderedAndClose());
+ } catch (IOException e) {
+ throw new ElasticSearchIllegalArgumentException("failed to parse template source", e);
+ }
+ }
public PutIndexTemplateRequest custom(IndexMetaData.Custom custom) {
customs.put(custom.type(), custom);
View
9 ...a/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequestBuilder.java
@@ -22,6 +22,7 @@
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.support.BaseIndicesRequestBuilder;
import org.elasticsearch.client.IndicesAdminClient;
+import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -166,6 +167,14 @@ public PutIndexTemplateRequestBuilder setSource(String templateSource) {
/**
* The template source definition.
*/
+ public PutIndexTemplateRequestBuilder setSource(BytesReference templateSource) {
+ request.source(templateSource);
+ return this;
+ }
+
+ /**
+ * The template source definition.
+ */
public PutIndexTemplateRequestBuilder setSource(byte[] templateSource) {
request.source(templateSource);
return this;
View
12 ...java/org/elasticsearch/action/admin/indices/validate/query/ShardValidateQueryRequest.java
@@ -20,9 +20,9 @@
package org.elasticsearch.action.admin.indices.validate.query;
import org.elasticsearch.action.support.broadcast.BroadcastShardOperationRequest;
-import org.elasticsearch.common.BytesHolder;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
+import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
@@ -33,10 +33,10 @@
*/
class ShardValidateQueryRequest extends BroadcastShardOperationRequest {
- private BytesHolder querySource;
+ private BytesReference querySource;
private String[] types = Strings.EMPTY_ARRAY;
-
+
private boolean explain;
@Nullable
@@ -54,14 +54,14 @@ public ShardValidateQueryRequest(String index, int shardId, @Nullable String[] f
this.filteringAliases = filteringAliases;
}
- public BytesHolder querySource() {
+ public BytesReference querySource() {
return querySource;
}
public String[] types() {
return this.types;
}
-
+
public boolean explain() {
return this.explain;
}
@@ -96,7 +96,7 @@ public void readFrom(StreamInput in) throws IOException {
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
- out.writeBytesHolder(querySource);
+ out.writeBytesReference(querySource, true);
out.writeVInt(types.length);
for (String type : types) {
View
8 ...a/org/elasticsearch/action/admin/indices/validate/query/TransportValidateQueryAction.java
@@ -136,8 +136,8 @@ protected ValidateQueryResponse newResponse(ValidateQueryRequest request, Atomic
} else {
ShardValidateQueryResponse validateQueryResponse = (ShardValidateQueryResponse) shardResponse;
valid = valid && validateQueryResponse.valid();
- if(request.explain()) {
- if(queryExplanations == null) {
+ if (request.explain()) {
+ if (queryExplanations == null) {
queryExplanations = newArrayList();
}
queryExplanations.add(new QueryExplanation(
@@ -170,9 +170,9 @@ protected ShardValidateQueryResponse shardOperation(ShardValidateQueryRequest re
null, indexShard.searcher(), indexService, indexShard,
scriptService));
try {
- ParsedQuery parsedQuery = queryParserService.parse(request.querySource().bytes(), request.querySource().offset(), request.querySource().length());
+ ParsedQuery parsedQuery = queryParserService.parse(request.querySource());
valid = true;
- if(request.explain()) {
+ if (request.explain()) {
explanation = parsedQuery.query().toString();
}
} catch (QueryParsingException e) {
View
74 ...main/java/org/elasticsearch/action/admin/indices/validate/query/ValidateQueryRequest.java
@@ -19,21 +19,20 @@
package org.elasticsearch.action.admin.indices.validate.query;
-import org.apache.lucene.util.UnicodeUtil;
import org.elasticsearch.ElasticSearchGenerationException;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.broadcast.BroadcastOperationRequest;
import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading;
import org.elasticsearch.client.Requests;
-import org.elasticsearch.common.BytesHolder;
import org.elasticsearch.common.Required;
import org.elasticsearch.common.Strings;
-import org.elasticsearch.common.Unicode;
-import org.elasticsearch.common.io.BytesStream;
+import org.elasticsearch.common.bytes.BytesArray;
+import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
+import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
@@ -51,11 +50,9 @@
private static final XContentType contentType = Requests.CONTENT_TYPE;
- private byte[] querySource;
- private int querySourceOffset;
- private int querySourceLength;
+ private BytesReference querySource;
private boolean querySourceUnsafe;
-
+
private boolean explain;
private String[] types = Strings.EMPTY_ARRAY;
@@ -89,8 +86,7 @@ public ValidateQueryRequest operationThreading(BroadcastOperationThreading opera
@Override
protected void beforeStart() {
if (querySourceUnsafe) {
- querySource = Arrays.copyOfRange(querySource, querySourceOffset, querySourceOffset + querySourceLength);
- querySourceOffset = 0;
+ querySource = querySource.copyBytesArray();
querySourceUnsafe = false;
}
}
@@ -112,8 +108,8 @@ public ValidateQueryRequest indices(String... indices) {
/**
* The query source to execute.
*/
- BytesHolder querySource() {
- return new BytesHolder(querySource, querySourceOffset, querySourceLength);
+ BytesReference querySource() {
+ return querySource;
}
/**
@@ -123,10 +119,7 @@ BytesHolder querySource() {
*/
@Required
public ValidateQueryRequest query(QueryBuilder queryBuilder) {
- BytesStream bos = queryBuilder.buildAsBytes();
- this.querySource = bos.underlyingBytes();
- this.querySourceOffset = 0;
- this.querySourceLength = bos.size();
+ this.querySource = queryBuilder.buildAsBytes();
this.querySourceUnsafe = false;
return this;
}
@@ -147,15 +140,9 @@ public ValidateQueryRequest query(Map querySource) {
@Required
public ValidateQueryRequest query(XContentBuilder builder) {
- try {
- this.querySource = builder.underlyingBytes();
- this.querySourceOffset = 0;
- this.querySourceLength = builder.underlyingBytesLength();
- this.querySourceUnsafe = false;
- return this;
- } catch (IOException e) {
- throw new ElasticSearchGenerationException("Failed to generate [" + builder + "]", e);
- }
+ this.querySource = builder.bytes();
+ this.querySourceUnsafe = false;
+ return this;
}
/**
@@ -164,11 +151,9 @@ public ValidateQueryRequest query(XContentBuilder builder) {
*/
@Required
public ValidateQueryRequest query(String querySource) {
- UnicodeUtil.UTF8Result result = Unicode.fromStringAsUtf8(querySource);
- this.querySource = result.result;
- this.querySourceOffset = 0;
- this.querySourceLength = result.length;
- this.querySourceUnsafe = true;
+ this.querySource = new BytesArray(querySource);
+ ;
+ this.querySourceUnsafe = false;
return this;
}
@@ -185,9 +170,15 @@ public ValidateQueryRequest query(byte[] querySource) {
*/
@Required
public ValidateQueryRequest query(byte[] querySource, int offset, int length, boolean unsafe) {
+ return query(new BytesArray(querySource, offset, length), unsafe);
+ }
+
+ /**
+ * The query source to validate.
+ */
+ @Required
+ public ValidateQueryRequest query(BytesReference querySource, boolean unsafe) {
this.querySource = querySource;
- this.querySourceOffset = offset;
- this.querySourceLength = length;
this.querySourceUnsafe = unsafe;
return this;
}
@@ -225,11 +216,8 @@ public boolean explain() {
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
- BytesHolder bytes = in.readBytesReference();
querySourceUnsafe = false;
- querySource = bytes.bytes();
- querySourceOffset = bytes.offset();
- querySourceLength = bytes.length();
+ querySource = in.readBytesReference();
int typesSize = in.readVInt();
if (typesSize > 0) {
@@ -240,25 +228,31 @@ public void readFrom(StreamInput in) throws IOException {
}
explain = in.readBoolean();
-
+
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
- out.writeBytesHolder(querySource, querySourceOffset, querySourceLength);
+ out.writeBytesReference(querySource, true);
out.writeVInt(types.length);
for (String type : types) {
out.writeUTF(type);
}
-
+
out.writeBoolean(explain);
}
@Override
public String toString() {
- return "[" + Arrays.toString(indices) + "]" + Arrays.toString(types) + ", querySource[" + Unicode.fromBytes(querySource, querySourceOffset, querySourceLength) + "], explain:" + explain;
+ String sSource = "_na_";
+ try {
+ sSource = XContentHelper.convertToJson(querySource, false);
+ } catch (Exception e) {
+ // ignore
+ }
+ return "[" + Arrays.toString(indices) + "]" + Arrays.toString(types) + ", querySource[" + sSource + "], explain:" + explain;
}
}
View
21 ...va/org/elasticsearch/action/admin/indices/validate/query/ValidateQueryRequestBuilder.java
@@ -4,6 +4,7 @@
import org.elasticsearch.action.admin.indices.support.BaseIndicesRequestBuilder;
import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading;
import org.elasticsearch.client.IndicesAdminClient;
+import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.index.query.QueryBuilder;
/**
@@ -45,6 +46,26 @@ public ValidateQueryRequestBuilder setQuery(QueryBuilder queryBuilder) {
*
* @see org.elasticsearch.index.query.QueryBuilders
*/
+ public ValidateQueryRequestBuilder setQuery(BytesReference querySource) {
+ request.query(querySource, false);
+ return this;
+ }
+
+ /**
+ * The query source to validate.
+ *
+ * @see org.elasticsearch.index.query.QueryBuilders
+ */
+ public ValidateQueryRequestBuilder setQuery(BytesReference querySource, boolean unsafe) {
+ request.query(querySource, unsafe);
+ return this;
+ }
+
+ /**
+ * The query source to validate.
+ *
+ * @see org.elasticsearch.index.query.QueryBuilders
+ */
public ValidateQueryRequestBuilder setQuery(byte[] querySource) {
request.query(querySource);
return this;
View
12 ...main/java/org/elasticsearch/action/admin/indices/warmer/put/TransportPutWarmerAction.java
@@ -30,7 +30,7 @@
import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
-import org.elasticsearch.common.BytesHolder;
+import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.Index;
@@ -108,11 +108,11 @@ public ClusterState execute(ClusterState currentState) {
String[] concreteIndices = metaData.concreteIndices(request.searchRequest().indices());
- BytesHolder source = null;
- if (request.searchRequest().source() != null && request.searchRequest().source().length > 0) {
- source = new BytesHolder(request.searchRequest().source(), request.searchRequest().sourceOffset(), request.searchRequest().sourceLength());
- } else if (request.searchRequest().extraSource() != null && request.searchRequest().extraSource().length > 0) {
- source = new BytesHolder(request.searchRequest().extraSource(), request.searchRequest().extraSourceOffset(), request.searchRequest().extraSourceLength());
+ BytesReference source = null;
+ if (request.searchRequest().source() != null && request.searchRequest().source().length() > 0) {
+ source = request.searchRequest().source();
+ } else if (request.searchRequest().extraSource() != null && request.searchRequest().extraSource().length() > 0) {
+ source = request.searchRequest().extraSource();
}
// now replace it on the metadata
View
29 src/main/java/org/elasticsearch/action/bulk/BulkRequest.java
@@ -27,6 +27,8 @@
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.support.replication.ReplicationType;
import org.elasticsearch.common.Nullable;
+import org.elasticsearch.common.bytes.BytesArray;
+import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.unit.TimeValue;
@@ -78,6 +80,10 @@ public BulkRequest add(DeleteRequest request) {
return this;
}
+ public List<ActionRequest> requests() {
+ return this.requests;
+ }
+
/**
* Adds a framed data in binary format
*/
@@ -89,15 +95,24 @@ public BulkRequest add(byte[] data, int from, int length, boolean contentUnsafe)
* Adds a framed data in binary format
*/
public BulkRequest add(byte[] data, int from, int length, boolean contentUnsafe, @Nullable String defaultIndex, @Nullable String defaultType) throws Exception {
- XContent xContent = XContentFactory.xContent(data, from, length);
+ return add(new BytesArray(data, from, length), contentUnsafe, defaultIndex, defaultType);
+ }
+
+ /**
+ * Adds a framed data in binary format
+ */
+ public BulkRequest add(BytesReference data, boolean contentUnsafe, @Nullable String defaultIndex, @Nullable String defaultType) throws Exception {
+ XContent xContent = XContentFactory.xContent(data);
+ int from = 0;
+ int length = data.length();
byte marker = xContent.streamSeparator();
while (true) {
int nextMarker = findNextMarker(marker, from, data, length);
if (nextMarker == -1) {
break;
}
// now parse the action
- XContentParser parser = xContent.createParser(data, from, nextMarker - from);
+ XContentParser parser = xContent.createParser(data.slice(from, nextMarker - from));
try {
// move pointers
@@ -177,18 +192,18 @@ public BulkRequest add(byte[] data, int from, int length, boolean contentUnsafe,
if ("index".equals(action)) {
if (opType == null) {
internalAdd(new IndexRequest(index, type, id).routing(routing).parent(parent).timestamp(timestamp).ttl(ttl).version(version).versionType(versionType)
- .source(data, from, nextMarker - from, contentUnsafe)
+ .source(data.slice(from, nextMarker - from), contentUnsafe)
.percolate(percolate));
} else {
internalAdd(new IndexRequest(index, type, id).routing(routing).parent(parent).timestamp(timestamp).ttl(ttl).version(version).versionType(versionType)
.create("create".equals(opType))
- .source(data, from, nextMarker - from, contentUnsafe)
+ .source(data.slice(from, nextMarker - from), contentUnsafe)
.percolate(percolate));
}
} else if ("create".equals(action)) {
internalAdd(new IndexRequest(index, type, id).routing(routing).parent(parent).timestamp(timestamp).ttl(ttl).version(version).versionType(versionType)
.create(true)
- .source(data, from, nextMarker - from, contentUnsafe)
+ .source(data.slice(from, nextMarker - from), contentUnsafe)
.percolate(percolate));
}
// move pointers
@@ -239,9 +254,9 @@ public ReplicationType replicationType() {
return this.replicationType;
}
- private int findNextMarker(byte marker, int from, byte[] data, int length) {
+ private int findNextMarker(byte marker, int from, BytesReference data, int length) {
for (int i = from; i < length; i++) {
- if (data[i] == marker) {
+ if (data.get(i) == marker) {
return i;
}
}
View
4 src/main/java/org/elasticsearch/action/bulk/TransportShardBulkAction.java
@@ -148,7 +148,7 @@ protected ShardIterator shards(ClusterState clusterState, BulkShardRequest reque
}
}
- SourceToParse sourceToParse = SourceToParse.source(indexRequest.underlyingSource(), indexRequest.underlyingSourceOffset(), indexRequest.underlyingSourceLength()).type(indexRequest.type()).id(indexRequest.id())
+ SourceToParse sourceToParse = SourceToParse.source(indexRequest.source()).type(indexRequest.type()).id(indexRequest.id())
.routing(indexRequest.routing()).parent(indexRequest.parent()).timestamp(indexRequest.timestamp()).ttl(indexRequest.ttl());
long version;
@@ -292,7 +292,7 @@ protected void shardOperationOnReplica(ReplicaOperationRequest shardRequest) {
if (item.request() instanceof IndexRequest) {
IndexRequest indexRequest = (IndexRequest) item.request();
try {
- SourceToParse sourceToParse = SourceToParse.source(indexRequest.underlyingSource(), indexRequest.underlyingSourceOffset(), indexRequest.underlyingSourceLength()).type(indexRequest.type()).id(indexRequest.id())
+ SourceToParse sourceToParse = SourceToParse.source(indexRequest.source()).type(indexRequest.type()).id(indexRequest.id())
.routing(indexRequest.routing()).parent(indexRequest.parent()).timestamp(indexRequest.timestamp()).ttl(indexRequest.ttl());
if (indexRequest.opType() == IndexRequest.OpType.INDEX) {
View
72 src/main/java/org/elasticsearch/action/count/CountRequest.java
@@ -19,18 +19,21 @@
package org.elasticsearch.action.count;
-import org.apache.lucene.util.UnicodeUtil;
import org.elasticsearch.ElasticSearchGenerationException;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.broadcast.BroadcastOperationRequest;
import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading;
import org.elasticsearch.client.Requests;
-import org.elasticsearch.common.*;
-import org.elasticsearch.common.io.BytesStream;
+import org.elasticsearch.common.Nullable;
+import org.elasticsearch.common.Required;
+import org.elasticsearch.common.Strings;
+import org.elasticsearch.common.bytes.BytesArray;
+import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
+import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
@@ -62,9 +65,7 @@
@Nullable
protected String routing;
- private byte[] querySource;
- private int querySourceOffset;
- private int querySourceLength;
+ private BytesReference querySource;
private boolean querySourceUnsafe;
private String[] types = Strings.EMPTY_ARRAY;
@@ -103,8 +104,7 @@ public CountRequest operationThreading(BroadcastOperationThreading operationThre
@Override
protected void beforeStart() {
if (querySourceUnsafe) {
- querySource = Arrays.copyOfRange(querySource, querySourceOffset, querySourceOffset + querySourceLength);
- querySourceOffset = 0;
+ querySource = querySource.copyBytesArray();
querySourceUnsafe = false;
}
}
@@ -150,29 +150,18 @@ public CountRequest minScore(float minScore) {
/**
* The query source to execute.
*/
- byte[] querySource() {
+ BytesReference querySource() {
return querySource;
}
- int querySourceOffset() {
- return querySourceOffset;
- }
-
- int querySourceLength() {
- return querySourceLength;
- }
-
/**
* The query source to execute.
*
* @see org.elasticsearch.index.query.QueryBuilders
*/
@Required
public CountRequest query(QueryBuilder queryBuilder) {
- BytesStream bos = queryBuilder.buildAsBytes();
- this.querySource = bos.underlyingBytes();
- this.querySourceOffset = 0;
- this.querySourceLength = bos.size();
+ this.querySource = queryBuilder.buildAsBytes();
this.querySourceUnsafe = false;
return this;
}
@@ -193,15 +182,9 @@ public CountRequest query(Map querySource) {
@Required
public CountRequest query(XContentBuilder builder) {
- try {
- this.querySource = builder.underlyingBytes();
- this.querySourceOffset = 0;
- this.querySourceLength = builder.underlyingBytesLength();
- this.querySourceUnsafe = false;
- return this;
- } catch (IOException e) {
- throw new ElasticSearchGenerationException("Failed to generate [" + builder + "]", e);
- }
+ this.querySource = builder.bytes();
+ this.querySourceUnsafe = false;
+ return this;
}
/**
@@ -210,11 +193,8 @@ public CountRequest query(XContentBuilder builder) {
*/
@Required
public CountRequest query(String querySource) {
- UnicodeUtil.UTF8Result result = Unicode.fromStringAsUtf8(querySource);
- this.querySource = result.result;
- this.querySourceOffset = 0;
- this.querySourceLength = result.length;
- this.querySourceUnsafe = true;
+ this.querySource = new BytesArray(querySource);
+ this.querySourceUnsafe = false;
return this;
}
@@ -231,9 +211,12 @@ public CountRequest query(byte[] querySource) {
*/
@Required
public CountRequest query(byte[] querySource, int offset, int length, boolean unsafe) {
+ return query(new BytesArray(querySource, offset, length), unsafe);
+ }
+
+ @Required
+ public CountRequest query(BytesReference querySource, boolean unsafe) {
this.querySource = querySource;
- this.querySourceOffset = offset;
- this.querySourceLength = length;
this.querySourceUnsafe = unsafe;
return this;
}
@@ -288,11 +271,8 @@ public void readFrom(StreamInput in) throws IOException {
routing = in.readUTF();
}
- BytesHolder bytes = in.readBytesReference();
querySourceUnsafe = false;
- querySource = bytes.bytes();
- querySourceOffset = bytes.offset();
- querySourceLength = bytes.length();
+ querySource = in.readBytesReference();
int typesSize = in.readVInt();
if (typesSize > 0) {
@@ -321,7 +301,7 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeUTF(routing);
}
- out.writeBytesHolder(querySource, querySourceOffset, querySourceLength());
+ out.writeBytesReference(querySource, true);
out.writeVInt(types.length);
for (String type : types) {
@@ -331,6 +311,12 @@ public void writeTo(StreamOutput out) throws IOException {
@Override
public String toString() {
- return "[" + Arrays.toString(indices) + "]" + Arrays.toString(types) + ", querySource[" + Unicode.fromBytes(querySource) + "]";
+ String sSource = "_na_";
+ try {
+ sSource = XContentHelper.convertToJson(querySource, false);
+ } catch (Exception e) {
+ // ignore
+ }
+ return "[" + Arrays.toString(indices) + "]" + Arrays.toString(types) + ", querySource[" + sSource + "]";
}
}
View
21 src/main/java/org/elasticsearch/action/count/CountRequestBuilder.java
@@ -23,6 +23,7 @@
import org.elasticsearch.action.support.BaseRequestBuilder;
import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading;
import org.elasticsearch.client.Client;
+import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.index.query.QueryBuilder;
/**
@@ -98,6 +99,26 @@ public CountRequestBuilder setQuery(QueryBuilder queryBuilder) {
*
* @see org.elasticsearch.index.query.QueryBuilders
*/
+ public CountRequestBuilder setQuery(BytesReference querySource) {
+ request.query(querySource, false);
+ return this;
+ }
+
+ /**
+ * The query source to execute.
+ *
+ * @see org.elasticsearch.index.query.QueryBuilders
+ */
+ public CountRequestBuilder setQuery(BytesReference querySource, boolean unsafe) {
+ request.query(querySource, unsafe);
+ return this;
+ }
+
+ /**
+ * The query source to execute.
+ *
+ * @see org.elasticsearch.index.query.QueryBuilders
+ */
public CountRequestBuilder setQuery(byte[] querySource) {
request.query(querySource);
return this;
View
23 src/main/java/org/elasticsearch/action/count/ShardCountRequest.java
@@ -20,9 +20,9 @@
package org.elasticsearch.action.count;
import org.elasticsearch.action.support.broadcast.BroadcastShardOperationRequest;
-import org.elasticsearch.common.BytesHolder;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
+import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
@@ -35,7 +35,7 @@
private float minScore;
- private byte[] querySource;
+ private BytesReference querySource;
private int querySourceOffset;
private int querySourceLength;
@@ -52,8 +52,6 @@ public ShardCountRequest(String index, int shardId, @Nullable String[] filtering
super(index, shardId);
this.minScore = request.minScore();
this.querySource = request.querySource();
- this.querySourceOffset = request.querySourceOffset();
- this.querySourceLength = request.querySourceLength();
this.types = request.types();
this.filteringAliases = filteringAliases;
}
@@ -62,18 +60,10 @@ public float minScore() {
return minScore;
}
- public byte[] querySource() {
+ public BytesReference querySource() {
return querySource;
}
- public int querySourceOffset() {
- return querySourceOffset;
- }
-
- public int querySourceLength() {
- return querySourceLength;
- }
-
public String[] types() {
return this.types;
}
@@ -87,10 +77,7 @@ public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
minScore = in.readFloat();
- BytesHolder bytes = in.readBytesReference();
- querySource = bytes.bytes();
- querySourceOffset = bytes.offset();
- querySourceLength = bytes.length();
+ querySource = in.readBytesReference();
int typesSize = in.readVInt();
if (typesSize > 0) {
@@ -113,7 +100,7 @@ public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeFloat(minScore);
- out.writeBytesHolder(querySource, querySourceOffset, querySourceLength);
+ out.writeBytesReference(querySource, true);
out.writeVInt(types.length);
for (String type : types) {
View
2 src/main/java/org/elasticsearch/action/count/TransportCountAction.java
@@ -131,7 +131,7 @@ protected CountResponse newResponse(CountRequest request, AtomicReferenceArray s
@Override
protected ShardCountResponse shardOperation(ShardCountRequest request) throws ElasticSearchException {
IndexShard indexShard = indicesService.indexServiceSafe(request.index()).shardSafe(request.shardId());
- long count = indexShard.count(request.minScore(), request.querySource(), request.querySourceOffset(), request.querySourceLength(),
+ long count = indexShard.count(request.minScore(), request.querySource(),
request.filteringAliases(), request.types());
return new ShardCountResponse(request.index(), request.shardId(), count);
}
View
71 src/main/java/org/elasticsearch/action/deletebyquery/DeleteByQueryRequest.java
@@ -19,20 +19,24 @@
package org.elasticsearch.action.deletebyquery;
-import org.apache.lucene.util.UnicodeUtil;
+import com.google.common.base.Charsets;
import org.elasticsearch.ElasticSearchGenerationException;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.WriteConsistencyLevel;
import org.elasticsearch.action.support.replication.IndicesReplicationOperationRequest;
import org.elasticsearch.action.support.replication.ReplicationType;
import org.elasticsearch.client.Requests;
-import org.elasticsearch.common.*;
-import org.elasticsearch.common.io.BytesStream;
+import org.elasticsearch.common.Nullable;
+import org.elasticsearch.common.Required;
+import org.elasticsearch.common.Strings;
+import org.elasticsearch.common.bytes.BytesArray;
+import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
+import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
@@ -57,9 +61,7 @@
private static final XContentType contentType = Requests.CONTENT_TYPE;
- private byte[] querySource;
- private int querySourceOffset;
- private int querySourceLength;
+ private BytesReference querySource;
private boolean querySourceUnsafe;
private String[] types = Strings.EMPTY_ARRAY;
@@ -106,13 +108,11 @@ public DeleteByQueryRequest indices(String... indices) {
/**
* The query source to execute.
*/
- BytesHolder querySource() {
+ BytesReference querySource() {
if (querySourceUnsafe) {
- querySource = Arrays.copyOfRange(querySource, querySourceOffset, querySourceOffset + querySourceLength);
- querySourceOffset = 0;
- querySourceUnsafe = false;
+ querySource = querySource.copyBytesArray();
}
- return new BytesHolder(querySource, querySourceOffset, querySourceLength);
+ return querySource;
}
/**
@@ -122,10 +122,7 @@ BytesHolder querySource() {
*/
@Required
public DeleteByQueryRequest query(QueryBuilder queryBuilder) {
- BytesStream bos = queryBuilder.buildAsBytes();
- this.querySource = bos.underlyingBytes();
- this.querySourceOffset = 0;
- this.querySourceLength = bos.size();
+ this.querySource = queryBuilder.buildAsBytes();
this.querySourceUnsafe = false;
return this;
}
@@ -136,11 +133,8 @@ public DeleteByQueryRequest query(QueryBuilder queryBuilder) {
*/
@Required
public DeleteByQueryRequest query(String querySource) {
- UnicodeUtil.UTF8Result result = Unicode.fromStringAsUtf8(querySource);
- this.querySource = result.result;
- this.querySourceOffset = 0;
- this.querySourceLength = result.length;
- this.querySourceUnsafe = true;
+ this.querySource = new BytesArray(querySource.getBytes(Charsets.UTF_8));
+ this.querySourceUnsafe = false;
return this;
}
@@ -160,15 +154,9 @@ public DeleteByQueryRequest query(Map querySource) {
@Required
public DeleteByQueryRequest query(XContentBuilder builder) {
- try {
- this.querySource = builder.underlyingBytes();
- this.querySourceOffset = 0;
- this.querySourceLength = builder.underlyingBytesLength();
- this.querySourceUnsafe = false;
- return this;
- } catch (IOException e) {
- throw new ElasticSearchGenerationException("Failed to generate [" + builder + "]", e);
- }
+ this.querySource = builder.bytes();
+ this.querySourceUnsafe = false;
+ return this;
}
/**
@@ -184,9 +172,13 @@ public DeleteByQueryRequest query(byte[] querySource) {
*/
@Required
public DeleteByQueryRequest query(byte[] querySource, int offset, int length, boolean unsafe) {
- this.querySource = querySource;
- this.querySourceOffset = offset;
- this.querySourceLength = length;
+ this.querySource = new BytesArray(querySource, offset, length);
+ this.querySourceUnsafe = unsafe;
+ return this;
+ }
+
+ public DeleteByQueryRequest query(BytesReference source, boolean unsafe) {
+ this.querySource = source;
this.querySourceUnsafe = unsafe;
return this;
}
@@ -270,11 +262,8 @@ public DeleteByQueryRequest replicationType(String replicationType) {
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
- BytesHolder bytes = in.readBytesReference();
querySourceUnsafe = false;
- querySource = bytes.bytes();
- querySourceOffset = bytes.offset();
- querySourceLength = bytes.length();
+ querySource = in.readBytesReference();
if (in.readBoolean()) {
routing = in.readUTF();
@@ -294,7 +283,7 @@ public void readFrom(StreamInput in) throws IOException {
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
- out.writeBytesHolder(querySource, querySourceOffset, querySourceLength);
+ out.writeBytesReference(querySource, true);
if (routing == null) {
out.writeBoolean(false);
@@ -311,6 +300,12 @@ public void writeTo(StreamOutput out) throws IOException {
@Override
public String toString() {
- return "[" + Arrays.toString(indices) + "][" + Arrays.toString(types) + "], querySource[" + Unicode.fromBytes(querySource) + "]";
+ String sSource = "_na_";
+ try {
+ sSource = XContentHelper.convertToJson(querySource, false);
+ } catch (Exception e) {
+ // ignore
+ }
+ return "[" + Arrays.toString(indices) + "][" + Arrays.toString(types) + "], querySource[" + sSource + "]";
}
}
View
17 src/main/java/org/elasticsearch/action/deletebyquery/DeleteByQueryRequestBuilder.java
@@ -24,6 +24,7 @@
import org.elasticsearch.action.support.BaseRequestBuilder;
import org.elasticsearch.action.support.replication.ReplicationType;
import org.elasticsearch.client.Client;
+import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.QueryBuilder;
@@ -118,6 +119,22 @@ public DeleteByQueryRequestBuilder setQuery(byte[] querySource) {
/**
* The query source to execute.
*/
+ public DeleteByQueryRequestBuilder setQuery(BytesReference querySource) {
+ request.query(querySource, false);
+ return this;
+ }
+
+ /**
+ * The query source to execute.
+ */
+ public DeleteByQueryRequestBuilder setQuery(BytesReference querySource, boolean unsafe) {
+ request.query(querySource, unsafe);
+ return this;
+ }
+
+ /**
+ * The query source to execute.
+ */
public DeleteByQueryRequestBuilder setQuery(byte[] querySource, int offset, int length, boolean unsafe) {
request.query(querySource, offset, length, unsafe);
return this;
View
8 src/main/java/org/elasticsearch/action/deletebyquery/IndexDeleteByQueryRequest.java
@@ -22,9 +22,9 @@
import gnu.trove.set.hash.THashSet;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.replication.IndexReplicationOperationRequest;
-import org.elasticsearch.common.BytesHolder;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
+import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.unit.TimeValue;
@@ -39,7 +39,7 @@
*/
public class IndexDeleteByQueryRequest extends IndexReplicationOperationRequest {
- private BytesHolder querySource;
+ private BytesReference querySource;
private String[] types = Strings.EMPTY_ARRAY;
@Nullable
private Set<String> routing;
@@ -60,7 +60,7 @@
IndexDeleteByQueryRequest() {
}
- BytesHolder querySource() {
+ BytesReference querySource() {
return querySource;
}
@@ -118,7 +118,7 @@ public void readFrom(StreamInput in) throws IOException {
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
- out.writeBytesHolder(querySource);
+ out.writeBytesReference(querySource, true);
out.writeVInt(types.length);
for (String type : types) {
out.writeUTF(type);
View
12 src/main/java/org/elasticsearch/action/deletebyquery/ShardDeleteByQueryRequest.java
@@ -22,12 +22,12 @@
import gnu.trove.set.hash.THashSet;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.replication.ShardReplicationOperationRequest;
-import org.elasticsearch.common.BytesHolder;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
-import org.elasticsearch.common.Unicode;
+import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
+import org.elasticsearch.common.xcontent.XContentHelper;
import java.io.IOException;
import java.util.Arrays;
@@ -41,7 +41,7 @@
public class ShardDeleteByQueryRequest extends ShardReplicationOperationRequest {
private int shardId;
- private BytesHolder querySource;
+ private BytesReference querySource;
private String[] types = Strings.EMPTY_ARRAY;
@Nullable
private Set<String> routing;
@@ -76,7 +76,7 @@ public int shardId() {
return this.shardId;
}
- BytesHolder querySource() {
+ BytesReference querySource() {
return querySource;
}
@@ -123,7 +123,7 @@ public void readFrom(StreamInput in) throws IOException {
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
- out.writeBytesHolder(querySource);
+ out.writeBytesReference(querySource, true);
out.writeVInt(shardId);
out.writeVInt(types.length);
for (String type : types) {
@@ -151,7 +151,7 @@ public void writeTo(StreamOutput out) throws IOException {
public String toString() {
String sSource = "_na_";
try {
- sSource = Unicode.fromBytes(querySource.bytes(), querySource.offset(), querySource.length());
+ sSource = XContentHelper.convertToJson(querySource, false);
} catch (Exception e) {
// ignore
}
View
6 src/main/java/org/elasticsearch/action/get/GetResponse.java
@@ -21,7 +21,7 @@
import org.elasticsearch.ElasticSearchParseException;
import org.elasticsearch.action.ActionResponse;
-import org.elasticsearch.common.BytesHolder;
+import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
@@ -138,14 +138,14 @@ public long getVersion() {
/**
* Returns bytes reference, also un compress the source if needed.
*/
- public BytesHolder sourceRef() {
+ public BytesReference sourceRef() {
return getResult.sourceRef();
}
/**
* Returns bytes reference, also un compress the source if needed.
*/
- public BytesHolder getSourceAsBytesRef() {
+ public BytesReference getSourceAsBytesRef() {
return sourceRef();
}
View
101 src/main/java/org/elasticsearch/action/index/IndexRequest.java
@@ -19,7 +19,7 @@
package org.elasticsearch.action.index;
-import org.apache.lucene.util.UnicodeUtil;
+import com.google.common.base.Charsets;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.ElasticSearchGenerationException;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
@@ -32,20 +32,19 @@
import org.elasticsearch.client.Requests;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
-import org.elasticsearch.common.*;
+import org.elasticsearch.common.Nullable;
+import org.elasticsearch.common.Required;
+import org.elasticsearch.common.UUID;
+import org.elasticsearch.common.bytes.BytesArray;
+import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.unit.TimeValue;
-import org.elasticsearch.common.xcontent.XContentBuilder;
-import org.elasticsearch.common.xcontent.XContentFactory;
-import org.elasticsearch.common.xcontent.XContentParser;
-import org.elasticsearch.common.xcontent.XContentType;
-import org.elasticsearch.common.xcontent.XContentHelper;
+import org.elasticsearch.common.xcontent.*;
import org.elasticsearch.index.VersionType;
import org.elasticsearch.index.mapper.internal.TimestampFieldMapper;
import java.io.IOException;
-import java.util.Arrays;
import java.util.Map;
import static org.elasticsearch.action.ValidateActions.addValidationError;
@@ -121,9 +120,7 @@ public static OpType fromId(byte id) {
private String timestamp;
private long ttl = -1;
- private byte[] source;
- private int sourceOffset;
- private int sourceLength;
+ private BytesReference source;
private boolean sourceUnsafe;
private OpType opType = OpType.INDEX;
@@ -177,9 +174,7 @@ public ActionRequestValidationException validate() {
@Override
public void beforeLocalFork() {
// only fork if copy over if source is unsafe
- if (sourceUnsafe) {
- source();
- }
+ safeSource();
}
/**
@@ -319,44 +314,21 @@ public long ttl() {
}
/**
- * The source of the document to index, recopied to a new array if it has an offset or unsafe.
+ * The source of the document to index, recopied to a new array if it is unsage.
*/
- public byte[] source() {
- if (sourceUnsafe || sourceOffset > 0 || source.length != sourceLength) {
- source = Arrays.copyOfRange(source, sourceOffset, sourceOffset + sourceLength);
- sourceOffset = 0;
- sourceUnsafe = false;
- }
+ public BytesReference source() {
return source;
}
- public BytesHolder underlyingSourceBytes() {
- return new BytesHolder(underlyingSource(), underlyingSourceOffset(), underlyingSourceLength());
- }
-
- public Map<String, Object> underlyingSourceAsMap() {
- return XContentHelper.convertToMap(underlyingSource(), underlyingSourceOffset(), underlyingSourceLength(), false).v2();
- }
-
- public byte[] underlyingSource() {
+ public BytesReference safeSource() {
if (sourceUnsafe) {
- source();
+ source = source.copyBytesArray();
}
- return this.source;
- }
-
- public int underlyingSourceOffset() {
- if (sourceUnsafe) {
- source();
- }
- return this.sourceOffset;
+ return source;
}
- public int underlyingSourceLength() {
- if (sourceUnsafe) {
- source();
- }
- return this.sourceLength;
+ public Map<String, Object> sourceAsMap() {
+ return XContentHelper.convertToMap(source, false).v2();
}
/**
@@ -393,11 +365,8 @@ public IndexRequest source(Map source, XContentType contentType) throws ElasticS
*/
@Required
public IndexRequest source(String source) {
- UnicodeUtil.UTF8Result result = Unicode.fromStringAsUtf8(source);
- this.source = result.result;
- this.sourceOffset = 0;
- this.sourceLength = result.length;
- this.sourceUnsafe = true;
+ this.source = new BytesArray(source.getBytes(Charsets.UTF_8));
+ this.sourceUnsafe = false;
return this;
}
@@ -406,14 +375,8 @@ public IndexRequest source(String source) {
*/
@Required
public IndexRequest source(XContentBuilder sourceBuilder) {
- try {
- source = sourceBuilder.underlyingBytes();
- sourceOffset = 0;
- sourceLength = sourceBuilder.underlyingBytesLength();
- sourceUnsafe = false;
- } catch (IOException e) {
- throw new ElasticSearchGenerationException("Failed to generate [" + sourceBuilder + "]", e);
- }
+ source = sourceBuilder.bytes();
+ sourceUnsafe = false;
return this;
}
@@ -464,6 +427,15 @@ public IndexRequest source(String field1, Object value1, String field2, Object v
/**
* Sets the document to index in bytes form.
*/
+ public IndexRequest source(BytesReference source, boolean unsafe) {
+ this.source = source;
+ this.sourceUnsafe = unsafe;
+ return this;
+ }
+
+ /**
+ * Sets the document to index in bytes form.
+ */
public IndexRequest source(byte[] source) {
return source(source, 0, source.length);
}
@@ -491,9 +463,7 @@ public IndexRequest source(byte[] source, int offset, int length) {
*/
@Required
public IndexRequest source(byte[] source, int offset, int length, boolean unsafe) {
- this.source = source;
- this.sourceOffset = offset;
- this.sourceLength = length;
+ this.source = new BytesArray(source, offset, length);
this.sourceUnsafe = unsafe;
return this;
}
@@ -647,7 +617,7 @@ public void process(MetaData metaData, String aliasOrIndex, @Nullable MappingMet
if (parseContext.shouldParse()) {
XContentParser parser = null;
try {
- parser = XContentFactory.xContent(source, sourceOffset, sourceLength).createParser(source, sourceOffset, sourceLength);
+ parser = XContentHelper.createParser(source);
mappingMd.parse(parser, parseContext);
if (parseContext.shouldParseId()) {
id = parseContext.id();
@@ -706,11 +676,8 @@ public void readFrom(StreamInput in) throws IOException {
timestamp = in.readUTF();
}
ttl = in.readLong();
- BytesHolder bytes = in.readBytesReference();
+ source = in.readBytesReference();
sourceUnsafe = false;
- source = bytes.bytes();
- sourceOffset = bytes.offset();
- sourceLength = bytes.length();
opType = OpType.fromId(in.readByte());
refresh = in.readBoolean();
@@ -750,7 +717,7 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeUTF(timestamp);
}
out.writeLong(ttl);
- out.writeBytesHolder(source, sourceOffset, sourceLength);
+ out.writeBytesReference(source, true);
out.writeByte(opType.id());
out.writeBoolean(refresh);
out.writeLong(version);
@@ -767,7 +734,7 @@ public void writeTo(StreamOutput out) throws IOException {
public String toString() {
String sSource = "_na_";
try {
- sSource = Unicode.fromBytes(source, sourceOffset, sourceLength);
+ sSource = XContentHelper.convertToJson(source, false);
} catch (Exception e) {
// ignore
}
View
17 src/main/java/org/elasticsearch/action/index/IndexRequestBuilder.java
@@ -25,6 +25,7 @@
import org.elasticsearch.action.support.replication.ReplicationType;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.Nullable;
+import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
@@ -89,6 +90,22 @@ public IndexRequestBuilder setParent(String parent) {
}
/**
+ * Sets the source.
+ */
+ public IndexRequestBuilder setSource(BytesReference source, boolean unsafe) {
+ request.source(source, unsafe);
+ return this;
+ }
+
+ /**
+ * Sets the source.
+ */
+ public IndexRequestBuilder setSource(BytesReference source) {
+ request.source(source, false);
+ return this;
+ }
+
+ /**
* Index the Map as a JSON.
*
* @param source The map to index
View
4 src/main/java/org/elasticsearch/action/index/TransportIndexAction.java
@@ -194,7 +194,7 @@ protected ShardIterator shards(ClusterState clusterState, IndexRequest request)
}
IndexShard indexShard = indicesService.indexServiceSafe(shardRequest.request.index()).shardSafe(shardRequest.shardId);
- SourceToParse sourceToParse = SourceToParse.source(request.underlyingSource(), request.underlyingSourceOffset(), request.underlyingSourceLength()).type(request.type()).id(request.id())
+ SourceToParse sourceToParse = SourceToParse.source(request.source()).type(request.type()).id(request.id())
.routing(request.routing()).parent(request.parent()).timestamp(request.timestamp()).ttl(request.ttl());
long version;
Engine.IndexingOperation op;
@@ -251,7 +251,7 @@ protected void postPrimaryOperation(IndexRequest request, PrimaryResponse<IndexR
protected void shardOperationOnReplica(ReplicaOperationRequest shardRequest) {
IndexShard indexShard = indicesService.indexServiceSafe(shardRequest.request.index()).shardSafe(shardRequest.shardId);
IndexRequest request = shardRequest.request;
- SourceToParse sourceToParse = SourceToParse.source(request.underlyingSource(), request.underlyingSourceOffset(), request.underlyingSourceLength()).type(request.type()).id(request.id())
+ SourceToParse sourceToParse = SourceToParse.source(request.source()).type(request.type()).id(request.id())
.routing(request.routing()).parent(request.parent()).timestamp(request.timestamp()).ttl(request.ttl());
if (request.opType() == IndexRequest.OpType.INDEX) {
Engine.Index index = indexShard.prepareIndex(sourceToParse)
View
76 src/main/java/org/elasticsearch/action/mlt/MoreLikeThisRequest.java
@@ -19,19 +19,17 @@
package org.elasticsearch.action.mlt;
-import org.apache.lucene.util.UnicodeUtil;
import org.elasticsearch.ElasticSearchGenerationException;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.ValidateActions;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Requests;
-import org.elasticsearch.common.Bytes;
import org.elasticsearch.common.Required;
import org.elasticsearch.common.Strings;
-import org.elasticsearch.common.Unicode;
-import org.elasticsearch.common.io.BytesStream;
+import org.elasticsearch.common.bytes.BytesArray;
+import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -41,7 +39,6 @@
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
-import java.util.Arrays;
import java.util.Map;
import static org.elasticsearch.search.Scroll.readScroll;
@@ -86,9 +83,7 @@
private String[] searchTypes;
private Scroll searchScroll;
- private byte[] searchSource;
- private int searchSourceOffset;
- private int searchSourceLength;
+ private BytesReference searchSource;
private boolean searchSourceUnsafe;
private boolean threadedListener = false;
@@ -315,8 +310,7 @@ public float boostTerms() {
void beforeLocalFork() {
if (searchSourceUnsafe) {
- searchSource = Arrays.copyOfRange(searchSource, searchSourceOffset, searchSourceOffset + searchSourceLength);
- searchSourceOffset = 0;
+ searchSource = searchSource.copyBytesArray();
searchSourceUnsafe = false;
}
}
@@ -326,11 +320,8 @@ void beforeLocalFork() {
* more like this documents.
*/
public MoreLikeThisRequest searchSource(SearchSourceBuilder sourceBuilder) {
- BytesStream bos = sourceBuilder.buildAsBytesStream(Requests.CONTENT_TYPE);
- this.searchSource = bos.underlyingBytes();
- this.searchSourceOffset = 0;
- this.searchSourceLength = bos.size();
- this.searchSourceUnsafe = true;
+ this.searchSource = sourceBuilder.buildAsBytes(Requests.CONTENT_TYPE);
+ this.searchSourceUnsafe = false;
return this;
}
@@ -339,11 +330,8 @@ public MoreLikeThisRequest searchSource(SearchSourceBuilder sourceBuilder) {
* more like this documents.
*/
public MoreLikeThisRequest searchSource(String searchSource) {
- UnicodeUtil.UTF8Result result = Unicode.fromStringAsUtf8(searchSource);
- this.searchSource = result.result;
- this.searchSourceOffset = 0;
- this.searchSourceLength = result.length;
- this.searchSourceUnsafe = true;
+ this.searchSource = new BytesArray(searchSource);
+ this.searchSourceUnsafe = false;
return this;
}
@@ -358,15 +346,9 @@ public MoreLikeThisRequest searchSource(Map searchSource) {
}
public MoreLikeThisRequest searchSource(XContentBuilder builder) {
- try {
- this.searchSource = builder.underlyingBytes();
- this.searchSourceOffset = 0;
- this.searchSourceLength = builder.underlyingBytesLength();
- this.searchSourceUnsafe = false;
- return this;
- } catch (IOException e) {
- throw new ElasticSearchGenerationException("Failed to generate [" + builder + "]", e);
- }
+ this.searchSource = builder.bytes();
+ this.searchSourceUnsafe = false;
+ return this;
}
/**
@@ -382,9 +364,15 @@ public MoreLikeThisRequest searchSource(byte[] searchSource) {
* more like this documents.
*/
public MoreLikeThisRequest searchSource(byte[] searchSource, int offset, int length, boolean unsafe) {
+ return searchSource(new BytesArray(searchSource, offset, length), unsafe);
+ }
+
+ /**
+ * An optional search source request allowing to control the search request for the
+ * more like this documents.
+ */
+ public MoreLikeThisRequest searchSource(BytesReference searchSource, boolean unsafe) {
this.searchSource = searchSource;
- this.searchSourceOffset = offset;
- this.searchSourceLength = length;
this.searchSourceUnsafe = unsafe;
return this;
}