Example usage for org.apache.lucene.util BytesRef BytesRef

List of usage examples for org.apache.lucene.util BytesRef BytesRef

Introduction

In this page you can find the example usage for org.apache.lucene.util BytesRef BytesRef.

Prototype

public BytesRef(CharSequence text) 

Source Link

Document

Initialize the byte[] from the UTF8 bytes for the provided String.

Usage

From source file:io.crate.integrationtests.PartitionedTableIntegrationTest.java

License:Apache License

@Test
public void testAlterTableResetPartitionedTable() throws Exception {
    execute("create table quotes (id integer, quote string, date timestamp) "
            + "partitioned by(date) clustered into 3 shards with (number_of_replicas='1-all')");
    ensureYellow();//  w w  w  .j a  va 2  s. c  o m
    assertThat(response.rowCount(), is(1L));

    execute("insert into quotes (id, quote, date) values (?, ?, ?), (?, ?, ?)",
            new Object[] { 1, "Don't panic", 1395874800000L, 2, "Now panic", 1395961200000L });
    assertThat(response.rowCount(), is(2L));
    ensureYellow();
    refresh();

    execute("alter table quotes reset (number_of_replicas)");
    ensureYellow();

    String templateName = PartitionName.templateName(null, "quotes");
    GetIndexTemplatesResponse templatesResponse = client().admin().indices().prepareGetTemplates(templateName)
            .execute().actionGet();
    Settings templateSettings = templatesResponse.getIndexTemplates().get(0).getSettings();
    assertThat(templateSettings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0), is(1));
    assertThat(templateSettings.get(IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS), is("false"));

    List<String> partitions = ImmutableList.of(
            new PartitionName("quotes", Arrays.asList(new BytesRef("1395874800000"))).asIndexName(),
            new PartitionName("quotes", Arrays.asList(new BytesRef("1395961200000"))).asIndexName());
    Thread.sleep(1000);
    GetSettingsResponse settingsResponse = client().admin().indices()
            .prepareGetSettings(partitions.get(0), partitions.get(1)).execute().get();

    for (String index : partitions) {
        assertThat(settingsResponse.getSetting(index, IndexMetaData.SETTING_NUMBER_OF_REPLICAS), is("1"));
        assertThat(settingsResponse.getSetting(index, IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS), is("false"));
    }

}

From source file:io.crate.integrationtests.PartitionedTableIntegrationTest.java

License:Apache License

@Test
public void testAlterPartitionedTablePartition() throws Exception {
    execute("create table quotes (id integer, quote string, date timestamp) "
            + "partitioned by(date) clustered into 3 shards with (number_of_replicas=0)");
    ensureYellow();// w  ww  . jav a  2s  .c  o m
    assertThat(response.rowCount(), is(1L));

    execute("insert into quotes (id, quote, date) values (?, ?, ?), (?, ?, ?)",
            new Object[] { 1, "Don't panic", 1395874800000L, 2, "Now panic", 1395961200000L });
    assertThat(response.rowCount(), is(2L));
    ensureYellow();
    refresh();

    execute("alter table quotes partition (date=1395874800000) set (number_of_replicas=1)");
    ensureYellow();
    List<String> partitions = ImmutableList.of(
            new PartitionName("quotes", Arrays.asList(new BytesRef("1395874800000"))).asIndexName(),
            new PartitionName("quotes", Arrays.asList(new BytesRef("1395961200000"))).asIndexName());

    GetSettingsResponse settingsResponse = client().admin().indices()
            .prepareGetSettings(partitions.get(0), partitions.get(1)).execute().get();
    assertThat(settingsResponse.getSetting(partitions.get(0), IndexMetaData.SETTING_NUMBER_OF_REPLICAS),
            is("1"));
    assertThat(settingsResponse.getSetting(partitions.get(1), IndexMetaData.SETTING_NUMBER_OF_REPLICAS),
            is("0"));

    String templateName = PartitionName.templateName(null, "quotes");
    GetIndexTemplatesResponse templatesResponse = client().admin().indices().prepareGetTemplates(templateName)
            .execute().actionGet();
    Settings templateSettings = templatesResponse.getIndexTemplates().get(0).getSettings();
    assertThat(templateSettings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0), is(0));
    assertThat(templateSettings.get(IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS), is("false"));

}

From source file:io.crate.integrationtests.PartitionedTableIntegrationTest.java

License:Apache License

@Test
public void testAlterPartitionedTableKeepsMetadata() throws Exception {
    execute("create table dynamic_table (" + "  id integer, " + "  score double"
            + ") partitioned by (score) with (number_of_replicas=0, column_policy='dynamic')");
    ensureGreen();/*from w  w  w  .ja  v  a2 s. com*/
    execute("insert into dynamic_table (id, score) values (1, 10)");
    execute("refresh table dynamic_table");
    ensureGreen();
    MappingMetaData partitionMetaData = clusterService().state().metaData().indices()
            .get(new PartitionName("dynamic_table", Arrays.asList(new BytesRef("10.0"))).asIndexName())
            .getMappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(String.valueOf(partitionMetaData.getSourceAsMap().get("_meta")),
            Matchers.is("{partitioned_by=[[score, double]]}"));
    execute("alter table dynamic_table set (column_policy= 'dynamic')");
    waitNoPendingTasksOnAll();
    partitionMetaData = clusterService().state().metaData().indices()
            .get(new PartitionName("dynamic_table", Arrays.asList(new BytesRef("10.0"))).asIndexName())
            .getMappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(String.valueOf(partitionMetaData.getSourceAsMap().get("_meta")),
            Matchers.is("{partitioned_by=[[score, double]]}"));
}

From source file:io.crate.integrationtests.PartitionedTableIntegrationTest.java

License:Apache License

@Test
public void testAlterNumberOfShards() throws Exception {
    execute("create table quotes (" + "  id integer, " + "  quote string, " + "  date timestamp) "
            + "partitioned by(date) clustered into 3 shards with (number_of_replicas='0-all')");
    ensureYellow();/*from ww w.j  a  v  a  2 s  .c om*/
    assertThat(response.rowCount(), is(1L));

    String templateName = PartitionName.templateName(null, "quotes");
    GetIndexTemplatesResponse templatesResponse = client().admin().indices().prepareGetTemplates(templateName)
            .execute().actionGet();
    Settings templateSettings = templatesResponse.getIndexTemplates().get(0).getSettings();
    assertThat(templateSettings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0), is(1));
    assertThat(templateSettings.get(IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS), is("0-all"));
    assertThat(templateSettings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 0), is(3));

    execute("alter table quotes set (number_of_shards=6)");

    templatesResponse = client().admin().indices().prepareGetTemplates(templateName).execute().actionGet();
    templateSettings = templatesResponse.getIndexTemplates().get(0).getSettings();
    assertThat(templateSettings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0), is(1));
    assertThat(templateSettings.get(IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS), is("0-all"));
    assertThat(templateSettings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 0), is(6));

    execute("insert into quotes (id, quote, date) values (?, ?, ?)",
            new Object[] { 1, "Don't panic", 1395874800000L });
    assertThat(response.rowCount(), is(1L));
    refresh();

    assertTrue(clusterService().state().metaData().aliases().containsKey("quotes"));

    execute("select number_of_replicas, number_of_shards from information_schema.tables where table_name = 'quotes'");
    assertEquals("0-all", response.rows()[0][0]);
    assertEquals(6, response.rows()[0][1]);

    execute("select number_of_shards from information_schema.table_partitions where table_name='quotes'");
    assertThat(response.rowCount(), is(1L));
    assertThat((Integer) response.rows()[0][0], is(6));

    execute("alter table quotes set (number_of_shards=2)");
    ensureYellow();

    execute("insert into quotes (id, quote, date) values (?, ?, ?)",
            new Object[] { 2, "Now panic", 1395961200000L });
    assertThat(response.rowCount(), is(1L));
    ensureYellow();
    refresh();

    execute("select number_of_replicas, number_of_shards from information_schema.tables where table_name = 'quotes'");
    assertEquals("0-all", response.rows()[0][0]);
    assertEquals(2, response.rows()[0][1]);

    execute("select number_of_shards from information_schema.table_partitions where table_name='quotes' order by number_of_shards ASC");
    assertThat(response.rowCount(), is(2L));
    assertThat((Integer) response.rows()[0][0], is(2));
    assertThat((Integer) response.rows()[1][0], is(6));

    templatesResponse = client().admin().indices().prepareGetTemplates(templateName).execute().actionGet();
    templateSettings = templatesResponse.getIndexTemplates().get(0).getSettings();
    assertThat(templateSettings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0), is(1));
    assertThat(templateSettings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 0), is(2));
    assertThat(templateSettings.get(IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS), is("0-all"));

    List<String> partitions = ImmutableList.of(
            new PartitionName("quotes", Arrays.asList(new BytesRef("1395874800000"))).asIndexName(),
            new PartitionName("quotes", Arrays.asList(new BytesRef("1395961200000"))).asIndexName());
    Thread.sleep(1000);
    GetSettingsResponse settingsResponse = client().admin().indices()
            .prepareGetSettings(partitions.get(0), partitions.get(1)).execute().get();

    String index = partitions.get(0);
    assertThat(settingsResponse.getSetting(index, IndexMetaData.SETTING_NUMBER_OF_SHARDS), is("6"));
    assertThat(settingsResponse.getSetting(index, IndexMetaData.SETTING_NUMBER_OF_REPLICAS), is("1"));
    assertThat(settingsResponse.getSetting(index, IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS), is("0-all"));

    index = partitions.get(1);
    assertThat(settingsResponse.getSetting(index, IndexMetaData.SETTING_NUMBER_OF_SHARDS), is("2"));
    assertThat(settingsResponse.getSetting(index, IndexMetaData.SETTING_NUMBER_OF_REPLICAS), is("1"));
    assertThat(settingsResponse.getSetting(index, IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS), is("0-all"));

}

