initial commit with everything
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package cs448;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
String input_file_path = args[0];
|
||||
int backend = Integer.parseInt(args[1]);
|
||||
String id = args[2];
|
||||
project1 p = new project1();
|
||||
long startts;
|
||||
String[] column_names = {"nconst", "primaryName", "birthYear", "deathYear", "primaryProfession", "knownForTitles"};
|
||||
try {
|
||||
switch (backend){
|
||||
case 0:
|
||||
startts = System.nanoTime();
|
||||
System.out.println(p.select_file(input_file_path,id,column_names));
|
||||
System.out.println("Elapsed time in (ns): "+(System.nanoTime()-startts));
|
||||
break;
|
||||
case 1:
|
||||
startts = System.nanoTime();
|
||||
p.load_mainmemory(input_file_path);
|
||||
System.out.println("Load: elapsed time in (ns): "+(System.nanoTime()-startts));
|
||||
startts = System.nanoTime();
|
||||
System.out.println(p.select_mainmemory(id,column_names));
|
||||
System.out.println("Query: elapsed time in (ns): "+(System.nanoTime()-startts));
|
||||
break;
|
||||
case 2:
|
||||
startts = System.nanoTime();
|
||||
p.load_mapdb(input_file_path);
|
||||
System.out.println("Load: elapsed time in (ns): "+(System.nanoTime()-startts));
|
||||
startts = System.nanoTime();
|
||||
System.out.println(p.select_mapdb(id,column_names));
|
||||
System.out.println("Query: elapsed time in (ns): "+(System.nanoTime()-startts));
|
||||
break;
|
||||
default:
|
||||
System.out.println("Syntax: project1 path/to/datafile 0|1|2 id");
|
||||
}
|
||||
}
|
||||
catch(IOException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (OutOfMemoryError e){
|
||||
System.gc();
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally {
|
||||
p.db.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
package cs448;
|
||||
|
||||
import org.mapdb.DB;
|
||||
import org.mapdb.DBMaker;
|
||||
import org.mapdb.Serializer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Dictionary;
|
||||
import java.util.HashMap;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Scanner;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.io.File;
|
||||
import java.lang.StringBuilder;
|
||||
|
||||
/**
|
||||
* A class to compare different storage backends.
|
||||
*
|
||||
* @author Sean Rettig
|
||||
* @version January 22, 2024
|
||||
*/
|
||||
|
||||
|
||||
public class project1 {
|
||||
|
||||
/* Data structure for Main-memory back-end */
|
||||
HashMap<String,Dictionary<String, String>> mm_map = new HashMap<String,Dictionary<String, String>>();
|
||||
|
||||
/* Data structure for MapDB persistent storage*/
|
||||
String dbfile = "data.db";
|
||||
DB db = DBMaker.fileDB(dbfile).make();
|
||||
|
||||
// use this for MapDB storage
|
||||
ConcurrentMap mapdb = db.hashMap("map").make();
|
||||
|
||||
void load_mainmemory(String file_path) throws IOException {
|
||||
Scanner sc = new Scanner((new File(file_path)));
|
||||
String line = "";
|
||||
|
||||
while (sc.hasNextLine()) {
|
||||
line = sc.nextLine();
|
||||
String[] row = line.split("\t");
|
||||
|
||||
Dictionary<String, String> row_dict = new Hashtable<>();
|
||||
|
||||
row_dict.put("primaryName", row[1]);
|
||||
row_dict.put("birthYear", row[2]);
|
||||
row_dict.put("deathYear", row[3]);
|
||||
row_dict.put("primaryProfession", row[4]);
|
||||
row_dict.put("knownForTitles", row[5]);
|
||||
|
||||
mm_map.put(row[0], row_dict);
|
||||
}
|
||||
}
|
||||
|
||||
void load_mapdb(String file_path) throws IOException{
|
||||
Scanner sc = new Scanner((new File(file_path)));
|
||||
String line = "";
|
||||
|
||||
while (sc.hasNextLine()) {
|
||||
line = sc.nextLine();
|
||||
String[] row = line.split("\t");
|
||||
|
||||
Dictionary<String, String> row_dict = new Hashtable<>();
|
||||
|
||||
row_dict.put("primaryName", row[1]);
|
||||
row_dict.put("birthYear", row[2]);
|
||||
row_dict.put("deathYear", row[3]);
|
||||
row_dict.put("primaryProfession", row[4]);
|
||||
row_dict.put("knownForTitles", row[5]);
|
||||
|
||||
mapdb.put(row[0], row_dict);
|
||||
}
|
||||
}
|
||||
|
||||
String select_file(String file_path, String key, String[] column_names) throws IOException{
|
||||
Scanner sc = new Scanner(new File(file_path));
|
||||
String line = "";
|
||||
|
||||
while (sc.hasNextLine()) {
|
||||
line = sc.nextLine();
|
||||
if (line.contains(key)) {
|
||||
sc.close();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (line != "") {
|
||||
String[] row = line.split("\t");
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String col_name : column_names) {
|
||||
switch (col_name) {
|
||||
case "nconst":
|
||||
sb.append(row[0]);
|
||||
break;
|
||||
|
||||
case "primaryName":
|
||||
sb.append(row[1]);
|
||||
break;
|
||||
|
||||
case "birthYear":
|
||||
sb.append(row[2]);
|
||||
break;
|
||||
|
||||
case "deathYear":
|
||||
sb.append(row[3]);
|
||||
break;
|
||||
|
||||
case "primaryProfession":
|
||||
sb.append(row[4]);
|
||||
break;
|
||||
|
||||
case "knownForTitles":
|
||||
sb.append(row[5]);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
sb.append("\t");
|
||||
}
|
||||
line = sb.toString();
|
||||
|
||||
}
|
||||
return line;
|
||||
}
|
||||
|
||||
String select_mainmemory(String key, String[] column_names){
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Dictionary<String, String> data = mm_map.get(key);
|
||||
for (String col_name : column_names) {
|
||||
if (col_name == "nconst") {
|
||||
sb.append(key);
|
||||
} else {
|
||||
sb.append(data.get(col_name));
|
||||
}
|
||||
sb.append("\t");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
String select_mapdb(String key, String[] column_names){
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Dictionary<String, String> data = (Dictionary<String, String>) mapdb.get(key);
|
||||
for (String col_name : column_names) {
|
||||
if (col_name == "nconst") {
|
||||
sb.append(key);
|
||||
} else {
|
||||
sb.append(data.get(col_name));
|
||||
}
|
||||
sb.append("\t");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
int fastestLoad(){
|
||||
// 1: Main-memory Load
|
||||
// 2: MapDB Load
|
||||
int fastest = 1;
|
||||
return fastest;
|
||||
}
|
||||
|
||||
int fastestSelect(){
|
||||
// 0: File Select
|
||||
// 1: Main-memory Select
|
||||
// 2: MapDB Select
|
||||
int fastest = 1;
|
||||
return fastest;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user