Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Ip2region
提交
1b48cb7c
I
Ip2region
项目概览
int
/
Ip2region
上一次同步 12 个月
通知
19
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
I
Ip2region
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
1b48cb7c
编写于
12月 05, 2022
作者:
L
Lion
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
use the final
上级
02173a2e
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
34 addition
and
25 deletion
+34
-25
maker/java/src/main/java/org/lionsoul/ip2region/xdb/Maker.java
.../java/src/main/java/org/lionsoul/ip2region/xdb/Maker.java
+27
-18
maker/java/src/main/java/org/lionsoul/ip2region/xdb/Segment.java
...ava/src/main/java/org/lionsoul/ip2region/xdb/Segment.java
+6
-6
maker/java/src/main/java/org/lionsoul/ip2region/xdb/Util.java
...r/java/src/main/java/org/lionsoul/ip2region/xdb/Util.java
+1
-1
未找到文件。
maker/java/src/main/java/org/lionsoul/ip2region/xdb/Maker.java
浏览文件 @
1b48cb7c
...
@@ -87,18 +87,27 @@ public class Maker {
...
@@ -87,18 +87,27 @@ public class Maker {
// vector index raw bytes
// vector index raw bytes
private
final
byte
[]
vectorIndex
;
private
final
byte
[]
vectorIndex
;
public
Maker
(
int
policy
,
String
src
File
,
String
dstFile
)
throws
FileNotFound
Exception
{
public
Maker
(
int
policy
,
String
src
Path
,
String
dstPath
)
throws
IO
Exception
{
this
.
srcFile
=
new
File
(
src
File
);
this
.
srcFile
=
new
File
(
src
Path
);
if
(!
this
.
srcFile
.
exists
())
{
if
(!
this
.
srcFile
.
exists
())
{
throw
new
FileNotFoundException
(
"source text file `"
+
srcFile
+
"` not found"
);
throw
new
FileNotFoundException
(
"source text file `"
+
srcPath
+
"` not found"
);
}
}
/// check and delete the target xdb file if it exists
/// final File dstFile = new File(dstPath);
/// if (dstFile.exists() && !dstFile.delete()) {
/// log.warnf("failed to delete the dest xdb file `%s`", dstPath);
/// }
this
.
bytesCharset
=
Charset
.
forName
(
"utf-8"
);
this
.
bytesCharset
=
Charset
.
forName
(
"utf-8"
);
this
.
segments
=
new
LinkedList
<
Segment
>();
this
.
segments
=
new
LinkedList
<
Segment
>();
this
.
dstHandle
=
new
RandomAccessFile
(
dst
File
,
"rw"
);
this
.
dstHandle
=
new
RandomAccessFile
(
dst
Path
,
"rw"
);
this
.
indexPolicy
=
policy
;
this
.
indexPolicy
=
policy
;
this
.
regionPool
=
new
HashMap
<
String
,
DataEntry
>();
this
.
regionPool
=
new
HashMap
<
String
,
DataEntry
>();
this
.
vectorIndex
=
new
byte
[
VectorIndexLength
];
// all filled with 0
this
.
vectorIndex
=
new
byte
[
VectorIndexLength
];
// all filled with 0
// truncate the original xdb file
this
.
dstHandle
.
setLength
(
0
);
}
}
// init the header of the target xdb binary file
// init the header of the target xdb binary file
...
@@ -122,7 +131,7 @@ public class Maker {
...
@@ -122,7 +131,7 @@ public class Maker {
// load all the segments
// load all the segments
private
void
loadSegments
()
throws
Exception
{
private
void
loadSegments
()
throws
Exception
{
log
.
infof
(
"try to load the segments ... "
);
log
.
infof
(
"try to load the segments ... "
);
long
tStart
=
System
.
currentTimeMillis
();
final
long
tStart
=
System
.
currentTimeMillis
();
Segment
last
=
null
;
Segment
last
=
null
;
String
line
;
String
line
;
...
@@ -136,8 +145,8 @@ public class Maker {
...
@@ -136,8 +145,8 @@ public class Maker {
throw
new
Exception
(
"invalid ip segment line `"
+
ps
[
0
]+
"`"
);
throw
new
Exception
(
"invalid ip segment line `"
+
ps
[
0
]+
"`"
);
}
}
long
sip
=
Util
.
checkIP
(
ps
[
0
]);
final
long
sip
=
Util
.
checkIP
(
ps
[
0
]);
long
eip
=
Util
.
checkIP
(
ps
[
1
]);
final
long
eip
=
Util
.
checkIP
(
ps
[
1
]);
if
(
sip
>
eip
)
{
if
(
sip
>
eip
)
{
br
.
close
();
br
.
close
();
throw
new
Exception
(
"start ip("
+
ps
[
0
]+
") should not be greater than end ip("
+
ps
[
1
]+
")"
);
throw
new
Exception
(
"start ip("
+
ps
[
0
]+
") should not be greater than end ip("
+
ps
[
1
]+
")"
);
...
@@ -156,7 +165,7 @@ public class Maker {
...
@@ -156,7 +165,7 @@ public class Maker {
}
}
}
}
Segment
seg
=
new
Segment
(
sip
,
eip
,
ps
[
2
]);
final
Segment
seg
=
new
Segment
(
sip
,
eip
,
ps
[
2
]);
segments
.
add
(
seg
);
segments
.
add
(
seg
);
last
=
seg
;
last
=
seg
;
}
}
...
@@ -176,10 +185,10 @@ public class Maker {
...
@@ -176,10 +185,10 @@ public class Maker {
// set the vector index info of the specified ip
// set the vector index info of the specified ip
private
void
setVectorIndex
(
long
ip
,
long
ptr
)
{
private
void
setVectorIndex
(
long
ip
,
long
ptr
)
{
int
il0
=
(
int
)
((
ip
>>
24
)
&
0xFF
);
final
int
il0
=
(
int
)
((
ip
>>
24
)
&
0xFF
);
int
il1
=
(
int
)
((
ip
>>
16
)
&
0xFF
);
final
int
il1
=
(
int
)
((
ip
>>
16
)
&
0xFF
);
int
idx
=
il0
*
VectorIndexCols
*
VectorIndexSize
+
il1
*
VectorIndexSize
;
final
int
idx
=
il0
*
VectorIndexCols
*
VectorIndexSize
+
il1
*
VectorIndexSize
;
long
sPtr
=
Util
.
getIntLong
(
vectorIndex
,
idx
);
final
long
sPtr
=
Util
.
getIntLong
(
vectorIndex
,
idx
);
if
(
sPtr
==
0
)
{
if
(
sPtr
==
0
)
{
Util
.
write
(
vectorIndex
,
idx
,
ptr
,
4
);
Util
.
write
(
vectorIndex
,
idx
,
ptr
,
4
);
Util
.
write
(
vectorIndex
,
idx
+
4
,
ptr
+
SegmentIndexSize
,
4
);
Util
.
write
(
vectorIndex
,
idx
+
4
,
ptr
+
SegmentIndexSize
,
4
);
...
@@ -200,14 +209,14 @@ public class Maker {
...
@@ -200,14 +209,14 @@ public class Maker {
log
.
infof
(
"try to write the data block ... "
);
log
.
infof
(
"try to write the data block ... "
);
for
(
Segment
seg
:
segments
)
{
for
(
Segment
seg
:
segments
)
{
log
.
infof
(
"try to write region `%s` ... "
,
seg
.
region
);
log
.
infof
(
"try to write region `%s` ... "
,
seg
.
region
);
DataEntry
e
=
regionPool
.
get
(
seg
.
region
);
final
DataEntry
e
=
regionPool
.
get
(
seg
.
region
);
if
(
e
!=
null
)
{
if
(
e
!=
null
)
{
log
.
infof
(
" --[Cached] with ptr=%d"
,
e
.
ptr
);
log
.
infof
(
" --[Cached] with ptr=%d"
,
e
.
ptr
);
continue
;
continue
;
}
}
// get the utf-8 bytes of the region info
// get the utf-8 bytes of the region info
byte
[]
regionBuff
=
seg
.
region
.
getBytes
(
bytesCharset
);
final
byte
[]
regionBuff
=
seg
.
region
.
getBytes
(
bytesCharset
);
if
(
regionBuff
.
length
<
1
)
{
if
(
regionBuff
.
length
<
1
)
{
throw
new
Exception
(
"empty region info for segment `"
+
seg
+
"`"
);
throw
new
Exception
(
"empty region info for segment `"
+
seg
+
"`"
);
}
else
if
(
regionBuff
.
length
>
0xFFFF
)
{
}
else
if
(
regionBuff
.
length
>
0xFFFF
)
{
...
@@ -215,7 +224,7 @@ public class Maker {
...
@@ -215,7 +224,7 @@ public class Maker {
}
}
// record the current ptr
// record the current ptr
long
pos
=
dstHandle
.
getFilePointer
();
final
long
pos
=
dstHandle
.
getFilePointer
();
dstHandle
.
write
(
regionBuff
);
dstHandle
.
write
(
regionBuff
);
// record the mapping
// record the mapping
...
@@ -227,7 +236,7 @@ public class Maker {
...
@@ -227,7 +236,7 @@ public class Maker {
log
.
infof
(
"try to write the segment index block ... "
);
log
.
infof
(
"try to write the segment index block ... "
);
int
counter
=
0
;
int
counter
=
0
;
long
startIndexPtr
=
-
1
,
endIndexPtr
=
-
1
;
long
startIndexPtr
=
-
1
,
endIndexPtr
=
-
1
;
byte
[]
indexBuff
=
new
byte
[
SegmentIndexSize
];
// 4 + 4 + 2 + 4
final
byte
[]
indexBuff
=
new
byte
[
SegmentIndexSize
];
// 4 + 4 + 2 + 4
for
(
Segment
seg
:
segments
)
{
for
(
Segment
seg
:
segments
)
{
// we need the region ptr
// we need the region ptr
DataEntry
e
=
regionPool
.
get
(
seg
.
region
);
DataEntry
e
=
regionPool
.
get
(
seg
.
region
);
...
@@ -282,8 +291,8 @@ public class Maker {
...
@@ -282,8 +291,8 @@ public class Maker {
}
}
private
static
class
DataEntry
{
private
static
class
DataEntry
{
long
ptr
;
final
long
ptr
;
int
length
;
// in bytes
final
int
length
;
// in bytes
DataEntry
(
int
length
,
long
ptr
)
{
DataEntry
(
int
length
,
long
ptr
)
{
this
.
length
=
length
;
this
.
length
=
length
;
...
...
maker/java/src/main/java/org/lionsoul/ip2region/xdb/Segment.java
浏览文件 @
1b48cb7c
...
@@ -17,7 +17,7 @@ public class Segment {
...
@@ -17,7 +17,7 @@ public class Segment {
// parser the Segment from an input string
// parser the Segment from an input string
public
static
Segment
parse
(
String
input
)
throws
Exception
{
public
static
Segment
parse
(
String
input
)
throws
Exception
{
String
[]
ps
=
input
.
trim
().
split
(
"\\|"
,
3
);
final
String
[]
ps
=
input
.
trim
().
split
(
"\\|"
,
3
);
if
(
ps
.
length
!=
3
)
{
if
(
ps
.
length
!=
3
)
{
throw
new
Exception
(
"invalid ip segment `"
+
input
+
"`"
);
throw
new
Exception
(
"invalid ip segment `"
+
input
+
"`"
);
}
}
...
@@ -39,8 +39,8 @@ public class Segment {
...
@@ -39,8 +39,8 @@ public class Segment {
// split the current segment for vector index
// split the current segment for vector index
public
List
<
Segment
>
split
()
{
public
List
<
Segment
>
split
()
{
long
sByte1
=
(
int
)
((
startIP
>>
24
)
&
0xFF
);
final
long
sByte1
=
(
int
)
((
startIP
>>
24
)
&
0xFF
);
long
eByte1
=
(
int
)
((
endIP
>>
24
)
&
0xFF
);
final
long
eByte1
=
(
int
)
((
endIP
>>
24
)
&
0xFF
);
long
nSip
=
startIP
;
long
nSip
=
startIP
;
final
List
<
Segment
>
tList
=
new
ArrayList
<
Segment
>();
final
List
<
Segment
>
tList
=
new
ArrayList
<
Segment
>();
for
(
long
i
=
sByte1
;
i
<=
eByte1
;
i
++)
{
for
(
long
i
=
sByte1
;
i
<=
eByte1
;
i
++)
{
...
@@ -59,10 +59,10 @@ public class Segment {
...
@@ -59,10 +59,10 @@ public class Segment {
// 2, split the segments with the second byte
// 2, split the segments with the second byte
final
List
<
Segment
>
segList
=
new
ArrayList
<
Segment
>();
final
List
<
Segment
>
segList
=
new
ArrayList
<
Segment
>();
for
(
Segment
seg
:
tList
)
{
for
(
Segment
seg
:
tList
)
{
long
base
=
seg
.
startIP
&
0xFF000000
;
final
long
base
=
seg
.
startIP
&
0xFF000000
;
final
long
sb2
=
(
seg
.
startIP
>>
16
)
&
0xFF
;
final
long
eb2
=
(
seg
.
endIP
>>
16
)
&
0xFF
;
long
tSip
=
seg
.
startIP
;
long
tSip
=
seg
.
startIP
;
long
sb2
=
(
seg
.
startIP
>>
16
)
&
0xFF
;
long
eb2
=
(
seg
.
endIP
>>
16
)
&
0xFF
;
for
(
long
i
=
sb2
;
i
<=
eb2
;
i
++)
{
for
(
long
i
=
sb2
;
i
<=
eb2
;
i
++)
{
long
sip
=
base
|
(
i
<<
16
)
|
(
tSip
&
0xFFFF
);
long
sip
=
base
|
(
i
<<
16
)
|
(
tSip
&
0xFFFF
);
long
eip
=
base
|
(
i
<<
16
)
|
0xFFFF
;
long
eip
=
base
|
(
i
<<
16
)
|
0xFFFF
;
...
...
maker/java/src/main/java/org/lionsoul/ip2region/xdb/Util.java
浏览文件 @
1b48cb7c
...
@@ -51,7 +51,7 @@ public class Util
...
@@ -51,7 +51,7 @@ public class Util
/* check the specified ip address */
/* check the specified ip address */
public
static
long
checkIP
(
String
ip
)
throws
Exception
{
public
static
long
checkIP
(
String
ip
)
throws
Exception
{
String
[]
ps
=
ip
.
split
(
"\\."
);
final
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
+
"`"
);
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录