Skip to content
Snippets Groups Projects
Commit bb649430 authored by Marius Dumitru Florea's avatar Marius Dumitru Florea
Browse files

XWIKI-13700: The document tree is still slow on MySql

* Fix query on PostgreSQL (PSQLException: ERROR: operator does not exist: boolean <> integer)
parent 8eb39969
No related branches found
No related tags found
No related merge requests found
......@@ -19,11 +19,16 @@
*/
package org.xwiki.index.tree.internal.nestedpages.query;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.SessionFactoryImplementor;
import org.xwiki.component.annotation.Component;
import com.xpn.xwiki.store.hibernate.HibernateSessionFactory;
/**
* Filters hidden pages. This filter works with the named <strong>native SQL</strong> queries declared in the
* {@code queries.hbm.xml} mapping file.
......@@ -36,17 +41,42 @@
@Singleton
public class HiddenPageFilter extends AbstractNestedPageFilter
{
@Inject
private HibernateSessionFactory sessionFactory;
@Override
protected String filterNestedPagesStatement(String statement)
{
// The constraint is different depending on whether we filter a native SQL query or an HQL query.
String constraint = statement.indexOf("XWS_REFERENCE") < 0 ? "hidden <> true " : "XWS_HIDDEN <> 1 ";
String constraint =
statement.indexOf("XWS_REFERENCE") < 0 ? "hidden <> true " : getHiddenConstraint("XWS_HIDDEN");
return insertWhereConstraint(statement, constraint);
}
@Override
protected String filterTerminalPagesStatement(String statement)
{
return statement + " and doc.XWD_HIDDEN <> 1 ";
return statement + " and " + getHiddenConstraint("doc.XWD_HIDDEN");
}
private String getHiddenConstraint(String field)
{
// I don't know exactly why "field = false" is not enough since the hidden field is marked as non-null so it
// should have only two values (true or false). I remember something related to Oracle but I don't know for
// sure. Let's keep this for now, but the downside of using the <> (not-equal) operator instead of = (equal) is
// that the database cannot use the index so the query is slower (which can be significant when we have
// thousands of documents and spaces in the database).
//
// Note that we can't use the boolean literal here because Oracle doesn't support it (ORA-00904: "TRUE": invalid
// identifier) and we can't use "1" (integer) also because PostgreSQL doesn't support automatic integer to
// boolean conversion (PSQLException: ERROR: operator does not exist: boolean <> integer). We're forced to get
// the boolean value from the SQL Dialect currently in use.
return String.format("%s <> %s ", field, toBooleanValueString(true));
}
private String toBooleanValueString(boolean value)
{
Dialect dialect = ((SessionFactoryImplementor) this.sessionFactory.getSessionFactory()).getDialect();
return dialect.toBooleanValueString(value);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment