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 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.XdbException;
|
||||
|
||||
|
|
@ -23,55 +23,60 @@ public class Config {
|
|||
|
||||
// search cache policy
|
||||
public final int cachePolicy;
|
||||
public final Version ipVersion;
|
||||
|
||||
// xdb file path
|
||||
public final String xdbPath;
|
||||
public final Header header;
|
||||
public final Version ipVersion;
|
||||
|
||||
// searcher pool limitation
|
||||
public final int minSearchers;
|
||||
public final int maxSearchers;
|
||||
public final byte[] vIndex;
|
||||
public final LongByteArray cBuffer;
|
||||
|
||||
public final int searchers;
|
||||
|
||||
// config builder
|
||||
public static ConfigBuilder custom() {
|
||||
return new ConfigBuilder();
|
||||
}
|
||||
|
||||
public Config(int cachePolicy, Version ipVersion, String xdbPath) throws IOException, XdbException {
|
||||
this(cachePolicy, ipVersion, xdbPath, 10, 50);
|
||||
}
|
||||
|
||||
public Config(int cachePolicy, Version ipVersion, String xdbPath, int minSearchers, int maxSearchers) throws IOException, XdbException {
|
||||
protected Config(int cachePolicy, Version ipVersion, String xdbPath,
|
||||
Header header, byte[] vIndex, LongByteArray cBuffer, int searchers) throws IOException, XdbException {
|
||||
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.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)
|
||||
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.maxSearchers = maxSearchers;
|
||||
this.searchers = searchers;
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
final StringBuffer sb = new StringBuffer();
|
||||
sb.append('{');
|
||||
sb.append("cache_policy:").append(cachePolicy).append(',');
|
||||
sb.append("version:").append(ipVersion.toString()).append(',');
|
||||
sb.append("xdb_path:").append(xdbPath).append(',');
|
||||
sb.append("header:").append(header.toString()).append(',');
|
||||
sb.append("version:").append(ipVersion.toString()).append(',');
|
||||
sb.append("min_searchers:").append(minSearchers).append(',');
|
||||
sb.append("max_searchers:").append(maxSearchers);
|
||||
if (vIndex == null) {
|
||||
sb.append("v_index: null, ");
|
||||
} 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('}');
|
||||
return sb.toString();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,11 @@
|
|||
package org.lionsoul.ip2region;
|
||||
|
||||
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.XdbException;
|
||||
|
||||
|
|
@ -22,11 +26,8 @@ public class ConfigBuilder {
|
|||
// xdb file path
|
||||
private String xdbPath = null;
|
||||
|
||||
// min searchers
|
||||
private int minSearchers = 10;
|
||||
|
||||
// max searchers
|
||||
private int maxSearchers = 30;
|
||||
// searchers
|
||||
private int searchers = 20;
|
||||
|
||||
public ConfigBuilder() {}
|
||||
|
||||
|
|
@ -44,24 +45,39 @@ public class ConfigBuilder {
|
|||
return this;
|
||||
}
|
||||
|
||||
public ConfigBuilder setMinSearchers(int minSearchers) {
|
||||
this.minSearchers = minSearchers;
|
||||
public ConfigBuilder setSeachers(int searchers) {
|
||||
this.searchers = searchers;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ConfigBuilder setMaxSearchers(int maxSearchers) {
|
||||
this.maxSearchers = maxSearchers;
|
||||
return this;
|
||||
private Config build(Version ipVersion) throws IOException, XdbException {
|
||||
// load the header and the cache buffer
|
||||
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
|
||||
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
|
||||
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.lionsoul.ip2region.xdb.Log;
|
||||
import org.lionsoul.ip2region.xdb.Version;
|
||||
import org.lionsoul.ip2region.xdb.XdbException;
|
||||
|
||||
public class ConfigTest {
|
||||
|
|
@ -22,19 +21,22 @@ public class ConfigTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testConfig() throws IOException, XdbException {
|
||||
final Config config = new Config(Config.VIndexCache, Version.IPv4, getDataPath("ip2region_v4.xdb"), 5, 10);
|
||||
log.debugf("config: %s", config);
|
||||
public void testBuildV4Config() throws IOException, XdbException {
|
||||
final Config v4Config = Config.custom()
|
||||
.setCachePolicy(Config.BufferCache)
|
||||
.setXdbPath(getDataPath("ip2region_v4.xdb"))
|
||||
.setSeachers(20)
|
||||
.asV4();
|
||||
log.debugf("builded config: %s", v4Config);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildConfig() throws IOException, XdbException {
|
||||
final Config config = Config.custom()
|
||||
.setCachePolicy(Config.BufferCache)
|
||||
public void testBuildV6Config() throws IOException, XdbException {
|
||||
final Config v4Config = Config.custom()
|
||||
.setCachePolicy(Config.VIndexCache)
|
||||
.setXdbPath(getDataPath("ip2region_v6.xdb"))
|
||||
.setMinSearchers(10)
|
||||
.setMaxSearchers(30)
|
||||
.setSeachers(20)
|
||||
.asV6();
|
||||
log.debugf("builded config: %s", config);
|
||||
log.debugf("builded config: %s", v4Config);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue