Skip to content

Commit

Permalink
Make note if fuzzy search is used
Browse files Browse the repository at this point in the history
Signed-off-by: shedaniel <daniel@shedaniel.me>
  • Loading branch information
shedaniel committed Aug 26, 2021
1 parent ff42961 commit a511fec
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 39 deletions.
71 changes: 39 additions & 32 deletions src/main/kotlin/me/shedaniel/linkie/discord/MappingsQueryUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ import me.shedaniel.linkie.utils.QueryContext
import me.shedaniel.linkie.utils.ResultHolder

object MappingsQueryUtils {
suspend fun query(mappings: MappingsContainer, searchTerm: String, vararg types: MappingsEntryType): MutableList<ResultHolder<*>> {
data class Result(
val results: MutableList<ResultHolder<*>>,
val fuzzy: Boolean,
)

suspend fun query(mappings: MappingsContainer, searchTerm: String, vararg types: MappingsEntryType): Result {
require(types.isNotEmpty())
val context = QueryContext(
provider = { mappings },
Expand Down Expand Up @@ -70,50 +75,52 @@ object MappingsQueryUtils {
fields?.also(result::addAll)
result.sortByDescending { it.score }

if (result.isEmpty()) {
runBlocking {
if (MappingsEntryType.CLASS in types) {
launch {
try {
classes = MappingsQuery.queryClasses(context.copy(accuracy = MatchAccuracy.Fuzzy)).value
} catch (e: NullPointerException) {
if (result.isNotEmpty()) {
return Result(result, false)
}

runBlocking {
if (MappingsEntryType.CLASS in types) {
launch {
try {
classes = MappingsQuery.queryClasses(context.copy(accuracy = MatchAccuracy.Fuzzy)).value
} catch (e: NullPointerException) {

}
}
}
if (MappingsEntryType.METHOD in types) {
launch {
try {
methods = MappingsQuery.queryMethods(context.copy(accuracy = MatchAccuracy.Fuzzy)).value
} catch (e: NullPointerException) {
}
if (MappingsEntryType.METHOD in types) {
launch {
try {
methods = MappingsQuery.queryMethods(context.copy(accuracy = MatchAccuracy.Fuzzy)).value
} catch (e: NullPointerException) {

}
}
}
if (MappingsEntryType.FIELD in types) {
launch {
try {
fields = MappingsQuery.queryFields(context.copy(accuracy = MatchAccuracy.Fuzzy)).value
} catch (e: NullPointerException) {
}
if (MappingsEntryType.FIELD in types) {
launch {
try {
fields = MappingsQuery.queryFields(context.copy(accuracy = MatchAccuracy.Fuzzy)).value
} catch (e: NullPointerException) {

}
}
}
}
classes?.also(result::addAll)
methods?.also(result::addAll)
fields?.also(result::addAll)
result.sortByDescending { it.score }
}
classes?.also(result::addAll)
methods?.also(result::addAll)
fields?.also(result::addAll)
result.sortByDescending { it.score }

if (result.isEmpty()) {
if (types.size != 1) {
MappingsQuery.errorNoResultsFound(null, searchTerm)
} else {
MappingsQuery.errorNoResultsFound(types.first(), searchTerm)
}
if (result.isEmpty()) {
if (types.size != 1) {
MappingsQuery.errorNoResultsFound(null, searchTerm)
} else {
MappingsQuery.errorNoResultsFound(types.first(), searchTerm)
}
}

return result
return Result(result, true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import me.shedaniel.linkie.utils.ResultHolder
import me.shedaniel.linkie.utils.onlyClass
import me.shedaniel.linkie.utils.valueKeeper
import java.util.*
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger
import kotlin.math.ceil

Expand Down Expand Up @@ -99,12 +100,13 @@ open class QueryMappingsCommand(
}

suspend fun execute(ctx: CommandContext, namespace: Namespace, version: String, searchTerm: String, types: Array<out MappingsEntryType>) = ctx.use {
val fuzzy = AtomicBoolean(false)
val maxPage = AtomicInteger(-1)
val query by valueKeeper {
QueryMappingsExtensions.query(searchTerm, namespace.getProvider(version), user, message, maxPage, types)
QueryMappingsExtensions.query(searchTerm, namespace.getProvider(version), user, message, maxPage, fuzzy, types)
}.initiate()
message.sendPages(ctx, 0, maxPage.get()) { page ->
QueryMessageBuilder.buildMessage(this, namespace, query.value, query.mappings, page, user, maxPage.get())
QueryMessageBuilder.buildMessage(this, searchTerm, namespace, query.value, query.mappings, page, user, maxPage.get(), fuzzy.get())
}
}

Expand Down Expand Up @@ -170,6 +172,7 @@ object QueryMappingsExtensions {
user: User,
message: MessageCreator,
maxPage: AtomicInteger,
fuzzy: AtomicBoolean,
types: Array<out MappingsEntryType>,
): QueryResult<MappingsContainer, MutableList<ResultHolder<*>>> {
val hasWildcard: Boolean = searchTerm.substringBeforeLast('/').onlyClass() == "*" || searchTerm.onlyClass() == "*"
Expand All @@ -182,8 +185,10 @@ object QueryMappingsExtensions {
}
return message.getCatching(user) {
val mappings = provider.get()
QueryResult(mappings, query(mappings, searchTerm, *types).also {
maxPage.set(ceil(it.size / 4.0).toInt())
QueryResult(mappings, query(mappings, searchTerm, *types).let {
maxPage.set(ceil(it.results.size / 4.0).toInt())
fuzzy.set(it.fuzzy)
it.results
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import me.shedaniel.linkie.utils.QueryResult
import me.shedaniel.linkie.utils.ResultHolder
import me.shedaniel.linkie.utils.dropAndTake
import me.shedaniel.linkie.utils.valueKeeper
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger

class QueryTranslateMappingsCommand(
Expand Down Expand Up @@ -132,15 +133,20 @@ class QueryTranslateMappingsCommand(
}

suspend fun execute(ctx: CommandContext, src: Namespace, dst: Namespace, version: String, searchTerm: String, types: Array<out MappingsEntryType>) = ctx.use {
val fuzzy = AtomicBoolean(false)
val maxPage = AtomicInteger(-1)
val result by valueKeeper {
translate(QueryMappingsExtensions.query(searchTerm, src.getProvider(version), user, message, maxPage, types), dst.getProvider(version))
translate(QueryMappingsExtensions.query(searchTerm, src.getProvider(version), user, message, maxPage, fuzzy, types), dst.getProvider(version))
}.initiate()
message.sendPages(ctx, 0, maxPage.get()) { page ->
basicEmbed(user)
if (maxPage.get() > 1) title("List of ${result.source.name}->${result.target.name} Mappings (Page ${page + 1}/${maxPage.get()})")
else title("List of ${result.source.name}->${result.target.name} Mappings")
buildSafeDescription {
if (fuzzy.get()) {
append("**No results found for __${searchTerm}__. Displaying related results.**").appendLine().appendLine()
}

var isFirst = true
result.value.dropAndTake(4 * page, 4).forEach { (original, translated) ->
if (!isFirst) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import me.shedaniel.linkie.discord.OptionlessCommand
import me.shedaniel.linkie.discord.config.ConfigManager
import me.shedaniel.linkie.discord.config.GuildConfig
import me.shedaniel.linkie.discord.utils.CommandContext
import me.shedaniel.linkie.discord.utils.QueryMessageBuilder.buildMessage
import me.shedaniel.linkie.discord.utils.addInlineField
import me.shedaniel.linkie.discord.utils.basicEmbed
import me.shedaniel.linkie.discord.utils.description
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ import me.shedaniel.linkie.utils.dropAndTake
import me.shedaniel.linkie.utils.localiseFieldDesc

object QueryMessageBuilder {
fun buildMessage(spec: EmbedCreateSpec.Builder, namespace: Namespace, results: List<ResultHolder<*>>, mappings: MappingsMetadata, page: Int, author: User, maxPage: Int) {
fun buildMessage(spec: EmbedCreateSpec.Builder, searchTerm: String, namespace: Namespace, results: List<ResultHolder<*>>, mappings: MappingsMetadata, page: Int, author: User, maxPage: Int, fuzzy: Boolean) {
buildHeader(spec, mappings, page, author, maxPage)
spec.buildSafeDescription {
if (fuzzy) {
append("**No results found for __${searchTerm}__. Displaying related results.**").appendLine().appendLine()
}

var isFirst = true
results.dropAndTake(4 * page, 4).forEach { (value, _) ->
if (isFirst) {
Expand Down

0 comments on commit a511fec

Please sign in to comment.