<commit_before>/*
 * Copyright 2010-2012 <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 javax.script.ScriptEngine;

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.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngineFactory;
import com.tinkerpop.gremlin.java.GremlinPipeline;

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

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

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

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

  public OGremlinHelper() {
    OCommandManager.instance().registerRequester("gremlin", OCommandGremlin.class);
    OCommandManager.instance().registerExecutor(OCommandGremlin.class, OCommandGremlinExecutor.class);
  }

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

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

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

      @Override
      public boolean reuseResource(Object iKey, Object[] iAdditionalArgs, ScriptEngine iReusedEngine) {
        iReusedEngine.getBindings(ScriptContext.ENGINE_SCOPE).clear();
        return true;
      }
    });

    graphPool = new OResourcePool<String, OrientGraph>(maxGraphs, new OResourcePoolListener<String, OrientGraph>() {

      @Override
      public OrientGraph createNewResource(final String iKey, final Object... iAdditionalArgs) {
        final String[] parts = iKey.split(",");
        return new OrientGraph(parts[1]);
      }

      @Override
      public boolean reuseResource(final String iKey, final Object[] iAdditionalArgs, final OrientGraph iReusedGraph) {
        return true;
      }
    });
  }

  /**
   * Destroys the helper by cleaning all the in memory objects.
   */
  public void destroy() {
    if (enginePool != null) {
      for (ScriptEngine engine : enginePool.getResources()) {
        engine.getBindings(ScriptContext.ENGINE_SCOPE).clear();
      }
      enginePool.close();
    }

    if (graphPool != null) {
      for (OrientGraph graph : graphPool.getResources()) {
        graph.shutdown();
      }
      graphPool.close();
    }
  }

  public ScriptEngine acquireEngine() {
    checkStatus();
    return enginePool.getResource(ONE, Long.MAX_VALUE);
  }

  public void releaseEngine(final ScriptEngine engine) {
    checkStatus();
    engine.getBindings(ScriptContext.ENGINE_SCOPE).clear();
    enginePool.returnResource(engine);
  }

  public OrientGraph acquireGraph(final String iURL) {
    return acquireGraph("admin", "admin", iURL);
  }

  public OrientGraph acquireGraph(final String iURL, final String iUserName, final String iUserPassword) {
    checkStatus();
    final OrientGraph g = graphPool.getResource(iUserName + "," + iURL, Long.MAX_VALUE);
    if (!g.getRawGraph().getUser().checkPassword(iUserPassword)) {
      graphPool.returnResource(g);
      throw new OSecurityException("User and/or password not valid to open database: " + iURL);
    }

    return g;
  }

  public OrientGraph acquireGraph(final OGraphDatabase iDatabase) {
    checkStatus();
    return ((OrientGraph) graphPool.getResource(iDatabase.getUser().getName() + "," + iDatabase.getURL(), Long.MAX_VALUE))
        .reuse(iDatabase);
  }
<commit_msg>Changed to use the new OrientGraph implementation<commit_after>/*
 * Copyright 2010-2012 <NAME> (<EMAIL>--at--orient<EMAIL>)
 *
 * 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 javax.script.ScriptEngine;

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.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngineFactory;
import com.tinkerpop.gremlin.java.GremlinPipeline;

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

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

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

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

  public OGremlinHelper() {
    OCommandManager.instance().registerRequester("gremlin", OCommandGremlin.class);
    OCommandManager.instance().registerExecutor(OCommandGremlin.class, OCommandGremlinExecutor.class);
  }

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

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

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

      @Override
      public boolean reuseResource(Object iKey, Object[] iAdditionalArgs, ScriptEngine iReusedEngine) {
        iReusedEngine.getBindings(ScriptContext.ENGINE_SCOPE).clear();
        return true;
      }
    });

    graphPool = new OResourcePool<String, OrientGraph>(maxGraphs, new OResourcePoolListener<String, OrientGraph>() {

      @Override
      public OrientGraph createNewResource(final String iKey, final Object... iAdditionalArgs) {
        final String[] parts = iKey.split(",");
        return new OrientGraph(parts[1]);
      }

      @Override
      public boolean reuseResource(final String iKey, final Object[] iAdditionalArgs, final OrientGraph iReusedGraph) {
        return true;
      }
    });
  }

  /**
   * Destroys the helper by cleaning all the in memory objects.
   */
  public void destroy() {
    if (enginePool != null) {
      for (ScriptEngine engine : enginePool.getResources()) {
        engine.getBindings(ScriptContext.ENGINE_SCOPE).clear();
      }
      enginePool.close();
    }

    if (graphPool != null) {
      for (OrientGraph graph : graphPool.getResources()) {
        graph.shutdown();
      }
      graphPool.close();
    }
  }

  public ScriptEngine acquireEngine() {
    checkStatus();
    return enginePool.getResource(ONE, Long.MAX_VALUE);
  }

  public void releaseEngine(final ScriptEngine engine) {
    checkStatus();
    engine.getBindings(ScriptContext.ENGINE_SCOPE).clear();
    enginePool.returnResource(engine);
  }

  public OrientGraph acquireGraph(final String iURL) {
    return acquireGraph("admin", "admin", iURL);
  }

  public OrientGraph acquireGraph(final String iURL, final String iUserName, final String iUserPassword) {
    checkStatus();
    final OrientGraph g = graphPool.getResource(iUserName + "," + iURL, Long.MAX_VALUE);
    if (!g.getRawGraph().getUser().checkPassword(iUserPassword)) {
      graphPool.returnResource(g);
      throw new OSecurityException("User and/or password not valid to open database: " + iURL);
    }

    return g;
  }

  public OrientGraph acquireGraph(final OGraphDatabase iDatabase) {
    checkStatus();
    return (OrientGraph) ((OrientGraph) graphPool.getResource(iDatabase.getUser().getName() + "," + iDatabase.getURL(), Long.MAX_VALUE))
        .reuse(iDatabase);
  }

{'fraction_non_alphanumeric': 0.0951195219123506, 'fraction_numerical': 0.0033200531208499337, 'mean_word_length': 3.666537567776917, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5016038', 'n_tokens_mistral': 3462, 'n_tokens_neox': 3288, 'n_words': 795}