Esempio n. 1
0
    def get_bundle(self, bundle_id):
        bundle_id = str(bundle_id)
        session = self._create_session()
        records = list()
        result_set = session.run(NEO4J_GET_BUNDLE_RETURN_NODES_RELATIONS,
                                 {"bundle_id": bundle_id})
        for result in result_set:
            record = result["re"]

            if record is None:
                raise DatabaseException("Record response should not be None")
            relation_record = self._split_attributes_metadata_from_node(record)
            records.append(relation_record)

        # Get bundle node and set identifier if there is a bundle node.
        bundle_node_result = session.run(NEO4j_GET_BUNDLE_RETURN_BUNDLE_NODE,
                                         {"bundle_id": bundle_id})

        raw_record = None
        for bundle in bundle_node_result:
            raw_record = self._split_attributes_metadata_from_node(bundle["b"])

        if raw_record is None and len(records) == 0:
            raise NotFoundException(
                "bundle with the id {} was not found ".format(bundle_id))

        bundle = namedtuple('Bundle', 'records, bundle_record')

        return bundle(records, raw_record)
Esempio n. 2
0
    def get_relation(self, relation_id):
        """
        Get a relation

        :param relation_id:
        :return: The relation
        :rtype: DbRelation
        """

        session = self._create_session()
        result_set = session.run(
            cypher_commands.NEO4J_GET_RELATION_RETURN_NODE,
            {"relation_id": int(relation_id)})

        relation = None
        for result in result_set:
            if not isinstance(result["relation"], Relationship):
                raise DatabaseException(
                    " should return only relationship {}, command {}".format(
                        relation_id,
                        cypher_commands.NEO4J_GET_RECORD_RETURN_NODE))

            relation = result["relation"]

        if relation is None:
            raise NotFoundException(
                "We cant find the relation with the id: {}, database command {}"
                .format(relation_id,
                        cypher_commands.NEO4J_GET_RECORD_RETURN_NODE))

        return self._split_attributes_metadata_from_node(relation)
Esempio n. 3
0
    def get_record(self, record_id):
        """
        Try to find the record in the database

        :param record_id:
        :return: DbRecord
        :rtype: DbRecord
        """

        session = self._create_session()
        result_set = session.run(cypher_commands.NEO4J_GET_RECORD_RETURN_NODE,
                                 {"record_id": int(record_id)})

        node = None
        for result in result_set:
            if node is not None:
                raise DatabaseException(
                    "get_record should return only one node for the id {}, command {}"
                    .format(record_id,
                            cypher_commands.NEO4J_GET_RECORD_RETURN_NODE))
            node = result["node"]

        if node is None:
            raise NotFoundException(
                "We cant find the node with the id: {}, database command {}".
                format(record_id,
                       cypher_commands.NEO4J_GET_RECORD_RETURN_NODE))

        return self._split_attributes_metadata_from_node(node)
    def delete_bundle(self, bundle_id):
        bundle = self.bundles.get(bundle_id)
        if bundle is None:
            raise NotFoundException()

        for record_id in bundle.records:
            record = self.all_records.get(record_id)

            if record is None:
                raise NotFoundException()

            del self.all_records[record_id]
            del record

        del self.bundles[bundle_id]
        del bundle
        return True
    def delete_document(self, document_id):
        bundle_ids = self.document_bundle_ids.get(document_id)

        if bundle_ids is None:
            raise NotFoundException()

        self.delete_bundle(document_id)
        for bundle_id in bundle_ids:
            self.delete_bundle(bundle_id)

        return True
    def get_bundle(self, bundle_id):
        bundle = self.bundles.get(bundle_id)
        if bundle is None:
            raise NotFoundException()

        records = list()
        for record_id in bundle.records:
            record = self.all_records.get(record_id)
            records.append(record)

        return DbBundle(records, bundle.bundle_record)
