Skip to content

Commit f222d80

Browse files
author
Zhen Li
authored
Merge pull request #216 from lutovich/1.2-initial-bookmark-in-session
Allow initial bookmark in session
2 parents 0321198 + 3f22758 commit f222d80

12 files changed

+543
-42
lines changed

src/v1/driver.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,14 @@ class Driver {
108108
* it is returned to the pool, the session will be reset to a clean state and
109109
* made available for others to use.
110110
*
111-
* @param {String} mode of session - optional
111+
* @param {string} [mode=WRITE] the access mode of this session, allowed values are {@link READ} and {@link WRITE}.
112+
* @param {string} [bookmark=null] the initial reference to some previous transaction. Value is optional and
113+
* absence indicates that that the bookmark does not exist or is unknown.
112114
* @return {Session} new session.
113115
*/
114-
session(mode) {
116+
session(mode, bookmark) {
115117
const sessionMode = Driver._validateSessionMode(mode);
116-
return this._createSession(sessionMode, this._connectionProvider);
118+
return this._createSession(sessionMode, this._connectionProvider, bookmark);
117119
}
118120

119121
static _validateSessionMode(rawMode) {
@@ -130,8 +132,8 @@ class Driver {
130132
}
131133

132134
//Extension point
133-
_createSession(mode, connectionProvider) {
134-
return new Session(mode, connectionProvider);
135+
_createSession(mode, connectionProvider, bookmark) {
136+
return new Session(mode, connectionProvider, bookmark);
135137
}
136138

137139
_driverOnErrorCallback(error) {

src/v1/routing-driver.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ class RoutingDriver extends Driver {
3535
return new LoadBalancer(address, connectionPool, driverOnErrorCallback);
3636
}
3737

38-
_createSession(mode, connectionProvider) {
39-
return new RoutingSession(mode, connectionProvider, (error, conn) => {
38+
_createSession(mode, connectionProvider, bookmark) {
39+
return new RoutingSession(mode, connectionProvider, bookmark, (error, conn) => {
4040
if (error.code === SERVICE_UNAVAILABLE || error.code === SESSION_EXPIRED) {
4141
// connection is undefined if error happened before connection was acquired
4242
if (conn) {
@@ -66,8 +66,8 @@ class RoutingDriver extends Driver {
6666
}
6767

6868
class RoutingSession extends Session {
69-
constructor(mode, connectionProvider, onFailedConnection) {
70-
super(mode, connectionProvider);
69+
constructor(mode, connectionProvider, bookmark, onFailedConnection) {
70+
super(mode, connectionProvider, bookmark);
7171
this._onFailedConnection = onFailedConnection;
7272
}
7373

src/v1/session.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,17 @@ class Session {
3434

3535
/**
3636
* @constructor
37-
* @param {string} mode - the default access mode for this session.
38-
* @param connectionProvider - the connection provider to acquire connections from.
37+
* @param {string} mode the default access mode for this session.
38+
* @param {ConnectionProvider} connectionProvider - the connection provider to acquire connections from.
39+
* @param {string} bookmark - the initial bookmark for this session.
3940
*/
40-
constructor(mode, connectionProvider) {
41+
constructor(mode, connectionProvider, bookmark) {
4142
this._mode = mode;
4243
this._readConnectionHolder = new ConnectionHolder(READ, connectionProvider);
4344
this._writeConnectionHolder = new ConnectionHolder(WRITE, connectionProvider);
4445
this._open = true;
4546
this._hasTx = false;
47+
this._lastBookmark = bookmark;
4648
}
4749

4850
/**
@@ -84,11 +86,17 @@ class Session {
8486
*
8587
* While a transaction is open the session cannot be used to run statements outside the transaction.
8688
*
89+
* @param {string} bookmark - a reference to a previous transaction. DEPRECATED: This function is deprecated in
90+
* favour of {@link Driver#session(string)} that accepts an initial bookmark. Session will ensure that all nested
91+
* transactions are chained with bookmarks to guarantee causal consistency.
8792
* @returns {Transaction} - New Transaction
8893
*/
8994
beginTransaction(bookmark) {
9095
if (bookmark) {
91-
assertString(bookmark, "Bookmark");
96+
assertString(bookmark, 'Bookmark');
97+
}
98+
if (typeof bookmark !== 'undefined') {
99+
this._lastBookmark = bookmark;
92100
}
93101

94102
if (this._hasTx) {
@@ -99,21 +107,25 @@ class Session {
99107

100108
this._hasTx = true;
101109

102-
// todo: add mode parameter
103-
const mode = this._mode;
104-
const connectionHolder = this._connectionHolderWithMode(mode);
110+
const connectionHolder = this._connectionHolderWithMode(this._mode);
105111
connectionHolder.initializeConnection();
106112

107113
return new Transaction(connectionHolder, () => {
108114
this._hasTx = false;
109115
},
110-
this._onRunFailure(), bookmark, (bookmark) => {this._lastBookmark = bookmark});
116+
this._onRunFailure(), this._lastBookmark, this._updateBookmark.bind(this));
111117
}
112118

113119
lastBookmark() {
114120
return this._lastBookmark;
115121
}
116122

123+
_updateBookmark(newBookmark) {
124+
if (newBookmark) {
125+
this._lastBookmark = newBookmark;
126+
}
127+
}
128+
117129
/**
118130
* Close this session.
119131
* @param {function()} callback - Function to be called after the session has been closed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
!: AUTO INIT
2+
!: AUTO RESET
3+
!: AUTO PULL_ALL
4+
5+
C: RUN "CALL dbms.cluster.routing.getServers" {}
6+
PULL_ALL
7+
S: SUCCESS {"fields": ["ttl", "servers"]}
8+
RECORD [9223372036854775807, [{"addresses": ["127.0.0.1:9007"],"role": "WRITE"}, {"addresses": ["127.0.0.1:9005"], "role": "READ"},{"addresses": ["127.0.0.1:9001","127.0.0.1:9002"], "role": "ROUTE"}]]
9+
SUCCESS {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
!: AUTO INIT
2+
!: AUTO RESET
3+
!: AUTO PULL_ALL
4+
5+
C: RUN "BEGIN" {"bookmark": "OldBookmark"}
6+
PULL_ALL
7+
S: SUCCESS {}
8+
SUCCESS {}
9+
C: RUN "MATCH (n) RETURN n.name AS name" {}
10+
PULL_ALL
11+
S: SUCCESS {"fields": ["name"]}
12+
RECORD ["Bob"]
13+
RECORD ["Alice"]
14+
SUCCESS {"bookmark": "NewBookmark"}
15+
C: RUN "COMMIT" {}
16+
PULL_ALL
17+
S: SUCCESS {}
18+
SUCCESS {}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
!: AUTO INIT
2+
!: AUTO RESET
3+
!: AUTO PULL_ALL
4+
5+
C: RUN "BEGIN" {"bookmark": "BookmarkA"}
6+
PULL_ALL
7+
S: SUCCESS {}
8+
SUCCESS {}
9+
C: RUN "CREATE (n {name:'Bob'})" {}
10+
PULL_ALL
11+
S: SUCCESS {}
12+
SUCCESS {"bookmark": "BookmarkB"}
13+
C: RUN "COMMIT" {}
14+
PULL_ALL
15+
S: SUCCESS {}
16+
SUCCESS {}
17+
C: RUN "BEGIN" {{{bookmarkOverride}}}
18+
PULL_ALL
19+
S: SUCCESS {}
20+
SUCCESS {}
21+
C: RUN "MATCH (n) RETURN n.name AS name" {}
22+
PULL_ALL
23+
S: SUCCESS {"fields": ["name"]}
24+
RECORD ["Bob"]
25+
SUCCESS {"bookmark": "BookmarkC"}
26+
C: RUN "COMMIT" {}
27+
PULL_ALL
28+
S: SUCCESS {}
29+
SUCCESS {}
30+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
!: AUTO INIT
2+
!: AUTO RESET
3+
!: AUTO PULL_ALL
4+
5+
C: RUN "BEGIN" {"bookmark": "BookmarkA"}
6+
PULL_ALL
7+
S: SUCCESS {}
8+
SUCCESS {}
9+
C: RUN "CREATE (n {name:'Bob'})" {}
10+
PULL_ALL
11+
S: SUCCESS {}
12+
SUCCESS {"bookmark": "BookmarkB"}
13+
C: RUN "COMMIT" {}
14+
PULL_ALL
15+
S: SUCCESS {}
16+
SUCCESS {}
17+
C: RUN "BEGIN" {"bookmark": "BookmarkB"}
18+
PULL_ALL
19+
S: SUCCESS {}
20+
SUCCESS {}
21+
C: RUN "MATCH (n) RETURN n.name AS name" {}
22+
PULL_ALL
23+
S: SUCCESS {"fields": ["name"]}
24+
RECORD ["Bob"]
25+
SUCCESS {"bookmark": "BookmarkC"}
26+
C: RUN "COMMIT" {}
27+
PULL_ALL
28+
S: SUCCESS {}
29+
SUCCESS {}
30+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
!: AUTO INIT
2+
!: AUTO RESET
3+
!: AUTO PULL_ALL
4+
5+
C: RUN "BEGIN" {"bookmark": "OldBookmark"}
6+
PULL_ALL
7+
S: SUCCESS {}
8+
SUCCESS {}
9+
C: RUN "CREATE (n {name:'Bob'})" {}
10+
PULL_ALL
11+
S: SUCCESS {}
12+
SUCCESS {"bookmark": "NewBookmark"}
13+
C: RUN "COMMIT" {}
14+
PULL_ALL
15+
S: SUCCESS {}
16+
SUCCESS {}

0 commit comments

Comments
 (0)