def get_revisions(pairs, revisions, filters=None): """Retrieve revision ids for pairs If revisions dictionary is provided it will validate that the selected revision exists in the objects revision history. Args: pairs: set([(parent_1, child_1), (parent_2, child_2), ...]) revisions: dict({(parent, child): revision_id, ...}) filters: predicate """ with benchmark("snapshotter.helpers.get_revisions"): revision_id_cache = dict() if pairs: with benchmark("get_revisions.create caches"): child_stubs = {pair.child for pair in pairs} with benchmark("get_revisions.create child -> parents cache"): parents_cache = collections.defaultdict(set) for parent, child in pairs: parents_cache[child].add(parent) with benchmark("get_revisions.retrieve revisions"): query = db.session.query( models.Revision.id, models.Revision.resource_type, models.Revision.resource_id).filter( tuple_(models.Revision.resource_type, models.Revision.resource_id).in_( child_stubs)).order_by( models.Revision.id.desc()) if filters: for _filter in filters: query = query.filter(_filter) with benchmark("get_revisions.create revision_id cache"): for revid, restype, resid in query: child = Stub(restype, resid) for parent in parents_cache[child]: key = Pair(parent, child) if key in revisions: if revid == revisions[key]: revision_id_cache[key] = revid else: logger.warning( "Specified revision for object %s but couldn't find the" "revision '%s' in object history", key, revisions[key]) else: if key not in revision_id_cache: revision_id_cache[key] = revid return revision_id_cache
def get_revisions(pairs, revisions, filters=None): """Retrieve revision ids for pairs Args: pairs: set([(parent_1, child_1), (parent_2, child_2), ...]) revisions: dict({(parent, child): revision_id, ...}) filters: predicate """ with benchmark("snapshotter.helpers.get_revisions"): revision_id_cache = dict() if pairs: with benchmark("get_revisions.create caches"): child_stubs = {pair.child for pair in pairs} with benchmark("get_revisions.create child -> parents cache"): parents_cache = collections.defaultdict(set) for parent, child in pairs: parents_cache[child].add(parent) with benchmark("get_revisions.retrieve revisions"): query = db.session.query( models.Revision.id, models.Revision.resource_type, models.Revision.resource_id).filter( tuple_(models.Revision.resource_type, models.Revision.resource_id).in_( child_stubs)).order_by( models.Revision.id.desc()) if filters: for _filter in filters: query = query.filter(_filter) with benchmark("get_revisions.create revision_id cache"): for revid, restype, resid in query: child = Stub(restype, resid) for parent in parents_cache[child]: key = Pair(parent, child) if key in revisions: if revid == revisions[key]: revision_id_cache[key] = revid else: if key not in revision_id_cache: revision_id_cache[key] = revid return revision_id_cache
def get_revisions(pairs, revisions, filters=None): """Retrieve revision ids for pairs If revisions dictionary is provided it will validate that the selected revision exists in the objects revision history. Args: pairs: set([(parent_1, child_1), (parent_2, child_2), ...]) revisions: dict({(parent, child): revision_id, ...}) filters: predicate """ with benchmark("snapshotter.helpers.get_revisions"): if not pairs: return {} with benchmark("get_revisions.create child -> parents cache"): parents_cache = collections.defaultdict(set) child_stubs = set() for parent, child in pairs: parents_cache[child].add(parent) child_stubs.add(child) with benchmark("get_revisions.retrieve revisions"): query = get_revisions_query(child_stubs, revisions, filters) revision_id_cache = {} with benchmark("get_revisions.create revision_id cache"): for revid, restype, resid in query: child = Stub(restype, resid) for parent in parents_cache[child]: key = Pair(parent, child) if key in revisions and revisions[key] != revid: logger.warning( "Specified revision for object %s but couldn't find the" "revision '%s' in object history", key, revisions[key], ) else: revision_id_cache[key] = revid return revision_id_cache
def analyze(self): """Analyze which snapshots need to be updated and which created""" query = set(db.session.query( models.Snapshot.parent_type, models.Snapshot.parent_id, models.Snapshot.child_type, models.Snapshot.child_id, ).filter(tuple_( models.Snapshot.parent_type, models.Snapshot.parent_id ).in_(self.parents))) existing_scope = {Pair.from_4tuple(fields) for fields in query} full_scope = {Pair(parent, child) for parent, children in self.snapshots.items() for child in children} for_update = existing_scope for_create = full_scope - existing_scope return for_create, for_update