Skip to content

Commit

Permalink
aha. made a nice performance adjustment. and also realized that Reply…
Browse files Browse the repository at this point in the history
…Transformers are static. and i need them bound to individual instances. filed redis/ioredis#895
  • Loading branch information
Jonahss committed Jun 13, 2019
1 parent fa2a6f5 commit 739aa62
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
46 changes: 33 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ class RedisGraph extends Redis {

// we use a special stored procedure to get these names, since the server responds with just index numbers for performance reasons
this.propertyNames = []
this.labelNames = []
this.labelNames = Promise.resolve([])
this.relationshipTypes = []

this._loadingLabelNames = false

this.resolvePropertyName = resolvePropertyName.bind(this)
this.resolveLabelName = resolveLabelName.bind(this)
this.resolveRelationshipType = resolveRelationshipType.bind(this)
this.parseNode = parseNode.bind(this)
this.parseRelation = parseRelation.bind(this)
Expand Down Expand Up @@ -65,6 +66,36 @@ class RedisGraph extends Redis {
async explain (command) {
return this.call('GRAPH.EXPLAIN', this.graphName, `${command}`)
}

async resolveLabelName (labelId) {
let labelName = (await this.labelNames)[labelId]
if (labelName) {
return labelName
}
// otherwise, we need to hydrate the label names from the server
// let's have any other callers wait until we're finished
await this.loadLabelNames()

return (await this.labelNames)[labelId]
}

async loadLabelNames () {
// if we're already loading, return the promise for the results
if (this._loadingLabelNames) {
return this._loadingLabelNames
}
// let's only do this once, have others wait
this._loadingLabelNames = new Promise(async (resolve) => {
console.log('loading label names')
let labelNames = await this.query(`CALL db.labels()`)
this.labelNames = labelNames.map((prop) => prop.label)
this._loadingLabelNames = false
console.log('label names loaded', labelNames, this.graphName)
resolve()
})

return this._loadingLabelNames
}
}

function parseMetaInformation (array) {
Expand Down Expand Up @@ -169,17 +200,6 @@ async function resolvePropertyName (propertyNameId) {
return this.propertyNames[propertyNameId]
}

async function resolveLabelName (labelId) {
if (!this.labelNames[labelId]) {
console.log('graphname?', this.graphName)
let labelNames = await this.query(`CALL db.labels()`)
this.labelNames = labelNames.map((prop) => prop.label)
console.log('label names', labelNames)
}

return this.labelNames[labelId]
}

async function resolveRelationshipType (relationshipTypeId) {
if (!this.relationshipTypes[relationshipTypeId]) {
let relationshipTypes = await this.query(`CALL db.relationshipTypes()`)
Expand Down
11 changes: 9 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ test('options constructor', async (t) => {
t.truthy(await graph.query(`CREATE (:person {name: 'Chuckwudi'})`) instanceof Array)
})

test.only('multiple instances stay separate', async (t) => {
let a = new RedisGraph('a')
let b = new RedisGraph('b')
t.truthy(a.graphName == 'a')
t.truthy(b.graphName == 'b')
})

test('response node parsing', async (t) => {
let graph = new RedisGraph('nodeResponse')
await graph.query(`CREATE (:person {name: 'Chuck'}), (:person {name: 'Austin'}), (:person {name: 'Zack'})`)
Expand All @@ -35,7 +42,7 @@ test('response node parsing', async (t) => {
t.truthy(result[1]['id(a)'])
})

test.skip('response relation parsing', async (t) => {
test('response relation parsing', async (t) => {
let graph = new RedisGraph('relationResponse')
await graph.query(`CREATE (:person {name: 'Chuck'})-[:friendsWith]->(:person {name: 'Austin'})`)
let result = await graph.query(`MATCH (:person)-[r:friendsWith]->(:person) RETURN r, id(r)`)
Expand All @@ -46,7 +53,7 @@ test.skip('response relation parsing', async (t) => {
t.truthy(!isNaN(result[0]['id(r)']))
})

test('delete graph', async (t) => {
test.skip('delete graph', async (t) => {
let graph = new RedisGraph('delete')
await graph.query(`CREATE (:person {name: 'Chuckwudi'})`)
t.truthy(await graph.delete())
Expand Down

0 comments on commit 739aa62

Please sign in to comment.