|
| 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 | + |
| 20 | +import { |
| 21 | + Node, |
| 22 | + newError, |
| 23 | + error, |
| 24 | + Relationship, |
| 25 | + UnboundRelationship, |
| 26 | + Path, |
| 27 | + toNumber, |
| 28 | + PathSegment |
| 29 | +} from 'neo4j-driver-core' |
| 30 | + |
| 31 | +import { structure } from '../packstream' |
| 32 | +import { TypeTransformer } from './transformer' |
| 33 | + |
| 34 | +const { PROTOCOL_ERROR } = error |
| 35 | + |
| 36 | +const NODE = 0x4e |
| 37 | +const NODE_STRUCT_SIZE = 3 |
| 38 | + |
| 39 | +const RELATIONSHIP = 0x52 |
| 40 | +const RELATIONSHIP_STRUCT_SIZE = 5 |
| 41 | + |
| 42 | +const UNBOUND_RELATIONSHIP = 0x72 |
| 43 | +const UNBOUND_RELATIONSHIP_STRUCT_SIZE = 3 |
| 44 | + |
| 45 | +const PATH = 0x50 |
| 46 | +const PATH_STRUCT_SIZE = 3 |
| 47 | + |
| 48 | +/** |
| 49 | + * Creates the Node Transformer |
| 50 | + * @returns {TypeTransformer} |
| 51 | + */ |
| 52 | +function createNodeTransformer () { |
| 53 | + return new TypeTransformer({ |
| 54 | + signature: NODE, |
| 55 | + isTypeInstance: object => object instanceof Node, |
| 56 | + toStructure: object => { |
| 57 | + throw newError( |
| 58 | + `It is not allowed to pass nodes in query parameters, given: ${object}`, |
| 59 | + PROTOCOL_ERROR |
| 60 | + ) |
| 61 | + }, |
| 62 | + fromStructure: struct => { |
| 63 | + structure.verifyStructSize('Node', NODE_STRUCT_SIZE, struct.size) |
| 64 | + |
| 65 | + const [identity, labels, properties] = struct.fields |
| 66 | + |
| 67 | + return new Node(identity, labels, properties) |
| 68 | + } |
| 69 | + }) |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Creates the Relationship Transformer |
| 74 | + * @returns {TypeTransformer} |
| 75 | + */ |
| 76 | +function createRelationshipTransformer () { |
| 77 | + return new TypeTransformer({ |
| 78 | + signature: RELATIONSHIP, |
| 79 | + isTypeInstance: object => object instanceof Relationship, |
| 80 | + toStructure: object => { |
| 81 | + throw newError( |
| 82 | + `It is not allowed to pass relationships in query parameters, given: ${object}`, |
| 83 | + PROTOCOL_ERROR |
| 84 | + ) |
| 85 | + }, |
| 86 | + fromStructure: struct => { |
| 87 | + structure.verifyStructSize('Relationship', RELATIONSHIP_STRUCT_SIZE, struct.size) |
| 88 | + |
| 89 | + const [identity, startNodeIdentity, endNodeIdentity, type, properties] = struct.fields |
| 90 | + |
| 91 | + return new Relationship(identity, startNodeIdentity, endNodeIdentity, type, properties) |
| 92 | + } |
| 93 | + }) |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Creates the Unbound Relationship Transformer |
| 98 | + * @returns {TypeTransformer} |
| 99 | + */ |
| 100 | +function createUnboundRelationshipTransformer () { |
| 101 | + return new TypeTransformer({ |
| 102 | + signature: UNBOUND_RELATIONSHIP, |
| 103 | + isTypeInstance: object => object instanceof UnboundRelationship, |
| 104 | + toStructure: object => { |
| 105 | + throw newError( |
| 106 | + `It is not allowed to pass unbound relationships in query parameters, given: ${object}`, |
| 107 | + PROTOCOL_ERROR |
| 108 | + ) |
| 109 | + }, |
| 110 | + fromStructure: struct => { |
| 111 | + structure.verifyStructSize( |
| 112 | + 'UnboundRelationship', |
| 113 | + UNBOUND_RELATIONSHIP_STRUCT_SIZE, |
| 114 | + struct.size |
| 115 | + ) |
| 116 | + |
| 117 | + const [identity, type, properties] = struct.fields |
| 118 | + |
| 119 | + return new UnboundRelationship(identity, type, properties) |
| 120 | + } |
| 121 | + }) |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * Creates the Path Transformer |
| 126 | + * @returns {TypeTransformer} |
| 127 | + */ |
| 128 | +function createPathTransformer () { |
| 129 | + return new TypeTransformer({ |
| 130 | + signature: PATH, |
| 131 | + isTypeInstance: object => object instanceof Path, |
| 132 | + toStructure: object => { |
| 133 | + throw newError( |
| 134 | + `It is not allowed to pass paths in query parameters, given: ${object}`, |
| 135 | + PROTOCOL_ERROR |
| 136 | + ) |
| 137 | + }, |
| 138 | + fromStructure: struct => { |
| 139 | + structure.verifyStructSize('Path', PATH_STRUCT_SIZE, struct.size) |
| 140 | + |
| 141 | + const [nodes, rels, sequence] = struct.fields |
| 142 | + |
| 143 | + const segments = [] |
| 144 | + let prevNode = nodes[0] |
| 145 | + |
| 146 | + for (let i = 0; i < sequence.length; i += 2) { |
| 147 | + const nextNode = nodes[sequence[i + 1]] |
| 148 | + const relIndex = toNumber(sequence[i]) |
| 149 | + let rel |
| 150 | + |
| 151 | + if (relIndex > 0) { |
| 152 | + rel = rels[relIndex - 1] |
| 153 | + if (rel instanceof UnboundRelationship) { |
| 154 | + // To avoid duplication, relationships in a path do not contain |
| 155 | + // information about their start and end nodes, that's instead |
| 156 | + // inferred from the path sequence. This is us inferring (and, |
| 157 | + // for performance reasons remembering) the start/end of a rel. |
| 158 | + rels[relIndex - 1] = rel = rel.bindTo( |
| 159 | + prevNode, |
| 160 | + nextNode |
| 161 | + ) |
| 162 | + } |
| 163 | + } else { |
| 164 | + rel = rels[-relIndex - 1] |
| 165 | + if (rel instanceof UnboundRelationship) { |
| 166 | + // See above |
| 167 | + rels[-relIndex - 1] = rel = rel.bindTo( |
| 168 | + nextNode, |
| 169 | + prevNode |
| 170 | + ) |
| 171 | + } |
| 172 | + } |
| 173 | + // Done hydrating one path segment. |
| 174 | + segments.push(new PathSegment(prevNode, rel, nextNode)) |
| 175 | + prevNode = nextNode |
| 176 | + } |
| 177 | + return new Path(nodes[0], nodes[nodes.length - 1], segments) |
| 178 | + } |
| 179 | + }) |
| 180 | +} |
| 181 | + |
| 182 | +export default { |
| 183 | + createNodeTransformer, |
| 184 | + createRelationshipTransformer, |
| 185 | + createUnboundRelationshipTransformer, |
| 186 | + createPathTransformer |
| 187 | +} |
0 commit comments