Search in sources :

Example 1 with HasAttributeNodeSelector

use of org.opensearch.client.HasAttributeNodeSelector in project OpenSearch by opensearch-project.

the class DoSection method parseAttributeValuesSelector.

private static NodeSelector parseAttributeValuesSelector(XContentParser parser) throws IOException {
    if (parser.currentToken() != XContentParser.Token.START_OBJECT) {
        throw new XContentParseException(parser.getTokenLocation(), "expected START_OBJECT");
    }
    String key = null;
    XContentParser.Token token;
    NodeSelector result = NodeSelector.ANY;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            key = parser.currentName();
        } else if (token.isValue()) {
            /*
                 * HasAttributeNodeSelector selects nodes that do not have
                 * attribute metadata set so it can be used against nodes that
                 * have not yet been sniffed. In these tests we expect the node
                 * metadata to be explicitly sniffed if we need it and we'd
                 * like to hard fail if it is not so we wrap the selector so we
                 * can assert that the data is sniffed.
                 */
            NodeSelector delegate = new HasAttributeNodeSelector(key, parser.text());
            NodeSelector newSelector = new NodeSelector() {

                @Override
                public void select(Iterable<Node> nodes) {
                    for (Node node : nodes) {
                        if (node.getAttributes() == null) {
                            throw new IllegalStateException("expected [attributes] metadata to be set but got " + node);
                        }
                    }
                    delegate.select(nodes);
                }

                @Override
                public String toString() {
                    return delegate.toString();
                }
            };
            result = result == NodeSelector.ANY ? newSelector : new ComposeNodeSelector(result, newSelector);
        } else {
            throw new XContentParseException(parser.getTokenLocation(), "expected [" + key + "] to be a value");
        }
    }
    return result;
}
Also used : Node(org.opensearch.client.Node) HasAttributeNodeSelector(org.opensearch.client.HasAttributeNodeSelector) HasAttributeNodeSelector(org.opensearch.client.HasAttributeNodeSelector) NodeSelector(org.opensearch.client.NodeSelector) XContentParser(org.opensearch.common.xcontent.XContentParser) XContentParseException(org.opensearch.common.xcontent.XContentParseException)

Aggregations

HasAttributeNodeSelector (org.opensearch.client.HasAttributeNodeSelector)1 Node (org.opensearch.client.Node)1 NodeSelector (org.opensearch.client.NodeSelector)1 XContentParseException (org.opensearch.common.xcontent.XContentParseException)1 XContentParser (org.opensearch.common.xcontent.XContentParser)1