9
9
10
10
package org .elasticsearch .ingest .geoip ;
11
11
12
- import com .maxmind .db .DatabaseRecord ;
13
- import com .maxmind .db .Network ;
14
12
import com .maxmind .db .NoCache ;
15
13
import 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 ;
24
14
25
15
import org .apache .logging .log4j .LogManager ;
26
16
import org .apache .logging .log4j .Logger ;
27
17
import org .apache .lucene .util .SetOnce ;
28
18
import org .elasticsearch .ExceptionsHelper ;
29
19
import org .elasticsearch .common .CheckedBiFunction ;
30
20
import org .elasticsearch .common .CheckedSupplier ;
31
- import org .elasticsearch .common .network .InetAddresses ;
32
- import org .elasticsearch .common .network .NetworkAddress ;
33
21
import org .elasticsearch .core .Booleans ;
34
22
import org .elasticsearch .core .IOUtils ;
35
23
import org .elasticsearch .core .Nullable ;
36
24
import org .elasticsearch .core .SuppressForbidden ;
37
25
38
26
import java .io .File ;
39
27
import java .io .IOException ;
40
- import java .net .InetAddress ;
41
28
import java .nio .file .Files ;
42
29
import java .nio .file .Path ;
43
- import java .util .List ;
44
30
import java .util .Objects ;
45
- import java .util .Optional ;
46
31
import java .util .concurrent .atomic .AtomicInteger ;
47
32
48
33
/**
49
34
* Facilitates lazy loading of the database reader, so that when the geoip plugin is installed, but not used,
50
35
* no memory is being wasted on the database reader.
51
36
*/
52
- class DatabaseReaderLazyLoader implements IpDatabase {
37
+ public class DatabaseReaderLazyLoader implements IpDatabase {
53
38
54
39
private static final boolean LOAD_DATABASE_ON_HEAP = Booleans .parseBoolean (System .getProperty ("es.geoip.load_db_on_heap" , "false" ));
55
40
@@ -96,94 +81,6 @@ public final String getDatabaseType() throws IOException {
96
81
return databaseType .get ();
97
82
}
98
83
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
-
187
84
boolean preLookup () {
188
85
return currentUsages .updateAndGet (current -> current < 0 ? current : current + 1 ) > 0 ;
189
86
}
@@ -199,14 +96,12 @@ int current() {
199
96
return currentUsages .get ();
200
97
}
201
98
99
+ @ Override
202
100
@ 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 ) {
207
102
return cache .putIfAbsent (ipAddress , databasePath .toString (), ip -> {
208
103
try {
209
- return responseProvider .apply (get (), ipAddress ). orElse ( null ) ;
104
+ return responseProvider .apply (get (), ipAddress );
210
105
} catch (Exception e ) {
211
106
throw ExceptionsHelper .convertToRuntime (e );
212
107
}
@@ -263,23 +158,6 @@ private static File pathToFile(Path databasePath) {
263
158
return databasePath .toFile ();
264
159
}
265
160
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
-
283
161
long getBuildDateMillis () throws IOException {
284
162
if (buildDate .get () == null ) {
285
163
synchronized (buildDate ) {
0 commit comments