From source file:io.crate.integrationtests.PartitionedTableIntegrationTest.java

License:Apache License

@Test
public void testSelectWhileShardsAreRelocating() throws Throwable {
    execute("create table t (name string, p string) " + "clustered into 2 shards "
            + "partitioned by (p) with (number_of_replicas = 0)");
    ensureYellow();/* ww  w. j  av  a2 s  . co m*/

    execute("insert into t (name, p) values (?, ?)",
            new Object[][] { new Object[] { "Marvin", "a" }, new Object[] { "Trillian", "a" }, });
    execute("refresh table t");
    execute("set global stats.enabled=true");

    final AtomicReference<Throwable> lastThrowable = new AtomicReference<>();
    final CountDownLatch selects = new CountDownLatch(100);

    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            while (selects.getCount() > 0) {
                try {
                    execute("select * from t");
                } catch (Throwable t) {
                    // The failed job should have three started operations
                    SQLResponse res = execute(
                            "select id from sys.jobs_log where error is not null order by started desc limit 1");
                    if (res.rowCount() > 0) {
                        String id = (String) res.rows()[0][0];
                        res = execute(
                                "select count(*) from sys.operations_log where name=? or name = ?and job_id = ?",
                                new Object[] { "collect", "fetchContext", id });
                        if ((long) res.rows()[0][0] < 3) {
                            // set the error if there where less than three attempts
                            lastThrowable.set(t);
                        }
                    }
                } finally {
                    selects.countDown();
                }
            }
        }
    });
    t.start();

    PartitionName partitionName = new PartitionName("t", Collections.singletonList(new BytesRef("a")));
    final String indexName = partitionName.asIndexName();

    ClusterService clusterService = internalCluster().getInstance(ClusterService.class);
    DiscoveryNodes nodes = clusterService.state().nodes();
    List<String> nodeIds = new ArrayList<>(2);
    for (DiscoveryNode node : nodes) {
        if (node.dataNode()) {
            nodeIds.add(node.id());
        }
    }
    final Map<String, String> nodeSwap = new HashMap<>(2);
    nodeSwap.put(nodeIds.get(0), nodeIds.get(1));
    nodeSwap.put(nodeIds.get(1), nodeIds.get(0));

    final CountDownLatch relocations = new CountDownLatch(20);
    Thread relocatingThread = new Thread(new Runnable() {
        @Override
        public void run() {
            while (relocations.getCount() > 0) {
                ClusterStateResponse clusterStateResponse = admin().cluster().prepareState()
                        .setIndices(indexName).execute().actionGet();
                List<ShardRouting> shardRoutings = clusterStateResponse.getState().routingTable()
                        .allShards(indexName);

                ClusterRerouteRequestBuilder clusterRerouteRequestBuilder = admin().cluster().prepareReroute();
                int numMoves = 0;
                for (ShardRouting shardRouting : shardRoutings) {
                    if (shardRouting.currentNodeId() == null) {
                        continue;
                    }
                    if (shardRouting.state() != ShardRoutingState.STARTED) {
                        continue;
                    }
                    String toNode = nodeSwap.get(shardRouting.currentNodeId());
                    clusterRerouteRequestBuilder.add(new MoveAllocationCommand(shardRouting.shardId(),
                            shardRouting.currentNodeId(), toNode));
                    numMoves++;
                }

                if (numMoves > 0) {
                    clusterRerouteRequestBuilder.execute().actionGet();
                    client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID)
                            .setWaitForRelocatingShards(0).setTimeout(ACCEPTABLE_RELOCATION_TIME).execute()
                            .actionGet();
                    relocations.countDown();
                }
            }
        }
    });
    relocatingThread.start();
    relocations.await();
    selects.await();

    Throwable throwable = lastThrowable.get();
    if (throwable != null) {
        throw throwable;
    }
}

