112 lines
3.3 KiB
Java
112 lines
3.3 KiB
Java
package cs448;
|
|
|
|
import org.mapdb.DB;
|
|
import org.mapdb.DBMaker;
|
|
import org.mapdb.Serializer;
|
|
|
|
import java.io.IOException;
|
|
import java.io.BufferedReader;
|
|
import java.io.FileReader;
|
|
import java.util.HashMap;
|
|
import java.util.concurrent.ConcurrentMap;
|
|
|
|
|
|
public class project1 {
|
|
|
|
/* Data structure for Main-memory back-end */
|
|
HashMap<String,String> mm_map = new HashMap<String,String>();
|
|
|
|
/* Data structure for MapDB persistent storage*/
|
|
String dbfile = "data.db";
|
|
DB db = DBMaker.fileDB(dbfile).make();
|
|
|
|
// use this for MapDB storage
|
|
ConcurrentMap<String, String> mapdb = db.hashMap("map", Serializer.STRING, Serializer.STRING).make();
|
|
|
|
void load_mainmemory(String file_path) throws IOException {
|
|
try (BufferedReader tsvFile = new BufferedReader(new FileReader(file_path))) {
|
|
String line = "";
|
|
String value = "";
|
|
while ((line = tsvFile.readLine()) != null ) {
|
|
String[] parts = line.split("\t");
|
|
|
|
for (int i = 1; i < parts.length; i++) {
|
|
if (i == parts.length) {
|
|
value += parts[i];
|
|
} else {
|
|
value += parts[i] + "\t";
|
|
}
|
|
parts[i] = null;
|
|
}
|
|
|
|
mm_map.put(parts[0], value);
|
|
parts[0] = null;
|
|
line = null;
|
|
value = null;
|
|
value = "";
|
|
}
|
|
} catch (IOException e) {
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
void load_mapdb(String file_path) throws IOException{
|
|
try (BufferedReader tsvFile = new BufferedReader(new FileReader(file_path))) {
|
|
String line = "";
|
|
String value = "";
|
|
while ((line = tsvFile.readLine()) != null ) {
|
|
String[] parts = line.split("\t");
|
|
|
|
for (int i = 1; i < parts.length; i++) {
|
|
if (i == parts.length) {
|
|
value += parts[i];
|
|
} else {
|
|
value += parts[i] + "\t";
|
|
}
|
|
}
|
|
|
|
mapdb.put(parts[0], value);
|
|
value = "";
|
|
}
|
|
} catch (IOException e) {
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
String select_file(String key, String file_path) throws IOException{
|
|
try (BufferedReader tsvFile = new BufferedReader(new FileReader(file_path))) {
|
|
String line = "";
|
|
while ((line = tsvFile.readLine()) != null ) {
|
|
if(line.startsWith(key)) {
|
|
String[] parts = line.split("\t");
|
|
String result = "";
|
|
|
|
for (int i = 1; i < parts.length; i++) {
|
|
if (i == parts.length) {
|
|
result += parts[i];
|
|
} else {
|
|
result += parts[i] + "\t";
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
} catch (IOException e) {
|
|
throw e;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
String select_mainmemory(String key){
|
|
return mm_map.get(key);
|
|
}
|
|
|
|
String select_mapdb(String key){
|
|
String result = mapdb.get(key);
|
|
// db.close();
|
|
return result;
|
|
}
|
|
}
|