com.maxmind.db.Reader Java Examples

The following examples show how to use com.maxmind.db.Reader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DatabaseReader.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
private DatabaseReader(Builder builder) throws IOException {
    if (builder.stream != null) {
        this.reader = new Reader(builder.stream);
    } else if (builder.database != null) {
        this.reader = new Reader(builder.database, builder.mode);
    } else {
        // This should never happen. If it does, review the Builder class
        // constructors for errors.
        throw new IllegalArgumentException(
                "Unsupported Builder configuration: expected either File or URL");
    }
    this.om = new ObjectMapper();
    this.om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
            false);
    this.om.configure(
            DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
    InjectableValues inject = new InjectableValues.Std().addValue(
            "locales", builder.locales);
    this.om.setInjectableValues(inject);
}
 
Example #2
Source File: MaxMindGeoLocationService.java    From prebid-server-java with Apache License 2.0 6 votes vote down vote up
public Future<?> setDataPath(String dataFilePath) {
    try (TarArchiveInputStream tarInput = new TarArchiveInputStream(new GZIPInputStream(
            new FileInputStream(dataFilePath)))) {

        TarArchiveEntry currentEntry;
        boolean hasDatabaseFile = false;
        while ((currentEntry = tarInput.getNextTarEntry()) != null) {
            if (currentEntry.getName().contains(DATABASE_FILE_NAME)) {
                hasDatabaseFile = true;
                break;
            }
        }
        if (!hasDatabaseFile) {
            return Future.failedFuture(String.format("Database file %s not found in %s archive", DATABASE_FILE_NAME,
                    dataFilePath));
        }

        databaseReader = new DatabaseReader.Builder(tarInput).fileMode(Reader.FileMode.MEMORY).build();
        return Future.succeededFuture();
    } catch (IOException e) {
        return Future.failedFuture(
                String.format("IO Exception occurred while trying to read an archive/db file: %s", e.getMessage()));
    }
}
 
Example #3
Source File: DatabaseReader.java    From nifi with Apache License 2.0 6 votes vote down vote up
private DatabaseReader(Builder builder) throws IOException {
    if (builder.stream != null) {
        this.reader = new Reader(builder.stream);
    } else if (builder.database != null) {
        this.reader = new Reader(builder.database, builder.mode);
    } else {
        // This should never happen. If it does, review the Builder class
        // constructors for errors.
        throw new IllegalArgumentException("Unsupported Builder configuration: expected either File or URL");
    }

    this.om = new ObjectMapper();
    this.om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    this.om.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
    InjectableValues inject = new InjectableValues.Std().addValue("locales", builder.locales);
    this.om.setInjectableValues(inject);
}
 
Example #4
Source File: DatabaseReader.java    From nifi with Apache License 2.0 6 votes vote down vote up
private DatabaseReader(final Builder builder) throws IOException {
    if (builder.stream != null) {
        this.reader = new Reader(builder.stream);
    } else if (builder.database != null) {
        this.reader = new Reader(builder.database, builder.mode);
    } else {
        // This should never happen. If it does, review the Builder class
        // constructors for errors.
        throw new IllegalArgumentException("Unsupported Builder configuration: expected either File or URL");
    }

    this.om = new ObjectMapper();
    this.om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
    this.om.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
    InjectableValues inject = new InjectableValues.Std().addValue("locales", builder.locales);
    this.om.setInjectableValues(inject);

    this.locales = builder.locales;
}
 
Example #5
Source File: Benchmark.java    From MaxMind-DB-Reader-java with Apache License 2.0 6 votes vote down vote up
private static void bench(Reader r, int count, int seed) throws IOException {
    Random random = new Random(seed);
    long startTime = System.nanoTime();
    byte[] address = new byte[4];
    for (int i = 0; i < count; i++) {
        random.nextBytes(address);
        InetAddress ip = InetAddress.getByAddress(address);
        JsonNode t = r.get(ip);
        if (TRACE) {
            if (i % 50000 == 0) {
                System.out.println(i + " " + ip);
                System.out.println(t);
            }
        }
    }
    long endTime = System.nanoTime();

    long duration = endTime - startTime;
    long qps = count * 1000000000L / duration;
    System.out.println("Requests per second: " + qps);
}
 
Example #6
Source File: MenuRetroAcademy.java    From petscii-bbs with Mozilla Public License 2.0 5 votes vote down vote up
public void init() throws IOException {
    try {
        File maxmindDb = new File(MAXMIND_DB);
        maxmindReader = new Reader(maxmindDb);
        maxmindResponse = maxmindReader.get(socket.getInetAddress());
        maxmindReader.close();

        geoData = new GeoData(
                maxmindResponse.get("city").get("names").get("en").asText(),
                maxmindResponse.get("city").get("geoname_id").asText(),
                maxmindResponse.get("country").get("names").get("en").asText(),
                maxmindResponse.get("location").get("latitude").asDouble(),
                maxmindResponse.get("location").get("longitude").asDouble(),
                maxmindResponse.get("location").get("time_zone").asText()
        );
        log("Location: " + geoData.city + ", " + geoData.country);
    } catch (Exception e) {
        maxmindResponse = null;
        geoData = null;
        log("Error retrieving GeoIP data: " + e.getClass().getName());
    }
}
 
Example #7
Source File: GeoIpProcessor.java    From sawmill with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static void loadDatabaseReader(String location) {
    try {
        databaseReader = new DatabaseReader.Builder(Resources.getResource(location).openStream())
                .fileMode(Reader.FileMode.MEMORY)
                .withCache(new CHMCache())
                .build();
    } catch (Exception e) {
        throw new SawmillException("Failed to load geoip database", e);
    }
}
 
Example #8
Source File: Benchmark.java    From MaxMind-DB-Reader-java with Apache License 2.0 5 votes vote down vote up
private static void loop(String msg, File file, int loops, NodeCache cache) throws IOException {
    System.out.println(msg);
    for (int i = 0; i < loops; i++) {
        Reader r = new Reader(file, FileMode.MEMORY_MAPPED, cache);
        bench(r, COUNT, i);
    }
    System.out.println();
}
 
Example #9
Source File: GeoIpService.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
private void startReading() throws IOException {
    databaseReader = new Reader(dataFile.toFile(), FileMode.MEMORY, new CHMCache());
    logger.info(LICENSE);

    // clear downloading flag, because we now have working reader instance
    downloading = false;
}
 
Example #10
Source File: AbstractGeoIPDissector.java    From logparser with Apache License 2.0 5 votes vote down vote up
@Override
public void prepareForRun() throws InvalidDissectorException {
    // This creates the DatabaseReader object, which should be reused across lookups.
    try {
        reader = new DatabaseReader
            .Builder(openDatabaseFile(databaseFileName))
            .fileMode(Reader.FileMode.MEMORY)
            .withCache(new CHMCache())
            .build();
    } catch (IOException e) {
        throw new InvalidDissectorException(this.getClass().getCanonicalName() + ":" + e.getMessage());
    }
}
 
Example #11
Source File: DatabaseReaderTest.java    From GeoIP2-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testMemoryModeFile() throws Exception {
    try (DatabaseReader reader = new DatabaseReader.Builder(this.geoipFile)
            .fileMode(Reader.FileMode.MEMORY).build()
    ) {
        this.testMemoryMode(reader);
    }
}
 
Example #12
Source File: DatabaseReaderTest.java    From GeoIP2-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testMemoryModeURL() throws Exception {
    try (DatabaseReader reader = new DatabaseReader.Builder(this.geoipFile)
            .fileMode(Reader.FileMode.MEMORY).build()
    ) {
        this.testMemoryMode(reader);
    }
}
 
Example #13
Source File: DatabaseReaderTest.java    From GeoIP2-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnsupportedFileMode() throws IOException {
    this.exception.expect(IllegalArgumentException.class);
    this.exception.expectMessage(containsString("Only FileMode.MEMORY"));
    try (DatabaseReader db = new DatabaseReader.Builder(this.geoipStream).fileMode(
            Reader.FileMode.MEMORY_MAPPED).build()
    ) {
    }
}
 
Example #14
Source File: GeoIPBuilder.java    From kite with Apache License 2.0 5 votes vote down vote up
public GeoIP(CommandBuilder builder, Config config, Command parent, 
                                   Command child, final MorphlineContext context) {
  
  super(builder, config, parent, child, context);      
  this.inputFieldName = getConfigs().getString(config, "inputField");
  this.databaseFile = new File(getConfigs().getString(config, "database", "GeoLite2-City.mmdb"));
  try {
    this.databaseReader = new Reader(databaseFile);
  } catch (IOException e) {
    throw new MorphlineCompilationException("Cannot read Maxmind database: " + databaseFile, config, e);
  }
  validateArguments();
}
 
Example #15
Source File: DatabaseReaderTest.java    From GeoIP2-java with Apache License 2.0 4 votes vote down vote up
@Test
public void metadata() throws IOException {
    DatabaseReader reader = new DatabaseReader.Builder(this.geoipFile)
            .fileMode(Reader.FileMode.MEMORY).build();
    assertEquals("GeoIP2-City", reader.getMetadata().getDatabaseType());
}