From: <th...@us...> - 2010-07-20 01:45:31
|
Revision: 1907 http://azsmrc.svn.sourceforge.net/azsmrc/?rev=1907&view=rev Author: the8472 Date: 2010-07-20 01:45:24 +0000 (Tue, 20 Jul 2010) Log Message: ----------- *sigh* comments didn't work due to "magical" aliases appearing in sql queries. so the hard way with custom dialects it is. Modified Paths: -------------- mldht/branches/indexer/DHTIndexer.jar mldht/branches/indexer/hibernate.cfg.xml mldht/branches/indexer/lbms/plugins/mldht/indexer/HibernateUtil.java mldht/branches/indexer/lbms/plugins/mldht/indexer/MetaDataGatherer.java Added Paths: ----------- mldht/branches/indexer/lbms/plugins/mldht/indexer/db/HSSQLIdxHintDialect.java mldht/branches/indexer/lbms/plugins/mldht/indexer/db/IndexHintInterceptor.java mldht/branches/indexer/lbms/plugins/mldht/indexer/db/MySQLIdxHintDialect.java Removed Paths: ------------- mldht/branches/indexer/lbms/plugins/mldht/indexer/db/MySQLIndexHintInterceptor.java Modified: mldht/branches/indexer/DHTIndexer.jar =================================================================== (Binary files differ) Modified: mldht/branches/indexer/hibernate.cfg.xml =================================================================== --- mldht/branches/indexer/hibernate.cfg.xml 2010-07-20 01:13:31 UTC (rev 1906) +++ mldht/branches/indexer/hibernate.cfg.xml 2010-07-20 01:45:24 UTC (rev 1907) @@ -15,7 +15,8 @@ <property name="connection.pool_size">5</property> <!-- SQL dialect - This tells the SQL grammer to be used --> - <property name="dialect">org.hibernate.dialect.HSQLDialect</property> + <!-- use custom dialects provided in the same package to make it work! --> + <property name="dialect">lbms.plugins.mldht.indexer.db.HSSQLIdxHintDialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> Modified: mldht/branches/indexer/lbms/plugins/mldht/indexer/HibernateUtil.java =================================================================== --- mldht/branches/indexer/lbms/plugins/mldht/indexer/HibernateUtil.java 2010-07-20 01:13:31 UTC (rev 1906) +++ mldht/branches/indexer/lbms/plugins/mldht/indexer/HibernateUtil.java 2010-07-20 01:45:24 UTC (rev 1907) @@ -16,7 +16,7 @@ */ package lbms.plugins.mldht.indexer; -import lbms.plugins.mldht.indexer.db.MySQLIndexHintInterceptor; +import lbms.plugins.mldht.indexer.db.IndexHintInterceptor; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; @@ -25,6 +25,8 @@ private static SessionFactory sessionFactory; + private static boolean isMySQL = false; + static { try @@ -32,11 +34,9 @@ //By default it will look for hibernate.cfg.xml in the class path AnnotationConfiguration config = new AnnotationConfiguration().configure(); config.addAnnotatedClass(TorrentDBEntry.class); + config.setInterceptor(new IndexHintInterceptor()); if(config.getProperty("hibernate.dialect").contains("MySQL")) - { - config.setProperty("hibernate.use_sql_comments", "true"); - config.setInterceptor(new MySQLIndexHintInterceptor()); - } + isMySQL = true; sessionFactory = config.buildSessionFactory(); } catch (Throwable ex) @@ -44,6 +44,10 @@ throw new ExceptionInInitializerError(ex); } } + + public static boolean isMySQL() { + return isMySQL; + } public static SessionFactory getSessionFactory() { return sessionFactory; Modified: mldht/branches/indexer/lbms/plugins/mldht/indexer/MetaDataGatherer.java =================================================================== --- mldht/branches/indexer/lbms/plugins/mldht/indexer/MetaDataGatherer.java 2010-07-20 01:13:31 UTC (rev 1906) +++ mldht/branches/indexer/lbms/plugins/mldht/indexer/MetaDataGatherer.java 2010-07-20 01:45:24 UTC (rev 1907) @@ -199,12 +199,12 @@ break; } - results = session.createQuery("from ihdata e where e.info_hash > ? and e.info_hash < ? and e.status = 0 and e.hitCount > 0 and e.lastFetchAttempt < ? order by e.info_hash") + results = session.createQuery("from ihdata e where useindex(e, primary) is true and e.info_hash > ? and e.info_hash < ? and e.status = 0 and e.hitCount > 0 and e.lastFetchAttempt < ? order by e.info_hash") .setString(0, startKey.toString(false)) .setString(1, ceilKey.toString(false)) .setLong(2, System.currentTimeMillis()/1000 - 3600) .setFirstResult(0) - .setComment("useIndex(e,primary)") + //.setComment("useIndex(e,primary)") .setMaxResults(wants) .setFetchSize(wants) .list(); Added: mldht/branches/indexer/lbms/plugins/mldht/indexer/db/HSSQLIdxHintDialect.java =================================================================== --- mldht/branches/indexer/lbms/plugins/mldht/indexer/db/HSSQLIdxHintDialect.java (rev 0) +++ mldht/branches/indexer/lbms/plugins/mldht/indexer/db/HSSQLIdxHintDialect.java 2010-07-20 01:45:24 UTC (rev 1907) @@ -0,0 +1,15 @@ +package lbms.plugins.mldht.indexer.db; + +import org.hibernate.Hibernate; +import org.hibernate.dialect.MySQL5Dialect; +import org.hibernate.dialect.MySQL5InnoDBDialect; +import org.hibernate.dialect.function.StandardSQLFunction; + +public class HSSQLIdxHintDialect extends org.hibernate.dialect.HSQLDialect +{ + public HSSQLIdxHintDialect() + { + super(); + registerFunction("useindex", new StandardSQLFunction("useindex", Hibernate.BOOLEAN)); + } +} \ No newline at end of file Property changes on: mldht/branches/indexer/lbms/plugins/mldht/indexer/db/HSSQLIdxHintDialect.java ___________________________________________________________________ Added: svn:mime-type + text/plain Copied: mldht/branches/indexer/lbms/plugins/mldht/indexer/db/IndexHintInterceptor.java (from rev 1903, mldht/branches/indexer/lbms/plugins/mldht/indexer/db/MySQLIndexHintInterceptor.java) =================================================================== --- mldht/branches/indexer/lbms/plugins/mldht/indexer/db/IndexHintInterceptor.java (rev 0) +++ mldht/branches/indexer/lbms/plugins/mldht/indexer/db/IndexHintInterceptor.java 2010-07-20 01:45:24 UTC (rev 1907) @@ -0,0 +1,81 @@ +package lbms.plugins.mldht.indexer.db; + + +import lbms.plugins.mldht.indexer.HibernateUtil; + +import org.hibernate.EmptyInterceptor; + +public class IndexHintInterceptor extends EmptyInterceptor +{ + private static final long serialVersionUID = 1L; + + @Override + public String onPrepareStatement(String sql) + { + while (true) + { + // check if function specified + int idx = sql.indexOf("useindex("); + if (idx < 0) { break; } + + // find end of function + int endidx = sql.indexOf(")=1", idx); + if (endidx < idx) + { + throwError("expected useindex(table, index) is true"); + } + + // get both parameters + String[] params = sql.substring(idx + 9, endidx).split(","); + if (params.length != 2) + { + throwError("expected 2 parameters to useindex(table, index)"); + } + + // trim parameters and verify + String tableId = params[0].trim(); + String indexHint = params[1].trim(); + if (tableId.length() == 0 || indexHint.length() == 0) + { + throwError("invalid parameters to useindex(table, index)"); + } + + // find actual table name minus id + int dotIdx = tableId.indexOf('.'); + if (dotIdx < 0) + { + throwError("invalid table name in useindex(table, index)"); + } + + // find table name within declaration + String tableName = tableId.substring(0, dotIdx); + int tableIdx = sql.indexOf(" " + tableName + " "); + if (tableIdx < 0) + { + throwError("unknown table name in useindex(table, index)"); + } + + // remove useindex function from predicate + String predicate = sql.substring(endidx + 3); + if (predicate.startsWith(" and ")) + { + predicate = predicate.substring(5); + } + + // inject use index after table declaration + sql = sql.substring(0, tableIdx + 2 + tableName.length()) + + (HibernateUtil.isMySQL() ? "use index (" + indexHint + ") " : "") + + sql.substring(tableIdx + 2 + tableName.length(), idx) + + predicate; + + } + + return sql; + } + + protected void throwError(String message) + { + throw new IllegalStateException(message); + } + +} \ No newline at end of file Property changes on: mldht/branches/indexer/lbms/plugins/mldht/indexer/db/IndexHintInterceptor.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: mldht/branches/indexer/lbms/plugins/mldht/indexer/db/MySQLIdxHintDialect.java =================================================================== --- mldht/branches/indexer/lbms/plugins/mldht/indexer/db/MySQLIdxHintDialect.java (rev 0) +++ mldht/branches/indexer/lbms/plugins/mldht/indexer/db/MySQLIdxHintDialect.java 2010-07-20 01:45:24 UTC (rev 1907) @@ -0,0 +1,15 @@ +package lbms.plugins.mldht.indexer.db; + +import org.hibernate.Hibernate; +import org.hibernate.dialect.MySQL5Dialect; +import org.hibernate.dialect.MySQL5InnoDBDialect; +import org.hibernate.dialect.function.StandardSQLFunction; + +public class MySQLIdxHintDialect extends org.hibernate.dialect.MySQL5InnoDBDialect +{ + public MySQLIdxHintDialect() + { + super(); + registerFunction("useindex", new StandardSQLFunction("useindex", Hibernate.BOOLEAN)); + } +} \ No newline at end of file Property changes on: mldht/branches/indexer/lbms/plugins/mldht/indexer/db/MySQLIdxHintDialect.java ___________________________________________________________________ Added: svn:mime-type + text/plain Deleted: mldht/branches/indexer/lbms/plugins/mldht/indexer/db/MySQLIndexHintInterceptor.java =================================================================== --- mldht/branches/indexer/lbms/plugins/mldht/indexer/db/MySQLIndexHintInterceptor.java 2010-07-20 01:13:31 UTC (rev 1906) +++ mldht/branches/indexer/lbms/plugins/mldht/indexer/db/MySQLIndexHintInterceptor.java 2010-07-20 01:45:24 UTC (rev 1907) @@ -1,34 +0,0 @@ -package lbms.plugins.mldht.indexer.db; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.hibernate.EmptyInterceptor; - -public class MySQLIndexHintInterceptor extends EmptyInterceptor -{ - private static final long serialVersionUID = 1L; - - @Override - public String onPrepareStatement(String sql) - { - Matcher m = Pattern.compile("^/\\* (.*) \\*/").matcher(sql); - if(!m.matches()) - return sql; - String comment = m.group(1); - - m = Pattern.compile("use index\\((.+),(.+)\\)").matcher(comment); - if(!m.matches()) - return sql; - - while(m.find()) - { - String table = m.group(1); - String index = m.group(2); - - sql = sql.replaceAll("(\\s"+table+"\\s)", "$1 use index("+index+")"); - } - - return sql; - } -} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |