def transaction(self, transaction_type: TransactionType, options=None) -> Transaction: if not options: options = GraknOptions.core() return Transaction(self._address, self._session_id, transaction_type, options)
def __init__(self, client, database: str, session_type: SessionType, options: GraknOptions = None): if not options: options = GraknOptions.core() self._address = client._address self._channel = grpc.insecure_channel(client._address) self._scheduler = sched.scheduler(time.time, time.sleep) self._database = database self._session_type = session_type self._grpc_stub = GraknStub(self._channel) open_req = session_proto.Session.Open.Req() open_req.database = database open_req.type = _session_type_proto(session_type) open_req.options.CopyFrom(grakn_proto_builder.options(options)) self._session_id = self._grpc_stub.session_open(open_req).session_id self._is_open = True self._pulse_thread_pool = ThreadPoolExecutor( thread_name_prefix='session_pulse_{}'.format( self._session_id.hex())) self._scheduler.enter(delay=self._PULSE_FREQUENCY_SECONDS, priority=1, action=self._transmit_pulse, argument=()) self._pulse_thread_pool.submit(self._scheduler.run)
def session(self, database: str, session_type: SessionType, options=None) -> Session: if not options: options = GraknOptions.core() return _RPCSession(self, database, session_type, options)
def __init__(self, address: str, session_id: bytes, transaction_type: TransactionType, options: GraknOptions = None): if not options: options = GraknOptions.core() self._transaction_type = transaction_type self._concept_manager = ConceptManager(self) self._query_manager = QueryManager(self) self._logic_manager = LogicManager(self) self._response_queues = {} self._grpc_stub = GraknStub(grpc.aio.insecure_channel(address)) self._request_iterator = RequestIterator() self._response_iterator = self._grpc_stub.transaction( self._request_iterator) self._transaction_was_closed = False open_req = transaction_proto.Transaction.Open.Req() open_req.session_id = session_id open_req.type = Transaction._transaction_type_proto(transaction_type) open_req.options.CopyFrom(grakn_proto_builder.options(options)) req = transaction_proto.Transaction.Req() req.open_req.CopyFrom(open_req) start_time = time.time() * 1000.0 res = self._execute(req) end_time = time.time() * 1000.0 self._network_latency_millis = end_time - start_time - res.open_res.processing_time_millis
def delete(self, query: str, options: GraknOptions = None): if not options: options = GraknOptions.core() request = query_proto.Query.Req() delete_req = query_proto.Query.Delete.Req() delete_req.query = query request.delete_req.CopyFrom(delete_req) return self._iterate_query(request, lambda res: [], options)
def undefine(self, query: str, options: GraknOptions = None): if not options: options = GraknOptions.core() request = query_proto.Query.Req() undefine_req = query_proto.Query.Undefine.Req() undefine_req.query = query request.undefine_req.CopyFrom(undefine_req) return self._iterate_query(request, lambda res: [], options)
def match_aggregate(self, query: str, options: GraknOptions = None): if not options: options = GraknOptions.core() request = query_proto.Query.Req() match_aggregate_req = query_proto.Query.MatchAggregate.Req() match_aggregate_req.query = query request.match_aggregate_req.CopyFrom(match_aggregate_req) return self._iterate_query( request, lambda res: [numeric._of(res.query_res.match_aggregate_res.answer)], options)
def insert(self, query: str, options: GraknOptions = None): if not options: options = GraknOptions.core() request = query_proto.Query.Req() insert_req = query_proto.Query.Insert.Req() insert_req.query = query request.insert_req.CopyFrom(insert_req) return map( lambda answer_proto: concept_map._of(answer_proto), self._iterate_query(request, lambda res: res.query_res.insert_res.answers, options))
def match_group(self, query: str, options: GraknOptions = None): if not options: options = GraknOptions.core() request = query_proto.Query.Req() match_group_req = query_proto.Query.MatchGroup.Req() match_group_req.query = query request.match_group_req.CopyFrom(match_group_req) return map( lambda cmg_proto: concept_map_group._of(cmg_proto), self._iterate_query( request, lambda res: res.query_res.match_group_res.answers, options))
def match_group_aggregate(self, query: str, options: GraknOptions = None): if not options: options = GraknOptions.core() request = query_proto.Query.Req() match_group_aggregate_req = query_proto.Query.MatchGroupAggregate.Req() match_group_aggregate_req.query = query request.match_group_aggregate_req.CopyFrom(match_group_aggregate_req) return map( lambda numeric_group_proto: numeric_group._of(numeric_group_proto), self._iterate_query( request, lambda res: res.query_res.match_group_aggregate_res.answers, options))
def __init__(self, client, database: str, session_type: SessionType, options: GraknOptions = None): if not options: options = GraknOptions.core() self._client = client self._address = client._address self._channel = grpc.aio.insecure_channel(client._address) self._scheduler = sched.scheduler(time.time, time.sleep) self._database = _DatabaseRPC(database_manager=client.databases(), name=database) self._session_type = session_type self._grpc_stub = GraknStub(self._channel) self._lock = Lock() open_req = session_proto.Session.Open.Req() open_req.database = database open_req.type = _session_type_proto(session_type) open_req.options.CopyFrom(grakn_proto_builder.options(options)) self._session_id: bytes = self._grpc_stub.session_open(open_req).session_id self._is_open = True self._pulse = self._scheduler.enter(delay=self._PULSE_FREQUENCY_SECONDS, priority=1, action=self._transmit_pulse, argument=()) Thread(target=self._scheduler.run, name="session_pulse_{}".format(self._session_id.hex()), daemon=True).start()