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'
|
||||
customCommands = require './custom-commands'
|
||||
crypto = require 'crypto'
|
||||
parameters = require './parameters'
|
||||
|
||||
OBJECT_REGEX = /\.$/
|
||||
INSTANCE_REGEX = /\.[\d]+\.$/
|
||||
@@ -74,7 +75,10 @@ updateDevice = (currentRequest, actions, callback) ->
|
||||
updates["#{i[0]}._name"] = i[1]
|
||||
|
||||
if actions.parameterNames?
|
||||
newAliases = [] # possible new aliased parameters
|
||||
for p in actions.parameterNames
|
||||
newAliases.push(p[0]) if parameters.getAlias(p[0])?
|
||||
|
||||
if OBJECT_REGEX.test(p[0])
|
||||
path = 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
|
||||
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)
|
||||
)
|
||||
else
|
||||
|
||||
+19
-17
@@ -19,16 +19,17 @@ getLsb = (num) ->
|
||||
return i
|
||||
|
||||
|
||||
# compile types into data types optimized for quick lookup
|
||||
types = []
|
||||
createParameterAttributeFinder = (attributeName) ->
|
||||
# compile attributes into data types optimized for quick lookup
|
||||
attributes = []
|
||||
stringSegments = {}
|
||||
regexSegments = {}
|
||||
paramRegex = {}
|
||||
|
||||
for k, v of config.PARAMETERS
|
||||
continue if not v.type?
|
||||
typeIndex = types.push(v.type) - 1
|
||||
throw new Error("There cannot be more than #{MAX_PARAMETERS} configured parameters") if typeIndex >= MAX_PARAMETERS
|
||||
continue if not v[attributeName]?
|
||||
attributeIndex = attributes.push(v[attributeName]) - 1
|
||||
throw new Error("There cannot be more than #{MAX_PARAMETERS} configured parameters") if attributeIndex >= MAX_PARAMETERS
|
||||
segmentIndex = 0
|
||||
parts = k.match(PATH_REGEX)
|
||||
for i in [0 ... parts.length]
|
||||
@@ -39,16 +40,16 @@ for k, v of config.PARAMETERS
|
||||
j = part.lastIndexOf('/')
|
||||
regExp = new RegExp(part[1...j], part[j+1..])
|
||||
regexSegments[positionHash] ?= 0
|
||||
regexSegments[positionHash] |= Math.pow(2, typeIndex)
|
||||
paramRegex[typeIndex] ?= {}
|
||||
paramRegex[typeIndex][i] = regExp
|
||||
regexSegments[positionHash] |= Math.pow(2, attributeIndex)
|
||||
paramRegex[attributeIndex] ?= {}
|
||||
paramRegex[attributeIndex][i] = regExp
|
||||
else
|
||||
stringSegments[positionHash] ?= {}
|
||||
stringSegments[positionHash][part] ?= 0
|
||||
stringSegments[positionHash][part] |= Math.pow(2, typeIndex)
|
||||
stringSegments[positionHash][part] |= Math.pow(2, attributeIndex)
|
||||
|
||||
|
||||
getType = (param) ->
|
||||
# Finder function
|
||||
return (param) ->
|
||||
parts = param.split('.')
|
||||
positionHash = parts.length * MAX_PARAMETERS
|
||||
|
||||
@@ -63,21 +64,21 @@ getType = (param) ->
|
||||
allIndices &= (strIdx | rgxIdx)
|
||||
|
||||
if stringIndices > 0
|
||||
return types[getLsb(stringIndices)]
|
||||
return attributes[getLsb(stringIndices)]
|
||||
|
||||
typeIndex = 0
|
||||
attributeIndex = 0
|
||||
while allIndices > 0
|
||||
if allIndices & 1
|
||||
match = true
|
||||
for i, r of paramRegex[typeIndex]
|
||||
for i, r of paramRegex[attributeIndex]
|
||||
if not r.test(parts[i])
|
||||
match = false
|
||||
break
|
||||
|
||||
if match
|
||||
return types[typeIndex]
|
||||
return attributes[attributeIndex]
|
||||
|
||||
++ typeIndex
|
||||
++ attributeIndex
|
||||
allIndices >>= 1
|
||||
|
||||
return null
|
||||
@@ -175,5 +176,6 @@ compileAliases = (callback) ->
|
||||
)
|
||||
|
||||
|
||||
exports.getType = getType
|
||||
exports.getType = createParameterAttributeFinder('type')
|
||||
exports.getAlias = createParameterAttributeFinder('alias')
|
||||
exports.compileAliases = compileAliases
|
||||
Reference in new issue
Block a user