Esempio n. 1
0
    def _fetch(self, request_id: str):
        try:
            return self._response_queues[request_id].get(block=False)
        except queue.Empty:
            pass

        # Keep taking responses until we get one that matches the request ID
        while True:
            try:
                response = next(self._response_iterator)
            except grpc.RpcError as e:
                self._transaction_was_closed = True
                grakn_exception = GraknClientException(e.details())
                for response_queue in self._response_queues.values():
                    response_queue.put(grakn_exception)
                # noinspection PyUnresolvedReferences
                raise grakn_exception
            except StopIteration:
                raise GraknClientException(
                    "The transaction has been closed and no further operation is allowed."
                )

            if isinstance(response, GraknClientException):
                raise response
            elif response.id == request_id:
                return response
            else:
                response_queue = self._response_queues[response.id]
                if response_queue is None:
                    raise GraknClientException(
                        "Received a response with unknown request id '" +
                        response.id + "'.")
                response_queue.put(response)
Esempio n. 2
0
 def __init__(self, transaction, iid: str):
     if not transaction:
         raise GraknClientException("Transaction must be set.")
     if not iid:
         raise GraknClientException("IID must be set.")
     self._transaction = transaction
     self._iid = iid
     self._hash = hash(iid)
Esempio n. 3
0
 def __init__(self, transaction, label: str, is_root: bool):
     if not transaction:
         raise GraknClientException("Transaction must be set.")
     if not label:
         raise GraknClientException("Label must be a non-empty string.")
     self._transaction = transaction
     self._label = label
     self._is_root = is_root
     self._hash = hash((self._transaction, label))
Esempio n. 4
0
 def __init__(self, label: str, when: str, then: str):
     if not label:
         raise GraknClientException("Label must be a non-empty string.")
     self._label = label
     self._when = when
     self._then = then
     self._hash = hash(label)
Esempio n. 5
0
 def all(self) -> List[str]:
     try:
         return list(
             self._grpc_stub.database_all(
                 database_proto.Database.All.Req()).names)
     except RpcError as e:
         raise GraknClientException(e)
Esempio n. 6
0
 def delete(self, name: str) -> None:
     request = database_proto.Database.Delete.Req()
     request.name = _not_blank(name)
     try:
         self._grpc_stub.database_delete(request)
     except RpcError as e:
         raise GraknClientException(e)
Esempio n. 7
0
 def contains(self, name: str) -> bool:
     request = database_proto.Database.Contains.Req()
     request.name = _not_blank(name)
     try:
         return self._grpc_stub.database_contains(request).contains
     except RpcError as e:
         raise GraknClientException(e)
Esempio n. 8
0
    def __next__(self):
        if self._done:
            raise StopIteration()
        if self._current_iterator is not None:
            try:
                return next(self._current_iterator)
            except StopIteration:
                self._current_iterator = None

        res = self._transaction._fetch(self._request_id)
        res_case = res.WhichOneof("res")
        if res_case == Stream._CONTINUE:
            continue_req = transaction_proto.Transaction.Req()
            continue_req.id = self._request_id
            setattr(continue_req, "continue", True)
            self._transaction._request_iterator.put(continue_req)
            return next(self)
        elif res_case == Stream._DONE:
            self._done = True
            raise StopIteration()
        elif res_case is None:
            raise GraknClientException(
                "The required field 'res' of type 'transaction_proto.Transaction.Res' was not set."
            )
        else:
            self._current_iterator = iter(self._transform_response(res))
            return next(self)
Esempio n. 9
0
 def contains(self, name: str) -> bool:
     errors = []
     for database_manager in self._database_managers.values():
         try:
             return database_manager.contains(name)
         except GraknClientException as e:
             errors.append(e)
     raise GraknClientException("Attempted connecting to all cluster members, but the following errors occurred: " + str([str(e) for e in errors]))
Esempio n. 10
0
 def all(self) -> List[DatabaseCluster]:
     errors = []
     for address in self._database_managers:
         try:
             res = self._client.grakn_cluster_grpc_stub(address).database_all(database_proto.Database.All.Req())
             return [_DatabaseClusterRPC.of(db, self) for db in res.databases]
         except GraknClientException as e:
             errors.append(e)
     raise GraknClientException("Attempted connecting to all cluster members, but the following errors occurred: " + str([str(e) for e in errors]))
def thing_encoding(thing_):
    if thing_.is_entity():
        return concept_proto.Thing.Encoding.Value("ENTITY")
    elif thing_.is_relation():
        return concept_proto.Thing.Encoding.Value("RELATION")
    elif thing_.is_attribute():
        return concept_proto.Thing.Encoding.Value("ATTRIBUTE")
    else:
        raise GraknClientException("Unrecognised thing encoding: " +
                                   str(thing_))
Esempio n. 12
0
 def _execute(self, request: transaction_proto.Transaction.Req):
     response_queue = queue.Queue()
     request_id = str(uuid.uuid4())
     request.id = request_id
     if self._transaction_was_closed:
         raise GraknClientException(
             "The transaction has been closed and no further operation is allowed."
         )
     self._response_queues[request_id] = response_queue
     self._request_iterator.put(request)
     return self._fetch(request_id)
