mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-27 15:39:49 +00:00
[PURIFY] Remove ProtocolUtils, TimeUtils, and XContentSource from HLRC (#64)
This commit removes the ProtocolUtils, TimeUtils, and XContentSource utility classes which is only used for xpack HLRC. Signed-off-by: Peter Nied <petern@amazon.com>
This commit is contained in:
parent
f88b30f46d
commit
caee04a348
@ -1,72 +0,0 @@
|
|||||||
/*
|
|
||||||
* Licensed to Elasticsearch under one or more contributor
|
|
||||||
* license agreements. See the NOTICE file distributed with
|
|
||||||
* this work for additional information regarding copyright
|
|
||||||
* ownership. Elasticsearch licenses this file to you under
|
|
||||||
* the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
* not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing,
|
|
||||||
* software distributed under the License is distributed on an
|
|
||||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
||||||
* KIND, either express or implied. See the License for the
|
|
||||||
* specific language governing permissions and limitations
|
|
||||||
* under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.elasticsearch.client.common;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Common utilities used for XPack protocol classes
|
|
||||||
*/
|
|
||||||
public final class ProtocolUtils {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Implements equals for a map of string arrays
|
|
||||||
*
|
|
||||||
* The map of string arrays is used in some XPack protocol classes but does't work with equal.
|
|
||||||
*/
|
|
||||||
public static boolean equals(Map<String, String[]> a, Map<String, String[]> b) {
|
|
||||||
if (a == null) {
|
|
||||||
return b == null;
|
|
||||||
}
|
|
||||||
if (b == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (a.size() != b.size()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
for (Map.Entry<String, String[]> entry : a.entrySet()) {
|
|
||||||
String[] val = entry.getValue();
|
|
||||||
String key = entry.getKey();
|
|
||||||
if (val == null) {
|
|
||||||
if (b.get(key) != null || b.containsKey(key) == false) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (Arrays.equals(val, b.get(key)) == false) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Implements hashCode for map of string arrays
|
|
||||||
*
|
|
||||||
* The map of string arrays does't work with hashCode.
|
|
||||||
*/
|
|
||||||
public static int hashCode(Map<String, String[]> a) {
|
|
||||||
int hash = 0;
|
|
||||||
for (Map.Entry<String, String[]> entry : a.entrySet())
|
|
||||||
hash += Arrays.hashCode(entry.getValue());
|
|
||||||
return hash;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,67 +0,0 @@
|
|||||||
/*
|
|
||||||
* Licensed to Elasticsearch under one or more contributor
|
|
||||||
* license agreements. See the NOTICE file distributed with
|
|
||||||
* this work for additional information regarding copyright
|
|
||||||
* ownership. Elasticsearch licenses this file to you under
|
|
||||||
* the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
* not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing,
|
|
||||||
* software distributed under the License is distributed on an
|
|
||||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
||||||
* KIND, either express or implied. See the License for the
|
|
||||||
* specific language governing permissions and limitations
|
|
||||||
* under the License.
|
|
||||||
*/
|
|
||||||
package org.elasticsearch.client.common;
|
|
||||||
|
|
||||||
import org.elasticsearch.common.time.DateFormatters;
|
|
||||||
import org.elasticsearch.common.xcontent.XContentParser;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.time.Instant;
|
|
||||||
import java.time.format.DateTimeFormatter;
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
public final class TimeUtil {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse out a Date object given the current parser and field name.
|
|
||||||
*
|
|
||||||
* @param parser current XContentParser
|
|
||||||
* @param fieldName the field's preferred name (utilized in exception)
|
|
||||||
* @return parsed Date object
|
|
||||||
* @throws IOException from XContentParser
|
|
||||||
*/
|
|
||||||
public static Date parseTimeField(XContentParser parser, String fieldName) throws IOException {
|
|
||||||
if (parser.currentToken() == XContentParser.Token.VALUE_NUMBER) {
|
|
||||||
return new Date(parser.longValue());
|
|
||||||
} else if (parser.currentToken() == XContentParser.Token.VALUE_STRING) {
|
|
||||||
return new Date(DateFormatters.from(DateTimeFormatter.ISO_INSTANT.parse(parser.text())).toInstant().toEpochMilli());
|
|
||||||
}
|
|
||||||
throw new IllegalArgumentException(
|
|
||||||
"unexpected token [" + parser.currentToken() + "] for [" + fieldName + "]");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse out an Instant object given the current parser and field name.
|
|
||||||
*
|
|
||||||
* @param parser current XContentParser
|
|
||||||
* @param fieldName the field's preferred name (utilized in exception)
|
|
||||||
* @return parsed Instant object
|
|
||||||
* @throws IOException from XContentParser
|
|
||||||
*/
|
|
||||||
public static Instant parseTimeFieldToInstant(XContentParser parser, String fieldName) throws IOException {
|
|
||||||
if (parser.currentToken() == XContentParser.Token.VALUE_NUMBER) {
|
|
||||||
return Instant.ofEpochMilli(parser.longValue());
|
|
||||||
} else if (parser.currentToken() == XContentParser.Token.VALUE_STRING) {
|
|
||||||
return DateFormatters.from(DateTimeFormatter.ISO_INSTANT.parse(parser.text())).toInstant();
|
|
||||||
}
|
|
||||||
throw new IllegalArgumentException(
|
|
||||||
"unexpected token [" + parser.currentToken() + "] for [" + fieldName + "]");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,85 +0,0 @@
|
|||||||
/*
|
|
||||||
* Licensed to Elasticsearch under one or more contributor
|
|
||||||
* license agreements. See the NOTICE file distributed with
|
|
||||||
* this work for additional information regarding copyright
|
|
||||||
* ownership. Elasticsearch licenses this file to you under
|
|
||||||
* the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
* not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing,
|
|
||||||
* software distributed under the License is distributed on an
|
|
||||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
||||||
* KIND, either express or implied. See the License for the
|
|
||||||
* specific language governing permissions and limitations
|
|
||||||
* under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.elasticsearch.client.common;
|
|
||||||
|
|
||||||
import org.elasticsearch.common.xcontent.ObjectPath;
|
|
||||||
import org.elasticsearch.common.xcontent.XContentParser;
|
|
||||||
import org.elasticsearch.common.xcontent.XContentUtils;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Encapsulates the xcontent source
|
|
||||||
*/
|
|
||||||
public class XContentSource {
|
|
||||||
|
|
||||||
private final Object data;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructs a new XContentSource out of the given parser
|
|
||||||
*/
|
|
||||||
public XContentSource(XContentParser parser) throws IOException {
|
|
||||||
this.data = XContentUtils.readValue(parser, parser.nextToken());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return true if the top level value of the source is a map
|
|
||||||
*/
|
|
||||||
public boolean isMap() {
|
|
||||||
return data instanceof Map;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return The source as a map
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public Map<String, Object> getAsMap() {
|
|
||||||
return (Map<String, Object>) data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return true if the top level value of the source is a list
|
|
||||||
*/
|
|
||||||
public boolean isList() {
|
|
||||||
return data instanceof List;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return The source as a list
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public List<Object> getAsList() {
|
|
||||||
return (List<Object>) data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Extracts a value identified by the given path in the source.
|
|
||||||
*
|
|
||||||
* @param path a dot notation path to the requested value
|
|
||||||
* @return The extracted value or {@code null} if no value is associated with the given path
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public <T> T getValue(String path) {
|
|
||||||
return (T) ObjectPath.eval(path, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,72 +0,0 @@
|
|||||||
/*
|
|
||||||
* Licensed to Elasticsearch under one or more contributor
|
|
||||||
* license agreements. See the NOTICE file distributed with
|
|
||||||
* this work for additional information regarding copyright
|
|
||||||
* ownership. Elasticsearch licenses this file to you under
|
|
||||||
* the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
* not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing,
|
|
||||||
* software distributed under the License is distributed on an
|
|
||||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
||||||
* KIND, either express or implied. See the License for the
|
|
||||||
* specific language governing permissions and limitations
|
|
||||||
* under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.elasticsearch.client.common;
|
|
||||||
|
|
||||||
import org.elasticsearch.test.ESTestCase;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class ProtocolUtilsTests extends ESTestCase {
|
|
||||||
|
|
||||||
public void testMapStringEqualsAndHash() {
|
|
||||||
assertTrue(ProtocolUtils.equals(null, null));
|
|
||||||
assertFalse(ProtocolUtils.equals(null, new HashMap<>()));
|
|
||||||
assertFalse(ProtocolUtils.equals(new HashMap<>(), null));
|
|
||||||
|
|
||||||
Map<String, String[]> a = new HashMap<>();
|
|
||||||
a.put("foo", new String[] { "a", "b" });
|
|
||||||
a.put("bar", new String[] { "b", "c" });
|
|
||||||
|
|
||||||
Map<String, String[]> b = new HashMap<>();
|
|
||||||
b.put("foo", new String[] { "a", "b" });
|
|
||||||
|
|
||||||
assertFalse(ProtocolUtils.equals(a, b));
|
|
||||||
assertFalse(ProtocolUtils.equals(b, a));
|
|
||||||
|
|
||||||
b.put("bar", new String[] { "c", "b" });
|
|
||||||
|
|
||||||
assertFalse(ProtocolUtils.equals(a, b));
|
|
||||||
assertFalse(ProtocolUtils.equals(b, a));
|
|
||||||
|
|
||||||
b.put("bar", new String[] { "b", "c" });
|
|
||||||
|
|
||||||
assertTrue(ProtocolUtils.equals(a, b));
|
|
||||||
assertTrue(ProtocolUtils.equals(b, a));
|
|
||||||
assertEquals(ProtocolUtils.hashCode(a), ProtocolUtils.hashCode(b));
|
|
||||||
|
|
||||||
b.put("baz", new String[] { "b", "c" });
|
|
||||||
|
|
||||||
assertFalse(ProtocolUtils.equals(a, b));
|
|
||||||
assertFalse(ProtocolUtils.equals(b, a));
|
|
||||||
|
|
||||||
a.put("non", null);
|
|
||||||
|
|
||||||
assertFalse(ProtocolUtils.equals(a, b));
|
|
||||||
assertFalse(ProtocolUtils.equals(b, a));
|
|
||||||
|
|
||||||
b.put("non", null);
|
|
||||||
b.remove("baz");
|
|
||||||
|
|
||||||
assertTrue(ProtocolUtils.equals(a, b));
|
|
||||||
assertTrue(ProtocolUtils.equals(b, a));
|
|
||||||
assertEquals(ProtocolUtils.hashCode(a), ProtocolUtils.hashCode(b));
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user