|
| 1 | +/** |
| 2 | + * Copyright (c) 2002-2017 "Neo Technology,"," |
| 3 | + * Network Engine for Objects in Lund AB [http://neotechnology.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 * as util from './util'; |
| 21 | + |
| 22 | +const BOOKMARK_KEY = 'bookmark'; |
| 23 | +const BOOKMARKS_KEY = 'bookmarks'; |
| 24 | +const BOOKMARK_PREFIX = 'neo4j:bookmark:v1:tx'; |
| 25 | + |
| 26 | +const UNKNOWN_BOOKMARK_VALUE = -1; |
| 27 | + |
| 28 | +export default class Bookmark { |
| 29 | + |
| 30 | + /** |
| 31 | + * @constructor |
| 32 | + * @param {string|string[]} values single bookmark as string or multiple bookmarks as a string array. |
| 33 | + */ |
| 34 | + constructor(values) { |
| 35 | + this._values = asStringArray(values); |
| 36 | + this._maxValue = maxBookmark(this._values); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Check if the given bookmark is meaningful and can be send to the database. |
| 41 | + * @return {boolean} returns <code>true</code> bookmark has a value, <code>false</code> otherwise. |
| 42 | + */ |
| 43 | + isEmpty() { |
| 44 | + return this._maxValue === null; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Get maximum value of this bookmark as string. |
| 49 | + * @return {string|null} the maximum value or <code>null</code> if it is not defined. |
| 50 | + */ |
| 51 | + maxBookmarkAsString() { |
| 52 | + return this._maxValue; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Get this bookmark as an object for begin transaction call. |
| 57 | + * @return {object} the value of this bookmark as object. |
| 58 | + */ |
| 59 | + asBeginTransactionParameters() { |
| 60 | + if (this.isEmpty()) { |
| 61 | + return {}; |
| 62 | + } |
| 63 | + |
| 64 | + // Driver sends {bookmark: "max", bookmarks: ["one", "two", "max"]} instead of simple |
| 65 | + // {bookmarks: ["one", "two", "max"]} for backwards compatibility reasons. Old servers can only accept single |
| 66 | + // bookmark that is why driver has to parse and compare given list of bookmarks. This functionality will |
| 67 | + // eventually be removed. |
| 68 | + return { |
| 69 | + [BOOKMARK_KEY]: this._maxValue, |
| 70 | + [BOOKMARKS_KEY]: this._values |
| 71 | + }; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Converts given value to an array. |
| 77 | + * @param {string|string[]} [value=undefined] argument to convert. |
| 78 | + * @return {string[]} value converted to an array. |
| 79 | + */ |
| 80 | +function asStringArray(value) { |
| 81 | + if (!value) { |
| 82 | + return []; |
| 83 | + } |
| 84 | + |
| 85 | + if (util.isString(value)) { |
| 86 | + return [value]; |
| 87 | + } |
| 88 | + |
| 89 | + if (Array.isArray(value)) { |
| 90 | + const result = []; |
| 91 | + for (let i = 0; i < value.length; i++) { |
| 92 | + const element = value[i]; |
| 93 | + if (!util.isString(element)) { |
| 94 | + throw new TypeError(`Bookmark should be a string, given: '${element}'`); |
| 95 | + } |
| 96 | + result.push(element); |
| 97 | + } |
| 98 | + return result; |
| 99 | + } |
| 100 | + |
| 101 | + throw new TypeError(`Bookmark should either be a string or a string array, given: '${value}'`); |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Find latest bookmark in the given array of bookmarks. |
| 106 | + * @param {string[]} bookmarks array of bookmarks. |
| 107 | + * @return {string|null} latest bookmark value. |
| 108 | + */ |
| 109 | +function maxBookmark(bookmarks) { |
| 110 | + if (!bookmarks || bookmarks.length === 0) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + let maxBookmark = bookmarks[0]; |
| 115 | + let maxValue = bookmarkValue(maxBookmark); |
| 116 | + |
| 117 | + for (let i = 1; i < bookmarks.length; i++) { |
| 118 | + const bookmark = bookmarks[i]; |
| 119 | + const value = bookmarkValue(bookmark); |
| 120 | + |
| 121 | + if (value > maxValue) { |
| 122 | + maxBookmark = bookmark; |
| 123 | + maxValue = value; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return maxBookmark; |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * Calculate numeric value for the given bookmark. |
| 132 | + * @param {string} bookmark argument to get numeric value for. |
| 133 | + * @return {number} value of the bookmark. |
| 134 | + */ |
| 135 | +function bookmarkValue(bookmark) { |
| 136 | + if (bookmark && bookmark.indexOf(BOOKMARK_PREFIX) === 0) { |
| 137 | + const result = parseInt(bookmark.substring(BOOKMARK_PREFIX.length)); |
| 138 | + return result ? result : UNKNOWN_BOOKMARK_VALUE; |
| 139 | + } |
| 140 | + return UNKNOWN_BOOKMARK_VALUE; |
| 141 | +} |
0 commit comments