async def get_path(goblin_app, source_label, source_id, target_label,
                   target_id):
    session = await goblin_app.session()
    traversal = (session.g.V().has(
        source_label, f"{source_label}_id",
        source_id).repeat(__.bothE().otherV().simplePath()).until(
            __.has(
                target_label,
                f"{target_label}_id", target_id).or_().loops().is_(6)).has(
                    target_label, f"{target_label}_id",
                    target_id).limit(1).path().index().map(__.unfold().filter(
                        __.unfold().tail().math("_ % 2").is_(1)).limit(
                            Scope.local,
                            1).project("source", "edge", "target").by(
                                project_vertex(__.outV())).by(
                                    __.project("id", "label", "values").by(
                                        __.id()).by(__.label()).by(
                                            __.valueMap())).by(
                                                project_vertex(
                                                    __.inV())).fold()))
    result = await traversal.toList()
    if not result:
        return None
    for path in result:
        for entry in path:
            cleanup_vertex(entry["source"], goblin_app)
            cleanup_vertex(entry["target"], goblin_app)
            cleanup_edge(entry["edge"])
    return result
Exemplo n.º 2
0
def get_locality_vertex_projection():
    return (__.cap("vertices").unfold().dedup().project(
        "id", "kind", "label", "values", "total_edge_count",
        "child_count").by(__.id()).by(__.constant("vertex")).by(__.label()).by(
            __.valueMap()).by(__.bothE().count()).by(
                __.inE("relationship").has(
                    "name",
                    P.within("Member Of", "Subsidiary Of",
                             "Subrelease Of")).count()))
def project_vertex(traversal, **extra_traversals):
    traversal = (traversal.project(
        "id",
        "label",
        "values",
        "total_edge_count",
        "child_count",
        "extra",
        "in_roles",
        "out_roles",
        *sorted(extra_traversals.keys()),
    ).by(__.id()).by(__.label()).by(__.valueMap()).by(__.bothE().count()).by(
        make_child_count_traversal()).by(make_track_extras_traversal()).by(
            __.inE("relationship").groupCount().by("name")).by(
                __.outE("relationship").groupCount().by("name")))
    for _, extra_traversal in sorted(extra_traversals.items()):
        traversal = traversal.by(extra_traversal)
    return traversal
Exemplo n.º 4
0
    async def _get_vertex_properties(self, vid, label):
        projection = self._g.V(vid).properties() \
                            .project('id', 'key', 'value', 'meta') \
                            .by(__.id()).by(__.key()).by(__.value()) \
                            .by(__.valueMap())
        props = await projection.toList()
        new_props = {'label': label, 'id': vid}
        for prop in props:
            key = prop['key']
            val = prop['value']
            # print('val_type', type(val))
            meta = prop['meta']
            new_props.setdefault(key, [])
            if meta:
                meta['key'] = key
                meta['value'] = val
                meta['id'] = prop['id']
                val = meta

            new_props[key].append(val)
        return new_props
Exemplo n.º 5
0
    async def _get_vertex_properties(self, vid, label):
        projection = self._g.V(vid).properties() \
                            .project('id', 'key', 'value', 'meta') \
                            .by(__.id()).by(__.key()).by(__.value()) \
                            .by(__.valueMap())
        props = await projection.toList()
        new_props = {'label': label, 'id': vid}
        for prop in props:
            key = prop['key']
            val = prop['value']
            # print('val_type', type(val))
            meta = prop['meta']
            new_props.setdefault(key, [])
            if meta:
                meta['key'] = key
                meta['value'] = val
                meta['id'] = prop['id']
                val = meta

            new_props[key].append(val)
        return new_props
async def upsert_edge(
    session, *, name, source, target, primacy=0, source_label=0, target_label=0
) -> Optional[Tuple[int, int]]:
    for attempt in range(10):
        traversal = session.g
        if isinstance(source, int):
            traversal = traversal.V(source)
        else:
            traversal = traversal.V().has(source[0], f"{source[0]}_id", source[1])
        traversal = traversal.as_("source")
        if isinstance(target, int):
            traversal = traversal.V(target)
        else:
            traversal = traversal.V().has(target[0], f"{target[0]}_id", target[1])
        traversal = traversal.as_("target")
        traversal = (
            traversal
            # If source or target do not yield traversers, it will not make it here
            .addE("relationship")
            .from_("source")
            .to("target")
            .property("last_modified", datetime.datetime.now())
            .property("name", name)
            .property("primacy", primacy)
            .select("source", "target")
            .by(__.id())
        )
        try:
            result = await traversal.next()
            if not result:
                return None
            return result["source"], result["target"]
        except GremlinServerError as e:
            logger.error(f"Backing off: {e!s}\n{traceback.format_exc()}")
            await backoff(attempt)
    return None
Exemplo n.º 7
0
def get_locality_edge_projection():
    return (__.cap("filteredEdges").unfold().project(
        "id", "kind", "label", "source", "target",
        "values").by(__.id()).by(__.constant("edge")).by(__.label()).by(
            __.outV().id()).by(__.inV().id()).by(__.valueMap()))