Example usage for org.hibernate ScrollableResults get

List of usage examples for org.hibernate ScrollableResults get

Introduction

In this page you can find the example usage for org.hibernate ScrollableResults get.

Prototype

Object get(int i);

Source Link

Document

Get the ith object in the current row of results, without initializing any other results in the row.

Usage

From source file:com.eucalyptus.cluster.callback.reporting.FullTableScanAbsoluteMetricConverter.java

License:Open Source License

protected static List<AbsoluteMetricQueueItem> dealWithAbsoluteMetrics(
        Iterable<AbsoluteMetricQueueItem> dataBatch) {
    List<AbsoluteMetricQueueItem> regularMetrics = Lists.newArrayList();
    List<SimpleAbsoluteMetricHistory> absoluteMetricsToInsert = Lists.newArrayList();
    SortedAbsoluteMetrics sortedAbsoluteMetrics = sortAbsoluteMetrics(dataBatch);
    regularMetrics.addAll(sortedAbsoluteMetrics.getRegularMetrics());
    AbsoluteMetricMap absoluteMetricMap = sortedAbsoluteMetrics.getAbsoluteMetricMap();
    try (final TransactionResource db = Entities.transactionFor(AbsoluteMetricHistory.class)) {
        int count = 0;
        Criteria criteria = Entities.createCriteria(AbsoluteMetricHistory.class);
        ScrollableResults absoluteMetrics = criteria.setCacheMode(CacheMode.IGNORE)
                .scroll(ScrollMode.FORWARD_ONLY);
        while (absoluteMetrics.next()) {
            AbsoluteMetricHistory absoluteMetricHistory = (AbsoluteMetricHistory) absoluteMetrics.get(0);
            if (absoluteMetricMap.containsKey(absoluteMetricHistory.getNamespace(),
                    absoluteMetricHistory.getMetricName(), absoluteMetricHistory.getDimensionName(),
                    absoluteMetricHistory.getDimensionValue())) {
                MetricsAndOtherFields metricsAndOtherFields = absoluteMetricMap.getMetricsAndOtherFields(
                        absoluteMetricHistory.getNamespace(), absoluteMetricHistory.getMetricName(),
                        absoluteMetricHistory.getDimensionName(), absoluteMetricHistory.getDimensionValue());
                Map<TimestampAndMetricValue, MetricDatum> metricDatumMap = metricsAndOtherFields
                        .getMetricDatumMap();
                SequentialMetrics sequentialMetrics = calculateSequentialMetrics(absoluteMetricHistory,
                        metricDatumMap, metricsAndOtherFields.getAccountId(),
                        metricsAndOtherFields.getRelativeMetricName());
                absoluteMetricMap.removeEntries(absoluteMetricHistory.getNamespace(),
                        absoluteMetricHistory.getMetricName(), absoluteMetricHistory.getDimensionName(),
                        absoluteMetricHistory.getDimensionValue());
                for (AbsoluteMetricQueueItem regularMetric : sequentialMetrics.getRegularMetrics()) {
                    if (AbsoluteMetricHelper.AWS_EBS_NAMESPACE.equals(regularMetric.getNamespace())) {
                        if (AbsoluteMetricHelper.VOLUME_READ_OPS_METRIC_NAME
                                .equals(regularMetric.getMetricDatum().getMetricName())) { // special case
                            regularMetrics.add(AbsoluteMetricHelper.createVolumeThroughputMetric(
                                    regularMetric.getAccountId(), regularMetric.getNamespace(),
                                    regularMetric.getMetricDatum()));
                        } else if (AbsoluteMetricHelper.VOLUME_TOTAL_READ_WRITE_TIME_METRIC_NAME
                                .equals(regularMetric.getMetricDatum().getMetricName())) {
                            AbsoluteMetricHelper.convertVolumeTotalReadWriteTimeToVolumeIdleTime(
                                    regularMetric.getMetricDatum());
                        }/*  w w  w .  j av  a2s  . c o m*/
                    }
                    regularMetrics.add(regularMetric);
                }
                absoluteMetricHistory.setTimestamp(sequentialMetrics.getUpdateTimestamp());
                absoluteMetricHistory.setLastMetricValue(sequentialMetrics.getUpdateValue());
                if (++count % AbsoluteMetricQueue.ABSOLUTE_METRIC_NUM_DB_OPERATIONS_UNTIL_SESSION_FLUSH == 0) {
                    Entities.flushSession(AbsoluteMetricHistory.class);
                    Entities.clearSession(AbsoluteMetricHistory.class);
                }
            }
        }
        db.commit();
    }
    // Now parse entries only in the map...
    for (AbsoluteMetricMap.NamespaceMetricNameAndDimension namespaceMetricNameAndDimension : absoluteMetricMap
            .keySet()) {
        AbsoluteMetricHistory absoluteMetricHistory = new AbsoluteMetricHistory();
        absoluteMetricHistory.setNamespace(namespaceMetricNameAndDimension.getNamespace());
        absoluteMetricHistory.setMetricName(namespaceMetricNameAndDimension.getMetricName());
        absoluteMetricHistory.setDimensionName(namespaceMetricNameAndDimension.getDimensionName());
        absoluteMetricHistory.setDimensionValue(namespaceMetricNameAndDimension.getDimensionValue());
        MetricsAndOtherFields metricsAndOtherFields = absoluteMetricMap.get(namespaceMetricNameAndDimension);
        Map<TimestampAndMetricValue, MetricDatum> metricDataMap = metricsAndOtherFields.getMetricDatumMap();
        if (metricDataMap.size() == 0)
            continue;
        TimestampAndMetricValue firstValue = metricDataMap.keySet().iterator().next();
        metricDataMap.remove(firstValue);
        absoluteMetricHistory.setLastMetricValue(firstValue.getMetricValue());
        absoluteMetricHistory.setTimestamp(firstValue.getTimestamp());
        if (metricDataMap.size() != 0) {
            SequentialMetrics sequentialMetrics = calculateSequentialMetrics(absoluteMetricHistory,
                    metricDataMap, metricsAndOtherFields.getAccountId(),
                    metricsAndOtherFields.getRelativeMetricName());
            for (AbsoluteMetricQueueItem regularMetric : sequentialMetrics.getRegularMetrics()) {
                if (AbsoluteMetricHelper.AWS_EBS_NAMESPACE.equals(regularMetric.getNamespace())) {
                    if (AbsoluteMetricHelper.VOLUME_READ_OPS_METRIC_NAME
                            .equals(regularMetric.getMetricDatum().getMetricName())) { // special case
                        regularMetrics.add(
                                AbsoluteMetricHelper.createVolumeThroughputMetric(regularMetric.getAccountId(),
                                        regularMetric.getNamespace(), regularMetric.getMetricDatum()));
                    } else if (AbsoluteMetricHelper.VOLUME_TOTAL_READ_WRITE_TIME_METRIC_NAME
                            .equals(regularMetric.getMetricDatum().getMetricName())) {
                        AbsoluteMetricHelper.convertVolumeTotalReadWriteTimeToVolumeIdleTime(
                                regularMetric.getMetricDatum());
                    }
                }
                regularMetrics.add(regularMetric);
            }
            absoluteMetricHistory.setTimestamp(sequentialMetrics.getUpdateTimestamp());
            absoluteMetricHistory.setLastMetricValue(sequentialMetrics.getUpdateValue());
        }
        absoluteMetricsToInsert.add(convertToSimpleAbsoluteMetricHistory(absoluteMetricHistory));
    }

    // insert all new points
    try (final TransactionResource db = Entities.transactionFor(AbsoluteMetricHistory.class)) {
        int count = 0;
        for (SimpleAbsoluteMetricHistory simpleAbsoluteMetricHistory : absoluteMetricsToInsert) {
            Entities.persist(convertToAbsoluteMetricHistory(simpleAbsoluteMetricHistory));
            if (++count % AbsoluteMetricQueue.ABSOLUTE_METRIC_NUM_DB_OPERATIONS_UNTIL_SESSION_FLUSH == 0) {
                Entities.flushSession(AbsoluteMetricHistory.class);
                Entities.clearSession(AbsoluteMetricHistory.class);
            }
        }
        db.commit();
    }
    return regularMetrics;
}

From source file:com.eucalyptus.reporting.art.generator.AbstractArtGenerator.java

License:Open Source License

@SuppressWarnings("unchecked")
protected <ET> void foreach(final Class<ET> eventClass, final Criterion criterion, final boolean ascending,
        final Predicate<? super ET> callback) {
    final EntityTransaction transaction = Entities.get(eventClass);
    ScrollableResults results = null;
    try {/* w  ww . j av  a  2 s .  c o  m*/
        results = Entities.createCriteria(eventClass).setReadOnly(true).setCacheable(false)
                .setCacheMode(CacheMode.IGNORE).setFetchSize(100).add(criterion)
                .addOrder(ascending ? Order.asc(TIMESTAMP_MS) : Order.desc(TIMESTAMP_MS))
                .scroll(ScrollMode.FORWARD_ONLY);

        while (results.next()) {
            final ET event = (ET) results.get(0);
            if (!callback.apply(event)) {
                break;
            }
            Entities.evict(event);
        }
    } finally {
        if (results != null)
            try {
                results.close();
            } catch (Exception e) {
            }
        transaction.rollback();
    }
}

From source file:com.hmsinc.epicenter.tools.reclassifier.Reclassifier.java

License:Open Source License

@Transactional
public void run() {

    setup();/* ww w . j  a  va2s  . c  om*/

    final String destinationTable = (arguments.length > 4) ? arguments[4] : DEFAULT_TABLE_NAME;
    final String query = new StringBuilder("INSERT INTO ").append(destinationTable)
            .append(INSERT_CLASSIFICATION).toString();

    final BatchSqlUpdate updater = new BatchSqlUpdate(modelDataSource, query);
    updater.declareParameter(new SqlParameter(Types.BIGINT));
    updater.declareParameter(new SqlParameter(Types.BIGINT));
    updater.setBatchSize(BATCH_SIZE);
    updater.compile();

    final StatelessSession ss = ((Session) entityManager.getDelegate()).getSessionFactory()
            .openStatelessSession();

    final Criteria c = ss.createCriteria(target.getInteractionClass())
            .add(Restrictions.eq("patientClass", target.getPatientClass())).addOrder(Order.asc("id"))
            .setCacheable(false);

    if (arguments.length > 2) {
        c.add(Restrictions.gt("id", Long.valueOf(arguments[2])));
    }

    if (arguments.length > 3) {
        c.add(Restrictions.lt("id", Long.valueOf(arguments[3])));
    }

    final ScrollableResults sr = c.scroll(ScrollMode.FORWARD_ONLY);
    int i = 0;
    while (sr.next()) {

        final Interaction interaction = (Interaction) sr.get(0);
        final Set<Classification> classifications = classificationService.classify(interaction, target);

        save(interaction, classifications, updater);

        i++;
        if (i % BATCH_SIZE == 0) {
            logger.info("Processed {} interactions (current id: {})", i, interaction.getId());
        }

        ((Session) entityManager.getDelegate()).evict(interaction);
    }

    sr.close();

    updater.flush();
}

From source file:com.ibm.asset.trails.dao.jpa.VSoftwareLparDAOJpa.java

public void paginatedList(DisplayTagList data, Account account, ReconSetting reconSetting, int startIndex,
        int objectsPerPage, String sort, String dir) {
    Criteria criteria = getHibernateSessionCriteria();

    criteria.createAlias("hardwareLpar", "hl")
            .createAlias("hl.hardwareLparEff", "hle", CriteriaSpecification.LEFT_JOIN)
            .createAlias("hl.hardware", "h").createAlias("h.machineType", "mt")
            .createAlias("installedSoftwares", "is")
            .createAlias("is.scheduleF", "sf", CriteriaSpecification.LEFT_JOIN)
            .createAlias("sf.scope", "scope", CriteriaSpecification.LEFT_JOIN)
            .createAlias("is.softwareLpar", "sl").createAlias("is.alert", "aus")
            .createAlias("aus.reconcile", "r", CriteriaSpecification.LEFT_JOIN)
            .createAlias("r.usedLicenses", "ul", CriteriaSpecification.LEFT_JOIN)
            .createAlias("ul.license", "license", CriteriaSpecification.LEFT_JOIN)
            .createAlias("r.reconcileType", "rt", CriteriaSpecification.LEFT_JOIN)
            .createAlias("is.software", "sw")
            .createAlias("sw.manufacturer", "mf", CriteriaSpecification.LEFT_JOIN)
            .add(Restrictions.eq("account", account));

    if (reconSetting.getReconcileType() != null) {
        criteria.add(Restrictions.eq("rt.id", reconSetting.getReconcileType()));
    }/*www.  ja  va2  s .  c o m*/

    if (StringUtils.isNotBlank(reconSetting.getAlertStatus())) {
        boolean open = false;
        if (reconSetting.getAlertStatus().equals("OPEN")) {
            open = true;
            criteria.add(Restrictions.eq("aus.open", open));
        } else {
            criteria.add(Restrictions.and(Restrictions.eq("aus.open", false),
                    Restrictions.eqProperty("is.id", "r.installedSoftware.id")));
        }

    } else {
        criteria.add(Restrictions.or(Restrictions.eq("aus.open", true),
                Restrictions.and(Restrictions.eq("aus.open", false),
                        Restrictions.eqProperty("is.id", "r.installedSoftware.id"))));
    }

    if (null != reconSetting.getAlertFrom() && reconSetting.getAlertFrom().intValue() >= 0) {
        criteria.add(Restrictions.ge("aus.alertAge", reconSetting.getAlertFrom()));
    }
    if (null != reconSetting.getAlertTo() && reconSetting.getAlertTo().intValue() >= 0) {
        criteria.add(Restrictions.le("aus.alertAge", reconSetting.getAlertTo()));
    }

    if (StringUtils.isNotBlank(reconSetting.getAssigned())) {
        if (reconSetting.getAssigned().equals("Assigned")) {
            criteria.add(Restrictions.ne("aus.remoteUser", "STAGING"));
        }
        if (reconSetting.getAssigned().equals("Unassigned")) {
            criteria.add(Restrictions.eq("aus.remoteUser", "STAGING"));
        }
    }

    if (StringUtils.isNotBlank(reconSetting.getAssignee())) {
        criteria.add(Restrictions.eq("aus.remoteUser", reconSetting.getAssignee()).ignoreCase());
    }

    if (StringUtils.isNotBlank(reconSetting.getOwner())) {
        if (reconSetting.getOwner().equalsIgnoreCase("IBM")) {
            criteria.add(Restrictions.eq("h.owner", reconSetting.getOwner()).ignoreCase());
        } else if (reconSetting.getOwner().equalsIgnoreCase("Customer")) {
            ArrayList<String> lalOwner = new ArrayList<String>();

            lalOwner.add("CUST");
            lalOwner.add("CUSTO");
            criteria.add(Restrictions.in("h.owner", lalOwner));
        }
    }

    // I'm not sure why the heck we aren't just getting a list of strings?
    if (reconSetting.getCountries().length > 0) {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < reconSetting.getCountries().length; i++) {
            if (StringUtils.isNotBlank(reconSetting.getCountries()[i])) {
                list.add(reconSetting.getCountries()[i].toUpperCase());
            }
        }

        if (list.size() > 0) {
            criteria.add(Restrictions.in("h.country", list));
        }
    }

    if (reconSetting.getNames().length > 0) {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < reconSetting.getNames().length; i++) {
            if (StringUtils.isNotBlank(reconSetting.getNames()[i])) {
                list.add(reconSetting.getNames()[i].toUpperCase());
            }
        }

        if (list.size() > 0) {
            criteria.add(Restrictions.in("hl.name", list));
        }
    }

    if (reconSetting.getSwcmIDs().length > 0) {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < reconSetting.getSwcmIDs().length; i++) {
            if (StringUtils.isNotBlank(reconSetting.getSwcmIDs()[i])) {
                list.add(reconSetting.getSwcmIDs()[i].toUpperCase());
            }
        }
        if (list.size() > 0) {
            criteria.add(Restrictions.in("license.extSrcId", list));
        }
    }

    if (reconSetting.getSerialNumbers().length > 0) {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < reconSetting.getSerialNumbers().length; i++) {
            if (StringUtils.isNotBlank(reconSetting.getSerialNumbers()[i])) {
                list.add(reconSetting.getSerialNumbers()[i].toUpperCase());
            }
        }

        if (list.size() > 0) {
            criteria.add(Restrictions.in("h.serial", list));
        }
    }

    if (reconSetting.getProductInfoNames().length > 0) {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < reconSetting.getProductInfoNames().length; i++) {
            if (StringUtils.isNotBlank(reconSetting.getProductInfoNames()[i])) {
                list.add(reconSetting.getProductInfoNames()[i]);
            }
        }

        if (list.size() > 0) {
            criteria.add(Restrictions.in("sw.softwareName", list));
        }
    }

    if (StringUtils.isNotBlank(reconSetting.getScope())) {
        if ("Not specified".equalsIgnoreCase(reconSetting.getScope())) {
            criteria.add(Restrictions.isNull("scope.description"));
        } else {
            criteria.add(Restrictions.eq("scope.description", reconSetting.getScope()));
        }
    }

    if (StringUtils.isNotBlank(reconSetting.getFinanResp())) {
        if ("Not Specified".trim().equalsIgnoreCase(reconSetting.getFinanResp())) {
            criteria.add(Restrictions.isNull("sf.SWFinanceResp"));
        } else {
            criteria.add(Restrictions.eq("sf.SWFinanceResp", reconSetting.getFinanResp()));
        }
    }

    criteria.setProjection(Projections.projectionList().add(Projections.property("aus.id").as("alertId"))
            .add(Projections.property("r.id").as("reconcileId"))
            .add(Projections.property("aus.alertAge").as("alertAgeI"))
            .add(Projections.property("is.id").as("installedSoftwareId"))
            .add(Projections.property("hl.name").as("hostname"))
            .add(Projections.property("sl.name").as("sl_hostname"))
            .add(Projections.property("hl.spla").as("spla"))
            .add(Projections.property("hl.sysplex").as("sysplex"))
            .add(Projections.property("hl.internetIccFlag").as("internetIccFlag"))
            .add(Projections.property("h.serial").as("serial"))
            .add(Projections.property("h.country").as("country"))
            .add(Projections.property("h.owner").as("owner"))
            .add(Projections.property("h.mastProcessorType").as("mastProcessorType"))
            .add(Projections.property("h.processorManufacturer").as("processorManufacturer"))
            .add(Projections.property("h.mastProcessorModel").as("mastProcessorModel"))
            .add(Projections.property("h.nbrCoresPerChip").as("nbrCoresPerChip"))
            .add(Projections.property("h.nbrOfChipsMax").as("nbrOfChipsMax"))
            .add(Projections.property("h.cpuLsprMips").as("cpuLsprMips"))
            .add(Projections.property("h.cpuIfl").as("cpuIFL"))
            .add(Projections.property("hl.partLsprMips").as("partLsprMips"))
            .add(Projections.property("h.cpuGartnerMips").as("cpuGartnerMips"))
            .add(Projections.property("hl.partGartnerMips").as("partGartnerMips"))
            .add(Projections.property("hl.effectiveThreads").as("effectiveThreads"))
            .add(Projections.property("hl.vcpu").as("vcpu")).add(Projections.property("h.cpuMsu").as("cpuMsu"))
            .add(Projections.property("hl.partMsu").as("partMsu"))
            .add(Projections.property("hl.serverType").as("lparServerType"))
            .add(Projections.property("h.shared").as("shared"))
            .add(Projections.property("h.multi_tenant").as("multi_tenant"))
            .add(Projections.property("mt.type").as("assetType"))
            .add(Projections.property("mt.name").as("assetName"))
            .add(Projections.property("h.hardwareStatus").as("hardwareStatus"))
            .add(Projections.property("hl.lparStatus").as("lparStatus"))
            .add(Projections.property("processorCount").as("processorCount"))
            .add(Projections.property("sw.softwareName").as("productInfoName"))
            .add(Projections.property("sw.softwareId").as("productInfoId"))
            .add(Projections.property("sw.pid").as("pid"))
            .add(Projections.property("mf.manufacturerName").as("manufacturerName"))
            .add(Projections.property("rt.name").as("reconcileTypeName"))
            .add(Projections.property("rt.id").as("reconcileTypeId"))
            .add(Projections.property("aus.remoteUser").as("assignee"))
            .add(Projections.property("h.processorCount").as("hardwareProcessorCount"))
            .add(Projections.property("hle.processorCount").as("hwLparEffProcessorCount"))
            .add(Projections.property("hl.osType").as("osType"))
            .add(Projections.property("hle.status").as("hwLparEffProcessorStatus"))
            .add(Projections.property("h.chips").as("chips")));
    criteria.setResultTransformer(new AliasToBeanResultTransformer(ReconWorkspace.class));

    criteria.addOrder(Order.desc("aus.open"));

    if (dir.equalsIgnoreCase("ASC")) {
        criteria.addOrder(Order.asc(sort));
    } else {
        criteria.addOrder(Order.desc(sort));
    }

    ArrayList<ReconWorkspace> list = new ArrayList<ReconWorkspace>();

    ScrollableResults itemCursor = criteria.scroll();
    itemCursor.beforeFirst();
    if (itemCursor.next()) {
        itemCursor.scroll(startIndex);
        int i = 0;

        while (objectsPerPage > i++) {
            ReconWorkspace rw = (ReconWorkspace) itemCursor.get(0);
            if (null != rw.getHwLparEffProcessorStatus()
                    && rw.getHwLparEffProcessorStatus().equalsIgnoreCase("INACTIVE")) {
                rw.setHwLparEffProcessorCount(0);
            }
            list.add(rw);
            if (!itemCursor.next())
                break;
        }

        data.setList(list);
        itemCursor.last();
        data.setFullListSize(itemCursor.getRowNumber() + 1);
        itemCursor.close();

        addSchedulef2List(account, data.getList());
    } else {
        data.setList(null);
        data.setFullListSize(0);
        itemCursor.close();
    }

}

From source file:com.ikon.module.db.stuff.IndexHelper.java

License:Open Source License

protected int doRebuildIndex() throws Exception {
    FullTextSession fullTextSession = (FullTextSession) entityManager.getDelegate();
    fullTextSession.setFlushMode(org.hibernate.FlushMode.MANUAL);
    fullTextSession.setCacheMode(org.hibernate.CacheMode.IGNORE);
    fullTextSession.purgeAll(NodeDocumentVersion.class);
    fullTextSession.getSearchFactory().optimize(NodeDocumentVersion.class);

    String query = "select ndv from NodeDocumentVersion ndv";
    ScrollableResults cursor = fullTextSession.createQuery(query).scroll();
    cursor.last();/* ww  w .j a va 2 s  . c  o m*/
    int count = cursor.getRowNumber() + 1;
    log.warn("Re-building Wine index for " + count + " objects.");

    if (count > 0) {
        int batchSize = 300;
        cursor.first(); // Reset to first result row
        int i = 0;

        while (true) {
            fullTextSession.index(cursor.get(0));

            if (++i % batchSize == 0) {
                fullTextSession.flushToIndexes();
                fullTextSession.clear(); // Clear persistence context for each batch
                log.info("Flushed index update " + i + " from Thread " + Thread.currentThread().getName());
            }

            if (cursor.isLast()) {
                break;
            }

            cursor.next();
        }
    }

    cursor.close();
    fullTextSession.flushToIndexes();
    fullTextSession.clear(); // Clear persistence context for each batch
    fullTextSession.getSearchFactory().optimize(NodeDocumentVersion.class);

    return count;
}

