示例#1
0
def query_dgraph_type(client: GraphClient,
                      type_name: str) -> List[QueryPredicateResult]:
    query = f"""
        schema(type: {type_name}) {{ type }}
    """
    txn = client.txn(read_only=True)
    try:
        res = json.loads(txn.query(query).json)
    finally:
        txn.discard()

    if not res:
        return []
    if not res.get("types"):
        return []

    res = res["types"][0]["fields"]
    predicate_names = []
    for pred in res:
        predicate_names.append(pred["name"])

    predicate_metas = []
    for predicate_name in predicate_names:
        predicate_metas.append(query_dgraph_predicate(client, predicate_name))

    return predicate_metas
示例#2
0
def _upsert(client: GraphClient, node_dict: Dict[str, Any]) -> str:
    node_dict["uid"] = "_:blank-0"
    node_key = node_dict["node_key"]
    query = f"""
        {{
            q0(func: eq(node_key, "{node_key}"), first: 1) {{
                    uid,
                    dgraph.type
                    expand(_all_)
            }}
        }}
        """
    txn = client.txn(read_only=False)

    try:
        res = json.loads(txn.query(query).json)["q0"]
        new_uid = None
        if res:
            node_dict["uid"] = res[0]["uid"]
            new_uid = res[0]["uid"]

        mutation = node_dict

        mut_res = txn.mutate(set_obj=mutation, commit_now=True)
        new_uid = node_dict.get("uid") or mut_res.uids["blank-0"]
        return cast(str, new_uid)
    finally:
        txn.discard()
示例#3
0
def query_batch(
    client: GraphClient,
    batch_size: int,
    ttl_cutoff_ms: int,
    last_uid: Optional[str] = None,
) -> List[Dict[str, Union[Dict, str]]]:
    after = "" if last_uid is None else f", after: {last_uid}"
    paging = f"first: {batch_size}{after}"
    query = f"""
    {{
        q(func: le(last_index_time, {ttl_cutoff_ms}), {paging}) {{
            uid,
            expand(_all_) {{ uid }}
        }}
    }}
    """

    txn = client.txn()
    try:
        app.log.debug(f"retrieving batch: {query}")
        batch = txn.query(query)
        app.log.debug(f"retrieved batch: {batch.json}")
        return json.loads(batch.json)["q"]
    finally:
        txn.discard()
示例#4
0
def set_property(client: GraphClient, uid: str, prop_name: str,
                 prop_value: Any) -> None:
    LOGGER.debug(f"Setting property {prop_name} as {prop_value} for {uid}")
    txn = client.txn(read_only=False)

    try:
        mutation = {"uid": uid, prop_name: prop_value}

        txn.mutate(set_obj=mutation, commit_now=True)
    finally:
        txn.discard()
示例#5
0
def delete_nodes(client: GraphClient, nodes: Iterator[str]) -> int:
    del_ = [{"uid": uid} for uid in nodes]

    txn = client.txn()
    try:
        mut = txn.create_mutation(del_obj=del_)
        app.log.debug(f"deleting nodes: {mut}")
        txn.mutate(mutation=mut, commit_now=True)
        app.log.debug(f"deleted nodes: {json.dumps(del_)}")
        return len(del_)
    finally:
        txn.discard()
示例#6
0
def query_dgraph_predicate(client: GraphClient,
                           predicate_name: str) -> QueryPredicateResult:
    query = f"""
        schema(pred: {predicate_name}) {{  }}
    """
    txn = client.txn(read_only=True)
    try:
        res = json.loads(txn.query(query).json)["schema"][0]
    finally:
        txn.discard()

    return res
示例#7
0
def set_score(client: GraphClient,
              uid: str,
              new_score: int,
              txn: Any = None) -> None:
    if not txn:
        txn = client.txn(read_only=False)

    try:
        mutation = {"uid": uid, "score": new_score}

        txn.mutate(set_obj=mutation, commit_now=True)
    finally:
        txn.discard()
示例#8
0
def create_edge(client: GraphClient, from_uid: int, edge_name: str,
                to_uid: int) -> None:
    if edge_name[0] == "~":
        mut = {"uid": to_uid, edge_name[1:]: {"uid": from_uid}}

    else:
        mut = {"uid": from_uid, edge_name: {"uid": to_uid}}

    txn = client.txn(read_only=False)
    try:
        res = txn.mutate(set_obj=mut, commit_now=True)
        LOGGER.debug("edge mutation result is: {}".format(res))
    finally:
        txn.discard()
示例#9
0
def delete_edges(client: GraphClient, edges: Iterator[Tuple[str, str, str]]) -> int:
    del_ = [
        create_edge_obj(src_uid, predicate, dest_uid)
        for src_uid, predicate, dest_uid in edges
    ]

    txn = client.txn()
    try:
        mut = txn.create_mutation(del_obj=del_)
        app.log.debug(f"deleting edges: {mut}")
        txn.mutate(mutation=mut, commit_now=True)
        app.log.debug(f"deleted edges: {json.dumps(del_)}")
        return len(del_)
    finally:
        txn.discard()
示例#10
0
    def query(self, graph_client: GraphClient, first: int) -> List[V]:
        var_alloc, query = gen_query(self, "q0", first=first)

        variables = {v: k for k, v in var_alloc.allocated.items()}
        txn = graph_client.txn(read_only=True)

        with graph_client.txn_context(read_only=True) as txn:
            try:
                qres = json.loads(txn.query(query, variables=variables).json)
            except Exception as e:
                raise QueryFailedException(query, variables) from e

        d = qres.get("q0")
        if d:
            return [
                self.associated_viewable().from_dict(node, graph_client) for node in d
            ]
        return []
示例#11
0
    def get_or_create(gclient: GraphClient, lens_name: str,
                      lens_type: str) -> "LensView":
        eg_txn = gclient.txn(read_only=False)
        try:
            query = """
            query res($a: string)
            {
              res(func: eq(node_key, $a), first: 1) @cascade
               {
                 uid,
                 node_type: dgraph.type,
                 node_key,
               }
             }"""
            res = eg_txn.query(
                query, variables={"$a": "lens-" + lens_type + lens_name})

            res = json.loads(res.json)["res"]
            new_uid = None
            if res:
                new_uid = res[0]["uid"]
            else:
                m_res = eg_txn.mutate(
                    set_obj={
                        "lens": lens_name,
                        "lens_type": lens_type,
                        "node_key": "lens-" + lens_type + lens_name,
                        "dgraph.type": "Lens",
                        "score": 0,
                    },
                    commit_now=True,
                )
                uids = m_res.uids

                new_uid = new_uid or uids["blank-0"]
        finally:
            eg_txn.discard()

        self_lens = LensQuery().with_lens_name(
            eq=lens_name).query_first(gclient)
        assert self_lens, "Lens must exist"
        return self_lens
示例#12
0
def query_dgraph_type(client: GraphClient, type_name: str) -> List[str]:
    query = f"""
        schema(type: {type_name}) {{ }}
    """
    LOGGER.debug(f"query: {query}")
    txn = client.txn(read_only=True)
    try:
        res = json.loads(txn.query(query).json)
        LOGGER.debug(f"res: {res}")
    finally:
        txn.discard()

    pred_names = []

    if "types" in res:
        for field in res["types"][0]["fields"]:
            pred_name = (f"<{field['name']}>"
                         if field["name"].startswith("~") else field["name"])
            pred_names.append(pred_name)

    return pred_names
示例#13
0
def get_uid(client: GraphClient, node_key: str) -> str:
    txn = client.txn(read_only=True)
    try:
        query = """
            query res($a: string)
            {
              res(func: eq(node_key, $a), first: 1) @cascade
               {
                 uid,
               }
             }"""
        res = txn.query(query, variables={"$a": node_key})
        res = json.loads(res.json)

        if isinstance(res["res"], list):
            if res["res"]:
                return str(res["res"][0]["uid"])
            else:
                raise Exception(f"get_uid failed for node_key: {node_key} {res}")
        else:
            return str(res["res"]["uid"])

    finally:
        txn.discard()