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
This commit is contained in:
Shay Banon 2014-03-18 20:50:31 +01:00
parent 7d3f49c43b
commit 0f6c24d0c5
6 changed files with 13 additions and 6 deletions

View File

@ -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}. */ /** 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<String, Object> sourceAsMap() { public Map<String, Object> sourceAsMap() {
return XContentHelper.convertToMap(source.array(), source.arrayOffset(), source.length(), true).v2(); return XContentHelper.convertToMap(source, true).v2();
} }
public boolean isNull() { public boolean isNull() {

View File

@ -356,7 +356,7 @@ public class LocalGatewayMetaState extends AbstractComponent implements ClusterS
try { try {
fos = new FileOutputStream(stateFile); fos = new FileOutputStream(stateFile);
BytesReference bytes = builder.bytes(); BytesReference bytes = builder.bytes();
fos.write(bytes.array(), bytes.arrayOffset(), bytes.length()); bytes.writeTo(fos);
fos.getChannel().force(true); fos.getChannel().force(true);
fos.close(); fos.close();
wroteAtLeastOnce = true; wroteAtLeastOnce = true;
@ -412,7 +412,7 @@ public class LocalGatewayMetaState extends AbstractComponent implements ClusterS
try { try {
fos = new FileOutputStream(stateFile); fos = new FileOutputStream(stateFile);
BytesReference bytes = builder.bytes(); BytesReference bytes = builder.bytes();
fos.write(bytes.array(), bytes.arrayOffset(), bytes.length()); bytes.writeTo(fos);
fos.getChannel().force(true); fos.getChannel().force(true);
fos.close(); fos.close();
wroteAtLeastOnce = true; wroteAtLeastOnce = true;

View File

@ -289,7 +289,7 @@ public class LocalGatewayShardsState extends AbstractComponent implements Cluste
try { try {
fos = new FileOutputStream(stateFile); fos = new FileOutputStream(stateFile);
BytesReference bytes = builder.bytes(); BytesReference bytes = builder.bytes();
fos.write(bytes.array(), bytes.arrayOffset(), bytes.length()); bytes.writeTo(fos);
fos.getChannel().force(true); fos.getChannel().force(true);
fos.close(); fos.close();
wroteAtLeastOnce = true; wroteAtLeastOnce = true;
@ -301,7 +301,7 @@ public class LocalGatewayShardsState extends AbstractComponent implements Cluste
} }
if (!wroteAtLeastOnce) { 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); throw new IOException("failed to write shard state for " + shardId, lastFailure);
} }

View File

@ -341,6 +341,9 @@ public class SourceFieldMapper extends AbstractFieldMapper<byte[]> implements In
} }
} }
} }
if (!source.hasArray()) {
source = source.toBytesArray();
}
assert source.hasArray(); assert source.hasArray();
fields.add(new StoredField(names().indexName(), source.array(), source.arrayOffset(), source.length())); fields.add(new StoredField(names().indexName(), source.array(), source.arrayOffset(), source.length()));
} }

View File

@ -350,6 +350,10 @@ public class FsTranslog extends AbstractIndexShardComponent implements Translog
out.seek(size); out.seek(size);
BytesReference ref = out.bytes(); 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(); byte[] refBytes = ref.array();
int refBytesOffset = ref.arrayOffset(); int refBytesOffset = ref.arrayOffset();
Location location = current.add(refBytes, refBytesOffset, size); Location location = current.add(refBytes, refBytesOffset, size);

View File

@ -235,7 +235,7 @@ public class SimpleAllMapperTests extends ElasticsearchTestCase {
.field("foo", "bar") .field("foo", "bar")
.field("_id", 1) .field("_id", 1)
.field("foobar", "foobar") .field("foobar", "foobar")
.endObject().bytes().array(); .endObject().bytes().toBytes();
Document doc = builtDocMapper.parse(new BytesArray(json)).rootDoc(); Document doc = builtDocMapper.parse(new BytesArray(json)).rootDoc();
AllField field = (AllField) doc.getField("_all"); AllField field = (AllField) doc.getField("_all");
if (enabled) { if (enabled) {