def commit_file(self, fobj, digest, desc=""): """See FileCacherBackend.commit_file(). """ fobj.close() try: with SessionGen() as session: fso = FSObject(description=desc) fso.digest = digest fso.loid = fobj.loid session.add(fso) session.commit() logger.info("File %s (%s) stored on the database.", digest, desc) except IntegrityError: # If someone beat us to adding the same object to the database, we # should at least drop the large object. LargeObject.unlink(fobj.loid) logger.warning("File %s (%s) caused an IntegrityError, ignoring.", digest, desc) return False return True
def create_file(self, digest): """See FileCacherBackend.create_file(). """ with SessionGen() as session: fso = FSObject.get_from_digest(digest, session) # Check digest uniqueness if fso is not None: logger.debug("File %s already stored on database, not " "sending it again.", digest) session.rollback() return None # If it is not already present, copy the file into the # lobject else: # Create the large object first. This should be populated # and committed before putting it into the FSObjects table. return LargeObject(0, mode='wb')