Skip to content

Commit 93d8401

Browse files
authored
Merge pull request #37 from CS3219-AY2425S1/add-generate-combi-util
Add queue name generation functions
2 parents 2fef691 + 2f42211 commit 93d8401

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

Backend/MatchingService/rabbitmq/setup.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
const amqp = require("amqplib");
2+
const generator = require("../utils/generateQueues");
23

34
const matching_exchange_name = "matching_exchange";
45
const dead_letter_exchange_name = "dead_letter_exchange";
56
const dead_letter_queue_name = "dead_letter_queue";
67
const cancel_queue_name = "cancel_queue";
7-
const queueNames = [
8-
'easy.python',
9-
'easy.java',
10-
'easy.cplusplus',
11-
'medium.python',
12-
'medium.java',
13-
'medium.cplusplus',
14-
'hard.python',
15-
'hard.java',
16-
'hard.cplusplus',
17-
];
8+
const difficulties = ["easy", "medium", "hard"];
9+
const languages = ["python", "java", "cplusplus"];
10+
11+
const queueNames = generator.generateQueueNames(
12+
generator.generateCombinations(difficulties, languages)
13+
);
1814

1915
async function setupRabbitMQ() {
2016
try {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Used to generate all possible combinations of criteria for matching
2+
/*
3+
Order matters:
4+
Example:
5+
- First criteria is the difficulty
6+
- Second criteria is the language
7+
- Combinations are [easy, python], ... , [hard, cplusplus]
8+
- Queue names are easy.python, ... , hard.cplusplus
9+
If you switch the order of the criteria, the combinations will be reversed!
10+
Example:
11+
- Combinations become [python, easy], ... , [cplusplus, hard]
12+
- Queue names become python.easy, ... , cplusplus.hard
13+
14+
Functions can be used with other criteria like difficulty, topic, etc.
15+
These functions can be chained multiple times to generate routing criteria longer than 2
16+
*/
17+
18+
function generateCombinations(criteria1, criteria2) {
19+
const combinations = [];
20+
for (let i = 0; i < criteria1.length; i++) {
21+
for (let j = 0; j < criteria2.length; j++) {
22+
combinations.push([criteria1[i], criteria2[j]]);
23+
}
24+
}
25+
return combinations;
26+
}
27+
28+
// output string is based on order of fields in the combinations array
29+
function generateQueueNames(combinations) {
30+
const queueNames = [];
31+
for (const combination of combinations) {
32+
const queueName = `${combination[0]}.${combination[1]}`;
33+
queueNames.push(queueName);
34+
}
35+
return queueNames;
36+
}
37+
38+
module.exports = { generateCombinations, generateQueueNames };

0 commit comments

Comments
 (0)