Esempio n. 1
0
    def add_descriptor(self, descriptor: DescriptorElement) -> None:
        """
        Add a descriptor to this set.

        Adding the same descriptor multiple times should not add multiple copies
        of the descriptor in the set (based on UUID). Added descriptors
        overwrite set descriptors based on UUID.

        :param descriptor: Descriptor to set.
        """
        if self.read_only:
            raise ReadOnlyError("Cannot clear a read-only set.")

        q = self.UPSERT_TMPL.format(
            table_name=self.table_name,
            uuid_col=self.uuid_col,
            element_col=self.element_col,
        )
        v = {
            'uuid_val':
            str(descriptor.uuid()),
            'element_val':
            psycopg2.Binary(pickle.dumps(descriptor, self.pickle_protocol))
        }

        def exec_hook(cur: psycopg2.extensions.cursor) -> None:
            cur.execute(q, v)

        list(self.psql_helper.single_execute(exec_hook))
Esempio n. 2
0
 def _doc_for_code_descr(self, d: DescriptorElement) -> Dict[str, Any]:
     """
     Generate standard identifying document base for the given
     descriptor element.
     """
     uuid = d.uuid()
     return {
         'id': '-'.join([self.set_uuid, str(uuid)]),
         self.set_uuid_field: self.set_uuid,
         self.d_uid_field: uuid,
     }
Esempio n. 3
0
    def _inner_add_descriptor(
        self,
        descriptor: DescriptorElement,
        no_cache: bool = False
    ) -> None:
        """
        Internal adder with the additional option to trigger caching or not.

        :param descriptor: Descriptor to index.
        :param no_cache: Do not cache the internal table if a file cache was
            provided. This would be used if adding many descriptors at a time,
            preventing a file write for every individual descriptor added.
        """
        self._table[descriptor.uuid()] = descriptor
        if not no_cache:
            self.cache_table()
Esempio n. 4
0
 def __contains__(self, item: DescriptorElement) -> bool:
     if isinstance(item, DescriptorElement):
         # Testing for UUID inclusion since element hash based on UUID
         # value.
         return self.has_descriptor(item.uuid())
     return False