Esempio n. 7
0
    def delete_record(self, record_id):
        """
        Delete a single record

        :param record_id: The node id
        :type record_id: str
        :return: Result of the delete operation
        :rtype: Bool
        """

        if record_id not in self.all_nodes:
            raise NotFoundException()

        del self.all_nodes[record_id]

        return True
Esempio n. 8
0
    def get_record(self, record_id):
        """
        Get a ProvDocument from the database based on the document id

        :param record_id: The id of the node
        :type record_id: str
        :return: A named tuple with (attributes, metadata)
        :rtype: DbRecord
        """
        if record_id not in self.all_nodes:
            raise NotFoundException()

        (attributes, metadata) = self.all_nodes.get(record_id)
        attributes = encode_dict_values_to_primitive(attributes)
        metadata = encode_dict_values_to_primitive(metadata)
        db_record = DbRecord(attributes, metadata)

        return db_record
    def delete_record(self, record_id):
        for bundle_id, bundle in self.bundles.items():
            relation_index = -1
            try:
                relation_index = bundle.records.index(record_id)
            except ValueError as e:
                pass

            if relation_index is not -1:
                del bundle.records[relation_index]

        record = self.all_records.get(record_id)

        if record is None:
            raise NotFoundException()

        del self.all_records[record_id]

        return True
Esempio n. 10
0
    def get_relation(self, relation_id):
        """
        Return the relation behind the relation_id

        :param relation_id: The id of the relation
        :type relation_id: str
        :return: The namedtuple with (attributes, metadata)
        :rtype: DbRelation
        """

        for (from_uri, relations) in self.all_relations.items():
            if relation_id in relations:
                (to_uri, attributes, metadata) = relations[relation_id]

                attributes = encode_dict_values_to_primitive(attributes)
                metadata = encode_dict_values_to_primitive(metadata)

                return DbRelation(attributes, metadata)
        raise NotFoundException(
            "could't find the relation with id {}".format(relation_id))
Esempio n. 11
0
    def delete_records_by_filter(self,
                                 attributes_dict=None,
                                 metadata_dict=None):
        """
        Delete a set of records based on filter conditions

        :param attributes_dict: A filter dict with a conjunction of all values in the attributes_dict and metadata_dict
        :type attributes_dict: dict
        :param metadata_dict: A filter for the metadata with a conjunction of all values (also in the attributes_dict )
        :type metadata_dict: dict
        :return: The result of the operation
        :rtype: Bool
        """
        if attributes_dict is None:
            attributes_dict = dict()
        if metadata_dict is None:
            metadata_dict = dict()

        # erase all if no filter set
        if len(attributes_dict) == 0 and len(metadata_dict) == 0:
            del self.all_nodes
            self.all_nodes = dict()
            return True

        # erase only matching nodes
        records_to_delete = self.get_records_by_filter(attributes_dict,
                                                       metadata_dict)

        for record in records_to_delete:
            if not isinstance(record, DbRecord):
                continue
            identifier = record.metadata[METADATA_KEY_IDENTIFIER]

            if identifier not in self.all_nodes:
                raise NotFoundException("We cant find the id ")
            del self.all_nodes[identifier]

        return True
    def save_relation(self, from_bundle_id, from_node, to_bundle_id, to_node,
                      attributes, metadata):
        # save all relation information and return the relation id as string

        from_bundle = self.bundles.get(from_bundle_id)
        to_bundle = self.bundles.get(to_bundle_id)

        if from_bundle is None or to_bundle is None:
            raise NotFoundException()

        new_rel_id = str(uuid4())

        from_bundle.records.append(new_rel_id)

        # transform the attributes and metadata to primitive data types
        attr = encode_dict_values_to_primitive(attributes)
        meta = encode_dict_values_to_primitive(metadata)

        db_relations = DbRelation(attr, meta)

        self.all_records.update({new_rel_id: db_relations})

        return new_rel_id
 def get_record(self, record_id):
     db_record = self.all_records.get(record_id)
     if db_record is None:
         raise NotFoundException()
     return db_record