text:
package com.yang.eto1.CordovaPlugin.TransparentWebViewServicePlugin;

import org.json.JSONException;
import org.json.JSONObject;
import java.util.Random;

import com.red_folder.phonegap.plugin.backgroundservice.BackgroundService;

import de.appplant.cordova.plugin.localnotification.ClickActivity;

import android.annotation.TargetApi;
import android.view.WindowManager;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings;
import android.webkit.JavascriptInterface;
import android.os.Build;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.app.PendingIntent;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.ApplicationInfo;
import android.media.RingtoneManager;
import android.net.Uri;
import android.util.Log;

public class TransparentWebViewService extends BackgroundService {
    private static final String TAG = "TransparentWebViewService";
    private static WindowManager windowManager;
    private static WebView wv;
    private boolean isActivityBound = false;
    private JSONObject currentMsg;

	@Override
    public void onCreate(){
		super.onCreate();

		windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.TOP | Gravity.LEFT;
        params.x = 0;
        params.y = 0;
        params.width = 0;
        params.height = 0;

        wv = new WebView(this);
        wv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        final WebSettings settings = wv.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        //ApplicationInfo appInfo = this.getApplicationContext().getApplicationInfo();
        if (/*(appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0 &&*/
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            enableRemoteDebugging();
        }
        if ((Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)) {
            Log.e(TAG, "Disabled addJavascriptInterface() bridge since Android version is old.");
            return;
        }
        SystemExposedJsApi exposedJsApi = new SystemExposedJsApi();
        wv.addJavascriptInterface(exposedJsApi, "simpleCordova");

        wv.loadUrl("file:///android_asset/www/background.html");
        windowManager.addView(wv, params);
        Log.i(TAG, "service onCreate() finished, webview is started");
	}

    @Override
	public void onDestroy() {
        windowManager.removeView(wv);
        wv.destroy();
        super.onDestroy();
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    private void enableRemoteDebugging() {
        try {
            wv.setWebContentsDebuggingEnabled(true);
        } catch (IllegalArgumentException e) {
            Log.d(TAG, "You have one job! To turn on Remote Web Debugging! YOU HAVE FAILED! ");
            e.printStackTrace();
        }
    }

    private void showNotification(String title, String text, String customData){
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        Intent notifyIntent = new Intent(this, ClickActivity.class);
        Uri notificationRing = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        //must contain soundUri, or will print stack
        notifyIntent.putExtra("NOTIFICATION_OPTIONS", "{\"soundUri\":\""+notificationRing+"\", \"data\":"+customData+"}");
        notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        int requestCode = new Random().nextInt();
        PendingIntent notifyPendingIntent =
                PendingIntent.getActivity(
                this,
                requestCode,
                notifyIntent,
                PendingIntent.FLAG_CANCEL_CURRENT
        );
        builder.setContentIntent(notifyPendingIntent);
        builder.setAutoCancel(true);
        builder.setSmallIcon(this.getResources().getIdentifier("icon", "drawable", getPackageName()));
        builder.setContentTitle(title);
        builder.setContentText(text);
        builder.setTicker(title+": "+text);
        builder.setSound(notificationRing);
        NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify((int)System.currentTimeMillis(), builder.build());
    }

    @Override
    public IBinder onBind(Intent intent) {
        isActivityBound = true;
        Log.i(TAG, "main activity bound to service");
        return super.onBind(intent);
    }

    @Override
    public void onRebind (Intent intent){
        isActivityBound = true;
        Log.i(TAG, "main activity rebound to service");
    }

    @Override
    public boolean onUnbind(Intent intent){
        isActivityBound = false;
        Log.i(TAG, "main activity unbound to service");
        return true;
    }

    class SystemExposedJsApi {
        @JavascriptInterface
        public void showNotification(String title, String text, String customData){
            Log.i(TAG, "no activity, just service, so show notification, title is "+title+", text is "+text+", customData json is "+customData);
            TransparentWebViewService.this.showNotification(title, text, customData);
        }

        @JavascriptInterface
        public boolean isActivityBound(){
            return TransparentWebViewService.this.isActivityBound;
        }

        @JavascriptInterface
        public void onMessage(String json){
            try{
                TransparentWebViewService.this.currentMsg = new JSONObject(json);
            }catch(JSONException e){
                Log.e(TAG, "construct JSONObject from javascript msg error");
                return;
            }
            TransparentWebViewService.this.runOnce();
            setLatestResult(null);
        }
    }

    private void webviewExecuteJSInMainThread(final String js){
        wv.post(new Runnable(){
            @Override
            public void run() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    wv.evaluateJavascript(js, null);
                } else {
                    wv.loadUrl("javascript:" + js);
                }
            }
        });
    }

    @Override
    protected JSONObject doWork() {
        Log.i(TAG, "doWork return current msg");
        return currentMsg;
    }

    @Override
    protected JSONObject getConfig() {
       return null;
    }

    @Override
    protected void setConfig(JSONObject config) {
        //TODO: rework below to a general version
        String type;
        try{
            type = config.getString("type");
            Log.i(TAG, "got msg from main activity, type is "+type);
        }catch(JSONException e){
            Log.e(TAG, "msg from main activity error, no type field");
            return;
        }
        if(type.equals("LoginInfo")){
            String username;
            String password;
            String role;
            try{
                username = config.getString("username");
                password = config.getString("password");
                role = config.getString("role");
                Log.i(TAG, "got login msg from main activity, username is "+username+", password is "+password+", role is "+role);
            }catch(JSONException e){
                Log.e(TAG, "LoginInfo msg from main activity error");
                return;
            }
            webviewExecuteJSInMainThread("myEvents.emit(\"LoginInfo\", \""+username+"\", \""+password+"\", \""+role+"\");");
        }else if(type.equals("Logout")){
            webviewExecuteJSInMainThread("myEvents.emit(\"Logout\");");
        }else if(type.equals("ActivityStartingWhenServiceRunning")){
            webviewExecuteJSInMainThread("myEvents.emit(\"ActivityStarting\");");
        }else if(type.equals("Subscribe")){
            String topic;
            try{
                topic = config.getString("topic");
                Log.i(TAG, "got subscribe msg from main activity, topic is "+topic);
            }catch(JSONException e){
                Log.e(TAG, "Subscribe msg from main activity error");
                return;
            }
            webviewExecuteJSInMainThread("myEvents.emit(\"Subscribe\", \""+topic+"\");");
        }else if(type.equals("Publish")){
            String topic;
            JSONObject msg;
            try{
                topic = config.getString("topic");
                msg = config.getJSONObject("message");
                Log.i(TAG, "got publish msg from main activity, topic is "+topic+", msg is "+msg.toString());
            }catch(JSONException e){
                Log.e(TAG, "publish msg from main activity error");
                return;
            }
            webviewExecuteJSInMainThread("myEvents.emit(\"Publish\", \""+topic+"\", "+msg.toString()+");");
        }else if(type.equals("PublishReliably")){
            String topic;
            JSONObject msg;
            try{
                topic = config.getString("topic");
                msg = config.getJSONObject("message");
                Log.i(TAG, "got publish msg from main activity, topic is "+topic+", msg is "+msg.toString());
            }catch(JSONException e){
                Log.e(TAG, "publish msg from main activity error");
                return;
            }
            webviewExecuteJSInMainThread("myEvents.emit(\"PublishReliably\", \""+topic+"\", "+msg.toString()+");");
        }else{
            Log.w(TAG, "got msg from main activity, but type is unknown");
        }
    }

    @Override
    protected JSONObject initialiseLatestResult() {
       return null;
    }
}


text:
local C = {}

local ControllerMethods = {
	admin = true,
}

function C.new(o, req)
	o = o or {}
	o.req = req
	o.ControllerMethods = ControllerMethods
	local controller = require 'aimi.controller'
	local parent = controller:new()
	setmetatable(
		o,
		{
			__index = parent
		}
	)
	return o
end

function C:admin(params)
	if params[1] == 'logout' then
		self:_deleteAdminSession()
		ngx.redirect(self.config.auth.adminRedirectURI)
		return
	end
	local reqParams = self.req:params()
	local errorMessage = ''
	if reqParams.userid and reqParams.password then
		local adminBase = require 'models.admin_base'
		local base = adminBase:new()
		local rows = base:select(
			{
				'admin_id =', reqParams.userid,
				'password =', reqParams.password,
			}
		)
		if #rows > 0 then
			self:_setAdminSession(rows[1].admin_id)
			return ngx.redirect(self.config.auth.adminRedirectURI)
		end
		errorMessage = self.config.message.authError
	end

	self.stash = {
		title    = 'Admin > auth',
		authUri  = self.config.base.uri .. self.config.auth.uri .. '/admin',
		userid   = reqParams.userid,
		password = reqParams.password,
		message  = errorMessage,
	}
	self.templateFileName = 'auth/admin.html'
	return self
end

return C

redpajama_set_name: RedPajamaGithub


text:
class SkillsController < ApplicationController
  load_and_authorize_resource :course
  load_and_authorize_resource :skill, through: :course, shallow: true, except: [:presentation, :sort]

  before_action :set_skill,  only: [:edit, :update, :destroy, :presentation]
  before_action :set_course, only: [:new, :create, :sort]

  def show
    @skill = Skill.includes(projects: [:creator, :criteria, :completions], resources: :creator).find(params[:id])
  end

  def new
    @skill = @course.skills.build
  end

  def edit
  end

  def create
    @skill = @course.skills.build skill_params.merge(creator_id: current_user.id)

    respond_to do |format|
      if @skill.save
        format.html { redirect_to @skill, notice: 'Skill was successfully created.' }
      else
        format.html { render :new }
      end
    end

  rescue ActiveRecord::RecordInvalid => e
    flash[:error] = e.message
    render :new
  end

  def update
    respond_to do |format|
      if @skill.update(skill_params)
        format.html { redirect_to @skill, notice: 'Skill was successfully updated.' }
      else
        format.html { render :edit }
      end
    end

  rescue ActiveRecord::RecordInvalid => e
    flash[:error] = e.message
    render :edit
  end

  def destroy
    @skill.destroy
    respond_to do |format|
      format.html { redirect_to :back, notice: 'Skill was successfully deleted.' }
    end
  end

  def sort
    authorize! :sort_skills_for, @course
    params[:skill].each_with_index do |id, index|
      Skill.find(id).update_attribute(:position, index+1)
    end
    render nothing: true
  end

  def presentation
    authorize! :read, @skill
    render text: @skill.presentation, layout: 'presentation'
  end

private

  def set_skill
    @skill = Skill.find(params[:id])
  end

  def set_course
    @course = Course.find(params[:course_id])
  end

  def skill_params
    params.require(:skill).permit(:title, :presentation, :primary_language_id, :tag_list, :read_only, prereq_ids: [])
  end
end


text:
package org.apache.toree.kernel.interpreter.sql

import java.io.ByteArrayOutputStream

import org.apache.toree.interpreter.broker.BrokerService
import org.apache.toree.kernel.api.KernelLike
import org.apache.toree.kernel.interpreter.sql.SqlTypes._

import scala.concurrent.Future
import scala.tools.nsc.interpreter._

/**
 * Represents the service that provides the high-level interface between the
 * JVM and Spark SQL.
 *
 * @param kernel The SQL Context of Apache Spark to use to perform SQL
 *                   queries
 */
class SqlService(private val kernel: KernelLike) extends BrokerService {
  import scala.concurrent.ExecutionContext.Implicits.global

  @volatile private var _isRunning: Boolean = false
  override def isRunning: Boolean = _isRunning

  /**
   * Submits code to the broker service to be executed and return a result.
   *
   * @param code The code to execute
   *
   * @return The result as a future to eventually return
   */
  override def submitCode(code: Code, kernelOutputStream: Option[OutputStream]): Future[CodeResults] = Future {
    println(s"Executing: '${code.trim}'")
    val result = kernel.sparkSession.sql(code.trim)

    // TODO: There is an internal method used for show called showString that
    //       supposedly is only for the Python API, look into why
    val stringOutput = {
      val outputStream = new ByteArrayOutputStream()
      Console.withOut(outputStream) {
        // TODO: Provide some way to change the number of records shown
        result.show(10)
      }
      outputStream.toString("UTF-8")
    }

    stringOutput
  }

  /** Stops the running broker service. */
  override def stop(): Unit = _isRunning = false

  /** Starts the broker service. */
  override def start(): Unit = _isRunning = true
}


text:
class CreateCommitteeMembers < ActiveRecord::Migration[4.2]
  def change
    create_table :committee_members do |t|
      t.integer :committee_id
      t.integer :member_id

      t.timestamps null: false
    end

    add_index :committee_members, :committee_id
    add_index :committee_members, :member_id

    add_index :committee_members, [:committee_id, :member_id], unique: true
  end
end


text:
<?php

namespace Test\Phinx\Migration\Manager;

use Phinx\Db\Adapter\MysqlAdapter;
use Phinx\Migration\Manager\Environment;

class EnvironmentTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var \Phinx\Migration\Manager\Environment
     */
    private $environment;

    public function setUp()
    {
        $this->environment = new Environment('test', array());
    }

    public function testConstructorWorksAsExpected()
    {
        $env = new Environment('testenv', array('foo' => 'bar'));
        $this->assertEquals('testenv', $env->getName());
        $this->assertArrayHasKey('foo', $env->getOptions());
    }

    public function testSettingTheName()
    {
        $this->environment->setName('prod123');
        $this->assertEquals('prod123', $this->environment->getName());
    }

    public function testSettingOptions()
    {
        $this->environment->setOptions(array('foo' => 'bar'));
        $this->assertArrayHasKey('foo', $this->environment->getOptions());
    }

    /**
     * @expectedException \RuntimeException
     * @expectedExceptionMessage Invalid adapter specified: fakeadapter
     */
    public function testInvalidAdapter()
    {
        $this->environment->setOptions(array('adapter' => 'fakeadapter'));
        $this->environment->getAdapter();
    }

    /**
     * @expectedException \RuntimeException
     */
    public function testNoAdapter()
    {
        $this->environment->getAdapter();
    }

    public function testSchemaName()
    {
        $this->assertEquals('phinxlog', $this->environment->getSchemaTableName());

        $this->environment->setSchemaTableName('changelog');
        $this->assertEquals('changelog', $this->environment->getSchemaTableName());
    }

    public function testAdapterFactoryCreatesMysqlAdapter()
    {
        $this->environment->setOptions(array('adapter' => 'mysql'));
        $this->assertTrue($this->environment->getAdapter() instanceof \Phinx\Db\Adapter\MysqlAdapter);
    }

    public function testAdapterFactoryCreatesSqliteAdapter()
    {
        $this->environment->setOptions(array('adapter' => 'sqlite'));
        $this->assertTrue($this->environment->getAdapter() instanceof \Phinx\Db\Adapter\SQLiteAdapter);
    }

    public function testCurrentVersion()
    {
        $stub = $this->getMock('\Phinx\Db\Adapter\PdoAdapter', array(), array(array()));
        $stub->expects($this->any())
             ->method('getVersions')
             ->will($this->returnValue(array('20110301080000')));

        $this->environment->setAdapter($stub);

        $this->assertEquals('20110301080000', $this->environment->getCurrentVersion());
    }

    public function testExecutingAMigrationUp()
    {
        // stub adapter
        $adapterStub = $this->getMock('\Phinx\Db\Adapter\PdoAdapter', array(), array(array()));
        $adapterStub->expects($this->once())
                    ->method('migrated')
                    ->will($this->returnArgument(0));

        $this->environment->setAdapter($adapterStub);

        // up
        $upMigration = $this->getMock('\Phinx\Migration\AbstractMigration', array('up'), array('20110301080000'));
        $upMigration->expects($this->once())
                    ->method('up');

        $this->environment->executeMigration($upMigration, \Phinx\Migration\MigrationInterface::UP);
    }

    public function testExecutingAMigrationDown()
    {
        // stub adapter
        $adapterStub = $this->getMock('\Phinx\Db\Adapter\PdoAdapter', array(), array(array()));
        $adapterStub->expects($this->once())
                    ->method('migrated')
                    ->will($this->returnArgument(0));

        $this->environment->setAdapter($adapterStub);

        // down
        $downMigration = $this->getMock('\Phinx\Migration\AbstractMigration', array('down'), array('20110301080000'));
        $downMigration->expects($this->once())
                      ->method('down');

        $this->environment->executeMigration($downMigration, \Phinx\Migration\MigrationInterface::DOWN);
    }

    public function testExecutingAMigrationWithTransactions()
    {
        // stub adapter
        $adapterStub = $this->getMock('\Phinx\Db\Adapter\PdoAdapter', array(), array(array()));
        $adapterStub->expects($this->once())
                    ->method('beginTransaction');

        $adapterStub->expects($this->once())
                    ->method('commitTransaction');

        $adapterStub->expects($this->exactly(2))
                    ->method('hasTransactions')
                    ->will($this->returnValue(true));

        $this->environment->setAdapter($adapterStub);

        // migrate
        $migration = $this->getMock('\Phinx\Migration\AbstractMigration', array('up'), array('20110301080000'));
        $migration->expects($this->once())
                  ->method('up');

        $this->environment->executeMigration($migration, \Phinx\Migration\MigrationInterface::UP);
    }

    public function testExecutingAChangeMigrationUp()
    {
        // stub adapter
        $adapterStub = $this->getMock('\Phinx\Db\Adapter\PdoAdapter', array(), array(array()));
        $adapterStub->expects($this->once())
                    ->method('migrated')
                    ->will($this->returnArgument(0));

        $this->environment->setAdapter($adapterStub);

        // migration
        $migration = $this->getMock('\Phinx\Migration\AbstractMigration', array('change'), array('20130301080000'));
        $migration->expects($this->once())
                  ->method('change');

        $this->environment->executeMigration($migration, \Phinx\Migration\MigrationInterface::UP);
    }

    public function testExecutingAChangeMigrationDown()
    {
        // stub adapter
        $adapterStub = $this->getMock('\Phinx\Db\Adapter\PdoAdapter', array(), array(array()));
        $adapterStub->expects($this->once())
                    ->method('migrated')
                    ->will($this->returnArgument(0));

        $this->environment->setAdapter($adapterStub);

        // migration
        $migration = $this->getMock('\Phinx\Migration\AbstractMigration', array('change'), array('20130301080000'));
        $migration->expects($this->once())
                  ->method('change');

        $this->environment->executeMigration($migration, \Phinx\Migration\MigrationInterface::DOWN);
    }

    /**
     * Data provider for `::testAdapterRetrieval()`
     */
    public function goodAdapterProvider()
    {
        return array(
            array(
                'mysql',
                'Phinx\\Db\\Adapter\\MysqlAdapter',
            ),
            array(
                'pgsql',
                'Phinx\\Db\\Adapter\\PostgresAdapter',
            ),
            array(
                'sqlite',
                'Phinx\\Db\\Adapter\\SQLiteAdapter',
            ),
            array(
                'sqlsrv',
                'Phinx\\Db\\Adapter\\SqlServerAdapter',
            ),
            array(
                'custom',
                'Phinx\\Db\\Adapter\\MysqlAdapter', //Using Mysql adapter in place of a 'custom' adapter.
            ),
        );
    }

    /**
     * @dataProvider goodAdapterProvider
     */
    public function testAdapterFactories($adapterName, $adapterClass)
    {
        $env = new Environment('test-'.$adapterName, array());
        $env->registerAdapter('custom', function (Environment $env) {
            return new MysqlAdapter($env->getOptions(), $env->getOutput());
        });
        $env->setOptions(array('adapter' => $adapterName));
        $this->assertInstanceOf($adapterClass, $env->getAdapter(), 'Expected adapter provided to be instance of '.$adapterClass.'.');
    }

    /**
     * Data provider for `::testBadAdapterCalls()`
     */
    public function badAdapterProvider()
    {
        return array(
            array(
                'not-set',
                null,
            ),
            array(
                'not-callable',
                'not callable',
            ),
            array(
                'bad-adapter',
                function(){
                    return 'not an AdapterInterface';
                },
            ),
        );
    }

    /**
     * @dataProvider badAdapterProvider
     * @expectedException \RuntimeException
     */
    public function testBadAdapterFactories($adapterName, $adapter)
    {
        $env = new Environment('test-'.$adapterName, array());
        $env->registerAdapter($adapterName, $adapter);
        $env->setOptions(array('adapter' => $adapterName));
        $env->getAdapter();
    }
}


text:
module JohnDoe
  module Models
    class Location
      include Virtus.model

      attribute :street, String
      attribute :city,   String
      attribute :state,  String
      attribute :zip,    String
    end
  end
end


text:
import { Colors } from '../constants/Colors';
import { InspectorHeader } from './InspectorHeader';
import { InspectorHr } from './InspectorHr';
import { InspectorItem } from './InspectorItem';
import { minimizeData } from '../../minimizeData';
import { useSelector } from '../states/store';
import React, { useCallback, useState } from 'react';
import styled from 'styled-components';

// == styles =======================================================================================
const Value = styled.div`
  margin: 0.15rem;
  font-size: 0.7rem;
  line-height: 1em;
  white-space: nowrap;
`;

const CalculateButton = styled.div`
  margin: 4px auto 0;
  font-size: 0.8rem;
  line-height: 1.2rem;
  width: 128px;
  text-align: center;
  background: ${ Colors.back3 };
  cursor: pointer;

  &:hover {
    background: ${ Colors.back4 };
  }

  &:active {
    background: ${ Colors.back1 };
  }
`;

// == element ======================================================================================
export interface InspectorStatsProps {
  className?: string;
}

const InspectorStats = (): JSX.Element | null => {
  const [ filesize, setFilesize ] = useState<number | null>( null );
  const [ filesizeMin, setFilesizeMin ] = useState<number | null>( null );

  const {
    automaton,
    channelsCount,
    channelItemsCount,
    curvesCount,
    curvesLength,
    fxDefsCount,
  } = useSelector( ( state ) => ( {
    automaton: state.automaton.instance,
    channelsCount: Object.keys( state.automaton.channels ).length,
    channelItemsCount: Object.values( state.automaton.channels ).reduce(
      ( prev, channel ) => prev + Object.keys( channel.items ).length,
      0,
    ),
    curvesCount: Object.keys( state.automaton.curves ).length,
    curvesLength: Object.values( state.automaton.curves ).reduce(
      ( prev, curve ) => prev + curve.length,
      0,
    ),
    fxDefsCount: Object.keys( state.automaton.fxDefinitions ).length,
  } ) );

  const handleCalculateFilesize = useCallback(
    () => {
      if ( !automaton ) { return; }

      const serialized = automaton.serialize();
      setFilesize( JSON.stringify( serialized ).length );
      const minimizeOptions = {
        precisionTime: automaton.guiSettings.minimizedPrecisionTime,
        precisionValue: automaton.guiSettings.minimizedPrecisionValue
      };
      const minimized = minimizeData( serialized, minimizeOptions );
      setFilesizeMin( JSON.stringify( minimized ).length );
    },
    [ automaton ]
  );

  return ( automaton && <>
    <InspectorHeader text="Project Stats" />

    <InspectorHr />

    <InspectorItem
      name="Channels"
      description="Channels exists in this project."
    >
      <Value>
        { channelsCount.toLocaleString() }
      </Value>
    </InspectorItem>

    <InspectorItem
      name="Channel Items"
      description="Items of channels exists in this project."
    >
      <Value>
        { channelItemsCount.toLocaleString() }
      </Value>
    </InspectorItem>

    <InspectorItem
      name="Curves"
      description="Curves exists in this project."
    >
      <Value>
        { curvesCount.toLocaleString() }
      </Value>
    </InspectorItem>

    <InspectorItem
      name="Curves Length"
      description="Total length of curves."
    >
      <Value>
        { `${ curvesLength.toLocaleString() } sec` }
      </Value>
    </InspectorItem>

    <InspectorItem
      name="Fx Definitions"
      description="Fx definitions loaded in this project."
    >
      <Value>
        { fxDefsCount.toLocaleString() }
      </Value>
    </InspectorItem>

    <InspectorHr />

    <InspectorItem
      name="Filesize"
      description="The size of its serialized data."
    >
      <Value>
        { filesize ? `${ filesize.toLocaleString() } bytes` : '----' }
      </Value>
    </InspectorItem>

    <InspectorItem
      name="Filesize (min)"
      description="The size of its minimized serialized data."
    >
      <Value>
        { filesizeMin ? `${ filesizeMin.toLocaleString() } bytes` : '----' }
      </Value>
    </InspectorItem>

    <CalculateButton
      data-stalker="Calculate the size of its serialized data."
      onClick={ handleCalculateFilesize }
    >
      Calculate Filesize
    </CalculateButton>
  </> ) ?? null;
};

export { InspectorStats };


text:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <version>1.0</version>
    <name>key_server</name>
    <artifactId>key_server</artifactId>
    <groupId>edu.sdsu.its.key_server</groupId>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.0-801.jdbc4</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.17</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.3.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-jersey2-jaxrs</artifactId>
            <version>1.5.0</version>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <groupId>javax.ws.rs</groupId>
                    <artifactId>jsr311-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.5.2</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>com.heroku.sdk</groupId>
                <artifactId>heroku-maven-plugin</artifactId>
                <version>0.5.3</version>
                <configuration>
                    <appName>its-key-server</appName>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>


text:
package compute.disks;

// [START compute_disk_list]

import com.google.cloud.compute.v1.Disk;
import com.google.cloud.compute.v1.DisksClient;
import com.google.cloud.compute.v1.ListDisksRequest;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

public class ListDisks {

  public static void main(String[] args)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample.

    // Project ID or project number of the Cloud project you want to use.
    String projectId = "YOUR_PROJECT_ID";

    // The zone where the disks are located.
    String zone = "europe-central2-b";

    // Filter to be applied when listing disks. Learn more about filters here:
    // https://cloud.google.com/python/docs/reference/compute/latest/google.cloud.compute_v1.types.ListDisksRequest
    String filter = "FILTER_CONDITION";

    listDisks(projectId, zone, filter);
  }

  // Lists disks from a project.
  public static void listDisks(String projectId, String zone, String filter)
      throws IOException {

    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the `disksClient.close()` method on the client to safely
    // clean up any remaining background resources.
    try (DisksClient disksClient = DisksClient.create()) {

      // Create the request object.
      ListDisksRequest listDisksRequest = ListDisksRequest.newBuilder()
          .setProject(projectId)
          .setZone(zone)
          .setFilter(filter)
          .build();

      System.out.println("List of disks:");
      for (Disk disk : disksClient.list(listDisksRequest).iterateAll()) {
        System.out.println(disk.getName());
      }
    }
  }
}
// [END compute_disk_list]


text:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting.Internal;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace ZDevTools.Wpf
{
    /// <summary>
    /// This hosts a WPF service, making sure the lifecycle is managed
    /// </summary>
    class WpfHostedService<TShell> : IHostedService
        where TShell : Window
    {
        readonly ILogger<WpfHostedService<TShell>> Logger;
        readonly IServiceProvider ServiceProvider;
        readonly IOptions<StartupOptions> Options;

        /// <summary>
        /// The constructor which takes all the DI objects
        /// </summary>
        /// <param name="logger">ILogger</param>
        /// <param name="serviceProvider">IServiceProvider</param>
        /// <param name="options"></param>
        public WpfHostedService(ILogger<WpfHostedService<TShell>> logger, IServiceProvider serviceProvider, IOptions<StartupOptions> options)
        {
            Logger = logger;
            ServiceProvider = serviceProvider;
            Options = options;
        }

        public bool IsRunning { get; private set; }

        Dispatcher _dispatcher;
        Application _application;
        public Task StartAsync(CancellationToken cancellationToken)
        {
            Thread thread = new Thread(state =>
             {
                 // Create our SynchronizationContext, and install it:
                 SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext(_dispatcher = Dispatcher.CurrentDispatcher));

                 if (Options.Value.IsUseSplashScreen)
                 {
                     SplashScreen splashScreen = new SplashScreen(Options.Value.SplashScreenResourceName);
                     splashScreen.Show(autoClose: true);
                 }

                 // Create the new WPF application
                 _application = ServiceProvider.GetService<Application>() ?? new Application();
                 // Shutdown Mode OnMainWindowClose
                 _application.ShutdownMode = ShutdownMode.OnMainWindowClose;
                 // Register to the WPF application exit to stop the host application
                 _application.Exit += (s, e) =>
                  {
                      IsRunning = false;

                      ServiceProvider.GetService<IHostApplicationLifetime>().StopApplication();
                  };

                 IsRunning = true;

                 // Run the WPF application in this thread which was specifically created for it, with the specified shell
                 if (ServiceProvider.GetRequiredService<TShell>() is Window wpfShell)
                 {
                     _application.Run(wpfShell);
                 }
                 else
                 {
                     throw new ApplicationException("未能找到Shell。");
                 }
             })
            { IsBackground = true };

            thread.SetApartmentState(ApartmentState.STA);
            // Start the new WPF thread
            thread.Start();

            // Make the UI thread go
            return Task.CompletedTask;
        }

        public async Task StopAsync(CancellationToken cancellationToken)
        {
            var isConsoleLifetime = ServiceProvider.GetService<IHostLifetime>() is ConsoleLifetime;
            if (IsRunning && !isConsoleLifetime) //由于ConsoleLifetime控制台会导致后面一句出错,因此不让其调用了。
            {
                Logger.LogDebug("Stopping WPF due to application exit.");
                // Stop application
                await _dispatcher.InvokeAsync(() => _application.Shutdown());
            }
        }
    }
}


text: Files for Learning TypeScript


text:
package org.kie.workbench.common.services.backend.source;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.inject.Instance;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.uberfire.java.nio.file.Files;
import org.uberfire.java.nio.file.Path;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

public class SourceServicesImplTest {

    private Instance instance;
    private List<SourceService> sourceServices;
    private List<Path> pathsToDelete;

    @Before
    public void setUp() throws Exception {
        instance = mock(Instance.class);
        sourceServices = new ArrayList<SourceService>();
        pathsToDelete = new ArrayList<Path>();
    }

    @After
    public void clearDown() {
        for (final Path p : pathsToDelete) {
            Files.delete(p);
        }
    }

    @Test
    public void testSomethingSimple() throws Exception {
        addToList(getSourceService(".drl"));
        assertTrue(new SourceServicesImpl(instance).hasServiceFor(makePath("myFile",
                ".drl")));
    }

    @Test
    public void testMissing() throws Exception {
        addToList(getSourceService(".notHere"));
        assertFalse(new SourceServicesImpl(instance).hasServiceFor(makePath("myFile",
                ".drl")));
    }