From source file:com.ikon.servlet.admin.RebuildIndexesServlet.java

License:Open Source License

/**
 * FlushToIndexes implementation//from   ww w .j  a v a2 s .c  o  m
 */
@SuppressWarnings("rawtypes")
private void luceneIndexesFlushToIndexes(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    log.debug("luceneIndexesFlushToIndexes({}, {})", request, response);
    PrintWriter out = response.getWriter();
    response.setContentType(MimeTypeConfig.MIME_HTML);
    header(out, "Rebuild Lucene indexes", breadcrumb);
    out.flush();

    FullTextSession ftSession = null;
    Session session = null;
    Transaction tx = null;

    try {
        Config.SYSTEM_MAINTENANCE = true;
        Config.SYSTEM_READONLY = true;
        out.println("<ul>");
        out.println("<li>System into maintenance mode</li>");
        FileLogger.info(BASE_NAME, "BEGIN - Rebuild Lucene indexes");

        session = HibernateUtil.getSessionFactory().openSession();
        ftSession = Search.getFullTextSession(session);
        ftSession.setFlushMode(FlushMode.MANUAL);
        ftSession.setCacheMode(CacheMode.IGNORE);
        tx = ftSession.beginTransaction();
        Map<String, Long> total = new HashMap<String, Long>();

        // Calculate number of entities
        for (Class cls : classes) {
            String nodeType = cls.getSimpleName();
            out.println("<li>Calculate " + nodeType + "</li>");
            out.flush();
            long partial = NodeBaseDAO.getInstance().getCount(nodeType);
            FileLogger.info(BASE_NAME, "Number of {0}: {1}", nodeType, partial);
            out.println("<li>Number of " + nodeType + ": " + partial + "</li>");
            out.flush();
            total.put(nodeType, partial);
        }

        // Rebuild indexes
        out.println("<li>Rebuilding indexes</li>");
        out.flush();

        // Scrollable results will avoid loading too many objects in memory
        for (Class cls : classes) {
            String nodeType = cls.getSimpleName();
            out.println("<li>Indexing " + nodeType + "</li>");
            out.flush();

            ProgressMonitor monitor = new ProgressMonitor(out, total.get(nodeType));
            ScrollableResults results = ftSession.createCriteria(cls)
                    .setFetchSize(Config.HIBERNATE_INDEXER_BATCH_SIZE_LOAD_OBJECTS)
                    .scroll(ScrollMode.FORWARD_ONLY);
            int index = 0;

            while (results.next()) {
                monitor.documentsAdded(1);
                ftSession.index(results.get(0)); // Index each element

                if (index++ % Config.HIBERNATE_INDEXER_BATCH_SIZE_LOAD_OBJECTS == 0) {
                    ftSession.flushToIndexes(); // Apply changes to indexes
                    ftSession.clear(); // Free memory since the queue is processed
                }
            }
        }

        tx.commit();

        Config.SYSTEM_READONLY = false;
        Config.SYSTEM_MAINTENANCE = false;
        out.println("<li>System out of maintenance mode</li>");
        out.flush();

        // Finalized
        FileLogger.info(BASE_NAME, "END - Rebuild Lucene indexes");
        out.println("<li>Index rebuilding completed!</li>");
        out.println("</ul>");
        out.flush();
    } catch (Exception e) {
        tx.rollback();
        out.println("<div class=\"warn\">Exception: " + e.getMessage() + "</div>");
        out.flush();
    } finally {
        HibernateUtil.close(ftSession);
        HibernateUtil.close(session);
    }

    // End page
    footer(out);
    out.flush();
    out.close();

    // Activity log
    UserActivity.log(request.getRemoteUser(), "ADMIN_FORCE_REBUILD_INDEXES", null, null, null);
    log.debug("luceneIndexesFlushToIndexes: void");
}

