Skip to content

Commit

Permalink
chore: Rename state types; ScanDirectoriesState, SizeCompareState, Co…
Browse files Browse the repository at this point in the history
…ntentCompareState
  • Loading branch information
guicamest committed Dec 23, 2023
1 parent 17546fc commit 149f018
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CoroutinesFindDuplicatesExecution(
filter: PathFilter,
) : this(
scope = scope,
initialState = ScanExecutionStateImpl.empty(initialDirectories = directories, filter = filter),
initialState = ScanDirectoriesStateImpl.empty(initialDirectories = directories, filter = filter),
)

override suspend fun duplicateEntries(): Collection<DuplicateGroup> = result.await()
Expand All @@ -53,28 +53,28 @@ class CoroutinesFindDuplicatesExecution(
init {
job =
scope.launch(CoroutineName("findDuplicatesExecution")) {
if (ScanExecutionState::class in stateHolder) {
if (ScanDirectoriesState::class in stateHolder) {
collectFiles(stateHolder)
stateHolder.updateStateToSizeFilter()
stateHolder.updateStateToSizeCompare()
}

delay(1L)
if (SizeFilterExecutionState::class in stateHolder) {
if (SizeCompareState::class in stateHolder) {
groupFilesBySize(stateHolder)
stateHolder.updateStateToContentFilter()
stateHolder.updateStateToContentCompare()
}

delay(1)
groupFilesByContent(stateHolder)
result.complete(
stateHolder.stateAs<ContentFilterExecutionState>().processedFiles.duplicateGroups(),
stateHolder.stateAs<ContentCompareState>().processedFiles.duplicateGroups(),
)
}
}

private fun collectFiles(stateHolder: FindProgressStateHolder) {
val (initialDirectories, filter) =
stateHolder.stateAs<ScanExecutionState>().run {
stateHolder.stateAs<ScanDirectoriesState>().run {
initialDirectories to filter
}
val visitor = ScanFileVisitor(filter, stateHolder)
Expand All @@ -84,7 +84,7 @@ class CoroutinesFindDuplicatesExecution(
}

private fun groupFilesBySize(stateHolder: FindProgressStateHolder) {
val filesToProcess = stateHolder.stateAs<SizeFilterExecutionState>().filesToProcess
val filesToProcess = stateHolder.stateAs<SizeCompareState>().filesToProcess
val destination = HashMap<Long, MutableList<PathWithAttributes>>(initialSize(filesToProcess))
val filteredFiles =
filesToProcess
Expand All @@ -95,7 +95,7 @@ class CoroutinesFindDuplicatesExecution(
.flatMap { it.value }
.toSet()

stateHolder.update { currentState: SizeFilterExecutionStateImpl ->
stateHolder.update { currentState: SizeCompareStateImpl ->
currentState.copy(processedFiles = filteredFiles, filesToProcess = emptySet())
}
}
Expand All @@ -104,7 +104,7 @@ class CoroutinesFindDuplicatesExecution(

private fun groupFilesByContent(stateHolder: FindProgressStateHolder) {
val filesWithHashes =
with(stateHolder.stateAs<ContentFilterExecutionState>()) {
with(stateHolder.stateAs<ContentCompareState>()) {
processedFiles + collectHashes(stateHolder, filesToProcess)
}

Expand All @@ -120,7 +120,7 @@ class CoroutinesFindDuplicatesExecution(
.flatMap { it.value }
.toSet()

stateHolder.update { currentState: ContentFilterExecutionStateImpl ->
stateHolder.update { currentState: ContentCompareStateImpl ->
currentState.copy(processedFiles = filteredFiles)
}
}
Expand All @@ -136,7 +136,7 @@ class CoroutinesFindDuplicatesExecution(
attributes = pwa.attributes,
contentHash = pwa.contentHash(),
)
stateHolder.update { currentState: ContentFilterExecutionStateImpl ->
stateHolder.update { currentState: ContentCompareStateImpl ->
currentState.copy(
filesToProcess = currentState.filesToProcess - pwa,
processedFiles = currentState.processedFiles + pathWithAttributesAndContent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ import java.nio.file.Path

sealed interface FindDuplicatesExecutionState

interface ScanExecutionState : FindDuplicatesExecutionState {
interface ScanDirectoriesState : FindDuplicatesExecutionState {
val initialDirectories: Collection<Path>
val filter: PathFilter
val visitedDirectories: Set<Path>
val filesToProcess: Set<PathWithAttributes>
}

data class ScanExecutionStateImpl(
data class ScanDirectoriesStateImpl(
override val initialDirectories: Collection<Path>,
override val filter: PathFilter,
override val visitedDirectories: Set<Path>,
override val filesToProcess: Set<PathWithAttributes>,
) : ScanExecutionState {
) : ScanDirectoriesState {
companion object {
internal fun empty(
initialDirectories: Collection<Path>,
filter: PathFilter,
): ScanExecutionState =
ScanExecutionStateImpl(
): ScanDirectoriesState =
ScanDirectoriesStateImpl(
initialDirectories = initialDirectories,
filter = filter,
visitedDirectories = emptySet(),
Expand All @@ -34,25 +34,25 @@ data class ScanExecutionStateImpl(
}
}

interface SizeFilterExecutionState : FindDuplicatesExecutionState {
interface SizeCompareState : FindDuplicatesExecutionState {
val filesToProcess: Set<PathWithAttributes>
val processedFiles: Set<PathWithAttributes>
}

data class SizeFilterExecutionStateImpl(
data class SizeCompareStateImpl(
override val filesToProcess: Set<PathWithAttributes>,
override val processedFiles: Set<PathWithAttributes>,
) : SizeFilterExecutionState
) : SizeCompareState

interface ContentFilterExecutionState : FindDuplicatesExecutionState {
interface ContentCompareState : FindDuplicatesExecutionState {
val filesToProcess: Set<PathWithAttributes>
val processedFiles: Set<PathWithAttributesAndContent>
}

data class ContentFilterExecutionStateImpl(
data class ContentCompareStateImpl(
override val filesToProcess: Set<PathWithAttributes>,
override val processedFiles: Set<PathWithAttributesAndContent>,
) : ContentFilterExecutionState
) : ContentCompareState

internal class FindProgressStateHolder(initial: FindDuplicatesExecutionState) {
private val _state = MutableStateFlow(initial)
Expand All @@ -74,17 +74,17 @@ internal class FindProgressStateHolder(initial: FindDuplicatesExecutionState) {
}
}

internal fun FindProgressStateHolder.updateStateToSizeFilter() =
update { currentState: ScanExecutionState ->
SizeFilterExecutionStateImpl(
internal fun FindProgressStateHolder.updateStateToSizeCompare() =
update { currentState: ScanDirectoriesState ->
SizeCompareStateImpl(
filesToProcess = currentState.filesToProcess,
processedFiles = emptySet(),
)
}

internal fun FindProgressStateHolder.updateStateToContentFilter() =
update { currentState: SizeFilterExecutionState ->
ContentFilterExecutionStateImpl(
internal fun FindProgressStateHolder.updateStateToContentCompare() =
update { currentState: SizeCompareState ->
ContentCompareStateImpl(
filesToProcess = currentState.processedFiles,
processedFiles = emptySet(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class ScanFileVisitor(
directory,
filter,
attributes,
stateHolder.stateAs<ScanExecutionState>().visitedDirectories,
stateHolder.stateAs<ScanDirectoriesState>().visitedDirectories,
)
) {
FileVisitResult.CONTINUE
Expand Down Expand Up @@ -56,15 +56,15 @@ internal class ScanFileVisitor(
}

private fun addFileToState(pathWithAttributes: PathWithAttributes) {
stateHolder.update { currentState: ScanExecutionStateImpl ->
stateHolder.update { currentState: ScanDirectoriesStateImpl ->
currentState.copy(
filesToProcess = currentState.filesToProcess + pathWithAttributes,
)
}
}

private fun addVisitedDirectoryToState(directory: Path) {
stateHolder.update { currentState: ScanExecutionStateImpl ->
stateHolder.update { currentState: ScanDirectoriesStateImpl ->
currentState.copy(
visitedDirectories = currentState.visitedDirectories.plusElement(directory),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,34 +57,34 @@ class StopResumeTest {
fun stopReturnsStateWhileScanning() =
runTest {
val state = findDuplicates(this, directory = searchDirectory).stop()
assertThat(state).isInstanceOf(ScanExecutionState::class.java)
assertThat(state).isInstanceOf(ScanDirectoriesState::class.java)
}

@ExperimentalCoroutinesApi
@Test
@DisplayName("return size filter state when `stop` is called")
@DisplayName("return size compare state when `stop` is called")
fun stopReturnsStateAfterScan() =
runTest {
val state =
findDuplicates(this, directory = searchDirectory).run {
advanceTimeBy(1)
stop()
}
assertThat(state).isInstanceOf(SizeFilterExecutionState::class.java)
assertThat(state).isInstanceOf(SizeCompareState::class.java)
}

@ExperimentalCoroutinesApi
@Test
@DisplayName("return content filter state when `stop` is called")
fun stopReturnsStateAfterSizeFilter() =
@DisplayName("return content compare state when `stop` is called")
fun stopReturnsStateAfterSizeCompare() =
runTest {
val state =
findDuplicates(this, directory = searchDirectory).run {
advanceTimeBy(1)
advanceTimeBy(1)
stop()
}
assertThat(state).isInstanceOf(ContentFilterExecutionState::class.java)
assertThat(state).isInstanceOf(ContentCompareState::class.java)
}

@ExperimentalCoroutinesApi
Expand All @@ -100,20 +100,20 @@ class StopResumeTest {
}
advanceUntilIdle()

assertThat(allStates.first()).isInstanceOf(ScanExecutionState::class.java)
assertThat(allStates.first()).isInstanceOf(ScanDirectoriesState::class.java)

val withoutScans = allStates.dropWhile { it is ScanExecutionState }
assertThat(withoutScans.first()).isInstanceOf(SizeFilterExecutionState::class.java)
val withoutScans = allStates.dropWhile { it is ScanDirectoriesState }
assertThat(withoutScans.first()).isInstanceOf(SizeCompareState::class.java)

val withoutSizeFilter = withoutScans.dropWhile { it is SizeFilterExecutionState }
assertThat(withoutSizeFilter).isNotEmpty.allMatch { it is ContentFilterExecutionState }
val withoutSizeStates = withoutScans.dropWhile { it is SizeCompareState }
assertThat(withoutSizeStates).isNotEmpty.allMatch { it is ContentCompareState }
}

@DisplayName("update scan state as it visits files/directories")
@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
inner class ScanStateTest {
private lateinit var scanStates: List<ScanExecutionState>
private lateinit var scanStates: List<ScanDirectoriesState>

@BeforeAll
@ExperimentalCoroutinesApi
Expand All @@ -126,7 +126,7 @@ class StopResumeTest {
state.toList(allStates)
}
advanceUntilIdle()
scanStates = allStates.takeWhile { it is ScanExecutionState }.map { it as ScanExecutionState }
scanStates = allStates.filterIsInstance<ScanDirectoriesState>()
}
}

Expand Down Expand Up @@ -183,7 +183,7 @@ class StopResumeTest {
@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
inner class SizeStateTest {
private lateinit var sizeFilterStates: List<SizeFilterExecutionState>
private lateinit var sizeCompareStates: List<SizeCompareState>

@BeforeAll
@ExperimentalCoroutinesApi
Expand All @@ -196,30 +196,30 @@ class StopResumeTest {
state.toList(allStates)
}
advanceUntilIdle()
sizeFilterStates =
sizeCompareStates =
allStates.dropWhile {
it is ScanExecutionState
}.takeWhile { it is SizeFilterExecutionState }.map { it as SizeFilterExecutionState }
it is ScanDirectoriesState
}.takeWhile { it is SizeCompareState }.map { it as SizeCompareState }
}
}

@Test
fun thereShouldBeTwoSizeStates() {
assertThat(sizeFilterStates).hasSize(2)
assertThat(sizeCompareStates).hasSize(2)
}

@Test
fun checkFirstState() {
assertThat(sizeFilterStates[0].filesToProcess.map { it.path })
assertThat(sizeCompareStates[0].filesToProcess.map { it.path })
.withPathComparator()
.containsExactlyInAnyOrderElementsOf(paths)
assertThat(sizeFilterStates[0].processedFiles).isEmpty()
assertThat(sizeCompareStates[0].processedFiles).isEmpty()
}

@Test
fun checkSecondState() {
assertThat(sizeFilterStates[1].filesToProcess).isEmpty()
assertThat(sizeFilterStates[1].processedFiles.map { it.path })
assertThat(sizeCompareStates[1].filesToProcess).isEmpty()
assertThat(sizeCompareStates[1].processedFiles.map { it.path })
.withPathComparator()
.containsExactlyInAnyOrderElementsOf(paths)
}
Expand All @@ -229,7 +229,7 @@ class StopResumeTest {
@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
inner class ContentStateTest {
private lateinit var contentStates: List<ContentFilterExecutionState>
private lateinit var contentStates: List<ContentCompareState>

@BeforeAll
@ExperimentalCoroutinesApi
Expand All @@ -244,10 +244,10 @@ class StopResumeTest {
advanceUntilIdle()
contentStates =
allStates.dropWhile {
it !is ContentFilterExecutionState
it !is ContentCompareState
}.takeWhile {
it is ContentFilterExecutionState
}.map { it as ContentFilterExecutionState }
it is ContentCompareState
}.map { it as ContentCompareState }
}
}

Expand Down Expand Up @@ -318,7 +318,7 @@ class StopResumeTest {
state.toList(allStates)
}
advanceUntilIdle()
val withoutScans = allStates.dropWhile { it is ScanExecutionState }
val withoutScans = allStates.dropWhile { it is ScanDirectoriesState }

val execution = resumeFindDuplicates(this, fromState = withoutScans.first())
val duplicateEntries = execution.duplicateEntries()
Expand All @@ -341,7 +341,7 @@ class StopResumeTest {
state.toList(allStates)
}
advanceUntilIdle()
val contentStates = allStates.dropWhile { it !is ContentFilterExecutionState }
val contentStates = allStates.dropWhile { it !is ContentCompareState }

val execution = resumeFindDuplicates(this, fromState = contentStates.first())
val duplicateEntries = execution.duplicateEntries()
Expand Down

0 comments on commit 149f018

Please sign in to comment.