예제 #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)
예제 #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)
예제 #3
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))
예제 #4
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() 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)
예제 #5
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))
예제 #6
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))
예제 #7
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
예제 #8
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)
예제 #9
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)))
예제 #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)
예제 #11
0
 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()
예제 #12
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)
예제 #13
0
 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()
예제 #14
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
예제 #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)
예제 #16
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)
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)
예제 #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)
예제 #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
예제 #20
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)
예제 #21
0
 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()
예제 #22
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]))
예제 #23
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)
예제 #24
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))
예제 #25
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)
예제 #26
0
 def _raise_transaction_closed_error(self):
     raise TypeDBClientException.of(
         TRANSACTION_CLOSED_WITH_ERRORS, self._error
     ) if self._error else TypeDBClientException.of(TRANSACTION_CLOSED)
예제 #27
0
 def get(self, name: str) -> _TypeDBDatabaseImpl:
     if self.contains(name):
         return _TypeDBDatabaseImpl(self._stub, name)
     else:
         raise TypeDBClientException.of(DB_DOES_NOT_EXIST, name)
예제 #28
0
def _not_blank(name: str) -> str:
    if name in [None, ""] or name.isspace():
        raise TypeDBClientException.of(MISSING_DB_NAME)
    return name
예제 #29
0
 def get(self, username: str) -> User:
     if (self.contains(username)):
         return _ClusterUser(self._client, username)
     else:
         raise TypeDBClientException.of(CLUSTER_USER_DOES_NOT_EXIST,
                                        username)
예제 #30
0
 def _cluster_not_available_exception(self) -> TypeDBClientException:
     return TypeDBClientException.of(
         CLUSTER_UNABLE_TO_CONNECT,
         str([str(addr) for addr in self.client.server_addresses()]))