Beispiel #1
0
 def _raise_transaction_closed(self):
     error = self._bidirectional_stream.get_error()
     if error is None:
         raise TypeDBClientException.of(TRANSACTION_CLOSED)
     else:
         raise TypeDBClientException.of(TRANSACTION_CLOSED_WITH_ERRORS,
                                        error)
Beispiel #2
0
    def fetch(self, request_id: UUID) -> Union[transaction_proto.Transaction.Res, transaction_proto.Transaction.ResPart]:
        # Keep taking responses until we get one that matches the request ID
        while True:
            try:
                return self._response_collector.get(request_id).get(block=False)
            except Empty:
                pass

            try:
                if not self._is_open.get():
                    raise TypeDBClientException.of(TRANSACTION_CLOSED)
                server_msg = next(self._response_iterator)
            except RpcError as e:
                error = TypeDBClientException.of_rpc(e)
                self.close(error)
                raise error
            except StopIteration:
                self.close()
                raise TypeDBClientException.of(TRANSACTION_CLOSED)

            server_case = server_msg.WhichOneof("server")
            if server_case == "res":
                self._collect(server_msg.res)
            elif server_case == "res_part":
                self._collect(server_msg.res_part)
            else:
                raise TypeDBClientException.of(ILLEGAL_ARGUMENT)
 def get(self, block: bool) -> R:
     response = self._response_queue.get(block=block)
     if response.is_value():
         return response.value
     elif response.is_done() and response.error is None:
         raise TypeDBClientException.of(TRANSACTION_CLOSED)
     elif response.is_done() and response.error is not None:
         raise TypeDBClientException.of_rpc(response.error)
     else:
         raise TypeDBClientException.of(ILLEGAL_STATE)
Beispiel #4
0
 def __init__(self, transaction_ext: Union["_TypeDBTransactionExtended", "TypeDBTransaction"], label: str, when: str, then: str):
     if not transaction_ext:
         raise TypeDBClientException.of(MISSING_TRANSACTION)
     if not label:
         raise TypeDBClientException.of(MISSING_LABEL)
     self._transaction_ext = transaction_ext
     self._label = label
     self._when = when
     self._then = then
     self._hash = hash((transaction_ext, label))
Beispiel #5
0
 def __init__(self, transaction: Union["_TypeDBTransactionExtended",
                                       "TypeDBTransaction"], label: Label,
              is_root: bool):
     if not transaction:
         raise TypeDBClientException.of(MISSING_TRANSACTION)
     if not label:
         raise TypeDBClientException.of(MISSING_LABEL)
     self._transaction_ext = transaction
     self._label = label
     self._is_root = is_root
     self._hash = hash((self._transaction_ext, label))
Beispiel #6
0
 def __init__(self, transaction: Union["_TypeDBTransactionExtended",
                                       "TypeDBTransaction"], iid: str,
              is_inferred: bool):
     if not transaction:
         raise TypeDBClientException.of(MISSING_TRANSACTION)
     if not iid:
         raise TypeDBClientException.of(MISSING_IID)
     self._transaction_ext = transaction
     self._iid = iid
     self._is_inferred = is_inferred
     self._hash = hash((self._transaction_ext, iid))
Beispiel #7
0
 def _collect(self, response: Union[transaction_proto.Transaction.Res, transaction_proto.Transaction.ResPart]):
     request_id = UUID(bytes=response.req_id)
     collector = self._response_collector.get(request_id)
     if collector:
         collector.put(response)
     else:
         raise TypeDBClientException.of(UNKNOWN_REQUEST_ID, (request_id, str(response)))
Beispiel #8
0
 def ownership(self, owner: str,
               attribute: str) -> "ConceptMap.Explainable":
     explainable = self._ownerships.get((owner, attribute))
     if not explainable:
         raise TypeDBClientException.of(
             NONEXISTENT_EXPLAINABLE_OWNERSHIP, (owner, attribute))
     return explainable
Beispiel #9
0
 def __init__(self, label: str, when: str, then: str):
     if not label:
         raise TypeDBClientException.of(MISSING_LABEL)
     self._label = label
     self._when = when
     self._then = then
     self._hash = hash(label)
Beispiel #10
0
 def get(self, block: bool) -> R:
     response = self._response_queue.get(block=block)
     if response.is_value():
         return response.value
     elif response.is_done():
         self._raise_transaction_closed_error()
     else:
         raise TypeDBClientException.of(ILLEGAL_STATE)
Beispiel #11
0
 def close(self, error: TypeDBClientException = None):
     if self._is_open.compare_and_set(True, False):
         self._error = error
         self._response_collector.close(error)
         try:
             self._dispatcher.close()
         except RpcError as e:
             raise TypeDBClientException.of_rpc(e)
 def dispatch(self, proto_req: transaction_proto.Transaction.Req):
     try:
         self._transmitter.access_lock.acquire_read()
         if not self._transmitter.is_open():
             raise TypeDBClientException.of(CLIENT_CLOSED)
         self._request_queue.put(proto_req)
         self._executor.may_start_running()
     finally:
         self._transmitter.access_lock.release_read()
 def dispatch_now(self, proto_req: transaction_proto.Transaction.Req):
     try:
         self._transmitter.access_lock.acquire_read()
         if not self._transmitter.is_open():
             raise TypeDBClientException.of(CLIENT_CLOSED)
         self._request_queue.put(proto_req)
         self.send_batched_requests()
     finally:
         self._transmitter.access_lock.release_read()
Beispiel #14
0
 def _has_next(self) -> bool:
     if self._state == ResponsePartIterator.State.DONE:
         return False
     elif self._state == ResponsePartIterator.State.FETCHED:
         return True
     elif self._state == ResponsePartIterator.State.EMPTY:
         return self._fetch_and_check()
     else:
         raise TypeDBClientException.of(ILLEGAL_STATE)
Beispiel #15
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 TypeDBClientException.of(BAD_ANSWER_TYPE, numeric_case)
Beispiel #16
0
 def _fetch_and_check(self) -> bool:
     res_part = self._bidirectional_stream.fetch(self._request_id)
     res_case = res_part.WhichOneof("res")
     if res_case == "stream_res_part":
         state = res_part.stream_res_part.state
         if state == transaction_proto.Transaction.Stream.State.Value("DONE"):
             self._state = ResponsePartIterator.State.DONE
             return False
         elif state == transaction_proto.Transaction.Stream.State.Value("CONTINUE"):
             self._bidirectional_stream.dispatcher().dispatch(transaction_stream_req(self._request_id))
             return self._fetch_and_check()
         else:
             raise TypeDBClientException.of(ILLEGAL_ARGUMENT)
     elif res_case is None:
         raise TypeDBClientException.of(MISSING_RESPONSE, self._request_id)
     else:
         self._next = res_part
         self._state = ResponsePartIterator.State.FETCHED
         return True
Beispiel #17
0
def thing(proto_thing: concept_proto.Thing):
    if proto_thing.type.encoding == concept_proto.Type.Encoding.Value(
            "ENTITY_TYPE"):
        return _Entity.of(proto_thing)
    elif proto_thing.type.encoding == concept_proto.Type.Encoding.Value(
            "RELATION_TYPE"):
        return _Relation.of(proto_thing)
    elif proto_thing.type.encoding == concept_proto.Type.Encoding.Value(
            "ATTRIBUTE_TYPE"):
        return attribute(proto_thing)
    else:
        raise TypeDBClientException.of(BAD_ENCODING, proto_thing.type.encoding)
Beispiel #18
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 TypeDBClientException.of(GET_HAS_WITH_MULTIPLE_FILTERS)
     if attribute_type:
         attribute_types = [attribute_type]
     return (concept_proto_reader.attribute(a) for rp in self.stream(
         thing_get_has_req(self.get_iid(
         ), concept_proto_builder.types(attribute_types), only_key))
             for a in rp.thing_get_has_res_part.attributes)
Beispiel #19
0
    def __init__(self,
                 username: str,
                 password: str,
                 tls_root_ca_path: str = None):
        self._username = username
        self._password = password

        if (tls_root_ca_path is not None
                and not path.exists(tls_root_ca_path)):
            raise TypeDBClientException.of(CLUSTER_INVALID_ROOT_CA_PATH,
                                           tls_root_ca_path)

        self._tls_root_ca_path = tls_root_ca_path
def encoding(_type: 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 TypeDBClientException.of(BAD_ENCODING, _type)
Beispiel #21
0
def thing_type(proto_type: concept_proto.Type):
    if proto_type.encoding == concept_proto.Type.Encoding.Value("ENTITY_TYPE"):
        return _EntityType.of(proto_type)
    elif proto_type.encoding == concept_proto.Type.Encoding.Value(
            "RELATION_TYPE"):
        return _RelationType.of(proto_type)
    elif proto_type.encoding == concept_proto.Type.Encoding.Value(
            "ATTRIBUTE_TYPE"):
        return attribute_type(proto_type)
    elif proto_type.encoding == concept_proto.Type.Encoding.Value(
            "THING_TYPE"):
        return _ThingType(Label.of(proto_type.label), proto_type.root)
    else:
        raise TypeDBClientException.of(BAD_ENCODING, proto_type.encoding)
 def dispatcher(
     self, request_iterator: "RequestIterator"
 ) -> "RequestTransmitter.Dispatcher":
     try:
         self.access_lock.acquire_read()
         if not self._is_open:
             raise TypeDBClientException.of(CLIENT_CLOSED)
         executor = self._next_executor()
         disp = RequestTransmitter.Dispatcher(executor, request_iterator,
                                              self)
         executor.dispatchers.append(disp)
         return disp
     finally:
         self.access_lock.release_read()
Beispiel #23
0
 def all(self) -> List[_ClusterDatabase]:
     errors = []
     for address in self._database_mgrs:
         try:
             res = self._client._stub(address).cluster_databases_all(
                 cluster_database_manager_all_req())
             return [
                 _ClusterDatabase.of(db, self._client)
                 for db in res.databases
             ]
         except TypeDBClientException as e:
             errors.append("- %s: %s\n" % (address, e))
     raise TypeDBClientException.of(CLUSTER_ALL_NODES_FAILED,
                                    str([str(e) for e in errors]))
Beispiel #24
0
 def may_renew_token(self, function: Callable[[], T]) -> T:
     try:
         return self.resilient_call(function)
     except TypeDBClientException as e:
         if e.error_message is not None and e.error_message is CLUSTER_TOKEN_CREDENTIAL_INVALID:
             self._token = None
             res = self._cluster_stub.user_token(
                 cluster_user_token_req(self._credential.username()))
             self._token = res.token
             try:
                 return self.resilient_call(function)
             except RpcError as e2:
                 raise TypeDBClientException.of_rpc(e2)
         else:
             raise e
Beispiel #25
0
 def __init__(self, channel: Channel, credential: TypeDBCredential):
     super(_ClusterServerStub, self).__init__()
     self._credential = credential
     self._channel = channel
     self._stub = core_service_proto.TypeDBStub(channel)
     self._cluster_stub = cluster_service_proto.TypeDBClusterStub(channel)
     self._token = None
     try:
         res = self._cluster_stub.user_token(
             cluster_user_token_req(self._credential.username()))
         self._token = res.token
     except RpcError as e:
         e2 = TypeDBClientException.of_rpc(e)
         if e2.error_message is not None and e2.error_message is not UNABLE_TO_CONNECT:
             raise e2
Beispiel #26
0
def attribute(proto_thing: concept_proto.Thing):
    if proto_thing.type.value_type == concept_proto.AttributeType.ValueType.Value(
            "BOOLEAN"):
        return _BooleanAttribute.of(proto_thing)
    elif proto_thing.type.value_type == concept_proto.AttributeType.ValueType.Value(
            "LONG"):
        return _LongAttribute.of(proto_thing)
    elif proto_thing.type.value_type == concept_proto.AttributeType.ValueType.Value(
            "DOUBLE"):
        return _DoubleAttribute.of(proto_thing)
    elif proto_thing.type.value_type == concept_proto.AttributeType.ValueType.Value(
            "STRING"):
        return _StringAttribute.of(proto_thing)
    elif proto_thing.type.value_type == concept_proto.AttributeType.ValueType.Value(
            "DATETIME"):
        return _DateTimeAttribute.of(proto_thing)
    else:
        raise TypeDBClientException.of(BAD_VALUE_TYPE,
                                       proto_thing.type.value_type)
Beispiel #27
0
 def _fetch_server_addresses(self, addresses: Iterable[str]) -> Set[str]:
     for address in addresses:
         try:
             print("Fetching list of cluster servers from %s..." % address)
             with _ClusterServerClient(address, self._credential) as client:
                 res = client.stub().servers_all(
                     cluster_server_manager_all_req())
                 members = {srv.address for srv in res.servers}
                 print("The cluster servers are %s" %
                       [str(member) for member in members])
                 return members
         except TypeDBClientException as e:
             if e.error_message is UNABLE_TO_CONNECT:
                 print("Fetching cluster servers from %s failed. %s" %
                       (address, str(e)))
             else:
                 raise e
     raise TypeDBClientException.of(CLUSTER_UNABLE_TO_CONNECT,
                                    ",".join(addresses))
Beispiel #28
0
def attribute_type(proto_type: concept_proto.Type):
    if proto_type.value_type == concept_proto.AttributeType.ValueType.Value(
            "BOOLEAN"):
        return _BooleanAttributeType.of(proto_type)
    elif proto_type.value_type == concept_proto.AttributeType.ValueType.Value(
            "LONG"):
        return _LongAttributeType.of(proto_type)
    elif proto_type.value_type == concept_proto.AttributeType.ValueType.Value(
            "DOUBLE"):
        return _DoubleAttributeType.of(proto_type)
    elif proto_type.value_type == concept_proto.AttributeType.ValueType.Value(
            "STRING"):
        return _StringAttributeType.of(proto_type)
    elif proto_type.value_type == concept_proto.AttributeType.ValueType.Value(
            "DATETIME"):
        return _DateTimeAttributeType.of(proto_type)
    elif proto_type.value_type == concept_proto.AttributeType.ValueType.Value(
            "OBJECT"):
        return _AttributeType(Label.of(proto_type.label), proto_type.root)
    else:
        raise TypeDBClientException.of(BAD_VALUE_TYPE, proto_type.value_type)
Beispiel #29
0
    def __init__(self,
                 session: "_TypeDBSessionImpl",
                 transaction_type: TransactionType,
                 options: TypeDBOptions = None):
        if not options:
            options = TypeDBOptions.core()
        self._transaction_type = transaction_type
        self._options = options
        self._concept_manager = _ConceptManager(self)
        self._query_manager = _QueryManager(self)
        self._logic_manager = _LogicManager(self)

        try:
            self._channel, stub = session.client().new_channel_and_stub()
            self._bidirectional_stream = BidirectionalStream(
                stub, session.transmitter())
            req = transaction_open_req(session.session_id(),
                                       transaction_type.proto(),
                                       options.proto(),
                                       session.network_latency_millis())
            self.execute(request=req, batch=False)
        except RpcError as e:
            raise TypeDBClientException.of_rpc(e)
Beispiel #30
0
 def _raise_transaction_closed_error(self):
     raise TypeDBClientException.of(
         TRANSACTION_CLOSED_WITH_ERRORS, self._error
     ) if self._error else TypeDBClientException.of(TRANSACTION_CLOSED)