PageRenderTime 57ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/src/edu/psu/chemxseer/structure/subsearch/Impl/GraphDatabase_OnDisk.java

https://github.com/Santa827/Chemxseer_subSearch
Java | 198 lines | 150 code | 18 blank | 30 comment | 9 complexity | 17b8591f729c28c69420d04ed53de2d4 MD5 | raw file
  1. package edu.psu.chemxseer.structure.subsearch.Impl;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.FileReader;
  9. import java.io.IOException;
  10. import java.io.OutputStream;
  11. import java.io.RandomAccessFile;
  12. import java.text.ParseException;
  13. import de.parmol.graph.Graph;
  14. import de.parmol.parsers.GraphParser;
  15. import edu.psu.chemxseer.structure.preprocess.MyFactory;
  16. import edu.psu.chemxseer.structure.subsearch.Interfaces.GraphDatabase;
  17. import edu.psu.chemxseer.structure.util.MemoryConsumptionCal;
  18. import edu.psu.chemxseer.structure.util.NumericConverter;
  19. /**
  20. * The default saving formate of graphs are with Smiles
  21. * This is self implementation of the graph dataset if self
  22. * It includes:
  23. * 1. One database file
  24. * 2. One database description (meta data) file
  25. * 3. Operation on those files: including reading and writing
  26. * @author dayuyuan
  27. *
  28. */
  29. public class GraphDatabase_OnDisk extends GraphDatabase{
  30. protected RandomAccessFile databaseFile;
  31. protected String databaseFileName;
  32. protected long[] index;
  33. public GraphDatabase_OnDisk(String dbFileName, GraphParser gParser){
  34. super(gParser);
  35. this.databaseFileName = dbFileName;
  36. try {
  37. this.databaseFile = new RandomAccessFile(dbFileName,"r");
  38. } catch (FileNotFoundException e) {
  39. e.printStackTrace();
  40. }
  41. File indexFile = new File(databaseFileName + "_index");
  42. if(!indexFile.exists())
  43. try {
  44. // create and store the index file
  45. createIndexFile();
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. }
  49. else
  50. try {
  51. loadIndexFile();
  52. } catch (IOException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. public GraphDatabase_OnDisk() {
  57. super(null);
  58. }
  59. public void finalize(){
  60. try {
  61. if(this.databaseFile!=null)
  62. this.databaseFile.close();
  63. } catch (IOException e) {
  64. e.printStackTrace();
  65. }
  66. }
  67. private boolean createIndexFile() throws IOException{
  68. OutputStream indexWriter = new FileOutputStream(databaseFileName + "_index", false);
  69. // First write how many graphs exists in this graph database
  70. BufferedReader dbMetaReader = new BufferedReader(new FileReader(databaseFileName + "_Meta"));
  71. dbMetaReader.readLine();
  72. String FirstLine = dbMetaReader.readLine();
  73. int totalNum = Integer.parseInt(FirstLine.split(":")[1]);
  74. this.index = new long[totalNum];
  75. indexWriter.write(NumericConverter.int2byte(totalNum));
  76. long shift = this.databaseFile.getFilePointer();
  77. this.databaseFile.seek(0);
  78. String aLine = this.databaseFile.readLine();
  79. int i = 0;
  80. while(aLine!=null){
  81. this.index[i] = shift;
  82. indexWriter.write(NumericConverter.long2byte(shift)); // write a long
  83. i++;
  84. shift = this.databaseFile.getFilePointer();
  85. aLine = this.databaseFile.readLine();
  86. }
  87. indexWriter.flush();
  88. indexWriter.close();
  89. return true;
  90. }
  91. /**
  92. * Given the graph database is already indexed, load the index into memory
  93. * This part is not counted as memory occupation
  94. * @return
  95. * @throws IOException
  96. */
  97. private boolean loadIndexFile() throws IOException{
  98. BufferedInputStream indexStream = new BufferedInputStream(new FileInputStream(databaseFileName + "_index"));
  99. byte[] tempByte = new byte[4];
  100. indexStream.read(tempByte);
  101. // Have to find more smarter ways of changing a byte array into a long numeric value
  102. int tempNum = NumericConverter.byte2int(tempByte);
  103. // create the in-memory index for the graph database
  104. this.index = new long[tempNum];
  105. // Initialize the in-memory index for the graph database
  106. byte[] tempByte2 = new byte[8];
  107. for(int i = 0; i< tempNum; i++){
  108. indexStream.read(tempByte2);
  109. index[i] = NumericConverter.byte2long(tempByte2);
  110. }
  111. return true;
  112. }
  113. public String findGraphString(int id){
  114. if(id < 0 || id > this.index.length){
  115. System.out.println("Exception, GraphDatabase: findGraph, index out of boundary");
  116. return null;
  117. }
  118. else{
  119. long shift = this.index[id];
  120. String graphSmiles = null;
  121. try {
  122. this.databaseFile.seek(shift);
  123. graphSmiles = this.databaseFile.readLine();
  124. } catch (IOException e) {
  125. e.printStackTrace();
  126. }
  127. if(graphSmiles == null)
  128. System.out.println("Exception, GraphDatabase: findGraph, Did not find the shift");
  129. String[] tokens = graphSmiles.split(" => ");
  130. if(tokens.length <=1)
  131. System.out.println("Lala");
  132. return tokens[1];
  133. }
  134. }
  135. public int getTotalNum(){
  136. return this.index.length;
  137. }
  138. public String getDBFileName(){
  139. return this.databaseFileName;
  140. }
  141. /**
  142. * Return the approximate memory consumption of this graph database
  143. * @return
  144. */
  145. public long getMemoryConsumption() {
  146. // Calculate the size of the databaseFileName:
  147. // Not the string's size, but the character size
  148. int stringSize = databaseFileName.length()<< 1;
  149. // Calculate the size of the RandomFile
  150. //MemoryConsumptionCal.runGC();
  151. long before = MemoryConsumptionCal.usedMemory();
  152. RandomAccessFile theFileCopy = null;
  153. try {
  154. theFileCopy = new RandomAccessFile(databaseFileName,"r");
  155. } catch (FileNotFoundException e) {
  156. e.printStackTrace();
  157. }
  158. long after = MemoryConsumptionCal.usedMemory();
  159. try {
  160. theFileCopy.close();
  161. } catch (IOException e) {
  162. e.printStackTrace();
  163. }
  164. // Calculate the index size
  165. long indexSize = index.length << 3;
  166. return indexSize + (after-before) + stringSize ;
  167. }
  168. public void getAverageSize(){
  169. float nodeNum = 0;
  170. float edgeNum = 0;
  171. for(int i = 0; i< this.getTotalNum(); i++){
  172. Graph g = this.findGraph(i);
  173. nodeNum += g.getNodeCount();
  174. edgeNum += g.getEdgeCount();
  175. }
  176. float totalNum = this.getTotalNum();
  177. System.out.println("EdgeNum " + edgeNum/totalNum);
  178. System.out.println("NodeNum " + nodeNum/totalNum);
  179. }
  180. }