Skip to content

Commit 2589eb6

Browse files
committed
Pass specified bookmark down to the discovery invocation
1 parent 07ec4cb commit 2589eb6

25 files changed

+1065
-711
lines changed

src/internal/bookmark.js

Lines changed: 18 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@
1919

2020
import * as util from './util'
2121

22-
const BOOKMARK_KEY = 'bookmark'
2322
const BOOKMARKS_KEY = 'bookmarks'
24-
const BOOKMARK_PREFIX = 'neo4j:bookmark:v1:tx'
25-
26-
const UNKNOWN_BOOKMARK_VALUE = -1
2723

2824
export default class Bookmark {
2925
/**
@@ -32,7 +28,6 @@ export default class Bookmark {
3228
*/
3329
constructor (values) {
3430
this._values = asStringArray(values)
35-
this._maxValue = maxBookmark(this._values)
3631
}
3732

3833
static empty () {
@@ -44,15 +39,7 @@ export default class Bookmark {
4439
* @return {boolean} returns `true` bookmark has a value, `false` otherwise.
4540
*/
4641
isEmpty () {
47-
return this._maxValue === null
48-
}
49-
50-
/**
51-
* Get maximum value of this bookmark as string.
52-
* @return {string|null} the maximum value or `null` if it is not defined.
53-
*/
54-
maxBookmarkAsString () {
55-
return this._maxValue
42+
return this._values.length === 0
5643
}
5744

5845
/**
@@ -77,7 +64,6 @@ export default class Bookmark {
7764
// bookmark that is why driver has to parse and compare given list of bookmarks. This functionality will
7865
// eventually be removed.
7966
return {
80-
[BOOKMARK_KEY]: this._maxValue,
8167
[BOOKMARKS_KEY]: this._values
8268
}
8369
}
@@ -87,7 +73,7 @@ const EMPTY_BOOKMARK = new Bookmark(null)
8773

8874
/**
8975
* Converts given value to an array.
90-
* @param {string|string[]} [value=undefined] argument to convert.
76+
* @param {string|string[]|Array} [value=undefined] argument to convert.
9177
* @return {string[]} value converted to an array.
9278
*/
9379
function asStringArray (value) {
@@ -101,13 +87,14 @@ function asStringArray (value) {
10187

10288
if (Array.isArray(value)) {
10389
const result = []
104-
for (let i = 0; i < value.length; i++) {
105-
const element = value[i]
90+
const flattenedValue = flattenArray(value)
91+
for (let i = 0; i < flattenedValue.length; i++) {
92+
const element = flattenedValue[i]
10693
// if it is undefined or null, ignore it
10794
if (element !== undefined && element !== null) {
10895
if (!util.isString(element)) {
10996
throw new TypeError(
110-
`Bookmark should be a string, given: '${element}'`
97+
`Bookmark value should be a string, given: '${element}'`
11198
)
11299
}
113100
result.push(element)
@@ -122,40 +109,17 @@ function asStringArray (value) {
122109
}
123110

124111
/**
125-
* Find latest bookmark in the given array of bookmarks.
126-
* @param {string[]} bookmarks array of bookmarks.
127-
* @return {string|null} latest bookmark value.
128-
*/
129-
function maxBookmark (bookmarks) {
130-
if (!bookmarks || bookmarks.length === 0) {
131-
return null
132-
}
133-
134-
let maxBookmark = bookmarks[0]
135-
let maxValue = bookmarkValue(maxBookmark)
136-
137-
for (let i = 1; i < bookmarks.length; i++) {
138-
const bookmark = bookmarks[i]
139-
const value = bookmarkValue(bookmark)
140-
141-
if (value > maxValue) {
142-
maxBookmark = bookmark
143-
maxValue = value
144-
}
145-
}
146-
147-
return maxBookmark
148-
}
149-
150-
/**
151-
* Calculate numeric value for the given bookmark.
152-
* @param {string} bookmark argument to get numeric value for.
153-
* @return {number} value of the bookmark.
112+
* Recursively flattens an array so that the result becomes a single array
113+
* of values, which does not include any sub-arrays
114+
*
115+
* @param {Array} value
154116
*/
155-
function bookmarkValue (bookmark) {
156-
if (bookmark && bookmark.indexOf(BOOKMARK_PREFIX) === 0) {
157-
const result = parseInt(bookmark.substring(BOOKMARK_PREFIX.length))
158-
return result || UNKNOWN_BOOKMARK_VALUE
159-
}
160-
return UNKNOWN_BOOKMARK_VALUE
117+
function flattenArray (values) {
118+
return values.reduce(
119+
(dest, value) =>
120+
Array.isArray(value)
121+
? dest.concat(flattenArray(value))
122+
: dest.concat(value),
123+
[]
124+
)
161125
}

src/internal/connection-holder.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import { newError } from '../error'
2121
import { assertString } from './util'
2222
import { ACCESS_MODE_WRITE } from './constants'
23+
import Bookmark from './bookmark'
2324

2425
/**
2526
* Utility to lazily initialize connections and return them back to the pool when unused.
@@ -34,10 +35,12 @@ export default class ConnectionHolder {
3435
constructor ({
3536
mode = ACCESS_MODE_WRITE,
3637
database = '',
38+
bookmark,
3739
connectionProvider
3840
} = {}) {
3941
this._mode = mode
4042
this._database = database ? assertString(database, 'database') : ''
43+
this._bookmark = bookmark || Bookmark.empty()
4144
this._connectionProvider = connectionProvider
4245
this._referenceCount = 0
4346
this._connectionPromise = Promise.resolve(null)
@@ -65,10 +68,11 @@ export default class ConnectionHolder {
6568
*/
6669
initializeConnection () {
6770
if (this._referenceCount === 0) {
68-
this._connectionPromise = this._connectionProvider.acquireConnection(
69-
this._mode,
70-
this._database
71-
)
71+
this._connectionPromise = this._connectionProvider.acquireConnection({
72+
accessMode: this._mode,
73+
database: this._database,
74+
bookmark: this._bookmark
75+
})
7276
}
7377
this._referenceCount++
7478
}

src/internal/connection-provider-direct.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default class DirectConnectionProvider extends PooledConnectionProvider {
2727
this._address = address
2828
}
2929

30-
acquireConnection (accessMode, database) {
30+
acquireConnection ({ accessMode, database, bookmarks } = {}) {
3131
return this._connectionPool
3232
.acquire(this._address)
3333
.then(connection => new DelegateConnection(connection, null))

0 commit comments

Comments
 (0)