Skip to content

Commit 06aed3d

Browse files
authored
Remove circular dependencies (neo4j#662)
The circular dependencies were creating high couple between the files and making the file resolution not work in @rollup/rollup-plugin-node-resolve * Removes circular dependencies temporal-util and temporal-types * Removes circular dependency between buf/combined-buf.js, node/index.js and node/node-utf8.js * Removes circular dependency between driver.js and internal/connection-verifier.js * Removes circular dependency between driver and session
1 parent 5ba26b3 commit 06aed3d

9 files changed

+272
-190
lines changed

src/driver.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -275,17 +275,6 @@ class Driver {
275275
})
276276
}
277277

278-
/**
279-
* @protected
280-
*/
281-
static _validateSessionMode (rawMode) {
282-
const mode = rawMode || WRITE
283-
if (mode !== ACCESS_MODE_READ && mode !== ACCESS_MODE_WRITE) {
284-
throw newError('Illegal session mode ' + mode)
285-
}
286-
return mode
287-
}
288-
289278
/**
290279
* @private
291280
*/
@@ -296,7 +285,7 @@ class Driver {
296285
reactive,
297286
fetchSize
298287
}) {
299-
const sessionMode = Driver._validateSessionMode(defaultAccessMode)
288+
const sessionMode = Session._validateSessionMode(defaultAccessMode)
300289
const connectionProvider = this._getOrCreateConnectionProvider()
301290
const bookmark = bookmarkOrBookmarks
302291
? new Bookmark(bookmarkOrBookmarks)

src/internal/connectivity-verifier.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
import ConnectionHolder from './connection-holder'
21-
import { READ } from '../driver'
21+
import { ACCESS_MODE_READ } from './constants'
2222
import { ResultStreamObserver } from './bolt'
2323

2424
/**
@@ -49,7 +49,7 @@ export default class ConnectivityVerifier {
4949
*/
5050
function acquireAndReleaseDummyConnection (connectionProvider, database) {
5151
const connectionHolder = new ConnectionHolder({
52-
mode: READ,
52+
mode: ACCESS_MODE_READ,
5353
database,
5454
connectionProvider
5555
})

src/internal/node/node-utf8.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
* limitations under the License.
1818
*/
1919

20-
import CombinedBuffer from '../buf/combined-buf'
2120
import NodeBuffer from './node-buf'
2221
import { newError } from '../../error'
2322
import node from 'buffer'
@@ -30,9 +29,9 @@ function encode (str) {
3029
}
3130

3231
function decode (buffer, length) {
33-
if (buffer instanceof NodeBuffer) {
32+
if (Object.prototype.hasOwnProperty.call(buffer, '_buffer')) {
3433
return decodeNodeBuffer(buffer, length)
35-
} else if (buffer instanceof CombinedBuffer) {
34+
} else if (Object.prototype.hasOwnProperty.call(buffer, '_buffers')) {
3635
return decodeCombinedBuffer(buffer, length)
3736
} else {
3837
throw newError(`Don't know how to decode strings from '${buffer}'`)

src/internal/packstream-v2.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@ import {
3333
import { int, isInt } from '../integer'
3434
import {
3535
dateToEpochDay,
36+
localDateTimeToEpochSecond,
37+
localTimeToNanoOfDay
38+
} from './temporal-util'
39+
40+
import {
3641
epochDayToDate,
3742
epochSecondAndNanoToLocalDateTime,
38-
localDateTimeToEpochSecond,
39-
localTimeToNanoOfDay,
4043
nanoOfDayToLocalTime
41-
} from './temporal-util'
44+
} from './temporal-factory'
4245

4346
const POINT_2D = 0x58
4447
const POINT_2D_STRUCT_SIZE = 3

src/internal/temporal-factory.js

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/**
2+
* Copyright (c) 2002-2020 "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 { Date, LocalDateTime, LocalTime } from '../temporal-types'
20+
import { int } from '../integer'
21+
import {
22+
DAYS_0000_TO_1970,
23+
DAYS_PER_400_YEAR_CYCLE,
24+
NANOS_PER_HOUR,
25+
NANOS_PER_MINUTE,
26+
NANOS_PER_SECOND,
27+
SECONDS_PER_DAY,
28+
floorDiv,
29+
floorMod
30+
} from './temporal-util'
31+
32+
/**
33+
* Converts given epoch day to a local date.
34+
* @param {Integer|number|string} epochDay the epoch day to convert.
35+
* @return {Date} the date representing the epoch day in years, months and days.
36+
*/
37+
export function epochDayToDate (epochDay) {
38+
epochDay = int(epochDay)
39+
40+
let zeroDay = epochDay.add(DAYS_0000_TO_1970).subtract(60)
41+
let adjust = int(0)
42+
if (zeroDay.lessThan(0)) {
43+
const adjustCycles = zeroDay
44+
.add(1)
45+
.div(DAYS_PER_400_YEAR_CYCLE)
46+
.subtract(1)
47+
adjust = adjustCycles.multiply(400)
48+
zeroDay = zeroDay.add(adjustCycles.multiply(-DAYS_PER_400_YEAR_CYCLE))
49+
}
50+
let year = zeroDay
51+
.multiply(400)
52+
.add(591)
53+
.div(DAYS_PER_400_YEAR_CYCLE)
54+
let dayOfYearEst = zeroDay.subtract(
55+
year
56+
.multiply(365)
57+
.add(year.div(4))
58+
.subtract(year.div(100))
59+
.add(year.div(400))
60+
)
61+
if (dayOfYearEst.lessThan(0)) {
62+
year = year.subtract(1)
63+
dayOfYearEst = zeroDay.subtract(
64+
year
65+
.multiply(365)
66+
.add(year.div(4))
67+
.subtract(year.div(100))
68+
.add(year.div(400))
69+
)
70+
}
71+
year = year.add(adjust)
72+
const marchDayOfYear = dayOfYearEst
73+
74+
const marchMonth = marchDayOfYear
75+
.multiply(5)
76+
.add(2)
77+
.div(153)
78+
const month = marchMonth
79+
.add(2)
80+
.modulo(12)
81+
.add(1)
82+
const day = marchDayOfYear
83+
.subtract(
84+
marchMonth
85+
.multiply(306)
86+
.add(5)
87+
.div(10)
88+
)
89+
.add(1)
90+
year = year.add(marchMonth.div(10))
91+
92+
return new Date(year, month, day)
93+
}
94+
95+
/**
96+
* Converts nanoseconds of the day into local time.
97+
* @param {Integer|number|string} nanoOfDay the nanoseconds of the day to convert.
98+
* @return {LocalTime} the local time representing given nanoseconds of the day.
99+
*/
100+
export function nanoOfDayToLocalTime (nanoOfDay) {
101+
nanoOfDay = int(nanoOfDay)
102+
103+
const hour = nanoOfDay.div(NANOS_PER_HOUR)
104+
nanoOfDay = nanoOfDay.subtract(hour.multiply(NANOS_PER_HOUR))
105+
106+
const minute = nanoOfDay.div(NANOS_PER_MINUTE)
107+
nanoOfDay = nanoOfDay.subtract(minute.multiply(NANOS_PER_MINUTE))
108+
109+
const second = nanoOfDay.div(NANOS_PER_SECOND)
110+
const nanosecond = nanoOfDay.subtract(second.multiply(NANOS_PER_SECOND))
111+
112+
return new LocalTime(hour, minute, second, nanosecond)
113+
}
114+
115+
/**
116+
* Converts given epoch second and nanosecond adjustment into a local date time object.
117+
* @param {Integer|number|string} epochSecond the epoch second to use.
118+
* @param {Integer|number|string} nano the nanosecond to use.
119+
* @return {LocalDateTime} the local date time representing given epoch second and nano.
120+
*/
121+
export function epochSecondAndNanoToLocalDateTime (epochSecond, nano) {
122+
const epochDay = floorDiv(epochSecond, SECONDS_PER_DAY)
123+
const secondsOfDay = floorMod(epochSecond, SECONDS_PER_DAY)
124+
const nanoOfDay = secondsOfDay.multiply(NANOS_PER_SECOND).add(nano)
125+
126+
const localDate = epochDayToDate(epochDay)
127+
const localTime = nanoOfDayToLocalTime(nanoOfDay)
128+
return new LocalDateTime(
129+
localDate.year,
130+
localDate.month,
131+
localDate.day,
132+
localTime.hour,
133+
localTime.minute,
134+
localTime.second,
135+
localTime.nanosecond
136+
)
137+
}

0 commit comments

Comments
 (0)