Skip to content

Commit

Permalink
fix: migrate LruMap from collections to lru-cache. (mqttjs#1396)
Browse files Browse the repository at this point in the history
  • Loading branch information
redboltz authored Jan 6, 2022
1 parent b4925d0 commit 5c67037
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
16 changes: 7 additions & 9 deletions lib/topic-alias-send.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Module dependencies
*/
const LruMap = require('collections/lru-map')
const LruMap = require('lru-cache')
const NumberAllocator = require('number-allocator').NumberAllocator

/**
Expand All @@ -17,7 +17,7 @@ function TopicAliasSend (max) {
}

if (max > 0) {
this.aliasToTopic = new LruMap()
this.aliasToTopic = new LruMap({ max: max })
this.topicToAlias = {}
this.numberAllocator = new NumberAllocator(1, max)
this.max = max
Expand All @@ -37,9 +37,9 @@ TopicAliasSend.prototype.put = function (topic, alias) {
}
const entry = this.aliasToTopic.get(alias)
if (entry) {
delete this.topicToAlias[entry.topic]
delete this.topicToAlias[entry]
}
this.aliasToTopic.set(alias, { topic: topic, alias: alias })
this.aliasToTopic.set(alias, topic)
this.topicToAlias[topic] = alias
this.numberAllocator.use(alias)
this.length = this.aliasToTopic.length
Expand All @@ -52,9 +52,7 @@ TopicAliasSend.prototype.put = function (topic, alias) {
* @returns {String} - if mapped topic exists return topic, otherwise return undefined
*/
TopicAliasSend.prototype.getTopicByAlias = function (alias) {
const entry = this.aliasToTopic.get(alias)
if (typeof entry === 'undefined') return entry
return entry.topic
return this.aliasToTopic.get(alias)
}

/**
Expand All @@ -74,7 +72,7 @@ TopicAliasSend.prototype.getAliasByTopic = function (topic) {
* Clear all entries
*/
TopicAliasSend.prototype.clear = function () {
this.aliasToTopic.clear()
this.aliasToTopic.reset()
this.topicToAlias = {}
this.numberAllocator.clear()
this.length = 0
Expand All @@ -87,7 +85,7 @@ TopicAliasSend.prototype.clear = function () {
TopicAliasSend.prototype.getLruAlias = function () {
const alias = this.numberAllocator.firstVacant()
if (alias) return alias
return this.aliasToTopic.min().alias
return this.aliasToTopic.keys()[this.aliasToTopic.length - 1]
}

module.exports = TopicAliasSend
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@
"net": false
},
"dependencies": {
"collections": "^5.1.13",
"commist": "^1.0.0",
"concat-stream": "^2.0.0",
"debug": "^4.1.1",
"duplexify": "^4.1.1",
"help-me": "^3.0.0",
"inherits": "^2.0.3",
"lru-cache": "^6.0.0",
"minimist": "^1.2.5",
"mqtt-packet": "^6.8.0",
"number-allocator": "^1.0.9",
Expand Down

0 comments on commit 5c67037

Please sign in to comment.