Refactoring for migrating to binny library

This commit is contained in:
eikek
2021-09-22 00:28:47 +02:00
parent 1f98d948b0
commit 20a829cf7a
45 changed files with 485 additions and 344 deletions

View File

@ -70,7 +70,7 @@ object BackendApp {
tagImpl <- OTag[F](store)
equipImpl <- OEquipment[F](store)
orgImpl <- OOrganization(store)
uploadImpl <- OUpload(store, queue, cfg.files, joexImpl)
uploadImpl <- OUpload(store, queue, joexImpl)
nodeImpl <- ONode(store)
jobImpl <- OJob(store, joexImpl)
createIndex <- CreateIndex.resource(ftsClient, store)
@ -115,7 +115,7 @@ object BackendApp {
httpClientEc: ExecutionContext
)(ftsFactory: Client[F] => Resource[F, FtsClient[F]]): Resource[F, BackendApp[F]] =
for {
store <- Store.create(cfg.jdbc, connectEC)
store <- Store.create(cfg.jdbc, cfg.files.chunkSize, connectEC)
httpClient <- BlazeClientBuilder[F](httpClientEc).resource
ftsClient <- ftsFactory(httpClient)
backend <- create(cfg, store, httpClient, ftsClient)

View File

@ -17,7 +17,6 @@ import docspell.store._
import docspell.store.queries.{QAttachment, QItem}
import docspell.store.records._
import bitpeace.{FileMeta, RangeDef}
import doobie.implicits._
trait OItemSearch[F[_]] {
@ -90,10 +89,10 @@ object OItemSearch {
trait BinaryData[F[_]] {
def data: Stream[F, Byte]
def name: Option[String]
def meta: FileMeta
def meta: RFileMeta
def fileId: Ident
}
case class AttachmentData[F[_]](ra: RAttachment, meta: FileMeta, data: Stream[F, Byte])
case class AttachmentData[F[_]](ra: RAttachment, meta: RFileMeta, data: Stream[F, Byte])
extends BinaryData[F] {
val name = ra.name
val fileId = ra.fileId
@ -101,7 +100,7 @@ object OItemSearch {
case class AttachmentSourceData[F[_]](
rs: RAttachmentSource,
meta: FileMeta,
meta: RFileMeta,
data: Stream[F, Byte]
) extends BinaryData[F] {
val name = rs.name
@ -110,7 +109,7 @@ object OItemSearch {
case class AttachmentPreviewData[F[_]](
rs: RAttachmentPreview,
meta: FileMeta,
meta: RFileMeta,
data: Stream[F, Byte]
) extends BinaryData[F] {
val name = rs.name
@ -119,7 +118,7 @@ object OItemSearch {
case class AttachmentArchiveData[F[_]](
rs: RAttachmentArchive,
meta: FileMeta,
meta: RFileMeta,
data: Stream[F, Byte]
) extends BinaryData[F] {
val name = rs.name
@ -189,7 +188,7 @@ object OItemSearch {
AttachmentData[F](
ra,
m,
store.bitpeace.fetchData2(RangeDef.all)(Stream.emit(m))
store.fileStore.getBytes(m.id)
)
}
@ -209,7 +208,7 @@ object OItemSearch {
AttachmentSourceData[F](
ra,
m,
store.bitpeace.fetchData2(RangeDef.all)(Stream.emit(m))
store.fileStore.getBytes(m.id)
)
}
@ -229,7 +228,7 @@ object OItemSearch {
AttachmentPreviewData[F](
ra,
m,
store.bitpeace.fetchData2(RangeDef.all)(Stream.emit(m))
store.fileStore.getBytes(m.id)
)
}
@ -249,7 +248,7 @@ object OItemSearch {
AttachmentPreviewData[F](
ra,
m,
store.bitpeace.fetchData2(RangeDef.all)(Stream.emit(m))
store.fileStore.getBytes(m.id)
)
}
@ -269,7 +268,7 @@ object OItemSearch {
AttachmentArchiveData[F](
ra,
m,
store.bitpeace.fetchData2(RangeDef.all)(Stream.emit(m))
store.fileStore.getBytes(m.id)
)
}
@ -277,15 +276,11 @@ object OItemSearch {
(None: Option[AttachmentArchiveData[F]]).pure[F]
}
private def makeBinaryData[A](fileId: Ident)(f: FileMeta => A): F[Option[A]] =
store.bitpeace
.get(fileId.id)
.unNoneTerminate
.compile
.last
.map(
_.map(m => f(m))
)
private def makeBinaryData[A](fileId: Ident)(f: RFileMeta => A): F[Option[A]] =
store.fileStore
.findMeta(fileId)
.map(fm => f(fm))
.value
def findAttachmentMeta(id: Ident, collective: Ident): F[Option[RAttachmentMeta]] =
store.transact(QAttachment.getAttachmentMeta(id, collective))

View File

@ -9,7 +9,6 @@ package docspell.backend.ops
import cats.data.OptionT
import cats.effect._
import cats.implicits._
import fs2.Stream
import docspell.backend.ops.OMail._
import docspell.common._
@ -18,7 +17,6 @@ import docspell.store.queries.QMails
import docspell.store.records._
import docspell.store.syntax.MimeTypes._
import bitpeace.{FileMeta, RangeDef}
import emil._
trait OMail[F[_]] {
@ -81,14 +79,17 @@ object OMail {
)
sealed trait AttachSelection {
def filter(v: Vector[(RAttachment, FileMeta)]): Vector[(RAttachment, FileMeta)]
def filter(v: Vector[(RAttachment, RFileMeta)]): Vector[(RAttachment, RFileMeta)]
}
object AttachSelection {
case object All extends AttachSelection {
def filter(v: Vector[(RAttachment, FileMeta)]): Vector[(RAttachment, FileMeta)] = v
def filter(v: Vector[(RAttachment, RFileMeta)]): Vector[(RAttachment, RFileMeta)] =
v
}
case class Selected(ids: List[Ident]) extends AttachSelection {
def filter(v: Vector[(RAttachment, FileMeta)]): Vector[(RAttachment, FileMeta)] = {
def filter(
v: Vector[(RAttachment, RFileMeta)]
): Vector[(RAttachment, RFileMeta)] = {
val set = ids.toSet
v.filter(set contains _._1.id)
}
@ -232,10 +233,10 @@ object OMail {
} yield {
val addAttach = m.attach.filter(ras).map { a =>
Attach[F](
Stream.emit(a._2).through(store.bitpeace.fetchData2(RangeDef.all))
store.fileStore.getBytes(a._2.id)
).withFilename(a._1.name)
.withLength(a._2.length)
.withMimeType(a._2.mimetype.toLocal.toEmil)
.withLength(a._2.length.bytes)
.withMimeType(a._2.mimetype.toEmil)
}
val fields: Seq[Trans[F]] = Seq(
From(sett.mailFrom),

View File

@ -12,14 +12,13 @@ import cats.effect._
import cats.implicits._
import fs2.Stream
import docspell.backend.{Config, JobFactory}
import docspell.backend.JobFactory
import docspell.common._
import docspell.common.syntax.all._
import docspell.store.Store
import docspell.store.queue.JobQueue
import docspell.store.records._
import bitpeace.MimetypeHint
import org.log4s._
trait OUpload[F[_]] {
@ -116,7 +115,6 @@ object OUpload {
def apply[F[_]: Sync](
store: Store[F],
queue: JobQueue[F],
cfg: Config.Files,
joex: OJoex[F]
): Resource[F, OUpload[F]] =
Resource.pure[F, OUpload[F]](new OUpload[F] {
@ -205,11 +203,10 @@ object OUpload {
/** Saves the file into the database. */
private def saveFile(file: File[F]): F[Option[ProcessItemArgs.File]] =
logger.finfo(s"Receiving file $file") *>
store.bitpeace
.saveNew(file.data, cfg.chunkSize, MimetypeHint(file.name, None), None)
file.data
.through(store.fileStore.save(MimeTypeHint(file.name, None)))
.compile
.lastOrError
.map(fm => Ident.unsafe(fm.id))
.attempt
.map(
_.fold(