forked from kincajou/hh-school
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add searching #1
Open
notbadcodecom
wants to merge
1
commit into
master
Choose a base branch
from
add-searching
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
parallelism/src/main/java/ru/hh/school/homework/search/PrintUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package ru.hh.school.homework.search; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class PrintUtil { | ||
|
||
public static void printWordsOfFiles(List<String> files, long start, long end) { | ||
System.out.println("Found " + files.size() + " file(s) in " + (end - start) + " ms:"); | ||
for (var file : files) { | ||
System.out.println(" " + file); | ||
} | ||
} | ||
|
||
public static void printWordsOfFiles(Map<String, Integer> folders, long start, long end) { | ||
System.out.println("Found " + folders.size() + " word(s) of each file in " + (end - start) + " ms:"); | ||
for (var word : folders.entrySet()) { | ||
System.out.println(" " + word.getKey() + " [" + word.getValue() + "]"); | ||
} | ||
} | ||
|
||
public static void printWordsOfFolders(Map<String, Integer> folders, long start, long end) { | ||
System.out.println("Found " + folders.size() + " word(s) of folder's file in " + (end - start) + " ms:"); | ||
for (var word : folders.entrySet()) { | ||
System.out.println(" " + word.getKey() + " [" + word.getValue() + "]"); | ||
} | ||
} | ||
|
||
public static void printStringList(List<String> list, long start, long end) { | ||
System.out.println("Search results found in Google in " + (end - start) + " ms:"); | ||
for (var line : list) { | ||
System.out.println(" " + line); | ||
} | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
parallelism/src/main/java/ru/hh/school/homework/search/file/FileSearcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package ru.hh.school.homework.search.file; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
public interface FileSearcher { | ||
|
||
List<String> parallelSearch(Path path, String fileName) throws IOException; | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
parallelism/src/main/java/ru/hh/school/homework/search/file/Folder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ru.hh.school.homework.search.file; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
public class Folder { | ||
|
||
private final List<Path> files; | ||
|
||
protected Folder(List<Path> files) { | ||
this.files = files; | ||
} | ||
|
||
protected List<Path> getFiles() { | ||
return files; | ||
} | ||
|
||
protected static Folder fromPath(Path path) throws IOException { | ||
try (Stream<Path> lines = Files.list(path)) { | ||
return new Folder(lines.toList()); | ||
} | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
parallelism/src/main/java/ru/hh/school/homework/search/file/ForkJionFilesSearcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package ru.hh.school.homework.search.file; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.concurrent.ForkJoinPool; | ||
|
||
public class ForkJionFilesSearcher implements FileSearcher { | ||
|
||
private final ForkJoinPool forkJoinPool = ForkJoinPool.commonPool(); | ||
|
||
@Override | ||
public List<String> parallelSearch(Path path, String fileName) throws IOException { | ||
return forkJoinPool.invoke(new SearchTask(path, fileName)); | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
parallelism/src/main/java/ru/hh/school/homework/search/file/SearchTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package ru.hh.school.homework.search.file; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.RecursiveTask; | ||
|
||
class SearchTask extends RecursiveTask<List<String>> { | ||
|
||
private final Folder folder; | ||
private final String fileName; | ||
|
||
protected SearchTask(Path path, String fileName) throws IOException { | ||
super(); | ||
this.folder = Folder.fromPath(path); | ||
this.fileName = fileName; | ||
} | ||
|
||
@Override | ||
protected List<String> compute() { | ||
List<String> foundFiles = new ArrayList<>(); | ||
List<RecursiveTask<List<String>>> forks = new ArrayList<>(); | ||
for (Path path : folder.getFiles()) { | ||
if (Files.isDirectory(path)) { | ||
SearchTask task = null; | ||
try { | ||
task = new SearchTask(path, fileName); | ||
forks.add(task); | ||
task.fork(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} else if (path.getFileName().toString().toUpperCase().contains(fileName.toUpperCase())) { | ||
foundFiles.add(path.toString()); | ||
} | ||
} | ||
for (var task : forks) { | ||
foundFiles.addAll(task.join()); | ||
} | ||
return foundFiles; | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
parallelism/src/main/java/ru/hh/school/homework/search/google/CompletableFutureSearcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package ru.hh.school.homework.search.google; | ||
|
||
import org.jsoup.Jsoup; | ||
import org.jsoup.nodes.Document; | ||
import org.jsoup.nodes.Element; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
public class CompletableFutureSearcher implements OnlineSearcher { | ||
|
||
private final String GOOGLE_URL = "https://www.google.com/search?q="; | ||
private final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " + | ||
"AppleWebKit/537.36 (KHTML, like Gecko) " + | ||
"Chrome/80.0.3987.116 Safari/537.36"; | ||
private ExecutorService threadPool = null; | ||
|
||
@Override | ||
public List<String> onlineSearch(Map<String, Integer> files) { | ||
threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); | ||
List<String> searchInfo = files.entrySet().parallelStream() | ||
.map(this::searchGoogleAsync) | ||
.map(CompletableFuture::join) | ||
.toList(); | ||
threadPool.close(); | ||
return searchInfo; | ||
} | ||
|
||
private long searchGoogle(String query) { | ||
Document document; | ||
try { | ||
document = Jsoup | ||
.connect(GOOGLE_URL + query) | ||
.userAgent(USER_AGENT) | ||
.get(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
Element divResultStats = document.select("div#result-stats").first(); | ||
String text = divResultStats.text(); | ||
String resultsPart = text.substring(0, text.indexOf('(')); | ||
return Long.parseLong(resultsPart.replaceAll("[^0-9]", "")); | ||
} | ||
|
||
private CompletableFuture<String> searchGoogleAsync(Map.Entry<String, Integer> file) { | ||
String query = file.getKey().split(" ")[2]; | ||
return CompletableFuture | ||
.supplyAsync(() -> getSearchResultInfo(file.getKey(), file.getValue(), searchGoogle(query)), threadPool); | ||
} | ||
|
||
private String getSearchResultInfo(String file, Integer wordCount, long searchCount) { | ||
return file + " [" + wordCount + "] - " + searchCount + " google search results"; | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
parallelism/src/main/java/ru/hh/school/homework/search/google/OnlineSearcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package ru.hh.school.homework.search.google; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public interface OnlineSearcher { | ||
|
||
List<String> onlineSearch(Map<String, Integer> files); | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
...elism/src/main/java/ru/hh/school/homework/search/word/ParallelStreamTopWordsSearcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package ru.hh.school.homework.search.word; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public class ParallelStreamTopWordsSearcher implements TopWordsSearcher { | ||
|
||
@Override | ||
public Map<String, Integer> inFilesTopWordsCounter(List<String> files, int limit) { | ||
return files.parallelStream() | ||
.flatMap(file -> fileReadLines(file).parallelStream() | ||
.map(line -> line.split("[^a-zA-Z]")) | ||
.flatMap(lines -> Arrays.stream(lines).parallel()) | ||
.filter(word -> word.length() > 3) | ||
.collect(Collectors.groupingByConcurrent(Function.identity(), Collectors.counting())) | ||
.entrySet() | ||
.parallelStream() | ||
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) | ||
.limit(limit) | ||
.collect(Collectors.toMap(v -> getName(file, v.getKey()), v -> v.getValue().intValue())) | ||
.entrySet() | ||
.parallelStream()) | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
} | ||
|
||
@Override | ||
public Map<String, Integer> inFoldersTopWordsCounter(List<String> folders, int limit) { | ||
return folders.parallelStream() | ||
.collect(Collectors.groupingByConcurrent(file -> Path.of(file).getParent(), Collectors.toList())) | ||
.entrySet() | ||
.parallelStream() | ||
.flatMap(files -> files.getValue().parallelStream() | ||
.flatMap(file -> fileReadLines(file).parallelStream()) | ||
.map(line -> line.split("[^a-zA-Z]")) | ||
.flatMap(lines -> Arrays.stream(lines).parallel()) | ||
.filter(word -> word.length() > 3) | ||
.collect(Collectors.groupingByConcurrent(Function.identity(), Collectors.counting())) | ||
.entrySet() | ||
.parallelStream() | ||
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) | ||
.limit(limit) | ||
.collect(Collectors.toMap(v -> getName(files.getKey(), v.getKey()), v -> v.getValue().intValue())) | ||
.entrySet() | ||
.parallelStream()) | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
} | ||
|
||
private List<String> fileReadLines(String path) { | ||
try (Stream<String> stringStream = Files.lines(Path.of(path))) { | ||
return stringStream.toList(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private String getName(String filePath, String word) { | ||
return filePath + " - " + word; | ||
} | ||
|
||
private String getName(Path filePath, String word) { | ||
return filePath.toString() + " - " + word; | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
parallelism/src/main/java/ru/hh/school/homework/search/word/TopWordsSearcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package ru.hh.school.homework.search.word; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public interface TopWordsSearcher { | ||
|
||
Map<String, Integer> inFilesTopWordsCounter(List<String> files, int limit); | ||
|
||
Map<String, Integer> inFoldersTopWordsCounter(List<String> files, int limit); | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jion -> Join
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Да, спасибо.