Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/in_memory.py
Show First 20 Lines • Show All 771 Lines • ▼ Show 20 Lines | def object_find_by_sha1_git(self, ids, db=None, cur=None): | ||||
objs = self._objects.get(id_, []) | objs = self._objects.get(id_, []) | ||||
ret[id_] = [{ | ret[id_] = [{ | ||||
'sha1_git': id_, | 'sha1_git': id_, | ||||
'type': obj[0], | 'type': obj[0], | ||||
'id': obj[1], | 'id': obj[1], | ||||
'object_id': id_, | 'object_id': id_, | ||||
} for obj in objs] | } for obj in objs] | ||||
return ret | return ret | ||||
def origin_get(self, origin): | def origin_get(self, origins): | ||||
anlambert: The method description should be updated to inform that multiple origins can be passed as… | |||||
"""Return the origin either identified by its id or its tuple | """Return origins, either all identified by their ids or all | ||||
(type, url). | identified by tuples (type, url). | ||||
Args: | Args: | ||||
origin: dictionary representing the individual origin to find. | origin: a list of dictionaries representing the individual | ||||
This dict has either the keys type and url: | origins to find. | ||||
These dicts have either the keys type and url: | |||||
- type (FIXME: enum TBD): the origin type ('git', 'wget', ...) | - type (FIXME: enum TBD): the origin type ('git', 'wget', ...) | ||||
- url (bytes): the url the origin points to | - url (bytes): the url the origin points to | ||||
or the id: | or the id: | ||||
- id (int): the origin's identifier | - id (int): the origin's identifier | ||||
Returns: | Returns: | ||||
dict: the origin dictionary with the keys: | dict: the origin dictionary with the keys: | ||||
- id: origin's id | - id: origin's id | ||||
- type: origin's type | - type: origin's type | ||||
- url: origin's url | - url: origin's url | ||||
Raises: | Raises: | ||||
ValueError: if the keys does not match (url and type) nor id. | ValueError: if the keys does not match (url and type) nor id. | ||||
""" | """ | ||||
if isinstance(origins, dict): | |||||
# Old API | |||||
return_single = True | |||||
origins = [origins] | |||||
else: | |||||
return_single = False | |||||
# Sanity check to be error-compatible with the pgsql backend | |||||
if any('id' in origin for origin in origins) \ | |||||
and not all('id' in origin for origin in origins): | |||||
raise ValueError( | |||||
'Either all origins or none at all should have an "id".') | |||||
if any('type' in origin and 'url' in origin for origin in origins) \ | |||||
and not all('type' in origin and 'url' in origin | |||||
for origin in origins): | |||||
raise ValueError( | |||||
'Either all origins or none at all should have a ' | |||||
'"type" and an "url".') | |||||
results = [] | |||||
for origin in origins: | |||||
if 'id' in origin: | if 'id' in origin: | ||||
origin_id = origin['id'] | origin_id = origin['id'] | ||||
elif 'type' in origin and 'url' in origin: | elif 'type' in origin and 'url' in origin: | ||||
origin_id = self._origin_id(origin) | origin_id = self._origin_id(origin) | ||||
else: | else: | ||||
raise ValueError('Origin must have either id or (type and url).') | raise ValueError( | ||||
'Origin must have either id or (type and url).') | |||||
origin = None | origin = None | ||||
# self._origin_id can return None | # self._origin_id can return None | ||||
if origin_id is not None and origin_id <= len(self._origins): | if origin_id is not None and origin_id <= len(self._origins): | ||||
origin = copy.deepcopy(self._origins[origin_id-1]) | origin = copy.deepcopy(self._origins[origin_id-1]) | ||||
origin['id'] = origin_id | origin['id'] = origin_id | ||||
return origin | results.append(origin) | ||||
if return_single: | |||||
assert len(results) == 1 | |||||
Done Inline ActionsI think that test can be removed, None will be returned with or without it anlambert: I think that test can be removed, `None` will be returned with or without it | |||||
Done Inline ActionsIndeed, that was from an earlier iteration of my code. vlorentz: Indeed, that was from an earlier iteration of my code. | |||||
return results[0] | |||||
else: | |||||
return results | |||||
Not Done Inline ActionsThat else block could be removed by initializing return_single to False before the dict check anlambert: That else block could be removed by initializing `return_single` to `False` before the dict… | |||||
def origin_get_range(self, origin_from=1, origin_count=100): | def origin_get_range(self, origin_from=1, origin_count=100): | ||||
"""Retrieve ``origin_count`` origins whose ids are greater | """Retrieve ``origin_count`` origins whose ids are greater | ||||
or equal than ``origin_from``. | or equal than ``origin_from``. | ||||
Origins are sorted by id before retrieving them. | Origins are sorted by id before retrieving them. | ||||
Args: | Args: | ||||
▲ Show 20 Lines • Show All 459 Lines • Show Last 20 Lines |
The method description should be updated to inform that multiple origins can be passed as parameter now.