MCPcopy
hub / github.com/hyperopt/hyperopt / set_attachment

Method set_attachment

hyperopt/mongoexp.py:583–630  ·  view source on GitHub ↗

Attach potentially large data string `blob` to `doc` by name `name` blob must be a string doc must have been saved in some collection (must have an _id), but not necessarily the jobs collection. name must be a string Returns None

(self, doc, blob, name, collection=None)

Source from the content-addressed store, hash-verified

581 return list(map(as_str, doc.get("_attachments", [])))
582
583 def set_attachment(self, doc, blob, name, collection=None):
584 """Attach potentially large data string `blob` to `doc` by name `name`
585
586 blob must be a string
587
588 doc must have been saved in some collection (must have an _id), but not
589 necessarily the jobs collection.
590
591 name must be a string
592
593 Returns None
594 """
595
596 # If there is already a file with the given name for this doc, then we will delete it
597 # after writing the new file
598 attachments = doc.get("_attachments", [])
599 name_matches = [a for a in attachments if a[0] == name]
600
601 # the filename is set to something so that fs.list() will display the file
602 new_file_id = self.gfs.put(blob, filename="{}_{}".format(doc["_id"], name))
603 logger.info(
604 "stored blob of %i bytes with id=%s and filename %s_%s"
605 % (len(blob), str(new_file_id), doc["_id"], name)
606 )
607
608 new_attachments = [a for a in attachments if a[0] != name] + [
609 (name, new_file_id)
610 ]
611
612 try:
613 ii = 0
614 doc = self.update(
615 doc, {"_attachments": new_attachments}, collection=collection
616 )
617 # there is a database leak until we actually delete the files that
618 # are no longer pointed to by new_attachments
619 while ii < len(name_matches):
620 self.gfs.delete(name_matches[ii][1])
621 ii += 1
622 except:
623 while ii < len(name_matches):
624 logger.warning(
625 "Leak during set_attachment: old_file_id=%s" % (name_matches[ii][1])
626 )
627 ii += 1
628 raise
629 assert len([n for n in self.attachment_names(doc) if n == name]) == 1
630 # return new_file_id
631
632 def get_attachment(self, doc, name):
633 """Retrieve data attached to `doc` by `attach_blob`.

Callers 1

__setitem__Method · 0.45

Calls 5

updateMethod · 0.95
attachment_namesMethod · 0.95
getMethod · 0.80
infoMethod · 0.80
deleteMethod · 0.80

Tested by

no test coverage detected