Esempio n. 13
0
 def get(self, name: str) -> DatabaseCluster:
     errors = []
     for address in self._database_managers:
         try:
             database_get_req = database_proto.Database.Get.Req()
             database_get_req.name = name
             res = self._client.grakn_cluster_grpc_stub(address).database_get(database_get_req)
             return _DatabaseClusterRPC.of(res.database, self)
         except GraknClientException as e:
             errors.append(e)
     raise GraknClientException("Attempted connecting to all cluster members, but the following errors occurred: " + str([str(e) for e in errors]))
Esempio n. 14
0
def _of(numeric_proto: answer_proto.Numeric):
    numeric_case = numeric_proto.WhichOneof("value")
    if numeric_case == "long_value":
        return Numeric(numeric_proto.long_value, None)
    elif numeric_case == "double_value":
        return Numeric(None, numeric_proto.double_value)
    elif numeric_case == "nan":
        return Numeric(None, None)
    else:
        raise GraknClientException("The answer type '" + numeric_case +
                                   "' was not recognised.")
Esempio n. 15
0
 def close(self) -> None:
     if self._is_open:
         self._is_open = False
         self._pulse_thread_pool.shutdown(wait=False)
         req = session_proto.Session.Close.Req()
         req.session_id = self._session_id
         try:
             self._grpc_stub.session_close(req)
         except RpcError as e:
             raise GraknClientException(e)
         finally:
             self._channel.close()
def thing(thing_proto: concept_proto.Thing):
    if thing_proto.encoding == concept_proto.Thing.Encoding.Value("ENTITY"):
        return Entity._of(thing_proto)
    elif thing_proto.encoding == concept_proto.Thing.Encoding.Value(
            "RELATION"):
        return Relation._of(thing_proto)
    elif thing_proto.encoding == concept_proto.Thing.Encoding.Value(
            "ATTRIBUTE"):
        return attribute(thing_proto)
    else:
        raise GraknClientException("The encoding " + thing_proto.encoding +
                                   " was not recognised.")
Esempio n. 17
0
 def parse(address: str) -> "ServerAddress":
     s = address.split(",")
     if len(s) == 1:
         s1 = address.split(":")
         return ServerAddress(client_host=s1[0],
                              client_port=int(s1[1]),
                              server_host=s1[0],
                              server_port=int(s1[1]))
     elif len(s) == 2:
         client_url = s[0].split(":")
         server_url = s[1].split(":")
         if len(client_url) != 2 or len(server_url) != 2:
             raise GraknClientException("Failed to parse server address: " +
                                        address)
         return ServerAddress(client_host=client_url[0],
                              client_port=int(client_url[1]),
                              server_host=server_url[0],
                              server_port=int(server_url[1]))
     else:
         raise GraknClientException("Failed to parse server address: " +
                                    address)
def type_encoding(_type):
    if _type.is_entity_type():
        return concept_proto.Type.Encoding.Value("ENTITY_TYPE")
    elif _type.is_relation_type():
        return concept_proto.Type.Encoding.Value("RELATION_TYPE")
    elif _type.is_attribute_type():
        return concept_proto.Type.Encoding.Value("ATTRIBUTE_TYPE")
    elif _type.is_role_type():
        return concept_proto.Type.Encoding.Value("ROLE_TYPE")
    elif _type.is_thing_type():
        return concept_proto.Type.Encoding.Value("THING_TYPE")
    else:
        raise GraknClientException("Unrecognised type encoding: " + str(_type))
Esempio n. 19
0
 def primary_replica(self) -> "_RPCSessionCluster.Replica":
     primaries = [
         replica for replica in self._replicas.values()
         if replica.is_primary()
     ]
     if primaries:
         return max(primaries, key=lambda r: r.term)
     else:
         max_term = max(
             [replica.term() for replica in self._replicas.values()])
         raise GraknClientException(
             "No replica has been marked as the primary replica for latest known term '%d'."
             % max_term)
def thing_type(type_proto: concept_proto.Type):
    if type_proto.encoding == concept_proto.Type.Encoding.Value("ENTITY_TYPE"):
        return EntityType._of(type_proto)
    elif type_proto.encoding == concept_proto.Type.Encoding.Value(
            "RELATION_TYPE"):
        return RelationType._of(type_proto)
    elif type_proto.encoding == concept_proto.Type.Encoding.Value(
            "ATTRIBUTE_TYPE"):
        return attribute_type(type_proto)
    elif type_proto.encoding == concept_proto.Type.Encoding.Value(
            "THING_TYPE"):
        return ThingType(type_proto.label, type_proto.root)
    else:
        raise GraknClientException("The encoding " + str(type_proto.encoding) +
                                   " was not recognised.")
