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

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

Introduction

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

Prototype

public String utf8ToString() 

Source Link

Document

Interprets stored bytes as UTF8 bytes, returning the resulting string

Usage

From source file:io.crate.core.NumberOfReplicasTest.java

License:Apache License

@Test
public void testNumber() throws Exception {
    BytesRef numberOfResplicas = NumberOfReplicas
            .fromSettings(ImmutableSettings.builder().put(NumberOfReplicas.NUMBER_OF_REPLICAS, 4).build());
    assertThat(numberOfResplicas.utf8ToString(), is("4"));
}

From source file:io.crate.core.NumberOfReplicasTest.java

License:Apache License

@Test
public void testAutoExpandSettingsTakePrecedence() throws Exception {
    BytesRef numberOfResplicas = NumberOfReplicas
            .fromSettings(ImmutableSettings.builder().put(NumberOfReplicas.AUTO_EXPAND_REPLICAS, "0-all")
                    .put(NumberOfReplicas.NUMBER_OF_REPLICAS, 1).build());
    assertThat(numberOfResplicas.utf8ToString(), is("0-all"));
}

From source file:io.crate.executor.BytesRefUtils.java

License:Apache License

public static void ensureStringTypesAreStrings(DataType[] dataTypes, Object[][] rows) {
    if (rows.length == 0) {
        return;//from w  w  w .j  a  v a 2  s.co  m
    }

    // NOTE: currently BytesRef inside Maps aren't converted here because
    // if the map is coming from a ESSearchTask/EsGetTask they already contain strings
    // and we have no case in which another Task returns a Map with ByteRefs/Strings inside.
    final IntArrayList stringColumns = new IntArrayList();
    final IntArrayList stringCollectionColumns = new IntArrayList();
    int idx = 0;
    for (DataType dataType : dataTypes) {
        if (BYTES_REF_TYPES.contains(dataType)) {
            stringColumns.add(idx);
        } else if ((DataTypes.isCollectionType(dataType)
                && (BYTES_REF_TYPES.contains(((CollectionType) dataType).innerType())))) {
            stringCollectionColumns.add(idx);
        }
        idx++;
    }

    for (int r = 0; r < rows.length; r++) {
        for (IntCursor stringColumn : stringColumns) {
            Object value = rows[r][stringColumn.value];
            if (value != null && value instanceof BytesRef) {
                rows[r][stringColumn.value] = ((BytesRef) value).utf8ToString();
            }
        }

        for (IntCursor stringCollectionColumn : stringCollectionColumns) {
            Object value = rows[r][stringCollectionColumn.value];
            if (value != null) {
                Iterator<BytesRef> iter = null;
                int size;
                if (value instanceof Set) {
                    @SuppressWarnings("unchecked")
                    Set<BytesRef> bytesRefSet = ((Set<BytesRef>) value);
                    iter = bytesRefSet.iterator();
                    size = bytesRefSet.size();
                } else if (value instanceof BytesRef[]) {
                    BytesRef[] bytesRefArray = (BytesRef[]) value;
                    iter = Arrays.asList(bytesRefArray).iterator();
                    size = bytesRefArray.length;
                } else if (value instanceof Object[]) {
                    try {
                        Object[] objectArray = (Object[]) value;
                        BytesRef[] bytesRefArray = Arrays.copyOf(objectArray, objectArray.length,
                                BytesRef[].class);
                        iter = Arrays.asList(bytesRefArray).iterator();
                        size = bytesRefArray.length;
                    } catch (ArrayStoreException e) {
                        continue;
                    }
                } else {
                    continue;
                }

                String[] valuesString = new String[size];
                for (int i = 0; i < size; i++) {
                    BytesRef bytesRef = iter.next();
                    valuesString[i] = bytesRef == null ? null : bytesRef.utf8ToString();
                }
                rows[r][stringCollectionColumn.value] = valuesString;
            }
        }
    }
}

From source file:io.crate.executor.RowsResponseBuilder.java

License:Apache License

private void convertBytesRef(DataType[] dataTypes, Object[][] rows) {
    if (rows.length == 0) {
        return;//from  w ww.ja  va 2 s.c  o  m
    }

    // NOTE: currently BytesRef inside Maps aren't converted here because
    // if the map is coming from a ESSearchTask/EsGetTask they already contain strings
    // and we have no case in which another Task returns a Map with ByteRefs/Strings inside.
    final IntArrayList stringColumns = new IntArrayList();
    final IntArrayList stringCollectionColumns = new IntArrayList();
    int idx = 0;
    for (DataType dataType : dataTypes) {
        if (dataType == DataType.STRING) {
            stringColumns.add(idx);
        } else if (dataType == DataType.STRING_SET || dataType == DataType.STRING_ARRAY) {
            stringCollectionColumns.add(idx);
        }
        idx++;
    }

    for (int r = 0; r < rows.length; r++) {
        for (IntCursor stringColumn : stringColumns) {
            Object value = rows[r][stringColumn.value];
            if (value != null && value instanceof BytesRef) {
                rows[r][stringColumn.value] = ((BytesRef) value).utf8ToString();
            }
        }

        for (IntCursor stringCollectionColumn : stringCollectionColumns) {
            Object value = rows[r][stringCollectionColumn.value];
            if (value != null) {
                if (value instanceof Set) {
                    rows[r][stringCollectionColumn.value] = Collections2.transform((Set<BytesRef>) value,
                            new Function<BytesRef, String>() {
                                @Nullable
                                @Override
                                public String apply(@Nullable BytesRef input) {
                                    return input == null ? null : input.utf8ToString();
                                }
                            });
                } else if (value instanceof BytesRef[]) {
                    BytesRef[] values = (BytesRef[]) value;
                    String[] valuesString = new String[values.length];
                    for (int i = 0; i < values.length; i++) {
                        valuesString[i] = values[i] == null ? null : values[i].utf8ToString();
                    }
                    rows[r][stringCollectionColumn.value] = valuesString;
                }
            }
        }
    }
}

From source file:io.crate.expression.operator.any.AbstractAnyLikeOperator.java

License:Apache License

@Override
protected Boolean doEvaluate(Object left, Iterable<?> rightIterable) {
    BytesRef rightBytesRef = (BytesRef) left;
    String pattern = rightBytesRef.utf8ToString();

    boolean hasNull = false;
    for (Object elem : rightIterable) {
        if (elem == null) {
            hasNull = true;//from   ww w.j a  v a  2 s .co  m
            continue;
        }
        assert elem instanceof BytesRef || elem instanceof String : "elem must be BytesRef or String";

        String elemValue;
        if (elem instanceof BytesRef) {
            elemValue = ((BytesRef) elem).utf8ToString();
        } else {
            elemValue = (String) elem;
        }
        if (matches(elemValue, pattern)) {
            return true;
        }
    }
    return hasNull ? null : false;
}

From source file:io.crate.expression.operator.LikeOperator.java

License:Apache License

@Override
public Boolean evaluate(Input<BytesRef>... args) {
    assert args != null : "args must not be null";
    assert args.length == 2 : "number of args must be 2";

    BytesRef expression = args[0].value();
    BytesRef pattern = args[1].value();//from  w w w . j  a  va  2s. c  om
    if (expression == null || pattern == null) {
        return null;
    }

    return matches(expression.utf8ToString(), pattern.utf8ToString());
}

From source file:io.crate.expression.operator.RegexpMatchCaseInsensitiveOperator.java

License:Apache License

@Override
public Boolean evaluate(Input<BytesRef>... args) {
    assert args.length == 2 : "invalid number of arguments";
    BytesRef source = args[0].value();//from  w  w  w . j a v  a2s  .  c o  m
    if (source == null) {
        return null;
    }
    BytesRef pattern = args[1].value();
    if (pattern == null) {
        return null;
    }

    Pattern p = Pattern.compile(pattern.utf8ToString(), Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
    return p.matcher(source.utf8ToString()).matches();
}

From source file:io.crate.expression.operator.RegexpMatchOperator.java

License:Apache License

@Override
public Boolean evaluate(Input<BytesRef>... args) {
    assert args.length == 2 : "invalid number of arguments";
    BytesRef source = args[0].value();//from   ww  w.  ja va  2s  .  c om
    if (source == null) {
        return null;
    }
    BytesRef pattern = args[1].value();
    if (pattern == null) {
        return null;
    }
    String sPattern = pattern.utf8ToString();
    if (isPcrePattern(sPattern)) {
        return source.utf8ToString().matches(sPattern);
    } else {
        RegExp regexp = new RegExp(sPattern);
        ByteRunAutomaton regexpRunAutomaton = new ByteRunAutomaton(regexp.toAutomaton());
        return regexpRunAutomaton.run(source.bytes, source.offset, source.length);
    }
}

From source file:io.crate.expression.scalar.DateTruncFunction.java

License:Apache License

private DateTimeUnit intervalAsUnit(BytesRef interval) {
    if (interval == null) {
        throw new IllegalArgumentException(
                String.format(Locale.ENGLISH, "invalid interval NULL for scalar '%s'", NAME));
    }/*from ww w.  j  a v a2  s. c o m*/
    DateTimeUnit intervalAsUnit = DATE_FIELD_PARSERS.get(interval);
    if (intervalAsUnit == null) {
        throw new IllegalArgumentException(String.format(Locale.ENGLISH,
                "invalid interval '%s' for scalar '%s'", interval.utf8ToString(), NAME));
    }
    return intervalAsUnit;
}

From source file:io.crate.expression.scalar.SubstrFunctionBytesRefTest.java

License:Apache License

@Test
public void testNoCopy() throws Exception {
    BytesRef ref = new BytesRef("i do not want to be copied!");
    BytesRef sub1 = SubstrFunction.substring(ref, 0, 10);
    BytesRef sub2 = SubstrFunction.substring(ref, 5, 14);
    assertThat(sub1.utf8ToString(), is("i do not w"));
    assertThat(sub2.utf8ToString(), is("not want "));
    assertThat(ref.bytes, allOf(is(sub2.bytes), is(sub1.bytes)));
}