Configure postgres fts backend

This commit is contained in:
eikek
2022-03-21 11:04:58 +01:00
parent 1e56e832da
commit 21e13341e3
18 changed files with 295 additions and 56 deletions

View File

@ -0,0 +1,27 @@
/*
* Copyright 2020 Eike K. & Contributors
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package docspell.config
import cats.data.NonEmptyList
sealed trait FtsType {
def name: String
}
object FtsType {
case object Solr extends FtsType { val name = "solr" }
case object PostgreSQL extends FtsType { val name = "postgresql" }
val all: NonEmptyList[FtsType] =
NonEmptyList.of(Solr, PostgreSQL)
def fromName(str: String): Either[String, FtsType] =
all.find(_.name.equalsIgnoreCase(str)).toRight(s"Unknown fts type: $str")
def unsafeFromName(str: String): FtsType =
fromName(str).fold(sys.error, identity)
}

View File

@ -10,9 +10,11 @@ import java.nio.file.{Path => JPath}
import scala.reflect.ClassTag
import cats.syntax.all._
import fs2.io.file.Path
import docspell.common._
import docspell.ftspsql.{PgQueryParser, RankNormalization}
import docspell.logging.{Level, LogConfig}
import com.github.eikek.calev.CalEvent
@ -85,11 +87,28 @@ object Implicits {
implicit val fileStoreTypeReader: ConfigReader[FileStoreType] =
ConfigReader[String].emap(reason(FileStoreType.fromString))
def reason[A: ClassTag](
f: String => Either[String, A]
): String => Either[FailureReason, A] =
implicit val pgQueryParserReader: ConfigReader[PgQueryParser] =
ConfigReader[String].emap(reason(PgQueryParser.fromName))
implicit val pgRankNormalizationReader: ConfigReader[RankNormalization] =
ConfigReader[List[Int]].emap(
reason(ints => ints.traverse(RankNormalization.byNumber).map(_.reduce(_ && _)))
)
implicit val languageReader: ConfigReader[Language] =
ConfigReader[String].emap(reason(Language.fromString))
implicit def languageMapReader[B: ConfigReader]: ConfigReader[Map[Language, B]] =
pureconfig.configurable.genericMapReader[Language, B](reason(Language.fromString))
implicit val ftsTypeReader: ConfigReader[FtsType] =
ConfigReader[String].emap(reason(FtsType.fromName))
def reason[T, A: ClassTag](
f: T => Either[String, A]
): T => Either[FailureReason, A] =
in =>
f(in).left.map(str =>
CannotConvert(in, implicitly[ClassTag[A]].runtimeClass.toString, str)
CannotConvert(in.toString, implicitly[ClassTag[A]].runtimeClass.toString, str)
)
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 2020 Eike K. & Contributors
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package docspell.config
import docspell.common._
import docspell.ftspsql._
import docspell.store.JdbcConfig
case class PgFtsConfig(
useDefaultConnection: Boolean,
jdbc: JdbcConfig,
pgQueryParser: PgQueryParser,
pgRankNormalization: RankNormalization,
pgConfig: Map[Language, String]
) {
def toPsqlConfig(stdConn: JdbcConfig): PsqlConfig = {
val db =
if (useDefaultConnection) stdConn
else jdbc
PsqlConfig(
db.url,
db.user,
Password(db.password),
pgConfig,
pgQueryParser,
pgRankNormalization
)
}
}
object PgFtsConfig {}