<commit_before>package com.gentics.mesh.changelog.highlevel.change;

import java.util.Iterator;

import javax.inject.Inject;
import javax.inject.Singleton;

import com.gentics.madl.tx.Tx;
import com.gentics.mesh.changelog.highlevel.AbstractHighLevelChange;
import com.gentics.mesh.core.data.container.impl.MicroschemaContainerImpl;
import com.gentics.mesh.core.data.container.impl.NodeGraphFieldContainerImpl;
import com.gentics.mesh.core.data.impl.GroupImpl;
import com.gentics.mesh.core.data.impl.ProjectImpl;
import com.gentics.mesh.core.data.impl.RoleImpl;
import com.gentics.mesh.core.data.impl.TagFamilyImpl;
import com.gentics.mesh.core.data.impl.TagImpl;
import com.gentics.mesh.core.data.impl.UserImpl;
import com.gentics.mesh.core.data.schema.impl.SchemaContainerImpl;
import com.gentics.mesh.graphdb.spi.Database;
import com.gentics.mesh.search.BucketableElement;
import com.gentics.mesh.search.verticle.eventhandler.SyncEventHandler;

import dagger.Lazy;
import io.vertx.core.Vertx;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;

@Singleton
public class PrepareBucketableElements extends AbstractHighLevelChange {

	private static final Logger log = LoggerFactory.getLogger(PrepareBucketableElements.class);

	private Lazy<Database> db;

	private Lazy<Vertx> vertx;

	@Inject
	public PrepareBucketableElements(Lazy<Database> db, Lazy<Vertx> vertx) {
		this.db = db;
		this.vertx = vertx;
	}

	@Override
	public String getName() {
		return "PrepareBucketableElements";
	}

	@Override
	public String getUuid() {
		return "038F1F8BFF534956AF4CC056DE7B2166";
	}

	@Override
	public String getDescription() {
		return "Sets the bucketIds for all bucketable elements.";
	}

	@Override
	public void apply() {
		log.info("Applying change: " + getName());
		migrate(UserImpl.class);
		migrate(GroupImpl.class);
		migrate(RoleImpl.class);
		migrate(TagImpl.class);
		migrate(TagFamilyImpl.class);
		migrate(ProjectImpl.class);
		migrate(SchemaContainerImpl.class);
		migrate(MicroschemaContainerImpl.class);
		migrate(NodeGraphFieldContainerImpl.class);

		// Now reindex the elements since the bucketId 
		// needs to be in the elasticsearch documents.
		SyncEventHandler.invokeClearCompletable(vertx.get())
			.andThen(SyncEventHandler.invokeSyncCompletable(vertx.get()))
			.blockingAwait();<commit_msg>Fix automatic sync<commit_after>package com.gentics.mesh.changelog.highlevel.change;

import java.util.Iterator;

import javax.inject.Inject;
import javax.inject.Singleton;

import com.gentics.madl.tx.Tx;
import com.gentics.mesh.changelog.highlevel.AbstractHighLevelChange;
import com.gentics.mesh.core.data.container.impl.MicroschemaContainerImpl;
import com.gentics.mesh.core.data.container.impl.NodeGraphFieldContainerImpl;
import com.gentics.mesh.core.data.impl.GroupImpl;
import com.gentics.mesh.core.data.impl.ProjectImpl;
import com.gentics.mesh.core.data.impl.RoleImpl;
import com.gentics.mesh.core.data.impl.TagFamilyImpl;
import com.gentics.mesh.core.data.impl.TagImpl;
import com.gentics.mesh.core.data.impl.UserImpl;
import com.gentics.mesh.core.data.schema.impl.SchemaContainerImpl;
import com.gentics.mesh.etc.config.MeshOptions;
import com.gentics.mesh.graphdb.spi.Database;
import com.gentics.mesh.search.BucketableElement;
import com.gentics.mesh.search.IndexHandlerRegistry;
import com.gentics.mesh.search.SearchProvider;
import com.gentics.mesh.search.verticle.eventhandler.SyncEventHandler;

import dagger.Lazy;
import io.reactivex.Observable;
import io.vertx.core.Vertx;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;

@Singleton
public class PrepareBucketableElements extends AbstractHighLevelChange {

	private static final Logger log = LoggerFactory.getLogger(PrepareBucketableElements.class);

	private final Lazy<Database> db;

	private final Lazy<Vertx> vertx;

	private final Lazy<SearchProvider> searchProvider;

	private final Lazy<IndexHandlerRegistry> registry;

	private MeshOptions options;

	@Inject
	public PrepareBucketableElements(MeshOptions options, Lazy<Database> db, Lazy<Vertx> vertx, Lazy<SearchProvider> searchProvider,
		Lazy<IndexHandlerRegistry> registry) {
		this.options = options;
		this.db = db;
		this.vertx = vertx;
		this.searchProvider = searchProvider;
		this.registry = registry;
	}

	@Override
	public String getName() {
		return "PrepareBucketableElements";
	}

	@Override
	public String getUuid() {
		return "038F1F8BFF534956AF4CC056DE7B2165";
	}

	@Override
	public String getDescription() {
		return "Sets the bucketIds for all bucketable elements.";
	}

	@Override
	public void apply() {
		log.info("Applying change: " + getName());
		migrate(UserImpl.class);
		migrate(GroupImpl.class);
		migrate(RoleImpl.class);
		migrate(TagImpl.class);
		migrate(TagFamilyImpl.class);
		migrate(ProjectImpl.class);
		migrate(SchemaContainerImpl.class);
		migrate(MicroschemaContainerImpl.class);
		migrate(NodeGraphFieldContainerImpl.class);

		if (options.getSearchOptions().getUrl() != null) {
			// Now reindex the elements since the bucketId
			// needs to be in the elasticsearch documents.
			searchProvider.get().clear()
				.andThen(Observable.fromIterable(registry.get().getHandlers())
					.flatMapCompletable(handler -> handler.init()))
				.blockingAwait();

			vertx.get().setTimer(6000, id -> {
				SyncEventHandler.invokeSync(vertx.get());
			});
		}

{'fraction_non_alphanumeric': 0.11121408711770157, 'fraction_numerical': 0.008155699721964782, 'mean_word_length': 5.580487804878048, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7492483', 'n_tokens_mistral': 1842, 'n_tokens_neox': 1747, 'n_words': 300}