Skip to content

Commit c4c48f4

Browse files
committed
Address comments from review
1 parent 3cb3979 commit c4c48f4

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

server/src/main/java/org/elasticsearch/index/mapper/ESInetAddressPoint.java

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import org.apache.lucene.util.BytesRef;
1616
import org.elasticsearch.common.network.CIDRUtils;
1717
import org.elasticsearch.common.network.InetAddresses;
18-
import org.elasticsearch.core.SuppressForbidden;
18+
import org.elasticsearch.common.network.NetworkAddress;
1919
import org.elasticsearch.xcontent.XContentString;
2020

2121
import java.net.InetAddress;
@@ -38,17 +38,46 @@ class ESInetAddressPoint extends Field {
3838
private final XContentString ipString;
3939
private final InetAddress inetAddress;
4040

41-
protected ESInetAddressPoint(String name, XContentString ipString) {
41+
/**
42+
* Creates a new ESInetAddressPoint, indexing the provided address.
43+
* <p>
44+
* This is the difference compared to {@link #ESInetAddressPoint(String, InetAddress)}
45+
* and {@link InetAddressPoint#InetAddressPoint(String, InetAddress)}
46+
* is that this constructor uses a more efficient way to parse the IP address that avoids the need to create
47+
* a {@link String} and an {@link InetAddress} object for the IP address.
48+
*
49+
* @param name the name of the field
50+
* @param value the IP address as a string
51+
* @throws IllegalArgumentException if the field name or value is null or if the IP address is invalid
52+
*/
53+
protected ESInetAddressPoint(String name, XContentString value) {
4254
super(name, TYPE);
43-
this.fieldsData = new BytesRef(InetAddresses.encodeAsIpv6(ipString));
44-
this.ipString = ipString;
55+
if (value == null) {
56+
throw new IllegalArgumentException("point must not be null");
57+
}
58+
this.fieldsData = new BytesRef(InetAddresses.encodeAsIpv6(value));
59+
this.ipString = value;
4560
this.inetAddress = null;
4661
}
4762

48-
protected ESInetAddressPoint(String name, InetAddress inetAddress) {
63+
/**
64+
* Creates a new ESInetAddressPoint, indexing the provided address.
65+
* <p>
66+
* This constructor is similar to Lucene's InetAddressPoint.
67+
* For performance reasons, it is recommended to use the constructor that accepts
68+
* an {@link XContentString} representation of the IP address instead.
69+
*
70+
* @param name field name
71+
* @param value InetAddress value
72+
* @throws IllegalArgumentException if the field name or value is null.
73+
*/
74+
protected ESInetAddressPoint(String name, InetAddress value) {
4975
super(name, TYPE);
50-
this.fieldsData = new BytesRef(CIDRUtils.encode(inetAddress.getAddress()));
51-
this.inetAddress = inetAddress;
76+
if (value == null) {
77+
throw new IllegalArgumentException("point must not be null");
78+
}
79+
this.fieldsData = new BytesRef(CIDRUtils.encode(value.getAddress()));
80+
this.inetAddress = value;
5281
this.ipString = null;
5382
}
5483

@@ -63,10 +92,6 @@ public InetAddress getInetAddress() {
6392
}
6493

6594
@Override
66-
@SuppressForbidden(
67-
reason = "Calling InetAddress#getHostAddress to mimic what InetAddressPoint does. "
68-
+ "Some tests depend on the exact string representation."
69-
)
7095
public String toString() {
7196
StringBuilder result = new StringBuilder();
7297
result.append(getClass().getSimpleName());
@@ -79,10 +104,10 @@ public String toString() {
79104
InetAddress address = InetAddressPoint.decode(BytesRef.deepCopyOf(bytes).bytes);
80105
if (address.getAddress().length == 16) {
81106
result.append('[');
82-
result.append(address.getHostAddress());
107+
result.append(NetworkAddress.format(address));
83108
result.append(']');
84109
} else {
85-
result.append(address.getHostAddress());
110+
result.append(NetworkAddress.format(address));
86111
}
87112

88113
result.append('>');

server/src/test/java/org/elasticsearch/index/mapper/IpScriptMapperTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,16 @@ protected IpFieldScript.Factory multipleValuesScript() {
7575
@Override
7676
protected void assertMultipleValues(List<IndexableField> fields) {
7777
assertEquals(4, fields.size());
78-
assertEquals("ESInetAddressPoint <field:[0:0:0:0:0:0:0:1]>", fields.get(0).toString());
78+
assertEquals("ESInetAddressPoint <field:[::1]>", fields.get(0).toString());
7979
assertEquals("docValuesType=SORTED_SET<field:[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1]>", fields.get(1).toString());
80-
assertEquals("ESInetAddressPoint <field:[0:0:0:0:0:0:0:2]>", fields.get(2).toString());
80+
assertEquals("ESInetAddressPoint <field:[::2]>", fields.get(2).toString());
8181
assertEquals("docValuesType=SORTED_SET<field:[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2]>", fields.get(3).toString());
8282
}
8383

8484
@Override
8585
protected void assertDocValuesDisabled(List<IndexableField> fields) {
8686
assertEquals(1, fields.size());
87-
assertEquals("ESInetAddressPoint <field:[0:0:0:0:0:0:0:1]>", fields.get(0).toString());
87+
assertEquals("ESInetAddressPoint <field:[::1]>", fields.get(0).toString());
8888
}
8989

9090
@Override

0 commit comments

Comments
 (0)