/** * Return a folder object that's used to search a recursive * directory containing the complete works of William Shakespeare. */ public static Folder getRootFolder(String rootFolderName, boolean parallel) throws URISyntaxException, IOException { return Folder .fromDirectory(new File(ClassLoader .getSystemResource(rootFolderName) .toURI()).toPath(), parallel); }
/** * A function that accepts two partial results and merges them. * The combiner function may fold state from one argument into the * other and return that, or may return a new result container. * * @return a function which combines two partial results into a combined * result */ @Override public BinaryOperator<Folder> combiner() { return (Folder one, Folder another) -> { one.getSubFolders().addAll(another.getSubFolders()); one.getDocuments().addAll(another.getDocuments()); return one; }; }
/** * A function that folds a Path into the mutable result container. * * @return a function which folds a Path into a mutable result container */ @Override public BiConsumer<Folder, Path> accumulator() { Function<Path, Folder> getFolders = ExceptionUtils.rethrowFunction(file -> Folder.fromDirectory(file, mParallel)); Function<Path, Document> getDocuments = ExceptionUtils.rethrowFunction(Document::fromPath); return (Folder folder, Path entry) -> { if (Files.isDirectory(entry)) folder.getSubFolders().add(getFolders.apply(entry)); else folder.getDocuments().add(getDocuments.apply(entry)); }; }
/** * Search for phrases to find starting at the root folder. */ @Override protected List<List<SearchResults>> compute() { // Get a list of all folders in root directory. List<Folder> subFolders = mRootFolder .getSubFolders(); if (mParallelWorks) return computeParallel(subFolders); else return computeSequential(subFolders); }
/** * Search the docs in the work sequentially. */ private List<SearchResults> computeSequential(List<Document> docs) { // The list of results. List<SearchResults> results = new ArrayList<>(); // Iterate through all the documents in the work. for (Document doc : docs) // Convert each Document to a SearchPhrasesInDocTask and // compute it. results.addAll(new SearchPhrasesInDocTask(mWork.getName(), doc, mPhrasesToFind, mParallelSearching, mParallelPhrases).compute()); // Return the results. return results; } }
/** * A function that folds a Path into the mutable result container. * * @return a function which folds a Path into a mutable result container */ @Override public BiConsumer<Folder, Path> accumulator() { Function<Path, Folder> getFolders = ExceptionUtils.rethrowFunction(file -> Folder.fromDirectory(file, mParallel)); Function<Path, Document> getDocuments = ExceptionUtils.rethrowFunction(Document::fromPath); return (Folder folder, Path entry) -> { if (Files.isDirectory(entry)) folder.getSubFolders().add(getFolders.apply(entry)); else folder.getDocuments().add(getDocuments.apply(entry)); }; }
/** * @return The input data in the given @a rootFolderName as a list * of CharSequences. */ public static List<CharSequence> getInput(String rootFolderName, boolean parallel) throws IOException, URISyntaxException { // Return the input data in the given @a rootFolderName as a // list of CharSequences. return new GetInputListTask(getRootFolder(rootFolderName, parallel) // Get all the subfolders. .getSubFolders(), parallel).compute(); }
/** * Search for the docs in the work in parallel using the fork-join pool. */ private List<SearchResults> computeParallel(List<Document> docs) { // The list of fork-join tasks to run in parallel. List<ForkJoinTask<List<SearchResults>>> forks = new ArrayList<>(); // The list of results. List<SearchResults> results = new ArrayList<>(); // Iterate through all the documents in the work. for (Document doc : docs) // Convert each Document to a SearchPhrasesInDocTask and // fork it to run in parallel. forks.add(new SearchPhrasesInDocTask(mWork.getName(), doc, mPhrasesToFind, mParallelSearching, mParallelPhrases).fork()); // Join all the results. for (ForkJoinTask<List<SearchResults>> task : forks) results.addAll(task.join()); // Return the results. return results; }
/** * A function that accepts two partial results and merges them. * The combiner function may fold state from one argument into the * other and return that, or may return a new result container. * * @return a function which combines two partial results into a combined * result */ @Override public BinaryOperator<Folder> combiner() { return (Folder one, Folder another) -> { one.getSubFolders().addAll(another.getSubFolders()); one.getDocuments().addAll(another.getDocuments()); return one; }; }
/** * Return a folder object that's used to search a recursive * directory containing the complete works of William Shakespeare. */ private static Folder getRootFolder(String rootFolderName, boolean parallel) throws URISyntaxException, IOException { return Folder .fromDirectory(new File(ClassLoader .getSystemResource(rootFolderName) .toURI()).toPath(), parallel); }
/** * Constructor initializes the fields. */ GetWorkContentsTask(Folder work, boolean parallel) { // Get the folder with the text for all the docs. List<Folder> docsFolder = work .getSubFolders(); // Start by adding the intro document to the docsList. mDocsList = work.getDocuments(); // Then add all the act documents if there are any. if (docsFolder.size() > 0) mDocsList.addAll(docsFolder.get(0).getDocuments()); mParallel = parallel; // Don't let mMinSplitSize fall below 1! mMinSplitSize = max(mDocsList.size() / 2, 1); }
/** * Searches one work to find all occurrences of the given * phrases. */ @Override protected List<SearchResults> compute() { // Get the folder with the text for all the docs in the work. List<Folder> docsFolder = mWork .getSubFolders(); // Start by adding the intro document. List<Document> docs = mWork.getDocuments(); // Then add all the act documents if there are any. if (docsFolder.size() > 0) docs.addAll(docsFolder.get(0).getDocuments()); if (mParallelDocument) return computeParallel(docs); else return computeSequential(docs); }