Merge pull request #455 from lionsoul2014/opt_missing_search_return
optimize the ptr checking and return empty string for missing data
This commit is contained in:
commit
0ad8031754
|
|
@ -196,6 +196,13 @@ XDB_PUBLIC(int) xdb_search(xdb_searcher_t *xdb, const bytes_ip_t *ip_bytes, int
|
||||||
}
|
}
|
||||||
|
|
||||||
// printf("s_ptr=%u, e_ptr=%u\n", s_ptr, e_ptr);
|
// printf("s_ptr=%u, e_ptr=%u\n", s_ptr, e_ptr);
|
||||||
|
// @Note: ptr validate, zero ptr means source data missing
|
||||||
|
// so we could just stop here and return an empty string.
|
||||||
|
if (s_ptr == 0 || e_ptr == 0) {
|
||||||
|
xdb_region_buffer_empty(region);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
// binary search to get the final region info
|
// binary search to get the final region info
|
||||||
// segment_buffer = xdb_malloc(seg_index_size);
|
// segment_buffer = xdb_malloc(seg_index_size);
|
||||||
seg_index_size = xdb->version->segment_index_size;
|
seg_index_size = xdb->version->segment_index_size;
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<id>IP2Region.Net</id>
|
<id>IP2Region.Net</id>
|
||||||
<version>3.0.1</version>
|
<version>3.0.2</version>
|
||||||
<title>IP2Region.Net</title>
|
<title>IP2Region.Net</title>
|
||||||
<authors>Alan Lee;Argo Zhang(argo@live.ca)</authors>
|
<authors>Alan Lee;Argo Zhang(argo@live.ca)</authors>
|
||||||
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
|
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
|
||||||
|
|
|
||||||
|
|
@ -75,6 +75,14 @@ public class Searcher(CachePolicy cachePolicy, string xdbPath) : ISearcher
|
||||||
var sPtr = BinaryPrimitives.ReadUInt32LittleEndian(vector.Span);
|
var sPtr = BinaryPrimitives.ReadUInt32LittleEndian(vector.Span);
|
||||||
var ePtr = BinaryPrimitives.ReadUInt32LittleEndian(vector.Span.Slice(4));
|
var ePtr = BinaryPrimitives.ReadUInt32LittleEndian(vector.Span.Slice(4));
|
||||||
|
|
||||||
|
// @Note: ptr validate, zero ptr means source data missing
|
||||||
|
// so we could just stop here and return an empty string.
|
||||||
|
if (sPtr == 0 || ePtr == 0)
|
||||||
|
{
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
var length = ipBytes.Length;
|
var length = ipBytes.Length;
|
||||||
var indexSize = length * 2 + 6;
|
var indexSize = length * 2 + 6;
|
||||||
var l = 0;
|
var l = 0;
|
||||||
|
|
|
||||||
|
|
@ -132,6 +132,11 @@ func (s *Searcher) Search(ip []byte) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// fmt.Printf("sPtr=%d, ePtr=%d\n", sPtr, ePtr)
|
// fmt.Printf("sPtr=%d, ePtr=%d\n", sPtr, ePtr)
|
||||||
|
// @Note: ptr validate, zero ptr means source data missing
|
||||||
|
// so we could just stop here and return an empty string.
|
||||||
|
if sPtr == 0 || ePtr == 0 {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
// binary search the segment index to get the region
|
// binary search the segment index to get the region
|
||||||
var bytes, dBytes = len(ip), len(ip) << 1
|
var bytes, dBytes = len(ip), len(ip) << 1
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.lionsoul</groupId>
|
<groupId>org.lionsoul</groupId>
|
||||||
<artifactId>ip2region</artifactId>
|
<artifactId>ip2region</artifactId>
|
||||||
<version>3.3.5</version>
|
<version>3.3.6</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.lionsoul</groupId>
|
<groupId>org.lionsoul</groupId>
|
||||||
<artifactId>ip2region</artifactId>
|
<artifactId>ip2region</artifactId>
|
||||||
<version>3.3.5</version>
|
<version>3.3.6</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
<groupId>org.lionsoul</groupId>
|
<groupId>org.lionsoul</groupId>
|
||||||
<artifactId>ip2region</artifactId>
|
<artifactId>ip2region</artifactId>
|
||||||
<version>3.3.5</version>
|
<version>3.3.6</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>ip2region</name>
|
<name>ip2region</name>
|
||||||
|
|
|
||||||
|
|
@ -138,6 +138,11 @@ public class Searcher {
|
||||||
}
|
}
|
||||||
|
|
||||||
// System.out.printf("sPtr: %d, ePtr: %d\n", sPtr, ePtr);
|
// System.out.printf("sPtr: %d, ePtr: %d\n", sPtr, ePtr);
|
||||||
|
// @Note: ptr validate, zero ptr means source data missing
|
||||||
|
// so we could just stop here and return an empty string.
|
||||||
|
if (sPtr == 0 || ePtr == 0) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
// binary search the segment index block to get the region info
|
// binary search the segment index block to get the region info
|
||||||
final int bytes = ip.length, dBytes = ip.length << 1;
|
final int bytes = ip.length, dBytes = ip.length << 1;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "ip2region.js",
|
"name": "ip2region.js",
|
||||||
"version": "3.1.7",
|
"version": "3.1.8",
|
||||||
"description": "official javascript binding for ip2region with both IPv4 and IPv6 supported ",
|
"description": "official javascript binding for ip2region with both IPv4 and IPv6 supported ",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
|
|
|
||||||
|
|
@ -66,6 +66,12 @@ export class Searcher {
|
||||||
}
|
}
|
||||||
|
|
||||||
// console.log(`sPtr: ${sPtr}, ePtr: ${ePtr}`);
|
// console.log(`sPtr: ${sPtr}, ePtr: ${ePtr}`);
|
||||||
|
// @Note: ptr validate, zero ptr means source data missing
|
||||||
|
// so we could just stop here and return an empty string.
|
||||||
|
if (sPtr == 0 || ePtr == 0) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
// binary search the segment index block to get the region info
|
// binary search the segment index block to get the region info
|
||||||
const bytes = ipBytes.length, dBytes = ipBytes.length << 1;
|
const bytes = ipBytes.length, dBytes = ipBytes.length << 1;
|
||||||
const indexSize = this.version.indexSize;
|
const indexSize = this.version.indexSize;
|
||||||
|
|
|
||||||
|
|
@ -129,6 +129,13 @@ function xdb:search(ip_bytes)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- print(string.format("s_ptr: %d, e_ptr: %d", s_ptr, e_ptr))
|
-- print(string.format("s_ptr: %d, e_ptr: %d", s_ptr, e_ptr))
|
||||||
|
-- @Note: ptr validate, zero ptr means source data missing
|
||||||
|
-- so we could just stop here and return an empty string.
|
||||||
|
if s_ptr == 0 or e_ptr == 0 then
|
||||||
|
return "", nil
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
-- binary search to get the data
|
-- binary search to get the data
|
||||||
local index_size, ip_sub_compare = version.index_size, version.ip_sub_compare
|
local index_size, ip_sub_compare = version.index_size, version.ip_sub_compare
|
||||||
local bytes, d_bytes = version.bytes, version.bytes << 1
|
local bytes, d_bytes = version.bytes, version.bytes << 1
|
||||||
|
|
|
||||||
|
|
@ -504,6 +504,12 @@ class Searcher {
|
||||||
}
|
}
|
||||||
|
|
||||||
// printf("sPtr: %d, ePtr: %d\n", $sPtr, $ePtr);
|
// printf("sPtr: %d, ePtr: %d\n", $sPtr, $ePtr);
|
||||||
|
// @Note: ptr validate, zero ptr means source data missing
|
||||||
|
// so we could just stop here and return an empty string.
|
||||||
|
if ($sPtr == 0 || $ePtr == 0) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
[$bytes, $dBytes] = [strlen($ipBytes), strlen($ipBytes) << 1];
|
[$bytes, $dBytes] = [strlen($ipBytes), strlen($ipBytes) << 1];
|
||||||
|
|
||||||
// binary search the segment index to get the region info
|
// binary search the segment index to get the region info
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,11 @@ class Searcher(object):
|
||||||
e_ptr = util.le_get_uint32(buff, 4)
|
e_ptr = util.le_get_uint32(buff, 4)
|
||||||
|
|
||||||
# print("s_ptr: {}, e_ptr: {}".format(s_ptr, e_ptr))
|
# print("s_ptr: {}, e_ptr: {}".format(s_ptr, e_ptr))
|
||||||
|
# @Note: ptr validate, zero ptr means source data missing
|
||||||
|
# so we could just stop here and return an empty string.
|
||||||
|
if s_ptr == 0 or e_ptr == 0:
|
||||||
|
return ""
|
||||||
|
|
||||||
# binary search the segment index block to get the region info
|
# binary search the segment index block to get the region info
|
||||||
_bytes, _d_bytes = len(ip_bytes), len(ip_bytes) << 1
|
_bytes, _d_bytes = len(ip_bytes), len(ip_bytes) << 1
|
||||||
index_size = self.version.index_size
|
index_size = self.version.index_size
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
setuptools.setup(
|
setuptools.setup(
|
||||||
name="py-ip2region",
|
name="py-ip2region",
|
||||||
version="3.0.3",
|
version="3.0.4",
|
||||||
description="ip2region official python binding with both IPv4 and IPv6 supported",
|
description="ip2region official python binding with both IPv4 and IPv6 supported",
|
||||||
long_description=open("README.md", encoding='utf-8').read(),
|
long_description=open("README.md", encoding='utf-8').read(),
|
||||||
long_description_content_type='text/markdown',
|
long_description_content_type='text/markdown',
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "ip2region"
|
name = "ip2region"
|
||||||
version = "0.2.0"
|
version = "0.2.1"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
rust-version = "1.89.0"
|
rust-version = "1.89.0"
|
||||||
description = "The rust binding for ip2region"
|
description = "The rust binding for ip2region"
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,12 @@ impl Searcher {
|
||||||
let end_ptr =
|
let end_ptr =
|
||||||
u32::from_le_bytes(vector_index[start_point + 4..start_point + 8].try_into()?) as usize;
|
u32::from_le_bytes(vector_index[start_point + 4..start_point + 8].try_into()?) as usize;
|
||||||
|
|
||||||
|
// @Note: ptr validate, zero ptr means source data missing
|
||||||
|
// so we could just stop here and return an empty string.
|
||||||
|
if start_ptr == 0 || end_ptr == 0 {
|
||||||
|
return Ok(String::new())
|
||||||
|
}
|
||||||
|
|
||||||
// Binary search the segment index to get the region
|
// Binary search the segment index to get the region
|
||||||
let segment_index_size = self.header.segment_index_size();
|
let segment_index_size = self.header.segment_index_size();
|
||||||
let ip_bytes_len = self.header.ip_bytes_len();
|
let ip_bytes_len = self.header.ip_bytes_len();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue