load from xdb File and xdb Path

This commit is contained in:
lionsoul2014 2025-12-12 12:15:21 +08:00
parent c68cc0097c
commit ea297041dd
3 changed files with 45 additions and 6 deletions

View File

@ -23,6 +23,9 @@ public class Config {
public static final int VIndexCache = 1;
public static final int BufferCache = 2;
// alias of BufferCache but easier to understand
public static final int FullCache = 2;
// search cache policy
public final int cachePolicy;
public final Version ipVersion;

View File

@ -204,13 +204,17 @@ public class Searcher {
return new Header(buff);
}
public static Header loadHeaderFromFile(String dbPath) throws IOException {
final RandomAccessFile handle = new RandomAccessFile(dbPath, "r");
public static Header loadHeaderFromFile(File xdbFile) throws IOException {
final RandomAccessFile handle = new RandomAccessFile(xdbFile, "r");
final Header header = loadHeader(handle);
handle.close();
return header;
}
public static Header loadHeaderFromFile(String xdbPath) throws IOException {
return loadHeaderFromFile(new File(xdbPath));
}
public static byte[] loadVectorIndex(RandomAccessFile handle) throws IOException {
handle.seek(HeaderInfoLength);
int len = VectorIndexRows * VectorIndexCols * VectorIndexSize;
@ -223,13 +227,17 @@ public class Searcher {
return buff;
}
public static byte[] loadVectorIndexFromFile(String dbPath) throws IOException {
final RandomAccessFile handle = new RandomAccessFile(dbPath, "r");
public static byte[] loadVectorIndexFromFile(File xdbFile) throws IOException {
final RandomAccessFile handle = new RandomAccessFile(xdbFile, "r");
final byte[] vIndex = loadVectorIndex(handle);
handle.close();
return vIndex;
}
public static byte[] loadVectorIndexFromFile(String xdbPath) throws IOException {
return loadVectorIndexFromFile(new File(xdbPath));
}
public static LongByteArray loadContent(RandomAccessFile handle) throws IOException {
handle.seek(0);
// check the length and do the buff load
@ -249,13 +257,17 @@ public class Searcher {
return byteArray;
}
public static LongByteArray loadContentFromFile(String dbPath) throws IOException {
final RandomAccessFile handle = new RandomAccessFile(dbPath, "r");
public static LongByteArray loadContentFromFile(File xdbFile) throws IOException {
final RandomAccessFile handle = new RandomAccessFile(xdbFile, "r");
final LongByteArray content = loadContent(handle);
handle.close();
return content;
}
public static LongByteArray loadContentFromFile(String xdbPath) throws IOException {
return loadContentFromFile(new File(xdbPath));
}
// --- verify util function
// Verify if the current Searcher could be used to search the specified xdb file.

View File

@ -32,6 +32,30 @@ public class SearcherPoolTest {
log.debugf("v4 searcher pool closed gracefully");
}
@Test
public void testInMemV4SearcherPool() throws Exception {
final Config v4Config = Config.custom()
.setCachePolicy(Config.FullCache)
.setSearchers(5)
.setXdbPath(ConfigTest.getDataPath("ip2region_v4.xdb"))
.asV4();
final String ipStr = "58.250.36.41";
final SearcherPool v4Pool = SearcherPool.create(v4Config);
for (int i = 0; i < 20; i++) {
final Searcher searcher = v4Pool.borrowSearcher();
log.debugf("borrowed searcher %d: %s", i, searcher.toString());
final String region = searcher.search(ipStr);
log.debugf("search(%s)=%s", ipStr, region);
v4Pool.returnSearcher(searcher);
log.debugf("return searcher %d", i);
}
v4Pool.close();
log.debugf("v4 searcher pool closed gracefully");
}
@Test
public void testV6SearcherPool() throws Exception {
final Config v6Config = Config.custom()