コード例 #1
0
 def test_trim_tree_7(self):
     test_parent_node = OrderedNodeBase(node_name='test_parent_node')
     test_child_node_1 = OrderedNodeBase(node_name='test_child_node_1')
     test_child_node_2 = OrderedNodeBase(node_name='test_child_node_2')
     test_child_node_3 = OrderedNodeBase(node_name='test_child_node_3')
     test_child_node_4 = OrderedNodeBase(node_name='test_child_node_4')
     test_child_node_5 = OrderedNodeBase(node_name='test_child_node_5')
     test_child_node_6 = OrderedNodeBase(node_name='test_child_node_6')
     test_tree = TreeBase(root=test_parent_node)
     test_tree.add_node(parent_node=test_parent_node,
                        child_node=test_child_node_1)
     test_tree.add_node(parent_node=test_parent_node,
                        child_node=test_child_node_2)
     test_tree.add_node(parent_node=test_child_node_1,
                        child_node=test_child_node_3)
     test_tree.add_node(parent_node=test_child_node_1,
                        child_node=test_child_node_4)
     test_tree.add_node(parent_node=test_child_node_2,
                        child_node=test_child_node_5)
     test_tree.add_node(parent_node=test_child_node_2,
                        child_node=test_child_node_6)
     test_tree.trim_tree(max_capacity=5, from_right=False)
     self.assertListEqual(
         test_tree.bfs_search(), [('test_parent_node', 0),
                                  ('test_child_node_1', 1),
                                  ('test_child_node_2', 1),
                                  ('test_child_node_5', 2),
                                  ('test_child_node_6', 2)])
     self.assertEqual(test_tree.get_num_nodes(), 5)
コード例 #2
0
 def test_add_node(self):
     test_parent_node = UnorderedNodeBase(node_name='test_parent_node')
     test_child_node = UnorderedNodeBase(node_name='test_child_node')
     test_tree = TreeBase(root=test_parent_node)
     test_tree.add_node(parent_node=test_parent_node,
                        child_node=test_child_node)
     self.assertEqual(test_tree.get_num_nodes(), 2)
コード例 #3
0
 def test_get_height_1(self):
     test_parent_node = OrderedNodeBase(node_name='test_parent_node')
     test_child_node_1 = OrderedNodeBase(node_name='test_child_node_1')
     test_child_node_2 = OrderedNodeBase(node_name='test_child_node_2')
     test_tree = TreeBase(root=test_parent_node)
     test_tree.add_node(parent_node=test_parent_node,
                        child_node=test_child_node_1)
     test_tree.add_node(parent_node=test_child_node_1,
                        child_node=test_child_node_2)
     self.assertEqual(test_tree.get_height(), 3)
コード例 #4
0
 def test_find_node(self):
     test_parent_node = UnorderedNodeBase(node_name='test_parent_node')
     test_child_node = UnorderedNodeBase(node_name='test_child_node')
     test_tree = TreeBase(root=test_parent_node)
     test_tree.add_node(parent_node=test_parent_node,
                        child_node=test_child_node)
     self.assertEqual(test_tree.find_node(node_name='test_parent_node'),
                      test_parent_node)
     self.assertEqual(test_tree.find_node(node_name='test_child_node'),
                      test_child_node)
     self.assertEqual(test_tree.find_node(node_name='random_node'), None)
コード例 #5
0
 def test_get_sub_tree_size(self):
     test_parent_node = UnorderedNodeBase(node_name='test_parent_node')
     test_child_node_1 = UnorderedNodeBase(node_name='test_child_node_1')
     test_child_node_2 = UnorderedNodeBase(node_name='test_child_node_2')
     test_tree = TreeBase(root=test_parent_node)
     test_tree.add_node(parent_node=test_parent_node,
                        child_node=test_child_node_1)
     test_tree.add_node(parent_node=test_child_node_1,
                        child_node=test_child_node_2)
     self.assertEqual(
         test_tree.get_num_nodes_subtree(node=test_parent_node), 3)
     self.assertEqual(
         test_tree.get_num_nodes_subtree(node=test_child_node_1), 2)
コード例 #6
0
 def test_trim_tree_2(self):
     test_parent_node = OrderedNodeBase(node_name='test_parent_node')
     test_child_node_1 = OrderedNodeBase(node_name='test_child_node_1')
     test_child_node_2 = OrderedNodeBase(node_name='test_child_node_2')
     test_tree = TreeBase(root=test_parent_node)
     test_tree.add_node(parent_node=test_parent_node,
                        child_node=test_child_node_1)
     test_tree.add_node(parent_node=test_child_node_1,
                        child_node=test_child_node_2)
     test_tree.trim_tree(max_capacity=2)
     self.assertListEqual(test_tree.bfs_search(),
                          [('test_parent_node', 0),
                           ('test_child_node_1', 1)])
     self.assertEqual(test_tree.get_num_nodes(), 2)
コード例 #7
0
 def test_get_leaves(self):
     test_parent_node = OrderedNodeBase(node_name='test_parent_node')
     test_child_node_1 = OrderedNodeBase(node_name='test_child_node_1')
     test_child_node_2 = OrderedNodeBase(node_name='test_child_node_2')
     test_tree = TreeBase(root=test_parent_node)
     test_tree.add_node(parent_node=test_parent_node,
                        child_node=test_child_node_1)
     test_tree.add_node(parent_node=test_child_node_1,
                        child_node=test_child_node_2)
     self.assertListEqual(test_tree.get_leaves(), ['test_child_node_2'])
     test_child_node_3 = OrderedNodeBase(node_name='test_child_node_3')
     test_tree.add_node(parent_node=test_parent_node,
                        child_node=test_child_node_3)
     self.assertListEqual(test_tree.get_leaves(),
                          ['test_child_node_3', 'test_child_node_2'])
コード例 #8
0
 def test_bfs_search(self):
     test_parent_node = OrderedNodeBase(node_name='test_parent_node')
     test_child_node_1 = OrderedNodeBase(node_name='test_child_node_1')
     test_child_node_2 = OrderedNodeBase(node_name='test_child_node_2')
     test_tree = TreeBase(root=test_parent_node)
     test_tree.add_node(parent_node=test_parent_node,
                        child_node=test_child_node_1)
     test_tree.add_node(parent_node=test_parent_node,
                        child_node=test_child_node_2)
     self.assertListEqual(test_tree.bfs_search(max_num_node=-1),
                          [('test_parent_node', 0),
                           ('test_child_node_1', 1),
                           ('test_child_node_2', 1)])
     self.assertListEqual(test_tree.bfs_search(max_num_node=2),
                          [('test_parent_node', 0),
                           ('test_child_node_1', 1)])
コード例 #9
0
 def test_dfs_search(self):
     test_parent_node = OrderedNodeBase(node_name='test_parent_node')
     test_child_node_1 = OrderedNodeBase(node_name='test_child_node_1')
     test_child_node_2 = OrderedNodeBase(node_name='test_child_node_2')
     test_tree = TreeBase(root=test_parent_node)
     test_tree.add_node(parent_node=test_parent_node,
                        child_node=test_child_node_1)
     test_tree.add_node(parent_node=test_child_node_1,
                        child_node=test_child_node_2)
     self.assertListEqual(test_tree.dfs_search(max_num_node=-1),
                          [('test_parent_node', 0),
                           ('test_child_node_1', 1),
                           ('test_child_node_2', 2)])
     test_child_node_3 = OrderedNodeBase(node_name='test_child_node_3')
     test_tree.add_node(parent_node=test_parent_node,
                        child_node=test_child_node_3)
     self.assertListEqual(test_tree.dfs_search(max_num_node=-1),
                          [('test_parent_node', 0),
                           ('test_child_node_3', 1),
                           ('test_child_node_1', 1),
                           ('test_child_node_2', 2)])
     test_child_node_4 = OrderedNodeBase(node_name='test_child_node_4')
     test_tree.add_node(parent_node=test_child_node_3,
                        child_node=test_child_node_4)
     self.assertListEqual(
         test_tree.dfs_search(max_num_node=-1), [('test_parent_node', 0),
                                                 ('test_child_node_3', 1),
                                                 ('test_child_node_4', 2),
                                                 ('test_child_node_1', 1),
                                                 ('test_child_node_2', 2)])
     test_child_node_5 = OrderedNodeBase(node_name='test_child_node_5')
     test_tree.add_node(parent_node=test_child_node_3,
                        child_node=test_child_node_5)
     self.assertListEqual(
         test_tree.dfs_search(max_num_node=-1), [('test_parent_node', 0),
                                                 ('test_child_node_3', 1),
                                                 ('test_child_node_5', 2),
                                                 ('test_child_node_4', 2),
                                                 ('test_child_node_1', 1),
                                                 ('test_child_node_2', 2)])
