Skip to content

Commit

Permalink
Merge pull request #291 from retronym/topic/millis
Browse files Browse the repository at this point in the history
Expose Milli.milli to let Zinc be more efficient
  • Loading branch information
eed3si9n authored Apr 6, 2020
2 parents df72997 + 953b479 commit cd82d02
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 36 deletions.
3 changes: 2 additions & 1 deletion io/src/main/scala/sbt/internal/io/Milli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ private abstract class MilliNative[Native] extends Milli {
* FFI for standard C library.
*/
private sealed trait PosixBase extends Library {

/** http://www.cplusplus.com/reference/cstring/strerror/ */
def strerror(errnum: Int): String
}
Expand Down Expand Up @@ -368,7 +369,7 @@ object Milli {
}
}

private val milli =
val milli: Milli =
if (jdkTimestamps || !isIntel)
JavaMilli
else
Expand Down
35 changes: 14 additions & 21 deletions io/src/main/scala/sbt/internal/io/Retry.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
package sbt.internal.io
import java.io.IOException

import scala.annotation.tailrec
import scala.util.control.NonFatal

private[sbt] object Retry {
Expand All @@ -20,35 +19,29 @@ private[sbt] object Retry {
try System.getProperty("sbt.io.retry.limit", defaultLimit.toString).toInt
catch { case NonFatal(_) => defaultLimit }
}
private[sbt] def apply[T](f: => T, excludedExceptions: Class[_ <: IOException]*): T =
private[sbt] def apply[@specialized T](f: => T, excludedExceptions: Class[_ <: IOException]*): T =
apply(f, limit, excludedExceptions: _*)
private[sbt] def apply[T](
private[sbt] def apply[@specialized T](
f: => T,
limit: Int,
excludedExceptions: Class[_ <: IOException]*
): T = {
lazy val filter: Exception => Boolean = excludedExceptions match {
def filter(e: Exception): Boolean = excludedExceptions match {
case s if s.nonEmpty =>
(e: Exception) => !excludedExceptions.exists(_.isAssignableFrom(e.getClass))
!excludedExceptions.exists(_.isAssignableFrom(e.getClass))
case _ =>
(_: Exception) => true
true
}
@tailrec
def impl(attempt: Int): T = {
val (retry, res) = try false -> Right(f)
catch {
case e: IOException if filter(e) && (attempt < limit) => (true, Left(e))
case e: IOException => (false, Left(e))
}
if (retry) {
Thread.sleep(0); impl(attempt + 1)
} else {
res match {
case Right(r) => r
case Left(e) => throw e
}
var attempt = 1
while (attempt < limit) {
try {
return f
} catch {
case e: IOException if filter(e) && (attempt < limit) =>
Thread.sleep(0);
attempt += 1
}
}
impl(1)
throw new IllegalStateException()
}
}
6 changes: 4 additions & 2 deletions io/src/main/scala/sbt/internal/nio/SwovalConverters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ private[sbt] object SwovalFileTreeView extends FileTreeView.Nio[FileAttributes]
}
result.result()
},
classOf[NotDirectoryException],
classOf[NoSuchFileException]
excludedExceptions: _*
)

private val excludedExceptions =
List(classOf[NotDirectoryException], classOf[NoSuchFileException])
}
9 changes: 7 additions & 2 deletions io/src/main/scala/sbt/io/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import sbt.nio.file.FileTreeView
import scala.Function.tupled
import scala.annotation.tailrec
import scala.collection.JavaConverters._
import scala.collection.immutable
import scala.collection.immutable.TreeSet
import scala.collection.mutable.{ HashMap, HashSet }
import scala.reflect.{ Manifest => SManifest }
Expand Down Expand Up @@ -1404,7 +1405,7 @@ object IO {
*/
def getModifiedTimeOrZero(file: File): Long =
try {
Retry(Milli.getModifiedTime(file), classOf[FileNotFoundException])
Retry(Milli.getModifiedTime(file), excludeFileNotFound: _*)
} catch {
case _: FileNotFoundException =>
val unnormalized = file.toPath
Expand All @@ -1429,7 +1430,7 @@ object IO {
*/
def setModifiedTimeOrFalse(file: File, mtime: Long): Boolean =
try {
Retry(Milli.setModifiedTime(file, mtime), classOf[FileNotFoundException])
Retry(Milli.setModifiedTime(file, mtime), excludeFileNotFound: _*)
true
} catch {
case _: FileNotFoundException =>
Expand Down Expand Up @@ -1461,4 +1462,8 @@ object IO {
// see Java bug #6791812
setModifiedTimeOrFalse(targetFile, math.max(last, 0L))
}

private val excludeFileNotFound: immutable.Seq[Class[_ <: IOException]] = List(
classOf[FileNotFoundException]
)
}
22 changes: 12 additions & 10 deletions io/src/main/scala/sbt/nio/file/FileTreeView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,18 @@ object FileTreeView {
* An implementation of [[FileTreeView]] that uses built in jvm apis. This implementation
* will throw an IOException if the input path is not a directory or doesn't exist.
*/
val nio: FileTreeView.Nio[FileAttributes] = (path: Path) =>
Retry(
{
val stream = Files.list(path)
try stream.iterator.asScala.flatMap(p => FileAttributes(p).toOption.map(p -> _)).toVector
finally stream.close()
},
classOf[NotDirectoryException],
classOf[NoSuchFileException]
)
val nio: FileTreeView.Nio[FileAttributes] = {
val excludedExceptions = List(classOf[NotDirectoryException], classOf[NoSuchFileException])
(path: Path) =>
Retry(
{
val stream = Files.list(path)
try stream.iterator.asScala.flatMap(p => FileAttributes(p).toOption.map(p -> _)).toVector
finally stream.close()
},
excludedExceptions: _*
)
}

/**
* Adds additional methods to [[FileTreeView]]. This api may be changed so it should not be
Expand Down

0 comments on commit cd82d02

Please sign in to comment.