use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class RefreshListenersTests method index.
private Engine.IndexResult index(String id, String testFieldValue) throws IOException {
String type = "test";
String uid = type + ":" + id;
Document document = new Document();
document.add(new TextField("test", testFieldValue, Field.Store.YES));
Field uidField = new Field("_uid", Uid.createUid(type, id), UidFieldMapper.Defaults.FIELD_TYPE);
Field versionField = new NumericDocValuesField("_version", Versions.MATCH_ANY);
SeqNoFieldMapper.SequenceID seqID = SeqNoFieldMapper.SequenceID.emptySeqID();
document.add(uidField);
document.add(versionField);
document.add(seqID.seqNo);
document.add(seqID.seqNoDocValue);
document.add(seqID.primaryTerm);
BytesReference source = new BytesArray(new byte[] { 1 });
ParsedDocument doc = new ParsedDocument(versionField, seqID, id, type, null, Arrays.asList(document), source, XContentType.JSON, null);
Engine.Index index = new Engine.Index(new Term("_uid", doc.uid()), doc);
return engine.index(index);
}
use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class TranslogTests method testTranslogOpSerialization.
public void testTranslogOpSerialization() throws Exception {
BytesReference B_1 = new BytesArray(new byte[] { 1 });
SeqNoFieldMapper.SequenceID seqID = SeqNoFieldMapper.SequenceID.emptySeqID();
assert Version.CURRENT.major <= 6 : "Using UNASSIGNED_SEQ_NO can be removed in 7.0, because 6.0+ nodes have actual sequence numbers";
long randomSeqNum = randomBoolean() ? SequenceNumbersService.UNASSIGNED_SEQ_NO : randomNonNegativeLong();
long randomPrimaryTerm = randomBoolean() ? 0 : randomNonNegativeLong();
seqID.seqNo.setLongValue(randomSeqNum);
seqID.seqNoDocValue.setLongValue(randomSeqNum);
seqID.primaryTerm.setLongValue(randomPrimaryTerm);
Field uidField = new Field("_uid", Uid.createUid("test", "1"), UidFieldMapper.Defaults.FIELD_TYPE);
Field versionField = new NumericDocValuesField("_version", 1);
Document document = new Document();
document.add(new TextField("value", "test", Field.Store.YES));
document.add(uidField);
document.add(versionField);
document.add(seqID.seqNo);
document.add(seqID.seqNoDocValue);
document.add(seqID.primaryTerm);
ParsedDocument doc = new ParsedDocument(versionField, seqID, "1", "type", null, Arrays.asList(document), B_1, XContentType.JSON, null);
Engine.Index eIndex = new Engine.Index(newUid(doc), doc, randomSeqNum, randomPrimaryTerm, 1, VersionType.INTERNAL, Origin.PRIMARY, 0, 0, false);
Engine.IndexResult eIndexResult = new Engine.IndexResult(1, randomSeqNum, true);
Translog.Index index = new Translog.Index(eIndex, eIndexResult);
BytesStreamOutput out = new BytesStreamOutput();
index.writeTo(out);
StreamInput in = out.bytes().streamInput();
Translog.Index serializedIndex = new Translog.Index(in);
assertEquals(index, serializedIndex);
Engine.Delete eDelete = new Engine.Delete(doc.type(), doc.id(), newUid(doc), randomSeqNum, randomPrimaryTerm, 2, VersionType.INTERNAL, Origin.PRIMARY, 0);
Engine.DeleteResult eDeleteResult = new Engine.DeleteResult(2, randomSeqNum, true);
Translog.Delete delete = new Translog.Delete(eDelete, eDeleteResult);
out = new BytesStreamOutput();
delete.writeTo(out);
in = out.bytes().streamInput();
Translog.Delete serializedDelete = new Translog.Delete(in);
assertEquals(delete, serializedDelete);
}
use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class TranslogTests method testCloseIntoReader.
public void testCloseIntoReader() throws IOException {
try (TranslogWriter writer = translog.createWriter(0)) {
final int numOps = randomIntBetween(8, 128);
final byte[] bytes = new byte[4];
final ByteArrayDataOutput out = new ByteArrayDataOutput(bytes);
for (int i = 0; i < numOps; i++) {
out.reset(bytes);
out.writeInt(i);
writer.add(new BytesArray(bytes), randomNonNegativeLong());
}
writer.sync();
final Checkpoint writerCheckpoint = writer.getCheckpoint();
try (TranslogReader reader = writer.closeIntoReader()) {
for (int i = 0; i < numOps; i++) {
final ByteBuffer buffer = ByteBuffer.allocate(4);
reader.readBytes(buffer, reader.getFirstOperationOffset() + 4 * i);
buffer.flip();
final int value = buffer.getInt();
assertEquals(i, value);
}
final Checkpoint readerCheckpoint = reader.getCheckpoint();
assertThat(readerCheckpoint, equalTo(writerCheckpoint));
}
}
}
use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class SimpleIndexTemplateIT method testIndexTemplateWithAliasesInSource.
public void testIndexTemplateWithAliasesInSource() {
client().admin().indices().preparePutTemplate("template_1").setSource(new BytesArray("{\n" + " \"template\" : \"*\",\n" + " \"aliases\" : {\n" + " \"my_alias\" : {\n" + " \"filter\" : {\n" + " \"type\" : {\n" + " \"value\" : \"type2\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + "}"), XContentType.JSON).get();
assertAcked(prepareCreate("test_index").addMapping("type1").addMapping("type2"));
ensureGreen();
GetAliasesResponse getAliasesResponse = client().admin().indices().prepareGetAliases().setIndices("test_index").get();
assertThat(getAliasesResponse.getAliases().size(), equalTo(1));
assertThat(getAliasesResponse.getAliases().get("test_index").size(), equalTo(1));
client().prepareIndex("test_index", "type1", "1").setSource("field", "value1").get();
client().prepareIndex("test_index", "type2", "2").setSource("field", "value2").get();
refresh();
SearchResponse searchResponse = client().prepareSearch("test_index").get();
assertHitCount(searchResponse, 2L);
searchResponse = client().prepareSearch("my_alias").get();
assertHitCount(searchResponse, 1L);
assertThat(searchResponse.getHits().getAt(0).getType(), equalTo("type2"));
}
use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class PipelineStoreTests method testDeleteUsingWildcard.
public void testDeleteUsingWildcard() {
HashMap<String, PipelineConfiguration> pipelines = new HashMap<>();
BytesArray definition = new BytesArray("{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"_value\"}}]}");
pipelines.put("p1", new PipelineConfiguration("p1", definition, XContentType.JSON));
pipelines.put("p2", new PipelineConfiguration("p2", definition, XContentType.JSON));
pipelines.put("q1", new PipelineConfiguration("q1", definition, XContentType.JSON));
IngestMetadata ingestMetadata = new IngestMetadata(pipelines);
ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).build();
ClusterState previousClusterState = clusterState;
clusterState = ClusterState.builder(clusterState).metaData(MetaData.builder().putCustom(IngestMetadata.TYPE, ingestMetadata)).build();
store.innerUpdatePipelines(previousClusterState, clusterState);
assertThat(store.get("p1"), notNullValue());
assertThat(store.get("p2"), notNullValue());
assertThat(store.get("q1"), notNullValue());
// Delete pipeline matching wildcard
DeletePipelineRequest deleteRequest = new DeletePipelineRequest("p*");
previousClusterState = clusterState;
clusterState = store.innerDelete(deleteRequest, clusterState);
store.innerUpdatePipelines(previousClusterState, clusterState);
assertThat(store.get("p1"), nullValue());
assertThat(store.get("p2"), nullValue());
assertThat(store.get("q1"), notNullValue());
// Exception if we used name which does not exist
try {
store.innerDelete(new DeletePipelineRequest("unknown"), clusterState);
fail("exception expected");
} catch (ResourceNotFoundException e) {
assertThat(e.getMessage(), equalTo("pipeline [unknown] is missing"));
}
// match all wildcard works on last remaining pipeline
DeletePipelineRequest matchAllDeleteRequest = new DeletePipelineRequest("*");
previousClusterState = clusterState;
clusterState = store.innerDelete(matchAllDeleteRequest, clusterState);
store.innerUpdatePipelines(previousClusterState, clusterState);
assertThat(store.get("p1"), nullValue());
assertThat(store.get("p2"), nullValue());
assertThat(store.get("q1"), nullValue());
// match all wildcard does not throw exception if none match
store.innerDelete(matchAllDeleteRequest, clusterState);
}
Aggregations