From source file:com.kanjiportal.portal.admin.LuceneManagementAction.java

License:Open Source License

public void reIndexKanji() {
    FullTextSession fullTextSession = (FullTextSession) entityManager.getDelegate();
    fullTextSession.setFlushMode(FlushMode.MANUAL);
    fullTextSession.setCacheMode(CacheMode.IGNORE);
    log.debug("Indexing kanjis ....");
    long beforeKanjis = System.currentTimeMillis();

    //Scrollable results will avoid loading too many objects in memory
    ScrollableResults results = fullTextSession.createCriteria(Kanji.class).setFetchSize(BATCH_SIZE)
            .setFetchMode("meanings", FetchMode.JOIN).setFetchMode("meanings.meaning.language", FetchMode.JOIN)
            .scroll(ScrollMode.FORWARD_ONLY);
    int index = 0;
    while (results.next()) {
        index++;// w w  w. ja  v  a2s  . co  m
        fullTextSession.index(results.get(0)); //index each element
        if (index % BATCH_SIZE == 0) {
            entityManager.flush();
            entityManager.clear();
            log.debug("flush()");
        }
    }

    log.debug("Indexing kanjis done");
    long afterKanjis = System.currentTimeMillis();

    facesMessages.add("Kanji Indexing done in #0 ms", afterKanjis - beforeKanjis);
}

