Java Code Examples for org.elasticsearch.common.bytes.BytesReference#length()

The following examples show how to use org.elasticsearch.common.bytes.BytesReference#length() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SQLXContentSourceParser.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public void parseSource(BytesReference source) throws SQLParseException {
    XContentParser parser = null;
    try {
        if (source != null && source.length() != 0) {
            parser = XContentFactory.xContent(source).createParser(source);
            parse(parser);
        }
        validate();
    } catch (Exception e) {
        String sSource = "_na_";
        try {
            sSource = XContentHelper.convertToJson(source, false);
        } catch (Throwable e1) {
            // ignore
        }
        throw new SQLParseException("Failed to parse source [" + sSource + "]", e);
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
}
 
Example 2
Source File: LocalTranslog.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public Location writeToLocal(BytesReference data) throws IOException {
    final long position;
    final long generation;
    try (ReleasableLock lock = writeLock.acquire()) {
        ensureOpen();
        if (writtenOffset > TRANSLOG_ROLLING_SIZE_BYTES) {
            IOUtils.close(writeChannel);
            tmpTranslogGeneration.incrementAndGet();
            writeChannel = FileChannel.open(this.translogPath.resolve(getFileNameFromId(tmpTranslogGeneration.get())), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
            writtenOffset = 0;
        }
        generation = tmpTranslogGeneration.get();
        position = writtenOffset;
        try {
            data.writeTo(writeChannel);
        } catch (Throwable e) {
            throw e;
        }
        writtenOffset = writtenOffset + data.length();
    }
    return new Translog.Location(generation, position, data.length());
}
 
Example 3
Source File: BaseTransportCoordinateSearchAction.java    From siren-join with GNU Affero General Public License v3.0 6 votes vote down vote up
protected Tuple<XContentType, Map<String, Object>> parseSource(BytesReference source) {
  // nothing to parse...
  if (source == null || source.length() == 0) {
    return null;
  }

  try {
    Tuple<XContentType, Map<String, Object>> parsedSource = XContentHelper.convertToMap(source, false);
    logger.debug("{}: Parsed source: {}", Thread.currentThread().getName(), parsedSource);
    return parsedSource;
  }
  catch (Throwable e) {
      String sSource = "_na_";
      try {
          sSource = XContentHelper.convertToJson(source, false);
      }
      catch (Throwable e1) { /* ignore  */ }
      throw new ElasticsearchParseException("Failed to parse source [" + sSource + "]", e);
  }
}
 
Example 4
Source File: WrapperQueryVisitor.java    From siren-join with GNU Affero General Public License v3.0 6 votes vote down vote up
protected Map<String, Object> parseQuery(BytesReference source) {
  // nothing to parse...
  if (source == null || source.length() == 0) {
    return null;
  }

  try {
    Tuple<XContentType, Map<String, Object>> parsedSource = XContentHelper.convertToMap(source, false);
    return parsedSource.v2();
  }
  catch (Throwable e) {
    String sSource = "_na_";
    try {
      sSource = XContentHelper.convertToJson(source, false);
    }
    catch (Throwable e1) { /* ignore  */ }
    throw new ElasticsearchParseException("Failed to parse source [" + sSource + "]", e);
  }
}
 
Example 5
Source File: TranslogWriter.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/**
 * add the given bytes to the translog and return the location they were written at
 */
public Translog.Location add(BytesReference data) throws IOException {
    final long position;
    try (ReleasableLock lock = writeLock.acquire()) {
        ensureOpen();
        position = writtenOffset;
        try {
            data.writeTo(channel);
        } catch (Throwable e) {
            closeWithTragicEvent(e);
            throw e;
        }
        writtenOffset = writtenOffset + data.length();
        operationCounter++;;
    }
    return new Translog.Location(generation, position, data.length());
}
 
Example 6
Source File: RestLoggerFilter.java    From es-restlog with Apache License 2.0 5 votes vote down vote up
private String encodeContent(BytesReference content) {
  if (content == null) return null;
  final int offset = content.arrayOffset();
  final int length = content.length();
  if (length - offset == 0) return null;
  return contentEncoder.encode(content.array(), offset, length);
}
 
Example 7
Source File: ImportParser.java    From elasticsearch-inout-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Main method of this class to parse given payload of _export action
 *
 * @param context
 * @param source
 * @throws SearchParseException
 */
public void parseSource(ImportContext context, BytesReference source) throws ImportParseException {
    XContentParser parser = null;
    try {
        if (source != null && source.length() != 0) {
            parser = XContentFactory.xContent(source).createParser(source);
            XContentParser.Token token;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    String fieldName = parser.currentName();
                    parser.nextToken();
                    ImportParseElement element = elementParsers.get(fieldName);
                    if (element == null) {
                        throw new ImportParseException(context, "No parser for element [" + fieldName + "]");
                    }
                    element.parse(parser, context);
                } else if (token == null) {
                    break;
                }
            }
        }
        validate(context);
    } catch (Exception e) {
        String sSource = "_na_";
        try {
            sSource = XContentHelper.convertToJson(source, false);
        } catch (Throwable e1) {
            // ignore
        }
        throw new ImportParseException(context, "Failed to parse source [" + sSource + "]", e);
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
}
 