From source file:io.crate.integrationtests.TransportSQLActionTest.java

License:Apache License

@Test
public void testCopyFromIntoPartitionedTable() throws Exception {
    execute("create table quotes (id integer primary key, "
            + "quote string index using fulltext) partitioned by (id)");
    ensureGreen();//from   ww  w .j a  v a  2 s .c om
    refresh();

    String filePath = Joiner.on(File.separator).join(copyFilePath, "test_copy_from.json");
    execute("copy quotes from ?", new Object[] { filePath });
    // 2 nodes on same machine resulting in double affected rows
    assertEquals(6L, response.rowCount());
    assertThat(response.duration(), greaterThanOrEqualTo(0L));
    refresh();
    ensureGreen();

    for (String id : ImmutableList.of("1", "2", "3")) {
        String partitionName = new PartitionName("quotes", ImmutableList.of(new BytesRef(id))).stringValue();
        assertNotNull(client().admin().cluster().prepareState().execute().actionGet().getState().metaData()
                .indices().get(partitionName));
        assertNotNull(client().admin().cluster().prepareState().execute().actionGet().getState().metaData()
                .indices().get(partitionName).aliases().get("quotes"));
    }

    execute("select * from quotes");
    assertEquals(3L, response.rowCount());
    assertThat(response.rows()[0].length, is(2));
}

From source file:io.crate.integrationtests.TransportSQLActionTest.java

License:Apache License

@Test
public void testInsertPartitionedTable() throws Exception {
    execute("create table parted (id integer, name string, date timestamp)" + "partitioned by (date)");
    ensureGreen();/*from  w w  w.ja  v  a  2 s .  c  o  m*/
    String templateName = PartitionName.templateName("parted");
    GetIndexTemplatesResponse templatesResponse = client().admin().indices().prepareGetTemplates(templateName)
            .execute().actionGet();
    assertThat(templatesResponse.getIndexTemplates().get(0).template(), is(templateName + "*"));
    assertThat(templatesResponse.getIndexTemplates().get(0).name(), is(templateName));
    assertTrue(templatesResponse.getIndexTemplates().get(0).aliases().containsKey("parted"));

    execute("insert into parted (id, name, date) values (?, ?, ?)",
            new Object[] { 1, "Ford", 13959981214861L });
    assertThat(response.rowCount(), is(1L));
    ensureGreen();
    refresh();

    assertTrue(clusterService().state().metaData().aliases().containsKey("parted"));

    String partitionName = new PartitionName("parted",
            Arrays.asList(new BytesRef(String.valueOf(13959981214861L)))).stringValue();
    MetaData metaData = client().admin().cluster().prepareState().execute().actionGet().getState().metaData();
    assertNotNull(metaData.indices().get(partitionName).aliases().get("parted"));
    assertThat(client().prepareCount(partitionName).setTypes(Constants.DEFAULT_MAPPING_TYPE)
            .setQuery(new MatchAllQueryBuilder()).execute().actionGet().getCount(), is(1L));

    execute("select id, name, date from parted");
    assertThat(response.rowCount(), is(1L));
    assertThat((Integer) response.rows()[0][0], is(1));
    assertThat((String) response.rows()[0][1], is("Ford"));
    assertThat((Long) response.rows()[0][2], is(13959981214861L));
}

From source file:io.crate.integrationtests.TransportSQLActionTest.java

License:Apache License

