Optimize Subsection computation
This commit is contained in:
@@ -8,6 +8,7 @@ The following internal data structures are implemented:
|
||||
_BlockRelations - Data structure for a single block's relations.
|
||||
_BlockData - Data structure for a single block's data.
|
||||
"""
|
||||
from copy import deepcopy
|
||||
from functools import partial
|
||||
from logging import getLogger
|
||||
|
||||
@@ -413,6 +414,19 @@ class BlockStructureBlockData(BlockStructure):
|
||||
# Map of a transformer's name to its non-block-specific data.
|
||||
self.transformer_data = TransformerDataMap()
|
||||
|
||||
def copy(self):
|
||||
"""
|
||||
Returns a new instance of BlockStructureBlockData with a
|
||||
deep-copy of this instance's contents.
|
||||
"""
|
||||
from .factory import BlockStructureFactory
|
||||
return BlockStructureFactory.create_new(
|
||||
self.root_block_usage_key,
|
||||
deepcopy(self._block_relations),
|
||||
deepcopy(self.transformer_data),
|
||||
deepcopy(self._block_data_map),
|
||||
)
|
||||
|
||||
def iteritems(self):
|
||||
"""
|
||||
Returns iterator of (UsageKey, BlockData) pairs for all
|
||||
|
||||
@@ -6,7 +6,8 @@ from logging import getLogger
|
||||
|
||||
from openedx.core.lib.cache_utils import zpickle, zunpickle
|
||||
|
||||
from .block_structure import BlockStructureModulestoreData, BlockStructureBlockData
|
||||
from .block_structure import BlockStructureBlockData
|
||||
from .factory import BlockStructureFactory
|
||||
|
||||
|
||||
logger = getLogger(__name__) # pylint: disable=C0103
|
||||
@@ -97,12 +98,12 @@ class BlockStructureCache(object):
|
||||
|
||||
# Deserialize and construct the block structure.
|
||||
block_relations, transformer_data, block_data_map = zunpickle(zp_data_from_cache)
|
||||
block_structure = BlockStructureModulestoreData(root_block_usage_key)
|
||||
block_structure._block_relations = block_relations
|
||||
block_structure.transformer_data = transformer_data
|
||||
block_structure._block_data_map = block_data_map
|
||||
|
||||
return block_structure
|
||||
return BlockStructureFactory.create_new(
|
||||
root_block_usage_key,
|
||||
block_relations,
|
||||
transformer_data,
|
||||
block_data_map,
|
||||
)
|
||||
|
||||
def delete(self, root_block_usage_key):
|
||||
"""
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""
|
||||
Module for factory class for BlockStructure objects.
|
||||
"""
|
||||
from .block_structure import BlockStructureModulestoreData
|
||||
from .block_structure import BlockStructureModulestoreData, BlockStructureBlockData
|
||||
|
||||
|
||||
class BlockStructureFactory(object):
|
||||
@@ -82,3 +82,14 @@ class BlockStructureFactory(object):
|
||||
NoneType - If the root_block_usage_key is not found in the cache.
|
||||
"""
|
||||
return block_structure_cache.get(root_block_usage_key)
|
||||
|
||||
@classmethod
|
||||
def create_new(cls, root_block_usage_key, block_relations, transformer_data, block_data_map):
|
||||
"""
|
||||
Returns a new block structure for given the arguments.
|
||||
"""
|
||||
block_structure = BlockStructureBlockData(root_block_usage_key)
|
||||
block_structure._block_relations = block_relations # pylint: disable=protected-access
|
||||
block_structure.transformer_data = transformer_data
|
||||
block_structure._block_data_map = block_data_map # pylint: disable=protected-access
|
||||
return block_structure
|
||||
|
||||
@@ -33,7 +33,7 @@ class BlockStructureManager(object):
|
||||
self.modulestore = modulestore
|
||||
self.block_structure_cache = BlockStructureCache(cache)
|
||||
|
||||
def get_transformed(self, transformers, starting_block_usage_key=None):
|
||||
def get_transformed(self, transformers, starting_block_usage_key=None, collected_block_structure=None):
|
||||
"""
|
||||
Returns the transformed Block Structure for the root_block_usage_key,
|
||||
starting at starting_block_usage_key, getting block data from the cache
|
||||
@@ -50,11 +50,17 @@ class BlockStructureManager(object):
|
||||
in the block structure that is to be transformed.
|
||||
If None, root_block_usage_key is used.
|
||||
|
||||
collected_block_structure (BlockStructureBlockData) - A
|
||||
block structure retrieved from a prior call to
|
||||
get_collected. Can be optionally provided if already available,
|
||||
for optimization.
|
||||
|
||||
Returns:
|
||||
BlockStructureBlockData - A transformed block structure,
|
||||
starting at starting_block_usage_key.
|
||||
"""
|
||||
block_structure = self.get_collected()
|
||||
block_structure = collected_block_structure.copy() if collected_block_structure else self.get_collected()
|
||||
|
||||
if starting_block_usage_key:
|
||||
# Override the root_block_usage_key so traversals start at the
|
||||
# requested location. The rest of the structure will be pruned
|
||||
|
||||
@@ -221,3 +221,48 @@ class TestBlockStructureData(TestCase, ChildrenMapTestMixin):
|
||||
block_structure = self.create_block_structure(ChildrenMapTestMixin.LINEAR_CHILDREN_MAP)
|
||||
block_structure.remove_block_traversal(lambda block: block == 2)
|
||||
self.assert_block_structure(block_structure, [[1], [], [], []], missing_blocks=[2])
|
||||
|
||||
def test_copy(self):
|
||||
def _set_value(structure, value):
|
||||
"""
|
||||
Sets a test transformer block field to the given value in the given structure.
|
||||
"""
|
||||
structure.set_transformer_block_field(1, 'transformer', 'test_key', value)
|
||||
|
||||
def _get_value(structure):
|
||||
"""
|
||||
Returns the value of the test transformer block field in the given structure.
|
||||
"""
|
||||
return structure[1].transformer_data['transformer'].test_key
|
||||
|
||||
# create block structure and verify blocks pre-exist
|
||||
block_structure = self.create_block_structure(ChildrenMapTestMixin.LINEAR_CHILDREN_MAP)
|
||||
self.assert_block_structure(block_structure, [[1], [2], [3], []])
|
||||
_set_value(block_structure, 'original_value')
|
||||
|
||||
# create a new copy of the structure and verify they are equivalent
|
||||
new_copy = block_structure.copy()
|
||||
self.assertEquals(block_structure.root_block_usage_key, new_copy.root_block_usage_key)
|
||||
for block in block_structure:
|
||||
self.assertIn(block, new_copy)
|
||||
self.assertEquals(block_structure.get_parents(block), new_copy.get_parents(block))
|
||||
self.assertEquals(block_structure.get_children(block), new_copy.get_children(block))
|
||||
self.assertEquals(_get_value(block_structure), _get_value(new_copy))
|
||||
|
||||
# verify edits to original block structure do not affect the copy
|
||||
block_structure.remove_block(2, keep_descendants=True)
|
||||
self.assert_block_structure(block_structure, [[1], [3], [], []], missing_blocks=[2])
|
||||
self.assert_block_structure(new_copy, [[1], [2], [3], []])
|
||||
|
||||
_set_value(block_structure, 'edit1')
|
||||
self.assertEquals(_get_value(block_structure), 'edit1')
|
||||
self.assertEquals(_get_value(new_copy), 'original_value')
|
||||
|
||||
# verify edits to copy do not affect the original
|
||||
new_copy.remove_block(3, keep_descendants=True)
|
||||
self.assert_block_structure(block_structure, [[1], [3], [], []], missing_blocks=[2])
|
||||
self.assert_block_structure(new_copy, [[1], [2], [], []], missing_blocks=[3])
|
||||
|
||||
_set_value(new_copy, 'edit2')
|
||||
self.assertEquals(_get_value(block_structure), 'edit1')
|
||||
self.assertEquals(_get_value(new_copy), 'edit2')
|
||||
|
||||
@@ -54,3 +54,15 @@ class TestBlockStructureFactory(TestCase, ChildrenMapTestMixin):
|
||||
block_structure_cache=cache,
|
||||
)
|
||||
)
|
||||
|
||||
def test_new(self):
|
||||
block_structure = BlockStructureFactory.create_from_modulestore(
|
||||
root_block_usage_key=0, modulestore=self.modulestore
|
||||
)
|
||||
new_structure = BlockStructureFactory.create_new(
|
||||
block_structure.root_block_usage_key,
|
||||
block_structure._block_relations, # pylint: disable=protected-access
|
||||
block_structure.transformer_data,
|
||||
block_structure._block_data_map, # pylint: disable=protected-access
|
||||
)
|
||||
self.assert_block_structure(new_structure, self.children_map)
|
||||
|
||||
@@ -139,6 +139,24 @@ class TestBlockStructureManager(TestCase, ChildrenMapTestMixin):
|
||||
TestTransformer1.assert_collected(block_structure)
|
||||
TestTransformer1.assert_transformed(block_structure)
|
||||
|
||||
def test_get_transformed_with_collected(self):
|
||||
with mock_registered_transformers(self.registered_transformers):
|
||||
collected_block_structure = self.bs_manager.get_collected()
|
||||
|
||||
# using the same collected block structure,
|
||||
# transform at different starting blocks
|
||||
for (starting_block, expected_structure, expected_missing_blocks) in [
|
||||
(0, [[1, 2], [3, 4], [], [], []], []),
|
||||
(1, [[], [3, 4], [], [], []], [0, 2]),
|
||||
(2, [[], [], [], [], []], [0, 1, 3, 4]),
|
||||
]:
|
||||
block_structure = self.bs_manager.get_transformed(
|
||||
self.transformers,
|
||||
starting_block_usage_key=starting_block,
|
||||
collected_block_structure=collected_block_structure,
|
||||
)
|
||||
self.assert_block_structure(block_structure, expected_structure, missing_blocks=expected_missing_blocks)
|
||||
|
||||
def test_get_transformed_with_nonexistent_starting_block(self):
|
||||
with mock_registered_transformers(self.registered_transformers):
|
||||
with self.assertRaises(UsageKeyNotInBlockStructure):
|
||||
|
||||
Reference in New Issue
Block a user