Example 8
Source File: DeflateCompressor.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isCompressed(BytesReference bytes) {
    if (bytes.length() < HEADER.length) {
        return false;
    }
    for (int i = 0; i < HEADER.length; ++i) {
        if (bytes.get(i) != HEADER[i]) {
            return false;
        }
    }
    return true;
}
 
Example 9
Source File: TcpTransport.java    From crate with Apache License 2.0 5 votes vote down vote up
private void sendResponse(
        final Version nodeVersion,
        final Set<String> features,
        final TcpChannel channel,
        final TransportResponse response,
        final long requestId,
        final String action,
        TransportResponseOptions options,
        byte status) throws IOException {
    if (compress) {
        options = TransportResponseOptions.builder(options).withCompress(true).build();
    }
    status = TransportStatus.setResponse(status); // TODO share some code with sendRequest
    ReleasableBytesStreamOutput bStream = new ReleasableBytesStreamOutput(bigArrays);
    CompressibleBytesOutputStream stream = new CompressibleBytesOutputStream(bStream, options.compress());
    boolean addedReleaseListener = false;
    try {
        if (options.compress()) {
            status = TransportStatus.setCompress(status);
        }
        ThreadContext.bwcWriteHeaders(stream);
        stream.setVersion(nodeVersion);
        stream.setFeatures(features);
        BytesReference message = buildMessage(requestId, status, nodeVersion, response, stream);

        final TransportResponseOptions finalOptions = options;
        // this might be called in a different thread
        SendListener listener = new SendListener(channel, stream,
            () -> messageListener.onResponseSent(requestId, action, response, finalOptions), message.length());
        internalSendMessage(channel, message, listener);
        addedReleaseListener = true;
    } finally {
        if (!addedReleaseListener) {
            IOUtils.close(stream);
        }
    }
}
 
Example 10
Source File: TransportSuggestAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected ShardSuggestResponse shardOperation(ShardSuggestRequest request) {
    IndexService indexService = indicesService.indexServiceSafe(request.shardId().getIndex());
    IndexShard indexShard = indexService.shardSafe(request.shardId().id());
    ShardSuggestMetric suggestMetric = indexShard.getSuggestMetric();
    suggestMetric.preSuggest();
    long startTime = System.nanoTime();
    XContentParser parser = null;
    try (Engine.Searcher searcher = indexShard.acquireSearcher("suggest")) {
        BytesReference suggest = request.suggest();
        if (suggest != null && suggest.length() > 0) {
            parser = XContentFactory.xContent(suggest).createParser(suggest);
            if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
                throw new IllegalArgumentException("suggest content missing");
            }
            final SuggestionSearchContext context = suggestPhase.parseElement().parseInternal(parser, indexService.mapperService(),
                    indexService.queryParserService(), request.shardId().getIndex(), request.shardId().id(), request);
            final Suggest result = suggestPhase.execute(context, searcher.searcher());
            return new ShardSuggestResponse(request.shardId(), result);
        }
        return new ShardSuggestResponse(request.shardId(), new Suggest());
    } catch (Throwable ex) {
        throw new ElasticsearchException("failed to execute suggest", ex);
    } finally {
        if (parser != null) {
            parser.close();
        }
        suggestMetric.postSuggest(System.nanoTime() - startTime);
    }
}
 
Example 11
Source File: TransportDeployAction.java    From elasticsearch-gatherer with Apache License 2.0 5 votes vote down vote up
@Override
protected DeployNodeResponse nodeOperation(DeployNodeRequest request) throws ElasticSearchException {
    String name = request.getRequest().getName();
    if (name == null) {
        throw new ElasticSearchException("no name given");
    }
    String path = request.getRequest().getPath();
    if (path == null) {
        throw new ElasticSearchException("no path given");
    }
    BytesReference ref = request.getRequest().getBytes();
    if (ref == null || ref.length() == 0) {
        throw new ElasticSearchException("no bytes in request");
    }
    // place all deployments under gatherer to avoid overwriting of other plugins
    File dir = new File(environment.pluginsFile(), GathererPlugin.NAME + "/" + name);
    if (dir.exists()) {
        throw new ElasticSearchException("refusing cowardly to overwrite existing path: " + dir.getAbsolutePath());
    }
    try {
        dir.mkdirs();
        File f = new File(path); // just to get file name
        File target = new File(dir, f.getName());
        logger.info("deploying to {}", target.getAbsolutePath());
        FileOutputStream out = new FileOutputStream(target);
        InputStream in = new ByteArrayInputStream(ref.array());
        StreamUtil.copy(in, out);
        in.close();
        out.close();
        // deploy service knows how to unpack archive and add jars to class path
        deployService.add(name, target.getAbsolutePath());
        // TODO set success result in DeployNodeResponse
    } catch (IOException e) {
        throw new ElasticSearchException(e.getMessage());
    }
    DeployNodeResponse response = new DeployNodeResponse();
    return response;
}
 
Example 12
Source File: LZFCompressor.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isCompressed(BytesReference bytes) {
    return bytes.length() >= 3 &&
            bytes.get(0) == LZFChunk.BYTE_Z &&
            bytes.get(1) == LZFChunk.BYTE_V &&
            (bytes.get(2) == LZFChunk.BLOCK_TYPE_COMPRESSED || bytes.get(2) == LZFChunk.BLOCK_TYPE_NON_COMPRESSED);
}
 
Example 13
Source File: DeflateCompressor.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isCompressed(BytesReference bytes) {
    if (bytes.length() < HEADER.length) {
        return false;
    }
    for (int i = 0; i < HEADER.length; ++i) {
        if (bytes.get(i) != HEADER[i]) {
            return false;
        }
    }
    return true;
}
 
Example 14
Source File: BufferingTranslogWriter.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Translog.Location add(BytesReference data) throws IOException {
    try (ReleasableLock lock = writeLock.acquire()) {
        ensureOpen();
        final long offset = totalOffset;
        if (data.length() >= buffer.length) {
            flush();
            // we use the channel to write, since on windows, writing to the RAF might not be reflected
            // when reading through the channel
            try {
                data.writeTo(channel);
            } catch (Throwable ex) {
                closeWithTragicEvent(ex);
                throw ex;
            }
            writtenOffset += data.length();
            totalOffset += data.length();
        } else {
            if (data.length() > buffer.length - bufferCount) {
                flush();
            }
            data.writeTo(bufferOs);
            totalOffset += data.length();
        }
        operationCounter++;
        return new Translog.Location(generation, offset, data.length());
    }
}
 
