mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-22 02:18:26 +00:00
Configure postgres fts backend
This commit is contained in:
27
modules/config/src/main/scala/docspell/config/FtsType.scala
Normal file
27
modules/config/src/main/scala/docspell/config/FtsType.scala
Normal 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)
|
||||
}
|
@ -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)
|
||||
)
|
||||
}
|
||||
|
@ -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 {}
|
Reference in New Issue
Block a user