From 0f6c24d0c5bd9197de4397e5ddd1dd3edeb9b524 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Tue, 18 Mar 2014 20:50:31 +0100 Subject: [PATCH] BytesReference usage to properly work when hasArray is not available when a BytesReference doesn't have a backing array, properly handle the case in places where its applicable closes #5455 --- .../admin/indices/mapping/get/GetFieldMappingsResponse.java | 2 +- .../gateway/local/state/meta/LocalGatewayMetaState.java | 4 ++-- .../gateway/local/state/shards/LocalGatewayShardsState.java | 4 ++-- .../index/mapper/internal/SourceFieldMapper.java | 3 +++ .../java/org/elasticsearch/index/translog/fs/FsTranslog.java | 4 ++++ .../elasticsearch/index/mapper/all/SimpleAllMapperTests.java | 2 +- 6 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetFieldMappingsResponse.java b/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetFieldMappingsResponse.java index bd55901b9df..f59feec155c 100644 --- a/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetFieldMappingsResponse.java +++ b/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetFieldMappingsResponse.java @@ -105,7 +105,7 @@ public class GetFieldMappingsResponse extends ActionResponse implements ToXConte /** Returns the mappings as a map. Note that the returned map has a single key which is always the field's {@link Mapper#name}. */ public Map sourceAsMap() { - return XContentHelper.convertToMap(source.array(), source.arrayOffset(), source.length(), true).v2(); + return XContentHelper.convertToMap(source, true).v2(); } public boolean isNull() { diff --git a/src/main/java/org/elasticsearch/gateway/local/state/meta/LocalGatewayMetaState.java b/src/main/java/org/elasticsearch/gateway/local/state/meta/LocalGatewayMetaState.java index c3f42e5330c..e36cd3c87b8 100644 --- a/src/main/java/org/elasticsearch/gateway/local/state/meta/LocalGatewayMetaState.java +++ b/src/main/java/org/elasticsearch/gateway/local/state/meta/LocalGatewayMetaState.java @@ -356,7 +356,7 @@ public class LocalGatewayMetaState extends AbstractComponent implements ClusterS try { fos = new FileOutputStream(stateFile); BytesReference bytes = builder.bytes(); - fos.write(bytes.array(), bytes.arrayOffset(), bytes.length()); + bytes.writeTo(fos); fos.getChannel().force(true); fos.close(); wroteAtLeastOnce = true; @@ -412,7 +412,7 @@ public class LocalGatewayMetaState extends AbstractComponent implements ClusterS try { fos = new FileOutputStream(stateFile); BytesReference bytes = builder.bytes(); - fos.write(bytes.array(), bytes.arrayOffset(), bytes.length()); + bytes.writeTo(fos); fos.getChannel().force(true); fos.close(); wroteAtLeastOnce = true; diff --git a/src/main/java/org/elasticsearch/gateway/local/state/shards/LocalGatewayShardsState.java b/src/main/java/org/elasticsearch/gateway/local/state/shards/LocalGatewayShardsState.java index 96774ce8ceb..861da81f4ca 100644 --- a/src/main/java/org/elasticsearch/gateway/local/state/shards/LocalGatewayShardsState.java +++ b/src/main/java/org/elasticsearch/gateway/local/state/shards/LocalGatewayShardsState.java @@ -289,7 +289,7 @@ public class LocalGatewayShardsState extends AbstractComponent implements Cluste try { fos = new FileOutputStream(stateFile); BytesReference bytes = builder.bytes(); - fos.write(bytes.array(), bytes.arrayOffset(), bytes.length()); + bytes.writeTo(fos); fos.getChannel().force(true); fos.close(); wroteAtLeastOnce = true; @@ -301,7 +301,7 @@ public class LocalGatewayShardsState extends AbstractComponent implements Cluste } if (!wroteAtLeastOnce) { - logger.warn("[{}][{}]: failed to write shard state", shardId.index().name(), shardId.id(), lastFailure); + logger.warn("[{}][{}]: failed to write shard state", lastFailure, shardId.index().name(), shardId.id()); throw new IOException("failed to write shard state for " + shardId, lastFailure); } diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/SourceFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/SourceFieldMapper.java index e685a119c51..7ccce9ba2e1 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/SourceFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/SourceFieldMapper.java @@ -341,6 +341,9 @@ public class SourceFieldMapper extends AbstractFieldMapper implements In } } } + if (!source.hasArray()) { + source = source.toBytesArray(); + } assert source.hasArray(); fields.add(new StoredField(names().indexName(), source.array(), source.arrayOffset(), source.length())); } diff --git a/src/main/java/org/elasticsearch/index/translog/fs/FsTranslog.java b/src/main/java/org/elasticsearch/index/translog/fs/FsTranslog.java index 2d9907cfaa1..05223200ac9 100644 --- a/src/main/java/org/elasticsearch/index/translog/fs/FsTranslog.java +++ b/src/main/java/org/elasticsearch/index/translog/fs/FsTranslog.java @@ -350,6 +350,10 @@ public class FsTranslog extends AbstractIndexShardComponent implements Translog out.seek(size); BytesReference ref = out.bytes(); + // TODO: pass teh BytesReference to the FsTranslogFile and have them optimize writing + if (!ref.hasArray()) { + ref = ref.toBytesArray(); + } byte[] refBytes = ref.array(); int refBytesOffset = ref.arrayOffset(); Location location = current.add(refBytes, refBytesOffset, size); diff --git a/src/test/java/org/elasticsearch/index/mapper/all/SimpleAllMapperTests.java b/src/test/java/org/elasticsearch/index/mapper/all/SimpleAllMapperTests.java index 1f15caf8618..96c94c0dd91 100644 --- a/src/test/java/org/elasticsearch/index/mapper/all/SimpleAllMapperTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/all/SimpleAllMapperTests.java @@ -235,7 +235,7 @@ public class SimpleAllMapperTests extends ElasticsearchTestCase { .field("foo", "bar") .field("_id", 1) .field("foobar", "foobar") - .endObject().bytes().array(); + .endObject().bytes().toBytes(); Document doc = builtDocMapper.parse(new BytesArray(json)).rootDoc(); AllField field = (AllField) doc.getField("_all"); if (enabled) {