call the ip_compare args func

This commit is contained in:
lionsoul2014 2025-10-30 22:23:57 +08:00
parent 9e2ca6bd98
commit 42b9cebb9d
2 changed files with 17 additions and 14 deletions

View File

@ -95,7 +95,7 @@ class Searcher(object):
# read and return the region info # read and return the region info
return self.read(d_ptr, d_len).decode("utf-8") return self.read(d_ptr, d_len).decode("utf-8")
def read(self, offset, length): def read(self, offset: int, length: int):
# check the content buffer first # check the content buffer first
if self.c_buffer != None: if self.c_buffer != None:
return self.c_buffer[offset, offset + length] return self.c_buffer[offset, offset + length]

View File

@ -24,7 +24,7 @@ VectorIndexSize = 8
VectorIndexLength = 524288 VectorIndexLength = 524288
class Header(object): class Header(object):
def __init__(self, buff): def __init__(self, buff: bytes):
self.version = le_get_uint16(buff, 0) self.version = le_get_uint16(buff, 0)
self.indexPolicy = le_get_uint16(buff, 2) self.indexPolicy = le_get_uint16(buff, 2)
self.createdAt = le_get_uint32(buff, 4) self.createdAt = le_get_uint32(buff, 4)
@ -96,18 +96,18 @@ def ip_sub_compare(ip1: bytes, buff: bytes, offset: int):
# ip version class and functions # ip version class and functions
class Version(object): class Version(object):
def __init__(self, id, name, byte_num, index_size, ip_compare_func: Callable[[bytes, bytes, int], int]): def __init__(self, id: int, name: str, byte_num: int, index_size: int, ip_compare: Callable[[bytes, bytes, int], int]):
self.id = id self.id = id
self.name = name self.name = name
self.byte_num = byte_num self.byte_num = byte_num
self.index_size = index_size self.index_size = index_size
self.ip_compare_func = ip_compare_func self.ip_compare = ip_compare
def ip_compare(self, ip1: bytes, ip2: bytes): def ip_compare(self, ip1: bytes, ip2: bytes):
return ip_sub_compare(ip1, ip2, 0) return self.ip_compare(ip1, ip2, 0)
def ip_sub_compare(self, ip1: bytes, buff: bytes, offset: int): def ip_sub_compare(self, ip1: bytes, buff: bytes, offset: int):
return ip_sub_compare(ip1, buff, offset) return self.ip_compare(ip1, buff, offset)
def __str__(self): def __str__(self):
return '{{"id": {}, "name": "{}", "bytes": {}, "index_size": {}}}'.format( return '{{"id": {}, "name": "{}", "bytes": {}, "index_size": {}}}'.format(
@ -131,6 +131,9 @@ def _v4_sub_compare(ip1: bytes, buff: bytes, offset: int):
if i1 > i2: if i1 > i2:
return 1 return 1
# increase the j
j = j - 1
return 0 return 0
@ -141,7 +144,7 @@ IPv4 = Version(XdbIPv4Id, "IPv4", 4, 14, _v4_sub_compare)
# 38 = 16 + 16 + 2 + 4 # 38 = 16 + 16 + 2 + 4
IPv6 = Version(XdbIPv6Id, "IPv6", 16, 38, ip_sub_compare) IPv6 = Version(XdbIPv6Id, "IPv6", 16, 38, ip_sub_compare)
def version_from_name(name): def version_from_name(name: str):
u_name = name.upper() u_name = name.upper()
if u_name == "IPV4" or u_name == "V4": if u_name == "IPV4" or u_name == "V4":
return IPv4 return IPv4
@ -150,7 +153,7 @@ def version_from_name(name):
else: else:
return None return None
def version_from_header(header): def version_from_header(header: bytes):
# old xdb 2.0 with IPv4 supports ONLY # old xdb 2.0 with IPv4 supports ONLY
if header.version < XdbStructure30: if header.version < XdbStructure30:
return IPv4 return IPv4
@ -168,7 +171,7 @@ def version_from_header(header):
# --- # ---
# buffer decode functions # buffer decode functions
def le_get_uint32(buff, offset): def le_get_uint32(buff: bytes, offset: int):
''' '''
decode an unsinged 4-bytes int from a buffer started from offset decode an unsinged 4-bytes int from a buffer started from offset
with little byte endian with little byte endian
@ -180,7 +183,7 @@ def le_get_uint32(buff, offset):
((buff[offset+3] << 24) & 0xFF000000) ((buff[offset+3] << 24) & 0xFF000000)
) )
def le_get_uint16(buff, offset): def le_get_uint16(buff: bytes, offset: int):
''' '''
decode an unsinged 2-bytes short from a buffer started from offset decode an unsinged 2-bytes short from a buffer started from offset
with little byte endian with little byte endian
@ -201,7 +204,7 @@ def load_header(handle):
handle.seek(0) handle.seek(0)
return Header(handle.read(HeaderInfoLength)) return Header(handle.read(HeaderInfoLength))
def load_header_from_file(db_file): def load_header_from_file(db_file: str):
handle = io.open(db_file, "rb") handle = io.open(db_file, "rb")
header = load_header(handle) header = load_header(handle)
handle.close() handle.close()
@ -214,7 +217,7 @@ def load_vector_index(handle):
handle.seek(HeaderInfoLength) handle.seek(HeaderInfoLength)
return handle.read(VectorIndexLength) return handle.read(VectorIndexLength)
def load_vector_index_from_file(db_file): def load_vector_index_from_file(db_file: str):
handle = io.open(db_file, "rb") handle = io.open(db_file, "rb")
v_index = load_vector_index(handle) v_index = load_vector_index(handle)
handle.close() handle.close()
@ -227,7 +230,7 @@ def load_content(handle):
handle.seek(0) handle.seek(0)
return handle.read() return handle.read()
def load_content_from_file(db_file): def load_content_from_file(db_file: str):
handle = io.open(db_file, "rb") handle = io.open(db_file, "rb")
c_buff = load_content(handle) c_buff = load_content(handle)
handle.close() handle.close()
@ -261,7 +264,7 @@ def verify(handle):
if __file_bytes > max_file_ptr: if __file_bytes > max_file_ptr:
raise Exception("xdb file exceeds the maximum supported bytes: {}".format(max_file_ptr)) raise Exception("xdb file exceeds the maximum supported bytes: {}".format(max_file_ptr))
def verify_from_file(db_file): def verify_from_file(db_file: str):
handle = io.open(db_file, "rb") handle = io.open(db_file, "rb")
verify(handle) verify(handle)
handle.close() handle.close()