Commit a042b99c authored by Ihrig, Arvid Conrad (ari)'s avatar Ihrig, Arvid Conrad (ari)
Browse files

Integrated Pipeline: refactored TreeParser implementations, moved common logic to trait

parent 70582cb8
......@@ -2,6 +2,7 @@ package eu.nomad_lab.integrated_pipeline
import java.io.InputStream
import java.nio.file.Path
import java.util.NoSuchElementException
import eu.nomad_lab.integrated_pipeline.messages.{ FileParsingTask, FileTreeScanTask }
import eu.nomad_lab.parsers.{ CandidateParser, ParserCollection }
......@@ -12,6 +13,33 @@ import eu.nomad_lab.parsers.{ CandidateParser, ParserCollection }
*/
trait ParsingTaskGenerator extends Iterator[FileParsingTask] {
protected[this] def findNextParsingCandidate(): Option[FileParsingTask]
private var nextRequest: Option[FileParsingTask] = None
private var searchStarted: Boolean = false
def hasNext: Boolean = {
if (!searchStarted) {
nextRequest = findNextParsingCandidate()
searchStarted = true
}
nextRequest.isDefined
}
def next(): FileParsingTask = {
if (!searchStarted) {
nextRequest = findNextParsingCandidate()
searchStarted = true
}
val toReturn = nextRequest
nextRequest = findNextParsingCandidate()
if (toReturn.isDefined) {
toReturn.get
} else {
throw new NoSuchElementException("No more elements in this iterator")
}
}
def scanInputStream(parsers: ParserCollection, input: InputStream,
internalFilePath: String): Seq[CandidateParser] = {
val buf = Array.fill[Byte](8 * 1024)(0)
......
......@@ -18,21 +18,8 @@ class DirectoryTreeParsingTaskGenerator(request: FileTreeScanTask, parserCollect
require(request.treeType == TreeType.Directory, "file tree to process must be a directory")
private val basePath = request.treeBasePath
private val fileIterator = Files.walk(basePath).iterator().asScala.filter(Files.isRegularFile(_))
private var nextRequest: Option[FileParsingTask] = findNextParsingCandidate()
override def hasNext = nextRequest.isDefined
override def next(): FileParsingTask = {
val toReturn = nextRequest
nextRequest = findNextParsingCandidate()
if (toReturn.isDefined) {
toReturn.get
} else {
throw new NoSuchElementException("No more elements in this iterator")
}
}
@tailrec private def findNextParsingCandidate(): Option[FileParsingTask] = {
@tailrec final protected[this] override def findNextParsingCandidate(): Option[FileParsingTask] = {
if (fileIterator.hasNext) {
val file = fileIterator.next()
val internalFilePath = basePath.relativize(file)
......
......@@ -18,25 +18,12 @@ class ZipTreeParsingTaskGenerator(request: FileTreeScanTask, parserCollection: P
private val zipFile = new ZipFile(request.treeBasePath.toFile)
private val zipEntries: java.util.Enumeration[ZipArchiveEntry] = zipFile.getEntries
private var nextRequest: Option[FileParsingTask] = findNextParsingCandidate()
override def hasNext: Boolean = nextRequest.isDefined
override def finalize(): Unit = {
zipFile.close()
}
override def next(): FileParsingTask = {
val toReturn = nextRequest
nextRequest = findNextParsingCandidate()
if (toReturn.isDefined) {
toReturn.get
} else {
throw new NoSuchElementException("No more elements in this iterator")
}
}
@tailrec private def findNextParsingCandidate(): Option[FileParsingTask] = {
@tailrec protected[this] final override def findNextParsingCandidate(): Option[FileParsingTask] = {
if (zipEntries.hasMoreElements) {
val zipEntry: ZipArchiveEntry = zipEntries.nextElement()
val internalFilePath = Paths.get(zipEntry.getName)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment