コード例 #1
0
ファイル: test_graph_base.py プロジェクト: kfrancischen/pslx
 def test_is_dag_2(self):
     test_node_1 = OrderedNodeBase(node_name='test_node_1')
     test_node_2 = OrderedNodeBase(node_name='test_node_2')
     test_node_3 = OrderedNodeBase(node_name='test_node_3')
     test_graph = GraphBase()
     test_graph.add_direct_edge(from_node=test_node_1, to_node=test_node_2)
     test_graph.add_direct_edge(from_node=test_node_2, to_node=test_node_3)
     test_graph.add_direct_edge(from_node=test_node_3, to_node=test_node_1)
     self.assertFalse(test_graph.is_dag())
コード例 #2
0
 def test_get_size(self):
     linked_list = LinkedListBase()
     self.assertEqual(linked_list.get_size(), 0)
     test_node_1 = OrderedNodeBase(node_name='test_node_1')
     linked_list.add_to_head(test_node_1)
     self.assertEqual(linked_list.get_size(), 1)
     test_node_2 = OrderedNodeBase(node_name='test_node_2')
     linked_list.add_to_head(test_node_2)
     self.assertEqual(linked_list.get_size(), 2)
コード例 #3
0
ファイル: test_graph_base.py プロジェクト: kfrancischen/pslx
 def test_is_connected_1(self):
     test_node_1 = OrderedNodeBase(node_name='test_node_1')
     test_node_2 = OrderedNodeBase(node_name='test_node_2')
     test_node_3 = OrderedNodeBase(node_name='test_node_3')
     test_graph = GraphBase()
     test_graph.add_direct_edge(from_node=test_node_1, to_node=test_node_2)
     test_graph.add_direct_edge(from_node=test_node_1, to_node=test_node_3)
     test_graph.add_direct_edge(from_node=test_node_2, to_node=test_node_3)
     self.assertTrue(test_graph.is_connected())
コード例 #4
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)
コード例 #5
0
    def test_ordered_node_reverse_2(self):
        test_child_node = OrderedNodeBase(node_name='child_node')
        test_parent_node_1 = OrderedNodeBase(node_name='parent_node_1')
        test_parent_node_2 = OrderedNodeBase(node_name='parent_node_2')
        test_parent_node_3 = OrderedNodeBase(node_name='parent_node_3')

        test_child_node.add_parent(parent_node=test_parent_node_1, order=SortOrder.REVERSE)
        test_child_node.add_parent(parent_node=test_parent_node_2, order=SortOrder.REVERSE)
        test_child_node.add_parent(parent_node=test_parent_node_3, order=SortOrder.REVERSE)
        self.assertListEqual(test_child_node.get_parents_names(), ['parent_node_3', 'parent_node_2', 'parent_node_1'])
コード例 #6
0
    def test_ordered_node_order(self):
        test_parent_node = OrderedNodeBase(node_name='parent_node')
        test_child_node_1 = OrderedNodeBase(node_name='child_node_1')
        test_child_node_2 = OrderedNodeBase(node_name='child_node_2')
        test_child_node_3 = OrderedNodeBase(node_name='child_node_3')

        test_parent_node.add_child(child_node=test_child_node_1, order=SortOrder.ORDER)
        test_parent_node.add_child(child_node=test_child_node_2, order=SortOrder.ORDER)
        test_parent_node.add_child(child_node=test_child_node_3, order=SortOrder.ORDER)
        self.assertListEqual(test_parent_node.get_children_names(), ['child_node_1', 'child_node_2', 'child_node_3'])
コード例 #7
0
ファイル: test_graph_base.py プロジェクト: kfrancischen/pslx
 def test_get_source_nodes(self):
     test_node_1 = OrderedNodeBase(node_name='test_node_1')
     test_node_2 = OrderedNodeBase(node_name='test_node_2')
     test_node_3 = OrderedNodeBase(node_name='test_node_3')
     test_node_4 = OrderedNodeBase(node_name='test_node_4')
     test_graph = GraphBase()
     test_graph.add_direct_edge(from_node=test_node_1, to_node=test_node_2)
     test_graph.add_direct_edge(from_node=test_node_1, to_node=test_node_3)
     test_graph.add_direct_edge(from_node=test_node_4, to_node=test_node_2)
     test_graph.add_direct_edge(from_node=test_node_4, to_node=test_node_3)
     self.assertListEqual(test_graph.get_source_nodes(), [test_node_1, test_node_4])
コード例 #8
0
ファイル: test_graph_base.py プロジェクト: kfrancischen/pslx
 def test_is_connected_3(self):
     test_node_1 = OrderedNodeBase(node_name='test_node_1')
     test_node_2 = OrderedNodeBase(node_name='test_node_2')
     test_node_3 = OrderedNodeBase(node_name='test_node_3')
     test_node_4 = OrderedNodeBase(node_name='test_node_4')
     test_node_5 = OrderedNodeBase(node_name='test_node_5')
     test_graph = GraphBase()
     test_graph.add_direct_edge(from_node=test_node_1, to_node=test_node_2)
     test_graph.add_direct_edge(from_node=test_node_3, to_node=test_node_4)
     test_graph.add_direct_edge(from_node=test_node_4, to_node=test_node_5)
     test_graph.add_direct_edge(from_node=test_node_5, to_node=test_node_3)
     self.assertFalse(test_graph.is_connected())
コード例 #9
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)
コード例 #10
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'])
コード例 #11
0
ファイル: test_graph_base.py プロジェクト: kfrancischen/pslx
    def test_topological_sort_1(self):
        test_node_1 = OrderedNodeBase(node_name='test_node_1')
        test_node_2 = OrderedNodeBase(node_name='test_node_2')
        test_node_3 = OrderedNodeBase(node_name='test_node_3')
        test_graph = GraphBase()
        test_graph.add_direct_edge(from_node=test_node_1, to_node=test_node_2)
        test_graph.add_direct_edge(from_node=test_node_1, to_node=test_node_3)

        test_graph.add_direct_edge(from_node=test_node_3, to_node=test_node_2)
        self.assertListEqual(test_graph.topological_sort(),
                             [('test_node_1', 0), ('test_node_3', 1), ('test_node_2', 2)])
        self.assertDictEqual(
            test_graph.get_node_levels(),
            {0: ['test_node_1'], 1: ['test_node_3'], 2: ['test_node_2']}
        )
コード例 #12
0
ファイル: test_graph_base.py プロジェクト: kfrancischen/pslx
 def test_topological_sort_3(self):
     test_node_1 = OrderedNodeBase(node_name='test_node_1')
     test_node_2 = OrderedNodeBase(node_name='test_node_2')
     test_node_3 = OrderedNodeBase(node_name='test_node_3')
     test_node_4 = OrderedNodeBase(node_name='test_node_4')
     test_node_5 = OrderedNodeBase(node_name='test_node_5')
     test_graph = GraphBase()
     test_graph.add_direct_edge(from_node=test_node_1, to_node=test_node_2)
     test_graph.add_direct_edge(from_node=test_node_1, to_node=test_node_3)
     test_graph.add_direct_edge(from_node=test_node_4, to_node=test_node_2)
     test_graph.add_direct_edge(from_node=test_node_4, to_node=test_node_3)
     test_graph.add_direct_edge(from_node=test_node_4, to_node=test_node_5)
     self.assertDictEqual(
         test_graph.get_node_levels(),
         {0: ['test_node_1', 'test_node_4'], 1: ['test_node_2', 'test_node_3', 'test_node_5']}
     )
コード例 #13
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)])
コード例 #14
0
ファイル: test_graph_base.py プロジェクト: kfrancischen/pslx
    def test_replace_node(self):
        test_node_1 = OrderedNodeBase(node_name='test_node_1')
        test_node_2 = OrderedNodeBase(node_name='test_node_2')
        test_node_3 = OrderedNodeBase(node_name='test_node_3')
        test_node_4 = OrderedNodeBase(node_name='test_node_4')
        test_node_5 = OrderedNodeBase(node_name='test_node_5')
        test_graph = GraphBase()
        test_graph.add_direct_edge(from_node=test_node_1, to_node=test_node_2)
        test_graph.add_direct_edge(from_node=test_node_1, to_node=test_node_3)
        test_graph.add_direct_edge(from_node=test_node_4, to_node=test_node_2)
        test_graph.add_direct_edge(from_node=test_node_4, to_node=test_node_3)

        test_graph.replace_node(old_node=test_node_4, new_node=test_node_5)
        self.assertListEqual(
            test_graph.topological_sort(),
            [('test_node_1', 0), ('test_node_5', 0), ('test_node_2', 1), ('test_node_3', 1)]
        )