From source file:com.kanjiportal.portal.admin.LuceneManagementAction.java

License:Open Source License

public void reIndexDictionary() {
    FullTextSession fullTextSession = (FullTextSession) entityManager.getDelegate();
    fullTextSession.setFlushMode(FlushMode.MANUAL);
    fullTextSession.setCacheMode(CacheMode.IGNORE);

    long beforeDictionary = System.currentTimeMillis();

    //Scrollable results will avoid loading too many objects in memory
    ScrollableResults results = fullTextSession.createCriteria(Dictionary.class).setFetchSize(BATCH_SIZE)
            .setFetchMode("translations", FetchMode.JOIN).setFetchMode("translations.language", FetchMode.JOIN)
            .scroll(ScrollMode.FORWARD_ONLY);
    int index = 0;
    while (results.next()) {
        index++;/*from  ww w.  j  a  v  a 2  s. c  om*/
        fullTextSession.index(results.get(0)); //index each element
        if (index % BATCH_SIZE == 0) {
            entityManager.flush();
            entityManager.clear();
            log.debug("flush()");
        }
    }

    log.debug("Indexing dictionary done");
    long afterDictionary = System.currentTimeMillis();

    facesMessages.add("Dictionary Indexing done in #0 ms", afterDictionary - beforeDictionary);
}

From source file:com.knowbout.epg.EPG.java

License:Apache License

