optimize the var define

This commit is contained in:
Lion 2022-06-29 18:44:50 +08:00
parent 7942816fad
commit 905590aff5
5 changed files with 16 additions and 19 deletions

View File

@ -49,7 +49,7 @@ public class SearcherTest {
### 缓存 `VectorIndex` 索引 ### 缓存 `VectorIndex` 索引
我们可以提前从 xdb 文件中加载出来 VectorIndex 数据,然后全局缓存,每次创建 Searcher 对象的时候使用全局的 VectorIndex 缓存可以减少一次固定的 IO 操作,从而加速查询,减少 IO 压力。 我们可以提前从 `xdb` 文件中加载出来 `VectorIndex` 数据,然后全局缓存,每次创建 Searcher 对象的时候使用全局的 VectorIndex 缓存可以减少一次固定的 IO 操作,从而加速查询,减少 IO 压力。
```java ```java
import org.lionsoul.ip2region.xdb.Searcher; import org.lionsoul.ip2region.xdb.Searcher;
import java.io.*; import java.io.*;
@ -94,7 +94,7 @@ public class SearcherTest {
} }
``` ```
### 缓存整个 xdb 数据 ### 缓存整个 `xdb` 数据
我们也可以预先加载整个 ip2region.xdb 的数据到内存,然后基于这个数据创建查询对象来实现完全基于文件的查询,类似之前的 memory search。 我们也可以预先加载整个 ip2region.xdb 的数据到内存,然后基于这个数据创建查询对象来实现完全基于文件的查询,类似之前的 memory search。
```java ```java
@ -176,7 +176,7 @@ ip2region>> 1.2.3.4
ip2region>> ip2region>>
``` ```
输入 ip 即可进行查询测试,也可以设置 `cache-policy` 来分别测试 file/vectorIndex/content 三种不同缓存方式的查询效果。 输入 ip 即可进行查询测试,也可以分别设置 `cache-policy` 为 file/vectorIndex/content 来测试三种不同缓存实现的查询效果。
# bench 测试 # bench 测试
@ -197,5 +197,5 @@ options:
Bench finished, {cachePolicy: vectorIndex, total: 3417955, took: 8s, cost: 2 μs/op} Bench finished, {cachePolicy: vectorIndex, total: 3417955, took: 8s, cost: 2 μs/op}
``` ```
可以通过设置 `cache-policy` 参数来分别测试 file/vectorIndex/content 三种不同缓存方式的性能 可以通过分别设置 `cache-policy` 为 file/vectorIndex/content 来测试三种不同缓存实现的效果
@Note: 注意 bench 使用的 src 文件要是生成对应 xdb 文件相同的源文件。 @Note: 注意 bench 使用的 src 文件要是生成对应 xdb 文件相同的源文件。

View File

@ -4,7 +4,7 @@
<groupId>org.lionsoul</groupId> <groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId> <artifactId>ip2region</artifactId>
<version>2.6.1</version> <version>2.6.2</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>ip2region</name> <name>ip2region</name>

View File

@ -156,7 +156,7 @@ public class SearchTest {
long sip; long sip;
try { try {
sip = Searcher.checkIpAddr(ps[0]); sip = Searcher.checkIP(ps[0]);
} catch (Exception e) { } catch (Exception e) {
System.out.printf("check start ip `%s`: %s\n", ps[0], e); System.out.printf("check start ip `%s`: %s\n", ps[0], e);
return; return;
@ -164,7 +164,7 @@ public class SearchTest {
long eip; long eip;
try { try {
eip = Searcher.checkIpAddr(ps[1]); eip = Searcher.checkIP(ps[1]);
} catch (Exception e) { } catch (Exception e) {
System.out.printf("check end ip `%s`: %s\n", ps[1], e); System.out.printf("check end ip `%s`: %s\n", ps[1], e);
return; return;

View File

@ -8,15 +8,13 @@ package org.lionsoul.ip2region;
import org.lionsoul.ip2region.xdb.Searcher; import org.lionsoul.ip2region.xdb.Searcher;
import java.io.IOException;
public class UtilTest { public class UtilTest {
public static void testIP2Long() { public static void testIP2Long() {
String ip = "1.2.3.4"; String ip = "1.2.3.4";
long ipAddr = 0; long ipAddr = 0;
try { try {
ipAddr = Searcher.checkIpAddr(ip); ipAddr = Searcher.checkIP(ip);
} catch (Exception e) { } catch (Exception e) {
System.out.printf("failed to check ip: %s\n", e); System.out.printf("failed to check ip: %s\n", e);
return; return;

View File

@ -11,7 +11,6 @@ package org.lionsoul.ip2region.xdb;
import java.io.IOException; import java.io.IOException;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.util.Arrays;
public class Searcher { public class Searcher {
// constant defined copied from the xdb maker // constant defined copied from the xdb maker
@ -72,9 +71,9 @@ public class Searcher {
return ioCount; return ioCount;
} }
public String searchByStr(String ip) throws Exception { public String searchByStr(String ipStr) throws Exception {
long ipAddr = checkIpAddr(ip); long ip = checkIP(ipStr);
return search(ipAddr); return search(ip);
} }
public String search(long ip) throws IOException { public String search(long ip) throws IOException {
@ -94,7 +93,7 @@ public class Searcher {
sPtr = getInt(contentBuff, HeaderInfoLength + idx); sPtr = getInt(contentBuff, HeaderInfoLength + idx);
ePtr = getInt(contentBuff, HeaderInfoLength + idx + 4); ePtr = getInt(contentBuff, HeaderInfoLength + idx + 4);
} else { } else {
final byte[] buff = new byte[8]; final byte[] buff = new byte[VectorIndexSize];
read(HeaderInfoLength + idx, buff); read(HeaderInfoLength + idx, buff);
sPtr = getInt(buff, 0); sPtr = getInt(buff, 0);
ePtr = getInt(buff, 4); ePtr = getInt(buff, 4);
@ -245,23 +244,23 @@ public class Searcher {
public static final byte[] shiftIndex = {24, 16, 8, 0}; public static final byte[] shiftIndex = {24, 16, 8, 0};
/* check the specified ip address */ /* check the specified ip address */
public static long checkIpAddr(String ip) throws Exception { public static long checkIP(String ip) throws Exception {
String[] ps = ip.split("\\."); String[] ps = ip.split("\\.");
if (ps.length != 4) { if (ps.length != 4) {
throw new Exception("invalid ip address `" + ip + "`"); throw new Exception("invalid ip address `" + ip + "`");
} }
long ipAddr = 0; long ipDst = 0;
for (int i = 0; i < ps.length; i++) { for (int i = 0; i < ps.length; i++) {
int val = Integer.parseInt(ps[i]); int val = Integer.parseInt(ps[i]);
if (val > 255) { if (val > 255) {
throw new Exception("ip part `"+ps[i]+"` should be less then 256"); throw new Exception("ip part `"+ps[i]+"` should be less then 256");
} }
ipAddr |= ((long) val << shiftIndex[i]); ipDst |= ((long) val << shiftIndex[i]);
} }
return ipAddr & 0xFFFFFFFFL; return ipDst & 0xFFFFFFFFL;
} }
} }