Esempio n. 1
0
    def get_count(
        self,
        graph_client,
        first: int = 100,
    ) -> int:
        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)

        try:
            qres = json.loads(txn.query(query, variables=variables).json)
        finally:
            txn.discard()

        return int(qres.get("query", {}).get("c", 0))
Esempio n. 2
0
    def query(self, graph_client, 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)

        try:
            qres = json.loads(txn.query(query, variables=variables).json)
        finally:
            txn.discard()

        d = qres.get("q0")
        if d:
            return [
                self.associated_viewable().from_dict(node, graph_client) for node in d
            ]
        return []
Esempio n. 3
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 []
Esempio n. 4
0
    def query_first(
        self,
        graph_client: GraphClient,
        contains_node_key: Optional[str] = None,
        best_effort=False,
    ) -> Optional[V]:
        if contains_node_key:
            var_alloc, query = gen_query_parameterized(self, "q0", contains_node_key, 0)
        else:
            var_alloc, query = gen_query(self, "q0", first=1)

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

        with graph_client.txn_context(read_only=True, best_effort=best_effort) 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(d[0], graph_client)
        return None
Esempio n. 5
0
    def query_first(
        self,
        graph_client,
        contains_node_key: Optional[str] = None,
        best_effort=False,
    ) -> Optional[V]:
        if contains_node_key:
            var_alloc, query = gen_query_parameterized(self, "q0", contains_node_key, 0)
        else:
            var_alloc, query = gen_query(self, "q0", first=1)

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

        try:
            qres = json.loads(txn.query(query, variables=variables).json)
        finally:
            txn.discard()

        d = qres.get("q0")
        if d:
            return self.associated_viewable().from_dict(d[0], graph_client)
        return None
Esempio n. 6
0
 def debug_query(self) -> Dict[str, Any]:
     var_alloc, query = gen_query(self, "q0", first=1)
     variables = {v: k for k, v in var_alloc.allocated.items()}
     return {"query": query, "variables": variables}