private static void updateSitemap(String sitemap) {
    int lastslash = sitemap.lastIndexOf('/');
    String inprogress = sitemap.substring(0, lastslash) + "/inprogress-" + sitemap.substring(lastslash + 1);
    String marker = "<!-- EVERYTHING BELOW IS AUTOMATICALLY GENERATED -->";
    String baseurl = null;/*from ww w.  j  a v a2 s .  com*/
    try {
        PrintStream doc = new PrintStream(new GZIPOutputStream(new FileOutputStream(inprogress)));
        BufferedReader orig = new BufferedReader(
                new InputStreamReader(new GZIPInputStream(new FileInputStream(sitemap))));
        String line = orig.readLine();
        while (line != null) {
            if ((line.indexOf("</urlset>") >= 0) || (line.indexOf(marker) >= 0)) {
                break;
            }
            if (baseurl == null) {
                if (line.indexOf("<loc>") >= 0) {
                    int http = line.indexOf("http://");
                    int nextslash = line.indexOf("/", http + 7);
                    baseurl = line.substring(http, nextslash);
                }
            }
            doc.println(line);
            line = orig.readLine();
        }
        doc.println(marker);
        Set<String> teams = new HashSet<String>();
        HibernateUtil.openSession();
        try {
            ScrollableResults scroll = Program.selectAllTeams();
            while (scroll.next()) {
                Program program = (Program) scroll.get(0);
                addToSitemap(doc, baseurl, program);
                teams.add(program.getSportName() + ":" + program.getTeamName());
            }
            scroll = Program.selectAllShowsMoviesSports();
            while (scroll.next()) {
                Program program = (Program) scroll.get(0);
                if (program.isSports()) {
                    if (!teams.contains(program.getSportName() + ":" + program.getHomeTeamName())) {
                        Program home = Program.selectByTeam(program.getSportName(), program.getHomeTeamName());
                        addToSitemap(doc, baseurl, home);
                        teams.add(home.getSportName() + ":" + home.getTeamName());
                    }
                    if (!teams.contains(program.getSportName() + ":" + program.getAwayTeamName())) {
                        Program away = Program.selectByTeam(program.getSportName(), program.getAwayTeamName());
                        addToSitemap(doc, baseurl, away);
                        teams.add(away.getSportName() + ":" + away.getTeamName());
                    }
                } else {
                    addToSitemap(doc, baseurl, program);
                }
            }
        } finally {
            HibernateUtil.closeSession();
        }
        doc.println("</urlset>");
        doc.close();
        orig.close();
        File origFile = new File(sitemap);
        File backupFile = new File(sitemap + ".bak");
        File inprogressFile = new File(inprogress);
        backupFile.delete();
        if (!origFile.renameTo(backupFile)) {
            throw new IOException("Could not rename " + origFile + " to " + backupFile);
        }
        if (!inprogressFile.renameTo(origFile)) {
            throw new IOException("Could not rename " + inprogressFile + " to " + origFile);
        }
    } catch (FileNotFoundException e) {
        log.error("Could not write to " + inprogress, e);
    } catch (IOException e) {
        log.error("IO Exception for " + inprogress + " or " + sitemap, e);
    }
}

From source file:com.leixl.easyframework.lucene.impl.LuceneEMovieDaoImpl.java

License:Open Source License

public Integer index(IndexWriter writer, Date startDate, Date endDate, Integer startId, Integer max)
        throws CorruptIndexException, IOException {
    Finder f = Finder.create("select bean from EMovie bean");

    f.append(" where 1=1");
    if (startId != null) {
        f.append(" and bean.id > :startId");
        f.setParam("startId", startId);
    }// ww  w.  j  av  a  2s.c o  m
    if (startDate != null) {
        f.append(" and bean.createDate >= :startDate");
        f.setParam("startDate", startDate);
    }
    if (endDate != null) {
        f.append(" and bean.createDate <= :endDate");
        f.setParam("endDate", endDate);
    }
    f.append(" order by bean.id asc");
    if (max != null) {
        f.setMaxResults(max);
    }
    Session session = getSession();
    ScrollableResults movies = f.createQuery(getSession()).setCacheMode(CacheMode.IGNORE)
            .scroll(ScrollMode.FORWARD_ONLY);
    int count = 0;
    EMovie movie = null;
    while (movies.next()) {
        movie = (EMovie) movies.get(0);
        writer.addDocument(LuceneContent.createDocument(movie));
        if (++count % 20 == 0) {
            session.clear();
        }
    }
    if (count < max || movie == null) {
        // ????????
        return null;
    } else {
        // ?ID
        return movie.getId();
    }

}