Final case classes (#32)

* case classes are final

* [Counters] Extract to it's own file

* [LocalFile] allow overriding Hash
This commit is contained in:
Paul Campbell 2019-06-01 22:43:26 +01:00 committed by GitHub
parent 0386fde322
commit bb283a12d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 56 additions and 42 deletions

View file

@ -1,9 +1,10 @@
package net.kemitix.s3thorp package net.kemitix.s3thorp
sealed trait Action sealed trait Action
case class DoNothing(remoteKey: RemoteKey) extends Action final case class DoNothing(remoteKey: RemoteKey) extends Action
case class ToUpload(localFile: LocalFile) extends Action final case class ToUpload(localFile: LocalFile) extends Action
case class ToCopy(sourceKey: RemoteKey, final case class ToCopy(
hash: MD5Hash, sourceKey: RemoteKey,
targetKey: RemoteKey) extends Action hash: MD5Hash,
case class ToDelete(remoteKey: RemoteKey) extends Action targetKey: RemoteKey) extends Action
final case class ToDelete(remoteKey: RemoteKey) extends Action

View file

@ -2,15 +2,16 @@ package net.kemitix.s3thorp
import java.io.File import java.io.File
case class Config(bucket: Bucket = Bucket(""), final case class Config(
prefix: RemoteKey = RemoteKey(""), bucket: Bucket = Bucket(""),
verbose: Int = 1, prefix: RemoteKey = RemoteKey(""),
filters: Seq[Filter] = List(), verbose: Int = 1,
excludes: Seq[Exclude] = List(), filters: Seq[Filter] = List(),
multiPartThreshold: Long = 1024 * 1024 * 5, excludes: Seq[Exclude] = List(),
maxRetries: Int = 3, multiPartThreshold: Long = 1024 * 1024 * 5,
source: File maxRetries: Int = 3,
) { source: File
) {
require(source.isDirectory, s"Source must be a directory: $source") require(source.isDirectory, s"Source must be a directory: $source")
require(multiPartThreshold >= 1024 * 1024 * 5, s"Threshold for multi-part upload is 5Mb: '$multiPartThreshold'") require(multiPartThreshold >= 1024 * 1024 * 5, s"Threshold for multi-part upload is 5Mb: '$multiPartThreshold'")
} }

View file

@ -0,0 +1,6 @@
package net.kemitix.s3thorp
final case class Counters(uploaded: Int = 0,
deleted: Int = 0,
copied: Int = 0)

View file

@ -4,7 +4,7 @@ import java.nio.file.Path
import java.util.function.Predicate import java.util.function.Predicate
import java.util.regex.Pattern import java.util.regex.Pattern
case class Exclude(exclude: String = "!.*") { final case class Exclude(exclude: String = "!.*") {
lazy val predicate: Predicate[String] = Pattern.compile(exclude).asPredicate() lazy val predicate: Predicate[String] = Pattern.compile(exclude).asPredicate()

View file

@ -4,7 +4,7 @@ import java.nio.file.Path
import java.util.function.Predicate import java.util.function.Predicate
import java.util.regex.Pattern import java.util.regex.Pattern
case class Filter(filter: String = ".*") { final case class Filter(filter: String = ".*") {
lazy val predicate: Predicate[String] = Pattern.compile(filter).asPredicate.negate lazy val predicate: Predicate[String] = Pattern.compile(filter).asPredicate.negate

View file

@ -3,15 +3,17 @@ package net.kemitix.s3thorp
import java.io.File import java.io.File
import java.nio.file.Path import java.nio.file.Path
case class LocalFile(file: File, final case class LocalFile(
source: File, file: File,
keyGenerator: File => RemoteKey) source: File,
(implicit c: Config) keyGenerator: File => RemoteKey,
suppliedHash: Option[MD5Hash] = None)
(implicit c: Config)
extends MD5HashGenerator { extends MD5HashGenerator {
require(!file.isDirectory, s"LocalFile must not be a directory: $file") require(!file.isDirectory, s"LocalFile must not be a directory: $file")
private lazy val myhash = md5File(file) private lazy val myhash = suppliedHash.getOrElse(md5File(file))
def hash: MD5Hash = myhash def hash: MD5Hash = myhash

View file

@ -9,22 +9,25 @@ sealed trait S3Action {
} }
case class DoNothingS3Action(remoteKey: RemoteKey) extends S3Action { final case class DoNothingS3Action(remoteKey: RemoteKey) extends S3Action {
override val order: Int = 0 override val order: Int = 0
} }
case class CopyS3Action(remoteKey: RemoteKey) extends S3Action { final case class CopyS3Action(remoteKey: RemoteKey) extends S3Action {
override val order: Int = 1 override val order: Int = 1
} }
case class UploadS3Action(remoteKey: RemoteKey,
md5Hash: MD5Hash) extends S3Action { final case class UploadS3Action(
remoteKey: RemoteKey,
md5Hash: MD5Hash) extends S3Action {
override val order: Int = 2 override val order: Int = 2
} }
case class DeleteS3Action(remoteKey: RemoteKey) extends S3Action {
final case class DeleteS3Action(remoteKey: RemoteKey) extends S3Action {
override val order: Int = 3 override val order: Int = 3
} }
case class ErroredS3Action(remoteKey: RemoteKey, e: Throwable) extends S3Action { final case class ErroredS3Action(remoteKey: RemoteKey, e: Throwable) extends S3Action {
override val order: Int = 10 override val order: Int = 10
} }

View file

@ -1,6 +1,7 @@
package net.kemitix.s3thorp package net.kemitix.s3thorp
// For the LocalFile, the set of matching S3 objects with the same MD5Hash, and any S3 object with the same remote key // For the LocalFile, the set of matching S3 objects with the same MD5Hash, and any S3 object with the same remote key
case class S3MetaData(localFile: LocalFile, final case class S3MetaData(
matchByHash: Set[RemoteMetaData], localFile: LocalFile,
matchByKey: Option[RemoteMetaData]) matchByHash: Set[RemoteMetaData],
matchByKey: Option[RemoteMetaData])

View file

@ -35,8 +35,4 @@ trait SyncLogging extends Logging {
} }
} }
case class Counters(uploaded: Int = 0,
deleted: Int = 0,
copied: Int = 0)
} }

View file

@ -1,4 +1,5 @@
package net.kemitix.s3thorp.awssdk package net.kemitix.s3thorp.awssdk
case class CancellableMultiPartUpload(e: Throwable, final case class CancellableMultiPartUpload(
uploadId: String) extends Exception(e) e: Throwable,
uploadId: String) extends Exception(e)

View file

@ -5,7 +5,8 @@ import net.kemitix.s3thorp.{HashModified, KeyModified, MD5Hash, RemoteKey}
/** /**
* A list of objects and their MD5 hash values. * A list of objects and their MD5 hash values.
*/ */
case class S3ObjectsData(byHash: Map[MD5Hash, Set[KeyModified]], final case class S3ObjectsData(
byKey: Map[RemoteKey, HashModified]) { byHash: Map[MD5Hash, Set[KeyModified]],
byKey: Map[RemoteKey, HashModified]) {
} }

View file

@ -8,9 +8,11 @@ abstract class UnitTest extends FunSpec {
def aLocalFile(path: String, myHash: MD5Hash, source: File, fileToKey: File => RemoteKey) def aLocalFile(path: String, myHash: MD5Hash, source: File, fileToKey: File => RemoteKey)
(implicit c: Config): LocalFile = (implicit c: Config): LocalFile =
new LocalFile(source.toPath.resolve(path).toFile, source, fileToKey) { LocalFile(
override def hash: MD5Hash = myHash file = source.toPath.resolve(path).toFile,
} source = source,
keyGenerator = fileToKey,
suppliedHash = Some(myHash))
def aRemoteKey(prefix: RemoteKey, path: String): RemoteKey = def aRemoteKey(prefix: RemoteKey, path: String): RemoteKey =
RemoteKey(prefix.key + "/" + path) RemoteKey(prefix.key + "/" + path)