Search in sources :

Example 1 with NodeCache

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);
}
Also used : NodeCache(com.maxmind.db.NodeCache) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) Element(net.sf.ehcache.Element) DatabaseReader(com.maxmind.geoip2.DatabaseReader) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) NodeCache(com.maxmind.db.NodeCache) Cache(net.sf.ehcache.Cache)

Example 2 with NodeCache

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));
}
Also used : Path(java.nio.file.Path) NodeCache(com.maxmind.db.NodeCache) IOException(java.io.IOException)

Example 3 with NodeCache

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);
}
Also used : Path(java.nio.file.Path) NodeCache(com.maxmind.db.NodeCache) ByteArrayInputStream(java.io.ByteArrayInputStream) BeforeClass(org.junit.BeforeClass)

Aggregations

NodeCache (com.maxmind.db.NodeCache)3 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 DatabaseReader (com.maxmind.geoip2.DatabaseReader)1 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Cache (net.sf.ehcache.Cache)1 Element (net.sf.ehcache.Element)1 BeforeClass (org.junit.BeforeClass)1