Example 15
Source File: MultiSearchRequest.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public MultiSearchRequest add(BytesReference data, boolean isTemplateRequest, @Nullable String[] indices, @Nullable String[] types, @Nullable String searchType, @Nullable String routing, IndicesOptions indicesOptions, boolean allowExplicitIndex) throws Exception {
    XContent xContent = XContentFactory.xContent(data);
    int from = 0;
    int length = data.length();
    byte marker = xContent.streamSeparator();
    while (true) {
        int nextMarker = findNextMarker(marker, from, data, length);
        if (nextMarker == -1) {
            break;
        }
        // support first line with \n
        if (nextMarker == 0) {
            from = nextMarker + 1;
            continue;
        }

        SearchRequest searchRequest = new SearchRequest();
        if (indices != null) {
            searchRequest.indices(indices);
        }
        if (indicesOptions != null) {
            searchRequest.indicesOptions(indicesOptions);
        }
        if (types != null && types.length > 0) {
            searchRequest.types(types);
        }
        if (routing != null) {
            searchRequest.routing(routing);
        }
        searchRequest.searchType(searchType);

        IndicesOptions defaultOptions = IndicesOptions.strictExpandOpenAndForbidClosed();


        // now parse the action
        if (nextMarker - from > 0) {
            try (XContentParser parser = xContent.createParser(data.slice(from, nextMarker - from))) {
                Map<String, Object> source = parser.map();
                for (Map.Entry<String, Object> entry : source.entrySet()) {
                    Object value = entry.getValue();
                    if ("index".equals(entry.getKey()) || "indices".equals(entry.getKey())) {
                        if (!allowExplicitIndex) {
                            throw new IllegalArgumentException("explicit index in multi percolate is not allowed");
                        }
                        searchRequest.indices(nodeStringArrayValue(value));
                    } else if ("type".equals(entry.getKey()) || "types".equals(entry.getKey())) {
                        searchRequest.types(nodeStringArrayValue(value));
                    } else if ("search_type".equals(entry.getKey()) || "searchType".equals(entry.getKey())) {
                        searchRequest.searchType(nodeStringValue(value, null));
                    } else if ("request_cache".equals(entry.getKey()) || "requestCache".equals(entry.getKey())) {
                        searchRequest.requestCache(nodeBooleanValue(value));
                    } else if ("preference".equals(entry.getKey())) {
                        searchRequest.preference(nodeStringValue(value, null));
                    } else if ("routing".equals(entry.getKey())) {
                        searchRequest.routing(nodeStringValue(value, null));
                    }
                }
                defaultOptions = IndicesOptions.fromMap(source, defaultOptions);
            }
        }
        searchRequest.indicesOptions(defaultOptions);

        // move pointers
        from = nextMarker + 1;
        // now for the body
        nextMarker = findNextMarker(marker, from, data, length);
        if (nextMarker == -1) {
            break;
        }
        if (isTemplateRequest) {
            searchRequest.templateSource(data.slice(from,  nextMarker - from));
        } else {
            searchRequest.source(data.slice(from, nextMarker - from));
        }
        // move pointers
        from = nextMarker + 1;

        add(searchRequest);
    }

    return this;
}
 
Example 16
Source File: RestoreParser.java    From elasticsearch-inout-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Main method of this class to parse given payload of _restore action
 *
 * @param context
 * @param source
 * @throws org.elasticsearch.search.SearchParseException
 */
public void parseSource(ImportContext context, BytesReference source) throws ImportParseException {
    XContentParser parser = null;
    this.setDefaults(context);
    context.settings(true);
    context.mappings(true);
    try {
        if (source != null && source.length() != 0) {
            parser = XContentFactory.xContent(source).createParser(source);
            XContentParser.Token token;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    String fieldName = parser.currentName();
                    parser.nextToken();
                    ImportParseElement element = elementParsers.get(fieldName);
                    if (element == null) {
                        throw new ImportParseException(context, "No parser for element [" + fieldName + "]");
                    }
                    element.parse(parser, context);
                } else if (token == null) {
                    break;
                }
            }
        }
        if (context.directory() == null) {
            context.directory(DumpParser.DEFAULT_DIR);
        }
    } catch (Exception e) {
        String sSource = "_na_";
        try {
            sSource = XContentHelper.convertToJson(source, false);
        } catch (Throwable e1) {
            // ignore
        }
        throw new ImportParseException(context, "Failed to parse source [" + sSource + "]", e);
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
}
 
Example 17
Source File: XContentFactory.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Guesses the content type based on the provided bytes.
 */
public static XContentType xContentType(BytesReference bytes) {
    int length = bytes.length();
    if (length == 0) {
        return null;
    }
    byte first = bytes.get(0);
    if (first == '{') {
        return XContentType.JSON;
    }
    if (length > 2 && first == SmileConstants.HEADER_BYTE_1 && bytes.get(1) == SmileConstants.HEADER_BYTE_2 && bytes.get(2) == SmileConstants.HEADER_BYTE_3) {
        return XContentType.SMILE;
    }
    if (length > 2 && first == '-' && bytes.get(1) == '-' && bytes.get(2) == '-') {
        return XContentType.YAML;
    }
    // CBOR logic similar to CBORFactory#hasCBORFormat
    if (first == CBORConstants.BYTE_OBJECT_INDEFINITE && length > 1){
        return XContentType.CBOR;
    }
    if (CBORConstants.hasMajorType(CBORConstants.MAJOR_TYPE_TAG, first) && length > 2) {
        // Actually, specific "self-describe tag" is a very good indicator
        if (first == (byte) 0xD9 && bytes.get(1) == (byte) 0xD9 && bytes.get(2) == (byte) 0xF7) {
            return XContentType.CBOR;
        }
    }
    // for small objects, some encoders just encode as major type object, we can safely
    // say its CBOR since it doesn't contradict SMILE or JSON, and its a last resort
    if (CBORConstants.hasMajorType(CBORConstants.MAJOR_TYPE_OBJECT, first)) {
        return XContentType.CBOR;
    }

    int jsonStart = 0;
    // JSON may be preceded by UTF-8 BOM
    if (length > 3 && first == (byte) 0xEF && bytes.get(1) == (byte) 0xBB && bytes.get(2) == (byte) 0xBF) {
        jsonStart = 3;
    }

    // a last chance for JSON
    for (int i = jsonStart; i < length; i++) {
        byte b = bytes.get(i);
        if (b == '{') {
            return XContentType.JSON;
        }
        if (Character.isWhitespace(b) == false) {
            break;
        }
    }
    return null;
}
 
