add vIndex & cBuffer fields and auto build
This commit is contained in:
parent
2c7b170637
commit
317a900077
|
|
@ -6,7 +6,7 @@ package org.lionsoul.ip2region;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.lionsoul.ip2region.xdb.Header;
|
import org.lionsoul.ip2region.xdb.Header;
|
||||||
import org.lionsoul.ip2region.xdb.Searcher;
|
import org.lionsoul.ip2region.xdb.LongByteArray;
|
||||||
import org.lionsoul.ip2region.xdb.Version;
|
import org.lionsoul.ip2region.xdb.Version;
|
||||||
import org.lionsoul.ip2region.xdb.XdbException;
|
import org.lionsoul.ip2region.xdb.XdbException;
|
||||||
|
|
||||||
|
|
@ -23,55 +23,60 @@ public class Config {
|
||||||
|
|
||||||
// search cache policy
|
// search cache policy
|
||||||
public final int cachePolicy;
|
public final int cachePolicy;
|
||||||
|
public final Version ipVersion;
|
||||||
|
|
||||||
// xdb file path
|
// xdb file path
|
||||||
public final String xdbPath;
|
public final String xdbPath;
|
||||||
public final Header header;
|
public final Header header;
|
||||||
public final Version ipVersion;
|
|
||||||
|
|
||||||
// searcher pool limitation
|
public final byte[] vIndex;
|
||||||
public final int minSearchers;
|
public final LongByteArray cBuffer;
|
||||||
public final int maxSearchers;
|
|
||||||
|
public final int searchers;
|
||||||
|
|
||||||
// config builder
|
// config builder
|
||||||
public static ConfigBuilder custom() {
|
public static ConfigBuilder custom() {
|
||||||
return new ConfigBuilder();
|
return new ConfigBuilder();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Config(int cachePolicy, Version ipVersion, String xdbPath) throws IOException, XdbException {
|
protected Config(int cachePolicy, Version ipVersion, String xdbPath,
|
||||||
this(cachePolicy, ipVersion, xdbPath, 10, 50);
|
Header header, byte[] vIndex, LongByteArray cBuffer, int searchers) throws IOException, XdbException {
|
||||||
}
|
|
||||||
|
|
||||||
public Config(int cachePolicy, Version ipVersion, String xdbPath, int minSearchers, int maxSearchers) throws IOException, XdbException {
|
|
||||||
this.cachePolicy = cachePolicy;
|
this.cachePolicy = cachePolicy;
|
||||||
this.xdbPath = xdbPath;
|
|
||||||
|
|
||||||
// load the header from the xdb path
|
|
||||||
final Header header = Searcher.loadHeaderFromFile(xdbPath);
|
|
||||||
this.header = header;
|
|
||||||
final Version xVersion = Version.fromHeader(header);
|
|
||||||
|
|
||||||
// this.ipVersion = Version.fromHeader(header);
|
|
||||||
this.ipVersion = ipVersion;
|
this.ipVersion = ipVersion;
|
||||||
|
|
||||||
|
this.xdbPath = xdbPath;
|
||||||
|
this.header = header;
|
||||||
|
this.vIndex = vIndex;
|
||||||
|
this.cBuffer = cBuffer;
|
||||||
|
|
||||||
|
final Version xVersion = Version.fromHeader(header);
|
||||||
// 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 " + xdbPath + " (" + xVersion.name + "), as " + ipVersion.name + " expected");
|
throw new XdbException("ip verison not match: xdb file "
|
||||||
|
+ xdbPath + " (" + xVersion.name + "), as " + ipVersion.name + " expected");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.minSearchers = minSearchers;
|
this.searchers = searchers;
|
||||||
this.maxSearchers = maxSearchers;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public String toString() {
|
@Override public String toString() {
|
||||||
final StringBuffer sb = new StringBuffer();
|
final StringBuffer sb = new StringBuffer();
|
||||||
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("xdb_path:").append(xdbPath).append(',');
|
sb.append("xdb_path:").append(xdbPath).append(',');
|
||||||
sb.append("header:").append(header.toString()).append(',');
|
sb.append("header:").append(header.toString()).append(',');
|
||||||
sb.append("version:").append(ipVersion.toString()).append(',');
|
if (vIndex == null) {
|
||||||
sb.append("min_searchers:").append(minSearchers).append(',');
|
sb.append("v_index: null, ");
|
||||||
sb.append("max_searchers:").append(maxSearchers);
|
} else {
|
||||||
|
sb.append("v_index: {bytes: ").append(vIndex.length).append("},");
|
||||||
|
}
|
||||||
|
if (cBuffer == null) {
|
||||||
|
sb.append("c_buffer: null, ");
|
||||||
|
} else {
|
||||||
|
sb.append("c_buffer: {bytes: ").append(cBuffer.length()).append("},");
|
||||||
|
}
|
||||||
|
sb.append("searchers:").append(searchers);
|
||||||
sb.append('}');
|
sb.append('}');
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,11 @@
|
||||||
package org.lionsoul.ip2region;
|
package org.lionsoul.ip2region;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.RandomAccessFile;
|
||||||
|
|
||||||
|
import org.lionsoul.ip2region.xdb.Header;
|
||||||
|
import org.lionsoul.ip2region.xdb.LongByteArray;
|
||||||
|
import org.lionsoul.ip2region.xdb.Searcher;
|
||||||
import org.lionsoul.ip2region.xdb.Version;
|
import org.lionsoul.ip2region.xdb.Version;
|
||||||
import org.lionsoul.ip2region.xdb.XdbException;
|
import org.lionsoul.ip2region.xdb.XdbException;
|
||||||
|
|
||||||
|
|
@ -22,11 +26,8 @@ public class ConfigBuilder {
|
||||||
// xdb file path
|
// xdb file path
|
||||||
private String xdbPath = null;
|
private String xdbPath = null;
|
||||||
|
|
||||||
// min searchers
|
// searchers
|
||||||
private int minSearchers = 10;
|
private int searchers = 20;
|
||||||
|
|
||||||
// max searchers
|
|
||||||
private int maxSearchers = 30;
|
|
||||||
|
|
||||||
public ConfigBuilder() {}
|
public ConfigBuilder() {}
|
||||||
|
|
||||||
|
|
@ -44,24 +45,39 @@ public class ConfigBuilder {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConfigBuilder setMinSearchers(int minSearchers) {
|
public ConfigBuilder setSeachers(int searchers) {
|
||||||
this.minSearchers = minSearchers;
|
this.searchers = searchers;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConfigBuilder setMaxSearchers(int maxSearchers) {
|
private Config build(Version ipVersion) throws IOException, XdbException {
|
||||||
this.maxSearchers = maxSearchers;
|
// load the header and the cache buffer
|
||||||
return this;
|
final RandomAccessFile raf = new RandomAccessFile(xdbPath, "r");
|
||||||
|
|
||||||
|
// 1, verify the xdb
|
||||||
|
Searcher.verify(raf);
|
||||||
|
|
||||||
|
// 2, load the header
|
||||||
|
final Header header = Searcher.loadHeader(raf);
|
||||||
|
|
||||||
|
// 3, check and load the vector index buffer
|
||||||
|
final byte[] vIndex = cachePolicy == Config.VIndexCache ? Searcher.loadVectorIndex(raf) : null;
|
||||||
|
|
||||||
|
// 4, check and load the content buffer
|
||||||
|
final LongByteArray cBuffer = cachePolicy == Config.BufferCache ? Searcher.loadContent(raf) : null;
|
||||||
|
|
||||||
|
raf.close();
|
||||||
|
return new Config(cachePolicy, ipVersion, xdbPath, 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 {
|
||||||
return new Config(cachePolicy, Version.IPv4, xdbPath, minSearchers, maxSearchers);
|
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 {
|
||||||
return new Config(cachePolicy, Version.IPv6, xdbPath, minSearchers, maxSearchers);
|
return build(Version.IPv6);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -4,7 +4,6 @@ import java.security.CodeSource;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.lionsoul.ip2region.xdb.Log;
|
import org.lionsoul.ip2region.xdb.Log;
|
||||||
import org.lionsoul.ip2region.xdb.Version;
|
|
||||||
import org.lionsoul.ip2region.xdb.XdbException;
|
import org.lionsoul.ip2region.xdb.XdbException;
|
||||||
|
|
||||||
public class ConfigTest {
|
public class ConfigTest {
|
||||||
|
|
@ -22,19 +21,22 @@ public class ConfigTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testConfig() throws IOException, XdbException {
|
public void testBuildV4Config() throws IOException, XdbException {
|
||||||
final Config config = new Config(Config.VIndexCache, Version.IPv4, getDataPath("ip2region_v4.xdb"), 5, 10);
|
final Config v4Config = Config.custom()
|
||||||
log.debugf("config: %s", config);
|
.setCachePolicy(Config.BufferCache)
|
||||||
|
.setXdbPath(getDataPath("ip2region_v4.xdb"))
|
||||||
|
.setSeachers(20)
|
||||||
|
.asV4();
|
||||||
|
log.debugf("builded config: %s", v4Config);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBuildConfig() throws IOException, XdbException {
|
public void testBuildV6Config() throws IOException, XdbException {
|
||||||
final Config config = Config.custom()
|
final Config v4Config = Config.custom()
|
||||||
.setCachePolicy(Config.BufferCache)
|
.setCachePolicy(Config.VIndexCache)
|
||||||
.setXdbPath(getDataPath("ip2region_v6.xdb"))
|
.setXdbPath(getDataPath("ip2region_v6.xdb"))
|
||||||
.setMinSearchers(10)
|
.setSeachers(20)
|
||||||
.setMaxSearchers(30)
|
|
||||||
.asV6();
|
.asV6();
|
||||||
log.debugf("builded config: %s", config);
|
log.debugf("builded config: %s", v4Config);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue