Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate LruMap from collections to lru-cache. #1396

Merged
merged 1 commit into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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