Commit f0d41f49 authored by Ali Saeed's avatar Ali Saeed

Update Leader class to make it's functions static

parent 23f07acd
package Registration_Discovery; package Registration_Discovery;
import io.grpc.stub.StreamObserver;
import org.lognet.springboot.grpc.GRpcService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
...@@ -16,87 +19,86 @@ import java.util.*; ...@@ -16,87 +19,86 @@ import java.util.*;
public class Leader { public class Leader {
private int PORT = 12345;
private ServiceRegistry serviceRegistry;
List<DocumentTermsInfo> documentTermsInfo;
static List<DocumentTermsInfo> documentTermsInfo;
private static final String FOLDER_PATH = "D:\\DS_Project\\resources"; private static final String FOLDER_PATH = "D:\\DS_Project\\resources";
private static final Logger logger = LoggerFactory.getLogger(Leader.class); private static final Logger logger = LoggerFactory.getLogger(Leader.class);
public Leader() {
public Leader(int port, ServiceRegistry serviceRegistry) {
this.PORT = port;
this.serviceRegistry = serviceRegistry;
} }
public void start() throws IOException, InterruptedException {
String searchQuery = ""; public static TreeMap<String,Double> start(String searchQuery1) throws IOException, InterruptedException {
while (!searchQuery.equals("exit")) {
documentTermsInfo = new ArrayList<>(); String searchQuery = searchQuery1;
documentTermsInfo = new ArrayList<>();
Scanner scanner = new Scanner(System.in); logger.info("Search Query: "+searchQuery);
logger.info("Enter your Search Query: "); if(!searchQuery.isEmpty()){
searchQuery = scanner.nextLine(); // Get the list of documents from the resources folder
logger.info("Search Query: "+searchQuery); List<Document> documents = getDocumentsFromResources();
if(!searchQuery.isEmpty()){ Map<Document,Double> documentsScores = new HashMap<>();
// Get the list of documents from the resources folder List<String> allServiceAddresses = ServiceRegistry.getAllServiceAddresses();
List<Document> documents = getDocumentsFromResources(); int numAddresses = allServiceAddresses.size();
Map<Document,Double> documentsScores = new HashMap<>(); int numDocuments = documents.size();
List<String> allServiceAddresses = serviceRegistry.getAllServiceAddresses(); if(numAddresses != 0){
int numAddresses = allServiceAddresses.size(); if(numDocuments != 0){
int numDocuments = documents.size(); int documentsPerAddress = numDocuments / numAddresses;
if(numAddresses != 0){ int remainingDocuments = numDocuments % numAddresses;
if(numDocuments != 0){ List<Thread> threads = new ArrayList<>();
int documentsPerAddress = numDocuments / numAddresses; int startIndex = 0;
int remainingDocuments = numDocuments % numAddresses; for (String address : allServiceAddresses) {
List<Thread> threads = new ArrayList<>(); int endIndex = startIndex + documentsPerAddress;
int startIndex = 0; if (remainingDocuments > 0) {
for (String address : allServiceAddresses) { endIndex++;
int endIndex = startIndex + documentsPerAddress; remainingDocuments--;
if (remainingDocuments > 0) {
endIndex++;
remainingDocuments--;
}
List<Document> subDocuments = new ArrayList<>();
for(int i= startIndex; i<endIndex;i++){
subDocuments.add(documents.get(i));
}
startIndex = endIndex;
DataForSearch dataForSearch = new DataForSearch(searchQuery, subDocuments);
Thread thread = new Thread(() -> {
try {
startSearchOnWorker(address, dataForSearch);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
logger.error("Interrupted while searching on workers: {}", e.getMessage());
}
});
thread.start();
threads.add(thread);
} }
for (Thread thread : threads) { List<Document> subDocuments = new ArrayList<>();
thread.join(); for(int i= startIndex; i<endIndex;i++){
subDocuments.add(documents.get(i));
} }
logger.info("\n\n"); startIndex = endIndex;
displayDocumentTermsInfos(documentTermsInfo);
Map<String,Double> IDFs = calculateIDF(documentTermsInfo, (double) numDocuments,searchQuery); DataForSearch dataForSearch = new DataForSearch(searchQuery, subDocuments);
documentsScores = calculateDocumentsScore(IDFs ,documentTermsInfo); Thread thread = new Thread(() -> {
TreeMap<Document, Double> sortedDocumentsScores = sortDocumentsScores(documentsScores); try {
displaySortedScores(sortedDocumentsScores); startSearchOnWorker(address, dataForSearch);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
logger.error("Interrupted while searching on workers: {}", e.getMessage());
}
});
thread.start();
threads.add(thread);
} }
else { for (Thread thread : threads) {
logger.info("No results !!!!"); thread.join();
} }
logger.info("\n\n");
displayDocumentTermsInfos(documentTermsInfo);
Map<String,Double> IDFs = calculateIDF(documentTermsInfo, (double) numDocuments,searchQuery);
documentsScores = calculateDocumentsScore(IDFs ,documentTermsInfo);
TreeMap<Document, Double> sortedDocumentsScores = sortDocumentsScores(documentsScores);
displaySortedScores(sortedDocumentsScores);
TreeMap<String, Double> sortedDocumentsScores1 = sortDocumentsScores1(documentsScores);
return sortedDocumentsScores1;
} }
else { else {
logger.info("Try again later !!!!"); logger.info("No results !!!!");
return null;
} }
}else {
logger.info("Please enter search query !!!");
} }
else {
logger.info("Try again later !!!!");
return null;
}
}else {
logger.info("Please enter search query !!!");
return null;
} }
} }
private Map<Document, Double> calculateDocumentsScore(Map<String, Double> idFs, List<DocumentTermsInfo> documentTermsInfo) {
private static Map<Document, Double> calculateDocumentsScore(Map<String, Double> idFs, List<DocumentTermsInfo> documentTermsInfo) {
Map<Document, Double> documentsScores = new TreeMap<>(); Map<Document, Double> documentsScores = new TreeMap<>();
...@@ -114,7 +116,7 @@ public class Leader { ...@@ -114,7 +116,7 @@ public class Leader {
return documentsScores; return documentsScores;
} }
private List<Document> getDocumentsFromResources() { private static List<Document> getDocumentsFromResources() {
List<Document> documents = new ArrayList<>(); List<Document> documents = new ArrayList<>();
try { try {
Path resourcesPath = Paths.get(FOLDER_PATH); Path resourcesPath = Paths.get(FOLDER_PATH);
...@@ -133,7 +135,7 @@ public class Leader { ...@@ -133,7 +135,7 @@ public class Leader {
return documents; return documents;
} }
private void displayDocumentTermsInfos(List<DocumentTermsInfo> documentTermsInfoList) { private static void displayDocumentTermsInfos(List<DocumentTermsInfo> documentTermsInfoList) {
System.out.println(); System.out.println();
System.out.println(); System.out.println();
for (DocumentTermsInfo documentTermsInfo : documentTermsInfoList) { for (DocumentTermsInfo documentTermsInfo : documentTermsInfoList) {
...@@ -150,11 +152,11 @@ public class Leader { ...@@ -150,11 +152,11 @@ public class Leader {
logger.info("................................................"); logger.info("................................................");
} }
} }
private void startSearchOnWorker(String workerAddress,DataForSearch dataForSearch) throws IOException, InterruptedException { private static void startSearchOnWorker(String workerAddress,DataForSearch dataForSearch) throws IOException, InterruptedException {
String[] addressParts = workerAddress.split(":"); String[] addressParts = workerAddress.split(":");
int port = Integer.parseInt(addressParts[1]); int port = Integer.parseInt(addressParts[1]);
String ipAddress = addressParts[0]; String ipAddress = addressParts[0];
logger.info(String.format("Searching on worker: %s ", ipAddress + ":" + port)); logger.info(String.format("Searching on worker: %s ", ipAddress + ":" + port));
try { try {
// Create a socket connection to the worker // Create a socket connection to the worker
Socket socket = new Socket(ipAddress, port); Socket socket = new Socket(ipAddress, port);
...@@ -179,7 +181,7 @@ public class Leader { ...@@ -179,7 +181,7 @@ public class Leader {
} }
} }
private Map<String,Double> calculateIDF(List<DocumentTermsInfo> documentTermsInfo,Double totalDocuments ,String searchQuery){ private static Map<String,Double> calculateIDF(List<DocumentTermsInfo> documentTermsInfo,Double totalDocuments ,String searchQuery){
Map<String,Double> IDFs = new HashMap<>(); Map<String,Double> IDFs = new HashMap<>();
Map<String, Double> wordDocumentCount = new HashMap<>(); Map<String, Double> wordDocumentCount = new HashMap<>();
String[] queryWords = searchQuery.split("\\s+"); String[] queryWords = searchQuery.split("\\s+");
...@@ -219,7 +221,7 @@ public class Leader { ...@@ -219,7 +221,7 @@ public class Leader {
return IDFs; return IDFs;
} }
private TreeMap<Document, Double> sortDocumentsScores(Map<Document, Double> documentsScores) { private static TreeMap<Document, Double> sortDocumentsScores(Map<Document, Double> documentsScores) {
Comparator<Document> comparator = new Comparator<Document>() { Comparator<Document> comparator = new Comparator<Document>() {
@Override @Override
public int compare(Document doc1, Document doc2) { public int compare(Document doc1, Document doc2) {
...@@ -238,7 +240,31 @@ public class Leader { ...@@ -238,7 +240,31 @@ public class Leader {
sortedMap.putAll(documentsScores); sortedMap.putAll(documentsScores);
return sortedMap; return sortedMap;
} }
private void displaySortedScores(TreeMap<Document, Double> sortedScores) { private static TreeMap<String, Double> sortDocumentsScores1(Map<Document, Double> documentsScores) {
Comparator<Document> comparator = new Comparator<Document>() {
@Override
public int compare(Document doc1, Document doc2) {
Double score1 = documentsScores.get(doc1);
Double score2 = documentsScores.get(doc2);
int scoreComparison = score2.compareTo(score1);
if (scoreComparison != 0) {
return scoreComparison;
} else {
return doc1.compareTo(doc2);
}
}
};
TreeMap<Document, Double> sortedMap = new TreeMap<>(comparator);
sortedMap.putAll(documentsScores);
TreeMap<String, Double> sortedMap1 = new TreeMap<>();
for (Map.Entry<Document, Double> entry : sortedMap.entrySet()) {
String documentName = entry.getKey().getName();
Double score = entry.getValue();
sortedMap1.put(documentName,score);
}
return sortedMap1;
}
private static void displaySortedScores(TreeMap<Document, Double> sortedScores) {
logger.info("Sorted Documents By Scores:"); logger.info("Sorted Documents By Scores:");
for (Map.Entry<Document, Double> entry : sortedScores.entrySet()) { for (Map.Entry<Document, Double> entry : sortedScores.entrySet()) {
Document document = entry.getKey(); Document document = entry.getKey();
......
Markdown is supported
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