2017-03-23 18:51:37 +00:00
|
|
|
import logging
|
|
|
|
|
2017-03-23 01:51:28 +00:00
|
|
|
from peewee import IntegrityError
|
|
|
|
|
|
|
|
from data.model import db_transaction
|
|
|
|
|
2017-03-23 18:51:37 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2017-03-23 01:51:28 +00:00
|
|
|
|
2017-03-23 16:37:32 +00:00
|
|
|
def _ensure_sha256_header(digest):
|
|
|
|
if digest.startswith('sha256:'):
|
|
|
|
return digest
|
|
|
|
return 'sha256:' + digest
|
|
|
|
|
|
|
|
|
2018-05-24 21:54:51 +00:00
|
|
|
def get_blob(digest, models_ref):
|
2017-03-23 01:51:28 +00:00
|
|
|
""" Find a blob by its digest. """
|
2018-05-24 21:54:51 +00:00
|
|
|
Blob = models_ref.Blob
|
2017-03-23 16:37:32 +00:00
|
|
|
return Blob.select().where(Blob.digest == _ensure_sha256_header(digest)).get()
|
2017-03-23 01:51:28 +00:00
|
|
|
|
|
|
|
|
2018-05-24 21:54:51 +00:00
|
|
|
def get_or_create_blob(digest, size, media_type_name, locations, models_ref):
|
2017-03-23 01:51:28 +00:00
|
|
|
""" Try to find a blob by its digest or create it. """
|
2018-05-24 21:54:51 +00:00
|
|
|
Blob = models_ref.Blob
|
|
|
|
BlobPlacement = models_ref.BlobPlacement
|
2017-03-23 18:51:37 +00:00
|
|
|
|
|
|
|
# Get or create the blog entry for the digest.
|
|
|
|
try:
|
2018-05-24 21:54:51 +00:00
|
|
|
blob = get_blob(digest, models_ref)
|
2017-03-23 18:51:37 +00:00
|
|
|
logger.debug('Retrieved blob with digest %s', digest)
|
|
|
|
except Blob.DoesNotExist:
|
|
|
|
blob = Blob.create(digest=_ensure_sha256_header(digest),
|
|
|
|
media_type_id=Blob.media_type.get_id(media_type_name),
|
|
|
|
size=size)
|
|
|
|
logger.debug('Created blob with digest %s', digest)
|
|
|
|
|
|
|
|
# Add the locations to the blob.
|
|
|
|
for location_name in locations:
|
|
|
|
location_id = BlobPlacement.location.get_id(location_name)
|
2017-03-23 01:51:28 +00:00
|
|
|
try:
|
2017-03-23 18:51:37 +00:00
|
|
|
BlobPlacement.create(blob=blob, location=location_id)
|
|
|
|
except IntegrityError:
|
|
|
|
logger.debug('Location %s already existing for blob %s', location_name, blob.id)
|
|
|
|
|
|
|
|
return blob
|
2017-03-23 01:51:28 +00:00
|
|
|
|
|
|
|
|
2018-05-24 21:54:51 +00:00
|
|
|
def get_blob_locations(digest, models_ref):
|
2017-03-23 01:51:28 +00:00
|
|
|
""" Find all locations names for a blob. """
|
2018-05-24 21:54:51 +00:00
|
|
|
Blob = models_ref.Blob
|
|
|
|
BlobPlacement = models_ref.BlobPlacement
|
|
|
|
BlobPlacementLocation = models_ref.BlobPlacementLocation
|
|
|
|
|
2017-03-23 01:51:28 +00:00
|
|
|
return [x.name for x in
|
|
|
|
BlobPlacementLocation
|
|
|
|
.select()
|
|
|
|
.join(BlobPlacement)
|
|
|
|
.join(Blob)
|
2017-03-23 16:37:32 +00:00
|
|
|
.where(Blob.digest == _ensure_sha256_header(digest))]
|
2017-03-23 01:51:28 +00:00
|
|
|
|
|
|
|
|
2018-05-24 21:54:51 +00:00
|
|
|
def ensure_blob_locations(models_ref, *names):
|
|
|
|
BlobPlacementLocation = models_ref.BlobPlacementLocation
|
|
|
|
|
2017-03-23 01:51:28 +00:00
|
|
|
with db_transaction():
|
|
|
|
locations = BlobPlacementLocation.select().where(BlobPlacementLocation.name << names)
|
|
|
|
|
|
|
|
insert_names = list(names)
|
|
|
|
|
|
|
|
for location in locations:
|
|
|
|
insert_names.remove(location.name)
|
|
|
|
|
|
|
|
if not insert_names:
|
|
|
|
return
|
|
|
|
|
|
|
|
data = [{'name': name} for name in insert_names]
|
|
|
|
BlobPlacementLocation.insert_many(data).execute()
|