add #setCacheSliceBytes and tests

This commit is contained in:
lionsoul2014 2025-12-24 13:01:52 +08:00
parent 7f9e6b8586
commit 1f850dcde4
2 changed files with 39 additions and 9 deletions

View File

@ -31,6 +31,9 @@ public class ConfigBuilder {
private File xdbFile = null;
private InputStream xdbInputStream = null;
// slice bytes for in-memory xdb content
private int cacheSliceBytes = Searcher.MAX_WRITE_BYTES;
// searchers
private int searchers = 20;
@ -60,6 +63,11 @@ public class ConfigBuilder {
return this;
}
public ConfigBuilder setCacheSliceBytes(int cacheSliceBytes) {
this.cacheSliceBytes = cacheSliceBytes;
return this;
}
public ConfigBuilder setSearchers(int searchers) {
this.searchers = searchers;
return this;
@ -74,7 +82,7 @@ public class ConfigBuilder {
throw new InvalidConfigException("SetXdbInputStream could ONLY be used with cachePolicy = Config.BufferCache");
} else {
// 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
Searcher.verify(Searcher.loadHeaderFromBuffer(cBuffer), cBuffer.length());
@ -108,7 +116,7 @@ public class ConfigBuilder {
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;
final LongByteArray cBuffer = cachePolicy == Config.BufferCache ? Searcher.loadContent(raf, cacheSliceBytes) : null;
raf.close();
return new Config(cachePolicy, ipVersion, xdbFile, header, vIndex, cBuffer, searchers);

View File

@ -49,39 +49,61 @@ public class ConfigTest {
.setXdbInputStream(new FileInputStream(getDataPath("ip2region_v4.xdb")))
.setSearchers(20)
.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
@Test
public void testBuildV6Config() throws IOException, XdbException, InvalidConfigException {
final Config v4Config = Config.custom()
final Config v6Config = Config.custom()
.setCachePolicy(Config.VIndexCache)
.setXdbPath(getDataPath("ip2region_v6.xdb"))
.setSearchers(20)
.asV6();
log.debugf("builded config: %s", v4Config);
log.debugf("builded config: %s", v6Config);
}
@Test
public void testBuildV6ConfigFromFile() throws IOException, XdbException, InvalidConfigException {
final Config v4Config = Config.custom()
final Config v6Config = Config.custom()
.setCachePolicy(Config.BufferCache)
.setXdbFile(new File(getDataPath("ip2region_v6.xdb")))
.setSearchers(20)
.asV6();
log.debugf("builded config: %s", v4Config);
log.debugf("builded config: %s", v6Config);
}
@Test
public void testBuildV6ConfigFromInputStream() throws IOException, XdbException, InvalidConfigException {
final Config v4Config = Config.custom()
final Config v6Config = Config.custom()
.setCachePolicy(Config.BufferCache)
.setXdbInputStream(new FileInputStream(getDataPath("ip2region_v6.xdb")))
.setSearchers(20)
.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);
}
}