1+ 'use strict' ;
2+
3+ Object . defineProperty ( exports , "__esModule" , {
4+ value : true
5+ } ) ;
6+
7+ var _defineProperty2 = require ( 'babel-runtime/helpers/defineProperty' ) ;
8+
9+ var _defineProperty3 = _interopRequireDefault ( _defineProperty2 ) ;
10+
11+ var _classCallCheck2 = require ( 'babel-runtime/helpers/classCallCheck' ) ;
12+
13+ var _classCallCheck3 = _interopRequireDefault ( _classCallCheck2 ) ;
14+
15+ var _createClass2 = require ( 'babel-runtime/helpers/createClass' ) ;
16+
17+ var _createClass3 = _interopRequireDefault ( _createClass2 ) ;
18+
19+ var _util = require ( './util' ) ;
20+
21+ var util = _interopRequireWildcard ( _util ) ;
22+
23+ function _interopRequireWildcard ( obj ) { if ( obj && obj . __esModule ) { return obj ; } else { var newObj = { } ; if ( obj != null ) { for ( var key in obj ) { if ( Object . prototype . hasOwnProperty . call ( obj , key ) ) newObj [ key ] = obj [ key ] ; } } newObj . default = obj ; return newObj ; } }
24+
25+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
26+
27+ var BOOKMARK_KEY = 'bookmark' ; /**
28+ * Copyright (c) 2002-2017 "Neo Technology,","
29+ * Network Engine for Objects in Lund AB [http://neotechnology.com]
30+ *
31+ * This file is part of Neo4j.
32+ *
33+ * Licensed under the Apache License, Version 2.0 (the "License");
34+ * you may not use this file except in compliance with the License.
35+ * You may obtain a copy of the License at
36+ *
37+ * http://www.apache.org/licenses/LICENSE-2.0
38+ *
39+ * Unless required by applicable law or agreed to in writing, software
40+ * distributed under the License is distributed on an "AS IS" BASIS,
41+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42+ * See the License for the specific language governing permissions and
43+ * limitations under the License.
44+ */
45+
46+ var BOOKMARKS_KEY = 'bookmarks' ;
47+ var BOOKMARK_PREFIX = 'neo4j:bookmark:v1:tx' ;
48+
49+ var UNKNOWN_BOOKMARK_VALUE = - 1 ;
50+
51+ var Bookmark = function ( ) {
52+
53+ /**
54+ * @constructor
55+ * @param {string|string[] } values single bookmark as string or multiple bookmarks as a string array.
56+ */
57+ function Bookmark ( values ) {
58+ ( 0 , _classCallCheck3 . default ) ( this , Bookmark ) ;
59+
60+ this . _values = asStringArray ( values ) ;
61+ this . _maxValue = maxBookmark ( this . _values ) ;
62+ }
63+
64+ /**
65+ * Check if the given bookmark is meaningful and can be send to the database.
66+ * @return {boolean } returns <code>true</code> bookmark has a value, <code>false</code> otherwise.
67+ */
68+
69+
70+ ( 0 , _createClass3 . default ) ( Bookmark , [ {
71+ key : 'isEmpty' ,
72+ value : function isEmpty ( ) {
73+ return this . _maxValue === null ;
74+ }
75+
76+ /**
77+ * Get maximum value of this bookmark as string.
78+ * @return {string|null } the maximum value or <code>null</code> if it is not defined.
79+ */
80+
81+ } , {
82+ key : 'maxBookmarkAsString' ,
83+ value : function maxBookmarkAsString ( ) {
84+ return this . _maxValue ;
85+ }
86+
87+ /**
88+ * Get this bookmark as an object for begin transaction call.
89+ * @return {object } the value of this bookmark as object.
90+ */
91+
92+ } , {
93+ key : 'asBeginTransactionParameters' ,
94+ value : function asBeginTransactionParameters ( ) {
95+ var _ref ;
96+
97+ if ( this . isEmpty ( ) ) {
98+ return { } ;
99+ }
100+
101+ // Driver sends {bookmark: "max", bookmarks: ["one", "two", "max"]} instead of simple
102+ // {bookmarks: ["one", "two", "max"]} for backwards compatibility reasons. Old servers can only accept single
103+ // bookmark that is why driver has to parse and compare given list of bookmarks. This functionality will
104+ // eventually be removed.
105+ return _ref = { } , ( 0 , _defineProperty3 . default ) ( _ref , BOOKMARK_KEY , this . _maxValue ) , ( 0 , _defineProperty3 . default ) ( _ref , BOOKMARKS_KEY , this . _values ) , _ref ;
106+ }
107+ } ] ) ;
108+ return Bookmark ;
109+ } ( ) ;
110+
111+ /**
112+ * Converts given value to an array.
113+ * @param {string|string[] } [value=undefined] argument to convert.
114+ * @return {string[] } value converted to an array.
115+ */
116+
117+
118+ exports . default = Bookmark ;
119+ function asStringArray ( value ) {
120+ if ( ! value ) {
121+ return [ ] ;
122+ }
123+
124+ if ( util . isString ( value ) ) {
125+ return [ value ] ;
126+ }
127+
128+ if ( Array . isArray ( value ) ) {
129+ var result = [ ] ;
130+ for ( var i = 0 ; i < value . length ; i ++ ) {
131+ var element = value [ i ] ;
132+ if ( ! util . isString ( element ) ) {
133+ throw new TypeError ( 'Bookmark should be a string, given: \'' + element + '\'' ) ;
134+ }
135+ result . push ( element ) ;
136+ }
137+ return result ;
138+ }
139+
140+ throw new TypeError ( 'Bookmark should either be a string or a string array, given: \'' + value + '\'' ) ;
141+ }
142+
143+ /**
144+ * Find latest bookmark in the given array of bookmarks.
145+ * @param {string[] } bookmarks array of bookmarks.
146+ * @return {string|null } latest bookmark value.
147+ */
148+ function maxBookmark ( bookmarks ) {
149+ if ( ! bookmarks || bookmarks . length === 0 ) {
150+ return null ;
151+ }
152+
153+ var maxBookmark = bookmarks [ 0 ] ;
154+ var maxValue = bookmarkValue ( maxBookmark ) ;
155+
156+ for ( var i = 1 ; i < bookmarks . length ; i ++ ) {
157+ var bookmark = bookmarks [ i ] ;
158+ var value = bookmarkValue ( bookmark ) ;
159+
160+ if ( value > maxValue ) {
161+ maxBookmark = bookmark ;
162+ maxValue = value ;
163+ }
164+ }
165+
166+ return maxBookmark ;
167+ }
168+
169+ /**
170+ * Calculate numeric value for the given bookmark.
171+ * @param {string } bookmark argument to get numeric value for.
172+ * @return {number } value of the bookmark.
173+ */
174+ function bookmarkValue ( bookmark ) {
175+ if ( bookmark && bookmark . indexOf ( BOOKMARK_PREFIX ) === 0 ) {
176+ var result = parseInt ( bookmark . substring ( BOOKMARK_PREFIX . length ) ) ;
177+ return result ? result : UNKNOWN_BOOKMARK_VALUE ;
178+ }
179+ return UNKNOWN_BOOKMARK_VALUE ;
180+ }
0 commit comments