load from xdb File and xdb Path
This commit is contained in:
parent
c68cc0097c
commit
ea297041dd
|
|
@ -23,6 +23,9 @@ public class Config {
|
||||||
public static final int VIndexCache = 1;
|
public static final int VIndexCache = 1;
|
||||||
public static final int BufferCache = 2;
|
public static final int BufferCache = 2;
|
||||||
|
|
||||||
|
// alias of BufferCache but easier to understand
|
||||||
|
public static final int FullCache = 2;
|
||||||
|
|
||||||
// search cache policy
|
// search cache policy
|
||||||
public final int cachePolicy;
|
public final int cachePolicy;
|
||||||
public final Version ipVersion;
|
public final Version ipVersion;
|
||||||
|
|
|
||||||
|
|
@ -204,13 +204,17 @@ public class Searcher {
|
||||||
return new Header(buff);
|
return new Header(buff);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Header loadHeaderFromFile(String dbPath) throws IOException {
|
public static Header loadHeaderFromFile(File xdbFile) throws IOException {
|
||||||
final RandomAccessFile handle = new RandomAccessFile(dbPath, "r");
|
final RandomAccessFile handle = new RandomAccessFile(xdbFile, "r");
|
||||||
final Header header = loadHeader(handle);
|
final Header header = loadHeader(handle);
|
||||||
handle.close();
|
handle.close();
|
||||||
return header;
|
return header;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Header loadHeaderFromFile(String xdbPath) throws IOException {
|
||||||
|
return loadHeaderFromFile(new File(xdbPath));
|
||||||
|
}
|
||||||
|
|
||||||
public static byte[] loadVectorIndex(RandomAccessFile handle) throws IOException {
|
public static byte[] loadVectorIndex(RandomAccessFile handle) throws IOException {
|
||||||
handle.seek(HeaderInfoLength);
|
handle.seek(HeaderInfoLength);
|
||||||
int len = VectorIndexRows * VectorIndexCols * VectorIndexSize;
|
int len = VectorIndexRows * VectorIndexCols * VectorIndexSize;
|
||||||
|
|
@ -223,13 +227,17 @@ public class Searcher {
|
||||||
return buff;
|
return buff;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] loadVectorIndexFromFile(String dbPath) throws IOException {
|
public static byte[] loadVectorIndexFromFile(File xdbFile) throws IOException {
|
||||||
final RandomAccessFile handle = new RandomAccessFile(dbPath, "r");
|
final RandomAccessFile handle = new RandomAccessFile(xdbFile, "r");
|
||||||
final byte[] vIndex = loadVectorIndex(handle);
|
final byte[] vIndex = loadVectorIndex(handle);
|
||||||
handle.close();
|
handle.close();
|
||||||
return vIndex;
|
return vIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static byte[] loadVectorIndexFromFile(String xdbPath) throws IOException {
|
||||||
|
return loadVectorIndexFromFile(new File(xdbPath));
|
||||||
|
}
|
||||||
|
|
||||||
public static LongByteArray loadContent(RandomAccessFile handle) throws IOException {
|
public static LongByteArray loadContent(RandomAccessFile handle) throws IOException {
|
||||||
handle.seek(0);
|
handle.seek(0);
|
||||||
// check the length and do the buff load
|
// check the length and do the buff load
|
||||||
|
|
@ -249,13 +257,17 @@ public class Searcher {
|
||||||
return byteArray;
|
return byteArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static LongByteArray loadContentFromFile(String dbPath) throws IOException {
|
public static LongByteArray loadContentFromFile(File xdbFile) throws IOException {
|
||||||
final RandomAccessFile handle = new RandomAccessFile(dbPath, "r");
|
final RandomAccessFile handle = new RandomAccessFile(xdbFile, "r");
|
||||||
final LongByteArray content = loadContent(handle);
|
final LongByteArray content = loadContent(handle);
|
||||||
handle.close();
|
handle.close();
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static LongByteArray loadContentFromFile(String xdbPath) throws IOException {
|
||||||
|
return loadContentFromFile(new File(xdbPath));
|
||||||
|
}
|
||||||
|
|
||||||
// --- verify util function
|
// --- verify util function
|
||||||
|
|
||||||
// Verify if the current Searcher could be used to search the specified xdb file.
|
// Verify if the current Searcher could be used to search the specified xdb file.
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,30 @@ public class SearcherPoolTest {
|
||||||
log.debugf("v4 searcher pool closed gracefully");
|
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
|
@Test
|
||||||
public void testV6SearcherPool() throws Exception {
|
public void testV6SearcherPool() throws Exception {
|
||||||
final Config v6Config = Config.custom()
|
final Config v6Config = Config.custom()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue