config build from xdb path and File supports
This commit is contained in:
parent
b52555c5ec
commit
c2a0a55005
|
|
@ -7,7 +7,7 @@
|
||||||
package org.lionsoul.ip2region;
|
package org.lionsoul.ip2region;
|
||||||
|
|
||||||
import org.lionsoul.ip2region.service.Config;
|
import org.lionsoul.ip2region.service.Config;
|
||||||
import org.lionsoul.ip2region.service.InvalidCachePolicyException;
|
import org.lionsoul.ip2region.service.InvalidConfigException;
|
||||||
import org.lionsoul.ip2region.service.Ip2Region;
|
import org.lionsoul.ip2region.service.Ip2Region;
|
||||||
import org.lionsoul.ip2region.xdb.InetAddressException;
|
import org.lionsoul.ip2region.xdb.InetAddressException;
|
||||||
import org.lionsoul.ip2region.xdb.XdbException;
|
import org.lionsoul.ip2region.xdb.XdbException;
|
||||||
|
|
@ -49,7 +49,7 @@ public class SearcherTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final Ip2Region createService(
|
public static final Ip2Region createService(
|
||||||
String v4XdbPath, String v4CachePolicy, String v6XdbPath, String v6CachePolicy) throws IOException, XdbException, InvalidCachePolicyException {
|
String v4XdbPath, String v4CachePolicy, String v6XdbPath, String v6CachePolicy) throws IOException, XdbException, InvalidConfigException {
|
||||||
final Config v4Config = Config.custom()
|
final Config v4Config = Config.custom()
|
||||||
.setCachePolicy(Config.cachePolicyFromName(v4CachePolicy))
|
.setCachePolicy(Config.cachePolicyFromName(v4CachePolicy))
|
||||||
.setSearchers(1)
|
.setSearchers(1)
|
||||||
|
|
@ -89,7 +89,7 @@ public class SearcherTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void searchTest(String[] args) throws IOException, XdbException, InvalidCachePolicyException, InterruptedException {
|
public static void searchTest(String[] args) throws IOException, XdbException, InterruptedException, InvalidConfigException {
|
||||||
String help = "";
|
String help = "";
|
||||||
String v4DbPath = "", v4CachePolicy = "vectorIndex";
|
String v4DbPath = "", v4CachePolicy = "vectorIndex";
|
||||||
String v6DbPath = "", v6CachePolicy = "vectorIndex";
|
String v6DbPath = "", v6CachePolicy = "vectorIndex";
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
package org.lionsoul.ip2region.service;
|
package org.lionsoul.ip2region.service;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.lionsoul.ip2region.xdb.Header;
|
import org.lionsoul.ip2region.xdb.Header;
|
||||||
|
|
@ -27,7 +28,7 @@ public class Config {
|
||||||
public final Version ipVersion;
|
public final Version ipVersion;
|
||||||
|
|
||||||
// xdb file path
|
// xdb file path
|
||||||
public final String xdbPath;
|
public final File xdbFile;
|
||||||
public final Header header;
|
public final Header header;
|
||||||
|
|
||||||
public final byte[] vIndex;
|
public final byte[] vIndex;
|
||||||
|
|
@ -40,12 +41,12 @@ public class Config {
|
||||||
return new ConfigBuilder();
|
return new ConfigBuilder();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Config(int cachePolicy, Version ipVersion, String xdbPath,
|
protected Config(int cachePolicy, Version ipVersion, File xdbFile,
|
||||||
Header header, byte[] vIndex, LongByteArray cBuffer, int searchers) throws IOException, XdbException {
|
Header header, byte[] vIndex, LongByteArray cBuffer, int searchers) throws IOException, XdbException {
|
||||||
this.cachePolicy = cachePolicy;
|
this.cachePolicy = cachePolicy;
|
||||||
this.ipVersion = ipVersion;
|
this.ipVersion = ipVersion;
|
||||||
|
|
||||||
this.xdbPath = xdbPath;
|
this.xdbFile = xdbFile;
|
||||||
this.header = header;
|
this.header = header;
|
||||||
this.vIndex = vIndex;
|
this.vIndex = vIndex;
|
||||||
this.cBuffer = cBuffer;
|
this.cBuffer = cBuffer;
|
||||||
|
|
@ -54,7 +55,7 @@ public class Config {
|
||||||
// verify the ip version (ipVersion and the version of the xdb file should be the same)
|
// verify the ip version (ipVersion and the version of the xdb file should be the same)
|
||||||
if (header.ipVersion != ipVersion.id) {
|
if (header.ipVersion != ipVersion.id) {
|
||||||
throw new XdbException("ip verison not match: xdb file "
|
throw new XdbException("ip verison not match: xdb file "
|
||||||
+ xdbPath + " (" + xVersion.name + "), as " + ipVersion.name + " expected");
|
+ xdbFile.getAbsolutePath() + " (" + xVersion.name + "), as " + ipVersion.name + " expected");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.searchers = searchers;
|
this.searchers = searchers;
|
||||||
|
|
@ -65,7 +66,7 @@ public class Config {
|
||||||
sb.append('{');
|
sb.append('{');
|
||||||
sb.append("cache_policy:").append(cachePolicy).append(',');
|
sb.append("cache_policy:").append(cachePolicy).append(',');
|
||||||
sb.append("version:").append(ipVersion.toString()).append(',');
|
sb.append("version:").append(ipVersion.toString()).append(',');
|
||||||
sb.append("xdb_path:").append(xdbPath).append(',');
|
sb.append("xdb_path:").append(xdbFile.getAbsolutePath()).append(',');
|
||||||
sb.append("header:").append(header.toString()).append(',');
|
sb.append("header:").append(header.toString()).append(',');
|
||||||
if (vIndex == null) {
|
if (vIndex == null) {
|
||||||
sb.append("v_index: null, ");
|
sb.append("v_index: null, ");
|
||||||
|
|
@ -82,7 +83,7 @@ public class Config {
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final int cachePolicyFromName(String name) throws InvalidCachePolicyException {
|
public static final int cachePolicyFromName(String name) throws InvalidConfigException {
|
||||||
final String lName = name.toLowerCase();
|
final String lName = name.toLowerCase();
|
||||||
if (lName.equals("file") || lName.equals("nocache")) {
|
if (lName.equals("file") || lName.equals("nocache")) {
|
||||||
return NoCache;
|
return NoCache;
|
||||||
|
|
@ -91,7 +92,7 @@ public class Config {
|
||||||
} else if (lName.equals("content") || lName.equals("buffercache")) {
|
} else if (lName.equals("content") || lName.equals("buffercache")) {
|
||||||
return BufferCache;
|
return BufferCache;
|
||||||
} else {
|
} else {
|
||||||
throw new InvalidCachePolicyException("invalid cache policy `" + name + "`");
|
throw new InvalidConfigException("invalid cache policy `" + name + "`");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -4,7 +4,9 @@
|
||||||
|
|
||||||
package org.lionsoul.ip2region.service;
|
package org.lionsoul.ip2region.service;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.io.RandomAccessFile;
|
import java.io.RandomAccessFile;
|
||||||
|
|
||||||
import org.lionsoul.ip2region.xdb.Header;
|
import org.lionsoul.ip2region.xdb.Header;
|
||||||
|
|
@ -23,8 +25,11 @@ public class ConfigBuilder {
|
||||||
// cache policy
|
// cache policy
|
||||||
private int cachePolicy = Config.VIndexCache;
|
private int cachePolicy = Config.VIndexCache;
|
||||||
|
|
||||||
// xdb file path
|
// xdb file path / Object / InputStream.
|
||||||
|
// Priority: InputStream -> File Object -> String path
|
||||||
private String xdbPath = null;
|
private String xdbPath = null;
|
||||||
|
private File xdbFile = null;
|
||||||
|
private InputStream xdbInputStream = null;
|
||||||
|
|
||||||
// searchers
|
// searchers
|
||||||
private int searchers = 20;
|
private int searchers = 20;
|
||||||
|
|
@ -45,14 +50,45 @@ public class ConfigBuilder {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ConfigBuilder setXdbFile(File xdbFile) {
|
||||||
|
this.xdbFile = xdbFile;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConfigBuilder setXdbInputStream(InputStream xdbInputStream) {
|
||||||
|
this.xdbInputStream = xdbInputStream;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public ConfigBuilder setSearchers(int searchers) {
|
public ConfigBuilder setSearchers(int searchers) {
|
||||||
this.searchers = searchers;
|
this.searchers = searchers;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Config build(Version ipVersion) throws IOException, XdbException {
|
private Config build(Version ipVersion) throws IOException, XdbException, InvalidConfigException {
|
||||||
|
if (xdbInputStream == null) {
|
||||||
|
// everyting is fine
|
||||||
|
} else if (cachePolicy != Config.BufferCache) {
|
||||||
|
// @Note: we can't directly rewrite the cachePolicy to Config.BufferCache.
|
||||||
|
// you must know what you are doing.
|
||||||
|
throw new InvalidConfigException("SetXdbInputStream could ONLY be used with cachePolicy = Config.BufferCache");
|
||||||
|
} else {
|
||||||
|
|
||||||
|
// return new Config(cachePolicy, ipVersion, null, header, null, cBuffer, searchers);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
// load the header and the cache buffer
|
// load the header and the cache buffer
|
||||||
final RandomAccessFile raf = new RandomAccessFile(xdbPath, "r");
|
final File xdbFile;
|
||||||
|
if (this.xdbFile != null) {
|
||||||
|
xdbFile = this.xdbFile;
|
||||||
|
} else if (this.xdbPath != null) {
|
||||||
|
xdbFile = new File(this.xdbPath);
|
||||||
|
} else {
|
||||||
|
throw new InvalidConfigException("Both xdbFile and xdbPath is null");
|
||||||
|
}
|
||||||
|
|
||||||
|
final RandomAccessFile raf = new RandomAccessFile(xdbFile, "r");
|
||||||
|
|
||||||
// 1, verify the xdb
|
// 1, verify the xdb
|
||||||
Searcher.verify(raf);
|
Searcher.verify(raf);
|
||||||
|
|
@ -67,16 +103,16 @@ public class ConfigBuilder {
|
||||||
final LongByteArray cBuffer = cachePolicy == Config.BufferCache ? Searcher.loadContent(raf) : null;
|
final LongByteArray cBuffer = cachePolicy == Config.BufferCache ? Searcher.loadContent(raf) : null;
|
||||||
|
|
||||||
raf.close();
|
raf.close();
|
||||||
return new Config(cachePolicy, ipVersion, xdbPath, header, vIndex, cBuffer, searchers);
|
return new Config(cachePolicy, ipVersion, xdbFile, header, vIndex, cBuffer, searchers);
|
||||||
}
|
}
|
||||||
|
|
||||||
// build the final #Config instance for IPv4
|
// build the final #Config instance for IPv4
|
||||||
public Config asV4() throws IOException, XdbException {
|
public Config asV4() throws IOException, XdbException, InvalidConfigException {
|
||||||
return build(Version.IPv4);
|
return build(Version.IPv4);
|
||||||
}
|
}
|
||||||
|
|
||||||
// build the final #Config instance for IPv6
|
// build the final #Config instance for IPv6
|
||||||
public Config asV6() throws IOException, XdbException {
|
public Config asV6() throws IOException, XdbException, InvalidConfigException {
|
||||||
return build(Version.IPv6);
|
return build(Version.IPv6);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,11 +0,0 @@
|
||||||
// Copyright 2022 The Ip2Region Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a Apache2.0-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package org.lionsoul.ip2region.service;
|
|
||||||
|
|
||||||
public class InvalidCachePolicyException extends Exception {
|
|
||||||
public InvalidCachePolicyException(String str) {
|
|
||||||
super(str);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
package org.lionsoul.ip2region.service;
|
||||||
|
|
||||||
|
public class InvalidConfigException extends Exception {
|
||||||
|
public InvalidConfigException(String str) {
|
||||||
|
super(str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
package org.lionsoul.ip2region.service;
|
package org.lionsoul.ip2region.service;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.lionsoul.ip2region.xdb.InetAddressException;
|
import org.lionsoul.ip2region.xdb.InetAddressException;
|
||||||
|
|
@ -37,23 +38,28 @@ public class Ip2Region {
|
||||||
return new Ip2Region(v4Config, v6Config).init();
|
return new Ip2Region(v4Config, v6Config).init();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final Ip2Region create(final String v4XdbPath, final String v6XdbPath) throws IOException, XdbException {
|
public static final Ip2Region create(final String v4XdbPath, final String v6XdbPath) throws IOException, XdbException, InvalidConfigException {
|
||||||
return new Ip2Region(v4XdbPath, v6XdbPath).init();
|
return new Ip2Region(new File(v4XdbPath), new File(v6XdbPath)).init();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final Ip2Region create(final File v4XdbFile, final File v6XdbFile) throws IOException, XdbException, InvalidConfigException {
|
||||||
|
return new Ip2Region(v4XdbFile, v6XdbFile).init();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* init the ip2reigon with two xdb file path and default cachePolicy vIndex.
|
* init the ip2reigon with two xdb file path and default cachePolicy vIndex.
|
||||||
* set it to null to disabled the search for specified version
|
* set it to null to disabled the search for specified version
|
||||||
*
|
*
|
||||||
* @param v4XdbPath
|
* @param v4XdbFile
|
||||||
* @param v6XdbPath
|
* @param v6XdbFile
|
||||||
* @throws XdbException
|
* @throws XdbException
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
|
* @throws InvalidConfigException
|
||||||
*/
|
*/
|
||||||
protected Ip2Region(String v4XdbPath, String v6XdbPath) throws IOException, XdbException {
|
protected Ip2Region(File v4XdbFile, File v6XdbFile) throws IOException, XdbException, InvalidConfigException {
|
||||||
this(
|
this(
|
||||||
v4XdbPath == null ? null : Config.custom().setXdbPath(v4XdbPath).asV4(),
|
v4XdbFile == null ? null : Config.custom().setXdbFile(v4XdbFile).asV4(),
|
||||||
v6XdbPath == null ? null : Config.custom().setXdbPath(v6XdbPath).asV6()
|
v6XdbFile == null ? null : Config.custom().setXdbFile(v6XdbFile).asV6()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -60,7 +60,7 @@ public class SearcherPool {
|
||||||
protected SearcherPool init() throws IOException {
|
protected SearcherPool init() throws IOException {
|
||||||
// create the searchers
|
// create the searchers
|
||||||
for (int i = pool.size(); i < config.searchers; i++) {
|
for (int i = pool.size(); i < config.searchers; i++) {
|
||||||
final Searcher searcher = new Searcher(config.ipVersion, config.xdbPath, config.vIndex, config.cBuffer);
|
final Searcher searcher = new Searcher(config.ipVersion, config.xdbFile, config.vIndex, config.cBuffer);
|
||||||
pool.add(searcher);
|
pool.add(searcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ public class ConfigTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBuildV4Config() throws IOException, XdbException {
|
public void testBuildV4Config() throws IOException, XdbException, InvalidConfigException {
|
||||||
final Config v4Config = Config.custom()
|
final Config v4Config = Config.custom()
|
||||||
.setCachePolicy(Config.BufferCache)
|
.setCachePolicy(Config.BufferCache)
|
||||||
.setXdbPath(getDataPath("ip2region_v4.xdb"))
|
.setXdbPath(getDataPath("ip2region_v4.xdb"))
|
||||||
|
|
@ -31,7 +31,7 @@ public class ConfigTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBuildV6Config() throws IOException, XdbException {
|
public void testBuildV6Config() throws IOException, XdbException, InvalidConfigException {
|
||||||
final Config v4Config = Config.custom()
|
final Config v4Config = Config.custom()
|
||||||
.setCachePolicy(Config.VIndexCache)
|
.setCachePolicy(Config.VIndexCache)
|
||||||
.setXdbPath(getDataPath("ip2region_v6.xdb"))
|
.setXdbPath(getDataPath("ip2region_v6.xdb"))
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ public class Ip2RegionTest {
|
||||||
private static final Log log = Log.getLogger(Ip2RegionTest.class).setLevel(Log.DEBUG);
|
private static final Log log = Log.getLogger(Ip2RegionTest.class).setLevel(Log.DEBUG);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void TestConfigCreate() throws IOException, XdbException, InetAddressException, InterruptedException {
|
public void TestConfigCreate() throws IOException, XdbException, InetAddressException, InterruptedException, InvalidConfigException {
|
||||||
final Config v4Config = Config.custom()
|
final Config v4Config = Config.custom()
|
||||||
.setCachePolicy(Config.NoCache)
|
.setCachePolicy(Config.NoCache)
|
||||||
.setSearchers(10)
|
.setSearchers(10)
|
||||||
|
|
@ -46,7 +46,7 @@ public class Ip2RegionTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void TestPathCreate() throws InetAddressException, IOException, XdbException, InterruptedException {
|
public void TestPathCreate() throws InetAddressException, IOException, XdbException, InterruptedException, InvalidConfigException {
|
||||||
byte[] v4Bytes = Util.parseIP("113.92.157.29");
|
byte[] v4Bytes = Util.parseIP("113.92.157.29");
|
||||||
byte[] v6Bytes = Util.parseIP("240e:3b7:3272:d8d0:db09:c067:8d59:539e");
|
byte[] v6Bytes = Util.parseIP("240e:3b7:3272:d8d0:db09:c067:8d59:539e");
|
||||||
final Ip2Region ip2Region = Ip2Region.create(ConfigTest.getDataPath("ip2region_v4.xdb"), ConfigTest.getDataPath("ip2region_v6.xdb"));
|
final Ip2Region ip2Region = Ip2Region.create(ConfigTest.getDataPath("ip2region_v4.xdb"), ConfigTest.getDataPath("ip2region_v6.xdb"));
|
||||||
|
|
@ -63,7 +63,7 @@ public class Ip2RegionTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void TestInMemSearch() throws IOException, XdbException, InetAddressException, InterruptedException {
|
public void TestInMemSearch() throws IOException, XdbException, InetAddressException, InterruptedException, InvalidConfigException {
|
||||||
final Config v4Config = Config.custom()
|
final Config v4Config = Config.custom()
|
||||||
.setCachePolicy(Config.BufferCache)
|
.setCachePolicy(Config.BufferCache)
|
||||||
.setXdbPath(ConfigTest.getDataPath("ip2region_v4.xdb"))
|
.setXdbPath(ConfigTest.getDataPath("ip2region_v4.xdb"))
|
||||||
|
|
@ -90,7 +90,7 @@ public class Ip2RegionTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void TestConcurrentCall() throws IOException, XdbException, InetAddressException, InterruptedException {
|
public void TestConcurrentCall() throws IOException, XdbException, InetAddressException, InterruptedException, InvalidConfigException {
|
||||||
final Config v4Config = Config.custom()
|
final Config v4Config = Config.custom()
|
||||||
.setCachePolicy(Config.VIndexCache)
|
.setCachePolicy(Config.VIndexCache)
|
||||||
.setSearchers(15)
|
.setSearchers(15)
|
||||||
|
|
@ -144,7 +144,7 @@ public class Ip2RegionTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void TestV4Only() throws IOException, XdbException, InetAddressException, InterruptedException {
|
public void TestV4Only() throws IOException, XdbException, InetAddressException, InterruptedException, InvalidConfigException {
|
||||||
final Config v4Config = Config.custom()
|
final Config v4Config = Config.custom()
|
||||||
.setCachePolicy(Config.NoCache)
|
.setCachePolicy(Config.NoCache)
|
||||||
.setXdbPath(ConfigTest.getDataPath("ip2region_v4.xdb"))
|
.setXdbPath(ConfigTest.getDataPath("ip2region_v4.xdb"))
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue