Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/in_memory.py
Show First 20 Lines • Show All 664 Lines • ▼ Show 20 Lines | def release_get(self, releases): | ||||
Yields: | Yields: | ||||
dicts with the same keys as those given to `release_add` | dicts with the same keys as those given to `release_add` | ||||
(or ``None`` if a release does not exist) | (or ``None`` if a release does not exist) | ||||
""" | """ | ||||
for rel_id in releases: | for rel_id in releases: | ||||
yield copy.deepcopy(self._releases.get(rel_id)) | yield copy.deepcopy(self._releases.get(rel_id)) | ||||
def snapshot_add(self, snapshots, origin=None, visit=None): | def snapshot_add(self, snapshots): | ||||
"""Add a snapshot to the storage | """Add a snapshot to the storage | ||||
Args: | Args: | ||||
snapshot ([dict]): the snapshots to add, containing the | snapshot ([dict]): the snapshots to add, containing the | ||||
following keys: | following keys: | ||||
- **id** (:class:`bytes`): id of the snapshot | - **id** (:class:`bytes`): id of the snapshot | ||||
- **branches** (:class:`dict`): branches the snapshot contains, | - **branches** (:class:`dict`): branches the snapshot contains, | ||||
Show All 12 Lines | def snapshot_add(self, snapshots): | ||||
ValueError: if the origin's or visit's identifier does not exist. | ValueError: if the origin's or visit's identifier does not exist. | ||||
Returns: | Returns: | ||||
Summary dict of keys with associated count as values | Summary dict of keys with associated count as values | ||||
snapshot_added: Count of object actually stored in db | snapshot_added: Count of object actually stored in db | ||||
""" | """ | ||||
if origin: | |||||
if not visit: | |||||
raise TypeError( | |||||
'snapshot_add expects one argument (or, as a legacy ' | |||||
'behavior, three arguments), not two') | |||||
if isinstance(snapshots, (int, str)): | |||||
# Called by legacy code that uses the new api/client.py | |||||
(origin, visit, snapshots) = \ | |||||
(snapshots, origin, [visit]) | |||||
else: | |||||
# Called by legacy code that uses the old api/client.py | |||||
snapshots = [snapshots] | |||||
count = 0 | count = 0 | ||||
for snapshot in snapshots: | for snapshot in snapshots: | ||||
snapshot_id = snapshot['id'] | snapshot_id = snapshot['id'] | ||||
if snapshot_id not in self._snapshots: | if snapshot_id not in self._snapshots: | ||||
if self.journal_writer: | if self.journal_writer: | ||||
self.journal_writer.write_addition('snapshot', snapshot) | self.journal_writer.write_addition('snapshot', snapshot) | ||||
self._snapshots[snapshot_id] = { | self._snapshots[snapshot_id] = { | ||||
'id': snapshot_id, | 'id': snapshot_id, | ||||
'branches': copy.deepcopy(snapshot['branches']), | 'branches': copy.deepcopy(snapshot['branches']), | ||||
'_sorted_branch_names': sorted(snapshot['branches']) | '_sorted_branch_names': sorted(snapshot['branches']) | ||||
} | } | ||||
self._objects[snapshot_id].append(('snapshot', snapshot_id)) | self._objects[snapshot_id].append(('snapshot', snapshot_id)) | ||||
count += 1 | count += 1 | ||||
if visit: | |||||
# Legacy API, there can be only one snapshot | |||||
self.origin_visit_update( | |||||
origin, visit, snapshot=snapshots[0]['id']) | |||||
return {'snapshot:add': count} | return {'snapshot:add': count} | ||||
def snapshot_get(self, snapshot_id): | def snapshot_get(self, snapshot_id): | ||||
"""Get the content, possibly partial, of a snapshot with the given id | """Get the content, possibly partial, of a snapshot with the given id | ||||
The branches of the snapshot are iterated in the lexicographical | The branches of the snapshot are iterated in the lexicographical | ||||
order of their names. | order of their names. | ||||
▲ Show 20 Lines • Show All 942 Lines • Show Last 20 Lines |