Skip to content

Commit b184b4c

Browse files
committed
Merge branch '1.0' into 1.1
2 parents 647dfd0 + 925e0f9 commit b184b4c

File tree

6 files changed

+87
-27
lines changed

6 files changed

+87
-27
lines changed

src/v1/result-summary.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,16 @@ class ResultSummary {
3333
constructor(statement, parameters, metadata) {
3434
this.statement = {text: statement, parameters};
3535
this.statementType = metadata.type;
36-
this.updateStatistics = new StatementStatistics(metadata.stats || {});
36+
let counters = new StatementStatistics(metadata.stats || {});
37+
this.counters = counters;
38+
//for backwards compatibility, remove in future version
39+
this.updateStatistics = counters;
3740
this.plan = metadata.plan || metadata.profile ? new Plan(metadata.plan || metadata.profile) : false;
3841
this.profile = metadata.profile ? new ProfiledPlan(metadata.profile) : false;
3942
this.notifications = this._buildNotifications(metadata.notifications);
43+
this.resultConsumedAfter = metadata.result_consumed_after;
44+
this.resultAvailableAfter = metadata.result_available_after;
4045
}
41-
4246
_buildNotifications(notifications) {
4347
if(!notifications) {
4448
return [];

src/v1/result.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ class Result {
3535
* @param {StreamObserver} streamObserver
3636
* @param {mixed} statement - Cypher statement to execute
3737
* @param {Object} parameters - Map with parameters to use in statement
38+
* @param metaSupplier function, when called provides metadata
3839
*/
39-
constructor(streamObserver, statement, parameters) {
40+
constructor(streamObserver, statement, parameters, metaSupplier) {
4041
this._streamObserver = streamObserver;
4142
this._p = null;
4243
this._statement = statement;
4344
this._parameters = parameters || {};
45+
this._metaSupplier = metaSupplier || function(){return {};};
4446
}
4547

4648
/**
@@ -102,7 +104,15 @@ class Result {
102104
*/
103105
subscribe(observer) {
104106
let onCompletedOriginal = observer.onCompleted;
107+
let self = this;
105108
let onCompletedWrapper = (metadata) => {
109+
110+
let additionalMeta = self._metaSupplier();
111+
for(var key in additionalMeta) {
112+
if (additionalMeta.hasOwnProperty(key)) {
113+
metadata[key] = additionalMeta[key];
114+
}
115+
}
106116
let sum = new ResultSummary(this._statement, this._parameters, metadata);
107117
onCompletedOriginal.call(observer, sum);
108118
};

src/v1/session.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import StreamObserver from './internal/stream-observer';
2121
import Result from './result';
2222
import Transaction from './transaction';
23+
import {Integer, int} from "./integer";
2324
import {newError} from "./error";
2425

2526
/**
@@ -57,7 +58,7 @@ class Session {
5758
parameters = statement.parameters || {};
5859
statement = statement.text;
5960
}
60-
let streamObserver = new StreamObserver();
61+
let streamObserver = new _RunObserver();
6162
if (!this._hasTx) {
6263
this._conn.run(statement, parameters, streamObserver);
6364
this._conn.pullAll(streamObserver);
@@ -67,7 +68,7 @@ class Session {
6768
+ "session with an open transaction; either run from within the "
6869
+ "transaction or use a different session."));
6970
}
70-
return new Result( streamObserver, statement, parameters );
71+
return new Result( streamObserver, statement, parameters, () => streamObserver.meta() );
7172
}
7273

7374
/**
@@ -107,4 +108,25 @@ class Session {
107108
}
108109
}
109110

111+
/** Internal stream observer used for transactional results*/
112+
class _RunObserver extends StreamObserver {
113+
constructor() {
114+
super();
115+
this._meta = {};
116+
}
117+
118+
onCompleted(meta) {
119+
super.onCompleted(meta);
120+
for(var key in meta){
121+
if(meta.hasOwnProperty(key)){
122+
this._meta[key]=meta[key];
123+
}
124+
}
125+
}
126+
127+
meta() {
128+
return this._meta;
129+
}
130+
}
131+
110132
export default Session;

test/v1/examples.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ describe('examples', function() {
8888
var s = driver.session();
8989
s.run( "CREATE (p:Person { name: {name} })", {name: "The One"} )
9090
.then( function(result) {
91-
var theOnesCreated = result.summary.updateStatistics.nodesCreated();
91+
var theOnesCreated = result.summary.counters.nodesCreated();
9292
console.log(theOnesCreated);
9393
s.close();
9494
driver.close();
@@ -106,7 +106,7 @@ describe('examples', function() {
106106
.run( "CREATE (person:Person {name: {name}})", {name: "Arthur"} )
107107
// end::statement[]
108108
.then( function(result) {
109-
var theOnesCreated = result.summary.updateStatistics.nodesCreated();
109+
var theOnesCreated = result.summary.counters.nodesCreated();
110110
console.log("There were " + theOnesCreated + " the ones created.")
111111
})
112112
.then(function() {
@@ -122,7 +122,7 @@ describe('examples', function() {
122122
.run( "CREATE (p:Person { name: 'Arthur' })" )
123123
// end::statement-without-parameters[]
124124
.then( function(result) {
125-
var theOnesCreated = result.summary.updateStatistics.nodesCreated();
125+
var theOnesCreated = result.summary.counters.nodesCreated();
126126
console.log("There were " + theOnesCreated + " the ones created.");
127127
});
128128

test/v1/session.test.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ var Session = require("../../lib/v1/session");
2323

2424
describe('session', function () {
2525

26-
var driver, session;
26+
var driver, session, server;
2727

2828
beforeEach(function (done) {
2929
driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
30+
driver.onCompleted = function (meta) {
31+
server = meta['server'];
32+
};
3033
session = driver.session();
3134

3235
session.run("MATCH (n) DETACH DELETE n").then(done);
@@ -151,13 +154,35 @@ describe('session', function () {
151154
var sum = result.summary;
152155
expect(sum.statement.text).toBe(statement);
153156
expect(sum.statement.parameters).toBe(params);
154-
expect(sum.updateStatistics.containsUpdates()).toBe(true);
155-
expect(sum.updateStatistics.nodesCreated()).toBe(1);
157+
expect(sum.counters.containsUpdates()).toBe(true);
158+
expect(sum.counters.nodesCreated()).toBe(1);
156159
expect(sum.statementType).toBe(StatementType.READ_WRITE);
157160
done();
158161
});
159162
});
160163

164+
it('should expose execution time information when using 3.1 and onwards', function (done) {
165+
166+
//lazy way of checking the version number
167+
//if server has been set we know it is at least
168+
//3.1 (todo actually parse the version string)
169+
if (!server) {
170+
done();
171+
return;
172+
}
173+
// Given
174+
var statement = "UNWIND range(1,10000) AS n RETURN n AS number";
175+
// When & Then
176+
177+
session.run(statement)
178+
.then(function (result) {
179+
var sum = result.summary;
180+
expect(sum.resultAvailableAfter.toInt()).toBeGreaterThan(0);
181+
expect(sum.resultConsumedAfter.toInt()).toBeGreaterThan(0);
182+
done();
183+
});
184+
});
185+
161186
it('should expose empty parameter map on call with no parameters', function (done) {
162187
// Given
163188
var statement = "CREATE (n:Label {prop:'string'}) RETURN n";
@@ -305,7 +330,6 @@ describe('session', function () {
305330
done();
306331
})
307332
});
308-
309333
});
310334

311335

test/v1/tck/steps/resultapisteps.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ module.exports = function () {
5656
});
5757

5858
this.Then(/^requesting `Counters` from `Result Summary` should give$/, function (table) {
59-
var updateStatistics = this.summary.updateStatistics
59+
var counters = this.summary.counters;
6060
for ( var i = 0 ; i < table.hashes().length; i++) {
6161
var statistic = table.hashes()[i].counter;
6262
var expected = util.literalValueToTestValueNormalIntegers(table.hashes()[i].result);
63-
var given = getStatistic(statistic, updateStatistics)
63+
var given = getStatistic(statistic, counters)
6464
if (!util.compareValues(given, expected)) {
6565
throw Error("Statistics for: " + statistic + " does not match. Expected: '" + expected + "' Given: '" + given + "'");
6666
}
@@ -195,42 +195,42 @@ this.Then(/^the `Result Summary` `Notifications` has one notification with$/, fu
195195
throw Error("No statement type mapping of: " + type)
196196
}
197197

198-
function getStatistic(statementString, updateStatistics) {
198+
function getStatistic(statementString, counters) {
199199
if (statementString == 'nodes created') {
200-
return updateStatistics.nodesCreated();
200+
return counters.nodesCreated();
201201
}
202202
if (statementString == 'nodes deleted') {
203-
return updateStatistics.nodesDeleted();
203+
return counters.nodesDeleted();
204204
}
205205
if (statementString == 'relationships created') {
206-
return updateStatistics.relationshipsCreated();
206+
return counters.relationshipsCreated();
207207
}
208208
if (statementString == 'relationships deleted') {
209-
return updateStatistics.relationshipsDeleted();
209+
return counters.relationshipsDeleted();
210210
}
211211
if (statementString == 'properties set') {
212-
return updateStatistics.propertiesSet();
212+
return counters.propertiesSet();
213213
}
214214
if (statementString == 'labels added') {
215-
return updateStatistics.labelsAdded();
215+
return counters.labelsAdded();
216216
}
217217
if (statementString == 'labels removed') {
218-
return updateStatistics.labelsRemoved();
218+
return counters.labelsRemoved();
219219
}
220220
if (statementString == 'indexes added') {
221-
return updateStatistics.indexesAdded();
221+
return counters.indexesAdded();
222222
}
223223
if (statementString == 'indexes removed') {
224-
return updateStatistics.indexesRemoved();
224+
return counters.indexesRemoved();
225225
}
226226
if (statementString == 'constraints added') {
227-
return updateStatistics.constraintsAdded();
227+
return counters.constraintsAdded();
228228
}
229229
if (statementString == 'constraints removed') {
230-
return updateStatistics.constraintsRemoved();
230+
return counters.constraintsRemoved();
231231
}
232232
if (statementString == 'contains updates') {
233-
return updateStatistics.containsUpdates();
233+
return counters.containsUpdates();
234234
}
235235
throw Error("No statistics mapping of: " + statementString)
236236
}

0 commit comments

Comments
 (0)