|
| 1 | +/** |
| 2 | + * Copyright (c) "Neo4j" |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +import BoltProtocolV43 from './bolt-protocol-v4x3' |
| 20 | + |
| 21 | +import { internal } from 'neo4j-driver-core' |
| 22 | +import RequestMessage, { ALL } from './request-message' |
| 23 | +import { RouteObserver, ResultStreamObserver } from './stream-observers' |
| 24 | + |
| 25 | +const { |
| 26 | + constants: { BOLT_PROTOCOL_V4_4 }, |
| 27 | + bookmark: { Bookmark }, |
| 28 | +} = internal |
| 29 | + |
| 30 | +export default class BoltProtocol extends BoltProtocolV43 { |
| 31 | + get version() { |
| 32 | + return BOLT_PROTOCOL_V4_4 |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Request routing information |
| 37 | + * |
| 38 | + * @param {Object} param - |
| 39 | + * @param {object} param.routingContext The routing context used to define the routing table. |
| 40 | + * Multi-datacenter deployments is one of its use cases |
| 41 | + * @param {string} param.databaseName The database name |
| 42 | + * @param {Bookmark} params.sessionContext.bookmark The bookmark used for request the routing table |
| 43 | + * @param {function(err: Error)} param.onError |
| 44 | + * @param {function(RawRoutingTable)} param.onCompleted |
| 45 | + * @returns {RouteObserver} the route observer |
| 46 | + */ |
| 47 | + requestRoutingInformation ({ |
| 48 | + routingContext = {}, |
| 49 | + databaseName = null, |
| 50 | + impersonatedUser = null, |
| 51 | + sessionContext = {}, |
| 52 | + onError, |
| 53 | + onCompleted |
| 54 | + }) { |
| 55 | + const observer = new RouteObserver({ |
| 56 | + onProtocolError: this._onProtocolError, |
| 57 | + onError, |
| 58 | + onCompleted |
| 59 | + }) |
| 60 | + const bookmark = sessionContext.bookmark || Bookmark.empty() |
| 61 | + this.write( |
| 62 | + RequestMessage.routeV4x4(routingContext, bookmark.values(), { databaseName, impersonatedUser }), |
| 63 | + observer, |
| 64 | + true |
| 65 | + ) |
| 66 | + |
| 67 | + return observer |
| 68 | + } |
| 69 | + |
| 70 | + run ( |
| 71 | + query, |
| 72 | + parameters, |
| 73 | + { |
| 74 | + bookmark, |
| 75 | + txConfig, |
| 76 | + database, |
| 77 | + mode, |
| 78 | + impersonatedUser, |
| 79 | + beforeKeys, |
| 80 | + afterKeys, |
| 81 | + beforeError, |
| 82 | + afterError, |
| 83 | + beforeComplete, |
| 84 | + afterComplete, |
| 85 | + flush = true, |
| 86 | + reactive = false, |
| 87 | + fetchSize = ALL |
| 88 | + } = {} |
| 89 | + ) { |
| 90 | + const observer = new ResultStreamObserver({ |
| 91 | + server: this._server, |
| 92 | + reactive: reactive, |
| 93 | + fetchSize: fetchSize, |
| 94 | + moreFunction: this._requestMore.bind(this), |
| 95 | + discardFunction: this._requestDiscard.bind(this), |
| 96 | + beforeKeys, |
| 97 | + afterKeys, |
| 98 | + beforeError, |
| 99 | + afterError, |
| 100 | + beforeComplete, |
| 101 | + afterComplete |
| 102 | + }) |
| 103 | + |
| 104 | + const flushRun = reactive |
| 105 | + this.write( |
| 106 | + RequestMessage.runWithMetadata(query, parameters, { |
| 107 | + bookmark, |
| 108 | + txConfig, |
| 109 | + database, |
| 110 | + mode, |
| 111 | + impersonatedUser |
| 112 | + }), |
| 113 | + observer, |
| 114 | + flushRun && flush |
| 115 | + ) |
| 116 | + |
| 117 | + if (!reactive) { |
| 118 | + this.write(RequestMessage.pull({ n: fetchSize }), observer, flush) |
| 119 | + } |
| 120 | + |
| 121 | + return observer |
| 122 | + } |
| 123 | + |
| 124 | + beginTransaction ({ |
| 125 | + bookmark, |
| 126 | + txConfig, |
| 127 | + database, |
| 128 | + mode, |
| 129 | + impersonatedUser, |
| 130 | + beforeError, |
| 131 | + afterError, |
| 132 | + beforeComplete, |
| 133 | + afterComplete |
| 134 | + } = {}) { |
| 135 | + const observer = new ResultStreamObserver({ |
| 136 | + server: this._server, |
| 137 | + beforeError, |
| 138 | + afterError, |
| 139 | + beforeComplete, |
| 140 | + afterComplete |
| 141 | + }) |
| 142 | + observer.prepareToHandleSingleResponse() |
| 143 | + |
| 144 | + this.write( |
| 145 | + RequestMessage.begin({ bookmark, txConfig, database, mode, impersonatedUser }), |
| 146 | + observer, |
| 147 | + true |
| 148 | + ) |
| 149 | + |
| 150 | + return observer |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments