<commit_before>/*
 * Copyright 2011-2015 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.b2international.snowowl.datastore.server.index;

import java.util.Collection;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.ecore.EObject;
import org.slf4j.Logger;

import com.b2international.commons.CancelableProgressMonitorWrapper;
import com.b2international.commons.LogProgressMonitorWrapper;
import com.b2international.snowowl.core.api.IBranchPath;
import com.b2international.snowowl.core.api.SnowowlServiceException;
import com.b2international.snowowl.core.api.index.IIndexEntry;
import com.b2international.snowowl.core.api.index.IIndexMappingStrategy;
import com.b2international.snowowl.core.api.index.IIndexUpdater;
import com.b2international.snowowl.datastore.BranchPathUtils;
import com.b2international.snowowl.datastore.CDOEditingContext;
import com.google.common.collect.Iterables;

/**
 * {@link AbstractReIndexJob} to reIndex element on terminologies.
 * 
 * @param T
 *            - the persisted {@link CDOObject} element type for the terminology
 * @param E
 *            - the {@link IIndexEntry} type for the terminology
 * @since 2.7
 */
public abstract class AbstractReIndexJob<T extends CDOObject, E extends IIndexEntry> extends Job {

	private IBranchPath branchPath = BranchPathUtils.createMainPath();

	public AbstractReIndexJob(final String name) {
		super(name);
	}

	@Override
	protected IStatus run(final IProgressMonitor monitor) {
		try {
			return reIndex(createSubMonitor(monitor, getLogger()));
		} catch (final OperationCanceledException e) {
			return Status.CANCEL_STATUS;
		} catch (final SnowowlServiceException e) {
			return new Status(Status.ERROR, getPluginId(), String.format("Caught exception while reIndexing %s",
					getIndexName()), e);
		} finally {
			monitor.done();
		}
	}

	/**
	 * Returns the {@link IBranchPath}.
	 * 
	 * @return
	 */
	public IBranchPath getBranchPath() {
		return branchPath;
	}

	protected abstract String getIndexName();

	protected abstract String getPluginId();

	protected abstract Logger getLogger();

	/**
	 * Do the reindex job within this method.
	 * 
	 * @param monitor
	 * @return
	 * @throws SnowowlServiceException
	 */
	protected IStatus reIndex(final SubMonitor monitor) throws SnowowlServiceException {
		CDOEditingContext context = null;
		try {
			context = createEditingContext();
			doReIndex(context, getIndexUpdater(), monitor);
		} finally {
			if (context != null) {
				context.close();
			}
		}
		// create editing context
		return Status.OK_STATUS;
	}

	protected void doReIndex(final CDOEditingContext context, final IIndexUpdater<E> indexUpdater, final SubMonitor monitor) {
		cleanIndex(context, indexUpdater, monitor.newChild(15));
		reIndex(context, indexUpdater, monitor.newChild(80));
		postProcess(indexUpdater, monitor.newChild(5));
	}
	
	protected void reIndex(final CDOEditingContext context, final IIndexUpdater<E> indexUpdater, final SubMonitor monitor) {
		monitor.setWorkRemaining(getTargetClasses().length);
		for (final Class<? extends T> targetClass : getTargetClasses()) {			
			reIndex(context, indexUpdater, targetClass, monitor.newChild(1));
		}
	}
	
	protected abstract Class<? extends T>[] getTargetClasses();

	/**
	 * Reindexes each desired target component in the containment list of the given {@link CDOEditingContext}'s
	 * rootResource by using the given {@link IIndexUpdater} with {@link #getMappingStrategy(CDOObject)}.
	 * 
	 * @param context
	 * @param indexUpdater
	 * @param monitor
	 */
	protected void reIndex(final CDOEditingContext context, final IIndexUpdater<E> indexUpdater, final Class<? extends T> targetClass, final SubMonitor monitor) {
		final Collection<EObject> contents = context.getContents();
		final int size = contents.size();
		monitor.setWorkRemaining(size + 5);
		for (final T component : Iterables.filter(contents, targetClass)) {
			indexUpdater.index(branchPath, getMappingStrategy(component));
			monitor.worked(1);
		}
		monitor.done();
	}

	/**
	 * Executes post process steps at the end of the reIndex task.
	 * 
	 * @param indexUpdater
	 * @param monitor
	 */
	protected void postProcess(final IIndexUpdater<E> indexUpdater, final SubMonitor monitor) {
		monitor.setWorkRemaining(1);
		indexUpdater.commit(branchPath);
		monitor.done();<commit_msg>[core] extracted component based reIndex method<commit_after>/*
 * Copyright 2011-2015 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.b2international.snowowl.datastore.server.index;

import java.util.Collection;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.ecore.EObject;
import org.slf4j.Logger;

import com.b2international.commons.CancelableProgressMonitorWrapper;
import com.b2international.commons.LogProgressMonitorWrapper;
import com.b2international.snowowl.core.api.IBranchPath;
import com.b2international.snowowl.core.api.SnowowlServiceException;
import com.b2international.snowowl.core.api.index.IIndexEntry;
import com.b2international.snowowl.core.api.index.IIndexMappingStrategy;
import com.b2international.snowowl.core.api.index.IIndexUpdater;
import com.b2international.snowowl.datastore.BranchPathUtils;
import com.b2international.snowowl.datastore.CDOEditingContext;
import com.google.common.collect.Iterables;

/**
 * {@link AbstractReIndexJob} to reIndex element on terminologies.
 * 
 * @param T
 *            - the persisted {@link CDOObject} element type for the terminology
 * @param E
 *            - the {@link IIndexEntry} type for the terminology
 * @since 2.7
 */
public abstract class AbstractReIndexJob<T extends CDOObject, E extends IIndexEntry> extends Job {

	private IBranchPath branchPath = BranchPathUtils.createMainPath();

	public AbstractReIndexJob(final String name) {
		super(name);
	}

	@Override
	protected IStatus run(final IProgressMonitor monitor) {
		try {
			return reIndex(createSubMonitor(monitor, getLogger()));
		} catch (final OperationCanceledException e) {
			return Status.CANCEL_STATUS;
		} catch (final SnowowlServiceException e) {
			return new Status(Status.ERROR, getPluginId(), String.format("Caught exception while reIndexing %s",
					getIndexName()), e);
		} finally {
			monitor.done();
		}
	}

	/**
	 * Returns the {@link IBranchPath}.
	 * 
	 * @return
	 */
	public IBranchPath getBranchPath() {
		return branchPath;
	}

	protected abstract String getIndexName();

	protected abstract String getPluginId();

	protected abstract Logger getLogger();

	/**
	 * Do the reindex job within this method.
	 * 
	 * @param monitor
	 * @return
	 * @throws SnowowlServiceException
	 */
	protected IStatus reIndex(final SubMonitor monitor) throws SnowowlServiceException {
		CDOEditingContext context = null;
		try {
			context = createEditingContext();
			doReIndex(context, getIndexUpdater(), monitor);
		} finally {
			if (context != null) {
				context.close();
			}
		}
		// create editing context
		return Status.OK_STATUS;
	}

	protected void doReIndex(final CDOEditingContext context, final IIndexUpdater<E> indexUpdater, final SubMonitor monitor) {
		cleanIndex(context, indexUpdater, monitor.newChild(15));
		reIndex(context, indexUpdater, monitor.newChild(80));
		postProcess(indexUpdater, monitor.newChild(5));
	}
	
	protected void reIndex(final CDOEditingContext context, final IIndexUpdater<E> indexUpdater, final SubMonitor monitor) {
		monitor.setWorkRemaining(getTargetClasses().length);
		for (final Class<? extends T> targetClass : getTargetClasses()) {			
			reIndex(context, indexUpdater, targetClass, monitor.newChild(1));
		}
	}
	
	protected abstract Class<? extends T>[] getTargetClasses();

	/**
	 * Reindexes each desired target component in the containment list of the given {@link CDOEditingContext}'s
	 * rootResource by using the given {@link IIndexUpdater} with {@link #getMappingStrategy(CDOObject)}.
	 * 
	 * @param context
	 * @param indexUpdater
	 * @param monitor
	 */
	protected void reIndex(final CDOEditingContext context, final IIndexUpdater<E> indexUpdater, final Class<? extends T> targetClass, final SubMonitor monitor) {
		final Collection<EObject> contents = context.getContents();
		final int size = contents.size();
		monitor.setWorkRemaining(size + 5);
		for (final T component : Iterables.filter(contents, targetClass)) {
			reIndex(component, indexUpdater);
			monitor.worked(1);
		}
		monitor.done();
	}

	protected void reIndex(final T component, final IIndexUpdater<E> indexUpdater) {
		indexUpdater.index(branchPath, getMappingStrategy(component));
	}

	/**
	 * Executes post process steps at the end of the reIndex task.
	 * 
	 * @param indexUpdater
	 * @param monitor
	 */
	protected void postProcess(final IIndexUpdater<E> indexUpdater, final SubMonitor monitor) {
		monitor.setWorkRemaining(1);
		indexUpdater.commit(branchPath);
		monitor.done();
{'fraction_non_alphanumeric': 0.09895038167938931, 'fraction_numerical': 0.006870229007633588, 'mean_word_length': 4.768299394606494, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '383181', 'n_tokens_mistral': 3293, 'n_tokens_neox': 3056, 'n_words': 834}