001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.gps.device.hibernate.indexer;
018:
019: import java.util.List;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023: import org.compass.core.CompassSession;
024: import org.compass.gps.device.hibernate.HibernateGpsDevice;
025: import org.compass.gps.device.hibernate.HibernateGpsDeviceException;
026: import org.compass.gps.device.hibernate.entities.EntityInformation;
027: import org.compass.gps.device.support.parallel.IndexEntity;
028: import org.hibernate.CacheMode;
029: import org.hibernate.Criteria;
030: import org.hibernate.Query;
031: import org.hibernate.Session;
032: import org.hibernate.Transaction;
033:
034: /**
035: * A Hibernate indexer uses Hibernate pagination using <code>setFirstResult</code>
036: * and <code>setMaxResults</code>.
037: *
038: * @author kimchy
039: */
040: public class PaginationHibernateIndexEntitiesIndexer implements
041: HibernateIndexEntitiesIndexer {
042:
043: private static final Log log = LogFactory
044: .getLog(PaginationHibernateIndexEntitiesIndexer.class);
045:
046: private HibernateGpsDevice device;
047:
048: public void setHibernateGpsDevice(HibernateGpsDevice device) {
049: this .device = device;
050: }
051:
052: public void performIndex(CompassSession session,
053: IndexEntity[] entities) {
054: for (IndexEntity entity : entities) {
055: EntityInformation entityInfo = (EntityInformation) entity;
056: int fetchCount = device.getFetchCount();
057: int current = 0;
058: while (true) {
059: if (!device.isRunning()) {
060: return;
061: }
062: Session hibernateSession = device.getSessionFactory()
063: .openSession();
064: hibernateSession.setCacheMode(CacheMode.IGNORE);
065: Transaction hibernateTransaction = null;
066: try {
067: hibernateTransaction = hibernateSession
068: .beginTransaction();
069: if (log.isDebugEnabled()) {
070: log
071: .debug(device
072: .buildMessage("Indexing entity ["
073: + entityInfo.getName()
074: + "] range ["
075: + current
076: + "-"
077: + (current + fetchCount)
078: + "]"));
079: }
080: List values;
081: Criteria criteria = entityInfo.getQueryProvider()
082: .createCriteria(hibernateSession,
083: entityInfo);
084: if (criteria != null) {
085: values = criteria.list();
086: } else {
087: Query query = entityInfo.getQueryProvider()
088: .createQuery(hibernateSession,
089: entityInfo).setFirstResult(
090: current).setMaxResults(
091: fetchCount);
092: values = query.list();
093: }
094: for (Object value : values) {
095: session.create(value);
096: }
097: session.evictAll();
098: hibernateTransaction.commit();
099: session.close();
100: current += fetchCount;
101: if (values.size() < fetchCount) {
102: break;
103: }
104: } catch (Exception e) {
105: log
106: .error(
107: device
108: .buildMessage("Failed to index the database"),
109: e);
110: if (hibernateTransaction != null) {
111: try {
112: hibernateTransaction.rollback();
113: } catch (Exception e1) {
114: log
115: .warn(
116: "Failed to rollback Hibernate",
117: e1);
118: }
119: }
120: if (!(e instanceof HibernateGpsDeviceException)) {
121: throw new HibernateGpsDeviceException(
122: device
123: .buildMessage("Failed to index the database"),
124: e);
125: }
126: throw (HibernateGpsDeviceException) e;
127: } finally {
128: hibernateSession.close();
129: }
130: }
131: }
132: }
133: }
|