Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/storage.py
Show First 20 Lines • Show All 762 Lines • ▼ Show 20 Lines | def release_get(self, releases, db=None, cur=None): | ||||
""" | """ | ||||
for release in db.release_get_from_list(releases, cur): | for release in db.release_get_from_list(releases, cur): | ||||
data = converters.db_to_release( | data = converters.db_to_release( | ||||
dict(zip(db.release_get_cols, release)) | dict(zip(db.release_get_cols, release)) | ||||
) | ) | ||||
yield data if data['target_type'] else None | yield data if data['target_type'] else None | ||||
@db_transaction() | @db_transaction() | ||||
def snapshot_add(self, snapshot, origin=None, visit=None, | def snapshot_add(self, snapshots, origin=None, visit=None, | ||||
db=None, cur=None): | db=None, cur=None): | ||||
"""Add a snapshot for the given origin/visit couple | """Add snapshots to the storage. | ||||
Args: | Args: | ||||
snapshot (dict): the snapshot to add to the visit, 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, | ||||
mapping the branch name (:class:`bytes`) to the branch target, | mapping the branch name (:class:`bytes`) to the branch target, | ||||
itself a :class:`dict` (or ``None`` if the branch points to an | itself a :class:`dict` (or ``None`` if the branch points to an | ||||
unknown object) | unknown object) | ||||
Show All 9 Lines | def snapshot_add(self, snapshots, origin=None, visit=None, | ||||
Raises: | Raises: | ||||
ValueError: if the origin or visit id does not exist. | ValueError: if the origin or visit id does not exist. | ||||
""" | """ | ||||
if origin: | if origin: | ||||
if not visit: | if not visit: | ||||
raise TypeError( | raise TypeError( | ||||
'snapshot_add expects one argument (or, as a legacy ' | 'snapshot_add expects one argument (or, as a legacy ' | ||||
'behavior, three arguments), not two') | 'behavior, three arguments), not two') | ||||
if isinstance(snapshot, int): | if isinstance(snapshots, int): | ||||
# Called by legacy code that uses the new api/client.py | # Called by legacy code that uses the new api/client.py | ||||
(origin_id, visit_id, snapshot) = \ | (origin_id, visit_id, snapshots) = \ | ||||
(snapshot, origin, visit) | (snapshots, origin, [visit]) | ||||
else: | else: | ||||
# Called by legacy code that uses the old api/client.py | # Called by legacy code that uses the old api/client.py | ||||
origin_id = origin | origin_id = origin | ||||
visit_id = visit | visit_id = visit | ||||
snapshots = [snapshots] | |||||
else: | else: | ||||
# Called by new code that uses the new api/client.py | # Called by new code that uses the new api/client.py | ||||
origin_id = visit_id = None | origin_id = visit_id = None | ||||
for snapshot in snapshots: | |||||
if not db.snapshot_exists(snapshot['id'], cur): | if not db.snapshot_exists(snapshot['id'], cur): | ||||
db.mktemp_snapshot_branch(cur) | db.mktemp_snapshot_branch(cur) | ||||
db.copy_to( | db.copy_to( | ||||
( | ( | ||||
{ | { | ||||
'name': name, | 'name': name, | ||||
'target': info['target'] if info else None, | 'target': info['target'] if info else None, | ||||
'target_type': info['target_type'] if info else None, | 'target_type': (info['target_type'] | ||||
if info else None), | |||||
} | } | ||||
for name, info in snapshot['branches'].items() | for name, info in snapshot['branches'].items() | ||||
), | ), | ||||
'tmp_snapshot_branch', | 'tmp_snapshot_branch', | ||||
['name', 'target', 'target_type'], | ['name', 'target', 'target_type'], | ||||
cur, | cur, | ||||
) | ) | ||||
if self.journal_writer: | if self.journal_writer: | ||||
self.journal_writer.write_addition('snapshot', snapshot) | self.journal_writer.write_addition('snapshot', snapshot) | ||||
db.snapshot_add(snapshot['id'], cur) | db.snapshot_add(snapshot['id'], cur) | ||||
if visit_id: | if visit_id: | ||||
# Legacy API, there can be only one snapshot | |||||
self.origin_visit_update( | self.origin_visit_update( | ||||
origin_id, visit_id, snapshot=snapshot['id'], | origin_id, visit_id, snapshot=snapshots[0]['id'], | ||||
db=db, cur=cur) | db=db, cur=cur) | ||||
@db_transaction(statement_timeout=2000) | @db_transaction(statement_timeout=2000) | ||||
def snapshot_get(self, snapshot_id, db=None, cur=None): | def snapshot_get(self, snapshot_id, db=None, cur=None): | ||||
"""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 784 Lines • Show Last 20 Lines |