@Test
public void testBulkInsertPartitionedTable() throws Exception {
    execute("create table parted (id integer, name string, date timestamp)" + "partitioned by (date)");
    ensureGreen();/*  w  w w.  j av  a 2 s  .  co m*/
    execute("insert into parted (id, name, date) values (?, ?, ?), (?, ?, ?), (?, ?, ?)",
            new Object[] { 1, "Ford", 13959981214861L, 2, "Trillian", 0L, 3, "Zaphod", null });
    assertThat(response.rowCount(), is(3L));
    ensureGreen();
    refresh();

    String partitionName = new PartitionName("parted",
            Arrays.asList(new BytesRef(String.valueOf(13959981214861L)))).stringValue();
    assertTrue(cluster().clusterService().state().metaData().hasIndex(partitionName));
    assertNotNull(client().admin().cluster().prepareState().execute().actionGet().getState().metaData()
            .indices().get(partitionName).aliases().get("parted"));
    assertThat(client().prepareCount(partitionName).setTypes(Constants.DEFAULT_MAPPING_TYPE)
            .setQuery(new MatchAllQueryBuilder()).execute().actionGet().getCount(), is(1L));

    partitionName = new PartitionName("parted", Arrays.asList(new BytesRef(String.valueOf(0L)))).stringValue();
    assertTrue(cluster().clusterService().state().metaData().hasIndex(partitionName));
    assertNotNull(client().admin().cluster().prepareState().execute().actionGet().getState().metaData()
            .indices().get(partitionName).aliases().get("parted"));
    assertThat(client().prepareCount(partitionName).setTypes(Constants.DEFAULT_MAPPING_TYPE)
            .setQuery(new MatchAllQueryBuilder()).execute().actionGet().getCount(), is(1L));

    List<BytesRef> nullList = new ArrayList<>();
    nullList.add(null);
    partitionName = new PartitionName("parted", nullList).stringValue();
    assertTrue(cluster().clusterService().state().metaData().hasIndex(partitionName));
    assertNotNull(client().admin().cluster().prepareState().execute().actionGet().getState().metaData()
            .indices().get(partitionName).aliases().get("parted"));
    assertThat(client().prepareCount(partitionName).setTypes(Constants.DEFAULT_MAPPING_TYPE)
            .setQuery(new MatchAllQueryBuilder()).execute().actionGet().getCount(), is(1L));
}

From source file:io.crate.integrationtests.TransportSQLActionTest.java

License:Apache License

@Test
public void testInsertPartitionedTableOnlyPartitionedColumns() throws Exception {
    execute("create table parted (name string, date timestamp)" + "partitioned by (name, date)");
    ensureGreen();//from w ww.  j  a  v  a 2 s.  com

    execute("insert into parted (name, date) values (?, ?)", new Object[] { "Ford", 13959981214861L });
    assertThat(response.rowCount(), is(1L));
    ensureGreen();
    refresh();
    String partitionName = new PartitionName("parted",
            Arrays.asList(new BytesRef("Ford"), new BytesRef(String.valueOf(13959981214861L)))).stringValue();
    assertNotNull(client().admin().cluster().prepareState().execute().actionGet().getState().metaData()
            .indices().get(partitionName).aliases().get("parted"));
    assertThat(client().prepareCount(partitionName).setTypes(Constants.DEFAULT_MAPPING_TYPE)
            .setQuery(new MatchAllQueryBuilder()).execute().actionGet().getCount(), is(1L));
    execute("select * from parted");
    assertThat(response.rowCount(), is(1L));
    assertThat(response.cols(), arrayContaining("date", "name"));
    assertThat((Long) response.rows()[0][0], is(13959981214861L));
    assertThat((String) response.rows()[0][1], is("Ford"));
}

From source file:io.crate.integrationtests.TransportSQLActionTest.java

License:Apache License

@Test
public void testInsertPartitionedTableSomePartitionedColumns() throws Exception {
    // insert only some partitioned column values
    execute("create table parted (id integer, name string, date timestamp)" + "partitioned by (name, date)");
    ensureGreen();// w w w  .  ja v a2s.  com

    execute("insert into parted (id, name) values (?, ?)", new Object[] { 1, "Trillian" });
    assertThat(response.rowCount(), is(1L));
    ensureGreen();
    refresh();
    String partitionName = new PartitionName("parted", Arrays.asList(new BytesRef("Trillian"), null))
            .stringValue();
    assertNotNull(client().admin().cluster().prepareState().execute().actionGet().getState().metaData()
            .indices().get(partitionName).aliases().get("parted"));

    execute("select id, name, date from parted");
    assertThat(response.rowCount(), is(1L));
    assertThat((Integer) response.rows()[0][0], is(1));
    assertThat((String) response.rows()[0][1], is("Trillian"));
    assertNull(response.rows()[0][2]);
}