Example #1
0
    def merge(self, graph, merge_properties=None, batch_size=None):
        """
        Merge nodes from NodeSet on merge properties.

        :param merge_properties: The merge properties.
        """
        log.debug('Merge NodeSet on {}'.format(merge_properties))

        if not batch_size:
            batch_size = self.batch_size

        if not merge_properties:
            merge_properties = self.merge_keys

        log.debug('Batch Size: {}'.format(batch_size))

        query = nodes_merge_unwind(self.labels, merge_properties)
        log.debug(query)

        i = 1
        for batch in chunks(self.node_properties(), size=batch_size):
            batch = list(batch)
            log.debug('Batch {}'.format(i))
            log.debug(batch[0])

            graph.run(query, props=batch)
            i += 1
Example #2
0
    def test_query_multiple_labels(self):
        test_labels = ['Foo', 'Bar']

        query = nodes_merge_unwind(test_labels, ['sid'])

        expected_query = """UNWIND $props AS properties
MERGE (n:Foo:Bar { sid: properties.sid } )
ON CREATE SET n = properties
ON MATCH SET n += properties"""

        assert query == expected_query
Example #3
0
    def test_query_single_label(self):
        test_label = ['Foo']

        query = nodes_merge_unwind(test_label, ['sid'])

        expected_query = """UNWIND $props AS properties
MERGE (n:Foo { sid: properties.sid } )
ON CREATE SET n = properties
ON MATCH SET n += properties"""

        assert query == expected_query
Example #4
0
    def test_nodes_are_created(self, graph, clear_graph):
        query = nodes_merge_unwind(['Foo'], ['testid'])

        graph.run(query, props=[{'testid': 1, 'key': 'newvalue'}])

        results = list(graph.run("MATCH (n:Foo) RETURN n"))

        first_row = results[0]
        first_row_first_element = first_row[0]

        assert len(results) == 1
        assert first_row_first_element['testid'] == 1
        assert first_row_first_element['key'] == 'newvalue'
Example #5
0
    def test_own_param_name(self):
        test_labels = ['Foo', 'Bar']
        merge_props = ['sid', 'other']

        query = nodes_merge_unwind(test_labels,
                                   merge_props,
                                   property_parameter='nodes')

        expected_query = """UNWIND $nodes AS properties
MERGE (n:Foo:Bar { sid: properties.sid, other: properties.other } )
ON CREATE SET n = properties
ON MATCH SET n += properties"""

        assert query == expected_query
Example #6
0
    def merge(self,
              graph,
              merge_properties=None,
              batch_size=None,
              raise_on_result_count_deviation=False):
        """
        Merge nodes from NodeSet on merge properties.

        :param merge_properties: The merge properties.

        :raise_on_result_count_deviation: boolean. Raise if less nodes were processed on DB side as sended with the query. This can happen in parallel processing environments. set Nodeset.failed_batch_handler(error,query,batch) to catch single failed batches
        """
        log.debug('Merge NodeSet on {}'.format(merge_properties))

        if not batch_size:
            batch_size = self.batch_size

        if not merge_properties:
            merge_properties = self.merge_keys

        log.debug('Batch Size: {}'.format(batch_size))

        query = nodes_merge_unwind(self.labels, merge_properties)
        log.debug(query)
        i = 1
        for batch in chunks(self.node_properties(), size=batch_size):
            batch = list(batch)
            log.debug('Batch {}'.format(i))
            log.debug(batch[0])
            try:
                tx = graph.begin()
                tx.run(query, props=batch)
                result = tx.run(query, props=batch)
                tx.commit()
                count = result.data()[0]["cnt"]
                if raise_on_result_count_deviation and count < len(batch):
                    raise MissingNodesEx(
                        "Excepted {} Nodes to be inserted, got {}", len(batch),
                        count)
            except Exception as e:
                if self.failed_batch_handler is not None:
                    self.failed_batch_handler(self, e, query, batch)
                else:
                    raise
            i += 1
Example #7
0
    def test_nodes_are_merged(self, graph, clear_graph):
        graph.run(
            "CREATE (n:Foo) SET n.testid = 1, n.key = 'value', n.other = 'other_value'"
        )

        query = nodes_merge_unwind(['Foo'], ['testid'])

        graph.run(query, props=[{'testid': 1, 'key': 'newvalue'}])

        results = list(graph.run("MATCH (n:Foo) RETURN n"))

        first_row = results[0]
        first_row_first_element = first_row[0]

        assert len(results) == 1
        assert first_row_first_element['testid'] == 1
        assert first_row_first_element['key'] == 'newvalue'
        # assert other value did not change
        assert first_row_first_element['other'] == 'other_value'