use of com.maxmind.db.NodeCache in project LogHub by fbacchella.
the class Geoip2 method configure.
@Override
public boolean configure(Properties properties) {
Object datfile = properties.get("geoip2data");
if (datfile != null) {
datfilepath = Paths.get(datfile.toString());
}
if (reader == null) {
final Cache ehCache = properties.getCache(100, "Geoip2", this);
NodeCache nc = new NodeCache() {
@Override
public JsonNode get(int key, Loader loader) throws IOException {
Element cacheElement = ehCache.get(key);
if (cacheElement != null) {
return (JsonNode) cacheElement.getObjectValue();
} else {
JsonNode node = loader.load(key);
ehCache.put(new Element(key, node));
return node;
}
}
};
if (datfilepath != null) {
try {
reader = new DatabaseReader.Builder(datfilepath.toFile()).withCache(nc).build();
} catch (IOException e) {
logger.error("can't read geoip database " + datfilepath.toString());
logger.throwing(Level.DEBUG, e);
return false;
}
} else {
try {
InputStream is = properties.classloader.getResourceAsStream("GeoLite2-City.mmdb");
if (is == null) {
logger.error("Didn't find a default database");
return false;
} else {
InputStream embedded = new BufferedInputStream(is);
reader = new DatabaseReader.Builder(embedded).withCache(nc).build();
}
} catch (IOException e) {
logger.error("Didn't find a default database");
logger.throwing(Level.DEBUG, e);
return false;
}
}
}
return super.configure(properties);
}
use of com.maxmind.db.NodeCache in project elasticsearch by elastic.
the class IngestGeoIpPlugin method getProcessors.
@Override
public Map<String, Processor.Factory> getProcessors(Processor.Parameters parameters) {
if (databaseReaders != null) {
throw new IllegalStateException("getProcessors called twice for geoip plugin!!");
}
Path geoIpConfigDirectory = parameters.env.configFile().resolve("ingest-geoip");
NodeCache cache;
long cacheSize = CACHE_SIZE.get(parameters.env.settings());
if (cacheSize > 0) {
cache = new GeoIpCache(cacheSize);
} else {
cache = NoCache.getInstance();
}
try {
databaseReaders = loadDatabaseReaders(geoIpConfigDirectory, cache);
} catch (IOException e) {
throw new RuntimeException(e);
}
return Collections.singletonMap(GeoIpProcessor.TYPE, new GeoIpProcessor.Factory(databaseReaders));
}
use of com.maxmind.db.NodeCache in project elasticsearch by elastic.
the class GeoIpProcessorFactoryTests method loadDatabaseReaders.
@BeforeClass
public static void loadDatabaseReaders() throws IOException {
Path configDir = createTempDir();
Path geoIpConfigDir = configDir.resolve("ingest-geoip");
Files.createDirectories(geoIpConfigDir);
Files.copy(new ByteArrayInputStream(StreamsUtils.copyToBytesFromClasspath("/GeoLite2-City.mmdb.gz")), geoIpConfigDir.resolve("GeoLite2-City.mmdb.gz"));
Files.copy(new ByteArrayInputStream(StreamsUtils.copyToBytesFromClasspath("/GeoLite2-Country.mmdb.gz")), geoIpConfigDir.resolve("GeoLite2-Country.mmdb.gz"));
NodeCache cache = randomFrom(NoCache.getInstance(), new GeoIpCache(randomNonNegativeLong()));
databaseReaders = IngestGeoIpPlugin.loadDatabaseReaders(geoIpConfigDir, cache);
}
Aggregations