Esempio n. 21
0
 def _stream(
     self,
     request: transaction_proto.Transaction.Req,
     transform_response: Callable[[transaction_proto.Transaction.Res],
                                  List] = None):
     response_queue = queue.Queue()
     request_id = str(uuid.uuid4())
     request.id = request_id
     if self._transaction_was_closed:
         raise GraknClientException(
             "The transaction has been closed and no further operation is allowed."
         )
     self._response_queues[request_id] = response_queue
     self._request_iterator.put(request)
     return Stream(self, request_id, transform_response)
def value_type(value_type_: ValueType):
    if value_type_ == ValueType.BOOLEAN:
        return concept_proto.AttributeType.ValueType.Value("BOOLEAN")
    elif value_type_ == ValueType.LONG:
        return concept_proto.AttributeType.ValueType.Value("LONG")
    elif value_type_ == ValueType.DOUBLE:
        return concept_proto.AttributeType.ValueType.Value("DOUBLE")
    elif value_type_ == ValueType.STRING:
        return concept_proto.AttributeType.ValueType.Value("STRING")
    elif value_type_ == ValueType.DATETIME:
        return concept_proto.AttributeType.ValueType.Value("DATETIME")
    elif value_type_ == ValueType.OBJECT:
        return concept_proto.AttributeType.ValueType.Value("OBJECT")
    else:
        raise GraknClientException("Unrecognised value type: " +
                                   str(value_type_))
Esempio n. 23
0
 def _discover_cluster(self, addresses: List[str]) -> Set[Address.Server]:
     for address in addresses:
         try:
             with _RPCGraknClient(address) as client:
                 print("Performing cluster discovery to %s..." % address)
                 grakn_cluster_stub = GraknClusterStub(client.channel())
                 res = grakn_cluster_stub.cluster_discover(
                     cluster_proto.Cluster.Discover.Req())
                 members = set(
                     [Address.Server.parse(srv) for srv in res.servers])
                 print("Discovered %s" % str(members))
                 return members
         except RpcError:
             print("Cluster discovery to %s failed." % address)
     raise GraknClientException(
         "Unable to connect to Grakn Cluster. Attempted connecting to the cluster members, but none are available: %s"
         % str(addresses))
def attribute(thing_proto: concept_proto.Thing):
    if thing_proto.value_type == concept_proto.AttributeType.ValueType.Value(
            "BOOLEAN"):
        return BooleanAttribute._of(thing_proto)
    elif thing_proto.value_type == concept_proto.AttributeType.ValueType.Value(
            "LONG"):
        return LongAttribute._of(thing_proto)
    elif thing_proto.value_type == concept_proto.AttributeType.ValueType.Value(
            "DOUBLE"):
        return DoubleAttribute._of(thing_proto)
    elif thing_proto.value_type == concept_proto.AttributeType.ValueType.Value(
            "STRING"):
        return StringAttribute._of(thing_proto)
    elif thing_proto.value_type == concept_proto.AttributeType.ValueType.Value(
            "DATETIME"):
        return DateTimeAttribute._of(thing_proto)
    else:
        raise GraknClientException("The value type " +
                                   str(thing_proto.value_type) +
                                   " was not recognised.")
Esempio n. 25
0
 def get_has(self,
             attribute_type=None,
             attribute_types: List = None,
             only_key=False):
     if [bool(attribute_type),
             bool(attribute_types), only_key].count(True) > 1:
         raise GraknClientException(
             "Only one filter can be applied at a time to get_has."
             "The possible filters are: [attribute_type, attribute_types, only_key]"
         )
     if attribute_type:
         attribute_types = [attribute_type]
     method = concept_proto.Thing.Req()
     get_has_req = concept_proto.Thing.GetHas.Req()
     if only_key:
         get_has_req.keys_only = only_key
     elif attribute_types:
         get_has_req.attribute_types.extend(
             concept_proto_builder.types(attribute_types))
     method.thing_get_has_req.CopyFrom(get_has_req)
     return self._thing_stream(method,
                               lambda res: res.thing_get_has_res.attributes)
Esempio n. 26
0
 def get(self, name: str) -> Database:
     if self.contains(name):
         return _DatabaseRPC(self, name)
     else:
         raise GraknClientException("The database '%s' does not exist." %
                                    name)
Esempio n. 27
0
def _not_blank(name: str) -> str:
    if name in [None, ""] or name.isspace():
        raise GraknClientException("Database name must not be empty.")
    return name
Esempio n. 28
0
 def __init__(self, label: str, is_root: bool):
     if not label:
         raise GraknClientException("Label must be a non-empty string.")
     self._label = label
     self._is_root = is_root
     self._hash = hash(label)
Esempio n. 29
0
 def _cluster_not_available_exception(self) -> GraknClientException:
     addresses = str(
         [str(addr) for addr in self._cluster_client.cluster_members()])
     return GraknClientException(
         "Unable to connect to Grakn Cluster. Attempted connecting to the cluster members, but none are available: '%s'"
         % addresses)
Esempio n. 30
0
def rpc_call(request: Callable[[], Any]) -> Any:
    try:
        return request()
    except RpcError as e:
        raise GraknClientException(e)