コード例 #10
0
 def test_get_height_2(self):
     test_parent_node = OrderedNodeBase(node_name='test_parent_node')
     test_child_node_1 = OrderedNodeBase(node_name='test_child_node_1')
     test_child_node_2 = OrderedNodeBase(node_name='test_child_node_2')
     test_child_node_3 = OrderedNodeBase(node_name='test_child_node_3')
     test_child_node_4 = OrderedNodeBase(node_name='test_child_node_4')
     test_child_node_5 = OrderedNodeBase(node_name='test_child_node_5')
     test_child_node_6 = OrderedNodeBase(node_name='test_child_node_6')
     test_tree = TreeBase(root=test_parent_node)
     test_tree.add_node(parent_node=test_parent_node,
                        child_node=test_child_node_1)
     test_tree.add_node(parent_node=test_parent_node,
                        child_node=test_child_node_2)
     test_tree.add_node(parent_node=test_child_node_1,
                        child_node=test_child_node_3)
     test_tree.add_node(parent_node=test_child_node_1,
                        child_node=test_child_node_4)
     test_tree.add_node(parent_node=test_child_node_2,
                        child_node=test_child_node_5)
     test_tree.add_node(parent_node=test_child_node_2,
                        child_node=test_child_node_6)
     self.assertEqual(test_tree.get_height(), 3)
コード例 #11
0
    def initialize_from_dir(self, dir_name, force=False):
        def _recursive_initialize_from_dir(node, max_recursion):
            self.sys_log("Starting recursion of " + str(max_recursion) + '.')
            if max_recursion == 0:
                self.sys_log("Exhausted all recursions for dir [" + dir_name +
                             '].')
                self._logger.info("Exhausted all recursions for dir [" +
                                  dir_name + '].')
                return

            node_name = node.get_node_name()
            for child_node_name in sorted(
                    FileUtil.list_dirs_in_dir(dir_name=node_name),
                    reverse=from_scratch):
                if from_scratch and self._file_tree.get_num_nodes(
                ) >= self._max_capacity > 0:
                    self.sys_log("Reach the max number of node: " +
                                 str(self._max_capacity) + '.')
                    return

                newly_added_string = child_node_name.replace(node_name,
                                                             '').replace(
                                                                 '/', '')
                if not newly_added_string.isdigit():
                    continue

                if not from_scratch and self._cmp_dir_by_timestamp(
                        dir_name_1=child_node_name,
                        dir_name_2=self._get_latest_dir_internal()):
                    continue

                child_node = self._file_tree.find_node(
                    node_name=child_node_name)
                if not child_node:
                    child_node = OrderedNodeBase(node_name=child_node_name)
                    # The nodes are ordered from large to small. So if the tree is built scratch, since the directory
                    # is listed from large to small, SortOrder.ORDER is used. If it is incremental build, since the
                    # directory is listed from small to large, SortOrder.REVERSE is used.
                    order = SortOrder.ORDER if from_scratch else SortOrder.REVERSE
                    self._file_tree.add_node(parent_node=node,
                                             child_node=child_node,
                                             order=order)
                    self.sys_log("Adding new node [" + child_node_name +
                                 node.get_node_name() + '].')
                    self._logger.info("Adding new node [" + child_node_name +
                                      "] to parent node [" +
                                      node.get_node_name() + '].')

                    if not from_scratch:
                        self._file_tree.trim_tree(
                            max_capacity=self._max_capacity)

                _recursive_initialize_from_dir(node=child_node,
                                               max_recursion=max_recursion - 1)

        from_scratch = False
        dir_name = FileUtil.normalize_dir_name(dir_name=dir_name)
        FileUtil.create_dir_if_not_exist(dir_name=dir_name)
        if not self._file_tree or self.is_updated() or force:
            root_node = OrderedNodeBase(node_name=FileUtil.normalize_dir_name(
                dir_name=dir_name))
            self._file_tree = TreeBase(root=root_node,
                                       max_dict_size=self._max_capacity)
            from_scratch = True

        _recursive_initialize_from_dir(
            node=self._file_tree.get_root_node(),
            max_recursion=self.PARTITIONER_TYPE_TO_HEIGHT_MAP[
                self.PARTITIONER_TYPE])
コード例 #12
0
 def test_get_root_node(self):
     test_root_node = UnorderedNodeBase(node_name='test_root_node')
     test_tree = TreeBase(root=test_root_node)
     self.assertEqual(test_tree.get_root_node(), test_root_node)