Merge "db: Migrate "image volume cache" APIs to enginefacade"

This commit is contained in:
Zuul 2022-06-07 12:07:54 +00:00 committed by Gerrit Code Review
commit 781d29e822

View File

@ -8445,74 +8445,92 @@ CALCULATE_COUNT_HELPERS = {
@require_context @require_context
def image_volume_cache_create(context, host, cluster_name, image_id, @main_context_manager.writer
image_updated_at, volume_id, size): def image_volume_cache_create(
session = get_session() context,
with session.begin(): host,
cache_entry = models.ImageVolumeCacheEntry() cluster_name,
cache_entry.host = host image_id,
cache_entry.cluster_name = cluster_name image_updated_at,
cache_entry.image_id = image_id volume_id,
cache_entry.image_updated_at = image_updated_at size,
cache_entry.volume_id = volume_id ):
cache_entry.size = size cache_entry = models.ImageVolumeCacheEntry()
session.add(cache_entry) cache_entry.host = host
return cache_entry cache_entry.cluster_name = cluster_name
cache_entry.image_id = image_id
cache_entry.image_updated_at = image_updated_at
cache_entry.volume_id = volume_id
cache_entry.size = size
context.session.add(cache_entry)
return cache_entry
@require_context @require_context
@main_context_manager.writer
def image_volume_cache_delete(context, volume_id): def image_volume_cache_delete(context, volume_id):
session = get_session() context.session.query(
with session.begin(): models.ImageVolumeCacheEntry,
session.query(models.ImageVolumeCacheEntry).\ ).filter_by(volume_id=volume_id).delete()
filter_by(volume_id=volume_id).\
delete()
@require_context @require_context
@main_context_manager.writer
def image_volume_cache_get_and_update_last_used(context, image_id, **filters): def image_volume_cache_get_and_update_last_used(context, image_id, **filters):
filters = _clean_filters(filters) filters = _clean_filters(filters)
session = get_session() entry = (
with session.begin(): context.session.query(models.ImageVolumeCacheEntry)
entry = session.query(models.ImageVolumeCacheEntry).\ .filter_by(image_id=image_id)
filter_by(image_id=image_id).\ .filter_by(**filters)
filter_by(**filters).\ .order_by(desc(models.ImageVolumeCacheEntry.last_used))
order_by(desc(models.ImageVolumeCacheEntry.last_used)).\ .first()
first() )
if entry: if entry:
entry.last_used = timeutils.utcnow() entry.last_used = timeutils.utcnow()
entry.save(session=session) entry.save(context.session)
return entry return entry
@require_context @require_context
@main_context_manager.reader
def image_volume_cache_get_by_volume_id(context, volume_id): def image_volume_cache_get_by_volume_id(context, volume_id):
session = get_session() return (
with session.begin(): context.session.query(models.ImageVolumeCacheEntry)
return session.query(models.ImageVolumeCacheEntry).\ .filter_by(volume_id=volume_id)
filter_by(volume_id=volume_id).\ .first()
first() )
@require_context @require_context
@main_context_manager.reader
def image_volume_cache_get_all(context, **filters): def image_volume_cache_get_all(context, **filters):
filters = _clean_filters(filters) filters = _clean_filters(filters)
session = get_session() return (
with session.begin(): context.session.query(models.ImageVolumeCacheEntry)
return session.query(models.ImageVolumeCacheEntry).\ .filter_by(**filters)
filter_by(**filters).\ .order_by(desc(models.ImageVolumeCacheEntry.last_used))
order_by(desc(models.ImageVolumeCacheEntry.last_used)).\ .all()
all() )
@require_admin_context @require_admin_context
def image_volume_cache_include_in_cluster(context, cluster, @main_context_manager.writer
partial_rename=True, **filters): def image_volume_cache_include_in_cluster(
context,
cluster,
partial_rename=True,
**filters,
):
"""Include all volumes matching the filters into a cluster.""" """Include all volumes matching the filters into a cluster."""
filters = _clean_filters(filters) filters = _clean_filters(filters)
return _include_in_cluster(context, cluster, models.ImageVolumeCacheEntry, return _include_in_cluster(
partial_rename, filters) context,
cluster,
models.ImageVolumeCacheEntry,
partial_rename,
filters,
)
################### ###################