Skip to content

Commit b8217ab

Browse files
authored
Use a custom cache record for CityResponse (#125806) (#125808)
1 parent 2e0d2cb commit b8217ab

File tree

1 file changed

+85
-53
lines changed

1 file changed

+85
-53
lines changed

Diff for: modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/MaxmindIpDataLookups.java

+85-53
Original file line numberDiff line numberDiff line change
@@ -201,87 +201,121 @@ protected Map<String, Object> transform(final AsnResponse response) {
201201
}
202202
}
203203

204-
static class City extends AbstractBase<CityResponse, CityResponse> {
204+
record CacheableCityResponse(
205+
Boolean isInEuropeanUnion,
206+
String countryIsoCode,
207+
String countryName,
208+
String continentCode,
209+
String continentName,
210+
String regionIsoCode,
211+
String regionName,
212+
String cityName,
213+
String timezone,
214+
Double latitude,
215+
Double longitude,
216+
Integer accuracyRadius,
217+
String postalCode,
218+
Boolean registeredCountryIsInEuropeanUnion,
219+
String registeredCountryIsoCode,
220+
String registeredCountryName
221+
) {}
222+
223+
static class City extends AbstractBase<CityResponse, Result<CacheableCityResponse>> {
205224
City(final Set<Database.Property> properties) {
206225
super(properties, CityResponse.class, CityResponse::new);
207226
}
208227

209228
@Override
210-
protected CityResponse cacheableRecord(CityResponse response) {
211-
return response;
229+
protected Result<CacheableCityResponse> cacheableRecord(CityResponse response) {
230+
final com.maxmind.geoip2.record.Country country = response.getCountry();
231+
final Continent continent = response.getContinent();
232+
final Subdivision subdivision = response.getMostSpecificSubdivision();
233+
final com.maxmind.geoip2.record.City city = response.getCity();
234+
final Location location = response.getLocation();
235+
final Postal postal = response.getPostal();
236+
final com.maxmind.geoip2.record.Country registeredCountry = response.getRegisteredCountry();
237+
final Traits traits = response.getTraits();
238+
239+
return new Result<>(
240+
new CacheableCityResponse(
241+
isInEuropeanUnion(country),
242+
country.getIsoCode(),
243+
country.getName(),
244+
continent.getCode(),
245+
continent.getName(),
246+
regionIsoCode(country, subdivision),
247+
subdivision.getName(),
248+
city.getName(),
249+
location.getTimeZone(),
250+
location.getLatitude(),
251+
location.getLongitude(),
252+
location.getAccuracyRadius(),
253+
postal.getCode(),
254+
isInEuropeanUnion(registeredCountry),
255+
registeredCountry.getIsoCode(),
256+
registeredCountry.getName()
257+
),
258+
traits.getIpAddress(),
259+
traits.getNetwork().toString()
260+
);
212261
}
213262

214263
@Override
215-
protected Map<String, Object> transform(final CityResponse response) {
216-
com.maxmind.geoip2.record.Country country = response.getCountry();
217-
com.maxmind.geoip2.record.Country registeredCountry = response.getRegisteredCountry();
218-
com.maxmind.geoip2.record.City city = response.getCity();
219-
Location location = response.getLocation();
220-
Continent continent = response.getContinent();
221-
Subdivision subdivision = response.getMostSpecificSubdivision();
222-
Postal postal = response.getPostal();
264+
protected Map<String, Object> transform(final Result<CacheableCityResponse> result) {
265+
CacheableCityResponse response = result.result();
223266

224267
Map<String, Object> data = new HashMap<>();
225268
for (Database.Property property : this.properties) {
226269
switch (property) {
227-
case IP -> data.put("ip", response.getTraits().getIpAddress());
270+
case IP -> data.put("ip", result.ip());
228271
case COUNTRY_IN_EUROPEAN_UNION -> {
229-
Boolean isInEuropeanUnion = isInEuropeanUnion(country);
230-
if (isInEuropeanUnion != null) {
231-
data.put("country_in_european_union", isInEuropeanUnion);
272+
if (response.isInEuropeanUnion != null) {
273+
data.put("country_in_european_union", response.isInEuropeanUnion);
232274
}
233275
}
234276
case COUNTRY_ISO_CODE -> {
235-
String countryIsoCode = country.getIsoCode();
236-
if (countryIsoCode != null) {
237-
data.put("country_iso_code", countryIsoCode);
277+
if (response.countryIsoCode != null) {
278+
data.put("country_iso_code", response.countryIsoCode);
238279
}
239280
}
240281
case COUNTRY_NAME -> {
241-
String countryName = country.getName();
242-
if (countryName != null) {
243-
data.put("country_name", countryName);
282+
if (response.countryName != null) {
283+
data.put("country_name", response.countryName);
244284
}
245285
}
246286
case CONTINENT_CODE -> {
247-
String continentCode = continent.getCode();
248-
if (continentCode != null) {
249-
data.put("continent_code", continentCode);
287+
if (response.continentCode != null) {
288+
data.put("continent_code", response.continentCode);
250289
}
251290
}
252291
case CONTINENT_NAME -> {
253-
String continentName = continent.getName();
254-
if (continentName != null) {
255-
data.put("continent_name", continentName);
292+
if (response.continentName != null) {
293+
data.put("continent_name", response.continentName);
256294
}
257295
}
258296
case REGION_ISO_CODE -> {
259-
String regionIsoCode = regionIsoCode(country, subdivision);
260-
if (regionIsoCode != null) {
261-
data.put("region_iso_code", regionIsoCode);
297+
if (response.regionIsoCode != null) {
298+
data.put("region_iso_code", response.regionIsoCode);
262299
}
263300
}
264301
case REGION_NAME -> {
265-
String subdivisionName = subdivision.getName();
266-
if (subdivisionName != null) {
267-
data.put("region_name", subdivisionName);
302+
if (response.regionName != null) {
303+
data.put("region_name", response.regionName);
268304
}
269305
}
270306
case CITY_NAME -> {
271-
String cityName = city.getName();
272-
if (cityName != null) {
273-
data.put("city_name", cityName);
307+
if (response.cityName != null) {
308+
data.put("city_name", response.cityName);
274309
}
275310
}
276311
case TIMEZONE -> {
277-
String locationTimeZone = location.getTimeZone();
278-
if (locationTimeZone != null) {
279-
data.put("timezone", locationTimeZone);
312+
if (response.timezone != null) {
313+
data.put("timezone", response.timezone);
280314
}
281315
}
282316
case LOCATION -> {
283-
Double latitude = location.getLatitude();
284-
Double longitude = location.getLongitude();
317+
Double latitude = response.latitude;
318+
Double longitude = response.longitude;
285319
if (latitude != null && longitude != null) {
286320
Map<String, Object> locationObject = new HashMap<>();
287321
locationObject.put("lat", latitude);
@@ -290,30 +324,28 @@ protected Map<String, Object> transform(final CityResponse response) {
290324
}
291325
}
292326
case ACCURACY_RADIUS -> {
293-
Integer accuracyRadius = location.getAccuracyRadius();
294-
if (accuracyRadius != null) {
295-
data.put("accuracy_radius", accuracyRadius);
327+
if (response.accuracyRadius != null) {
328+
data.put("accuracy_radius", response.accuracyRadius);
296329
}
297330
}
298331
case POSTAL_CODE -> {
299-
if (postal.getCode() != null) {
300-
data.put("postal_code", postal.getCode());
332+
if (response.postalCode != null) {
333+
data.put("postal_code", response.postalCode);
301334
}
302335
}
303336
case REGISTERED_COUNTRY_IN_EUROPEAN_UNION -> {
304-
Boolean isInEuropeanUnion = isInEuropeanUnion(registeredCountry);
305-
if (isInEuropeanUnion != null) {
306-
data.put("registered_country_in_european_union", isInEuropeanUnion);
337+
if (response.registeredCountryIsInEuropeanUnion != null) {
338+
data.put("registered_country_in_european_union", response.registeredCountryIsInEuropeanUnion);
307339
}
308340
}
309341
case REGISTERED_COUNTRY_ISO_CODE -> {
310-
if (registeredCountry.getIsoCode() != null) {
311-
data.put("registered_country_iso_code", registeredCountry.getIsoCode());
342+
if (response.registeredCountryIsoCode != null) {
343+
data.put("registered_country_iso_code", response.registeredCountryIsoCode);
312344
}
313345
}
314346
case REGISTERED_COUNTRY_NAME -> {
315-
if (registeredCountry.getName() != null) {
316-
data.put("registered_country_name", registeredCountry.getName());
347+
if (response.registeredCountryName != null) {
348+
data.put("registered_country_name", response.registeredCountryName);
317349
}
318350
}
319351
}

0 commit comments

Comments
 (0)