    @Test
    public void testShorter() throws Exception {
        SourceService DRL = getSourceService(".drl");
        SourceService modelDRL = getSourceService(".model.drl");
        addToList(DRL, modelDRL);

        assertEquals(DRL, new SourceServicesImpl(instance).getServiceFor(makePath("myFile",
                ".drl")));

        sourceServices.clear();

        addToList(modelDRL, DRL);

        assertEquals(DRL, new SourceServicesImpl(instance).getServiceFor(makePath("myFile",
                ".drl")));
    }

    @Test
    public void testLonger() throws Exception {
        SourceService DRL = getSourceService(".drl");
        SourceService modelDRL = getSourceService(".model.drl");
        addToList(DRL,
                modelDRL);

        assertEquals(modelDRL, new SourceServicesImpl(instance).getServiceFor(makePath("myFile",
                ".model.drl")));

        sourceServices.clear();

        addToList(modelDRL,
                DRL);

        assertEquals(modelDRL, new SourceServicesImpl(instance).getServiceFor(makePath("myFile",
                ".model.drl")));
    }

    private SourceService getSourceService(final String extension) {
        return new SourceService() {

            @Override
            public boolean accepts(final Path path) {
                final String uri = path.toUri().toString();
                return uri.substring(uri.length() - extension.length()).equals(extension);
            }

            @Override
            public String getSource(Path path, Object model) {
                return null;
            }

            @Override
            public String getSource(Path path) {
                return null;
            }

            @Override
            public String getPattern() {
                return extension;
            }
        };
    }

    private void addToList(SourceService... services) {

        for (SourceService service : services) {
            sourceServices.add(service);
        }

        when(
                instance.iterator()
        ).thenReturn(
                sourceServices.iterator()
        );
    }

    private Path makePath(final String prefix,
                          final String suffix) throws URISyntaxException {
        Path path = mock(Path.class);

        when(
                path.toUri()
        ).thenReturn(
                new URI(prefix + suffix)
        );
        return path;
    }

}


text:
module PgHero
  class HomeController < ActionController::Base
    layout "pg_hero/application"

    protect_from_forgery with: :exception

    http_basic_authenticate_with name: PgHero.username, password: PgHero.password if PgHero.password

    before_action :check_api
    before_action :set_database
    before_action :set_query_stats_enabled
    before_action :set_show_details, only: [:index, :queries, :show_query]
    before_action :ensure_query_stats, only: [:queries]

    if PgHero.config["override_csp"]
      # note: this does not take into account asset hosts
      # which can be a string with %d or a proc
      # https://api.rubyonrails.org/classes/ActionView/Helpers/AssetUrlHelper.html
      # users should set CSP manually if needed
      # see https://github.com/ankane/pghero/issues/297
      after_action do
        response.headers["Content-Security-Policy"] = "default-src 'self' 'unsafe-inline'"
      end
    end

    def index
      @title = "Overview"
      @extended = params[:extended]

      if @replica
        @replication_lag = @database.replication_lag
        @good_replication_lag = @replication_lag ? @replication_lag < 5 : true
      else
        @inactive_replication_slots = @database.replication_slots.select { |r| !r[:active] }
      end

      @walsender_queries, long_running_queries = @database.long_running_queries.partition { |q| q[:backend_type] == "walsender" }
      @autovacuum_queries, @long_running_queries = long_running_queries.partition { |q| q[:query].starts_with?("autovacuum:") }

      connection_states = @database.connection_states
      @total_connections = connection_states.values.sum
      @idle_connections = connection_states["idle in transaction"].to_i

      @good_total_connections = @total_connections < @database.total_connections_threshold
      @good_idle_connections = @idle_connections < 100

      @transaction_id_danger = @database.transaction_id_danger(threshold: 1500000000)

      @readable_sequences, @unreadable_sequences = @database.sequences.partition { |s| s[:readable] }

      @sequence_danger = @database.sequence_danger(threshold: (params[:sequence_threshold] || 0.9).to_f, sequences: @readable_sequences)

      @indexes = @database.indexes
      @invalid_indexes = @database.invalid_indexes(indexes: @indexes)
      @invalid_constraints = @database.invalid_constraints
      @duplicate_indexes = @database.duplicate_indexes(indexes: @indexes)

      if @query_stats_enabled
        @query_stats = @database.query_stats(historical: true, start_at: 3.hours.ago)
        @slow_queries = @database.slow_queries(query_stats: @query_stats)
        set_suggested_indexes((params[:min_average_time] || 20).to_f, (params[:min_calls] || 50).to_i)
      else
        @query_stats_available = @database.query_stats_available?
        @query_stats_extension_enabled = @database.query_stats_extension_enabled? if @query_stats_available
        @suggested_indexes = []
      end

      if @extended
        @index_hit_rate = @database.index_hit_rate || 0
        @table_hit_rate = @database.table_hit_rate || 0
        @good_cache_rate = @table_hit_rate >= @database.cache_hit_rate_threshold / 100.0 && @index_hit_rate >= @database.cache_hit_rate_threshold / 100.0
        @unused_indexes = @database.unused_indexes(max_scans: 0)
      end

      @show_migrations = PgHero.show_migrations
    end

    def space
      @title = "Space"
      @days = (params[:days] || 7).to_i
      @database_size = @database.database_size
      @only_tables = params[:tables].present?
      @relation_sizes = @only_tables ? @database.table_sizes : @database.relation_sizes
      @space_stats_enabled = @database.space_stats_enabled? && !@only_tables
      if @space_stats_enabled
        space_growth = @database.space_growth(days: @days, relation_sizes: @relation_sizes)
        @growth_bytes_by_relation = space_growth.to_h { |r| [[r[:schema], r[:relation]], r[:growth_bytes]] }
        if params[:sort] == "growth"
          @relation_sizes.sort_by! { |r| s = @growth_bytes_by_relation[[r[:schema], r[:relation]]]; [s ? 0 : 1, -s.to_i, r[:schema], r[:relation]] }
        end
      end

      if params[:sort] == "name"
        @relation_sizes.sort_by! { |r| r[:relation] || r[:table] }
      end

      @header_options = @only_tables ? {tables: "t"} : {}

      across = params[:across].to_s.split(",")
      @unused_indexes = @database.unused_indexes(max_scans: 0, across: across)
      @unused_index_names = Set.new(@unused_indexes.map { |r| r[:index] })
      @show_migrations = PgHero.show_migrations
      @system_stats_enabled = @database.system_stats_enabled?
      @index_bloat = [] # @database.index_bloat
    end

    def relation_space
      @schema = params[:schema] || "public"
      @relation = params[:relation]
      @title = @relation
      relation_space_stats = @database.relation_space_stats(@relation, schema: @schema)
      @chart_data = [{name: "Value", data: relation_space_stats.map { |r| [r[:captured_at].change(sec: 0), r[:size_bytes].to_i] }, library: chart_library_options}]
    end

    def index_bloat
      @title = "Index Bloat"
      @index_bloat = @database.index_bloat
      @show_sql = params[:sql]
    end

    def live_queries
      @title = "Live Queries"
      @running_queries = @database.running_queries(all: true)
      @vacuum_progress = @database.vacuum_progress.index_by { |q| q[:pid] }

      if params[:state]
        @running_queries.select! { |q| q[:state] == params[:state] }
      end
    end

    def queries
      @title = "Queries"
      @sort = %w(average_time calls).include?(params[:sort]) ? params[:sort] : nil
      @min_average_time = params[:min_average_time] ? params[:min_average_time].to_i : nil
      @min_calls = params[:min_calls] ? params[:min_calls].to_i : nil

      if @historical_query_stats_enabled
        begin
          @start_at = params[:start_at] ? Time.zone.parse(params[:start_at]) : 24.hours.ago
          @end_at = Time.zone.parse(params[:end_at]) if params[:end_at]
        rescue
          @error = true
        end
      end

      @query_stats =
        if @historical_query_stats_enabled && !request.xhr?
          []
        else
          @database.query_stats(
            historical: true,
            start_at: @start_at,
            end_at: @end_at,
            sort: @sort,
            min_average_time: @min_average_time,
            min_calls: @min_calls
          )
        end

      @indexes = @database.indexes
      set_suggested_indexes

      # fix back button issue with caching
      response.headers["Cache-Control"] = "must-revalidate, no-store, no-cache, private"
      if request.xhr?
        render layout: false, partial: "queries_table", locals: {queries: @query_stats, xhr: true}
      end
    end

    def show_query
      @query_hash = params[:query_hash].to_i
      @user = params[:user].to_s
      @title = @query_hash

      stats = @database.query_stats(historical: true, query_hash: @query_hash, start_at: 24.hours.ago).find { |qs| qs[:user] == @user }
      if stats
        @query = stats[:query]
        @explainable_query = stats[:explainable_query]

        if @show_details
          query_hash_stats = @database.query_hash_stats(@query_hash, user: @user)

          @chart_data = [{name: "Value", data: query_hash_stats.map { |r| [r[:captured_at].change(sec: 0), (r[:total_minutes] * 60 * 1000).round] }, library: chart_library_options}]
          @chart2_data = [{name: "Value", data: query_hash_stats.map { |r| [r[:captured_at].change(sec: 0), r[:average_time].round(1)] }, library: chart_library_options}]
          @chart3_data = [{name: "Value", data: query_hash_stats.map { |r| [r[:captured_at].change(sec: 0), r[:calls]] }, library: chart_library_options}]

          @origins = query_hash_stats.group_by { |r| r[:origin].to_s }.to_h { |k, v| [k, v.size] }
          @total_count = query_hash_stats.size
        end

        @tables = PgQuery.parse(@query).tables rescue []
        @tables.sort!

        if @tables.any?
          @row_counts = @database.table_stats(table: @tables).to_h { |i| [i[:table], i[:estimated_rows]] }
          @indexes_by_table = @database.indexes.group_by { |i| i[:table] }
        end
      else
        render_text "Unknown query", status: :not_found
      end
    end

    def system
      @title = "System"
      @periods = {
        "1 hour" => {duration: 1.hour, period: 60.seconds},
        "1 day" => {duration: 1.day, period: 10.minutes},
        "1 week" => {duration: 1.week, period: 30.minutes},
        "2 weeks" => {duration: 2.weeks, period: 1.hours}
      }
      if @database.system_stats_provider == :azure
        # doesn't support 10, just 5 and 15
        @periods["1 day"][:period] = 15.minutes
      end

      @duration = (params[:duration] || 1.hour).to_i
      @period = (params[:period] || 60.seconds).to_i

      if @duration / @period > 1440
        render_text "Too many data points", status: :bad_request
      elsif @period % 60 != 0
        render_text "Period must be a multiple of 60", status: :bad_request
      end
    end

    def cpu_usage
      render json: [{name: "CPU", data: @database.cpu_usage(**system_params).map { |k, v| [k, v ? v.round : v] }, library: chart_library_options}]
    end

    def connection_stats
      render json: [{name: "Connections", data: @database.connection_stats(**system_params), library: chart_library_options}]
    end

    def replication_lag_stats
      render json: [{name: "Lag", data: @database.replication_lag_stats(**system_params), library: chart_library_options}]
    end

    def load_stats
      stats =
        case @database.system_stats_provider
        when :azure
          [
            {name: "IO Consumption", data: @database.azure_stats("io_consumption_percent", **system_params), library: chart_library_options}
          ]
        when :gcp
          [
            {name: "Read Ops", data: @database.read_iops_stats(**system_params).map { |k, v| [k, v ? v.round : v] }, library: chart_library_options},
            {name: "Write Ops", data: @database.write_iops_stats(**system_params).map { |k, v| [k, v ? v.round : v] }, library: chart_library_options}
          ]
        else
          [
            {name: "Read IOPS", data: @database.read_iops_stats(**system_params).map { |k, v| [k, v ? v.round : v] }, library: chart_library_options},
            {name: "Write IOPS", data: @database.write_iops_stats(**system_params).map { |k, v| [k, v ? v.round : v] }, library: chart_library_options}
          ]
        end
      render json: stats
    end

    def free_space_stats
      render json: [
        {name: "Free Space", data: @database.free_space_stats(duration: 14.days, period: 1.hour), library: chart_library_options},
      ]
    end

    def explain
      @title = "Explain"
      @query = params[:query]
      # TODO use get + token instead of post so users can share links
      # need to prevent CSRF and DoS
      if request.post? && @query
        begin
          prefix =
            case params[:commit]
            when "Analyze"
              "ANALYZE "
            when "Visualize"
              "(ANALYZE, COSTS, VERBOSE, BUFFERS, FORMAT JSON) "
            else
              ""
            end
          @explanation = @database.explain("#{prefix}#{@query}")
          @suggested_index = @database.suggested_indexes(queries: [@query]).first if @database.suggested_indexes_enabled?
          @visualize = params[:commit] == "Visualize"
        rescue ActiveRecord::StatementInvalid => e
          @error = e.message

          if @error.include?("bind message supplies 0 parameters")
            @error = "Can't explain queries with bind parameters"
          end
        end
      end
    end

    def tune
      @title = "Tune"
      @settings = @database.settings
      @autovacuum_settings = @database.autovacuum_settings if params[:autovacuum]
    end

    def connections
      @title = "Connections"
      connections = @database.connections

      @total_connections = connections.count
      @connection_sources = group_connections(connections, [:database, :user, :source, :ip])
      @connections_by_database = group_connections_by_key(connections, :database)
      @connections_by_user = group_connections_by_key(connections, :user)

      if params[:security] && @database.server_version_num >= 90500
        connections.each do |connection|
          connection[:ssl_status] =
            if connection[:ssl]
              # no way to tell if client used verify-full
              # so connection may not be actually secure
              "SSL"
            else
              # variety of reasons for no SSL
              if !connection[:database].present?
                "Internal Process"
              elsif !connection[:ip]
                if connection[:state]
                  "Socket"
                else
                  # tcp or socket, don't have permission to tell
                  "No SSL"
                end
              else
                # tcp
                # could separate out localhost since this should be safe
                "No SSL"
              end
            end
        end

        @connections_by_ssl_status = group_connections_by_key(connections, :ssl_status)
      end
    end

    def maintenance
      @title = "Maintenance"
      @maintenance_info = @database.maintenance_info
      @time_zone = PgHero.time_zone
      @show_dead_rows = params[:dead_rows]
    end

    def kill
      if @database.kill(params[:pid])
        redirect_backward notice: "Query killed"
      else
        redirect_backward notice: "Query no longer running"
      end
    end

    def kill_long_running_queries
      @database.kill_long_running_queries
      redirect_backward notice: "Queries killed"
    end

    def kill_all
      @database.kill_all
      redirect_backward notice: "Connections killed"
    end

    def enable_query_stats
      @database.enable_query_stats
      redirect_backward notice: "Query stats enabled"
    rescue ActiveRecord::StatementInvalid
      redirect_backward alert: "The database user does not have permission to enable query stats"
    end

    def reset_query_stats
      success =
        if @database.server_version_num >= 120000
          @database.reset_query_stats
        else
          @database.reset_instance_query_stats
        end

      if success
        redirect_backward notice: "Query stats reset"
      else
        redirect_backward alert: "The database user does not have permission to reset query stats"
      end
    end

    protected

    def redirect_backward(options = {})
      redirect_back fallback_location: root_path, **options
    end

    def set_database
      @databases = PgHero.databases.values
      if params[:database]
        # don't do direct lookup, since you don't want to call to_sym on user input
        @database = @databases.find { |d| d.id == params[:database] }
      elsif @databases.size > 1
        redirect_to url_for(controller: controller_name, action: action_name, database: @databases.first.id)
      else
        @database = @databases.first
      end
    end

    def default_url_options
      {database: params[:database]}
    end

    def set_query_stats_enabled
      @query_stats_enabled = @database.query_stats_enabled?
      @system_stats_enabled = @database.system_stats_enabled?
      @replica = @database.replica?
    end

    def set_suggested_indexes(min_average_time = 0, min_calls = 0)
      @suggested_indexes_by_query =
        if @database.suggested_indexes_enabled?
          @database.suggested_indexes_by_query(query_stats: @query_stats.select { |qs| qs[:average_time] >= min_average_time && qs[:calls] >= min_calls })
        else
          {}
        end

      @suggested_indexes = @database.suggested_indexes(suggested_indexes_by_query: @suggested_indexes_by_query, indexes: @indexes)
      @query_stats_by_query = @query_stats.index_by { |q| q[:query] }
      @debug = params[:debug].present?
    end

    def system_params
      {
        duration: params[:duration],
        period: params[:period],
        series: true
      }.delete_if { |_, v| v.nil? }
    end

    def chart_library_options
      {pointRadius: 0, pointHoverRadius: 0, pointHitRadius: 5, borderWidth: 4}
    end

    def set_show_details
      @historical_query_stats_enabled = @query_stats_enabled && @database.historical_query_stats_enabled?
      @show_details = @historical_query_stats_enabled && @database.supports_query_hash?
    end

    def group_connections(connections, keys)
      connections
        .group_by { |conn| conn.slice(*keys) }
        .map { |k, v| k.merge(total_connections: v.count) }
        .sort_by { |v| [-v[:total_connections]] + keys.map { |k| v[k].to_s } }
    end

    def group_connections_by_key(connections, key)
      group_connections(connections, [key]).map { |v| [v[key], v[:total_connections]] }.to_h
    end

    def check_api
      if Rails.application.config.try(:api_only)
        render_text "No support for Rails API. See https://github.com/pghero/pghero for a standalone app.", status:  :internal_server_error
      end
    end

    def render_text(message, status:)
      render plain: message, status: status
    end

    def ensure_query_stats
      unless @query_stats_enabled
        redirect_to root_path, alert: "Query stats not enabled"
      end
    end
  end
end


text:
"""
Dialog interperter.
"""
__author__ = "Mark Birger"
__date__ = "19 Nov 2014"
__version__ = "0.3"

from dialog import STORAGEPATH
from dialog.parser import Parser
from dialog.scope import Scope
from dialog.returns import Returns
import dialog.link_parser as link_parser

import multiprocessing
# import sys
import argparse
import os
import signal
import sys
from socket import *

# USE_EVAL = False
# TTS = "att"

THRESHOLD = 0.2

class Dialog:
    """
    Dialog interperter class.
    """
    def __init__(self, scope, storage="./"):

        global STORAGEPATH
        STORAGEPATH = storage + STORAGEPATH
        STORAGEPATH = os.path.abspath(STORAGEPATH)
        print("Data storage path:", STORAGEPATH)
        if not os.path.exists(STORAGEPATH):
            os.makedirs(STORAGEPATH)
        if not os.path.exists(STORAGEPATH + "/answers/"):
            os.makedirs(STORAGEPATH + "/answers/")
        if not os.path.exists(STORAGEPATH + "/questions/"):
            os.makedirs(STORAGEPATH + "/questions/")

        self.expected = []
        self.scope = Scope(scope)
        self.returns = Returns()
        signal.signal(signal.SIGINT, self.interrupt_handler)

    def load(self, filename):
        """
        Loads dialog from the file.
        Uses Parser module.
        """
        parser = Parser(filename, self.scope, self.returns)
        self.expected.extend(parser.result())

    def _extend_expected(self, quesitons):
        """
        Method extends set of expected phrases.
        """
        # TODO: more elegant way to extends expected questions
        expected_strings = [str(x) for x in self.expected]
        for new in quesitons:
            if not str(new) in expected_strings:
                print("+\t%s"%(new))
                self.expected.append(new)

    def start(self):
        """
        Symlink for start_text().
        """
        print("Dialog system started in text mode by default.")
        return self.start_text()

    def interpret(self, input_phrase):
        states_probability = []
        for state in self.expected:
            # print(state, state.compare(input_phrase))
            states_probability.append((state, state.compare(input_phrase)))
        states_probability = sorted(states_probability, key=lambda x: (x[1][0][0], x[1][0][1]), reverse=True)
        allowed_states_probability = []
        no_separator = True
        print("======")
        for state in states_probability:
            if len(state[1][1]):
                rejected = "R"
                due = "(%s)" % state[1][1][0]
            else:
                rejected = " "
                due = ""
                allowed_states_probability.append(state)
            if no_separator and state[1][0][0] < THRESHOLD:
                no_separator = False
                print("------")
            print("%.2f [%i] %s  %s %s" % (state[1][0][0], state[1][0][1], rejected, state[0], due))
        print("======")
        if not len(allowed_states_probability) or \
            allowed_states_probability[0][1][0][0] < THRESHOLD:
            print("Bot> ???")
            return None
        return allowed_states_probability[0][0]

    def start_spoken(self):
        """
        Interprets dialog with natural speech.
        """
        import dialog.speech as speech
        occupation = multiprocessing.Event()
        listener_queue = multiprocessing.Queue(maxsize=0)
        recognizer_queue = multiprocessing.Queue(maxsize=0)
        speaker_queue = multiprocessing.Queue(maxsize=0)
        speaker = multiprocessing.Process(
            target=speech.speaker,
            args=(occupation, speaker_queue, ))
        recognizer = multiprocessing.Process(
            target=speech.recognizer,
            args=(recognizer_queue, listener_queue, ))
        listener = multiprocessing.Process(
            target=speech.listener,
            args=(occupation, recognizer_queue, ))
        speaker.start()
        recognizer.start()
        listener.start()
        occupation.set()
        print("======")
        for state in self.expected:
            print("\t    %s" % (state))
        print("======")
        while True:
            # process routines answers
            answers = self.returns.get_returns()
            for answer in answers:
                tosay, questions = answer.accept()
                speaker_queue.put(tosay)
                print("Bot> "+tosay)
                self._extend_expected(questions)
            # process input
            if not listener_queue.empty():
                input_phrase = listener_queue.get()
                input_phrase = link_parser.parse(input_phrase)
                state = self.interpret(input_phrase)
                if state:
                    tosay, questions = state.accept(input_phrase)
                    for answer in tosay:
                        if answer != "":
                            speaker_queue.put(answer)
                            print("Bot> "+answer)
                    self._extend_expected(questions)

    def start_offline(self):
        """
        Interprets dialog with offline TTS and STT.
        """
        import dialog.speech as speech
        occupation = multiprocessing.Event()
        listener_queue = multiprocessing.Queue(maxsize=0)
        recognizer_queue = multiprocessing.Queue(maxsize=0)
        speaker_queue = multiprocessing.Queue(maxsize=0)
        speaker = multiprocessing.Process(
            target=speech.speaker_pico,
            args=(occupation, speaker_queue, ))
        recognizer = multiprocessing.Process(
            target=speech.recognizer_sphinx,
            args=(recognizer_queue, listener_queue, ))
        listener = multiprocessing.Process(
            target=speech.listener,
            args=(occupation, recognizer_queue, ))
        speaker.start()
        recognizer.start()
        listener.start()
        occupation.set()
        print("======")
        for state in self.expected:
            print("\t    %s" % (state))
        print("======")
        while True:
            # process routines answers
            answers = self.returns.get_returns()
            for answer in answers:
                tosay, questions = answer.accept()
                speaker_queue.put(tosay)
                print("Bot> "+tosay)
                self._extend_expected(questions)
            # process input
            if not listener_queue.empty():
                input_phrase = listener_queue.get()
                input_phrase = link_parser.parse(input_phrase)
                state = self.interpret(input_phrase)
                if state:
                    tosay, questions = state.accept(input_phrase)
                    for answer in tosay:
                        if answer != "":
                            speaker_queue.put(answer)
                            print("Bot> "+answer)
                    self._extend_expected(questions)

    def start_text(self):
        """
        Interprets dialog in text mode.
        """
        print("======")
        for state in self.expected:
            print("\t    %s" % (state))
        print("======")
        while True:
            # process routines answers
            answers = self.returns.get_returns()
            for answer in answers:
                tosay, questions = answer.accept()
                print("Bot> "+tosay)
                self._extend_expected(questions)
            # process input
            input_phrase = input("You> ")
            while not input_phrase.strip():
                # print("Empty input string")
                # process routines second time! duplex tests
                answers = self.returns.get_returns()
                # print(answers)
                for answer in answers:
                    tosay, questions = answer.accept()
                    print("Bot> "+tosay)
                    self._extend_expected(questions)
                input_phrase = input("You> ")
            input_phrase = link_parser.parse(input_phrase)
            state = self.interpret(input_phrase)
            if state:
                tosay, questions = state.accept(input_phrase)
                for answer in tosay:
                    if answer != "":
                        print("Bot> "+answer)
                self._extend_expected(questions)

    def start_socket(self, port=42424):
        """
        Listens socket for input phrases.
        """
        host = ""
        buf = 1024
        addr = (host, port)
        UDPSock = socket(AF_INET, SOCK_DGRAM)
        UDPSock.bind(addr)
        print("Listening socket")

        occupation = multiprocessing.Event()
        # listener_queue = multiprocessing.Queue(maxsize=0)
        # recognizer_queue = multiprocessing.Queue(maxsize=0)
        speaker_queue = multiprocessing.Queue(maxsize=0)
        speaker = multiprocessing.Process(
            target=speech.speaker,
            args=(occupation, speaker_queue, ))
        # recognizer = multiprocessing.Process(
        #     target=speech.recognizer,
        #     args=(recognizer_queue, listener_queue, ))
        # listener = multiprocessing.Process(
        #     target=speech.listener,
        #     args=(occupation, recognizer_queue, ))
        speaker.start()
        # recognizer.start()
        # listener.start()
        occupation.set()

        print("======")
        for state in self.expected:
            print("\t    %s" % (state))
        print("======")
        while True:
            # process routines answers
            answers = self.returns.get_returns()
            for answer in answers:
                tosay, questions = answer.accept()
                speaker_queue.put(tosay)
                print("Bot> "+tosay)
                self._extend_expected(questions)
            # process input
            (data, addr) = UDPSock.recvfrom(buf)
            input_phrase = data.decode("utf-8")
            print("You> "+input_phrase)
            while not input_phrase.strip():
                print("Empty input string")
                input_phrase = input("You> ")
            input_phrase = link_parser.parse(input_phrase)
            state = self.interpret(input_phrase)
            if state:
                tosay, questions = state.accept(input_phrase)
                for answer in tosay:
                    if answer != "":
                        speaker_queue.put(answer)
                        print("Bot> "+answer)
                self._extend_expected(questions)
        UDPSock.close()

    @staticmethod
    def interrupt_handler(signal, frame):
        print("INFO: dialog system process is closed")
        sys.exit(0)

def handle(callbacks, before=lambda scope, responses: None, after=lambda scope, responses: None):
    """
    Decorator wrapper, which wraps duplex routine.
    Recives decorator params (callbacks).
    """
    def decorator(async_func):
        """
        Decorator, which recieves duplex routine function.
        """
        def inner(requests, responses, global_scope):
            """
            Resulting function, with changed behavior.
            Called by dialog system.
            """
            initial_scope = {}
            for name, obj in global_scope.items():
                if not name.startswith('__'):
                    initial_scope[name] = obj
            scope = type('', (), initial_scope)()
            before(scope, responses)
            while True:
                while not requests.empty():
                    request = requests.get()
                    if request in callbacks:
                        callbacks[request](scope, responses)
                    else:
                        print("Warining: unhandled request")
                        if "" in callbacks:
                            callbacks[""](scope, responses)
                        else:
                            pass
                # TODO: is it good way to finish routine?
                if hasattr(scope, '_exit') and scope._exit:
                    break
                async_func(requests, responses, scope)
                if hasattr(scope, '_exit') and scope._exit:
                    break
            after(scope, responses)
        return inner
    return decorator

def run_master():
    parser = argparse.ArgumentParser(
        description=__doc__,
        epilog=__author__+" "+__date__)
    parser.add_argument(
        '--version',
        action='version',
        version='%(prog)s '+__version__)
    parser.add_argument(
        '--spoken',
        action='store_true',
        required=False,
        help='run as a spoken dialog system',
        dest='is_spoken')
    parser.add_argument(
        '-s',
        '--scripts',
        metavar='PY',
        default=[],
        nargs='+',
        help='Python scripts')
    parser.add_argument(
        '-d',
        '--dialogs',
        metavar='DLG',
        default=[],
        nargs='+',
        help='dialog descriptions')
    options = vars(parser.parse_args())
    # print(options)
    print("Slave mode uses exec() function! \
        \nYou can wipe your data inside of your scripts. \
        \nScripts from the parameters will be evaluated.")
    if input("Continue? [y/n]: ") != "y":
        print("aborted")
        sys.exit(0)
    scope = {}
    for filename in options['scripts']:
        try:
            content = open(filename, "rb").read().decode("utf-8")
        except FileNotFoundError:
            print("Can't find", filename)
            sys.exit(0)
        exec(content, scope)
    del scope["__builtins__"]
    # print(scope)
    dialog_instance = Dialog(scope)
    for filename in options['dialogs']:
        dialog_instance.load(filename)
    if options["is_spoken"]:
        dialog_instance.start_spoken()
    else:
        dialog_instance.start_text()

if __name__ == "__main__":
    run_master()


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Services.Contracts.Models
{
    public class ErrorModel
    {
        public string Message { get; set; }
    }
}


text:
package org.apache.beam.runners.dataflow.worker.status;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.client.util.Key;
import com.google.api.services.dataflow.Dataflow;
import com.google.api.services.dataflow.model.GetDebugConfigRequest;
import com.google.api.services.dataflow.model.GetDebugConfigResponse;
import com.google.api.services.dataflow.model.SendDebugCaptureRequest;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.apache.beam.runners.dataflow.options.DataflowWorkerHarnessOptions;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * DebugCapture encapsulates a simple periodic sender for HTML pages to the debug capture service.
 * It is dynamically configured by the service.
 */
public class DebugCapture {
  private static final Logger LOG = LoggerFactory.getLogger(DebugCapture.class);
  private static final JsonFactory JSON_FACTORY = new JacksonFactory();
  private static final String COMPONENT = "JavaHarness";
  // How often to refresh debug capture config.
  private static final long UPDATE_CONFIG_PERIOD_SEC = 60;
  // How often to check whether to send capture data.
  private static final long SEND_CAPTURE_PERIOD_SEC = 10;

  /** Config */
  public static class Config {
    @Key
    @JsonProperty("expire_timestamp_usec")
    public long expireTimestampUsec;

    @Key
    @JsonProperty("capture_frequency_usec")
    public long captureFrequencyUsec;
  }

  private static class Payload {
    @Key public List<Page> pages = new ArrayList<>();
  }

  private static class Page {
    public Page(String name, String content) {
      this.name = name;
      this.content = content;
    }

    // Implicitly read. Not a bug.
    @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    @Key
    public String name;

    // Implicitly read. Not a bug.
    @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    @Key
    public String content;
  }

  /** Interface for capturable components. */
  public interface Capturable {
    String pageName();

    void captureData(PrintWriter writer);
  }

  /** Manager */
  public static class Manager {
    private final Object lock = new Object();
    private final ObjectMapper mapper = new ObjectMapper();
    private String project, job, host, region;
    private Dataflow client = null;
    private ScheduledExecutorService executor = null;
    private List<Capturable> capturables;
    private boolean enabled;

    private long lastCaptureUsec = 0;
    @VisibleForTesting Config captureConfig = new Config();

    public Manager(DataflowWorkerHarnessOptions options, List<Capturable> capturables) {
      try {
        client = options.getDataflowClient();
      } catch (Exception e) {
        LOG.info("Failed to get dataflow client. Does not send debug capture data. ", e);
        return;
      }

      project = options.getProject();
      job = options.getJobId();
      host = options.getWorkerId();
      region = options.getRegion();
      this.capturables = capturables;
      enabled = !capturables.isEmpty();
    }

    @SuppressWarnings("FutureReturnValueIgnored")
    public void start() {
      if (!enabled) {
        return;
      }

      executor = Executors.newScheduledThreadPool(2);
      executor.scheduleAtFixedRate(
          this::updateConfig, 0, UPDATE_CONFIG_PERIOD_SEC, TimeUnit.SECONDS);
      executor.scheduleAtFixedRate(
          this::maybeSendCapture, 0, SEND_CAPTURE_PERIOD_SEC, TimeUnit.SECONDS);
    }

    public void stop() {
      if (executor != null) {
        executor.shutdown();
      }
    }

    @VisibleForTesting
    void updateConfig() {
      try {
        GetDebugConfigRequest request =
            new GetDebugConfigRequest().setComponentId(COMPONENT).setWorkerId(host);
        GetDebugConfigResponse response =
            this.client
                .projects()
                .locations()
                .jobs()
                .debug()
                .getConfig(project, region, job, request)
                .execute();
        Config config = mapper.readValue(response.getConfig(), Config.class);
        synchronized (lock) {
          captureConfig = config;
        }
      } catch (Exception e) {
        LOG.info("Does not update debug config", e);
      }
    }

    private boolean shouldSendCapture() {
      synchronized (lock) {
        long nowUsec = DateTime.now().getMillis() * 1000;
        if (captureConfig.expireTimestampUsec < nowUsec) {
          return false; // expired config
        }
        if (captureConfig.captureFrequencyUsec == 0) {
          return false; // disabled
        }
        return captureConfig.captureFrequencyUsec < (nowUsec - lastCaptureUsec);
      }
    }

    @VisibleForTesting
    void maybeSendCapture() {
      if (!shouldSendCapture()) {
        return;
      }

      try {
        Payload payload = new Payload();
        for (Capturable capturable : capturables) {
          StringWriter content = new StringWriter();
          PrintWriter writer = new PrintWriter(content);
          capturable.captureData(writer);
          writer.flush();
          payload.pages.add(new Page(capturable.pageName(), content.toString()));
        }

        SendDebugCaptureRequest request =
            new SendDebugCaptureRequest()
                .setComponentId(COMPONENT)
                .setWorkerId(host)
                .setData(JSON_FACTORY.toString(payload));
        this.client
            .projects()
            .locations()
            .jobs()
            .debug()
            .sendCapture(project, region, job, request)
            .execute();

        synchronized (lock) {
          lastCaptureUsec = DateTime.now().getMillis() * 1000;
        }
      } catch (Exception e) {
        LOG.info("Does not send debug capture data", e);
      }
    }
  }
}


text:
#include <dae/daeDom.h>
#include <dom/domFx_surface_init_from_common.h>
#include <dae/daeMetaCMPolicy.h>
#include <dae/daeMetaSequence.h>
#include <dae/daeMetaChoice.h>
#include <dae/daeMetaGroup.h>
#include <dae/daeMetaAny.h>
#include <dae/daeMetaElementAttribute.h>

daeElementRef
domFx_surface_init_from_common::create(daeInt bytes)
{
	domFx_surface_init_from_commonRef ref = new(bytes) domFx_surface_init_from_common;
	return ref;
}

daeMetaElement *
domFx_surface_init_from_common::registerElement()
{
    if ( _Meta != NULL ) return _Meta;

    _Meta = new daeMetaElement;
    _Meta->setName( "fx_surface_init_from_common" );
	_Meta->registerClass(domFx_surface_init_from_common::create, &_Meta);

	//	Add attribute: _value
 	{
		daeMetaAttribute *ma = new daeMetaAttribute;
		ma->setName( "_value" );
		ma->setType( daeAtomicType::get("xsIDREF"));
		ma->setOffset( daeOffsetOf( domFx_surface_init_from_common , _value ));
		ma->setContainer( _Meta );
		_Meta->appendAttribute(ma);
	}

	//	Add attribute: mip
 	{
		daeMetaAttribute *ma = new daeMetaAttribute;
		ma->setName( "mip" );
		ma->setType( daeAtomicType::get("xsUnsignedInt"));
		ma->setOffset( daeOffsetOf( domFx_surface_init_from_common , attrMip ));
		ma->setContainer( _Meta );
		ma->setDefault( "0");

		_Meta->appendAttribute(ma);
	}

	//	Add attribute: slice
 	{
		daeMetaAttribute *ma = new daeMetaAttribute;
		ma->setName( "slice" );
		ma->setType( daeAtomicType::get("xsUnsignedInt"));
		ma->setOffset( daeOffsetOf( domFx_surface_init_from_common , attrSlice ));
		ma->setContainer( _Meta );
		ma->setDefault( "0");

		_Meta->appendAttribute(ma);
	}

	//	Add attribute: face
 	{
		daeMetaAttribute *ma = new daeMetaAttribute;
		ma->setName( "face" );
		ma->setType( daeAtomicType::get("Fx_surface_face_enum"));
		ma->setOffset( daeOffsetOf( domFx_surface_init_from_common , attrFace ));
		ma->setContainer( _Meta );
		ma->setDefault( "POSITIVE_X");

		_Meta->appendAttribute(ma);
	}

	_Meta->setElementSize(sizeof(domFx_surface_init_from_common));
	_Meta->validate();

	return _Meta;
}

daeMetaElement * domFx_surface_init_from_common::_Meta = NULL;


text:
package com.cloudera.sqoop.mapreduce;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Mapper;
import com.cloudera.sqoop.lib.DelimiterSet;
import com.cloudera.sqoop.lib.FieldFormatter;
import com.cloudera.sqoop.lib.RecordParser;
import com.cloudera.sqoop.manager.MySQLUtils;
import com.cloudera.sqoop.util.AsyncSink;
import com.cloudera.sqoop.util.ErrorableAsyncSink;
import com.cloudera.sqoop.util.ErrorableThread;
import com.cloudera.sqoop.util.JdbcUrl;
import com.cloudera.sqoop.util.LoggingAsyncSink;
import com.cloudera.sqoop.util.PerfCounters;

/**
 * Mapper that opens up a pipe to mysqldump and pulls data directly.
 */