コード例 #15
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)
コード例 #16
0
ファイル: partitioner_base.py プロジェクト: kfrancischen/pslx
        def _recursive_initialize_from_dir(node, max_recursion):
            self._SYS_LOGGER.info("Starting recursion of " +
                                  str(max_recursion) + '.')
            if max_recursion == 0:
                self._SYS_LOGGER.info("Exhausted all recursions for dir [" +
                                      dir_name + '].')
                self._logger.info("Exhausted all recursions for dir [" +
                                  dir_name + '].')
                return

            node_name = node.get_node_name()
            self.increment_rpc_count_by(n=1)
            child_node_names = sorted(
                FileUtil.list_dirs_in_dir(dir_name=node_name),
                reverse=from_scratch)
            for child_node_name in child_node_names:
                if from_scratch and self._file_tree.get_num_nodes(
                ) >= self._max_capacity > 0:
                    self._SYS_LOGGER.info("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_LOGGER.info("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)
コード例 #17
0
    def test_delete(self):
        linked_list = LinkedListBase()
        for i in range(10):
            test_node = OrderedNodeBase(node_name='test_node_' + str(i))
            linked_list.add_to_head(test_node)

        linked_list.delete_by_node_name(node_name='test_node_0')
        self.assertEqual(linked_list.get_size(), 9)
        linked_list.delete_by_node_name(node_name='test_node_4')
        self.assertEqual(linked_list.get_size(), 8)
        linked_list.delete_by_node_name(node_name='test_node_xyz')
        self.assertEqual(linked_list.get_size(), 8)
コード例 #18
0
ファイル: test_graph_base.py プロジェクト: kfrancischen/pslx
 def test_add_direct_edge(self):
     test_node_1 = OrderedNodeBase(node_name='test_node_1')
     test_node_2 = OrderedNodeBase(node_name='test_node_2')
     test_graph = GraphBase()
     test_graph.add_direct_edge(from_node=test_node_1, to_node=test_node_2)
     self.assertEqual(test_graph.get_num_nodes(), 2)
     self.assertEqual(test_graph.get_num_edges(), 1)
     self.assertTrue(test_node_1.has_child(child_name='test_node_2'))
     self.assertFalse(test_node_2.has_child(child_name='test_node_1'))
コード例 #19
0
 def make_new_partition(self, timestamp):
     new_dir_list = FileUtil.parse_timestamp_to_dir(
         timestamp=timestamp).split('/')
     new_dir = '/'.join(new_dir_list[:self.PARTITIONER_TYPE_TO_HEIGHT_MAP[
         self.PARTITIONER_TYPE]])
     child_node = OrderedNodeBase(node_name=FileUtil.join_paths_to_dir(
         root_dir=self._file_tree.get_root_name(), base_name=new_dir))
     if FileUtil.does_dir_exist(dir_name=child_node.get_node_name()):
         self.sys_log('Node [' + child_node.get_node_name() +
                      "] exist. Don't make new partition.")
         return None
     else:
         self.sys_log('Node [' + child_node.get_node_name() +
                      "] doesn't exist. Make new partition.")
         self._logger.info('Node [' + child_node.get_node_name() +
                           "] doesn't exist. Make new partition.")
         FileUtil.create_dir_if_not_exist(
             dir_name=child_node.get_node_name())
         self.initialize_from_dir(dir_name=self._file_tree.get_root_name())
         return child_node.get_node_name()
コード例 #20
0
ファイル: partitioner_base.py プロジェクト: kfrancischen/pslx
    def make_new_partition(self, timestamp):
        new_dir_list = FileUtil.parse_timestamp_to_dir(
            timestamp=timestamp).split('/')
        for i in range(
                1, self.PARTITIONER_TYPE_TO_HEIGHT_MAP[self.PARTITIONER_TYPE] +
                1):
            new_dir = '/'.join(new_dir_list[:i])
            child_node_name = FileUtil.join_paths_to_dir(
                root_dir=self._file_tree.get_root_name(), base_name=new_dir)
            if not self._file_tree.find_node(child_node_name):
                parent_node_name = FileUtil.join_paths_to_dir(
                    root_dir=self._file_tree.get_root_name(),
                    base_name='/'.join(new_dir_list[:i - 1]) if i > 1 else '')
                parent_node = self._file_tree.find_node(
                    node_name=parent_node_name)
                child_node = OrderedNodeBase(node_name=child_node_name)

                assert parent_node is not None, "Parent node at least needs to exist."
                self._file_tree.add_node(parent_node=parent_node,
                                         child_node=child_node,
                                         order=SortOrder.REVERSE)

        self._file_tree.trim_tree(max_capacity=self._max_capacity)
コード例 #21
0
ファイル: lru_cache_tool.py プロジェクト: kfrancischen/pslx
    def set(self, key, value):
        if key not in self._key_value_store and self._linked_list.get_size(
        ) >= self._max_capacity:
            tail_node = self._linked_list.get_tail_node()
            self._linked_list.delete_by_node(node=tail_node)
            self._key_value_store.pop(tail_node.get_node_name(), None)

        self._linked_list.delete_by_node(
            node=self._key_value_store.pop(key, None))
        if isinstance(key, tuple):
            new_node = OrderedNodeBase(
                node_name='_'.join([str(val) for val in key]))

        else:
            new_node = OrderedNodeBase(node_name=str(key))
        new_node.set_content(content=value)
        self._key_value_store[key] = new_node
        self._linked_list.add_to_head(node=new_node)
コード例 #22
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)
コード例 #23
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)])
コード例 #24
0
 def test_add_to_head(self):
     linked_list = LinkedListBase()
     for i in range(10):
         test_node = OrderedNodeBase(node_name='test_node_' + str(i))
         linked_list.add_to_head(test_node)
         self.assertEqual(linked_list.get_size(), i + 1)
コード例 #25
0
ファイル: linked_list_base.py プロジェクト: alpha-hunter/pslx
class LinkedListBase(Base):
    def __init__(self):
        super().__init__()
        self._dummy_node = OrderedNodeBase('LINKED_LIST_BASE_DUMMY_NODE')
        self._tail = self._dummy_node
        self._node_dict = {}

    def get_size(self):
        return len(self._node_dict)

    def get_tail_node(self):
        return self._tail

    def add_to_head(self, node):
        if node.get_node_name() == 'LINKED_LIST_BASE_DUMMY_NODE':
            self.sys_log(
                'LINKED_LIST_BASE_DUMMY_NODE is a reserved name. Please consider changing name.'
            )
        self.sys_log("Adding " + node.get_node_name() + " to head.")
        if node.get_node_name() in self._node_dict:
            self.delete_by_node(node=node)

        if self._dummy_node.get_num_children() == 0:
            self._tail = node
        else:
            cur_head = self._dummy_node.get_children_nodes()[0]
            self._dummy_node.delete_child(child_node=cur_head)
            node.add_child(child_node=cur_head)

        self._dummy_node.add_child(child_node=node)
        self._node_dict[node.get_node_name()] = node
        self.check_valid()

    def delete_by_node(self, node):
        if not node or node.get_num_parents() == 0:
            return
        self.sys_log("Deleting " + node.get_node_name() + ".")

        node_parent = node.get_parents_nodes()[0]
        node_parent.delete_child(child_node=node)
        if node == self._tail:
            self._tail = node_parent
        else:
            node_child = node.get_children_nodes()[0]
            node_child.delete_parent(parent_node=node)
            node_parent.add_child(child_node=node_child)

        self.check_valid()

    def delete_by_node_name(self, node_name):
        node = self._node_dict.pop(node_name, None)
        if node:
            self.delete_by_node(node=node)

    def print_self(self):
        print_str = []
        start_node = self._dummy_node
        while start_node.get_num_children() > 0:
            start_node = start_node.get_children_nodes()[0]
            print_str.append(start_node.get_node_name())

        print('->'.join(print_str))

    def check_valid(self):
        start_node = self._dummy_node
        assert start_node.get_num_children(
        ) == 0 or start_node.get_num_children() == 1
        while start_node.get_num_children() > 0:
            start_node = start_node.get_children_nodes()[0]
            assert start_node.get_num_children(
            ) == 0 or start_node.get_num_children() == 1
コード例 #26
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])
コード例 #27
0
ファイル: test_graph_base.py プロジェクト: kfrancischen/pslx
 def test_is_dag_1(self):
     test_node_1 = OrderedNodeBase(node_name='test_node_1')
     test_node_2 = OrderedNodeBase(node_name='test_node_2')
     test_graph = GraphBase()
     test_graph.add_indirect_edge(node_1=test_node_1, node_2=test_node_2)
     self.assertFalse(test_graph.is_dag())
コード例 #28
0
ファイル: linked_list_base.py プロジェクト: alpha-hunter/pslx
 def __init__(self):
     super().__init__()
     self._dummy_node = OrderedNodeBase('LINKED_LIST_BASE_DUMMY_NODE')
     self._tail = self._dummy_node
     self._node_dict = {}
コード例 #29
0
 def test_ordered_node_children_ordered(self):
     test_node = OrderedNodeBase(node_name='test_node')
     self.assertTrue(test_node.is_children_ordered())