Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/storage.py
Show First 20 Lines • Show All 879 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, snapshots, origin=None, visit=None, | def snapshot_add(self, snapshots, db=None, cur=None): | ||||
db=None, cur=None): | |||||
"""Add snapshots to the storage. | """Add snapshots 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, | ||||
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) | ||||
- **target_type** (:class:`str`): one of ``content``, | - **target_type** (:class:`str`): one of ``content``, | ||||
``directory``, ``revision``, ``release``, | ``directory``, ``revision``, ``release``, | ||||
``snapshot``, ``alias`` | ``snapshot``, ``alias`` | ||||
- **target** (:class:`bytes`): identifier of the target | - **target** (:class:`bytes`): identifier of the target | ||||
(currently a ``sha1_git`` for all object kinds, or the name | (currently a ``sha1_git`` for all object kinds, or the name | ||||
of the target branch for aliases) | of the target branch for aliases) | ||||
origin (int): legacy argument for backward compatibility | |||||
visit (int): legacy argument for backward compatibility | |||||
Raises: | Raises: | ||||
ValueError: if the origin or visit id does not exist. | ValueError: if the origin or visit id does not exist. | ||||
Returns: | Returns: | ||||
Summary dict of keys with associated count as values | Summary dict of keys with associated count as values | ||||
snapshot:add: Count of object actually stored in db | snapshot:add: 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_id, visit_id, snapshots) = \ | |||||
(snapshots, origin, [visit]) | |||||
else: | |||||
# Called by legacy code that uses the old api/client.py | |||||
origin_id = origin | |||||
visit_id = visit | |||||
snapshots = [snapshots] | |||||
else: | |||||
# Called by new code that uses the new api/client.py | |||||
origin_id = visit_id = None | |||||
created_temp_table = False | created_temp_table = False | ||||
count = 0 | count = 0 | ||||
for snapshot in snapshots: | for snapshot in snapshots: | ||||
if not db.snapshot_exists(snapshot['id'], cur): | if not db.snapshot_exists(snapshot['id'], cur): | ||||
if not created_temp_table: | if not created_temp_table: | ||||
db.mktemp_snapshot_branch(cur) | db.mktemp_snapshot_branch(cur) | ||||
created_temp_table = True | created_temp_table = True | ||||
Show All 14 Lines | def snapshot_add(self, snapshots, db=None, cur=None): | ||||
) | ) | ||||
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) | ||||
count += 1 | count += 1 | ||||
if visit_id: | |||||
# Legacy API, there can be only one snapshot | |||||
self.origin_visit_update( | |||||
origin_id, visit_id, snapshot=snapshots[0]['id'], | |||||
db=db, cur=cur) | |||||
return {'snapshot:add': count} | return {'snapshot:add': count} | ||||
@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 927 Lines • Show Last 20 Lines |