Clear cached aliases as soon as we detect a new previously unseen aliased parameter. Mostly useful for dynamic regex-based aliases.
This commit is contained in:
1 parent
8dede7743f
commit
edf8f757ed
2 files changed
+33
-17
No files matched your search
+14
@@ -13,6 +13,7 @@ fs = require 'fs'
|
|||||||
apiFunctions = require './api-functions'
|
apiFunctions = require './api-functions'
|
||||||
customCommands = require './custom-commands'
|
customCommands = require './custom-commands'
|
||||||
crypto = require 'crypto'
|
crypto = require 'crypto'
|
||||||
|
parameters = require './parameters'
|
||||||
|
|
||||||
OBJECT_REGEX = /\.$/
|
OBJECT_REGEX = /\.$/
|
||||||
INSTANCE_REGEX = /\.[\d]+\.$/
|
INSTANCE_REGEX = /\.[\d]+\.$/
|
||||||
@@ -74,7 +75,10 @@ updateDevice = (currentRequest, actions, callback) ->
|
|||||||
updates["#{i[0]}._name"] = i[1]
|
updates["#{i[0]}._name"] = i[1]
|
||||||
|
|
||||||
if actions.parameterNames?
|
if actions.parameterNames?
|
||||||
|
newAliases = [] # possible new aliased parameters
|
||||||
for p in actions.parameterNames
|
for p in actions.parameterNames
|
||||||
|
newAliases.push(p[0]) if parameters.getAlias(p[0])?
|
||||||
|
|
||||||
if OBJECT_REGEX.test(p[0])
|
if OBJECT_REGEX.test(p[0])
|
||||||
path = p[0]
|
path = p[0]
|
||||||
if INSTANCE_REGEX.test(p[0])
|
if INSTANCE_REGEX.test(p[0])
|
||||||
@@ -97,6 +101,16 @@ updateDevice = (currentRequest, actions, callback) ->
|
|||||||
|
|
||||||
if Object.keys(updates).length > 0 or Object.keys(deletes).length > 0
|
if Object.keys(updates).length > 0 or Object.keys(deletes).length > 0
|
||||||
db.devicesCollection.update({'_id' : currentRequest.deviceId}, {'$set' : updates, '$unset' : deletes}, {}, (err, count) ->
|
db.devicesCollection.update({'_id' : currentRequest.deviceId}, {'$set' : updates, '$unset' : deletes}, {}, (err, count) ->
|
||||||
|
# Clear aliases cache if there's a new aliased parameter that has not been included in aliases cache
|
||||||
|
if newAliases?.length
|
||||||
|
db.getAliases((aliases) ->
|
||||||
|
for a in newAliases
|
||||||
|
if not aliases[a]?
|
||||||
|
db.redisClient.del('aliases', (err, res) ->
|
||||||
|
throw err if err
|
||||||
|
)
|
||||||
|
break
|
||||||
|
)
|
||||||
callback?(err)
|
callback?(err)
|
||||||
)
|
)
|
||||||
else
|
else
|
||||||
|
|||||||
+19
-17
@@ -19,16 +19,17 @@ getLsb = (num) ->
|
|||||||
return i
|
return i
|
||||||
|
|
||||||
|
|
||||||
# compile types into data types optimized for quick lookup
|
createParameterAttributeFinder = (attributeName) ->
|
||||||
types = []
|
# compile attributes into data types optimized for quick lookup
|
||||||
|
attributes = []
|
||||||
stringSegments = {}
|
stringSegments = {}
|
||||||
regexSegments = {}
|
regexSegments = {}
|
||||||
paramRegex = {}
|
paramRegex = {}
|
||||||
|
|
||||||
for k, v of config.PARAMETERS
|
for k, v of config.PARAMETERS
|
||||||
continue if not v.type?
|
continue if not v[attributeName]?
|
||||||
typeIndex = types.push(v.type) - 1
|
attributeIndex = attributes.push(v[attributeName]) - 1
|
||||||
throw new Error("There cannot be more than #{MAX_PARAMETERS} configured parameters") if typeIndex >= MAX_PARAMETERS
|
throw new Error("There cannot be more than #{MAX_PARAMETERS} configured parameters") if attributeIndex >= MAX_PARAMETERS
|
||||||
segmentIndex = 0
|
segmentIndex = 0
|
||||||
parts = k.match(PATH_REGEX)
|
parts = k.match(PATH_REGEX)
|
||||||
for i in [0 ... parts.length]
|
for i in [0 ... parts.length]
|
||||||
@@ -39,16 +40,16 @@ for k, v of config.PARAMETERS
|
|||||||
j = part.lastIndexOf('/')
|
j = part.lastIndexOf('/')
|
||||||
regExp = new RegExp(part[1...j], part[j+1..])
|
regExp = new RegExp(part[1...j], part[j+1..])
|
||||||
regexSegments[positionHash] ?= 0
|
regexSegments[positionHash] ?= 0
|
||||||
regexSegments[positionHash] |= Math.pow(2, typeIndex)
|
regexSegments[positionHash] |= Math.pow(2, attributeIndex)
|
||||||
paramRegex[typeIndex] ?= {}
|
paramRegex[attributeIndex] ?= {}
|
||||||
paramRegex[typeIndex][i] = regExp
|
paramRegex[attributeIndex][i] = regExp
|
||||||
else
|
else
|
||||||
stringSegments[positionHash] ?= {}
|
stringSegments[positionHash] ?= {}
|
||||||
stringSegments[positionHash][part] ?= 0
|
stringSegments[positionHash][part] ?= 0
|
||||||
stringSegments[positionHash][part] |= Math.pow(2, typeIndex)
|
stringSegments[positionHash][part] |= Math.pow(2, attributeIndex)
|
||||||
|
|
||||||
|
# Finder function
|
||||||
getType = (param) ->
|
return (param) ->
|
||||||
parts = param.split('.')
|
parts = param.split('.')
|
||||||
positionHash = parts.length * MAX_PARAMETERS
|
positionHash = parts.length * MAX_PARAMETERS
|
||||||
|
|
||||||
@@ -63,21 +64,21 @@ getType = (param) ->
|
|||||||
allIndices &= (strIdx | rgxIdx)
|
allIndices &= (strIdx | rgxIdx)
|
||||||
|
|
||||||
if stringIndices > 0
|
if stringIndices > 0
|
||||||
return types[getLsb(stringIndices)]
|
return attributes[getLsb(stringIndices)]
|
||||||
|
|
||||||
typeIndex = 0
|
attributeIndex = 0
|
||||||
while allIndices > 0
|
while allIndices > 0
|
||||||
if allIndices & 1
|
if allIndices & 1
|
||||||
match = true
|
match = true
|
||||||
for i, r of paramRegex[typeIndex]
|
for i, r of paramRegex[attributeIndex]
|
||||||
if not r.test(parts[i])
|
if not r.test(parts[i])
|
||||||
match = false
|
match = false
|
||||||
break
|
break
|
||||||
|
|
||||||
if match
|
if match
|
||||||
return types[typeIndex]
|
return attributes[attributeIndex]
|
||||||
|
|
||||||
++ typeIndex
|
++ attributeIndex
|
||||||
allIndices >>= 1
|
allIndices >>= 1
|
||||||
|
|
||||||
return null
|
return null
|
||||||
@@ -175,5 +176,6 @@ compileAliases = (callback) ->
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
exports.getType = getType
|
exports.getType = createParameterAttributeFinder('type')
|
||||||
|
exports.getAlias = createParameterAttributeFinder('alias')
|
||||||
exports.compileAliases = compileAliases
|
exports.compileAliases = compileAliases
|
||||||
Reference in new issue
Block a user