Minor improvements

This commit is contained in:
Eike Kettner 2020-11-09 21:16:53 +01:00
parent 29455d638c
commit 10305bc82d
4 changed files with 27 additions and 26 deletions

View File

@ -48,8 +48,8 @@ object MakePageCountTask {
private def pageCountExists[F[_]: Sync](ctx: Context[F, Args]): F[Boolean] =
ctx.store.transact(
RAttachmentMeta
.findById(ctx.args.attachment)
.map(_.flatMap(_.pages).exists(_ > 0))
.findPageCountById(ctx.args.attachment)
.map(_.exists(_ > 0))
)
}

View File

@ -56,22 +56,15 @@ object AllPreviewsTask {
private def createJobs[F[_]: Sync](
ctx: Context[F, Args]
)(ras: Chunk[RAttachment]): Stream[F, RJob] = {
val collectiveOrSystem = ctx.args.collective.getOrElse(DocspellSystem.taskGroup)
val collectiveOrSystem = {
val cid = ctx.args.collective.getOrElse(DocspellSystem.taskGroup)
AccountId(cid, DocspellSystem.user)
}
def mkJob(ra: RAttachment): F[RJob] =
for {
id <- Ident.randomId[F]
now <- Timestamp.current[F]
} yield RJob.newJob(
id,
MakePreviewArgs.taskName,
collectiveOrSystem,
JobFactory.makePreview(
MakePreviewArgs(ra.id, ctx.args.storeMode),
s"Create preview ${ra.id.id}/${ra.name.getOrElse("-")}",
now,
collectiveOrSystem,
Priority.Low,
Some(MakePreviewArgs.taskName / ra.id)
collectiveOrSystem.some
)
val jobs = ras.traverse(mkJob)

View File

@ -257,6 +257,7 @@ object RAttachment {
def findAllWithoutPageCount(chunkSize: Int): Stream[ConnectionIO, RAttachment] = {
val aId = Columns.id.prefix("a")
val aCreated = Columns.created.prefix("a")
val mId = RAttachmentMeta.Columns.id.prefix("m")
val mPages = RAttachmentMeta.Columns.pages.prefix("m")
@ -265,7 +266,7 @@ object RAttachment {
RAttachmentMeta.table ++ fr"m ON" ++ aId.is(mId)
val cond = mPages.isNull
selectSimple(cols, join, cond)
(selectSimple(cols, join, cond) ++ orderBy(aCreated.desc))
.query[RAttachment]
.streamWithChunkSize(chunkSize)
}
@ -276,6 +277,7 @@ object RAttachment {
): Stream[ConnectionIO, RAttachment] = {
val aId = Columns.id.prefix("a")
val aItem = Columns.itemId.prefix("a")
val aCreated = Columns.created.prefix("a")
val pId = RAttachmentPreview.Columns.id.prefix("p")
val iId = RItem.Columns.id.prefix("i")
val iColl = RItem.Columns.cid.prefix("i")
@ -292,11 +294,11 @@ object RAttachment {
case Some(cid) =>
val join = baseJoin ++ fr"INNER JOIN" ++ RItem.table ++ fr"i ON" ++ iId.is(aItem)
val cond = and(baseCond ++ Seq(iColl.is(cid)))
selectSimple(cols, join, cond)
(selectSimple(cols, join, cond) ++ orderBy(aCreated.desc))
.query[RAttachment]
.streamWithChunkSize(chunkSize)
case None =>
selectSimple(cols, baseJoin, and(baseCond))
(selectSimple(cols, baseJoin, and(baseCond)) ++ orderBy(aCreated.desc))
.query[RAttachment]
.streamWithChunkSize(chunkSize)
}

View File

@ -54,6 +54,12 @@ object RAttachmentMeta {
def findById(attachId: Ident): ConnectionIO[Option[RAttachmentMeta]] =
selectSimple(all, table, id.is(attachId)).query[RAttachmentMeta].option
def findPageCountById(attachId: Ident): ConnectionIO[Option[Int]] =
selectSimple(Seq(pages), table, id.is(attachId))
.query[Option[Int]]
.option
.map(_.flatten)
def upsert(v: RAttachmentMeta): ConnectionIO[Int] =
for {
n0 <- update(v)