Example #1
0
    def delete_orphans_by_id(content_unit_list, flush=True):
        """
        Delete the given orphaned content units.

        Each content unit in the content unit list must be a mapping object with
        the fields `content_type_id` and `unit_id` present.

        NOTE: `flush` should not be set to False unless you know what you're doing

        :param content_unit_list: list of orphaned content units to delete
        :type content_unit_list: iterable of mapping objects
        :param flush: flush the database updates to disk on completion
        :type flush: bool
        """

        content_units_by_content_type = {}

        for content_unit in content_unit_list:
            if 'content_type_id' not in content_unit or 'unit_id' not in content_unit:
                raise pulp_exceptions.InvalidValue(['content_type_id', 'unit_id'])

            content_unit_id_list = content_units_by_content_type.setdefault(
                content_unit['content_type_id'], [])
            content_unit_id_list.append(content_unit['unit_id'])

        for content_type_id, content_unit_id_list in content_units_by_content_type.items():
            OrphanManager.delete_orphans_by_type(content_type_id, content_unit_id_list, flush=False)

        if flush:
            db_connection.flush_database()
Example #2
0
    def delete_orphans_by_type(self, content_type_id, content_unit_ids=None, flush=True):
        """
        Delete the orphaned content units for the given content type.

        If the content_unit_ids parameter is not None, is acts as a filter of
        the specific orphaned content units that may be deleted.

        NOTE: this method deletes the content unit's bits from disk, if applicable.
        NOTE: `flush` should not be set to False unless you know what you're doing

        :param content_type_id: id of the content type
        :type content_type_id: basestring
        :param content_unit_ids: list of content unit ids to delete; None means delete them all
        :type content_unit_ids: iterable or None
        :param flush: flush the database updates to disk on completion
        :type flush: bool
        """

        content_units_collection = content_types_db.type_units_collection(content_type_id)

        for content_unit in self.generate_orphans_by_type(content_type_id, fields=['_id', '_storage_path']):

            if content_unit_ids is not None and content_unit['_id'] not in content_unit_ids:
                continue

            content_units_collection.remove(content_unit['_id'], safe=False)

            storage_path = content_unit.get('_storage_path', None)
            if storage_path is not None:
                self.delete_orphaned_file(storage_path)

        # this forces the database to flush any cached changes to the disk
        # in the background; for example: the unsafe deletes in the loop above
        if flush:
            db_connection.flush_database()
Example #3
0
    def delete_all_orphans(flush=True):
        """
        Delete all orphaned content units.

        NOTE: `flush` should not be set to False unless you know what you're doing

        :param flush: flush the database updates to disk on completion
        :type flush: bool
        """

        for content_type_id in content_types_db.all_type_ids():
            OrphanManager.delete_orphans_by_type(content_type_id, flush=False)

        if flush:
            db_connection.flush_database()
Example #4
0
    def delete_orphans_by_type(self,
                               content_type_id,
                               content_unit_ids=None,
                               flush=True):
        """
        Delete the orphaned content units for the given content type.

        If the content_unit_ids parameter is not None, is acts as a filter of
        the specific orphaned content units that may be deleted.

        NOTE: this method deletes the content unit's bits from disk, if applicable.
        NOTE: `flush` should not be set to False unless you know what you're doing

        :param content_type_id: id of the content type
        :type content_type_id: basestring
        :param content_unit_ids: list of content unit ids to delete; None means delete them all
        :type content_unit_ids: iterable or None
        :param flush: flush the database updates to disk on completion
        :type flush: bool
        """

        content_units_collection = content_types_db.type_units_collection(
            content_type_id)

        for content_unit in self.generate_orphans_by_type(
                content_type_id, fields=['_id', '_storage_path']):

            if content_unit_ids is not None and content_unit[
                    '_id'] not in content_unit_ids:
                continue

            content_units_collection.remove(content_unit['_id'], safe=False)

            storage_path = content_unit.get('_storage_path', None)
            if storage_path is not None:
                self.delete_orphaned_file(storage_path)

        # this forces the database to flush any cached changes to the disk
        # in the background; for example: the unsafe deletes in the loop above
        if flush:
            db_connection.flush_database()