Skip to content
Snippets Groups Projects
Commit 8b38cb08 authored by Steven Cahail's avatar Steven Cahail
Browse files

ipc_logging: Refactor LogPage_v1.sortAndLink()

LogPage_v1.sortAndLink() has excessive cyclomatic complexity. Reduce the
complexity by refactoring sortAndLink() into several smaller functions.

This commit is to be squashed into the commit containing all other
style fixes.

Change-Id: I52ac6027e5f9d45af7d44d4b24beff7a28baea05
parent 6f7faee9
No related branches found
No related tags found
No related merge requests found
......@@ -909,30 +909,18 @@ class LogPage_v1(LogPage):
self.context.header_size = V1_PAGE_HDR_SIZE
self.page_header_size = self.context.header_size
def sortAndLink(self, lstPages, bSort):
def find_min_end_position(self, lstPages):
"""
Given a list of pages in ascending page number order,
sort them chronologically and link the pages together.
This must be called before iterating over items in a page.
Find the last page written to. Since the pages are filled in order by
page ID, the first non-full page is the last page written to.
:param lstPages: The list of pages to be sorted
:param bSort: A boolean indicating whether the logs are full or
partially full. If they are full, bSort is :const:`True`.
:param lstPages: The list of pages
:return: The sorted and linked list of pages.
**Side Effects**: If the starting page cannot be found, this
method will fail an assertion.
:return: The index of the last page written to
"""
# Sort pages chronologically by finding the first active page.
# Since the pages are filled in order by page-id, the first
# non-full page is the last page written to.
if bSort:
# Rotate to lowest non-zero end time
min_end_time = None
min_start_time = None
min_end_position = 0
min_start_position = 0
for n in range(len(lstPages)):
cur_end_time = lstPages[n].get_end_time()
if cur_end_time == 0:
......@@ -947,8 +935,22 @@ class LogPage_v1(LogPage):
min_end_time = cur_end_time
min_end_position = n
min_position = min_end_position
if lstPages[min_end_position].read_offset == 0:
return min_end_position
def find_min_position(self, lstPages, min_position):
"""
Find the first chronological page (the page with the lowest non-zero
end time.
:param lstPages: The list of pages
:param min_position: The index of the first non-full page
:return: The index of the first chronological page
"""
min_start_time = None
min_start_position = 0
if lstPages[min_position].read_offset == 0:
for n in range(len(lstPages)):
cur_start_time = lstPages[n].get_start_time()
if cur_start_time == 0:
......@@ -966,6 +968,31 @@ class LogPage_v1(LogPage):
if lstPages[min_start_position].read_offset != 0:
min_position = min_start_position
return min_position
def sortAndLink(self, lstPages, bSort):
"""
Given a list of pages in ascending page number order,
sort them chronologically and link the pages together.
This must be called before iterating over items in a page.
:param lstPages: The list of pages to be sorted
:param bSort: A boolean indicating whether the logs are full or
partially full. If they are full, bSort is :const:`True`.
:return: The sorted and linked list of pages.
**Side Effects**: If the starting page cannot be found, this
method will fail an assertion.
"""
# Sort pages chronologically by finding the first active page.
# Since the pages are filled in order by page-id, the first
# non-full page is the last page written to.
if bSort:
min_end_position = self.find_min_end_position(lstPages)
min_position = self.find_min_position(lstPages, min_end_position)
# Rotate to lowest non-zero end time
lstPages.rotate(-min_position)
lstPages = list(lstPages)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment