Skip to content

Commit e764306

Browse files
authored
Merge pull request #496 from zhenlineo/4.0-system-updates
Added missing system updates
2 parents 480fc77 + 73e3d43 commit e764306

File tree

7 files changed

+186
-140
lines changed

7 files changed

+186
-140
lines changed

src/result-summary.js

Lines changed: 43 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,24 @@ class ProfiledPlan {
174174
this.operatorType = profile.operatorType
175175
this.identifiers = profile.identifiers
176176
this.arguments = profile.args
177-
this.dbHits = intValue(profile.args.DbHits)
178-
this.rows = intValue(profile.args.Rows)
177+
this.dbHits = valueOrDefault('dbHits', profile)
178+
this.rows = valueOrDefault('rows', profile)
179+
this.pageCacheMisses = valueOrDefault('pageCacheMisses', profile)
180+
this.pageCacheHits = valueOrDefault('pageCacheHits', profile)
181+
this.pageCacheHitRatio = valueOrDefault('pageCacheHitRatio', profile)
182+
this.time = valueOrDefault('time', profile)
179183
this.children = profile.children
180184
? profile.children.map(child => new ProfiledPlan(child))
181185
: []
182186
}
187+
188+
hasPageCacheStats () {
189+
return (
190+
this.pageCacheMisses > 0 ||
191+
this.pageCacheHits > 0 ||
192+
this.pageCacheHitRatio > 0
193+
)
194+
}
183195
}
184196

