add #setCacheSliceBytes and tests
This commit is contained in:
parent
7f9e6b8586
commit
1f850dcde4
|
|
@ -31,6 +31,9 @@ public class ConfigBuilder {
|
||||||
private File xdbFile = null;
|
private File xdbFile = null;
|
||||||
private InputStream xdbInputStream = null;
|
private InputStream xdbInputStream = null;
|
||||||
|
|
||||||
|
// slice bytes for in-memory xdb content
|
||||||
|
private int cacheSliceBytes = Searcher.MAX_WRITE_BYTES;
|
||||||
|
|
||||||
// searchers
|
// searchers
|
||||||
private int searchers = 20;
|
private int searchers = 20;
|
||||||
|
|
||||||
|
|
@ -60,6 +63,11 @@ public class ConfigBuilder {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ConfigBuilder setCacheSliceBytes(int cacheSliceBytes) {
|
||||||
|
this.cacheSliceBytes = cacheSliceBytes;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public ConfigBuilder setSearchers(int searchers) {
|
public ConfigBuilder setSearchers(int searchers) {
|
||||||
this.searchers = searchers;
|
this.searchers = searchers;
|
||||||
return this;
|
return this;
|
||||||
|
|
@ -74,7 +82,7 @@ public class ConfigBuilder {
|
||||||
throw new InvalidConfigException("SetXdbInputStream could ONLY be used with cachePolicy = Config.BufferCache");
|
throw new InvalidConfigException("SetXdbInputStream could ONLY be used with cachePolicy = Config.BufferCache");
|
||||||
} else {
|
} else {
|
||||||
// 1, load the content buffer
|
// 1, load the content buffer
|
||||||
final LongByteArray cBuffer = Searcher.loadContentFromInputStream(xdbInputStream);
|
final LongByteArray cBuffer = Searcher.loadContentFromInputStream(xdbInputStream, cacheSliceBytes);
|
||||||
|
|
||||||
// 2, verify the xdb from the buffer
|
// 2, verify the xdb from the buffer
|
||||||
Searcher.verify(Searcher.loadHeaderFromBuffer(cBuffer), cBuffer.length());
|
Searcher.verify(Searcher.loadHeaderFromBuffer(cBuffer), cBuffer.length());
|
||||||
|
|
@ -108,7 +116,7 @@ public class ConfigBuilder {
|
||||||
final byte[] vIndex = cachePolicy == Config.VIndexCache ? Searcher.loadVectorIndex(raf) : null;
|
final byte[] vIndex = cachePolicy == Config.VIndexCache ? Searcher.loadVectorIndex(raf) : null;
|
||||||
|
|
||||||
// 4, check and load the content buffer
|
// 4, check and load the content buffer
|
||||||
final LongByteArray cBuffer = cachePolicy == Config.BufferCache ? Searcher.loadContent(raf) : null;
|
final LongByteArray cBuffer = cachePolicy == Config.BufferCache ? Searcher.loadContent(raf, cacheSliceBytes) : null;
|
||||||
|
|
||||||
raf.close();
|
raf.close();
|
||||||
return new Config(cachePolicy, ipVersion, xdbFile, header, vIndex, cBuffer, searchers);
|
return new Config(cachePolicy, ipVersion, xdbFile, header, vIndex, cBuffer, searchers);
|
||||||
|
|
|
||||||
|
|
@ -49,39 +49,61 @@ public class ConfigTest {
|
||||||
.setXdbInputStream(new FileInputStream(getDataPath("ip2region_v4.xdb")))
|
.setXdbInputStream(new FileInputStream(getDataPath("ip2region_v4.xdb")))
|
||||||
.setSearchers(20)
|
.setSearchers(20)
|
||||||
.asV4();
|
.asV4();
|
||||||
log.debugf("builded config: %s", v4Config);
|
log.debugf("builded config: buffs.size=%d, %s", v4Config.cBuffer.size(), v4Config);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBuildV4SliceBytes() throws IOException, XdbException, InvalidConfigException {
|
||||||
|
final Config v4Config = Config.custom()
|
||||||
|
.setCachePolicy(Config.FullCache)
|
||||||
|
.setCacheSliceBytes(1024 * 1024) // 1MiB
|
||||||
|
.setXdbPath(getDataPath("ip2region_v4.xdb"))
|
||||||
|
.setSearchers(20)
|
||||||
|
.asV4();
|
||||||
|
log.debugf("builded config: buffs.size=%d, %s", v4Config.cBuffer.size(), v4Config);
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- IPv6
|
// --- IPv6
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBuildV6Config() throws IOException, XdbException, InvalidConfigException {
|
public void testBuildV6Config() throws IOException, XdbException, InvalidConfigException {
|
||||||
final Config v4Config = Config.custom()
|
final Config v6Config = Config.custom()
|
||||||
.setCachePolicy(Config.VIndexCache)
|
.setCachePolicy(Config.VIndexCache)
|
||||||
.setXdbPath(getDataPath("ip2region_v6.xdb"))
|
.setXdbPath(getDataPath("ip2region_v6.xdb"))
|
||||||
.setSearchers(20)
|
.setSearchers(20)
|
||||||
.asV6();
|
.asV6();
|
||||||
log.debugf("builded config: %s", v4Config);
|
log.debugf("builded config: %s", v6Config);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBuildV6ConfigFromFile() throws IOException, XdbException, InvalidConfigException {
|
public void testBuildV6ConfigFromFile() throws IOException, XdbException, InvalidConfigException {
|
||||||
final Config v4Config = Config.custom()
|
final Config v6Config = Config.custom()
|
||||||
.setCachePolicy(Config.BufferCache)
|
.setCachePolicy(Config.BufferCache)
|
||||||
.setXdbFile(new File(getDataPath("ip2region_v6.xdb")))
|
.setXdbFile(new File(getDataPath("ip2region_v6.xdb")))
|
||||||
.setSearchers(20)
|
.setSearchers(20)
|
||||||
.asV6();
|
.asV6();
|
||||||
log.debugf("builded config: %s", v4Config);
|
log.debugf("builded config: %s", v6Config);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBuildV6ConfigFromInputStream() throws IOException, XdbException, InvalidConfigException {
|
public void testBuildV6ConfigFromInputStream() throws IOException, XdbException, InvalidConfigException {
|
||||||
final Config v4Config = Config.custom()
|
final Config v6Config = Config.custom()
|
||||||
.setCachePolicy(Config.BufferCache)
|
.setCachePolicy(Config.BufferCache)
|
||||||
.setXdbInputStream(new FileInputStream(getDataPath("ip2region_v6.xdb")))
|
.setXdbInputStream(new FileInputStream(getDataPath("ip2region_v6.xdb")))
|
||||||
.setSearchers(20)
|
.setSearchers(20)
|
||||||
.asV6();
|
.asV6();
|
||||||
log.debugf("builded config: %s", v4Config);
|
log.debugf("builded config: buffs.size=%d, %s", v6Config.cBuffer.size(), v6Config);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBuildV6SliceBytes() throws IOException, XdbException, InvalidConfigException {
|
||||||
|
final Config v6Config = Config.custom()
|
||||||
|
.setCachePolicy(Config.BufferCache)
|
||||||
|
.setCacheSliceBytes(1024 * 1024 * 4) // 4 MiB
|
||||||
|
.setXdbInputStream(new FileInputStream(getDataPath("ip2region_v6.xdb")))
|
||||||
|
.setSearchers(20)
|
||||||
|
.asV6();
|
||||||
|
log.debugf("builded config: buffs.size=%d, %s", v6Config.cBuffer.size(), v6Config);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue