<commit_before>/*
 * Copyright 1999-2010 <NAME> (l.garulli--at--orientechnologies.com)
 *
 * 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.orientechnologies.orient.graph.gremlin;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.script.ScriptContext;

import com.orientechnologies.common.concur.resource.OResourcePool;
import com.orientechnologies.common.concur.resource.OResourcePoolListener;
import com.orientechnologies.orient.core.command.OCommandManager;
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.db.graph.OGraphDatabase;
import com.orientechnologies.orient.core.db.record.ODatabaseRecord;
import com.orientechnologies.orient.core.db.record.ODatabaseRecordTx;
import com.orientechnologies.orient.core.exception.OCommandExecutionException;
import com.orientechnologies.orient.core.exception.OConfigurationException;
import com.orientechnologies.orient.core.exception.OSecurityException;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.OSQLEngine;
import com.tinkerpop.blueprints.pgm.impls.orientdb.OrientGraph;
import com.tinkerpop.gremlin.jsr223.GremlinScriptEngine;
import com.tinkerpop.gremlin.pipes.GremlinPipeline;

public class OGremlinHelper {
	private static final String													PARAM_OUTPUT	= "output";
	private static Object																ONE						= new Object();
	private static OGremlinHelper												instance			= new OGremlinHelper();

	private int																					maxEngines		= 50;
	private int																					maxGraphs			= 50;

	private OResourcePool<Object, GremlinScriptEngine>	enginePool;
	private OResourcePool<String, OrientGraph>					graphPool;

	public static interface OGremlinCallback {
		public boolean call(GremlinScriptEngine iEngine, OrientGraph iGraph);
	}

	public OGremlinHelper() {
		OCommandManager.instance().registerRequester("gremlin", OCommandGremlin.class);
		OCommandManager.instance().registerExecutor(OCommandGremlin.class, OCommandGremlinExecutor.class);
		OSQLEngine.getInstance().registerFunction(OSQLFunctionGremlin.NAME, OSQLFunctionGremlin.class);
	}

	/**
	 * Initializes the pools.
	 */
	public void create() {
		if (enginePool != null)
			throw new IllegalStateException("OGremlinHelper instance already created");

		enginePool = new OResourcePool<Object, GremlinScriptEngine>(maxEngines,
				new OResourcePoolListener<Object, GremlinScriptEngine>() {

					@Override
					public GremlinScriptEngine createNewResource(Object iKey, Object... iAdditionalArgs) {
						try {
							return new GremlinScriptEngine();
						} catch (Throwable e) {
							throw new OConfigurationException("Error on loading Gremlin engine", e);
						}
					}

					@Override
					public GremlinScriptEngine reuseResource(Object iKey, Object[] iAdditionalArgs, GremlinScriptEngine iReusedEngine) {
						iReusedEngine.getBindings(ScriptContext.ENGINE_SCOPE).clear();
						return iReusedEngine;
					}<commit_msg>Fixed problem with multiple starts<commit_after>/*
 * Copyright 1999-2010 <NAME> (l.garulli--at--orientechnologies.com)
 *
 * 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.orientechnologies.orient.graph.gremlin;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.script.ScriptContext;

import com.orientechnologies.common.concur.resource.OResourcePool;
import com.orientechnologies.common.concur.resource.OResourcePoolListener;
import com.orientechnologies.orient.core.command.OCommandManager;
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.db.graph.OGraphDatabase;
import com.orientechnologies.orient.core.db.record.ODatabaseRecord;
import com.orientechnologies.orient.core.db.record.ODatabaseRecordTx;
import com.orientechnologies.orient.core.exception.OCommandExecutionException;
import com.orientechnologies.orient.core.exception.OConfigurationException;
import com.orientechnologies.orient.core.exception.OSecurityException;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.OSQLEngine;
import com.tinkerpop.blueprints.pgm.impls.orientdb.OrientGraph;
import com.tinkerpop.gremlin.jsr223.GremlinScriptEngine;
import com.tinkerpop.gremlin.pipes.GremlinPipeline;

public class OGremlinHelper {
	private static final String													PARAM_OUTPUT	= "output";
	private static Object																ONE						= new Object();
	private static OGremlinHelper												instance			= new OGremlinHelper();

	private int																					maxEngines		= 50;
	private int																					maxGraphs			= 50;

	private OResourcePool<Object, GremlinScriptEngine>	enginePool;
	private OResourcePool<String, OrientGraph>					graphPool;

	public static interface OGremlinCallback {
		public boolean call(GremlinScriptEngine iEngine, OrientGraph iGraph);
	}

	public OGremlinHelper() {
		OCommandManager.instance().registerRequester("gremlin", OCommandGremlin.class);
		OCommandManager.instance().registerExecutor(OCommandGremlin.class, OCommandGremlinExecutor.class);
		OSQLEngine.getInstance().registerFunction(OSQLFunctionGremlin.NAME, OSQLFunctionGremlin.class);
	}

	/**
	 * Initializes the pools.
	 */
	public void create() {
		if (enginePool != null)
			// ALREADY CREATED
			return;

		enginePool = new OResourcePool<Object, GremlinScriptEngine>(maxEngines,
				new OResourcePoolListener<Object, GremlinScriptEngine>() {

					@Override
					public GremlinScriptEngine createNewResource(Object iKey, Object... iAdditionalArgs) {
						try {
							return new GremlinScriptEngine();
						} catch (Throwable e) {
							throw new OConfigurationException("Error on loading Gremlin engine", e);
						}
					}

					@Override
					public GremlinScriptEngine reuseResource(Object iKey, Object[] iAdditionalArgs, GremlinScriptEngine iReusedEngine) {
						iReusedEngine.getBindings(ScriptContext.ENGINE_SCOPE).clear();
						return iReusedEngine;
					}
{'fraction_non_alphanumeric': 0.08707829408020369, 'fraction_numerical': 0.004837683004455761, 'mean_word_length': 4.701015965166909, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '1569648', 'n_tokens_mistral': 2562, 'n_tokens_neox': 2219, 'n_words': 513}