Exception handling optimization

This commit is contained in:
leolin49 2023-02-01 00:37:29 +08:00
parent 187ed333dd
commit 4be7192c79
1 changed files with 13 additions and 21 deletions

View File

@ -114,7 +114,7 @@ class Maker:
# Write header buffer to file # Write header buffer to file
self.dst_handle.write(header) self.dst_handle.write(header)
def load_segments(self) -> list: def load_segments(self):
""" """
Load the segments [start ip|end ip|region] from source ip text file. Load the segments [start ip|end ip|region] from source ip text file.
:return: the list of Segment :return: the list of Segment
@ -128,41 +128,37 @@ class Maker:
logging.info("load segment: `{}`".format(line)) logging.info("load segment: `{}`".format(line))
ps = line.split("|", maxsplit=2) ps = line.split("|", maxsplit=2)
if len(ps) != 3: if len(ps) != 3:
logging.error("invalid ip segment line `{}`".format(line)) raise Exception("invalid ip segment line `{}`".format(line))
return []
sip = util.check_ip(ps[0]) sip = util.check_ip(ps[0])
if sip == -1: if sip == -1:
logging.error( raise Exception(
"invalid ip address `{}` in line `{}`".format(ps[0], line) "invalid ip address `{}` in line `{}`".format(ps[0], line)
) )
return []
eip = util.check_ip(ps[1]) eip = util.check_ip(ps[1])
if eip == -1: if eip == -1:
logging.error( raise Exception(
"invalid ip address `{}` in line `{}`".format(ps[1], line) "invalid ip address `{}` in line `{}`".format(ps[1], line)
) )
return []
if sip > eip: if sip > eip:
logging.error( raise Exception(
"start ip({}) should not be greater than end ip({})".format( "start ip({}) should not be greater than end ip({})".format(
ps[0], ps[1] ps[0], ps[1]
) )
) )
return []
if len(ps[2]) < 1: if len(ps[2]) < 1:
logging.error("empty region info in segment line `{}`".format(line)) raise Exception("empty region info in segment line `{}`".format(line))
return []
segment = seg.Segment(sip=sip, eip=eip, reg=ps[2]) segment = seg.Segment(sip=sip, eip=eip, reg=ps[2])
# Check the continuity of data segment # Check the continuity of data segment
if last is not None: if last is not None:
if last.end_ip + 1 != segment.start_ip: if last.end_ip + 1 != segment.start_ip:
logging.error( raise Exception(
"discontinuous data segment: last.eip+1({})!=seg.sip({}, {})".format( "discontinuous data segment: last.eip+1({})!=seg.sip({}, {})".format(
sip, eip, ps[0] sip, eip, ps[0]
) )
) )
return []
self.segments.append(segment) self.segments.append(segment)
last = segment last = segment
logging.info( logging.info(
@ -189,8 +185,7 @@ class Maker:
Start to make the 'xdb' binary file. Start to make the 'xdb' binary file.
""" """
if len(self.segments) < 1: if len(self.segments) < 1:
logging.error("empty segment list") raise Exception("empty segment list")
return
# 1. Write all the region/data to the binary file # 1. Write all the region/data to the binary file
self.dst_handle.seek(Header_Info_Length + Vector_Index_Length, 0) self.dst_handle.seek(Header_Info_Length + Vector_Index_Length, 0)
@ -205,12 +200,11 @@ class Maker:
continue continue
region = bytes(s.region, encoding="utf-8") region = bytes(s.region, encoding="utf-8")
if len(region) > 0xFFFF: if len(region) > 0xFFFF:
logging.error( raise Exception(
"too long region info `{}`: should be less than {} bytes".format( "too long region info `{}`: should be less than {} bytes".format(
s.region, 0xFFFF s.region, 0xFFFF
) )
) )
return
# Get the first ptr of the next region # Get the first ptr of the next region
pos = self.dst_handle.seek(0, 1) pos = self.dst_handle.seek(0, 1)
logging.info("{} {} {}".format(pos, region, s.region)) logging.info("{} {} {}".format(pos, region, s.region))
@ -222,12 +216,10 @@ class Maker:
counter, start_index_ptr, end_index_ptr = 0, -1, -1 counter, start_index_ptr, end_index_ptr = 0, -1, -1
for sg in self.segments: for sg in self.segments:
if sg.region not in self.region_pool: if sg.region not in self.region_pool:
logging.error("missing ptr cache for region `{}`".format(sg.region)) raise Exception("missing ptr cache for region `{}`".format(sg.region))
return
data_len = len(bytes(sg.region, encoding="utf-8")) data_len = len(bytes(sg.region, encoding="utf-8"))
if data_len < 1: if data_len < 1:
logging.error("empty region info for segment '{}'".format(sg.region)) raise Exception("empty region info for segment '{}'".format(sg.region))
return
seg_list = sg.split() seg_list = sg.split()
logging.info( logging.info(