Example 18
Source File: WrapperQueryBuilder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a query builder given a query provided as a {@link BytesReference}
 */
public WrapperQueryBuilder(BytesReference source) {
    this.source = source.array();
    this.offset = source.arrayOffset();
    this.length = source.length();
}
 
Example 19
Source File: TcpTransport.java    From crate with Apache License 2.0 4 votes vote down vote up
/**
 * Validates the first N bytes of the message header and returns <code>false</code> if the message is
 * a ping message and has no payload ie. isn't a real user level message.
 *
 * @throws IllegalStateException    if the message is too short, less than the header or less that the header plus the message size
 * @throws HttpOnTransportException if the message has no valid header and appears to be an HTTP message
 * @throws IllegalArgumentException if the message is greater that the maximum allowed frame size. This is dependent on the available
 *                                  memory.
 */
public static boolean validateMessageHeader(BytesReference buffer) throws IOException {
    final int sizeHeaderLength = TcpHeader.MARKER_BYTES_SIZE + TcpHeader.MESSAGE_LENGTH_SIZE;
    if (buffer.length() < sizeHeaderLength) {
        throw new IllegalStateException("message size must be >= to the header size");
    }
    int offset = 0;
    if (buffer.get(offset) != 'E' || buffer.get(offset + 1) != 'S') {
        // special handling for what is probably HTTP
        if (bufferStartsWith(buffer, offset, "GET ") ||
            bufferStartsWith(buffer, offset, "POST ") ||
            bufferStartsWith(buffer, offset, "PUT ") ||
            bufferStartsWith(buffer, offset, "HEAD ") ||
            bufferStartsWith(buffer, offset, "DELETE ") ||
            bufferStartsWith(buffer, offset, "OPTIONS ") ||
            bufferStartsWith(buffer, offset, "PATCH ") ||
            bufferStartsWith(buffer, offset, "TRACE ")) {

            throw new HttpOnTransportException("This is not an HTTP port");
        }

        // we have 6 readable bytes, show 4 (should be enough)
        throw new StreamCorruptedException("invalid internal transport message format, got ("
            + Integer.toHexString(buffer.get(offset) & 0xFF) + ","
            + Integer.toHexString(buffer.get(offset + 1) & 0xFF) + ","
            + Integer.toHexString(buffer.get(offset + 2) & 0xFF) + ","
            + Integer.toHexString(buffer.get(offset + 3) & 0xFF) + ")");
    }

    final int dataLen;
    try (StreamInput input = buffer.streamInput()) {
        input.skip(TcpHeader.MARKER_BYTES_SIZE);
        dataLen = input.readInt();
        if (dataLen == PING_DATA_SIZE) {
            // discard the messages we read and continue, this is achieved by skipping the bytes
            // and returning null
            return false;
        }
    }

    if (dataLen <= 0) {
        throw new StreamCorruptedException("invalid data length: " + dataLen);
    }
    // safety against too large frames being sent
    if (dataLen > NINETY_PER_HEAP_SIZE) {
        throw new IllegalArgumentException("transport content length received [" + new ByteSizeValue(dataLen) + "] exceeded ["
            + new ByteSizeValue(NINETY_PER_HEAP_SIZE) + "]");
    }

    if (buffer.length() < dataLen + sizeHeaderLength) {
        throw new IllegalStateException("buffer must be >= to the message size but wasn't");
    }
    return true;
}
 
Example 20
Source File: Strings.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * Check that the given BytesReference is neither <code>null</code> nor of length 0
 * Note: Will return <code>true</code> for a BytesReference that purely consists of whitespace.
 *
 * @param bytesReference the BytesReference to check (may be <code>null</code>)
 * @return <code>true</code> if the BytesReference is not null and has length
 * @see #hasLength(CharSequence)
 */
public static boolean hasLength(BytesReference bytesReference) {
    return (bytesReference != null && bytesReference.length() > 0);
}