<commit_before>/*
 * Copyright 2010-2013 <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.server.distributed;

import com.orientechnologies.common.io.OFileUtils;
import com.orientechnologies.common.util.OCallable;
import com.orientechnologies.orient.core.Orient;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.storage.OStorage;
import com.orientechnologies.orient.core.storage.impl.local.paginated.OLocalPaginatedStorage;
import com.orientechnologies.orient.server.OServer;
import com.orientechnologies.orient.server.hazelcast.OHazelcastPlugin;
import com.orientechnologies.orient.server.network.protocol.binary.ONetworkProtocolBinary;
import com.tinkerpop.blueprints.impls.orient.OrientBaseGraph;
import com.tinkerpop.blueprints.impls.orient.OrientGraphFactory;

import java.io.File;
import java.io.IOException;

/**
 * Running server instance.
 * 
 * @author <NAME> (l.garulli--at--orientechnologies.com)
 */
public class ServerRun {
  protected final String serverId;
  protected String       rootPath;
  protected OServer      server;

  public ServerRun(final String iRootPath, final String serverId) {
    this.rootPath = iRootPath;
    this.serverId = serverId;
  }

  public static String getServerHome(final String iServerId) {
    return "target/server" + iServerId;
  }

  public static String getDatabasePath(final String iServerId, final String iDatabaseName) {
    return getServerHome(iServerId) + "/databases/" + iDatabaseName;
  }

  @Override
  public String toString() {
    return server.getDistributedManager().getLocalNodeName() + "(" + serverId + ")";
  }

  public OServer getServerInstance() {
    return server;
  }

  public String getServerId() {
    return serverId;
  }

  public String getBinaryProtocolAddress() {
    return server.getListenerByProtocol(ONetworkProtocolBinary.class).getListeningAddress(true);
  }

  public void deleteNode() {
    OFileUtils.deleteRecursively(new File(getServerHome()));
  }

  public boolean isActive() {
    return server.isActive();
  }

  public void crashServer() {
    if (server != null) {
      server.getClientConnectionManager().killAllChannels();
      ((OHazelcastPlugin) server.getDistributedManager()).getHazelcastInstance().getLifecycleService().terminate();
      server.shutdown();
    }
  }

  protected OrientBaseGraph createDatabase(final String iName) {
    return createDatabase(iName, null);
  }

  public OrientBaseGraph createDatabase(final String iName, final OCallable<Object, OrientGraphFactory> iCfgCallback) {
    String dbPath = getDatabasePath(iName);

    new File(dbPath).mkdirs();

    final OrientGraphFactory factory = new OrientGraphFactory("plocal:" + dbPath);
    if (factory.exists()) {
      System.out.println("Dropping previous database '" + iName + "' under: " + dbPath + "...");
      new ODatabaseDocumentTx("plocal:" + dbPath).open("admin", "admin").drop();
      OFileUtils.deleteRecursively(new File(dbPath));
    }

    if (iCfgCallback != null)
      iCfgCallback.call(factory);

    System.out.println("Creating database '" + iName + "' under: " + dbPath + "...");
    return factory.getNoTx();
  }

  public void copyDatabase(final String iDatabaseName, final String iDestinationDirectory) throws IOException {<commit_msg>review of database creation using ODocumentDatabaseTx instead of OrientGraphFactory<commit_after>/*
 * Copyright 2010-2013 <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.server.distributed;

import com.orientechnologies.common.io.OFileUtils;
import com.orientechnologies.common.util.OCallable;
import com.orientechnologies.orient.core.Orient;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.storage.OStorage;
import com.orientechnologies.orient.core.storage.impl.local.paginated.OLocalPaginatedStorage;
import com.orientechnologies.orient.server.OServer;
import com.orientechnologies.orient.server.hazelcast.OHazelcastPlugin;
import com.orientechnologies.orient.server.network.protocol.binary.ONetworkProtocolBinary;
import com.tinkerpop.blueprints.impls.orient.OrientBaseGraph;
import com.tinkerpop.blueprints.impls.orient.OrientGraphFactory;

import java.io.File;
import java.io.IOException;

/**
 * Running server instance.
 *
 * @author <NAME> (l.garulli--at--orientechnologies.com)
 */
public class ServerRun {
  protected final String  serverId;
  protected       String  rootPath;
  protected       OServer server;

  public ServerRun(final String iRootPath, final String serverId) {
    this.rootPath = iRootPath;
    this.serverId = serverId;
  }

  public static String getServerHome(final String iServerId) {
    return "target/server" + iServerId;
  }

  public static String getDatabasePath(final String iServerId, final String iDatabaseName) {
    return getServerHome(iServerId) + "/databases/" + iDatabaseName;
  }

  @Override
  public String toString() {
    return server.getDistributedManager().getLocalNodeName() + "(" + serverId + ")";
  }

  public OServer getServerInstance() {
    return server;
  }

  public String getServerId() {
    return serverId;
  }

  public String getBinaryProtocolAddress() {
    return server.getListenerByProtocol(ONetworkProtocolBinary.class).getListeningAddress(true);
  }

  public void deleteNode() {
    OFileUtils.deleteRecursively(new File(getServerHome()));
  }

  public boolean isActive() {
    return server.isActive();
  }

  public void crashServer() {
    if (server != null) {
      server.getClientConnectionManager().killAllChannels();
      ((OHazelcastPlugin) server.getDistributedManager()).getHazelcastInstance().getLifecycleService().terminate();
      server.shutdown();
    }
  }

  protected OrientBaseGraph createDatabase(final String iName) {
    return createDatabase(iName, null);
  }

  public OrientBaseGraph createDatabase(final String iName, final OCallable<Object, OrientGraphFactory> iCfgCallback) {
    String dbPath = getDatabasePath(iName);

    new File(dbPath).mkdirs();

    ODatabaseDocumentTx db = new ODatabaseDocumentTx("plocal:" + dbPath);
    if (db.exists()) {
      System.out.println("Dropping previous database '" + iName + "' under: " + dbPath + "...");
      db.open("admin", "admin").drop();
      OFileUtils.deleteRecursively(new File(dbPath));
    }

    final OrientGraphFactory factory = new OrientGraphFactory("plocal:" + dbPath);

    if (iCfgCallback != null) {
      iCfgCallback.call(factory);
    }

    System.out.println("Creating database '" + iName + "' under: " + dbPath + "...");
    return factory.getNoTx();
  }

  public void copyDatabase(final String iDatabaseName, final String iDestinationDirectory) throws IOException {
{'fraction_non_alphanumeric': 0.10551558752997602, 'fraction_numerical': 0.003029155622870125, 'mean_word_length': 4.427397260273972, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '7254210', 'n_tokens_mistral': 2325, 'n_tokens_neox': 2260, 'n_words': 547}