Skip to content

Commit

Permalink
Create unique atom getter suggestions (#6694)
Browse files Browse the repository at this point in the history
related #6611

Prevent creating atom getters with the same name.
  • Loading branch information
4e6 authored May 15, 2023
1 parent 06624f3 commit b7d51ed
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ final class SuggestionBuilder[A: IndexedSource](
}
val getters = members
.flatMap(_.arguments)
.distinctBy(_.name)
.distinctBy(_.name.name)
.map(buildGetter(module, tpName.name, _))

val tpSuggestions = tpe +: conses ++: getters
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package org.enso.searcher.sql

import java.util.UUID
import org.enso.polyglot.runtime.Runtime.Api._
import org.enso.polyglot.{ExportedSymbol, Suggestion}
import org.enso.polyglot.runtime.Runtime.Api.{
ExportsAction,
ExportsUpdate,
SuggestionAction,
SuggestionUpdate,
SuggestionsDatabaseAction
}
import org.enso.searcher.data.QueryResult
import org.enso.searcher.{SuggestionEntry, SuggestionsRepo}
import slick.jdbc.SQLiteProfile.api._
import slick.jdbc.meta.MTable
import slick.relational.RelationalProfile

import scala.collection.immutable.{HashMap, ListMap}
import java.util.UUID

import scala.collection.immutable.HashMap
import scala.collection.mutable
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success}

Expand Down Expand Up @@ -682,15 +678,28 @@ final class SqlSuggestionsRepo(val db: SqlDatabase)(implicit
private def insertAllQuery(
suggestions: Iterable[Suggestion]
): DBIO[Seq[Long]] = {
val suggestionsMap =
suggestions.map(s => SuggestionRowUniqueIndex(s) -> s).to(ListMap)
val rows = suggestions.map(toSuggestionRow)
for {
_ <- Suggestions ++= rows
rows <- Suggestions.result
} yield {
val rowsMap = rows.map(r => SuggestionRowUniqueIndex(r) -> r.id.get).toMap
suggestionsMap.keys.map(rowsMap(_)).toSeq
val duplicatesBuilder = Vector.newBuilder[(Suggestion, Suggestion)]
val suggestionsMap: mutable.Map[SuggestionRowUniqueIndex, Suggestion] =
mutable.LinkedHashMap()
suggestions.foreach { suggestion =>
val idx = SuggestionRowUniqueIndex(suggestion)
suggestionsMap.put(idx, suggestion).foreach { duplicate =>
duplicatesBuilder.addOne((duplicate, suggestion))
}
}
val duplicates = duplicatesBuilder.result()
if (duplicates.isEmpty) {
val rows = suggestions.map(toSuggestionRow)
for {
_ <- Suggestions ++= rows
rows <- Suggestions.result
} yield {
val rowsMap =
rows.map(r => SuggestionRowUniqueIndex(r) -> r.id.get).toMap
suggestionsMap.keys.map(rowsMap(_)).toSeq
}
} else {
DBIO.failed(SqlSuggestionsRepo.UniqueConstraintViolatedError(duplicates))
}
}

Expand Down Expand Up @@ -1053,3 +1062,14 @@ final class SqlSuggestionsRepo(val db: SqlDatabase)(implicit
} yield new UUID(m, l)

}

object SqlSuggestionsRepo {

/** An error indicating that the database unique constraint was violated.
*
* @param duplicates the entries that violate the unique constraint
*/
final case class UniqueConstraintViolatedError(
duplicates: Seq[(Suggestion, Suggestion)]
) extends Exception(s"Database unique constraint is violated [$duplicates].")
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import org.enso.polyglot.{ExportedSymbol, ModuleExports, Suggestion}
import org.enso.polyglot.runtime.Runtime.Api
import org.enso.searcher.SuggestionEntry
import org.enso.searcher.data.QueryResult
import org.enso.searcher.sql.SqlSuggestionsRepo.UniqueConstraintViolatedError
import org.enso.searcher.sql.equality.SuggestionsEquality
import org.enso.testkit.RetrySpec
import org.scalactic.TripleEqualsSupport
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

import java.sql.SQLException

import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
Expand Down Expand Up @@ -132,7 +131,10 @@ class SuggestionsRepoTest
_ <- repo.insertAll(Seq(suggestion.local, suggestion.local))
} yield ()

an[SQLException] should be thrownBy Await.result(action, Timeout)
an[UniqueConstraintViolatedError] should be thrownBy Await.result(
action,
Timeout
)
}

"select suggestion by id" taggedAs Retry in withRepo { repo =>
Expand Down

0 comments on commit b7d51ed

Please sign in to comment.