/* Generated By:JJTree: Do not edit this line. OProfileStorageStatement.java Version 4.3 */
/* JavaCCOptions:MULTI=true,NODE_USES_PARSER=false,VISITOR=true,TRACK_TOKENS=true,NODE_PREFIX=O,NODE_EXTENDS=,NODE_FACTORY=,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
package com.orientechnologies.orient.core.sql.parser;

import com.orientechnologies.common.listener.OProgressListener;
import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.db.ODatabaseDocumentInternal;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.orientechnologies.orient.core.storage.OStorage;
import com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage;
import com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic;

import java.util.Map;

public class OProfileStorageStatement extends OStatement {

  protected boolean on;

  public static final String KEYWORD_PROFILE = "PROFILE";

  public OProfileStorageStatement(int id) {
    super(id);
  }

  public OProfileStorageStatement(OrientSql p, int id) {
    super(p, id);
  }

  @Override public Object execute(OSQLAsynchQuery<ODocument> request, OCommandContext context, OProgressListener progressListener) {
    try {
      ODatabaseDocumentInternal db = getDatabase();

      final OStorage storage = db.getStorage();
      if (on) {
        // activate the profiler
        ((OAbstractPaginatedStorage) storage).startGatheringPerformanceStatisticForCurrentThread();
        ODocument result = new ODocument();
        result.field("result", "OK");
        request.getResultListener().result(result);
      } else {
        // stop the profiler and return the stats
        final OSessionStoragePerformanceStatistic performanceStatistic = ((OAbstractPaginatedStorage) storage)
            .completeGatheringPerformanceStatisticForCurrentThread();

        if (performanceStatistic != null)
          request.getResultListener().result(performanceStatistic.toDocument());
        else {
          ODocument result = new ODocument();
          result.field("result", "Error: profiling of storage was not started.");
          request.getResultListener().result(result);
        }

      }
      return getResult(request);
    } finally {
      if (request.getResultListener() != null) {
        request.getResultListener().end();
      }
    }
  }

  protected Object getResult(OSQLAsynchQuery<ODocument> request) {
    if (request instanceof OSQLSynchQuery)
      return ((OSQLSynchQuery<ODocument>) request).getResult();

    return null;
  }

  public void toString(Map<Object, Object> params, StringBuilder builder) {
    builder.append("PROFILE STORAGE ");
    builder.append(on ? "ON" : "OFF");
  }

  @Override public OProfileStorageStatement copy() {
    OProfileStorageStatement result = new OProfileStorageStatement(-1);
    result.on = on;
    return result;
  }

  @Override public boolean equals(Object o) {
    if (this == o)
      return true;
    if (o == null || getClass() != o.getClass())
      return false;

    OProfileStorageStatement that = (OProfileStorageStatement) o;

    if (on != that.on)
      return false;

    return true;
  }

  @Override public int hashCode() {
    return (on ? 1 : 0);
  }
}

/* JavaCC - OriginalChecksum=645887712797ae14a17820bfa944f78e (do not edit this line) */

@@ -550,28 +550,306 @@
 ore.
-record.impl.ODocumen
+db.ODatabaseInternal;%0Aimport com.orientechnologies.orient.core.record.impl.ODocument;%0Aimport com.orientechnologies.orient.core.sql.executor.OInternalResultSet;%0Aimport com.orientechnologies.orient.core.sql.executor.OResultInternal;%0Aimport com.orientechnologies.orient.core.sql.executor.OTodoResultSe
 t;%0Ai
@@ -1307,16 +1307,26 @@
 tends OS
+impleExecS
 tatement
@@ -1549,32 +1549,1174 @@
 er(p, id);%0A  %7D%0A%0A
+  //new execution logic%0A  @Override public OTodoResultSet executeSimple(OCommandContext ctx) %7B%0A    OResultInternal result = new OResultInternal();%0A    result.setProperty(%22operation%22, %22optimize databae%22);%0A%0A    OStorage storage = ((ODatabaseInternal) ctx.getDatabase()).getStorage();%0A%0A    if (on) %7B%0A      // activate the profiler%0A      ((OAbstractPaginatedStorage) storage).startGatheringPerformanceStatisticForCurrentThread();%0A      result.setProperty(%22value%22, %22on%22);%0A    %7D else %7B%0A      // stop the profiler and return the stats%0A      final OSessionStoragePerformanceStatistic performanceStatistic = ((OAbstractPaginatedStorage) storage)%0A          .completeGatheringPerformanceStatisticForCurrentThread();%0A%0A      result.setProperty(%22value%22, %22off%22);%0A      if (performanceStatistic != null) %7B%0A        result.setProperty(%22result%22, performanceStatistic.toDocument());%0A      %7D else %7B%0A        result.setProperty(%22result%22, %22error%22);%0A        result.setProperty(%22errorMessage%22, %22profiling of storage was not started%22);%0A      %7D%0A    %7D%0A%0A    OInternalResultSet rs = new OInternalResultSet();%0A    rs.add(result);%0A    return rs;%0A  %7D%0A%0A  //old execution logic%0A
   @Override publ

Implement PROFILE storage with the new executor