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.jpa.indexer;
018:
019: import java.util.List;
020: import javax.persistence.EntityManager;
021: import javax.persistence.Query;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.compass.core.CompassSession;
026: import org.compass.gps.device.jpa.EntityManagerWrapper;
027: import org.compass.gps.device.jpa.JpaGpsDevice;
028: import org.compass.gps.device.jpa.JpaGpsDeviceException;
029: import org.compass.gps.device.jpa.entities.EntityInformation;
030: import org.compass.gps.device.support.parallel.IndexEntity;
031:
032: /**
033: * The default JPA indexer. Uses plain JPA API to do pagination (<code>setFirstResult</code>
034: * and <code>setMaxResults</code>).
035: *
036: * @author kimchy
037: */
038: public class DefaultJpaIndexEntitiesIndexer implements
039: JpaIndexEntitiesIndexer {
040:
041: private static final Log log = LogFactory
042: .getLog(DefaultJpaIndexEntitiesIndexer.class);
043:
044: private JpaGpsDevice jpaGpsDevice;
045:
046: public void setJpaGpsDevice(JpaGpsDevice jpaGpsDevice) {
047: this .jpaGpsDevice = jpaGpsDevice;
048: }
049:
050: public void performIndex(CompassSession session,
051: IndexEntity[] entities) {
052: for (IndexEntity indexEntity : entities) {
053: EntityInformation entityInformation = (EntityInformation) indexEntity;
054: if (jpaGpsDevice.isFilteredForIndex(entityInformation
055: .getName())) {
056: continue;
057: }
058: int fetchCount = jpaGpsDevice.getFetchCount();
059: int current = 0;
060: while (true) {
061: if (!jpaGpsDevice.isRunning()) {
062: return;
063: }
064: EntityManagerWrapper wrapper = jpaGpsDevice
065: .getEntityManagerWrapper().newInstance();
066: try {
067: wrapper.open();
068: EntityManager entityManager = wrapper
069: .getEntityManager();
070: if (log.isDebugEnabled()) {
071: log.debug(jpaGpsDevice
072: .buildMessage("Indexing entities ["
073: + entityInformation.getName()
074: + "] range ["
075: + current
076: + "-"
077: + (current + fetchCount)
078: + "] using query ["
079: + entityInformation
080: .getQueryProvider()
081: + "]"));
082: }
083: Query query = entityInformation.getQueryProvider()
084: .createQuery(entityManager,
085: entityInformation);
086: query.setFirstResult(current);
087: query.setMaxResults(fetchCount);
088: List results = query.getResultList();
089: for (Object result : results) {
090: session.create(result);
091: }
092: session.evictAll();
093: entityManager.clear();
094: wrapper.close();
095: if (results.size() < fetchCount) {
096: break;
097: }
098: current += fetchCount;
099: } catch (Exception e) {
100: log
101: .error(
102: jpaGpsDevice
103: .buildMessage("Failed to index the database"),
104: e);
105: wrapper.closeOnError();
106: if (!(e instanceof JpaGpsDeviceException)) {
107: throw new JpaGpsDeviceException(
108: jpaGpsDevice
109: .buildMessage("Failed to index the database"),
110: e);
111: }
112: throw (JpaGpsDeviceException) e;
113: }
114: }
115: }
116: }
117: }
|