185197
/**
@@ -206,12 +218,18 @@ class StatementStatistics {
206218
constraintsAdded: 0,
207219
constraintsRemoved: 0
208220
}
221+
this._systemUpdates = 0
209222
Object.keys(statistics).forEach(index => {
210223
// To camelCase
211-
this._stats[index.replace(/(-\w)/g, m => m[1].toUpperCase())] = intValue(
212-
statistics[index]
213-
)
224+
const camelCaseIndex = index.replace(/(-\w)/g, m => m[1].toUpperCase())
225+
if (camelCaseIndex in this._stats) {
226+
this._stats[camelCaseIndex] = intValue(statistics[index])
227+
} else if (camelCaseIndex === 'systemUpdates') {
228+
this._systemUpdates = intValue(statistics[index])
229+
}
214230
})
231+
232+
this._stats = Object.freeze(this._stats)
215233
}
216234

217235
/**
@@ -227,80 +245,26 @@ class StatementStatistics {
227245
}
228246

229247
/**
230-
* @return {Number} - Number of nodes created.
231-
*/
232-
nodesCreated () {
233-
return this._stats.nodesCreated
234-
}
235-
236-
/**
237-
* @return {Number} - Number of nodes deleted.
238-
*/
239-
nodesDeleted () {
240-
return this._stats.nodesDeleted
241-
}
242-
243-
/**
244-
* @return {Number} - Number of relationships created.
245-
*/
246-
relationshipsCreated () {
247-
return this._stats.relationshipsCreated
248-
}
249-
250-
/**
251-
* @return {Number} - Number of nodes deleted.
252-
*/
253-
relationshipsDeleted () {
254-
return this._stats.relationshipsDeleted
255-
}
256-
257-
/**
258-
* @return {Number} - Number of properties set.
259-
*/
260-
propertiesSet () {
261-
return this._stats.propertiesSet
262-
}
263-
264-
/**
265-
* @return {Number} - Number of labels added.
266-
*/
267-
labelsAdded () {
268-
return this._stats.labelsAdded
269-
}
270-
271-
/**
272-
* @return {Number} - Number of labels removed.
273-
*/
274-
labelsRemoved () {
275-
return this._stats.labelsRemoved
276-
}
277-
278-
/**
279-
* @return {Number} - Number of indexes added.
280-
*/
281-
indexesAdded () {
282-
return this._stats.indexesAdded
283-
}
284-
285-
/**
286-
* @return {Number} - Number of indexes removed.
248+
* Returns the statement statistics updates in a dictionary.
249+
* @returns {*}
287250
*/
288-
indexesRemoved () {
289-
return this._stats.indexesRemoved
251+
updates () {
252+
return this._stats
290253
}
291254

292255
/**
293-
* @return {Number} - Number of constraints added.
256+
* Return true if the system database get updated, otherwise false
257+
* @returns {boolean} - If the system database get updated or not.
294258
*/
295-
constraintsAdded () {
296-
return this._stats.constraintsAdded
259+
containsSystemUpdates () {
260+
return this._systemUpdates > 0
297261
}
298262

299263
/**
300-
* @return {Number} - Number of constraints removed.
264+
* @returns {number} - Number of system updates
301265
*/
302-
constraintsRemoved () {
303-
return this._stats.constraintsRemoved
266+
systemUpdates () {
267+
return this._systemUpdates
304268
}
305269
}
306270

@@ -356,6 +320,15 @@ function intValue (value) {
356320
return isInt(value) ? value.toInt() : value
357321
}
358322

323+
function valueOrDefault (key, values, defaultValue = 0) {
324+
if (key in values) {
325+
const value = values[key]
326+
return isInt(value) ? value.toInt() : value
327+
} else {
328+
return defaultValue
329+
}
330+
}
331+
359332
const statementType = {
360333
READ_ONLY: 'r',
361334
READ_WRITE: 'rw',

test/rx/summary.test.js

Lines changed: 104 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,43 @@ describe('#integration-rx summary', () => {
201201
shouldReturnNotification(serverVersion, txc))
202202
})
203203

204+
describe('system', () => {
205+
let driver
206+
/** @type {RxSession} */
207+
let session
208+
/** @type {ServerVersion} */
209+
let serverVersion
210+
211+
beforeEach(async () => {
212+
driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken)
213+
session = driver.rxSession({ database: 'system' })
214+
//
215+
216+
const normalSession = driver.session()
217+
try {
218+
const result = await normalSession.run('MATCH (n) DETACH DELETE n')
219+
serverVersion = ServerVersion.fromString(result.summary.server.version)
220+
} finally {
221+
await normalSession.close()
222+
}
223+
224+
await dropConstraintsAndIndices(driver)
225+
})
226+
227+
afterEach(async () => {
228+
if (session) {
229+
await session.close().toPromise()
230+
}
231+
await driver.close()
232+
})
233+
234+
it('session should return summary with correct system updates for create', () =>
235+
shouldReturnSummaryWithSystemUpdates(serverVersion, session))
236+
237+
it('transaction should return summary with correct system updates for create', () =>
238+
shouldReturnSummaryWithSystemUpdates(serverVersion, session, true))
239+
})
240+
204241
/**
205242
* @param {ServerVersion} version
206243
* @param {RxSession|RxTransaction} runnable
@@ -269,6 +306,37 @@ describe('#integration-rx summary', () => {
269306
await verifyStatementType(runnable, 'CREATE (n) RETURN n', 'rw')
270307
}
271308

309+
/**
310+
* @param {ServerVersion} version
311+
* @param {RxSession} session
312+
* @param {boolean} useTransaction
313+
*/
314+
async function shouldReturnSummaryWithSystemUpdates (
315+
version,
316+
session,
317+
useTransaction = false
318+
) {
319+
if (version.compareTo(VERSION_4_0_0) < 0) {
320+
return
321+
}
322+
323+
let runnable = session
324+
if (useTransaction) {
325+
runnable = await session.beginTransaction().toPromise()
326+
}
327+
328+
try {
329+
await verifySystemUpdates(
330+
runnable,
331+
"CREATE USER foo SET PASSWORD 'bar'",
332+
{},
333+
1
334+
)
335+
} finally {
336+
await verifySystemUpdates(runnable, 'DROP USER foo', {}, 1)
337+
}
338+
}
339+
272340
/**
273341
* @param {ServerVersion} version
274342
* @param {RxSession|RxTransaction} runnable
@@ -281,7 +349,7 @@ describe('#integration-rx summary', () => {
281349
return
282350
}
283351

284-
await verifyCounters(
352+
await verifyUpdates(
285353
runnable,
286354
'CREATE (n:Label1 {id: $id1})-[:KNOWS]->(m:Label2 {id: $id2}) RETURN n, m',
287355
{ id1: 10, id2: 20 },
@@ -316,7 +384,7 @@ describe('#integration-rx summary', () => {
316384
// first create the to-be-deleted nodes
317385
await shouldReturnSummaryWithUpdateStatisticsForCreate(version, runnable)
318386

319-
await verifyCounters(
387+
await verifyUpdates(
320388
runnable,
321389
'MATCH (n:Label1)-[r:KNOWS]->(m:Label2) DELETE n, r',
322390
null,
@@ -348,7 +416,7 @@ describe('#integration-rx summary', () => {
348416
return
349417
}
350418

351-
await verifyCounters(runnable, 'CREATE INDEX on :Label(prop)', null, {
419+
await verifyUpdates(runnable, 'CREATE INDEX on :Label(prop)', null, {
352420
nodesCreated: 0,
353421
nodesDeleted: 0,
354422
relationshipsCreated: 0,
@@ -384,7 +452,7 @@ describe('#integration-rx summary', () => {
384452
await session.close()
385453
}
386454

387-
await verifyCounters(runnable, 'DROP INDEX on :Label(prop)', null, {
455+
await verifyUpdates(runnable, 'DROP INDEX on :Label(prop)', null, {
388456
nodesCreated: 0,
389457
nodesDeleted: 0,
390458
relationshipsCreated: 0,
@@ -411,7 +479,7 @@ describe('#integration-rx summary', () => {
411479
return
412480
}
413481

414-
await verifyCounters(
482+
await verifyUpdates(
415483
runnable,
416484
'CREATE CONSTRAINT ON (book:Book) ASSERT book.isbn IS UNIQUE',
417485
null,
@@ -454,7 +522,7 @@ describe('#integration-rx summary', () => {
454522
await session.close()
455523
}
456524

457-
await verifyCounters(
525+
await verifyUpdates(
458526
runnable,
459527
'DROP CONSTRAINT ON (book:Book) ASSERT book.isbn IS UNIQUE',
460528
null,
@@ -628,27 +696,42 @@ describe('#integration-rx summary', () => {
628696
* @param {RxSession|RxTransaction} runnable
629697
* @param {string} statement
630698
* @param {*} parameters
631-
* @param {*} counters
699+
* @param {*} stats
700+
*/
701+
async function verifyUpdates (runnable, statement, parameters, stats) {
702+
const summary = await runnable
703+
.run(statement, parameters)
704+
.summary()
705+
.toPromise()
706+
expect(summary).toBeDefined()
707+
expect(summary.counters.containsUpdates()).toBeTruthy()
708+
expect(summary.counters.updates()).toEqual(stats)
709+
expect(summary.counters.containsSystemUpdates()).toBeFalsy()
710+
}
711+
712+
/**
713+
*
714+
* @param {RxSession|RxTransaction} runnable
715+
* @param {string} statement
716+
* @param {*} parameters
717+
* @param {number} systemUpdates
718+
* @returns {Promise<void>}
632719
*/
633-
async function verifyCounters (runnable, statement, parameters, counters) {
720+
async function verifySystemUpdates (
721+
runnable,
722+
statement,
723+
parameters,
724+
systemUpdates
725+
) {
634726
const summary = await runnable
635727
.run(statement, parameters)
636728
.summary()
637729
.toPromise()
638730
expect(summary).toBeDefined()
639-
expect({
640-
nodesCreated: summary.counters.nodesCreated(),
641-
nodesDeleted: summary.counters.nodesDeleted(),
642-
relationshipsCreated: summary.counters.relationshipsCreated(),
643-
relationshipsDeleted: summary.counters.relationshipsDeleted(),
644-
propertiesSet: summary.counters.propertiesSet(),
645-
labelsAdded: summary.counters.labelsAdded(),
646-
labelsRemoved: summary.counters.labelsRemoved(),
647-
indexesAdded: summary.counters.indexesAdded(),
648-
indexesRemoved: summary.counters.indexesRemoved(),
649-
constraintsAdded: summary.counters.constraintsAdded(),
650-
constraintsRemoved: summary.counters.constraintsRemoved()
651-
}).toEqual(counters)
731+
732+
expect(summary.counters.containsSystemUpdates()).toBeTruthy()
733+
expect(summary.counters.systemUpdates()).toBe(systemUpdates)
734+
expect(summary.counters.containsUpdates()).toBeFalsy()
652735
}
653736

654737
async function dropConstraintsAndIndices (driver) {

0 commit comments

Comments
 (0)