diff src/main/java/de/mpiwg/gazetteer/utils/DBService.java @ 10:5610250d021a default tip

SectionsIndex, we added a method to print the setting of the VM
author "jurzua <jurzua@mpiwg-berlin.mpg.de>"
date Thu, 19 Mar 2015 11:46:33 +0100
parents 5316e79f9a27
children
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/gazetteer/utils/DBService.java	Wed Mar 18 17:23:45 2015 +0100
+++ b/src/main/java/de/mpiwg/gazetteer/utils/DBService.java	Thu Mar 19 11:46:33 2015 +0100
@@ -8,7 +8,9 @@
 import java.sql.Statement;
 import java.util.ArrayList;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.log4j.Logger;
 import org.hibernate.Query;
@@ -31,6 +33,59 @@
 	static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
 	static final String DB_URL = "jdbc:mysql://localhost/";
 
+	private static DBService instance = null;
+	
+	public static DBService getInstance(){
+		if(instance == null){
+			instance = new DBService();
+		}
+		return instance;
+	}
+	
+	private Map<String, DBBook> bookMap;
+	
+	public DBBook getBook(String id){
+		return getBookMap().get(id);
+	}
+	
+	private Map<String, DBBook> getBookMap(){
+		if(bookMap == null){
+			try {
+				this.loadBookMap();	
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+		}
+		return this.bookMap;
+	}
+	
+	private void loadBookMap() throws SQLException{
+		Long start = System.currentTimeMillis();
+		Connection conn = null;
+		Statement stmt = null;
+		this.bookMap = new HashMap<String, DBBook>();
+		
+		try {
+			String query = "SELECT * FROM books";
+			logger.debug(query);
+			conn = getNewConnection();
+			stmt   = conn.createStatement();
+			ResultSet rs = stmt.executeQuery(query);
+			
+			while(rs.next()){
+				DBBook book = new DBBook(rs);
+				this.bookMap.put(book.getId(), book);
+			}
+		} catch (Exception e) {
+			e.printStackTrace();
+		}finally{
+			conn.close();
+		}
+		
+		long end = System.currentTimeMillis();
+		logger.debug("Time execution loading Book Map [ms]: " + (end - start));
+	}
+	
 	/**
 	 * This methods search from a list of terms. 
 	 * Every term is considered a subsequence of whole section name.
@@ -56,7 +111,6 @@
 			}
 			query += "name like '%" + term + "%' ";
 		}
-		//query += " limit 50";
 
 		try {
 			Class.forName(JDBC_DRIVER);
@@ -67,7 +121,8 @@
 			while (rs.next()) {
 				DBSection section = new DBSection(rs);
 				//DBBook book = getBook0(conn, section.getBookId());
-				//section.setBook(book);
+				DBBook book = getInstance().getBook(section.getBookId());
+				section.setBook(book);
 				list.add(section);
 			}
 			rs.close();
@@ -78,7 +133,7 @@
 		}
 		
 		long end = System.currentTimeMillis();
-		System.out.println("Time execution [ms]: " + (end - start));
+		logger.debug("Time execution serching [ms]: " + (end - start));
 		
 		return list;
 	}
@@ -173,7 +228,7 @@
 				String content = getContent(conn, bookId, startPage, endPage);
 				response.setText(content);
 				
-				DBBook book = getBook0(conn, bookId);
+				DBBook book = getBook0FromDB(conn, bookId);
 				response.setBook(book);
 				
 			}
@@ -225,15 +280,14 @@
 	}
 	
 	
-	public static DBBook getBook(String id) throws SQLException{
+	public static DBBook getBookFromDB(String id) throws SQLException{
 		Connection conn = null;
 		DBBook book = null;
 	
 		try {
-			Class.forName(JDBC_DRIVER);
-
+			//Class.forName(JDBC_DRIVER);
 			conn = getNewConnection();
-			book = getBook0(conn, id);
+			book = getBook0FromDB(conn, id);
 		} catch (Exception e) {
 			e.printStackTrace();
 		} finally {
@@ -242,7 +296,7 @@
 		return book;
 	}
 	
-	private static DBBook getBook0(Connection conn, String id) throws SQLException{
+	private static DBBook getBook0FromDB(Connection conn, String id) throws SQLException{
 		DBBook book = null;
 		
 		String query = "SELECT * FROM books WHERE id = '" + id + "'";