Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Map;
- import java.util.Queue;
- import java.util.Scanner;
- import java.util.Vector;
- import java.io.*;
- import org.gibello.zql.ParseException;
- /* Code belongs to Kartik Kohli & Jigar Thakkar :P */
- public class DBSystem{
- final int CacheSize = 100;
- static HashMap<String, Integer> tableNameToInt = new HashMap<String, Integer>();
- Vector<Vector<Page>> listOfMaps = new Vector<Vector<Page>>();
- static Vector<Table> tables = new Vector<Table>();
- List<Page> pages = new ArrayList<Page>();
- int PAGESIZE,NUM_PAGES;
- String PATH_FOR_DATA;
- int numberOfTables;
- Queue<Page> cache = new LinkedList<Page>();
- public void readConfig(String configFilePath) {
- BufferedReader br = null;
- String str = "";
- //System.out.println(configFilePath);
- //String[] params = new String[1000];
- int ctr = 0;
- try {
- String currentLine;
- br = new BufferedReader(new FileReader(configFilePath));
- while((currentLine = br.readLine()) != null) {
- str += currentLine + "\n";
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- String[] params = str.split("\n");
- //System.out.println(params[0]);
- //Extracting Parameters from config file
- PAGESIZE = Integer.valueOf(params[0].split(" ")[1]);
- NUM_PAGES = Integer.valueOf(params[1].split(" ")[1]);
- PATH_FOR_DATA = params[2].split(" ")[1];
- //System.out.println(PAGESIZE + " " + NUM_PAGES + " " + PATH_FOR_DATA);
- int i=3,tableCount=0;
- //System.out.println(params.length+"");
- while(i<params.length) {
- //if i > parax
- tableNameToInt.put(params[++i], tableCount++);
- Table table = new Table(params[i]);
- System.out.println(params[i]);
- Vector<Page> vectorPage = new Vector<Page>();
- vectorPage.add(new Page(0, 0, 0, 0, params[i]));
- //System.out.println(table.toString());
- listOfMaps.add(vectorPage);
- int col = 0;
- while(params[++i].equals("END")==false) {
- //REad TABle attributes
- table.field.put(params[i].split(",")[0]+"",col++);
- String dataType = params[i].split(",")[1];
- if(dataType.indexOf("int")!=-1) {
- table.columns.add(0);
- }
- else {
- table.columns.add(1);
- }
- System.out.println(table.field.get(params[i]+""));
- }
- tables.add(table);
- i++;
- //System.out.println(i+"");
- }
- }
- public void populateDBInfo() {
- BufferedReader br = null;
- try {
- String currentLine;
- for(Map.Entry<String, Integer> entry: tableNameToInt.entrySet()) {
- br = new BufferedReader(new FileReader(PATH_FOR_DATA + entry.getKey() + ".csv"));
- //System.out.println(entry.getKey());
- Vector<Page> table = listOfMaps.get(entry.getValue());
- int start=0,end=0,currentSize=0,pageNumber=0;
- Page currentPage = table.get(pageNumber);
- while((currentLine = br.readLine())!=null) {
- //System.out.println(currentLine);
- //System.out.println("size:" + currentPage.currentSize);
- tables.get(entry.getValue()).records.add(currentLine);
- if(currentPage.currentSize + currentLine.length()>PAGESIZE) {
- //System.out.println(start + ":" + end);
- //if(pageNumber==0)
- //{
- // table.get(pageNumber).limit.end = end;
- //}
- //System.out.println("page Number" + pageNumber + "start" + start + "end"+ end);
- start=end + 1;
- table.add(new Page(++pageNumber,entry.getValue(),start, end, entry.getKey()));
- //System.out.println(table.toString());
- currentPage = table.get(pageNumber);
- currentPage.currentSize += currentLine.length();
- table.get(pageNumber).limit.end = ++end;
- }
- else {
- currentPage.currentSize += currentLine.length();
- //System.out.println(currentPage.currentSize+ "" + "size");
- table.get(pageNumber).limit.end = ++end;
- }
- }
- }
- } catch (FileNotFoundException e) {
- //e.printStackTrace();
- System.out.println("File Not Found:"+e.getLocalizedMessage()+"\n");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public String getRecord(String tableName, int recordId) {
- //System.out.println("Get" + recordId);
- int tableNumber = tableNameToInt.get(tableName);
- int first = 0;
- int last = listOfMaps.get(tableNumber).size() - 1;
- int middle = (first + last)/2;
- while( first <= last )
- {
- Page midPage = listOfMaps.get(tableNumber).get(middle);
- //System.out.println(midPage.limit.start + "End: " + midPage.limit.end);
- if ( midPage.limit.end < recordId )
- {
- /*if (recordId == 41)
- {
- System.out.println(midPage.limit.end+"" + " middle page");
- System.out.println(first +" " +last);
- }*/
- first = middle + 1;
- }
- else if ( midPage.limit.start <= recordId && midPage.limit.end >= recordId)
- {
- //System.out.println("DSADAS");
- //System.out.println("here: \n" + recordId + "\n");
- //System.out.println(search + " found at location " + (middle + 1) + ".");
- if (cache.contains(midPage))
- {
- //if (cache.get(cache.size() - 1) != midPage)
- //{
- //Cache already has the Page.
- cache.remove(midPage);
- cache.add(midPage);
- //}
- System.out.println("HIT");
- }
- else
- {
- //System.out.println("CacheSize "+ cache.size());
- if (cache.size() < NUM_PAGES)
- {
- //Cache not full. Adding a Page to cache;
- cache.add(midPage);
- System.out.println("New Page added to the LRU cache!\nPage Number: "+ midPage.pageNumber);
- }
- else
- {
- //Cache full and some page is replaced.
- Page replacedPage = cache.remove();
- cache.add(midPage);
- System.out.println("MISS " + replacedPage.pageNumber);
- }
- }
- break;
- }
- else
- last = middle - 1;
- middle = (first + last)/2;
- }
- return "record";
- }
- public void insertRecord(String tableName, String record) {
- System.out.println("CacheSize "+ cache.size());
- int tableNumber = tableNameToInt.get(tableName);
- int tablePages = listOfMaps.get(tableNumber).size();
- Page lastPage = listOfMaps.get(tableNumber).get(tablePages - 1);
- //System.out.println(lastPage.limit.end + "");
- lastPage.currentSize += record.length();
- if (lastPage.currentSize <= PAGESIZE)
- {
- lastPage.page = record;
- if (cache.contains(lastPage))
- {
- cache.remove(lastPage);
- System.out.println("HIT in insert");
- }
- else
- {
- //cache.add(lastPage);
- System.out.println("MISSED during insert");
- }
- cache.add(lastPage);
- lastPage.limit.end ++;
- }
- else
- {
- listOfMaps.get(tableNumber).add(new Page(lastPage.pageNumber + 1, lastPage.tableNumber, 0, 0, tableName));
- Page addedPage = listOfMaps.get(tableNumber).get(tablePages);
- addedPage.limit.end ++;
- addedPage.page = record;
- if (cache.size() < NUM_PAGES)
- {
- cache.add(addedPage);
- System.out.println("Newly created Page added to the cache!!");
- }
- else
- {
- Page replacedPage = cache.remove();
- cache.add(addedPage);
- System.out.println("Replaced a page in cache for the newly created page. Replaced Page: " + replacedPage.pageNumber);
- }
- }
- //System.out.println(lastPage.limit.end + "");
- }
- public void flushPages()
- {
- for (int i = 0;i < cache.size();i ++)
- {
- //System.out.println("here");
- Page poppedPage = cache.remove();
- //System.out.println(poppedPage.page);
- try {
- String finalPath = PATH_FOR_DATA + poppedPage.tableName + ".csv";
- //System.out.println(finalPath);
- //PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter(finalPath, true)));
- if (poppedPage.page.length()!=0) {
- PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter(finalPath, true)));
- output.println(poppedPage.page);
- output.close();
- }
- //output.write(poppedPage.page);
- cache.add(poppedPage);
- }
- catch(IOException e){
- System.out.println("Failed to FLush!!");
- }
- }
- }
- public static class Pair {
- public int start,end;
- public Pair(int a,int b) {
- this.start = a;
- this.end = b;
- }
- }
- public static class Table {
- public String tableName;
- public HashMap<String,Integer> field;
- Vector<Integer> columns;
- Vector<String> records;
- public Table(String tableName) {
- this.tableName = tableName;
- this.field = new HashMap<String,Integer>();
- this.records = new Vector<String>();
- this.columns = new Vector<Integer>();
- }
- }
- public static class Page {
- public String page;
- public Integer pageNumber;
- public Pair limit;
- public int currentSize,tableNumber;
- public String tableName;
- public Page(int id,int tableNumber,int start,int end, String tableName) {
- this.pageNumber = id;
- this.tableNumber = tableNumber;
- this.limit = new Pair(start, end);
- this.tableName = tableName;
- this.currentSize = 0;
- this.page = "";
- }
- }
- public static void main(String[] args) throws ParseException
- {
- DBSystem dbs = new DBSystem();
- dbs.readConfig("./config.txt");
- dbs.populateDBInfo();
- Table x = tables.get(0);
- for(int i=0;i<x.records.size();i++) {
- System.out.println(x.records.get(i));
- }
- /*
- dbs.getRecord("countries",0);
- dbs.getRecord("countries",1);
- dbs.getRecord("countries",2);
- dbs.insertRecord("countries", "record");
- dbs.getRecord("countries",2);
- dbs.getRecord("countries",2);
- dbs.getRecord("countries",3);
- dbs.getRecord("countries",41);
- dbs.getRecord("countries",9);
- dbs.getRecord("countries",39);
- dbs.getRecord("countries",28);
- dbs.getRecord("countries",1);
- dbs.getRecord("countries",30);
- dbs.getRecord("countries",38);
- dbs.getRecord("countries",39);
- dbs.getRecord("countries",31);
- dbs.insertRecord("countries", "record");
- dbs.getRecord("countries",42);
- dbs.getRecord("countries",28);
- dbs.flushPages();
- */
- System.out.println("Reading SQL from stdin");
- Scanner s = new Scanner(System.in);
- String input = s.nextLine();
- System.out.println("Scanned: " + input);
- DBQueries q = new DBQueries(tableNameToInt,dbs);
- q.queryType(input);
- //System.out.println(d.PageSize);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement