From a8385e873e272c4d74e836290046fd71c1b1cc2a Mon Sep 17 00:00:00 2001 From: Muqiu Han Date: Thu, 14 Sep 2023 15:56:22 +0800 Subject: [PATCH] Fix: utils.KeyUtils.routingKeyCompare --- .gitignore | 6 ++- build.sbt | 2 +- src/main/scala/network/io/Server.scala | 2 + src/main/scala/utils/KeyUtils.scala | 54 +++++++++++++------------- 4 files changed, 33 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index dd70dd9..ac7a321 100644 --- a/.gitignore +++ b/.gitignore @@ -37,6 +37,8 @@ build/ ### Mac OS ### .DS_Store -.idea/ -.bsp/ +/.idea +/.bsp /project +/.bloop +/.metals \ No newline at end of file diff --git a/build.sbt b/build.sbt index 6348e23..afaf71c 100644 --- a/build.sbt +++ b/build.sbt @@ -12,6 +12,6 @@ libraryDependencies += "io.netty" % "netty-all" % "4.1.50.Final" libraryDependencies += "org.apache.commons" % "commons-lang3" % "3.4" libraryDependencies += "org.slf4j" % "slf4j-log4j12" % "1.7.26" libraryDependencies += "com.alibaba" % "fastjson" % "1.2.52" -libraryDependencies += "junit" % "junit" % "4.13.2" +libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.17" % "test" libraryDependencies += "org.java-websocket" % "Java-WebSocket" % "1.3.8" libraryDependencies += "org.projectlombok" % "lombok" % "1.18.20" diff --git a/src/main/scala/network/io/Server.scala b/src/main/scala/network/io/Server.scala index 917d134..41893da 100644 --- a/src/main/scala/network/io/Server.scala +++ b/src/main/scala/network/io/Server.scala @@ -17,7 +17,9 @@ class Server(): .channel(classOf[NioServerSocketChannel]) .childHandler(new ServerInitializer()) +/* override def start(): Unit = // TODO: Load banner and initialize configuration // val channel = server.channel() Unit + */ diff --git a/src/main/scala/utils/KeyUtils.scala b/src/main/scala/utils/KeyUtils.scala index bcc9230..37ab305 100644 --- a/src/main/scala/utils/KeyUtils.scala +++ b/src/main/scala/utils/KeyUtils.scala @@ -3,32 +3,30 @@ package utils import scala.util.boundary, boundary.break object KeyUtils: - def routingKeyCompare(routingKey: String, bindingKey: String): Boolean = - val keys = bindingKey.split("\\|") - val part1 = routingKey.split("\\.") - - boundary: - for (key <- keys) do - val part2 = key.split("\\.") - val len2 = part2.length - val len1 = part1.length - if key.contains("#") then - var i = 0 - if part2(0) == "#" then - for (i <- 1 until Math.min(len1, len2)) do - if part2(len2 - i) != part1(len1 - i) then break(false) - if part2(len2 - i) == "#" then break(false) - else if (part2(len2 - 1) == "#") then - for (i <- 0 until Math.min(len1, len2)) do - if part2(i) != part1(i) then break(false) - if part2(i) == "#" then break(true) - else - var flag = true - if len1 == len2 then - for (i <- 0 until len1) do - if !(part2(i) == "*" || part1(i) == part2(i)) then - flag = false - break(false) - if flag then break(true) - false + def routingKeyCompare(routingKey: String, bindingKey: String): Boolean = + val keys: Array[String] = bindingKey.split("\\|") + val part1: Array[String] = routingKey.split("\\.") + keys.exists(key => + val part2: Array[String] = key.split("\\.") + val len2: Int = part2.length + val len1: Int = part1.length + if key.contains("#") then + var i: Int = -1 + if part2.head == "#" then + i = 1 + while i <= math.min(len1, len2) && part2(len2 - i) == part1(len1 - i) + do i += 1 + if part2(len2 - i) == "#" then return true + else if part2.last == "#" then + i = 0 + while i < math.min(len1, len2) && part2(i) == part1(i) do i += 1 + if part2(i) == "#" then return true + else + var flag: Boolean = true + if len1 == len2 then + for i <- 0 until len1 if !(part2(i) == "*" || part1(i) == part2(i)) do + flag = false + if flag then return true + false + )