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.expression.scalar.regex.RegexMatcherTest.java

License:Apache License

@Test
public void testMatch() throws Exception {
    String pattern = "hello";
    BytesRef text = new BytesRef("foobarbequebaz");
    RegexMatcher regexMatcher = new RegexMatcher(pattern);
    assertEquals(false, regexMatcher.match(text));
    assertArrayEquals(null, regexMatcher.groups());

    pattern = "ba";
    regexMatcher = new RegexMatcher(pattern);
    assertEquals(true, regexMatcher.match(text));
    assertThat(regexMatcher.groups(), arrayContaining(new BytesRef("ba")));

    pattern = "(ba)";
    regexMatcher = new RegexMatcher(pattern);
    assertEquals(true, regexMatcher.match(text));
    assertThat(regexMatcher.groups(), arrayContaining(new BytesRef("ba")));

    pattern = ".*(ba).*";
    regexMatcher = new RegexMatcher(pattern);
    assertEquals(true, regexMatcher.match(text));
    assertThat(regexMatcher.groups(), arrayContaining(new BytesRef("ba")));

    pattern = "((\\w+?)(ba))";
    regexMatcher = new RegexMatcher(pattern);
    assertEquals(true, regexMatcher.match(text));
    assertThat(regexMatcher.groups(),// www .j  a v a 2  s  . c o m
            arrayContaining(new BytesRef("fooba"), new BytesRef("foo"), new BytesRef("ba")));
}

From source file:io.crate.expression.scalar.regex.RegexMatcherTest.java

License:Apache License

@Test
public void testReplaceNoMatch() throws Exception {
    String pattern = "crate";
    BytesRef text = new BytesRef("foobarbequebaz");
    String replacement = "crate";
    RegexMatcher regexMatcher = new RegexMatcher(pattern);
    assertEquals(text, regexMatcher.replace(text, replacement));
}

From source file:io.crate.expression.scalar.regex.RegexMatcherTest.java

License:Apache License

@Test
public void testReplace() throws Exception {
    String pattern = "ba";
    BytesRef text = new BytesRef("foobarbequebaz");
    String replacement = "Crate";
    RegexMatcher regexMatcher = new RegexMatcher(pattern);
    assertEquals(new BytesRef("fooCraterbequebaz"), regexMatcher.replace(text, replacement));

    pattern = "(ba).*(ba)";
    replacement = "First$1Second$2";
    regexMatcher = new RegexMatcher(pattern);
    assertEquals(new BytesRef("fooFirstbaSecondbaz"), regexMatcher.replace(text, replacement));
}

From source file:io.crate.expression.scalar.regex.RegexMatcherTest.java

License:Apache License

@Test
public void testReplaceGlobal() throws Exception {
    String pattern = "ba";
    BytesRef text = new BytesRef("foobarbequebaz");
    String replacement = "Crate";
    RegexMatcher regexMatcher = new RegexMatcher(pattern, new BytesRef("g"));
    assertEquals(new BytesRef("fooCraterbequeCratez"), regexMatcher.replace(text, replacement));
}

From source file:io.crate.expression.scalar.regex.RegexMatcherTest.java

License:Apache License

@Test
public void testMatchesNullGroup() throws Exception {
    String pattern = "\\w+( --?\\w+)*( \\w+)*";
    BytesRef text = new BytesRef("gcc -Wall --std=c99 -o source source.c");
    RegexMatcher regexMatcher = new RegexMatcher(pattern);
    assertEquals(true, regexMatcher.match(text));
    assertThat(regexMatcher.groups(), arrayContaining(new BytesRef(" --std"), null));
}

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)));
}

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

License:Apache License

@Test
public void testSubstring() throws Exception {
    assertThat(SubstrFunction.substring(new BytesRef("cratedata"), 2, 5), is(new BytesRef("ate")));
    assertThat(SubstrFunction.substring(TestingHelpers.bytesRef("cratedata", random()), 2, 5),
            is(new BytesRef("ate")));
}

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

License:Apache License

public static BytesRef format(BytesRef formatString, DateTime timestamp) {
    StringBuilder buffer = new StringBuilder(formatString.length);
    String format = formatString.utf8ToString();
    boolean percentEscape = false;
    int length = format.length();
    for (int i = 0; i < length; i++) {
        char current = format.charAt(i);

        if (current == '%') {
            if (!percentEscape) {
                percentEscape = true;/* w w w  . j  a  va2  s .  c  o  m*/
            } else {
                buffer.append('%');
                percentEscape = false;
            }
        } else {
            if (percentEscape) {
                FormatTimestampPartFunction partFormatter = PART_FORMATTERS.get(current);
                if (partFormatter == null) {
                    buffer.append(current);
                } else {
                    buffer.append(partFormatter.format(timestamp));
                }
            } else {
                buffer.append(current);
            }
            percentEscape = false;
        }
    }
    return new BytesRef(buffer.toString());
}

From source file:io.crate.expression.symbol.Literal.java

License:Apache License

public static Literal<BytesRef> of(String value) {
    if (value == null) {
        return new Literal<>(DataTypes.STRING, null);
    }//from   w w w .j  av  a2  s.c o  m
    return new Literal<>(DataTypes.STRING, new BytesRef(value));
}

From source file:io.crate.http.netty.NettyHttpChannel.java

License:Apache License

@Override
public void sendResponse(RestResponse response) {
    // Decide whether to close the connection or not.
    boolean http10 = nettyRequest.getProtocolVersion().equals(HttpVersion.HTTP_1_0);
    boolean close = HttpHeaders.Values.CLOSE
            .equalsIgnoreCase(nettyRequest.headers().get(HttpHeaders.Names.CONNECTION))
            || (http10 && !HttpHeaders.Values.KEEP_ALIVE
                    .equalsIgnoreCase(nettyRequest.headers().get(HttpHeaders.Names.CONNECTION)));

    // Build the response object.
    HttpResponseStatus status = getStatus(response.status());
    org.jboss.netty.handler.codec.http.HttpResponse resp;
    if (http10) {
        resp = new DefaultHttpResponse(HttpVersion.HTTP_1_0, status);
        if (!close) {
            resp.headers().add(HttpHeaders.Names.CONNECTION, "Keep-Alive");
        }/*from   w ww . j av a 2 s .co m*/
    } else {
        resp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);
    }
    if (RestUtils.isBrowser(nettyRequest.headers().get(USER_AGENT))) {
        if (transport.settings().getAsBoolean(SETTING_CORS_ENABLED, false)) {
            String originHeader = request.header(ORIGIN);
            if (!Strings.isNullOrEmpty(originHeader)) {
                if (corsPattern == null) {
                    resp.headers().add(ACCESS_CONTROL_ALLOW_ORIGIN,
                            transport.settings().get(SETTING_CORS_ALLOW_ORIGIN, "*"));
                } else {
                    resp.headers().add(ACCESS_CONTROL_ALLOW_ORIGIN,
                            corsPattern.matcher(originHeader).matches() ? originHeader : "null");
                }
            }
            if (nettyRequest.getMethod() == HttpMethod.OPTIONS) {
                // Allow Ajax requests based on the CORS "preflight" request
                resp.headers().add(ACCESS_CONTROL_MAX_AGE,
                        transport.settings().getAsInt(SETTING_CORS_MAX_AGE, 1728000));
                resp.headers().add(ACCESS_CONTROL_ALLOW_METHODS, transport.settings()
                        .get(SETTING_CORS_ALLOW_METHODS, "OPTIONS, HEAD, GET, POST, PUT, DELETE"));
                resp.headers().add(ACCESS_CONTROL_ALLOW_HEADERS, transport.settings()
                        .get(SETTING_CORS_ALLOW_HEADERS, "X-Requested-With, Content-Type, Content-Length"));
            }

            if (transport.settings().getAsBoolean(SETTING_CORS_ALLOW_CREDENTIALS, false)) {
                resp.headers().add(ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
            }
        }
    }

    String opaque = nettyRequest.headers().get("X-Opaque-Id");
    if (opaque != null) {
        resp.headers().add("X-Opaque-Id", opaque);
    }

    // Add all custom headers
    Map<String, List<String>> customHeaders = response.getHeaders();
    if (customHeaders != null) {
        for (Map.Entry<String, List<String>> headerEntry : customHeaders.entrySet()) {
            for (String headerValue : headerEntry.getValue()) {
                resp.headers().add(headerEntry.getKey(), headerValue);
            }
        }
    }

    BytesReference content = response.content();
    ChannelBuffer buffer;
    boolean addedReleaseListener = false;
    try {
        buffer = content.toChannelBuffer();
        // handle JSONP
        String callback = request.param("callback");
        if (callback != null) {
            final BytesRef callbackBytes = new BytesRef(callback);
            callbackBytes.bytes[callbackBytes.length] = '(';
            callbackBytes.length++;
            buffer = ChannelBuffers.wrappedBuffer(
                    NettyUtils.DEFAULT_GATHERING, ChannelBuffers.wrappedBuffer(callbackBytes.bytes,
                            callbackBytes.offset, callbackBytes.length),
                    buffer, ChannelBuffers.wrappedBuffer(END_JSONP));
            // Add content-type header of "application/javascript"
            resp.headers().add(HttpHeaders.Names.CONTENT_TYPE, "application/javascript");
        }
        resp.setContent(buffer);

        // If our response doesn't specify a content-type header, set one
        if (!resp.headers().contains(HttpHeaders.Names.CONTENT_TYPE)) {
            resp.headers().add(HttpHeaders.Names.CONTENT_TYPE, response.contentType());
        }

        // If our response has no content-length, calculate and set one
        if (!resp.headers().contains(HttpHeaders.Names.CONTENT_LENGTH)) {
            resp.headers().add(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(buffer.readableBytes()));
        }

        if (transport.resetCookies) {
            String cookieString = nettyRequest.headers().get(HttpHeaders.Names.COOKIE);
            if (cookieString != null) {
                CookieDecoder cookieDecoder = new CookieDecoder();
                Set<Cookie> cookies = cookieDecoder.decode(cookieString);
                if (!cookies.isEmpty()) {
                    // Reset the cookies if necessary.
                    CookieEncoder cookieEncoder = new CookieEncoder(true);
                    for (Cookie cookie : cookies) {
                        cookieEncoder.addCookie(cookie);
                    }
                    resp.headers().add(HttpHeaders.Names.SET_COOKIE, cookieEncoder.encode());
                }
            }
        }

        ChannelFuture future;

        if (orderedUpstreamMessageEvent != null) {
            OrderedDownstreamChannelEvent downstreamChannelEvent = new OrderedDownstreamChannelEvent(
                    orderedUpstreamMessageEvent, 0, true, resp);
            future = downstreamChannelEvent.getFuture();
            channel.getPipeline().sendDownstream(downstreamChannelEvent);
        } else {
            future = channel.write(resp);
        }

        if (content instanceof Releasable) {
            future.addListener(new ReleaseChannelFutureListener((Releasable) content));
            addedReleaseListener = true;
        }

        if (close) {
            future.addListener(ChannelFutureListener.CLOSE);
        }

    } finally {
        if (!addedReleaseListener && content instanceof Releasable) {
            ((Releasable) content).close();
        }
    }
}