public class MySQLDumpMapper
    extends Mapper<String, NullWritable, String, NullWritable> {

  public static final Log LOG = LogFactory.getLog(
      MySQLDumpMapper.class.getName());

  private Configuration conf;

  // AsyncSinks used to import data from mysqldump directly into HDFS.

  /**
   * Copies data directly from mysqldump into HDFS, after stripping some
   * header and footer characters that are attached to each line in mysqldump.
   */
  static class CopyingAsyncSink extends ErrorableAsyncSink {
    private final MySQLDumpMapper.Context context;
    private final PerfCounters counters;

    CopyingAsyncSink(final MySQLDumpMapper.Context context,
        final PerfCounters ctrs) {
      this.context = context;
      this.counters = ctrs;
    }

    public void processStream(InputStream is) {
      child = new CopyingStreamThread(is, context, counters);
      child.start();
    }

    private static class CopyingStreamThread extends ErrorableThread {
      public static final Log LOG = LogFactory.getLog(
          CopyingStreamThread.class.getName());

      private final MySQLDumpMapper.Context context;
      private final InputStream stream;
      private final PerfCounters counters;

      CopyingStreamThread(final InputStream is,
          final Context c, final PerfCounters ctrs) {
        this.context = c;
        this.stream = is;
        this.counters = ctrs;
      }

      public void run() {
        BufferedReader r = null;

        try {
          r = new BufferedReader(new InputStreamReader(this.stream));

          // Actually do the read/write transfer loop here.
          int preambleLen = -1; // set to this for "undefined"
          while (true) {
            String inLine = r.readLine();
            if (null == inLine) {
              break; // EOF.
            }

            // this line is of the form "INSERT .. VALUES ( actual value text
            // );" strip the leading preamble up to the '(' and the trailing
            // ');'.
            if (preambleLen == -1) {
              // we haven't determined how long the preamble is. It's constant
              // across all lines, so just figure this out once.
              String recordStartMark = "VALUES (";
              preambleLen = inLine.indexOf(recordStartMark)
                  + recordStartMark.length();
            }

            // chop off the leading and trailing text as we write the
            // output to HDFS.
            int len = inLine.length() - 2 - preambleLen;
            context.write(inLine.substring(preambleLen, inLine.length() - 2),
                null);
            context.write("\n", null);
            counters.addBytes(1 + len);
          }
        } catch (IOException ioe) {
          LOG.error("IOException reading from mysqldump: " + ioe.toString());
          // flag this error so we get an error status back in the caller.
          setError();
        } catch (InterruptedException ie) {
          LOG.error("InterruptedException reading from mysqldump: "
              + ie.toString());
          // flag this error so we get an error status back in the caller.
          setError();
        } finally {
          if (null != r) {
            try {
              r.close();
            } catch (IOException ioe) {
              LOG.info("Error closing FIFO stream: " + ioe.toString());
            }
          }
        }
      }
    }
  }

  /**
   * The ReparsingAsyncSink will instantiate a RecordParser to read mysqldump's
   * output, and re-emit the text in the user's specified output format.
   */
  static class ReparsingAsyncSink extends ErrorableAsyncSink {
    private final MySQLDumpMapper.Context context;
    private final Configuration conf;
    private final PerfCounters counters;

    ReparsingAsyncSink(final MySQLDumpMapper.Context c,
        final Configuration conf, final PerfCounters ctrs) {
      this.context = c;
      this.conf = conf;
      this.counters = ctrs;
    }

    public void processStream(InputStream is) {
      child = new ReparsingStreamThread(is, context, conf, counters);
      child.start();
    }

    private static class ReparsingStreamThread extends ErrorableThread {
      public static final Log LOG = LogFactory.getLog(
          ReparsingStreamThread.class.getName());

      private final MySQLDumpMapper.Context context;
      private final Configuration conf;
      private final InputStream stream;
      private final PerfCounters counters;

      ReparsingStreamThread(final InputStream is,
          final MySQLDumpMapper.Context c, Configuration conf,
          final PerfCounters ctrs) {
        this.context = c;
        this.conf = conf;
        this.stream = is;
        this.counters = ctrs;
      }

      private static final char MYSQL_FIELD_DELIM = ',';
      private static final char MYSQL_RECORD_DELIM = '\n';
      private static final char MYSQL_ENCLOSE_CHAR = '\'';
      private static final char MYSQL_ESCAPE_CHAR = '\\';
      private static final boolean MYSQL_ENCLOSE_REQUIRED = false;

      private static final RecordParser MYSQLDUMP_PARSER;

      static {
        // build a record parser for mysqldump's format
        MYSQLDUMP_PARSER = new RecordParser(DelimiterSet.MYSQL_DELIMITERS);
      }

      public void run() {
        BufferedReader r = null;

        try {
          r = new BufferedReader(new InputStreamReader(this.stream));

          // Configure the output with the user's delimiters.
          char outputFieldDelim = (char) conf.getInt(
              MySQLUtils.OUTPUT_FIELD_DELIM_KEY,
              DelimiterSet.NULL_CHAR);
          String outputFieldDelimStr = "" + outputFieldDelim;
          char outputRecordDelim = (char) conf.getInt(
              MySQLUtils.OUTPUT_RECORD_DELIM_KEY,
              DelimiterSet.NULL_CHAR);
          String outputRecordDelimStr = "" + outputRecordDelim;
          char outputEnclose = (char) conf.getInt(
              MySQLUtils.OUTPUT_ENCLOSED_BY_KEY,
              DelimiterSet.NULL_CHAR);
          char outputEscape = (char) conf.getInt(
              MySQLUtils.OUTPUT_ESCAPED_BY_KEY,
              DelimiterSet.NULL_CHAR);
          boolean outputEncloseRequired = conf.getBoolean(
              MySQLUtils.OUTPUT_ENCLOSE_REQUIRED_KEY, false);

          DelimiterSet delimiters = new DelimiterSet(
             outputFieldDelim,
             outputRecordDelim,
             outputEnclose,
             outputEscape,
             outputEncloseRequired);

          // Actually do the read/write transfer loop here.
          int preambleLen = -1; // set to this for "undefined"
          while (true) {
            String inLine = r.readLine();
            if (null == inLine) {
              break; // EOF.
            }

            // this line is of the form "INSERT .. VALUES ( actual value text
            // );" strip the leading preamble up to the '(' and the trailing
            // ');'.
            if (preambleLen == -1) {
              // we haven't determined how long the preamble is. It's constant
              // across all lines, so just figure this out once.
              String recordStartMark = "VALUES (";
              preambleLen = inLine.indexOf(recordStartMark)
                  + recordStartMark.length();
            }

            // Wrap the input string in a char buffer that ignores the leading
            // and trailing text.
            CharBuffer charbuf = CharBuffer.wrap(inLine, preambleLen,
                inLine.length() - 2);

            // Pass this along to the parser
            List<String> fields = null;
            try {
              fields = MYSQLDUMP_PARSER.parseRecord(charbuf);
            } catch (RecordParser.ParseError pe) {
              LOG.warn("ParseError reading from mysqldump: "
                  + pe.toString() + "; record skipped");
              continue; // Skip emitting this row.
            }

            // For all of the output fields, emit them using the delimiters
            // the user chooses.
            boolean first = true;
            int recordLen = 1; // for the delimiter.
            for (String field : fields) {
              if (!first) {
                context.write(outputFieldDelimStr, null);
              } else {
                first = false;
              }

              String fieldStr = FieldFormatter.escapeAndEnclose(field,
                  delimiters);
              context.write(fieldStr, null);
              recordLen += fieldStr.length();
            }

            context.write(outputRecordDelimStr, null);
            counters.addBytes(recordLen);
          }
        } catch (IOException ioe) {
          LOG.error("IOException reading from mysqldump: " + ioe.toString());
          // flag this error so the parent can handle it appropriately.
          setError();
        } catch (InterruptedException ie) {
          LOG.error("InterruptedException reading from mysqldump: "
              + ie.toString());
          // flag this error so we get an error status back in the caller.
          setError();
        } finally {
          if (null != r) {
            try {
              r.close();
            } catch (IOException ioe) {
              LOG.info("Error closing FIFO stream: " + ioe.toString());
            }
          }
        }
      }
    }
  }

  // TODO(aaron): Refactor this method to be much shorter.
  // CHECKSTYLE:OFF
  /**
   * Import the table into HDFS by using mysqldump to pull out the data from
   * the database and upload the files directly to HDFS.
   */
  public void map(String splitConditions, NullWritable val, Context context)
      throws IOException, InterruptedException {

    LOG.info("Beginning mysqldump fast path import");

    ArrayList<String> args = new ArrayList<String>();
    String tableName = conf.get(MySQLUtils.TABLE_NAME_KEY);

    // We need to parse the connect string URI to determine the database name.
    // Using java.net.URL directly on the connect string will fail because
    // Java doesn't respect arbitrary JDBC-based schemes. So we chop off the
    // scheme (everything before '://') and replace it with 'http', which we
    // know will work.
    String connectString = conf.get(MySQLUtils.CONNECT_STRING_KEY);
    String databaseName = JdbcUrl.getDatabaseName(connectString);
    String hostname = JdbcUrl.getHostName(connectString);
    int port = JdbcUrl.getPort(connectString);

    if (null == databaseName) {
      throw new IOException("Could not determine database name");
    }

    LOG.info("Performing import of table " + tableName + " from database "
        + databaseName);

    args.add(MySQLUtils.MYSQL_DUMP_CMD); // requires that this is on the path.

    String password = conf.get(MySQLUtils.PASSWORD_KEY);
    String passwordFile = null;

    Process p = null;
    AsyncSink sink = null;
    AsyncSink errSink = null;
    PerfCounters counters = new PerfCounters();
    try {
      // --defaults-file must be the first argument.
      if (null != password && password.length() > 0) {
        passwordFile = MySQLUtils.writePasswordFile(conf);
        args.add("--defaults-file=" + passwordFile);
      }

      // Don't use the --where="<whereClause>" version because spaces in it can
      // confuse Java, and adding in surrounding quotes confuses Java as well.
      String whereClause = conf.get(MySQLUtils.WHERE_CLAUSE_KEY, "(1=1)")
          + " AND (" + splitConditions + ")";
      args.add("-w");
      args.add(whereClause);

      args.add("--host=" + hostname);
      if (-1 != port) {
        args.add("--port=" + Integer.toString(port));
      }
      args.add("--skip-opt");
      args.add("--compact");
      args.add("--no-create-db");
      args.add("--no-create-info");
      args.add("--quick"); // no buffering
      args.add("--single-transaction");

      String username = conf.get(MySQLUtils.USERNAME_KEY);
      if (null != username) {
        args.add("--user=" + username);
      }

      // If the user supplied extra args, add them here.
      String [] extra = conf.getStrings(MySQLUtils.EXTRA_ARGS_KEY);
      if (null != extra) {
        for (String arg : extra) {
          args.add(arg);
        }
      }

      args.add(databaseName);
      args.add(tableName);

      // begin the import in an external process.
      LOG.debug("Starting mysqldump with arguments:");
      for (String arg : args) {
        LOG.debug("  " + arg);
      }

      // Actually start the mysqldump.
      p = Runtime.getRuntime().exec(args.toArray(new String[0]));

      // read from the stdout pipe into the HDFS writer.
      InputStream is = p.getInputStream();

      if (MySQLUtils.outputDelimsAreMySQL(conf)) {
        LOG.debug("Output delimiters conform to mysqldump; "
            + "using straight copy");
        sink = new CopyingAsyncSink(context, counters);
      } else {
        LOG.debug("User-specified delimiters; using reparsing import");
        LOG.info("Converting data to use specified delimiters.");
        LOG.info("(For the fastest possible import, use");
        LOG.info("--mysql-delimiters to specify the same field");
        LOG.info("delimiters as are used by mysqldump.)");
        sink = new ReparsingAsyncSink(context, conf, counters);
      }

      // Start an async thread to read and upload the whole stream.
      counters.startClock();
      sink.processStream(is);

      // Start an async thread to send stderr to log4j.
      errSink = new LoggingAsyncSink(LOG);
      errSink.processStream(p.getErrorStream());
    } finally {

      // block until the process is done.
      int result = 0;
      if (null != p) {
        while (true) {
          try {
            result = p.waitFor();
          } catch (InterruptedException ie) {
            // interrupted; loop around.
            continue;
          }

          break;
        }
      }

      // Remove the password file.
      if (null != passwordFile) {
        if (!new File(passwordFile).delete()) {
          LOG.error("Could not remove mysql password file " + passwordFile);
          LOG.error("You should remove this file to protect your credentials.");
        }
      }

      // block until the stream sink is done too.
      int streamResult = 0;
      if (null != sink) {
        while (true) {
          try {
            streamResult = sink.join();
          } catch (InterruptedException ie) {
            // interrupted; loop around.
            continue;
          }

          break;
        }
      }

      // Try to wait for stderr to finish, but regard any errors as advisory.
      if (null != errSink) {
        try {
          if (0 != errSink.join()) {
            LOG.info("Encountered exception reading stderr stream");
          }
        } catch (InterruptedException ie) {
          LOG.info("Thread interrupted waiting for stderr to complete: "
              + ie.toString());
        }
      }

      LOG.info("Transfer loop complete.");

      if (0 != result) {
        throw new IOException("mysqldump terminated with status "
            + Integer.toString(result));
      }

      if (0 != streamResult) {
        throw new IOException("Encountered exception in stream sink");
      }

      counters.stopClock();
      LOG.info("Transferred " + counters.toString());
    }
  }
  // CHECKSTYLE:ON

  @Override
  protected void setup(Context context) {
    this.conf = context.getConfiguration();
  }
}