def update(self, doc, process=None):
        if process is None:
            pk = 'id'
            id = doc.id
            doc.synced = False
            set = doc
        elif process == UPDATE_PROCESS:
            if self.db.docs.find({'sid': doc.sid}).count() == 0:
                return self.insert(doc, synced=True)
            pk = 'sid'
            id = doc.sid
            doc.synced = not doc.merged() # must be commited if was merged
            doc.merged(False)
            set = {'tid': doc.tid, 'data': doc.data, 'user': doc.user,
                   'public': doc.public, 'synced': doc.synced}
        elif process == COMMIT_PROCESS:
            pk = 'id'
            id = doc.id
            doc.synced = True
            set = {'sid': doc.sid, 'tid': doc.tid, 'synced': doc.synced}
        else:
            raise DatabaseException('Incorrect update process')

        try:
            rs = self.db.docs.update({pk: id}, {'$set': set}, safe=True)
            assert rs['n'] == 1
        except Exception as e:
            raise DatabaseException(e)
        return doc
def reset_interactive_output_stream(
        cls, interactive_output_stream, override_faulthandler_destination=True
    ):
        """Class state:

        - Overwrites `cls._interactive_output_stream`.
        OS state:
        - Overwrites the SIGUSR2 handler.

        This method registers a SIGUSR2 handler, which permits a non-fatal `kill -31 <pants pid>` for
        stacktrace retrieval. This is also where the the error message on fatal exit will be printed to.
        """
        try:
            # NB: mutate process-global state!
            # This permits a non-fatal `kill -31 <pants pid>` for stacktrace retrieval.
            if override_faulthandler_destination:
                faulthandler.register(
                    signal.SIGUSR2, interactive_output_stream, all_threads=True, chain=False
                )
            # NB: mutate the class variables!
            cls._interactive_output_stream = interactive_output_stream
        except ValueError:
            # Warn about "ValueError: IO on closed file" when the stream is closed.
            cls.log_exception(
                "Cannot reset interactive_output_stream -- stream (probably stderr) is closed"
            )