def read_message_py2(self): chunks = [] while True: try: hi, lo = self.wire.read(2) except WireError as error: raise_from(ConnectionBroken("Failed to read message"), error) else: if hi == lo == 0: # We hit a zero chunk... if chunks: # ... and some non-zero chunks have been collected, # therefore we can exit the loop with a message. break else: # ... and no non-zero chunks have yet been collected, # therefore, this must be a null message used for # keep-alive and we should carry on looping. pass else: # We found a non-zero chunk which can be collected. size = hi << 8 | lo chunks.append(self.wire.read(size)) message = bytearray(b"".join(map(bytes, chunks))) assert message # the message should never be empty _, n = divmod(message[0], 0x10) try: fields = list(unpack(message, offset=2)) except ValueError as error: raise_from(ProtocolError("Bad message content"), error) else: return message[1], fields
def rollback(self, tx): self._assert_open() self._assert_transaction_open(tx) self._transaction.set_complete() response = self.append_message(0x13) try: self._sync(response) except BrokenWireError as error: tx.mark_broken() raise_from(ConnectionBroken("Transaction broken by disconnection " "during rollback"), error) else: try: self._audit(self._transaction) except Neo4jError as error: tx.mark_broken() raise_from(ConnectionBroken("Failed to rollback transaction"), error) else: return Bookmark(response.metadata.get("bookmark"))
def rollback(self, tx): self._assert_open() self._assert_transaction_open(tx) self._transaction.set_complete() try: self._sync(self.append_message(0x10, "ROLLBACK", {}), self.append_message(0x2F)) except BrokenWireError as error: tx.mark_broken() raise_from(ConnectionBroken("Transaction broken by disconnection " "during rollback"), error) else: try: self._audit(self._transaction) except Neo4jError as error: tx.mark_broken() raise_from(ConnectionBroken("Failed to rollback transaction"), error) else: return Bookmark()
def pull(self, result, n=-1, capacity=-1): self._assert_open() qid = self._assert_result_consumable(result) args = {"n": n, "qid": qid} response = self.append_message(0x3F, args, capacity=capacity) result.append(response, final=(n == -1)) try: self._sync(response) except BrokenWireError as error: result.transaction.mark_broken() raise_from(ConnectionBroken("Transaction broken by disconnection " "during pull"), error) else: self._audit(self._transaction) return response
def discard(self, result): self._assert_open() self._assert_result_consumable(result) response = self.append_message(0x2F) result.append(response, final=True) try: self._sync(response) except BrokenWireError as error: if self._transaction: self._transaction.mark_broken() raise_from(ConnectionBroken("Transaction broken by disconnection " "during discard"), error) else: self._audit(self._transaction) return response
def begin(self, graph_name, readonly=False, # after=None, metadata=None, timeout=None ): self._set_transaction(graph_name, readonly=readonly, # after, metadata, timeout ) responses = (self.append_message(0x10, "BEGIN", self._transaction.extra), self.append_message(0x2F)) try: self._sync(*responses) except BrokenWireError as error: raise_from(ConnectionBroken("Transaction could not begin " "due to disconnection"), error) else: self._audit(self._transaction) return self._transaction
def begin(self, graph_name, readonly=False, # after=None, metadata=None, timeout=None ): self._assert_open() self._assert_no_transaction() self._transaction = BoltTransactionRef(self, graph_name, readonly, # after, metadata, timeout ) response = self.append_message(0x11, self._transaction.extra) try: self._sync(response) except BrokenWireError as error: raise_from(ConnectionBroken("Transaction could not begin " "due to disconnection"), error) else: self._audit(self._transaction) return self._transaction
def pull(self, result, n=-1, capacity=-1): self._assert_open() self._assert_result_consumable(result) if n != -1: raise IndexError("Flow control is not available in this version of Neo4j") response = self.append_message(0x3F, capacity=capacity) result.append(response, final=True) try: self._sync(response) except BrokenWireError as error: if self._transaction: self._transaction.mark_broken() raise_from(ConnectionBroken("Transaction broken by disconnection " "during pull"), error) else: self._audit(self._transaction) return response
def read_message_py2(self): chunks = [] while True: try: hi, lo = self.wire.read(2) except WireError as error: raise_from(ConnectionBroken("Failed to read message"), error) else: if hi == lo == 0: break size = hi << 8 | lo chunks.append(self.wire.read(size)) message = bytearray(b"".join(map(bytes, chunks))) _, n = divmod(message[0], 0x10) try: unpacker = UnpackStream(message, offset=2) fields = [unpacker.unpack() for _ in range(n)] except ValueError as error: raise_from(ProtocolError("Bad message content"), error) else: return message[1], fields
def send(self, final=False): try: return self.wire.send(final=final) except WireError as error: raise_from(ConnectionBroken("Failed to send Bolt messages"), error)