com.maxmind.db.Reader.FileMode Java Examples

The following examples show how to use com.maxmind.db.Reader.FileMode. 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: StatisticsLocationUtil.java    From arctic-sea with Apache License 2.0 6 votes vote down vote up
@Override
public void initDatabase(LocationDatabaseType type, String pathToDatabase) {
    Objects.requireNonNull(type);
    Objects.requireNonNull(pathToDatabase);
    LOG.info("Init {} as type {} with file {}", getClass().toString(), type.toString(), pathToDatabase);
    dbType = type;
    try {
        File f = new File(pathToDatabase);
        reader = new DatabaseReader.Builder(f).fileMode(FileMode.MEMORY_MAPPED).build();
        // mismatch
        if (!type.getGeoLite2Name().equals(reader.getMetadata().getDatabaseType())) {
            LOG.error("DatabaseType {} not match with the databasefile {}. Exiting.",
                         type.toString(), pathToDatabase);
            destroy();
        }
    } catch (Throwable e) {
        LOG.error("Couldn't initation geolocation database ", e);
        reader = null;
    }
}
 
Example #2
Source File: BufferHolder.java    From MaxMind-DB-Reader-java with Apache License 2.0 6 votes vote down vote up
BufferHolder(File database, FileMode mode) throws IOException {
    try (
            final RandomAccessFile file = new RandomAccessFile(database, "r");
            final FileChannel channel = file.getChannel()
    ) {
        if (mode == FileMode.MEMORY) {
            final ByteBuffer buf = ByteBuffer.wrap(new byte[(int) channel.size()]);
            if (channel.read(buf) != buf.capacity()) {
                throw new IOException("Unable to read "
                        + database.getName()
                        + " into memory. Unexpected end of stream.");
            }
            this.buffer = buf.asReadOnlyBuffer();
        } else {
            this.buffer = channel.map(MapMode.READ_ONLY, 0, channel.size()).asReadOnlyBuffer();
        }
    }
}
 
Example #3
Source File: DatabaseReader.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * @param val The file mode used to open the GeoIP2 database
 * @return Builder object
 * @throws java.lang.IllegalArgumentException if you initialized the Builder with a URL, which uses {@link FileMode#MEMORY}, but you provided a different FileMode to this method.
 */
public Builder fileMode(FileMode val) {
    if (this.stream != null && !FileMode.MEMORY.equals(val)) {
        throw new IllegalArgumentException(
                "Only FileMode.MEMORY is supported when using an InputStream.");
    }
    this.mode = val;
    return this;
}
 
Example #4
Source File: DatabaseReader.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * @param val The file mode used to open the GeoIP2 database
 * @return Builder object
 * @throws java.lang.IllegalArgumentException if you initialized the Builder with a URL, which uses {@link FileMode#MEMORY}, but you provided a different FileMode to this method.
 */
public Builder fileMode(FileMode val) {
    if (this.stream != null && !FileMode.MEMORY.equals(val)) {
        throw new IllegalArgumentException("Only FileMode.MEMORY is supported when using an InputStream.");
    }

    this.mode = val;
    return this;
}
 
Example #5
Source File: DatabaseReader.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * @param val The file mode used to open the GeoIP2 database
 * @return Builder object
 * @throws java.lang.IllegalArgumentException if you initialized the Builder with a URL, which uses {@link FileMode#MEMORY}, but you provided a different FileMode to this method.
 */
public Builder fileMode(FileMode val) {
    if (this.stream != null && !FileMode.MEMORY.equals(val)) {
        throw new IllegalArgumentException(
                "Only FileMode.MEMORY is supported when using an InputStream.");
    }
    this.mode = val;
    return this;
}
 
Example #6
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 #7
Source File: PointerTest.java    From MaxMind-DB-Reader-java with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("static-method")
@Test
public void testWithPointers() throws IOException {
    File file = ReaderTest.getFile("maps-with-pointers.raw");
    BufferHolder ptf = new BufferHolder(file, FileMode.MEMORY);
    Decoder decoder = new Decoder(NoCache.getInstance(), ptf.get(), 0);

    ObjectMapper om = new ObjectMapper();

    ObjectNode map = om.createObjectNode();
    map.put("long_key", "long_value1");
    assertEquals(map, decoder.decode(0));

    map = om.createObjectNode();
    map.put("long_key", "long_value2");
    assertEquals(map, decoder.decode(22));

    map = om.createObjectNode();
    map.put("long_key2", "long_value1");
    assertEquals(map, decoder.decode(37));

    map = om.createObjectNode();
    map.put("long_key2", "long_value2");
    assertEquals(map, decoder.decode(50));

    map = om.createObjectNode();
    map.put("long_key", "long_value1");
    assertEquals(map, decoder.decode(55));

    map = om.createObjectNode();
    map.put("long_key2", "long_value2");
    assertEquals(map, decoder.decode(57));
}
 
Example #8
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 #9
Source File: Benchmark.java    From GeoIP2-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 GeoIp2Exception, IOException {
    System.out.println(msg);
    for (int i = 0; i < loops; i++) {
        DatabaseReader r = new DatabaseReader.Builder(file).fileMode(FileMode.MEMORY_MAPPED).withCache(cache).build();
        bench(r, COUNT, i);
    }
    System.out.println();
}
 
Example #10
Source File: DatabaseReader.java    From GeoIP2-java with Apache License 2.0 5 votes vote down vote up
/**
 * @param val The file mode used to open the GeoIP2 database
 * @return Builder object
 * @throws java.lang.IllegalArgumentException if you initialized the Builder with a URL, which uses
 *                                            {@link FileMode#MEMORY}, but you provided a different
 *                                            FileMode to this method.
 */
public Builder fileMode(FileMode val) {
    if (this.stream != null && FileMode.MEMORY != val) {
        throw new IllegalArgumentException(
                "Only FileMode.MEMORY is supported when using an InputStream.");
    }
    this.mode = val;
    return this;
}