99
1010package org .elasticsearch .ingest .geoip ;
1111
12- import com .maxmind .db .DatabaseRecord ;
13- import com .maxmind .db .Network ;
1412import com .maxmind .db .NoCache ;
1513import com .maxmind .db .Reader ;
16- import com .maxmind .geoip2 .model .AnonymousIpResponse ;
17- import com .maxmind .geoip2 .model .AsnResponse ;
18- import com .maxmind .geoip2 .model .CityResponse ;
19- import com .maxmind .geoip2 .model .ConnectionTypeResponse ;
20- import com .maxmind .geoip2 .model .CountryResponse ;
21- import com .maxmind .geoip2 .model .DomainResponse ;
22- import com .maxmind .geoip2 .model .EnterpriseResponse ;
23- import com .maxmind .geoip2 .model .IspResponse ;
2414
2515import org .apache .logging .log4j .LogManager ;
2616import org .apache .logging .log4j .Logger ;
2717import org .apache .lucene .util .SetOnce ;
2818import org .elasticsearch .ExceptionsHelper ;
2919import org .elasticsearch .common .CheckedBiFunction ;
3020import org .elasticsearch .common .CheckedSupplier ;
31- import org .elasticsearch .common .network .InetAddresses ;
32- import org .elasticsearch .common .network .NetworkAddress ;
3321import org .elasticsearch .core .Booleans ;
3422import org .elasticsearch .core .IOUtils ;
3523import org .elasticsearch .core .Nullable ;
3624import org .elasticsearch .core .SuppressForbidden ;
3725
3826import java .io .File ;
3927import java .io .IOException ;
40- import java .net .InetAddress ;
4128import java .nio .file .Files ;
4229import java .nio .file .Path ;
43- import java .util .List ;
4430import java .util .Objects ;
45- import java .util .Optional ;
4631import java .util .concurrent .atomic .AtomicInteger ;
4732
4833/**
4934 * Facilitates lazy loading of the database reader, so that when the geoip plugin is installed, but not used,
5035 * no memory is being wasted on the database reader.
5136 */
52- class DatabaseReaderLazyLoader implements IpDatabase {
37+ public class DatabaseReaderLazyLoader implements IpDatabase {
5338
5439 private static final boolean LOAD_DATABASE_ON_HEAP = Booleans .parseBoolean (System .getProperty ("es.geoip.load_db_on_heap" , "false" ));
5540
@@ -96,94 +81,6 @@ public final String getDatabaseType() throws IOException {
9681 return databaseType .get ();
9782 }
9883
99- @ Nullable
100- @ Override
101- public CityResponse getCity (String ipAddress ) {
102- return getResponse (ipAddress , (reader , ip ) -> lookup (reader , ip , CityResponse .class , CityResponse ::new ));
103- }
104-
105- @ Nullable
106- @ Override
107- public CountryResponse getCountry (String ipAddress ) {
108- return getResponse (ipAddress , (reader , ip ) -> lookup (reader , ip , CountryResponse .class , CountryResponse ::new ));
109- }
110-
111- @ Nullable
112- @ Override
113- public AsnResponse getAsn (String ipAddress ) {
114- return getResponse (
115- ipAddress ,
116- (reader , ip ) -> lookup (
117- reader ,
118- ip ,
119- AsnResponse .class ,
120- (response , responseIp , network , locales ) -> new AsnResponse (response , responseIp , network )
121- )
122- );
123- }
124-
125- @ Nullable
126- @ Override
127- public AnonymousIpResponse getAnonymousIp (String ipAddress ) {
128- return getResponse (
129- ipAddress ,
130- (reader , ip ) -> lookup (
131- reader ,
132- ip ,
133- AnonymousIpResponse .class ,
134- (response , responseIp , network , locales ) -> new AnonymousIpResponse (response , responseIp , network )
135- )
136- );
137- }
138-
139- @ Nullable
140- @ Override
141- public ConnectionTypeResponse getConnectionType (String ipAddress ) {
142- return getResponse (
143- ipAddress ,
144- (reader , ip ) -> lookup (
145- reader ,
146- ip ,
147- ConnectionTypeResponse .class ,
148- (response , responseIp , network , locales ) -> new ConnectionTypeResponse (response , responseIp , network )
149- )
150- );
151- }
152-
153- @ Nullable
154- @ Override
155- public DomainResponse getDomain (String ipAddress ) {
156- return getResponse (
157- ipAddress ,
158- (reader , ip ) -> lookup (
159- reader ,
160- ip ,
161- DomainResponse .class ,
162- (response , responseIp , network , locales ) -> new DomainResponse (response , responseIp , network )
163- )
164- );
165- }
166-
167- @ Nullable
168- @ Override
169- public EnterpriseResponse getEnterprise (String ipAddress ) {
170- return getResponse (ipAddress , (reader , ip ) -> lookup (reader , ip , EnterpriseResponse .class , EnterpriseResponse ::new ));
171- }
172-
173- @ Nullable
174- @ Override
175- public IspResponse getIsp (String ipAddress ) {
176- return getResponse (
177- ipAddress ,
178- (reader , ip ) -> lookup (
179- reader ,
180- ip ,
181- IspResponse .class ,
182- (response , responseIp , network , locales ) -> new IspResponse (response , responseIp , network )
183- )
184- );
185- }
186-
18784 boolean preLookup () {
18885 return currentUsages .updateAndGet (current -> current < 0 ? current : current + 1 ) > 0 ;
18986 }
@@ -199,14 +96,12 @@ int current() {
19996 return currentUsages .get ();
20097 }
20198
99+ @ Override
202100 @ Nullable
203- private <RESPONSE > RESPONSE getResponse (
204- String ipAddress ,
205- CheckedBiFunction <Reader , String , Optional <RESPONSE >, Exception > responseProvider
206- ) {
101+ public <RESPONSE > RESPONSE getResponse (String ipAddress , CheckedBiFunction <Reader , String , RESPONSE , Exception > responseProvider ) {
207102 return cache .putIfAbsent (ipAddress , databasePath .toString (), ip -> {
208103 try {
209- return responseProvider .apply (get (), ipAddress ). orElse ( null ) ;
104+ return responseProvider .apply (get (), ipAddress );
210105 } catch (Exception e ) {
211106 throw ExceptionsHelper .convertToRuntime (e );
212107 }
@@ -263,23 +158,6 @@ private static File pathToFile(Path databasePath) {
263158 return databasePath .toFile ();
264159 }
265160
266- @ FunctionalInterface
267- private interface ResponseBuilder <RESPONSE > {
268- RESPONSE build (RESPONSE response , String responseIp , Network network , List <String > locales );
269- }
270-
271- private <RESPONSE > Optional <RESPONSE > lookup (Reader reader , String ip , Class <RESPONSE > clazz , ResponseBuilder <RESPONSE > builder )
272- throws IOException {
273- InetAddress inetAddress = InetAddresses .forString (ip );
274- DatabaseRecord <RESPONSE > record = reader .getRecord (inetAddress , clazz );
275- RESPONSE result = record .getData ();
276- if (result == null ) {
277- return Optional .empty ();
278- } else {
279- return Optional .of (builder .build (result , NetworkAddress .format (inetAddress ), record .getNetwork (), List .of ("en" )));
280- }
281- }
282-
283161 long getBuildDateMillis () throws IOException {
284162 if (buildDate .get () == null ) {
285163 synchronized (buildDate ) {
0 commit comments