1 43 package net.jforum.dao.generic; 44 45 import java.sql.Connection ; 46 import java.sql.PreparedStatement ; 47 import java.sql.ResultSet ; 48 import java.sql.Timestamp ; 49 import java.util.ArrayList ; 50 import java.util.List ; 51 52 import net.jforum.dao.DataAccessDriver; 53 import net.jforum.dao.SearchIndexerDAO; 54 import net.jforum.entities.Post; 55 import net.jforum.util.preferences.SystemGlobals; 56 57 import org.apache.log4j.Logger; 58 59 63 public class GenericScheduledSearchIndexerDAO implements net.jforum.dao.ScheduledSearchIndexerDAO 64 { 65 private static Logger logger = Logger.getLogger(GenericScheduledSearchIndexerDAO.class); 66 67 70 public int index(int step, Connection conn) throws Exception 71 { 72 Timestamp timestamp = new Timestamp (System.currentTimeMillis()); 73 74 PreparedStatement p = conn.prepareStatement(SystemGlobals.getSql("SearchModel.maxPostIdUntilNow")); 76 p.setTimestamp(1, timestamp); 77 78 ResultSet rs = p.executeQuery(); 79 int maxPostId = -1; 80 81 if (rs.next()) { 82 maxPostId = rs.getInt(1); 83 } 84 85 rs.close(); 87 p.close(); 88 89 int latestIndexedPostId = -1; 90 91 p = conn.prepareStatement(SystemGlobals.getSql("SearchModel.lastIndexedPostId")); 92 rs = p.executeQuery(); 93 94 if (rs.next()) { 95 latestIndexedPostId = rs.getInt(1); 96 } 97 98 rs.close(); 99 p.close(); 100 101 if (maxPostId == -1 || latestIndexedPostId == -1 || maxPostId <= latestIndexedPostId) { 102 logger.info("No posts found to index. Leaving..."); 103 return 0; 104 } 105 106 if (logger.isInfoEnabled()) { 107 logger.info("Going to index posts from " + latestIndexedPostId + " to " + maxPostId); 108 } 109 110 p = conn.prepareStatement(SystemGlobals.getSql("SearchModel.howManyToIndex")); 112 p.setTimestamp(1, timestamp); 113 p.setInt(2, latestIndexedPostId); 114 115 rs = p.executeQuery(); 116 rs.next(); 117 118 int total = rs.getInt(1); 119 120 rs.close(); 121 p.close(); 122 123 SearchIndexerDAO sim = DataAccessDriver.getInstance().newSearchIndexerDAO(); 125 sim.setConnection(conn); 126 127 int start = 0; 128 while (true) { 129 List posts = this.getPosts(start, step, latestIndexedPostId, maxPostId, conn); 130 131 if (posts.size() == 0) { 132 break; 133 } 134 135 logger.info("Indexing range [" + start + ", " + (start + step) + "] from a total of " + total); 136 137 sim.insertSearchWords(posts); 138 139 start += step; 140 } 141 142 return maxPostId; 143 } 144 145 public List getPosts(int start, int count, int minPostId, int maxPostId, Connection conn) throws Exception 146 { 147 List l = new ArrayList (); 148 149 PreparedStatement p = conn.prepareStatement(SystemGlobals.getSql("SearchModel.getPostsToIndex")); 150 p.setInt(1, minPostId); 151 p.setInt(2, maxPostId); 152 p.setInt(3, start); 153 p.setInt(4, count); 154 155 ResultSet rs = p.executeQuery(); 156 while (rs.next()) { 157 l.add(this.makePost(rs)); 158 } 159 160 rs.close(); 161 p.close(); 162 163 return l; 164 } 165 166 protected String getPostTextFromResultSet(ResultSet rs) throws Exception 167 { 168 return rs.getString("post_text"); 169 } 170 171 protected Post makePost(ResultSet rs) throws Exception 172 { 173 Post p = new Post(); 174 p.setId(rs.getInt("post_id")); 175 p.setText(this.getPostTextFromResultSet(rs)); 176 p.setSubject("post_subject"); 177 178 return p; 179 } 180 } 181 | Popular Tags |