diff options
author | Wojtek Kosior <koszko@koszko.org> | 2022-08-10 21:07:54 +0200 |
---|---|---|
committer | Wojtek Kosior <koszko@koszko.org> | 2022-08-10 21:07:54 +0200 |
commit | 72fcc76cc75ccb7e180886170db01dae637e250e (patch) | |
tree | 7224c4ac0d10d9b668749285d57f938f9c11c0ad /src/hydrilla/proxy | |
parent | 879c41927171efc8d77d1de2739b18e2eb57580f (diff) | |
download | haketilo-hydrilla-72fcc76cc75ccb7e180886170db01dae637e250e.tar.gz haketilo-hydrilla-72fcc76cc75ccb7e180886170db01dae637e250e.zip |
small clean up for item definitions handling before dependency resolution happens
Diffstat (limited to 'src/hydrilla/proxy')
-rw-r--r-- | src/hydrilla/proxy/simple_dependency_satisfying.py | 29 | ||||
-rw-r--r-- | src/hydrilla/proxy/state_impl/concrete_state.py | 72 |
2 files changed, 60 insertions, 41 deletions
diff --git a/src/hydrilla/proxy/simple_dependency_satisfying.py b/src/hydrilla/proxy/simple_dependency_satisfying.py index 9716fe5..889ae98 100644 --- a/src/hydrilla/proxy/simple_dependency_satisfying.py +++ b/src/hydrilla/proxy/simple_dependency_satisfying.py @@ -57,7 +57,7 @@ ComputedPayloadsDict = dict[ empty_identifiers_set: set[str] = set() @dc.dataclass(frozen=True) -class ItemsCollection: +class _ItemsCollection: resources: t.Mapping[str, item_infos.ResourceInfo] mappings: t.Mapping[str, item_infos.MappingInfo] @@ -187,3 +187,30 @@ class ItemsCollection: computed_result.pop(self.mappings[identifier], None) return computed_result + +AnyInfoVar = t.TypeVar( + 'AnyInfoVar', + item_infos.ResourceInfo, + item_infos.MappingInfo +) + +def _choose_newest(infos: t.Iterable[AnyInfoVar]) -> dict[str, AnyInfoVar]: + best_versions: dict[str, AnyInfoVar] = {} + + for info in infos: + other = best_versions.setdefault(info.identifier, info) + + if (other.version, other.repo, info.repo_iteration) < \ + (info.version, info.repo, other.repo_iteration): + best_versions[info.identifier] = info + + return best_versions + +def compute_payloads( + resources: t.Iterable[item_infos.ResourceInfo], + mappings: t.Iterable[item_infos.MappingInfo] +) -> ComputedPayloadsDict: + best_resources = _choose_newest(resources) + best_mappings = _choose_newest(mappings) + + return _ItemsCollection(best_resources, best_mappings).compute_payloads() diff --git a/src/hydrilla/proxy/state_impl/concrete_state.py b/src/hydrilla/proxy/state_impl/concrete_state.py index cd6698c..1b46ae9 100644 --- a/src/hydrilla/proxy/state_impl/concrete_state.py +++ b/src/hydrilla/proxy/state_impl/concrete_state.py @@ -48,7 +48,7 @@ from ... import pattern_tree from ... import url_patterns from ... import versions from ... import item_infos -from ..simple_dependency_satisfying import ItemsCollection, ComputedPayload +from ..simple_dependency_satisfying import compute_payloads, ComputedPayload from .. import state as st from .. import policies from . import base @@ -56,8 +56,6 @@ from . import base here = Path(__file__).resolve().parent -AnyInfo = t.Union[item_infos.ResourceInfo, item_infos.MappingInfo] - @dc.dataclass(frozen=True, unsafe_hash=True) # type: ignore[misc] class ConcreteRepoRef(st.RepoRef): def remove(self, state: st.HaketiloState) -> None: @@ -316,7 +314,7 @@ def get_or_make_item_version( item_id: int, repo_iteration_id: int, definition: str, - info: AnyInfo + info: item_infos.AnyInfo ) -> int: ver_str = versions.version_string(info.version) @@ -419,6 +417,31 @@ def make_file_use( (item_version_id, file_id, name, type, mime_type, idx) ) +def get_infos_of_type(cursor: sqlite3.Cursor, info_type: t.Type[AnyInfoVar]) \ + -> t.Iterable[AnyInfoVar]: + cursor.execute( + ''' + SELECT + iv.definition, r.name, ri.iteration + FROM + item_versions AS iv + JOIN items AS i USING (item_id) + JOIN repo_iterations AS ri USING (repo_iteration_id) + JOIN repos AS r USING (repo_id) + WHERE + i.type = ?; + ''', + (info_type.type_name[0].upper(),) + ) + + result: list[AnyInfoVar] = [] + + for definition, repo_name, repo_iteration in cursor.fetchall(): + definition_io = io.StringIO(definition) + result.append(info_type.load(definition_io, repo_name, repo_iteration)) + + return result + @dc.dataclass class ConcreteHaketiloState(base.HaketiloStateWithFields): def __post_init__(self) -> None: @@ -474,7 +497,7 @@ class ConcreteHaketiloState(base.HaketiloStateWithFields): with self.cursor(lock=True, transaction=True) as cursor: for info_type in [item_infos.ResourceInfo, item_infos.MappingInfo]: - info: AnyInfo + info: item_infos.AnyInfo for info, definition in read_items( malcontent_dir_path, info_type # type: ignore @@ -551,45 +574,14 @@ class ConcreteHaketiloState(base.HaketiloStateWithFields): ) def rebuild_structures(self, cursor: sqlite3.Cursor) -> None: - cursor.execute( - ''' - SELECT - item_id, type, version, definition - FROM - item_versions JOIN items USING (item_id); - ''' - ) - - best_versions: dict[int, versions.VerTuple] = {} - definitions = {} - types = {} - - for item_id, item_type, ver_str, definition in cursor.fetchall(): - # TODO: what we're doing in this loop does not yet take different - # repos and different repo iterations into account. - ver = versions.parse_normalize_version(ver_str) - if best_versions.get(item_id, (0,)) < ver: - best_versions[item_id] = ver - definitions[item_id] = definition - types[item_id] = item_type - - resources = {} - mappings = {} - - for item_id, definition in definitions.items(): - if types[item_id] == 'R': - r_info = item_infos.ResourceInfo.load(io.StringIO(definition)) - resources[r_info.identifier] = r_info - else: - m_info = item_infos.MappingInfo.load(io.StringIO(definition)) - mappings[m_info.identifier] = m_info + resources = get_infos_of_type(cursor, item_infos.ResourceInfo) + mappings = get_infos_of_type(cursor, item_infos.MappingInfo) - items_collection = ItemsCollection(resources, mappings) - computed_payloads = items_collection.compute_payloads() + payloads = compute_payloads(resources, mappings) payloads_data = {} - for mapping_info, by_pattern in computed_payloads.items(): + for mapping_info, by_pattern in payloads.items(): for num, (pattern, payload) in enumerate(by_pattern.items()): payload_id = f'{num}@{mapping_info.identifier}' |