Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.21 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Queue;
  7. import java.util.Scanner;
  8. import java.util.Vector;
  9. import java.io.*;
  10.  
  11. import org.gibello.zql.ParseException;
  12.  
  13. /* Code belongs to Kartik Kohli & Jigar Thakkar :P */
  14.  
  15. public class DBSystem{
  16.  
  17. final int CacheSize = 100;
  18. static HashMap<String, Integer> tableNameToInt = new HashMap<String, Integer>();
  19. Vector<Vector<Page>> listOfMaps = new Vector<Vector<Page>>();
  20. static Vector<Table> tables = new Vector<Table>();
  21. List<Page> pages = new ArrayList<Page>();
  22. int PAGESIZE,NUM_PAGES;
  23. String PATH_FOR_DATA;
  24. int numberOfTables;
  25. Queue<Page> cache = new LinkedList<Page>();
  26.  
  27. public void readConfig(String configFilePath) {
  28. BufferedReader br = null;
  29. String str = "";
  30. //System.out.println(configFilePath);
  31. //String[] params = new String[1000];
  32. int ctr = 0;
  33. try {
  34. String currentLine;
  35. br = new BufferedReader(new FileReader(configFilePath));
  36. while((currentLine = br.readLine()) != null) {
  37. str += currentLine + "\n";
  38. }
  39. } catch (FileNotFoundException e) {
  40. e.printStackTrace();
  41. } catch (IOException e) {
  42. // TODO Auto-generated catch block
  43. e.printStackTrace();
  44. }
  45. String[] params = str.split("\n");
  46. //System.out.println(params[0]);
  47. //Extracting Parameters from config file
  48. PAGESIZE = Integer.valueOf(params[0].split(" ")[1]);
  49. NUM_PAGES = Integer.valueOf(params[1].split(" ")[1]);
  50. PATH_FOR_DATA = params[2].split(" ")[1];
  51. //System.out.println(PAGESIZE + " " + NUM_PAGES + " " + PATH_FOR_DATA);
  52. int i=3,tableCount=0;
  53. //System.out.println(params.length+"");
  54. while(i<params.length) {
  55. //if i > parax
  56. tableNameToInt.put(params[++i], tableCount++);
  57. Table table = new Table(params[i]);
  58. System.out.println(params[i]);
  59. Vector<Page> vectorPage = new Vector<Page>();
  60. vectorPage.add(new Page(0, 0, 0, 0, params[i]));
  61. //System.out.println(table.toString());
  62. listOfMaps.add(vectorPage);
  63. int col = 0;
  64. while(params[++i].equals("END")==false) {
  65. //REad TABle attributes
  66. table.field.put(params[i].split(",")[0]+"",col++);
  67. String dataType = params[i].split(",")[1];
  68. if(dataType.indexOf("int")!=-1) {
  69. table.columns.add(0);
  70. }
  71. else {
  72. table.columns.add(1);
  73. }
  74. System.out.println(table.field.get(params[i]+""));
  75. }
  76. tables.add(table);
  77. i++;
  78. //System.out.println(i+"");
  79. }
  80. }
  81.  
  82. public void populateDBInfo() {
  83. BufferedReader br = null;
  84. try {
  85. String currentLine;
  86. for(Map.Entry<String, Integer> entry: tableNameToInt.entrySet()) {
  87. br = new BufferedReader(new FileReader(PATH_FOR_DATA + entry.getKey() + ".csv"));
  88. //System.out.println(entry.getKey());
  89. Vector<Page> table = listOfMaps.get(entry.getValue());
  90. int start=0,end=0,currentSize=0,pageNumber=0;
  91. Page currentPage = table.get(pageNumber);
  92. while((currentLine = br.readLine())!=null) {
  93. //System.out.println(currentLine);
  94. //System.out.println("size:" + currentPage.currentSize);
  95. tables.get(entry.getValue()).records.add(currentLine);
  96. if(currentPage.currentSize + currentLine.length()>PAGESIZE) {
  97. //System.out.println(start + ":" + end);
  98. //if(pageNumber==0)
  99. //{
  100. // table.get(pageNumber).limit.end = end;
  101. //}
  102. //System.out.println("page Number" + pageNumber + "start" + start + "end"+ end);
  103. start=end + 1;
  104. table.add(new Page(++pageNumber,entry.getValue(),start, end, entry.getKey()));
  105. //System.out.println(table.toString());
  106. currentPage = table.get(pageNumber);
  107. currentPage.currentSize += currentLine.length();
  108. table.get(pageNumber).limit.end = ++end;
  109. }
  110. else {
  111. currentPage.currentSize += currentLine.length();
  112. //System.out.println(currentPage.currentSize+ "" + "size");
  113. table.get(pageNumber).limit.end = ++end;
  114. }
  115. }
  116. }
  117.  
  118. } catch (FileNotFoundException e) {
  119. //e.printStackTrace();
  120. System.out.println("File Not Found:"+e.getLocalizedMessage()+"\n");
  121. } catch (IOException e) {
  122. e.printStackTrace();
  123. }
  124. }
  125.  
  126. public String getRecord(String tableName, int recordId) {
  127. //System.out.println("Get" + recordId);
  128. int tableNumber = tableNameToInt.get(tableName);
  129. int first = 0;
  130. int last = listOfMaps.get(tableNumber).size() - 1;
  131. int middle = (first + last)/2;
  132. while( first <= last )
  133. {
  134. Page midPage = listOfMaps.get(tableNumber).get(middle);
  135. //System.out.println(midPage.limit.start + "End: " + midPage.limit.end);
  136. if ( midPage.limit.end < recordId )
  137. {
  138. /*if (recordId == 41)
  139. {
  140. System.out.println(midPage.limit.end+"" + " middle page");
  141. System.out.println(first +" " +last);
  142. }*/
  143. first = middle + 1;
  144. }
  145. else if ( midPage.limit.start <= recordId && midPage.limit.end >= recordId)
  146. {
  147. //System.out.println("DSADAS");
  148. //System.out.println("here: \n" + recordId + "\n");
  149. //System.out.println(search + " found at location " + (middle + 1) + ".");
  150. if (cache.contains(midPage))
  151. {
  152. //if (cache.get(cache.size() - 1) != midPage)
  153. //{
  154. //Cache already has the Page.
  155. cache.remove(midPage);
  156. cache.add(midPage);
  157. //}
  158. System.out.println("HIT");
  159. }
  160. else
  161. {
  162. //System.out.println("CacheSize "+ cache.size());
  163. if (cache.size() < NUM_PAGES)
  164. {
  165. //Cache not full. Adding a Page to cache;
  166. cache.add(midPage);
  167. System.out.println("New Page added to the LRU cache!\nPage Number: "+ midPage.pageNumber);
  168. }
  169. else
  170. {
  171. //Cache full and some page is replaced.
  172. Page replacedPage = cache.remove();
  173. cache.add(midPage);
  174. System.out.println("MISS " + replacedPage.pageNumber);
  175. }
  176. }
  177. break;
  178. }
  179. else
  180. last = middle - 1;
  181.  
  182. middle = (first + last)/2;
  183. }
  184. return "record";
  185. }
  186.  
  187. public void insertRecord(String tableName, String record) {
  188. System.out.println("CacheSize "+ cache.size());
  189. int tableNumber = tableNameToInt.get(tableName);
  190. int tablePages = listOfMaps.get(tableNumber).size();
  191. Page lastPage = listOfMaps.get(tableNumber).get(tablePages - 1);
  192. //System.out.println(lastPage.limit.end + "");
  193. lastPage.currentSize += record.length();
  194. if (lastPage.currentSize <= PAGESIZE)
  195. {
  196. lastPage.page = record;
  197. if (cache.contains(lastPage))
  198. {
  199. cache.remove(lastPage);
  200. System.out.println("HIT in insert");
  201. }
  202. else
  203. {
  204. //cache.add(lastPage);
  205. System.out.println("MISSED during insert");
  206. }
  207. cache.add(lastPage);
  208. lastPage.limit.end ++;
  209. }
  210. else
  211. {
  212. listOfMaps.get(tableNumber).add(new Page(lastPage.pageNumber + 1, lastPage.tableNumber, 0, 0, tableName));
  213. Page addedPage = listOfMaps.get(tableNumber).get(tablePages);
  214. addedPage.limit.end ++;
  215. addedPage.page = record;
  216. if (cache.size() < NUM_PAGES)
  217. {
  218. cache.add(addedPage);
  219. System.out.println("Newly created Page added to the cache!!");
  220. }
  221. else
  222. {
  223. Page replacedPage = cache.remove();
  224. cache.add(addedPage);
  225. System.out.println("Replaced a page in cache for the newly created page. Replaced Page: " + replacedPage.pageNumber);
  226. }
  227. }
  228. //System.out.println(lastPage.limit.end + "");
  229. }
  230. public void flushPages()
  231. {
  232.  
  233. for (int i = 0;i < cache.size();i ++)
  234. {
  235. //System.out.println("here");
  236. Page poppedPage = cache.remove();
  237. //System.out.println(poppedPage.page);
  238. try {
  239. String finalPath = PATH_FOR_DATA + poppedPage.tableName + ".csv";
  240. //System.out.println(finalPath);
  241. //PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter(finalPath, true)));
  242. if (poppedPage.page.length()!=0) {
  243. PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter(finalPath, true)));
  244. output.println(poppedPage.page);
  245. output.close();
  246. }
  247. //output.write(poppedPage.page);
  248. cache.add(poppedPage);
  249. }
  250. catch(IOException e){
  251. System.out.println("Failed to FLush!!");
  252. }
  253. }
  254. }
  255.  
  256. public static class Pair {
  257. public int start,end;
  258. public Pair(int a,int b) {
  259. this.start = a;
  260. this.end = b;
  261. }
  262. }
  263.  
  264. public static class Table {
  265. public String tableName;
  266. public HashMap<String,Integer> field;
  267. Vector<Integer> columns;
  268. Vector<String> records;
  269. public Table(String tableName) {
  270. this.tableName = tableName;
  271. this.field = new HashMap<String,Integer>();
  272. this.records = new Vector<String>();
  273. this.columns = new Vector<Integer>();
  274. }
  275. }
  276.  
  277. public static class Page {
  278. public String page;
  279. public Integer pageNumber;
  280. public Pair limit;
  281. public int currentSize,tableNumber;
  282. public String tableName;
  283. public Page(int id,int tableNumber,int start,int end, String tableName) {
  284. this.pageNumber = id;
  285. this.tableNumber = tableNumber;
  286. this.limit = new Pair(start, end);
  287. this.tableName = tableName;
  288. this.currentSize = 0;
  289. this.page = "";
  290. }
  291. }
  292.  
  293. public static void main(String[] args) throws ParseException
  294. {
  295.  
  296. DBSystem dbs = new DBSystem();
  297. dbs.readConfig("./config.txt");
  298. dbs.populateDBInfo();
  299. Table x = tables.get(0);
  300. for(int i=0;i<x.records.size();i++) {
  301. System.out.println(x.records.get(i));
  302. }
  303. /*
  304. dbs.getRecord("countries",0);
  305. dbs.getRecord("countries",1);
  306. dbs.getRecord("countries",2);
  307. dbs.insertRecord("countries", "record");
  308. dbs.getRecord("countries",2);
  309. dbs.getRecord("countries",2);
  310. dbs.getRecord("countries",3);
  311. dbs.getRecord("countries",41);
  312. dbs.getRecord("countries",9);
  313. dbs.getRecord("countries",39);
  314. dbs.getRecord("countries",28);
  315. dbs.getRecord("countries",1);
  316. dbs.getRecord("countries",30);
  317. dbs.getRecord("countries",38);
  318. dbs.getRecord("countries",39);
  319. dbs.getRecord("countries",31);
  320. dbs.insertRecord("countries", "record");
  321. dbs.getRecord("countries",42);
  322. dbs.getRecord("countries",28);
  323. dbs.flushPages();
  324. */
  325. System.out.println("Reading SQL from stdin");
  326. Scanner s = new Scanner(System.in);
  327. String input = s.nextLine();
  328. System.out.println("Scanned: " + input);
  329. DBQueries q = new DBQueries(tableNameToInt,dbs);
  330. q.queryType(input);
  331. //System.out.println(d.PageSize);
  332. }
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement