Skip to content

Commit

Permalink
Merge pull request #9 from evolvedbinary/remove-cats-effect
Browse files Browse the repository at this point in the history
Remove cats-effect
  • Loading branch information
adamretter authored Oct 2, 2023
2 parents 7348b92 + 05e3b1b commit d288f80
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 242 deletions.
2 changes: 0 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ headers := Map(
libraryDependencies ++= {

val catsCoreV = "2.10.0"
val catsEffectV = "3.5.1"
val existV = "4.4.0"
val algoliaV = "2.19.0"
val akkaV = "2.5.16"
Expand All @@ -33,7 +32,6 @@ libraryDependencies ++= {
"org.scala-lang.modules" %% "scala-parser-combinators" % "1.1.1",
"org.scala-lang.modules" %% "scala-java8-compat" % "0.9.0",
"org.typelevel" %% "cats-core" % catsCoreV,
"org.typelevel" %% "cats-effect" % catsEffectV,
"org.clapper" %% "grizzled-slf4j" % "1.3.2"
exclude("org.slf4j", "slf4j-api"),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ import org.exist.xquery.XQueryContext
import org.exist_db.collection_config._1.Algolia
import org.w3c.dom.{Element, Node, NodeList}
import AlgoliaIndexWorker._
import akka.actor.{ActorPath, ActorRef, ActorSystem}
import akka.actor.{ActorRef, ActorSystem}
import org.apache.logging.log4j.{LogManager, Logger}
import org.humanistika.exist.index.algolia.backend.IncrementalIndexingManagerActor
import org.humanistika.exist.index.algolia.backend.IncrementalIndexingManagerActor.RemoveForCollection

import scala.collection.JavaConverters._
Expand Down
55 changes: 22 additions & 33 deletions src/main/scala/org/humanistika/exist/index/algolia/Checksum.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ import java.io.InputStream
import java.nio.file.{Files, Path}
import java.security.MessageDigest

import cats.effect.{IO, Resource}
import cats.effect.unsafe.implicits.global // TODO(AR) switch to using cats.effect.IOApp

import scala.annotation.tailrec

/**
Expand Down Expand Up @@ -66,18 +63,12 @@ object Checksum {
digest.digest()
}

val checksumIO = Resource.make(IO { Files.newInputStream(file) })(is => IO { is.close() }).use { is =>
getHash(algorithm).flatMap { digest =>
// 16 KB buffer
IO.pure(Array.ofDim[Byte](bufferSize)).flatMap { buf =>
IO { digestStream(is, buf, digest) }
}
}
}

checksumIO
.attempt
.unsafeRunSync()
With(Files.newInputStream(file)) { is =>
val digest = getHash(algorithm)
// 16 KB buffer
val buf = Array.ofDim[Byte](bufferSize)
digestStream(is, buf, digest)
}.toEither
}

/**
Expand All @@ -87,24 +78,22 @@ object Checksum {
*
* @return the message digest
*/
private def getHash(algorithm: Algorithm) : IO[MessageDigest] = {
IO {
algorithm match {
case MD2 =>
MessageDigest.getInstance("MD2")
case MD5 =>
MessageDigest.getInstance("MD5")
case SHA1 =>
MessageDigest.getInstance("SHA-1")
case SHA256 =>
MessageDigest.getInstance("SHA-256")
case SHA384 =>
MessageDigest.getInstance("SHA-384")
case SHA512 =>
MessageDigest.getInstance("SHA-512")
case _ =>
throw new UnsupportedOperationException(s"Support for $algorithm not yet implemented.")
}
private def getHash(algorithm: Algorithm) : MessageDigest = {
algorithm match {
case MD2 =>
MessageDigest.getInstance("MD2")
case MD5 =>
MessageDigest.getInstance("MD5")
case SHA1 =>
MessageDigest.getInstance("SHA-1")
case SHA256 =>
MessageDigest.getInstance("SHA-256")
case SHA384 =>
MessageDigest.getInstance("SHA-384")
case SHA512 =>
MessageDigest.getInstance("SHA-512")
case _ =>
throw new UnsupportedOperationException(s"Support for $algorithm not yet implemented.")
}
}
}
75 changes: 30 additions & 45 deletions src/main/scala/org/humanistika/exist/index/algolia/Serializer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import org.exist.util.serializer.{SAXSerializer, SerializerPool}
import org.humanistika.exist.index.algolia.JsonUtil.writeValueField
import org.humanistika.exist.index.algolia.LiteralTypeConfig.LiteralTypeConfig
import org.w3c.dom.{Attr, Element, NamedNodeMap, Node, NodeList, Text}
import cats.effect.{IO, Resource}
import cats.effect.unsafe.implicits.global // TODO(AR) switch to using cats.effect.IOApp
import cats.syntax.either._

object Serializer {
Expand Down Expand Up @@ -123,44 +121,38 @@ object Serializer {
}
}

val jsonIO: IO[String] = Resource.fromAutoCloseable(IO { new StringWriter() }).use { writer =>
Resource.fromAutoCloseable(IO { new JsonFactory().createGenerator(writer) }).use { gen =>
IO {
val childNodes = element.getChildNodes
if (hasOnlyTextChildren(childNodes)) {
With(new StringWriter()) { writer =>
With(new JsonFactory().createGenerator(writer)) { gen =>
val childNodes = element.getChildNodes
if (hasOnlyTextChildren(childNodes)) {

// needed so Jackson's JSON Generator won't complain
gen.writeStartObject()
// needed so Jackson's JSON Generator won't complain
gen.writeStartObject()

gen.writeRaw(',')
gen.writeRaw(',')

serializeAttributes(gen)(element.getAttributes)
if (childNodes.getLength > 0) {
serializeTextNodes(gen)(childNodes)
}
serializeAttributes(gen)(element.getAttributes)
if (childNodes.getLength > 0) {
serializeTextNodes(gen)(childNodes)
}

// needed so Jackson's JSON Generator won't complain
gen.writeEndObject()
} else {
serialize(element).map { elementGenerator =>
// needed so Jackson's JSON Generator won't complain
gen.writeEndObject()
} else {
serialize(element).map { elementGenerator =>

// needed so Jackson's JSON Generator won't complain
gen.writeStartObject()
// needed so Jackson's JSON Generator won't complain
gen.writeStartObject()

elementGenerator(gen)
elementGenerator(gen)

// needed so Jackson's JSON Generator won't complain
gen.writeEndObject()
}
// needed so Jackson's JSON Generator won't complain
gen.writeEndObject()
}
}
}.map(_ => stripStartObjectEndObject(writer.toString)) // strip the extras we added for the JSON generator (so it won't complain)
}

jsonIO
.redeem(_.asLeft.leftMap(Seq(_)), _.asRight)
.unsafeRunSync()
}
}.flatten.toEither.leftMap(Seq(_))

def serializeAsText(node: Node): Either[Seq[Throwable], String] = {
val properties = new Properties()
Expand Down Expand Up @@ -226,22 +218,15 @@ object Serializer {
}

def serialize(node: Node, properties: Properties): Either[Seq[Throwable], String] = {

val serializationIO = Resource.make(IO {serializerPool.borrowObject(classOf[SAXSerializer]).asInstanceOf[SAXSerializer]})(serializer => IO {serializerPool.returnObject()}).use { serializer =>
Resource.fromAutoCloseable(IO {new StringWriter()}).use { writer =>
IO {
serializer.setOutput(writer, properties)

val transformer = transformerFactory.newTransformer()
val result = new SAXResult(serializer)
transformer.transform(new DOMSource(node), result)
writer.toString
}
With(serializerPool.borrowObject(classOf[SAXSerializer]).asInstanceOf[SAXSerializer])(serializer => serializerPool.returnObject(serializer)) { serializer =>
With(new StringWriter()) { writer =>
serializer.setOutput(writer, properties)

val transformer = transformerFactory.newTransformer()
val result = new SAXResult(serializer)
transformer.transform(new DOMSource(node), result)
writer.toString
}
}

serializationIO
.redeem(_.asLeft.leftMap(Seq(_)), _.asRight)
.unsafeRunSync()
}.flatten.toEither.leftMap(Seq(_))
}
}
32 changes: 32 additions & 0 deletions src/main/scala/org/humanistika/exist/index/algolia/With.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.humanistika.exist.index.algolia

import scala.util.{Failure, Success, Try}

// TODO(AR) replace with `Using` in Scala 2.13
object With {
def apply[R, A](acquire: => R)(release: R => Unit)(f: R => A): Try[A] = {
val r = acquire
try {
val a = f(r)
Success(a)
} catch {
case t: Throwable =>
Failure(t)
} finally {
release(r)
}
}

def apply[R <: AutoCloseable, A](acquire: => R)(f: R => A): Try[A] = {
val r = acquire
try {
val a = f(r)
Success(a)
} catch {
case t: Throwable =>
Failure(t)
} finally {
r.close()
}
}
}
Loading

0 comments on commit d288f80

Please sign in to comment.