Skip to content

Commit

Permalink
Merge pull request searchbox-io#523 from logzio/add-rollover-action
Browse files Browse the repository at this point in the history
Add rollover action
  • Loading branch information
ferhatsb authored Jul 30, 2017
2 parents 4881377 + 5fcc83e commit 545b437
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 0 deletions.
69 changes: 69 additions & 0 deletions jest-common/src/main/java/io/searchbox/indices/Rollover.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.searchbox.indices;

import io.searchbox.action.AbstractAction;
import io.searchbox.action.GenericResultAbstractAction;

import java.util.HashMap;
import java.util.Map;

public class Rollover extends GenericResultAbstractAction {

protected Rollover(Rollover.Builder builder) {
super(builder);

this.indexName = builder.index;
Map<String, Object> rolloverConditions = new HashMap<>();
if (builder.conditions != null) {
rolloverConditions.put("conditions", builder.conditions);
}
if (builder.settings != null) {
rolloverConditions.put("settings", builder.settings);
}
this.payload = rolloverConditions;

setURI(buildURI() + (builder.isDryRun ? "?dry_run" : ""));
}

@Override
public String getRestMethodName() {
return "POST";
}

@Override
protected String buildURI() {
return super.buildURI() + "/_rollover";
}


public static class Builder extends AbstractAction.Builder<Rollover, Rollover.Builder> {

private String index;
private Object conditions;
private Object settings;
private boolean isDryRun;

public Builder(String index) {
this.index = index;
}

public Rollover.Builder conditions(Object conditions) {
this.conditions = conditions;
return this;
}

public Rollover.Builder setDryRun(boolean dryRun) {
this.isDryRun = dryRun;
return this;
}

public Rollover.Builder settings(Object settings) {
this.settings = settings;
return this;
}

@Override
public Rollover build() {
return new Rollover(this);
}
}
}
61 changes: 61 additions & 0 deletions jest-common/src/test/java/io/searchbox/indices/RolloverTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.searchbox.indices;

import com.google.gson.Gson;
import org.elasticsearch.common.collect.MapBuilder;
import org.junit.Test;

import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

public class RolloverTest {

Map<String, Object> rolloverConditions = new MapBuilder<String, Object>()
.put("max_docs", "10000")
.put("max_age", "1d")
.immutableMap();
Map<String, Object> rolloverSettings = new MapBuilder<String, Object>()
.put("index.number_of_shards", "2")
.immutableMap();


@Test
public void testBasicUriGeneration() {
Rollover rollover = new Rollover.Builder("twitter").conditions(rolloverConditions).build();
assertEquals("POST", rollover.getRestMethodName());
assertEquals("twitter/_rollover", rollover.getURI());
assertEquals("{\"conditions\":{\"max_age\":\"1d\",\"max_docs\":\"10000\"}}", rollover.getData(new Gson()));
}

@Test
public void testBasicUriWithSettingsGeneration() {
Rollover rollover = new Rollover.Builder("twitter").conditions(rolloverConditions).settings(rolloverSettings).build();
assertEquals("POST", rollover.getRestMethodName());
assertEquals("twitter/_rollover", rollover.getURI());
assertEquals("{\"settings\":{\"index.number_of_shards\":\"2\"},\"conditions\":{\"max_age\":\"1d\",\"max_docs\":\"10000\"}}", rollover.getData(new Gson()));
}

@Test
public void testDryRunUriGeneration() {
Rollover rollover = new Rollover.Builder("twitter").conditions(rolloverConditions).setDryRun(true).build();
assertEquals("POST", rollover.getRestMethodName());
assertEquals("twitter/_rollover?dry_run", rollover.getURI());
}

@Test
public void equalsReturnsTrueForSameDestination() {
Rollover indexRollover1 = new Rollover.Builder("twitter").conditions(rolloverConditions).build();
Rollover indexRollover2 = new Rollover.Builder("twitter").conditions(rolloverConditions).build();

assertEquals(indexRollover1, indexRollover2);
}

@Test
public void equalsReturnsFalseForDifferentIndex() {
Rollover indexRollover1 = new Rollover.Builder("twitter").conditions(rolloverConditions).build();
Rollover indexRollover2 = new Rollover.Builder("myspace").conditions(rolloverConditions).build();

assertNotEquals(indexRollover1, indexRollover2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.searchbox.indices;

import io.searchbox.client.JestResult;
import io.searchbox.common.AbstractIntegrationTest;
import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.test.ESIntegTestCase;
import org.junit.Test;

import java.io.IOException;
import java.util.Map;

@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.SUITE, numDataNodes = 1)
public class RolloverIntegrationTest extends AbstractIntegrationTest {

private final Map<String, Object> rolloverConditions = new MapBuilder<String, Object>()
.put("max_docs", "1")
.put("max_age", "1d")
.immutableMap();

@Test
public void testRollover() throws IOException {
String aliasSetting = "{ \"aliases\": { \"rollover-test-index\": {} } }";
CreateIndex createIndex = new CreateIndex.Builder("rollover-test-index-000001").settings(aliasSetting).build();

JestResult result = client.execute(createIndex);
assertTrue(result.getErrorMessage(), result.isSucceeded());

Rollover rollover = new Rollover.Builder("rollover-test-index").conditions(rolloverConditions).build();

result = client.execute(rollover);
assertTrue(result.getErrorMessage(), result.isSucceeded());
}
}

0 comments on commit 545b437

Please sign in to comment.