mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-06 15:15:58 +00:00
Refactoring index migration task
This commit is contained in:
parent
9acea8307d
commit
2f6e531c45
@ -22,7 +22,7 @@ object IndexTask {
|
|||||||
.flatMap(_ =>
|
.flatMap(_ =>
|
||||||
Task(ctx =>
|
Task(ctx =>
|
||||||
Migration[F](cfg, ctx.store, fts, ctx.logger)
|
Migration[F](cfg, ctx.store, fts, ctx.logger)
|
||||||
.run(Migration.migrationTasks[F])
|
.run(migrationTasks[F])
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -45,4 +45,11 @@ object IndexTask {
|
|||||||
Some(taskName)
|
Some(taskName)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
private val solrEngine = Ident.unsafe("solr")
|
||||||
|
def migrationTasks[F[_]: Effect]: List[Migration[F]] =
|
||||||
|
List(
|
||||||
|
Migration[F](1, solrEngine, "initialize", MigrationTask[F](ctx => ctx.fts.initialize)),
|
||||||
|
Migration[F](2, solrEngine, "Index all from database", MigrationTask.insertAll[F])
|
||||||
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,13 @@
|
|||||||
|
package docspell.joex.fts
|
||||||
|
|
||||||
|
import docspell.common.Logger
|
||||||
|
import docspell.joex.Config
|
||||||
|
import docspell.store.Store
|
||||||
|
import docspell.ftsclient.FtsClient
|
||||||
|
|
||||||
|
case class MigrateCtx[F[_]](
|
||||||
|
cfg: Config.FullTextSearch,
|
||||||
|
store: Store[F],
|
||||||
|
fts: FtsClient[F],
|
||||||
|
logger: Logger[F]
|
||||||
|
)
|
@ -8,26 +8,17 @@ import docspell.common._
|
|||||||
import docspell.joex.Config
|
import docspell.joex.Config
|
||||||
import docspell.store.{AddResult, Store}
|
import docspell.store.{AddResult, Store}
|
||||||
import docspell.store.records.RFtsMigration
|
import docspell.store.records.RFtsMigration
|
||||||
import docspell.store.queries.{QAttachment, QItem}
|
|
||||||
import docspell.ftsclient._
|
import docspell.ftsclient._
|
||||||
|
|
||||||
object Migration {
|
|
||||||
private val solrEngine = Ident.unsafe("solr")
|
|
||||||
|
|
||||||
case class MigrateCtx[F[_]](
|
|
||||||
cfg: Config.FullTextSearch,
|
|
||||||
store: Store[F],
|
|
||||||
fts: FtsClient[F],
|
|
||||||
logger: Logger[F]
|
|
||||||
)
|
|
||||||
|
|
||||||
case class Migration[F[_]](
|
case class Migration[F[_]](
|
||||||
version: Int,
|
version: Int,
|
||||||
engine: Ident,
|
engine: Ident,
|
||||||
description: String,
|
description: String,
|
||||||
task: Kleisli[F, MigrateCtx[F], Unit]
|
task: MigrationTask[F]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
object Migration {
|
||||||
|
|
||||||
def apply[F[_]: Effect](
|
def apply[F[_]: Effect](
|
||||||
cfg: Config.FullTextSearch,
|
cfg: Config.FullTextSearch,
|
||||||
store: Store[F],
|
store: Store[F],
|
||||||
@ -72,47 +63,4 @@ object Migration {
|
|||||||
ctx.logger.info(s"Migration ${m.version}/${m.description} already applied.")
|
ctx.logger.info(s"Migration ${m.version}/${m.description} already applied.")
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
def migrationTasks[F[_]: Effect]: List[Migration[F]] =
|
|
||||||
List(
|
|
||||||
Migration[F](1, solrEngine, "initialize", Kleisli(ctx => ctx.fts.initialize)),
|
|
||||||
Migration[F](
|
|
||||||
2,
|
|
||||||
solrEngine,
|
|
||||||
"Index all attachments from database",
|
|
||||||
Kleisli(ctx =>
|
|
||||||
ctx.fts.indexData(
|
|
||||||
ctx.logger,
|
|
||||||
ctx.store
|
|
||||||
.transact(
|
|
||||||
QAttachment.allAttachmentMetaAndName(ctx.cfg.migration.indexAllChunk)
|
|
||||||
)
|
|
||||||
.map(caa =>
|
|
||||||
TextData
|
|
||||||
.attachment(
|
|
||||||
caa.item,
|
|
||||||
caa.id,
|
|
||||||
caa.collective,
|
|
||||||
caa.lang,
|
|
||||||
caa.name,
|
|
||||||
caa.content
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
),
|
|
||||||
Migration[F](
|
|
||||||
3,
|
|
||||||
solrEngine,
|
|
||||||
"Index all items from database",
|
|
||||||
Kleisli(ctx =>
|
|
||||||
ctx.fts.indexData(
|
|
||||||
ctx.logger,
|
|
||||||
ctx.store
|
|
||||||
.transact(QItem.allNameAndNotes(ctx.cfg.migration.indexAllChunk * 5))
|
|
||||||
.map(nn => TextData.item(nn.id, nn.collective, Option(nn.name), nn.notes))
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,56 @@
|
|||||||
|
package docspell.joex.fts
|
||||||
|
|
||||||
|
import cats.effect._
|
||||||
|
import cats.data.{Kleisli, NonEmptyList}
|
||||||
|
import cats.{FlatMap, Semigroup}
|
||||||
|
import docspell.store.queries.{QAttachment, QItem}
|
||||||
|
import docspell.ftsclient.TextData
|
||||||
|
|
||||||
|
object MigrationTask {
|
||||||
|
def apply[F[_]](f: MigrateCtx[F] => F[Unit]): MigrationTask[F] =
|
||||||
|
Kleisli(f)
|
||||||
|
|
||||||
|
def all[F[_]: FlatMap](
|
||||||
|
m0: MigrationTask[F],
|
||||||
|
mn: MigrationTask[F]*
|
||||||
|
): MigrationTask[F] =
|
||||||
|
NonEmptyList.of(m0, mn: _*).reduce(semigroup[F])
|
||||||
|
|
||||||
|
implicit def semigroup[F[_]: FlatMap]: Semigroup[MigrationTask[F]] =
|
||||||
|
Semigroup.instance((mt1, mt2) => mt1.flatMap(_ => mt2))
|
||||||
|
|
||||||
|
// some tasks
|
||||||
|
|
||||||
|
def insertAll[F[_]: Effect]: MigrationTask[F] =
|
||||||
|
MigrationTask
|
||||||
|
.all(
|
||||||
|
MigrationTask(ctx =>
|
||||||
|
ctx.fts.indexData(
|
||||||
|
ctx.logger,
|
||||||
|
ctx.store
|
||||||
|
.transact(
|
||||||
|
QAttachment.allAttachmentMetaAndName(ctx.cfg.migration.indexAllChunk)
|
||||||
|
)
|
||||||
|
.map(caa =>
|
||||||
|
TextData
|
||||||
|
.attachment(
|
||||||
|
caa.item,
|
||||||
|
caa.id,
|
||||||
|
caa.collective,
|
||||||
|
caa.lang,
|
||||||
|
caa.name,
|
||||||
|
caa.content
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
),
|
||||||
|
MigrationTask(ctx =>
|
||||||
|
ctx.fts.indexData(
|
||||||
|
ctx.logger,
|
||||||
|
ctx.store
|
||||||
|
.transact(QItem.allNameAndNotes(ctx.cfg.migration.indexAllChunk * 5))
|
||||||
|
.map(nn => TextData.item(nn.id, nn.collective, Option(nn.name), nn.notes))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package docspell.joex
|
||||||
|
|
||||||
|
import cats.data.Kleisli
|
||||||
|
|
||||||
|
package object fts {
|
||||||
|
|
||||||
|
type MigrationTask[F[_]] = Kleisli[F, MigrateCtx[F], Unit]
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user