Skip to content

Commit

Permalink
use hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
johnynek committed Jan 17, 2025
1 parent d643bf3 commit 787ea21
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
17 changes: 14 additions & 3 deletions cli/src/main/scala/org/bykn/bosatsu/IOPlatformIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import _root_.bosatsu.{TypedAst => proto}
import cats.MonadError
import cats.effect.{IO, Resource}
import com.monovore.decline.Argument
import java.nio.file.{Paths, Path => JPath}
import java.nio.file.{Paths, Path => JPath, Files}
import java.io.{
FileInputStream,
FileOutputStream,
Expand All @@ -14,6 +14,7 @@ import java.io.{
}
import scalapb.{GeneratedMessage, GeneratedMessageCompanion}
import org.typelevel.paiges.Doc
import org.bykn.bosatsu.hashing.{Hashed, Algo}

import cats.syntax.all._

Expand Down Expand Up @@ -60,7 +61,7 @@ object IOPlatformIO extends PlatformIO[IO, JPath] {
parResource.use(fn)

def readUtf8(path: Path): IO[String] =
IO.blocking(new String(java.nio.file.Files.readAllBytes(path), "utf-8"))
IO.blocking(new String(Files.readAllBytes(path), "utf-8"))

def fsDataType(p: Path): IO[Option[PlatformIO.FSDataType]] =
IO.blocking {
Expand Down Expand Up @@ -93,6 +94,15 @@ object IOPlatformIO extends PlatformIO[IO, JPath] {
}
}

def readHashed[A <: GeneratedMessage, H](
path: Path
)(implicit gmc: GeneratedMessageCompanion[A], algo: Algo[H]): IO[Hashed[H, A]] =
IO.blocking {
val bytes = Files.readAllBytes(path)
val a = gmc.parseFrom(bytes)
Hashed(Algo.hashBytes[H](bytes), a)
}

def write(a: GeneratedMessage, path: Path): IO[Unit] =
IO.blocking {
val f = path.toFile
Expand Down Expand Up @@ -125,7 +135,8 @@ object IOPlatformIO extends PlatformIO[IO, JPath] {
def readPackages(paths: List[Path]): IO[List[Package.Typed[Unit]]] =
readInterfacesAndPackages(Nil, paths).map(_._2)

def readLibrary(path: Path): IO[proto.Library] = read[proto.Library](path)
def readLibrary(path: Path): IO[Hashed[Algo.Sha256, proto.Library]] =
readHashed[proto.Library, Algo.Sha256](path)

def writeInterfaces(
interfaces: List[Package.Interface],
Expand Down
17 changes: 16 additions & 1 deletion cliJS/src/main/scala/org/bykn/bosatsu/Fs2PlatformIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import cats.effect.{IO, Resource}
import fs2.io.file.{Files, Path}
import com.monovore.decline.Argument
import org.typelevel.paiges.Doc
import org.bykn.bosatsu.hashing.{Hashed, Algo}
import scala.util.{Failure, Success, Try}

import cats.syntax.all._
Expand Down Expand Up @@ -84,6 +85,19 @@ object Fs2PlatformIO extends PlatformIO[IO, Path] {
IO(A.parseFrom(bytes))
}

def readHashed[A <: GeneratedMessage, H](
path: Path
)(implicit gmc: GeneratedMessageCompanion[A], algo: Algo[H]): IO[Hashed[H, A]] =
FilesIO
.readAll(path)
.compile.to(Array)
.flatMap { bytes =>
IO {
val a = gmc.parseFrom(bytes)
Hashed(Algo.hashBytes[H](bytes), a)
}
}

def readPackages(paths: List[Path]): IO[List[Package.Typed[Unit]]] =
paths.parTraverse { path =>
for {
Expand All @@ -102,7 +116,8 @@ object Fs2PlatformIO extends PlatformIO[IO, Path] {
}
.map(_.flatten)

def readLibrary(path: Path): IO[proto.Library] = read[proto.Library](path)
def readLibrary(path: Path): IO[Hashed[Algo.Sha256, proto.Library]] =
readHashed[proto.Library, Algo.Sha256](path)

/** given an ordered list of prefered roots, if a packFile starts with one of
* these roots, return a PackageName based on the rest
Expand Down
9 changes: 6 additions & 3 deletions core/src/main/scala/org/bykn/bosatsu/MemoryMain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ object MemoryMain {
case class Str(str: String) extends FileContent
case class Packages(ps: List[Package.Typed[Unit]]) extends FileContent
case class Interfaces(ifs: List[Package.Interface]) extends FileContent
case class Lib(lib: proto.Library) extends FileContent
case class Lib(lib: Hashed[Algo.Sha256, proto.Library]) extends FileContent
}

case class State(children: SortedMap[String, Either[State, FileContent]], stdOut: Doc, stdErr: Doc) {
Expand Down Expand Up @@ -324,8 +324,11 @@ object MemoryMain {
def writePackages[A](packs: List[Package.Typed[A]], path: Path): F[Unit] =
writeFC(path, FileContent.Packages(packs.map(_.void)))

def writeLibrary(lib: proto.Library, path: Path): F[Unit] =
writeFC(path, FileContent.Lib(lib))
def writeLibrary(lib: proto.Library, path: Path): F[Unit] = {
val hash = Algo.hashBytes(lib.toByteArray)
val hashed = Hashed(hash, lib)
writeFC(path, FileContent.Lib(hashed))
}

def writeStdout(doc: Doc): F[Unit] =
StateT.modify { state =>
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/scala/org/bykn/bosatsu/hashing/Algo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.bykn.bosatsu.hashing

sealed abstract class Algo[A] {
def name: String
def hashBytesHex(bytes: Array[Byte]): HashValue[A]
def hashBytes(bytes: Array[Byte]): HashValue[A]
}

object Algo {
Expand All @@ -12,10 +12,10 @@ object Algo {
implicit val sha256Algo: Algo[Sha256] =
new Algo[Sha256] {
def name: String = "sha256"
def hashBytesHex(bytes: Array[Byte]): HashValue[Sha256] =
def hashBytes(bytes: Array[Byte]): HashValue[Sha256] =
HashValue(Sha256Hash.sha256HashHex(bytes))
}

def hashBytesHex[A](bytes: Array[Byte])(implicit algo: Algo[A]): HashValue[A] =
algo.hashBytesHex(bytes)
def hashBytes[A](bytes: Array[Byte])(implicit algo: Algo[A]): HashValue[A] =
algo.hashBytes(bytes)
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ case class LibConfig(

val thisHistory = previous match {
case None => proto.LibHistory()
case Some(Hashed(hashValue, p)) =>
case Some(Hashed(_, p)) =>
val prevHistory = p.history.getOrElse(proto.LibHistory())
val desc = p.descriptor match {
case Some(desc) => desc
Expand Down

0 comments on commit 787ea21

Please sign in to comment.