data.oci_model: init with app methods
This commit is contained in:
parent
3ccf3c5f33
commit
9f684fa73f
8 changed files with 464 additions and 0 deletions
54
data/oci_model/blob.py
Normal file
54
data/oci_model/blob.py
Normal file
|
@ -0,0 +1,54 @@
|
|||
from peewee import IntegrityError
|
||||
|
||||
from data.model import db_transaction
|
||||
from data.database import Blob, BlobPlacementLocation, BlobPlacement
|
||||
|
||||
|
||||
def get_blob(digest):
|
||||
""" Find a blob by its digest. """
|
||||
return Blob.select().where(Blob.digest == digest).get()
|
||||
|
||||
|
||||
def get_or_create_blob(digest, size, media_type_name, locations):
|
||||
""" Try to find a blob by its digest or create it. """
|
||||
with db_transaction():
|
||||
try:
|
||||
blob = get_blob(digest)
|
||||
except Blob.DoesNotExist:
|
||||
blob = Blob.create(digest=digest,
|
||||
media_type_id=Blob.media_type.get_id(media_type_name),
|
||||
size=size)
|
||||
for location_name in locations:
|
||||
location_id = BlobPlacement.location.get_id(location_name)
|
||||
try:
|
||||
BlobPlacement.create(blob=blob, location=location_id)
|
||||
except IntegrityError:
|
||||
pass
|
||||
|
||||
return blob
|
||||
|
||||
|
||||
def get_blob_locations(digest):
|
||||
""" Find all locations names for a blob. """
|
||||
return [x.name for x in
|
||||
BlobPlacementLocation
|
||||
.select()
|
||||
.join(BlobPlacement)
|
||||
.join(Blob)
|
||||
.where(Blob.digest == digest)]
|
||||
|
||||
|
||||
def ensure_blob_locations(*names):
|
||||
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()
|
Reference in a new issue