text:
> Introduced in GitLab 8.14

Mattermost commands give users an extra interface to perform common operations
from the chat environment. This allows one to, for example, create an issue as
soon as the idea was discussed in Mattermost.

## Prerequisites

Mattermost 3.4 and up is required.

If you have the Omnibus GitLab package installed, Mattermost is already bundled
in it. All you have to do is configure it. Read more in the
[Omnibus GitLab Mattermost documentation][omnimmdocs].

## Automated Configuration

If Mattermost is installed on the same server as GitLab, the configuration process can be
done for you by GitLab.

Go to the Mattermost Slash Command service on your project and click the 'Add to Mattermost' button.

## Manual Configuration

The configuration consists of two parts. First you need to enable the slash
commands in Mattermost and then enable the service in GitLab.

### Step 1. Enable custom slash commands in Mattermost

This step is only required when using a source install, omnibus installs will be
preconfigured with the right settings.

The first thing to do in Mattermost is to enable custom slash commands from
the administrator console.

1. Log in with an account that has admin privileges and navigate to the system
   console.

    ![Mattermost go to console](img/mattermost_goto_console.png)

    ---

1. Click **Custom integrations** and set **Enable Custom Slash Commands**,
   **Enable custom integrations to override usernames**, and **Override
   custom integrations to override profile picture icons** to true

    ![Mattermost console](img/mattermost_console_integrations.png)

    ---

1. Click **Save** at the bottom to save the changes.

### Step 2. Open the Mattermost slash commands service in GitLab

1. Open a new tab for GitLab, go to your project's
   [Integrations page](project_services.md#accessing-the-project-services)
   and select the **Mattermost command** service to configure it.
   A screen will appear with all the values you need to copy in Mattermost as
   described in the next step. Leave the window open.

    >**Note:**
    GitLab will propose some values for the Mattermost settings. The only one
    required to copy-paste as-is is the **Request URL**, all the others are just
    suggestions.

    ![Mattermost setup instructions](img/mattermost_config_help.png)

    ---

1. Proceed to the next step and create a slash command in Mattermost with the
   above values.

### Step 3. Create a new custom slash command in Mattermost

Now that you have enabled custom slash commands in Mattermost and opened
the Mattermost slash commands service in GitLab, it's time to copy these values
in a new slash command.

1. Back to Mattermost, under your team page settings, you should see the
   **Integrations** option.

    ![Mattermost team integrations](img/mattermost_team_integrations.png)

    ---

1. Go to the **Slash Commands** integration and add a new one by clicking the
   **Add Slash Command** button.

    ![Mattermost add command](img/mattermost_add_slash_command.png)

    ---

1. Fill in the options for the custom command as described in
   [step 2](#step-2-open-the-mattermost-slash-commands-service-in-gitlab).

    >**Note:**
    If you plan on connecting multiple projects, pick a slash command trigger
    word that relates to your projects such as `/gitlab-project-name` or even
    just `/project-name`. Only use `/gitlab` if you will only connect a single
    project to your Mattermost team.

    ![Mattermost add command configuration](img/mattermost_slash_command_configuration.png)

1. After you set up all the values, copy the token (we will use it below) and
   click **Done**.

    ![Mattermost slash command token](img/mattermost_slash_command_token.png)

### Step 4. Copy the Mattermost token into the Mattermost slash command service

1. In GitLab, paste the Mattermost token you copied in the previous step and
   check the **Active** checkbox.

    ![Mattermost copy token to GitLab](img/mattermost_gitlab_token.png)

1. Click **Save changes** for the changes to take effect.

---

You are now set to start using slash commands in Mattermost that talk to the
GitLab project you configured.

## Authorizing Mattermost to interact with GitLab

The first time a user will interact with the newly created slash commands,
Mattermost will trigger an authorization process.

![Mattermost bot authorize](img/mattermost_bot_auth.png)

This will connect your Mattermost user with your GitLab user. You can
see all authorized chat accounts in your profile's page under **Chat**.

When the authorization process is complete, you can start interacting with
GitLab using the Mattermost commands.

## Available slash commands

The available slash commands are:

| Command | Description | Example |
| ------- | ----------- | ------- |
| /<trigger> issue new <title> ⇧ Shift+↵ Enter <description> | Create a new issue in the project that `<trigger>` is tied to. `<description>` is optional. | <samp>/gitlab issue new We need to change the homepage</samp> |
| /<trigger> issue show <issue-number> | Show the issue with ID `<issue-number>` from the project that `<trigger>` is tied to. | <samp>/gitlab issue show 42</samp> |
| /<trigger> deploy <environment> to <environment> | Start the CI job that deploys from one environment to another, for example `staging` to `production`. CI/CD must be [properly configured][ciyaml]. | <samp>/gitlab deploy staging to production</samp> |

To see a list of available commands to interact with GitLab, type the
trigger word followed by help. Example: <samp>/gitlab help</samp>

![Mattermost bot available commands](img/mattermost_bot_available_commands.png)

## Permissions

The permissions to run the [available commands](#available-slash-commands) derive from
the [permissions you have on the project](../../permissions.md#project-members-permissions).

## Further reading

- [Mattermost slash commands documentation][mmslashdocs]
- [Omnibus GitLab Mattermost][omnimmdocs]

[omnimmdocs]: https://docs.gitlab.com/omnibus/gitlab-mattermost/
[mmslashdocs]: https://docs.mattermost.com/developer/slash-commands.html
[ciyaml]: ../../../ci/yaml/README.md


text:
const gulp = require('gulp')
const gutil = require('gulp-util')

const webpack = require('webpack')
const webpackConfig = require('./webpack.config.js')

gulp.task('default', ['build'])

// BUILD-DEV
gulp.task('build-dev', ['webpack:build-dev'], () => {
  gulp.watch(['src/**/*'], ['webpack:build-dev'])
})

// BUILD
gulp.task('build', ['webpack:build'])

// WEBPACK
function noop() {}

function handleWebpack(name, config, callback) {
  callback = callback || noop

  webpack(config, (err, stats) => {
    if (err) throw new gutil.PluginError(name, err)

    gutil.log(`[${name}]`, stats.toString({
      colors: true
    }))

    callback()
  })
}

// WEBPACK:BUILD
gulp.task('webpack:build', callback => {
  const webpackProdConfig = Object.assign({}, webpackConfig, {
    plugins: webpackConfig.plugins.concat(
      new webpack.DefinePlugin({
        'process.env': {
          'NODE_ENV': JSON.stringify('production')
        }
      }),
      new webpack.optimize.DedupePlugin(),
      new webpack.optimize.UglifyJsPlugin()
    )
  })

  handleWebpack('webpack:build', webpackProdConfig, callback)
})

// WEBPACK:BUILD-DEV
gulp.task('webpack:build-dev', callback => {
  const webpackDevConfig = Object.assign({}, webpackConfig, {
    cache: false,
    devtool: 'sourcemap',
    debug: true
  })

  handleWebpack('webpack:build-dev', webpackDevConfig, callback)
})

redpajama_set_name: RedPajamaGithub


text:
extern "Java"
{
  namespace java
  {
    namespace text
    {
        class AttributedCharacterIterator;
        class AttributedCharacterIterator$Attribute;
        class AttributedString;
        class AttributedString$AttributeRange;
        class StringCharacterIterator;
    }
  }
}

class java::text::AttributedString : public ::java::lang::Object
{

public:
  AttributedString(::java::lang::String *);
  AttributedString(::java::lang::String *, ::java::util::Map *);
  AttributedString(::java::text::AttributedCharacterIterator *);
  AttributedString(::java::text::AttributedCharacterIterator *, jint, jint);
  AttributedString(::java::text::AttributedCharacterIterator *, jint, jint, JArray< ::java::text::AttributedCharacterIterator$Attribute * > *);
  virtual void addAttribute(::java::text::AttributedCharacterIterator$Attribute *, ::java::lang::Object *);
  virtual void addAttribute(::java::text::AttributedCharacterIterator$Attribute *, ::java::lang::Object *, jint, jint);
  virtual void addAttributes(::java::util::Map *, jint, jint);
  virtual ::java::text::AttributedCharacterIterator * getIterator();
  virtual ::java::text::AttributedCharacterIterator * getIterator(JArray< ::java::text::AttributedCharacterIterator$Attribute * > *);
  virtual ::java::text::AttributedCharacterIterator * getIterator(JArray< ::java::text::AttributedCharacterIterator$Attribute * > *, jint, jint);
private:
  ::java::text::StringCharacterIterator * __attribute__((aligned(__alignof__( ::java::lang::Object)))) sci;
  JArray< ::java::text::AttributedString$AttributeRange * > * attribs;
public:
  static ::java::lang::Class class$;
};

#endif // __java_text_AttributedString__


text:
using System;
using Voxalia.Shared;
using Voxalia.ClientGame.OtherSystems;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4;

namespace Voxalia.ClientGame.GraphicsSystems.LightingSystem
{
    public class PointLight : LightObject
    {
        public int Texsize;

        float Radius;

        Location Color;

        public PointLight(Location pos, int tsize, float radius, Location col)
        {
            EyePos = pos;
            Texsize = tsize;
            Radius = radius;
            Color = col;
            for (int i = 0; i < 6; i++)
            {
                Light li = new Light();
                li.Create(ClientUtilities.ConvertD(pos), ClientUtilities.ConvertD(pos + Location.UnitX), 90f, Radius, ClientUtilities.Convert(Color));
                InternalLights.Add(li);
            }
            InternalLights[4].up = new Vector3(0, 1, 0);
            InternalLights[5].up = new Vector3(0, 1, 0);
            Reposition(EyePos);
            MaxDistance = radius;
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
        }

        public void Destroy()
        {
        }

        public override void Reposition(Location pos)
        {
            EyePos = pos;
            for (int i = 0; i < 6; i++)
            {
                InternalLights[i].NeedsUpdate = true;
                InternalLights[i].eye = ClientUtilities.ConvertD(EyePos);
            }
            InternalLights[0].target = ClientUtilities.ConvertD(EyePos + new Location(1, 0, 0));
            InternalLights[1].target = ClientUtilities.ConvertD(EyePos + new Location(-1, 0, 0));
            InternalLights[2].target = ClientUtilities.ConvertD(EyePos + new Location(0, 1, 0));
            InternalLights[3].target = ClientUtilities.ConvertD(EyePos + new Location(0, -1, 0));
            InternalLights[4].target = ClientUtilities.ConvertD(EyePos + new Location(0, 0, 1));
            InternalLights[5].target = ClientUtilities.ConvertD(EyePos + new Location(0, 0, -1));
        }
    }
}


text:
.form-message {
  margin: var(--form-message-margin);
  font-family: var(--form-message-font-family);
  font-weight: var(--form-message-font-weight);
  font-size: var(--form-message-font-size);
  font-style: var(--form-message-font-style);
  color: var(--form-message-color);
  line-height: var(--form-message-line-height);
  text-transform: var(--form-message-text-transform);
  letter-spacing: var(--form-message-letter-spacing);

  &:empty {
    display: none;
  }

  &.error {
    color: var(--error);
  }

  &.warning {
    color: var(--warning);
  }

  &.success {
    color: var(--success);
  }

  &.info {
    color: var(--info);
  }
}


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<title>TrackpointComparator</title>
<meta name="date" content="2018-03-14">
<link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style">

JavaScript is disabled on your browser.

Skip navigation links

Package

Class

Use

Tree

Deprecated

Index

Help

Prev Class

Next Class

Frames

No Frames

All Classes

Summary:

Nested |

Field |

Constr |

Method

Detail:

Field |

Constr |

Method

it.michelebonacina.lib.jgps.points

Class TrackpointComparator

java.lang.Object

it.michelebonacina.lib.jgps.points.TrackpointComparator

All Implemented Interfaces:

java.util.Comparator<Trackpoint>

public class TrackpointComparator
extends java.lang.Object
implements java.util.Comparator<Trackpoint>

Trackpoint comparator. Defines the rules for sorting trakpoints. This comparator is used for ordering two trackpoints and it's
 based on the timestamp of each point; the trackpoint with the higher timestamp is the bigger point.

Since:

0.0.1.

Version:

0.1.0.

Author:

Michele Bonacina

Constructor Summary

Constructors

Constructor and Description

TrackpointComparator()

Method Summary

All Methods Instance Methods Concrete Methods

Modifier and Type
Method and Description

int
compare(Trackpoint firstTrackpoint,
       Trackpoint secondTrackpoint)

Methods inherited from class java.lang.Object

equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Methods inherited from interface java.util.Comparator

comparing, comparing, comparingDouble, comparingInt, comparingLong, equals, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong

Constructor Detail

TrackpointComparator

public TrackpointComparator()

Method Detail

compare

public int compare(Trackpoint firstTrackpoint,
                   Trackpoint secondTrackpoint)

Specified by:

compare in interface java.util.Comparator<Trackpoint>

Skip navigation links

Package

Class

Use

Tree

Deprecated

Index

Help

Prev Class

Next Class

Frames

No Frames

All Classes

Summary:

Nested |

Field |

Constr |

Method

Detail:

Field |

Constr |

Method


text:
public static int B;
public static int H;
public static boolean flag;

static {
    Scanner in = new Scanner(System.in);
    B = in.nextInt();
    H = in.nextInt();

    if(B > 0 && H > 0) {
        flag = true;
    }
    else {
        flag = false;
        System.out.println("java.lang.Exception: Breadth and height must be positive");
    }
}


text:
package jenkins.plugins.clangscanbuild.reports;

import hudson.model.Run;

public class GraphPoint {

        private Run<?,?> run;

	private int bugCount;

	public GraphPoint(Run<?, ?> run, int bugCount) {
		super();
		this.run = run;
		this.bugCount = bugCount;
	}

	public Run<?, ?> getRun() {
		return run;
	}
	public void setRun(Run<?, ?> run) {
		this.run = run;
	}
	public int getBugCount() {
		return bugCount;
	}
	public void setBugCount(int bugCount) {
		this.bugCount = bugCount;
	}

}


text:
package org.joda.time.format;

import java.util.Locale;

import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.joda.time.Chronology;
import org.joda.time.DateTimeFieldType;
import org.joda.time.DateTimeZone;
import org.joda.time.chrono.BuddhistChronology;
import org.joda.time.chrono.ISOChronology;

/**
 * Test.
 */
public class TestDateTimeParserBucket extends TestCase {

    private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
    private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
    private static final Chronology BUDDHIST_UTC = BuddhistChronology.getInstanceUTC();
    private static final Chronology BUDDHIST_PARIS = BuddhistChronology.getInstance(PARIS);
    private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC();
    private static final DateTimeZone ZONE_0400 = DateTimeZone.forOffsetHours(4);
    private static final Chronology ISO_0400 = ISOChronology.getInstance(ZONE_0400);
    private static final int MILLIS_PER_HOUR = 3600000;
    private static final int MILLIS_PER_MINUTE = 60000;
    private static final int OFFSET_0400 = 4 * MILLIS_PER_HOUR;
    private static final Locale LOCALE = Locale.CANADA;

    public static void main(String[] args) {
        junit.textui.TestRunner.run(suite());
    }

    public static TestSuite suite() {
        return new TestSuite(TestDateTimeParserBucket.class);
    }

    public TestDateTimeParserBucket(String name) {
        super(name);
    }

    //-----------------------------------------------------------------------
    @SuppressWarnings("deprecation")
    public void testConstructor_3arg() {
        DateTimeParserBucket test = new DateTimeParserBucket(100, BUDDHIST_PARIS, LOCALE);
        assertEquals(BUDDHIST_UTC, test.getChronology());
        assertEquals(LOCALE, test.getLocale());
        assertEquals(null, test.getPivotYear());
        assertEquals(null, test.getOffsetInteger());
        assertEquals(PARIS, test.getZone());
    }

    @SuppressWarnings("deprecation")
    public void testConstructor_4arg() {
        DateTimeParserBucket test = new DateTimeParserBucket(100, BUDDHIST_PARIS, LOCALE, 2010);
        assertEquals(BUDDHIST_UTC, test.getChronology());
        assertEquals(LOCALE, test.getLocale());
        assertEquals((Integer) 2010, test.getPivotYear());
        assertEquals(null, test.getOffsetInteger());
        assertEquals(PARIS, test.getZone());
    }

    public void testConstructor_5arg() {
        DateTimeParserBucket test = new DateTimeParserBucket(100, BUDDHIST_PARIS, LOCALE, 2010, 2001);
        assertEquals(BUDDHIST_UTC, test.getChronology());
        assertEquals(LOCALE, test.getLocale());
        assertEquals((Integer) 2010, test.getPivotYear());
        assertEquals(null, test.getOffsetInteger());
        assertEquals(PARIS, test.getZone());
    }

    @SuppressWarnings("deprecation")
    public void testSetPivotYear() {
        DateTimeParserBucket test = new DateTimeParserBucket(100, BUDDHIST_PARIS, LOCALE, 2010, 2001);
        assertEquals((Integer) 2010, test.getPivotYear());
        test.setPivotYear(null);
        assertEquals(null, test.getPivotYear());
        test.setPivotYear(2030);
        assertEquals((Integer) 2030, test.getPivotYear());
    }

    public void testSetOffset() {
        DateTimeParserBucket test = new DateTimeParserBucket(100, BUDDHIST_PARIS, LOCALE, 2010, 2001);
        assertEquals(null, test.getOffsetInteger());
        test.setOffset((Integer) 1000);
        assertEquals((Integer) 1000, test.getOffsetInteger());
        test.setOffset(null);
        assertEquals(null, test.getOffsetInteger());
    }

    public void testSetZone() {
        DateTimeParserBucket test = new DateTimeParserBucket(100, BUDDHIST_PARIS, LOCALE, 2010, 2001);
        assertEquals(PARIS, test.getZone());
        test.setZone(LONDON);
        assertEquals(LONDON, test.getZone());
    }

    public void testCompute() {
        DateTimeParserBucket test = new DateTimeParserBucket(100, ISO_0400, LOCALE, 2000, 2000);
        assertEquals(100 - OFFSET_0400, test.computeMillis());
        assertEquals(100 - OFFSET_0400, test.computeMillis(false));
        // note that computeMillis(true) differs depending on whether fields are saved or not
        assertEquals(100 - OFFSET_0400, test.computeMillis(true));
    }

    public void testSaveCompute() {
        DateTimeParserBucket test = new DateTimeParserBucket(100, ISO_0400, LOCALE, 2000, 2000);
        test.saveField(DateTimeFieldType.hourOfDay(), 2);
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis());
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
        test.saveField(DateTimeFieldType.hourOfDay(), 5);
        assertEquals(5 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
        assertEquals(5 * MILLIS_PER_HOUR - OFFSET_0400, test.computeMillis(true));
        assertEquals(5 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
    }

    public void testSaveRestoreState() {
        DateTimeParserBucket test = new DateTimeParserBucket(100, ISO_0400, LOCALE, 2000, 2000);
        test.saveField(DateTimeFieldType.hourOfDay(), 2);
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
        Object state = test.saveState();
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
        test.saveField(DateTimeFieldType.minuteOfHour(), 6);
        assertEquals(2 * MILLIS_PER_HOUR + 6 * MILLIS_PER_MINUTE + 100 - OFFSET_0400, test.computeMillis(false));
        assertEquals(true, test.restoreState(state));
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
        test.saveField(DateTimeFieldType.minuteOfHour(), 7);
        assertEquals(2 * MILLIS_PER_HOUR + 7 * MILLIS_PER_MINUTE + 100 - OFFSET_0400, test.computeMillis(false));
        assertEquals(true, test.restoreState(state));
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
    }

    public void testSaveRestoreState_avoidSideEffects() {
        // computeMillis() has side effects, so check works without it
        DateTimeParserBucket test = new DateTimeParserBucket(100, ISO_0400, LOCALE, 2000, 2000);
        test.saveField(DateTimeFieldType.hourOfDay(), 2);
        Object state = test.saveState();
        test.saveField(DateTimeFieldType.minuteOfHour(), 6);
        assertEquals(true, test.restoreState(state));
        test.saveField(DateTimeFieldType.minuteOfHour(), 7);
        assertEquals(2 * MILLIS_PER_HOUR + 7 * MILLIS_PER_MINUTE + 100 - OFFSET_0400, test.computeMillis(false));
        assertEquals(true, test.restoreState(state));
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
    }

    public void testSaveRestoreState_offset() {
        DateTimeParserBucket test = new DateTimeParserBucket(100, ISO_0400, LOCALE, 2000, 2000);
        test.saveField(DateTimeFieldType.hourOfDay(), 2);
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
        Object state = test.saveState();
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
        test.setOffset((Integer) 0);
        assertEquals(2 * MILLIS_PER_HOUR + 100, test.computeMillis(false));
        assertEquals(true, test.restoreState(state));
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
    }

    public void testSaveRestoreState_zone() {
        DateTimeParserBucket test = new DateTimeParserBucket(100, ISO_0400, LOCALE, 2000, 2000);
        test.saveField(DateTimeFieldType.hourOfDay(), 2);
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
        Object state = test.saveState();
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
        test.setZone(DateTimeZone.UTC);
        assertEquals(2 * MILLIS_PER_HOUR + 100, test.computeMillis(false));
        assertEquals(true, test.restoreState(state));
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
    }

    public void testSaveRestoreState_text() {
        DateTimeParserBucket test = new DateTimeParserBucket(100, ISO_0400, LOCALE, 2000, 2000);
        test.saveField(DateTimeFieldType.hourOfDay(), "2", Locale.ENGLISH);
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
        Object state = test.saveState();
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
        test.saveField(DateTimeFieldType.minuteOfHour(), "6", Locale.ENGLISH);
        assertEquals(2 * MILLIS_PER_HOUR + 6 * MILLIS_PER_MINUTE + 100 - OFFSET_0400, test.computeMillis(false));
        assertEquals(true, test.restoreState(state));
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
    }

    public void testSaveRestoreState_twoStates() {
        DateTimeParserBucket test = new DateTimeParserBucket(100, ISO_0400, LOCALE, 2000, 2000);
        test.saveField(DateTimeFieldType.hourOfDay(), 2);
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
        Object state1 = test.saveState();
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
        test.saveField(DateTimeFieldType.minuteOfHour(), 6);
        assertEquals(2 * MILLIS_PER_HOUR + 6 * MILLIS_PER_MINUTE + 100 - OFFSET_0400, test.computeMillis(false));
        Object state2 = test.saveState();
        assertEquals(2 * MILLIS_PER_HOUR + 6 * MILLIS_PER_MINUTE + 100 - OFFSET_0400, test.computeMillis(false));
        test.saveField(DateTimeFieldType.secondOfMinute(), 8);
        assertEquals(2 * MILLIS_PER_HOUR + 6 * MILLIS_PER_MINUTE + 8000 + 100 - OFFSET_0400, test.computeMillis(false));
        assertEquals(true, test.restoreState(state2));
        assertEquals(2 * MILLIS_PER_HOUR + 6 * MILLIS_PER_MINUTE + 100 - OFFSET_0400, test.computeMillis(false));
        assertEquals(true, test.restoreState(state1));
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
        assertEquals(true, test.restoreState(state2));
        assertEquals(2 * MILLIS_PER_HOUR + 6 * MILLIS_PER_MINUTE + 100 - OFFSET_0400, test.computeMillis(false));
        assertEquals(true, test.restoreState(state1));
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
    }

    public void testSaveRestoreState_sameStates() {
        DateTimeParserBucket test = new DateTimeParserBucket(100, ISO_0400, LOCALE, 2000, 2000);
        test.saveField(DateTimeFieldType.hourOfDay(), 2);
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
        Object state1 = test.saveState();
        Object state2 = test.saveState();
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
        test.saveField(DateTimeFieldType.minuteOfHour(), 6);
        assertEquals(2 * MILLIS_PER_HOUR + 6 * MILLIS_PER_MINUTE + 100 - OFFSET_0400, test.computeMillis(false));
        assertEquals(true, test.restoreState(state2));
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
        test.saveField(DateTimeFieldType.minuteOfHour(), 8);
        assertEquals(2 * MILLIS_PER_HOUR + 8 * MILLIS_PER_MINUTE + 100 - OFFSET_0400, test.computeMillis(false));
        assertEquals(true, test.restoreState(state1));
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
        assertEquals(true, test.restoreState(state2));
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
        assertEquals(true, test.restoreState(state1));
        assertEquals(2 * MILLIS_PER_HOUR + 100 - OFFSET_0400, test.computeMillis(false));
    }

    public void testSaveRestoreState_badType() {
        DateTimeParserBucket bucket1 = new DateTimeParserBucket(100, ISO_0400, LOCALE, 2000, 2000);
        DateTimeParserBucket bucket2 = new DateTimeParserBucket(100, BUDDHIST_PARIS, LOCALE, 2000, 2000);
        assertEquals(false, bucket1.restoreState(null));
        assertEquals(false, bucket1.restoreState(""));
        assertEquals(false, bucket2.restoreState(bucket1.saveState()));
    }

    //-------------------------------------------------------------------------
    public void testReset() {
        DateTimeParserBucket test = new DateTimeParserBucket(100, ISO_0400, LOCALE, 2000, 2000);
        assertEquals(ISO_UTC, test.getChronology());
        assertEquals(LOCALE, test.getLocale());
        assertEquals((Integer) 2000, test.getPivotYear());
        assertEquals(null, test.getOffsetInteger());
        assertEquals(ZONE_0400, test.getZone());

        test.setOffset((Integer) 200);
        test.setZone(LONDON);
        test.saveField(DateTimeFieldType.hourOfDay(), 2);
        assertEquals(2 * MILLIS_PER_HOUR + 100 - 200, test.computeMillis(false));
        assertEquals((Integer) 200, test.getOffsetInteger());
        assertEquals(LONDON, test.getZone());

        test.reset();
        assertEquals(ISO_UTC, test.getChronology());
        assertEquals(LOCALE, test.getLocale());
        assertEquals((Integer) 2000, test.getPivotYear());
        assertEquals(null, test.getOffsetInteger());
        assertEquals(ZONE_0400, test.getZone());
        assertEquals(100 - OFFSET_0400, test.computeMillis(false));
    }

    public void testParse() {
        DateTimeParserBucket test = new DateTimeParserBucket(0, ISO_0400, LOCALE, 2000, 2000);
        DateTimeParser parser = new DateTimeParser() {
            public int parseInto(DateTimeParserBucket bucket, String text, int position) {
                bucket.saveField(DateTimeFieldType.hourOfDay(), 2);
                bucket.saveField(DateTimeFieldType.minuteOfHour(), 6);
                return position + 1;
            }
            public int estimateParsedLength() {
                return 1;
            }
        };
        long millis = test.parseMillis(parser, "A");
        assertEquals(2 * MILLIS_PER_HOUR + 6 * MILLIS_PER_MINUTE - OFFSET_0400, millis);
        millis = test.parseMillis(parser, "B");
        assertEquals(2 * MILLIS_PER_HOUR + 6 * MILLIS_PER_MINUTE - OFFSET_0400, millis);
        millis = test.parseMillis(parser, "C");
        assertEquals(2 * MILLIS_PER_HOUR + 6 * MILLIS_PER_MINUTE - OFFSET_0400, millis);
    }

}


text:
package com.google.cloud.dataflow.sdk.runners.worker;

import com.google.api.services.dataflow.model.MapTask;
import com.google.api.services.dataflow.model.ParallelInstruction;
import com.google.cloud.dataflow.sdk.coders.Coder;
import com.google.cloud.dataflow.sdk.io.UnboundedSource;
import com.google.cloud.dataflow.sdk.options.DataflowWorkerHarnessOptions;
import com.google.cloud.dataflow.sdk.options.PipelineOptionsFactory;
import com.google.cloud.dataflow.sdk.runners.dataflow.CustomSources;
import com.google.cloud.dataflow.sdk.runners.worker.logging.DataflowWorkerLoggingInitializer;
import com.google.cloud.dataflow.sdk.runners.worker.logging.DataflowWorkerLoggingMDC;
import com.google.cloud.dataflow.sdk.runners.worker.windmill.Windmill;
import com.google.cloud.dataflow.sdk.runners.worker.windmill.WindmillServerStub;
import com.google.cloud.dataflow.sdk.util.BoundedQueueExecutor;
import com.google.cloud.dataflow.sdk.util.MemoryMonitor;
import com.google.cloud.dataflow.sdk.util.Serializer;
import com.google.cloud.dataflow.sdk.util.Transport;
import com.google.cloud.dataflow.sdk.util.UserCodeException;
import com.google.cloud.dataflow.sdk.util.common.Counter;
import com.google.cloud.dataflow.sdk.util.common.Counter.AggregationKind;
import com.google.cloud.dataflow.sdk.util.common.Counter.CounterMean;
import com.google.cloud.dataflow.sdk.util.common.CounterSet;
import com.google.cloud.dataflow.sdk.util.common.worker.MapTaskExecutor;
import com.google.cloud.dataflow.sdk.util.common.worker.OutputObjectAndByteCounter;
import com.google.cloud.dataflow.sdk.util.common.worker.ReadOperation;
import com.google.cloud.dataflow.sdk.util.common.worker.StateSampler;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.protobuf.ByteString;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.joda.time.Instant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Random;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import javax.annotation.Nullable;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Implements a Streaming Dataflow worker.
 */
public class StreamingDataflowWorker {
  private static final Logger LOG = LoggerFactory.getLogger(StreamingDataflowWorker.class);
  // Maximum number of threads for processing.  Currently each thread processes one key at a time.
  static final int MAX_PROCESSING_THREADS = 300;
  static final long THREAD_EXPIRATION_TIME_SEC = 60;
  // Maximum work units retrieved from Windmill and queued before processing. Limiting this delays
  // retrieving extra work from Windmill without working on it, leading to better
  // prioritization / utilization.
  static final int MAX_WORK_UNITS_QUEUED = 100;
  static final long MAX_COMMIT_BYTES = 32 << 20;
  static final int DEFAULT_STATUS_PORT = 8081;
  static final String DEFAULT_WINDMILL_SERVER_CLASS_NAME =
      "com.google.cloud.dataflow.sdk.runners.worker.windmill.WindmillServer";
  static final int MAX_COMMIT_QUEUE_BYTES = 500 << 20;  // 500MB

  // Maximum size of the result of a GetWork request.
  private static final long MAX_GET_WORK_FETCH_BYTES = 64L << 20; // 64m

  /**
   * Maximum number of items to return in a GetWork request.
   */
  private static final long MAX_GET_WORK_ITEMS = 100;

  private static final MemoryMonitor memoryMonitor = new MemoryMonitor();

  /**
   * Indicates that the key token was invalid when data was attempted to be fetched.
   */
  public static class KeyTokenInvalidException extends RuntimeException {
    public KeyTokenInvalidException(String key) {
      super("Unable to fetch data due to token mismatch for key " + key);
    }
  }

  /**
   * Returns whether an exception was caused by a {@link KeyTokenInvalidException}.
   */
  public static boolean isKeyTokenInvalidException(Throwable t) {
    while (t != null) {
      if (t instanceof KeyTokenInvalidException) {
        return true;
      }
      t = t.getCause();
    }
    return false;
  }

  /**
   * Returns whether an exception was caused by a {@link OutOfMemoryError}.
   */
  private static boolean isOutOfMemoryError(Throwable t) {
    while (t != null) {
      if (t instanceof OutOfMemoryError) {
        return true;
      }
      t = t.getCause();
    }
    return false;
  }

  static MapTask parseMapTask(String input) throws IOException {
    return Transport.getJsonFactory()
        .fromString(input, MapTask.class);
  }

  public static void main(String[] args) throws Exception {
    Thread.setDefaultUncaughtExceptionHandler(
        DataflowWorkerHarness.WorkerUncaughtExceptionHandler.INSTANCE);

    new Thread(memoryMonitor).start();

    DataflowWorkerLoggingInitializer.initialize();
    DataflowWorkerHarnessOptions options =
        PipelineOptionsFactory.createFromSystemPropertiesInternal();
    // TODO: Remove setting these options once we have migrated to passing
    // through the pipeline options.
    options.setAppName("StreamingWorkerHarness");
    options.setStreaming(true);

    DataflowWorkerLoggingInitializer.configure(options);
    String hostport = System.getProperty("windmill.hostport");
    if (hostport == null) {
      throw new Exception("-Dwindmill.hostport must be set to the location of the windmill server");
    }

    int statusPort = DEFAULT_STATUS_PORT;
    if (System.getProperties().containsKey("status_port")) {
      statusPort = Integer.parseInt(System.getProperty("status_port"));
    }

    String windmillServerClassName = DEFAULT_WINDMILL_SERVER_CLASS_NAME;
    if (System.getProperties().containsKey("windmill.serverclassname")) {
      windmillServerClassName = System.getProperty("windmill.serverclassname");
    }

    ArrayList<MapTask> mapTasks = new ArrayList<>();
    for (String arg : args) {
      mapTasks.add(parseMapTask(arg));
    }

    WindmillServerStub windmillServer =
        (WindmillServerStub) Class.forName(windmillServerClassName)
        .getDeclaredConstructor(String.class).newInstance(hostport);

    StreamingDataflowWorker worker =
        new StreamingDataflowWorker(mapTasks, windmillServer, options);
    worker.start();

    worker.runStatusServer(statusPort);
  }

  /**
   * Entry in a per-key UnboundedSource#UnboundedReader cache.
   */
  public static class ReaderCacheEntry {
    UnboundedSource.UnboundedReader<?> reader;
    long token;

    public ReaderCacheEntry(UnboundedSource.UnboundedReader<?> reader, long token) {
      this.reader = reader;
      this.token = token;
    }
  }

  /**
   * Bounded set of queues, with a maximum total weight.
   */
  private static class KeyedWeightBoundedQueue<K, V> {
    private final ConcurrentMap<K, ConcurrentLinkedQueue<V>> queueMap = new ConcurrentHashMap<>();
    private final int maxWeight;
    private final Semaphore limit;
    private final Function<V, Integer> weigher;

    public KeyedWeightBoundedQueue(int maxWeight, Function<V, Integer> weigher) {
      this.maxWeight = maxWeight;
      this.limit = new Semaphore(maxWeight, true);
      this.weigher = weigher;
    }

    /**
     * Adds a new sub-queue for the given key.
     */
    public void addQueue(K key) {
      queueMap.put(key, new ConcurrentLinkedQueue<V>());
    }

    /**
     * Adds the value to the queue for the key, blocking if this would cause the overall weight to
     * exceed the limit.
     */
    public void put(K key, V value) {
      limit.acquireUninterruptibly(Math.max(maxWeight, weigher.apply(value)));
      Preconditions.checkNotNull(queueMap.get(key),
          "Must create a queue by calling addQueue() before put. Missing key %s", key).add(value);
    }

    /**
     * Return the set of keys for which there are sub-queues.
     */
    public Set<K> keySet() {
      return queueMap.keySet();
    }

    /**
     * Returns and removes the next value from the given sub-queue, or null if there is no such
     * value.
     */
    @Nullable
    public V poll(K key) {
      V result = queueMap.get(key).poll();
      if (result != null) {
        limit.release(Math.max(maxWeight, weigher.apply(result)));
      }
      return result;
    }

    /**
     * Returns the size of the given sub-queue.
     */
    public int queueSize(K key) {
      return queueMap.get(key).size();
    }

    /**
     * Returns the current weight of all queues.
     */
    public int weight() {
      return maxWeight - limit.availablePermits();
    }
  }

  // Maps from computation ids to per-computation state.
  private final ConcurrentMap<String, MapTask> instructionMap;
  private final ConcurrentMap<String, ConcurrentLinkedQueue<WorkerAndContext>> mapTaskExecutors;
  private final ConcurrentMap<String, ActiveWorkForComputation> activeWorkMap;
  // Per computation cache of active readers, keyed by split ID.
  private final ConcurrentMap<String, ConcurrentMap<ByteString, ReaderCacheEntry>> readerCache;
  private final KeyedWeightBoundedQueue<String, Windmill.WorkItemCommitRequest> commitQueue =
      new KeyedWeightBoundedQueue<>(
          MAX_COMMIT_QUEUE_BYTES, new Function<Windmill.WorkItemCommitRequest, Integer>() {
            @Override
            public Integer apply(Windmill.WorkItemCommitRequest input) {
              return input.getSerializedSize();
            }
          });

  // Map of tokens to commit callbacks.
  private ConcurrentMap<Long, Runnable> commitCallbacks;

  // Map of user state names to system state names.
  private ConcurrentMap<String, String> stateNameMap;
  private ConcurrentMap<String, String> systemNameToComputationIdMap;

  private WindmillStateCache stateCache = new WindmillStateCache();

  private ThreadFactory threadFactory;
  private BoundedQueueExecutor workUnitExecutor;
  private ExecutorService commitExecutor;
  private WindmillServerStub windmillServer;
  private Thread dispatchThread;
  private AtomicBoolean running;
  private StateFetcher stateFetcher;
  private DataflowWorkerHarnessOptions options;
  private long clientId;
  private Server statusServer;
  private final AtomicReference<Throwable> lastException;
  private final MetricTrackingWindmillServerStub metricTrackingWindmillServer;
  private Timer globalCountersUpdatesTimer;

  private final UserCodeTimeTracker userCodeTimeTracker = new UserCodeTimeTracker();
  private final AtomicInteger nextStateSamplerId = new AtomicInteger();

  public StreamingDataflowWorker(
      List<MapTask> mapTasks, WindmillServerStub server, DataflowWorkerHarnessOptions options) {
    this.options = options;
    this.instructionMap = new ConcurrentHashMap<>();
    this.mapTaskExecutors = new ConcurrentHashMap<>();
    this.activeWorkMap = new ConcurrentHashMap<>();
    this.readerCache = new ConcurrentHashMap<>();
    this.commitCallbacks = new ConcurrentHashMap<>();
    this.stateNameMap = new ConcurrentHashMap<>();
    this.systemNameToComputationIdMap = new ConcurrentHashMap<>();
    this.threadFactory = new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
          Thread t = new Thread(r);
          t.setDaemon(true);
          return t;
        }
      };
    this.workUnitExecutor = new BoundedQueueExecutor(
        chooseMaximumNumberOfThreads(options), THREAD_EXPIRATION_TIME_SEC, TimeUnit.SECONDS,
        MAX_WORK_UNITS_QUEUED, threadFactory);
    this.commitExecutor =
        new ThreadPoolExecutor(
            1,
            1,
            Long.MAX_VALUE,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(2),
            new ThreadFactory() {
              @Override
              public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setDaemon(true);
                t.setPriority(Thread.MAX_PRIORITY);
                t.setName("CommitThread");
                return t;
              }
            },
            new ThreadPoolExecutor.DiscardPolicy());
    this.windmillServer = server;
    this.metricTrackingWindmillServer = new MetricTrackingWindmillServerStub(server, memoryMonitor);
    this.running = new AtomicBoolean();
    this.stateFetcher = new StateFetcher(metricTrackingWindmillServer);
    this.clientId = new Random().nextLong();
    this.lastException = new AtomicReference<>();

    for (MapTask mapTask : mapTasks) {
      addComputation(mapTask);
    }

    DataflowWorkerLoggingMDC.setJobId(options.getJobId());
    DataflowWorkerLoggingMDC.setWorkerId(options.getWorkerId());
  }

  private static int chooseMaximumNumberOfThreads(DataflowWorkerHarnessOptions pipelineOptions) {
    if (pipelineOptions.getNumberOfWorkerHarnessThreads() != 0) {
      return pipelineOptions.getNumberOfWorkerHarnessThreads();
    }
    return MAX_PROCESSING_THREADS;
  }

  void addStateNameMappings(Map<String, String> nameMap) {
    stateNameMap.putAll(nameMap);
  }

  public void start() {
    running.set(true);
    dispatchThread = threadFactory.newThread(new Runnable() {
      @Override
      public void run() {
        dispatchLoop();
      }
    });
    dispatchThread.setPriority(Thread.MIN_PRIORITY);
    dispatchThread.setName("DispatchThread");
    dispatchThread.start();
    globalCountersUpdatesTimer = new Timer("GlobalCountersUpdates");
    //  Report counters update every second.
    globalCountersUpdatesTimer.schedule(new TimerTask() {
      @Override
      public void run() {
        reportPeriodicStats();
      }
    }, 1000, 1000);
    reportHarnessStartup();
  }

  public void stop() {
    try {
      if (globalCountersUpdatesTimer != null) {
        globalCountersUpdatesTimer.cancel();
      }
      if (statusServer != null) {
        statusServer.stop();
      }
      running.set(false);
      dispatchThread.join();
      workUnitExecutor.shutdown();
      if (!workUnitExecutor.awaitTermination(5, TimeUnit.MINUTES)) {
        throw new RuntimeException("Work executor did not terminate within 5 minutes");
      }
      for (ConcurrentLinkedQueue<WorkerAndContext> queue : mapTaskExecutors.values()) {
        WorkerAndContext workerAndContext;
        while ((workerAndContext = queue.poll()) != null) {
          workerAndContext.getWorker().close();
        }
      }
      commitExecutor.shutdown();
      if (!commitExecutor.awaitTermination(5, TimeUnit.MINUTES)) {
        throw new RuntimeException("Commit executor did not terminate within 5 minutes");
      }
    } catch (Exception e) {
      LOG.warn("Exception while shutting down: ", e);
    }
  }

  public void runStatusServer(int statusPort) {
    statusServer = new Server(statusPort);
    statusServer.setHandler(new StatusHandler());
    try {
      statusServer.start();
      LOG.info("Status server started on port {}", statusPort);
      statusServer.join();
    } catch (Exception e) {
      LOG.warn("Status server failed to start: ", e);
    }
  }

  private void addComputation(MapTask mapTask) {
    String computationId =
        systemNameToComputationIdMap.containsKey(mapTask.getSystemName())
            ? systemNameToComputationIdMap.get(mapTask.getSystemName())
            : mapTask.getSystemName();
    if (!instructionMap.containsKey(computationId)) {
      LOG.info("Adding config for {}: {}", computationId, mapTask);
      commitQueue.addQueue(computationId);
      instructionMap.put(computationId, mapTask);
      mapTaskExecutors.put(computationId, new ConcurrentLinkedQueue<WorkerAndContext>());
      activeWorkMap.put(computationId, new ActiveWorkForComputation(workUnitExecutor));
      readerCache.put(
          computationId, new ConcurrentHashMap<ByteString, ReaderCacheEntry>());
    }
  }

  private static void sleep(int millis) {
    try {
      Thread.sleep(millis);
    } catch (InterruptedException e) {
      // NOLINT
    }
  }

  private void dispatchLoop() {
    LOG.info("Dispatch starting");
    while (running.get()) {
      memoryMonitor.waitForResources("GetWork");

      int backoff = 1;
      Windmill.GetWorkResponse workResponse;
      do {
        workResponse = getWork();
        if (workResponse.getWorkCount() > 0) {
          break;
        }
        sleep(backoff);
        backoff = Math.min(1000, backoff * 2);
      } while (running.get());
      for (final Windmill.ComputationWorkItems computationWork : workResponse.getWorkList()) {
        final String computation = computationWork.getComputationId();
        if (!instructionMap.containsKey(computation)) {
          getConfig(computation);
        }
        final MapTask mapTask = instructionMap.get(computation);
        if (mapTask == null) {
          LOG.warn(
              "Received work for unknown computation: {}. Known computations are {}",
              computation, instructionMap.keySet());
          continue;
        }

        // May be null if input watermark not yet known.
        // TODO: Can assert this is non-null once Windmill waits for known input watermark.
        @Nullable
        final Instant inputDataWatermark =
            WindmillTimeUtils.windmillToHarnessInputWatermark(
                computationWork.getInputDataWatermark());
        ActiveWorkForComputation activeWork = activeWorkMap.get(computation);
        for (final Windmill.WorkItem workItem : computationWork.getWorkList()) {
          // May be null if output watermark not yet known.
          @Nullable
          final Instant outputDataWatermark =
              WindmillTimeUtils.windmillToHarnessOutputWatermark(
                  workItem.getOutputDataWatermark());
          Preconditions.checkState(inputDataWatermark == null || outputDataWatermark == null
              || !outputDataWatermark.isAfter(inputDataWatermark));
          Work work = new Work(workItem.getWorkToken()) {
            @Override
            public void run() {
              process(computation, mapTask, inputDataWatermark, outputDataWatermark, workItem);
            }
          };
          if (activeWork.activateWork(workItem.getKey(), work)) {
            workUnitExecutor.execute(work);
          }
        }
      }
    }
    LOG.info("Dispatch done");
  }

  abstract static class Work implements Runnable {
    private final long workToken;
    public Work(long workToken) {
      this.workToken = workToken;
    }
    public long getWorkToken() {
      return workToken;
    }
  }

  private void process(final String computation, final MapTask mapTask,
      @Nullable final Instant inputDataWatermark, @Nullable final Instant outputDataWatermark,
      final Windmill.WorkItem work) {
    LOG.debug("Starting processing for {}:\n{}", computation, work);

    Windmill.WorkItemCommitRequest.Builder outputBuilder =
        Windmill.WorkItemCommitRequest.newBuilder()
        .setKey(work.getKey())
        .setWorkToken(work.getWorkToken());

    StreamingModeExecutionContext context = null;
    MapTaskExecutor worker = null;

    try {
      DataflowWorkerLoggingMDC.setWorkId(
          work.getKey().toStringUtf8() + "-" + Long.toString(work.getWorkToken()));
      DataflowWorkerLoggingMDC.setStageName(computation);
      WorkerAndContext workerAndContext = mapTaskExecutors.get(computation).poll();
      if (workerAndContext == null) {
        CounterSet counters = new CounterSet();
        context = new StreamingModeExecutionContext(mapTask.getSystemName(),
            readerCache.get(computation), stateNameMap, stateCache.forComputation(computation));
        StateSampler sampler =
            new StateSampler(mapTask.getStageName() + "-", counters.getAddCounterMutator());
        // In streaming mode, state samplers are long lived. So here a unique id is generated as
        // the item_id for the userCodeTimeTracker.
        int stateSamplerId = nextStateSamplerId.incrementAndGet();
        sampler.addSamplingCallback(
            new UserCodeTimeTracker.StateSamplerCallback(
                userCodeTimeTracker, stateSamplerId));
        // "work" will never finish here.
        userCodeTimeTracker.workStarted(
            sampler.getPrefix(), stateSamplerId, counters.getAddCounterMutator());
        worker = MapTaskExecutorFactory.create(options, mapTask, context, counters, sampler);
        ReadOperation readOperation = worker.getReadOperation();
        // Disable progress updates since its results are unused for streaming
        // and involves starting a thread.
        readOperation.setProgressUpdatePeriodMs(ReadOperation.DONT_UPDATE_PERIODICALLY);
        Preconditions.checkState(
            worker.supportsRestart(), "Streaming runner requires all operations support restart.");

        // If using a custom source, count bytes read for autoscaling.
        ParallelInstruction read = mapTask.getInstructions().get(0);
        if (CustomSources.class.getName().equals(
                read.getRead().getSource().getSpec().get("@type"))) {
          Coder<?> coder = Serializer.deserialize(read.getOutputs().get(0).getCodec(), Coder.class);
          readOperation.receivers[0].addOutputCounter(
              new OutputObjectAndByteCounter(
                  new MapTaskExecutorFactory.ElementByteSizeObservableCoder<>(coder),
                  worker.getOutputCounters().getAddCounterMutator())
                  .setSamplingPeriod(100)
                  .countBytes("dataflow_input_size-" + mapTask.getSystemName()));
        }
      } else {
        worker = workerAndContext.getWorker();
        context = workerAndContext.getContext();
      }

      WindmillStateReader stateReader = new WindmillStateReader(
          metricTrackingWindmillServer, computation, work.getKey(), work.getWorkToken());
      StateFetcher localStateFetcher = stateFetcher.byteTrackingView();
      context.start(work, inputDataWatermark, outputDataWatermark, stateReader, localStateFetcher,
          outputBuilder);

      for (Long callbackId : context.getReadyCommitCallbackIds()) {
        final Runnable callback = commitCallbacks.remove(callbackId);
        if (callback != null) {
          workUnitExecutor.forceExecute(new Runnable() {
              @Override
              public void run() {
                try {
                  callback.run();
                } catch (Throwable t) {
                  // TODO: Count interesting failures.
                  LOG.error("Source checkpoint finalization failed:", t);
                }
              }
            });
        }
      }

      // Blocks while executing work.
      worker.execute();

      commitCallbacks.putAll(context.flushState());

      // Compute shuffle and state byte statistics after the work is completely done, but before
      // counters are added to the outputBuilder.
      long shuffleBytesRead = 0;
      for (Windmill.InputMessageBundle bundle : work.getMessageBundlesList()) {
        for (Windmill.Message message : bundle.getMessagesList()) {
          shuffleBytesRead += message.getSerializedSize();
        }
      }
      long stateBytesRead = stateReader.getBytesRead() + localStateFetcher.getBytesRead();
      long stateBytesWritten = Windmill.WorkItemCommitRequest.newBuilder(outputBuilder.build())
                                   .clearOutputMessages()
                                   .build()
                                   .getSerializedSize();
      CounterSet counters = worker.getOutputCounters();
      counters
          .getAddCounterMutator()
          .addCounter(Counter.longs("WindmillShuffleBytesRead", Counter.AggregationKind.SUM))
          .addValue(shuffleBytesRead);
      counters
          .getAddCounterMutator()
          .addCounter(Counter.longs("WindmillStateBytesRead", Counter.AggregationKind.SUM))
          .addValue(stateBytesRead);
      counters
          .getAddCounterMutator()
          .addCounter(Counter.longs("WindmillStateBytesWritten", Counter.AggregationKind.SUM))
          .addValue(stateBytesWritten);

      buildCounters(counters, outputBuilder);

      mapTaskExecutors.get(computation).offer(new WorkerAndContext(worker, context));
      worker = null;
      context = null;

      commitQueue.put(computation, outputBuilder.build());
      scheduleCommit();

      LOG.debug("Processing done for work token: {}", work.getWorkToken());
    } catch (Throwable t) {
      if (worker != null) {
        try {
          worker.close();
        } catch (Exception e) {
          LOG.warn("Failed to close worker: ", e);
        } finally {
          // Release references to potentially large objects early.
          worker = null;
          context = null;
        }
      }

      t = t instanceof UserCodeException ? t.getCause() : t;

      if (isOutOfMemoryError(t)) {
        reportFailure(computation, work, t);
        LOG.error("Received OutOfMemoryError, crashing.  Error was ", t);
        System.exit(1);
      } else if (isKeyTokenInvalidException(t)) {
        LOG.debug(
            "Execution of work for {} for key {} failed due to token expiration, "
            + "will not retry locally.",
            computation, work.getKey().toStringUtf8());
        activeWorkMap.get(computation).completeWork(work.getKey());
      } else {
        LOG.error(
            "Execution of work for {} for key {} failed, retrying.",
            computation,
            work.getKey().toStringUtf8());
        LOG.error("\nError: ", t);
        lastException.set(t);
        LOG.debug("Failed work: {}", work);
        if (reportFailure(computation, work, t)) {
          // Try again, after some delay and at the end of the queue to avoid a tight loop.
          sleep(10000);
          workUnitExecutor.forceExecute(new Runnable() {
            @Override
            public void run() {
              process(computation, mapTask, inputDataWatermark, outputDataWatermark, work);
            }
          });
        } else {
          // If we failed to report the error, the item is invalid and should
          // not be retried internally.  It will be retried at the higher level.
          LOG.debug("Aborting processing due to exception reporting failure");
          activeWorkMap.get(computation).completeWork(work.getKey());
        }
      }
    } finally {
      DataflowWorkerLoggingMDC.setWorkId(null);
      DataflowWorkerLoggingMDC.setStageName(null);
    }
  }

  private void scheduleCommit() {
    commitExecutor.execute(new Commit());
  }

  private class Commit implements Runnable {
    @Override
    public void run() {
      while (true) {
        Windmill.CommitWorkRequest.Builder commitRequestBuilder =
            Windmill.CommitWorkRequest.newBuilder();
        long remainingCommitBytes = MAX_COMMIT_BYTES;
        for (String computation : commitQueue.keySet()) {
          Windmill.ComputationCommitWorkRequest.Builder computationRequestBuilder =
              Windmill.ComputationCommitWorkRequest.newBuilder();
          while (remainingCommitBytes > 0) {
            Windmill.WorkItemCommitRequest request = commitQueue.poll(computation);
            if (request == null) {
              break;
            }
            remainingCommitBytes -= request.getSerializedSize();
            computationRequestBuilder.addRequests(request);
          }
          if (computationRequestBuilder.getRequestsCount() > 0) {
            computationRequestBuilder.setComputationId(computation);
            commitRequestBuilder.addRequests(computationRequestBuilder);
          }
        }
        if (commitRequestBuilder.getRequestsCount() > 0) {
          Windmill.CommitWorkRequest commitRequest = commitRequestBuilder.build();
          LOG.trace("Commit: {}", commitRequest);
          commitWork(commitRequest);
          for (Windmill.ComputationCommitWorkRequest computationRequest :
              commitRequest.getRequestsList()) {
            ActiveWorkForComputation activeWork =
                activeWorkMap.get(computationRequest.getComputationId());
            for (Windmill.WorkItemCommitRequest workRequest :
                computationRequest.getRequestsList()) {
              activeWork.completeWork(workRequest.getKey());
            }
          }
        } else {
          break;
        }
      }
    }
  }

  private Windmill.GetWorkResponse getWork() {
    return windmillServer.getWork(
        Windmill.GetWorkRequest.newBuilder()
            .setClientId(clientId)
            .setMaxItems(MAX_GET_WORK_ITEMS)
            .setMaxBytes(MAX_GET_WORK_FETCH_BYTES)
            .build());
  }

  private void commitWork(Windmill.CommitWorkRequest request) {
    windmillServer.commitWork(request);
  }

  private void getConfig(String computation) {
    Windmill.GetConfigRequest request =
        Windmill.GetConfigRequest.newBuilder().addComputations(computation).build();

    Windmill.GetConfigResponse response = windmillServer.getConfig(request);
    for (Windmill.GetConfigResponse.SystemNameToComputationIdMapEntry entry :
        response.getSystemNameToComputationIdMapList()) {
      systemNameToComputationIdMap.put(entry.getSystemName(), entry.getComputationId());
    }
    for (String serializedMapTask : response.getCloudWorksList()) {
      try {
        addComputation(parseMapTask(serializedMapTask));
      } catch (IOException e) {
        LOG.warn("Parsing MapTask failed: {}", serializedMapTask);
        LOG.warn("Error: ", e);
      }
    }
    for (Windmill.GetConfigResponse.NameMapEntry entry : response.getNameMapList()) {
      stateNameMap.put(entry.getUserName(), entry.getSystemName());
    }
  }

  private void buildCounters(CounterSet counterSet,
                             Windmill.WorkItemCommitRequest.Builder builder) {
    for (Counter<?> counter : counterSet) {
      Windmill.Counter.Builder counterBuilder = Windmill.Counter.newBuilder();
      Windmill.Counter.Kind kind;
      Object aggregateObj = null;
      switch (counter.getKind()) {
        case SUM: kind = Windmill.Counter.Kind.SUM; break;
        case MAX: kind = Windmill.Counter.Kind.MAX; break;
        case MIN: kind = Windmill.Counter.Kind.MIN; break;
        case MEAN:
          kind = Windmill.Counter.Kind.MEAN;
          CounterMean<?> mean = counter.getAndResetMeanDelta();
          long count = mean.getCount();
          aggregateObj = mean.getAggregate();
          if (count <= 0) {
            continue;
          }
          counterBuilder.setMeanCount(count);
          break;
        default:
          LOG.debug("Unhandled counter type: {}", counter.getKind());
          continue;
      }
      if (counter.getKind() != AggregationKind.MEAN) {
        aggregateObj = counter.getAndResetDelta();
      }
      if (addKnownTypeToCounterBuilder(aggregateObj, counterBuilder)) {
        counterBuilder.setName(counter.getName()).setKind(kind);
        builder.addCounterUpdates(counterBuilder);
      }
    }
  }

  private boolean addKnownTypeToCounterBuilder(Object aggregateObj,
      Windmill.Counter.Builder counterBuilder) {
    if (aggregateObj instanceof Double) {
      double aggregate = (Double) aggregateObj;
      if (aggregate != 0) {
        counterBuilder.setDoubleScalar(aggregate);
      }
    } else if (aggregateObj instanceof Long) {
      long aggregate = (Long) aggregateObj;
      if (aggregate != 0) {
        counterBuilder.setIntScalar(aggregate);
      }
    } else if (aggregateObj instanceof Integer) {
      long aggregate = ((Integer) aggregateObj).longValue();
      if (aggregate != 0) {
        counterBuilder.setIntScalar(aggregate);
      }
    } else {
      LOG.debug("Unhandled aggregate class: {}", aggregateObj.getClass());
      return false;
    }
    return true;
  }

  private Windmill.Exception buildExceptionReport(Throwable t) {
    Windmill.Exception.Builder builder = Windmill.Exception.newBuilder();

    builder.addStackFrames(t.toString());
    for (StackTraceElement frame : t.getStackTrace()) {
      builder.addStackFrames(frame.toString());
    }
    if (t.getCause() != null) {
      builder.setCause(buildExceptionReport(t.getCause()));
    }

    return builder.build();
  }

  // Returns true if reporting the exception is successful and the work should be retried.
  private boolean reportFailure(String computation, Windmill.WorkItem work, Throwable t) {
    Windmill.ReportStatsResponse response =
        windmillServer.reportStats(Windmill.ReportStatsRequest.newBuilder()
            .setComputationId(computation)
            .setKey(work.getKey())
            .setWorkToken(work.getWorkToken())
            .addExceptions(buildExceptionReport(t))
            .build());
    return !response.getFailed();
  }

  private void reportHarnessStartup() {
    Windmill.Counter.Builder counterBuilder = Windmill.Counter.newBuilder();
    counterBuilder =
        counterBuilder.setName("dataflow_java_harness_restarts")
            .setKind(Windmill.Counter.Kind.SUM)
            .setIntScalar(1);
    Windmill.ReportStatsResponse response = windmillServer.reportStats(
        Windmill.ReportStatsRequest.newBuilder().addCounterUpdates(counterBuilder).build());
    if (response.getFailed()) {
      LOG.warn("Failed to notify windmill on harness startup. dataflow_java_harness_restarts will "
          + " not be incremented.");
    }
  }

  private void reportPeriodicStats() {
    Runtime rt = Runtime.getRuntime();
    long usedMemory = rt.totalMemory() - rt.freeMemory();
    long maxMemory =  rt.maxMemory();
    Windmill.Counter.Builder counterBuilder = Windmill.Counter.newBuilder();
    counterBuilder =
        counterBuilder.setName("dataflow_java_harness_memory_utilization")
            .setKind(Windmill.Counter.Kind.MEAN)
            .setCumulative(true)
            .setIntScalar(usedMemory)
            .setMeanCount(maxMemory);
    Windmill.ReportStatsResponse response = windmillServer.reportStats(
        Windmill.ReportStatsRequest.newBuilder()
            .addCounterUpdates(counterBuilder)
            .build());
    if (response.getFailed()) {
      LOG.warn("Failed to send periodic counters to windmill.");
    }
  }

  /**
   * Class representing the state of active work for a computation.
   *
   * <p>This class is synchronized, but only used from the dispatch and commit threads, so should
   * not be heavily contended.  Still, blocking work should not be done by it.
   */
  static class ActiveWorkForComputation {
    private Map<ByteString, Queue<Work>> activeWork = new HashMap<>();
    private BoundedQueueExecutor executor;

    ActiveWorkForComputation(BoundedQueueExecutor executor) {
      this.executor = executor;
    }

    /**
     * Mark the given key and work as active.  Returns whether the work is ready to be run
     * immediately.
     */
    public synchronized boolean activateWork(ByteString key, Work work) {
      Queue<Work> queue = activeWork.get(key);
      if (queue == null) {
        queue = new LinkedList<>();
        activeWork.put(key, queue);
        queue.add(work);
        return true;
      }
      if (queue.peek().getWorkToken() != work.getWorkToken()) {
        queue.add(work);
      }
      return false;
    }

    /**
     * Marks the work for a the given key as complete.  Schedules queued work for the key if any.
     */
    public synchronized void completeWork(ByteString key) {
      Queue<Work> queue = activeWork.get(key);
      queue.poll();
      if (queue.peek() != null) {
        executor.forceExecute(queue.peek());
      } else {
        activeWork.remove(key);
      }
    }

    public synchronized void printActiveWork(PrintWriter writer) {
      writer.println("<ul>");
      for (Map.Entry<ByteString, Queue<Work>> entry : activeWork.entrySet()) {
        Queue<Work> queue = entry.getValue();
        writer.print("<li>Key: ");
        writer.print(entry.getKey().toStringUtf8());
        writer.print(" Token: ");
        writer.print(queue.peek().getWorkToken());
        if (queue.size() > 1) {
          writer.print("(");
          writer.print(queue.size() - 1);
          writer.print(" queued)");
        }
        writer.println("</li>");
      }
      writer.println("</ul>");
    }
  }

  private static class WorkerAndContext {
    public MapTaskExecutor worker;
    public StreamingModeExecutionContext context;

    public WorkerAndContext(MapTaskExecutor worker, StreamingModeExecutionContext context) {
      this.worker = worker;
      this.context = context;
    }

    public MapTaskExecutor getWorker() {
      return worker;
    }

    public StreamingModeExecutionContext getContext() {
      return context;
    }
  }

  private class StatusHandler extends AbstractHandler {
    @Override
    public void handle(
        String target, Request baseRequest,
        HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

      response.setContentType("text/html;charset=utf-8");
      response.setStatus(HttpServletResponse.SC_OK);
      baseRequest.setHandled(true);

      PrintWriter responseWriter = response.getWriter();

      responseWriter.println("<html><body>");

      if (target.equals("/healthz")) {
        responseWriter.println("ok");
      } else if (target.equals("/threadz")) {
        printThreads(responseWriter);
      } else if (target.equals("/heapz")) {
        dumpHeap(responseWriter);
      } else if (target.equals("/cachez")) {
        stateCache.printDetailedHtml(responseWriter);
      } else {
        printHeader(responseWriter);
        printResources(responseWriter);
        printMetrics(responseWriter);
        printLastException(responseWriter);
        printSpecs(responseWriter);
      }

      responseWriter.println("</body></html>");
    }
  }

  private void printHeader(PrintWriter response) {
    response.println("<h1>Streaming Worker Harness</h1>");
    response.println("Running: " + running.get() + "<br>");
    response.println("ID: " + clientId + "<br>");
  }

  private void printMetrics(PrintWriter response) {
    response.println("<h2>Metrics</h2>");
    response.println("Worker Threads: " + workUnitExecutor.getPoolSize()
        + "/" + workUnitExecutor.getMaximumPoolSize() + "<br>");
    response.println("Active Threads: " + workUnitExecutor.getActiveCount() + "<br>");
    response.println("Work Queue Size: " + workUnitExecutor.getQueue().size()
        + "/" + MAX_WORK_UNITS_QUEUED + "<br>");
    response.print("Commit Queues: (");
    response.print(commitQueue.weight() >> 20);
    response.println("MB)<ul>");
    for (String computation : commitQueue.keySet()) {
      response.print("<li>");
      response.print(computation);
      response.print(": ");
      response.print(commitQueue.queueSize(computation));
      response.println("</li>");
    }
    response.println("</ul>");

    stateCache.printSummaryHtml(response);

    metricTrackingWindmillServer.printHtml(response);

    response.println("Active Keys: <ul>");
    for (Map.Entry<String, ActiveWorkForComputation> computationEntry
             : activeWorkMap.entrySet()) {
      response.print("<li>");
      response.print(computationEntry.getKey());
      response.print(":");
      computationEntry.getValue().printActiveWork(response);
      response.println("</li>");
    }
    response.println("</ul>");
  }

  private void printResources(PrintWriter response) {
    response.append("<h2>Resources</h2>\n");
    response.append("Memory is " + memoryMonitor.describeMemory() + "<br>\n");
  }

  private void printSpecs(PrintWriter response) {
    response.append("<h2>Specs</h2>\n");
    for (Map.Entry<String, MapTask> entry : instructionMap.entrySet()) {
      response.println("<h3>" + entry.getKey() + "</h3>");
      response.print("<script>document.write(JSON.stringify(");
      response.print(entry.getValue().toString());
      response.println(", null, \"&nbsp&nbsp\").replace(/\\n/g, \"<br>\"))</script>");
    }
  }

  private void printLastException(PrintWriter response) {
    Throwable t = lastException.get();
    if (t != null) {
      response.println("<h2>Last Exception</h2>");
      StringWriter writer = new StringWriter();
      t.printStackTrace(new PrintWriter(writer));
      response.println(writer.toString().replace("\t", "&nbsp&nbsp").replace("\n", "<br>"));
    }
  }

  private void printThreads(PrintWriter response) {
    Map<Thread, StackTraceElement[]> stacks = Thread.getAllStackTraces();
    for (Map.Entry<Thread,  StackTraceElement[]> entry : stacks.entrySet()) {
      Thread thread = entry.getKey();
      response.println("Thread: " + thread + " State: " + thread.getState() + "<br>");
      for (StackTraceElement element : entry.getValue()) {
        response.println("&nbsp&nbsp" + element + "<br>");
      }
      response.println("<br>");
    }
  }

  private void dumpHeap(PrintWriter response) {
    try {
      String dumpFile = MemoryMonitor.dumpHeap();
      response.println("Dumped heap to " + dumpFile);
    } catch (Exception e) {
      response.println("Failed to dump heap: <br>");
      StringWriter writer = new StringWriter();
      e.printStackTrace(new PrintWriter(writer));
      response.println(writer.toString().replace("\t", "&nbsp&nbsp").replace("\n", "<br>"));
    }
  }
}


text:
<?php namespace DummyNamespace;

use Illuminate\Database\Eloquent\Model;

class DummyClass extends Model
{
    //
}


text:
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="ProjectModuleManager">
    <modules>
      <module fileurl="file://$PROJECT_DIR$/.idea/sandbox_symphony.iml" filepath="$PROJECT_DIR$/.idea/sandbox_symphony.iml" />
    </modules>
  </component>
</project>


text:
<?php

/**
 * O3 Engine debug
 *
 * Used for debuging. O3 debuger shows php and javascript errors in the same web browsers consol window.
 *
 * @package o3
 * @link    todo: add url
 * @author  Zotlan Fischer <zoli_fischer@yahoo.com>
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */

//o3 debug/log types
if ( !defined( 'O3_LOG' ) )
	/** int Value: 1 */
	define('O3_LOG',1);
if ( !defined( 'O3_DEBUG' ) )
	/** int Value: 2 */
	define('O3_DEBUG',2);

//o3 debug allow debuging
if ( !defined( 'O3_DEBUG_ALLOWED' ) )
	/** boolean Value: false */
	define('O3_DEBUG_ALLOWED',false);

/**
 * O3 Engine debug class
 *
 * Used for debuging. O3 debuger shows php and javascript errors in the same web browsers consol window.
 *
 * @example example.php
 * //as O3 Engine module

 * $o3 = new o3();

 * $o3->debug->('Insert into log file and debug console', O3_LOG | O3_DEBUG);

 * $o3->debug->('Insert into debug console only');

 * //as o3_debug class

 * $o3_debug = new o3_debug();

 * $o3_debug->('Insert into console');

 * $o3_debug->show( true );
 *
 * @package o3_debug
 */
class o3_debug {

	/** mixed Parent object */
	public $parent = null;

	/** boolean Set to show or hide the debug list */
	private $is_show = true;

	/** array List of debug messages */
	private $_s = array();

	/** int Script start time in milliseconds */
	private $start_time = 0;

	/** int Script end time in milliseconds */
	private $end_time = 0;

	/** int Total memory used by the script */
	private $total_memory_usage = 0;

	/** int Peak memory used by the script */
	private $peak_memory_usage = 0;

	/** int Resource used by the script */
	private $resource_usage = false;

	/** array List of shutdown callback functions */
	private $shutdown_callbacks = array();

	/** array List of output buffer callback functions */
	private $ob_start_callbacks = array();

	/**
	 * O3 debug constructor
	 * @return void
	 */
	public function __construct() {
		ob_start( array( $this, 'ob_start_callback' ) ); //set output buffer callback
		register_shutdown_function( array( $this, 'shutdown_function' ) ); //set shutdown callback

		$this->start_time = o3_micro_time(); //store start time
		$this->_('Started: '.date('d.m.Y H:i',$this->start_time));

		//set custom error handler
		set_error_handler( array( $this, 'errorHandler' ) );

	}

 	/**
	 * Custom error handler
	 *
	 * Errors are trasformed to consol error.

 Only E_USER_ERROR stops the script.
	 *
	 * @link http://www.php.net/manual/en/function.set-error-handler.php
	 *
	 * @param int $errno Level of the error raised
	 * @param string $errstr Error message
	 * @param string $errfile Filename that the error was raised
	 * @param int $errline Line number the error was raised at
	 * @param array $errcontext Array of every variable that existed in the scope the error was triggered in. User error handler must not modify error context.
	 *
	 * @return boolean
	 */
	public function errorHandler( $errno, $errstr, $errfile, $errline, $errcontext ) {

	 	//check for error reporting level
		//if ( $errno & error_reporting() ) {
			switch ($errno) {
				case E_USER_ERROR:
				case E_ERROR:
				    $this->_( "[Error:$errno] $errstr", O3_LOG | O3_DEBUG, 0, $errno );
					$this->_( "On line $errline in $errfile", O3_LOG | O3_DEBUG, 0, $errno );
					$this->die_(); //stop script
				    break;
				case E_USER_WARNING:
				case E_WARNING:
				    $this->_( "[Warning:$errno] $errstr", O3_LOG | O3_DEBUG, 0, $errno );
					$this->_( "On line $errline in $errfile", O3_LOG | O3_DEBUG, 0, $errno );
				    break;
				case E_USER_NOTICE:
				case E_NOTICE:
				    $this->_( "[Notice:$errno] $errstr", O3_LOG | O3_DEBUG, 0, $errno );
					$this->_( "On line $errline in $errfile", O3_LOG | O3_DEBUG, 0, $errno );
				    break;
				default:
				    $this->_( "[Unknown:$errno] $errstr", O3_LOG | O3_DEBUG, 0, $errno );
					$this->_( "On line $errline in $errfile", O3_LOG | O3_DEBUG, 0, $errno );
					break;
			}
		//}

		/* Don't execute PHP internal error handler */
		return true;
	}

	/**
	 * Output a message and terminate the current script
	 *
	 * @param string $message Function prints the status just before exiting
	 * @param int $message (optional) Value will be used as the exit status and not printed
	 *
	 * @return void
	 */
	public function die_( $message = '' ) {
		die( $message );
	}

	/**
	 * Function sets the parent object
	 *
	 * @param object $parent Parent object
	 *
	 * @return object
	 */
	public function set_parent( $parent ) {
		return $this->parent = &$parent;
	}

	/**
	* Same as add.
	*
	* @param string $message Message to display
	* @param int $type (optional) Debug message type. Accepted values: O3_DEBUG (default) or O3_LOG.
	* @param int $time (optional) Timestamp for the date to display. Value is current time if no timestamp is given.
	* @param int $error_code (optional) Set error code. Default value is E_USER_NOTICE.
	*
	* @see o3_debug::add() o3_debug::add()
	*/
	public function _( $message, $type = O3_DEBUG, $time = 0, $error_code = E_USER_NOTICE ) {
		$this->add( $message, $type, $time, $error_code );
	}

	/**
	* Add new line to the debug message list.
	*
	* @param string $message Message to display
	* @param int $type (optional) Debug message type. Accepted values: O3_DEBUG (default) or O3_LOG.
	* @param int $time (optional) Timestamp for the date to display. Value is current time if no timestamp is given.
	* @param int $error_code (optional) Set error code. Default value is E_USER_NOTICE.
	*
	* @see o3_debug_list_item o3_debug_list_item
	* @see O3_DEBUG O3_DEBUG
	* @see O3_LOG O3_LOG
	*
	* @return void
	*/
	public function add( $message, $type = O3_DEBUG, $time = 0, $error_code = E_USER_NOTICE ) {
		$time = $time > 0 ? time() : '';
		$this->_s[] = new o3_debug_list_item( $message, $time, $type, $error_code );
	}

	/**
	* Set to show the debug messages in the console.
	*
	* @param boolean $value (optional) Values: true - show, false - don't show
	*
	* @return void
	*/
	public function show( $value = false ) {
		$this->is_show = $value;
	}

	/**
	 * Function injects the debug javascript script in html code after the head tag.
	 *
	 * Custom output buffer callbacks are also executed.

	 * The code is only injected if the show was allowed.
	 * The injected script creates the console object if not exists in the web browser.
	 *
	 * @see o3_debug::show() o3_debug::show()
	 * @see o3_debug::add_ob_start_callback() o3_debug::add_ob_start_callback()
	 *
	 * @param string $buffer
	 *
	 * @return void
	 */
	public function ob_start_callback( $buffer ) {

		//callbacks
		foreach ( $this->ob_start_callbacks as $key => $value ) {
			if ( !is_array($value) ) {
				$buffer = call_user_func( $value, $buffer );
			} else if ( count($value) == 2 ) {
				$buffer = call_user_func( array( $value[0], $value[1] ), $buffer );
			}
		}

		if ( $this->is_show && O3_DEBUG_ALLOWED ) {
			$scriptData = array();
			$consoleData = array();
			foreach ( $this->_s as $key => $value ) {
				if ( $value->type & O3_DEBUG ) { //&& ( $value->error_code & error_reporting() )
					$scriptData[] = "console.log('".addslashes($value->prnt(false))."');";
					$consoleData[] = $value->prnt(false);
				}
			}

			$scriptCode = '
	';

			$obj = json_decode( $buffer );
			//if json add debug to object
			if ( is_object($obj) ) {
				$obj->o3_console = $consoleData;
				return json_encode($obj);
			} else {
				//split html at head tag
				$matches = preg_split('/()/i', $buffer, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
				if ( count($matches) > 1 ) {

					$title_matches = preg_split('/(<\/title.*?>)/i', $buffer, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
					if ( count($title_matches) > 1 ) {

						$buffer = $title_matches[0] . $title_matches[1] . $scriptCode;
						for ( $i = 2; $i < count($title_matches); $i++ )
							$buffer .= $title_matches[$i];

					} else {
						//assemble the html output back with the script code in it
						$buffer = $matches[0] . $matches[1] . ( !preg_match( '/(<header)/i', $matches[1]) ? $scriptCode : '' );
						for ( $i = 2; $i < count($matches); $i++ )
							$buffer .= $matches[$i];
					}

				}
			}
		}
		return $buffer;
	}

	/**
	 * Runed before script shutdown.
	 *
	 * Function added as shutdown callback in the contstructor.

	 * The functions runs the added custom debug shutdown callbacks.
	 *
	 * @see o3_debug::$shutdown_callbacks o3_debug::$shutdown_callbacks
	 * @see o3_debug::add_shutdown_callback() o3_debug::add_shutdown_callback()
	 *
	 * @return void
	 */
	public function shutdown_function() {
		$this->end_time = o3_micro_time(); //store start time
		$this->total_memory_usage = memory_get_usage();
		$this->peak_memory_usage = memory_get_peak_usage();

		$this->_('Total memory usage: '.o3_bytes_display('v U',$this->total_memory_usage));
		$this->_('Peak memory usage: '.o3_bytes_display('v U',$this->peak_memory_usage));

		if ( function_exists('getrusage') ) {
			$this->resource_usage = getrusage();
			$this->_('CPU usage: '. ($this->resource_usage['ru_utime.tv_sec'] + $this->resource_usage['ru_utime.tv_usec'] / 1000000) );
			$this->_('CPU system usage: '. ($this->resource_usage['ru_stime.tv_sec'] + $this->resource_usage['ru_stime.tv_usec'] / 1000000) );
		}

		$this->_('Duration: '.number_format($this->end_time-$this->start_time,4).' sec');
		$this->_('Ended: '.date('d.m.Y H:i',$this->end_time));

		foreach ( $this->shutdown_callbacks as $key => $value ) {
			if ( !is_array($value) ) {
				call_user_func( $value, '', $this->_s );
			} else if ( count($value) == 2 ) {
				call_user_func( array( $value[0], $value[1] ), '', $this->_s );
			}
		}
	}

	/**
	 * Add custom script shutdown callback.
	 *
	 * The added function will be executed at script shutdown.
	 *
	 * @see o3_debug::$shutdown_callbacks o3_debug::$shutdown_callbacks
	 * @see o3_debug::add_shutdown_callback() o3_debug::add_shutdown_callback()
	 *
	 * @param string|array $data String is name of the function. Array with 2 elements first is the object and second is the function.
	 *
	 * @return void
	 */
	public function add_shutdown_callback( $data ) {
		$this->shutdown_callbacks[] = $data;
	}

	/**
	 * Add custom output buffer callbacks.
	 *
	 * The added function will called at output buffer flush.
 The buffer is passed to the function and should return a string.
	 *
	 * @see o3_debug::$ob_start_callbacks o3_debug::$ob_start_callbacks
	 *
	 * @param string|array $data String is name of the function. Array with 2 elements first is the object and second is the function.
	 *
	 * @return void
	 */
	public function add_ob_start_callback( $data ) {
		$this->ob_start_callbacks[] = $data;
	}

	/**
	* Destructor of debug
	*
	* @return void
	*/
	public function __destruct() {}

}

/**
 * O3 Engine debug item class
 *
 * Used for storing debug message.
 *
 * @package o3_debug
 */
class o3_debug_list_item {

	/** string Debug message */
	public $message = '';

	/** int Timestamp for the date to display */
	public $timestamp = 0;

	/** int Type of item */
	public $type = O3_DEBUG;

	/** int Error code value */
	public $error_code = E_USER_NOTICE;

	/**
	 * O3 debug item constructor
	 *
	 * @see O3_DEBUG O3_DEBUG
	 * @see O3_LOG O3_LOG
	 *
	 * @param string $message Message to display
	 * @param int $timestamp (optional) Timestamp for the date to display. Value is current time if no timestamp is given.
	 * @param int $type (optional) Debug message type. Accepted values: O3_DEBUG (default) or O3_LOG.
	 *
	 * @return void
	 */
	public function __construct( $message, $timestamp = 0, $type = O3_DEBUG, $error_code = E_USER_NOTICE ) {
		$this->message = $message;
		$this->timestamp = $timestamp == 0 ? time() : '';
		$this->type = $type;
		$this->error_code = $error_code;
	}

	/**
	 * Prepare message for output
	 *
	 * @param boolean $print (optional) If true the message will be printed else the function returns the message as a string.
	 *
	 * @return string
	 */
	public function prnt( $print = true ) {
	  $b = is_string($this->message) ? preg_replace("/[\n\r|\r|\n]/"," ",$this->message) : preg_replace("/[\n\r|\r|\n]/","",var_export($this->message,true));
		if ( $print )
			echo $b;
		return $b;
	}

	/**
	 * Destructor of O3
	 *
	 * @return void
	 */
	public function __destruct() {}

}

?>


text:
require_relative '../spec_helper'

describe Fix::Bloomberg do

  describe '.alias_namespace!' do
    it 'should already have been called' do
      expect(FB).to be(Fix::Bloomberg)
    end
  end

end


text:
<?php

defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * PDO Database Adapter Class
 *
 * Note: _DB is an extender class that the app controller
 * creates dynamically based on whether the query builder
 * class is being used or not.
 *
 * @package		CodeIgniter
 * @subpackage	Drivers
 * @category	Database
 * @author		EllisLab Dev Team
 * @link		http://codeigniter.com/user_guide/database/
 */
class CI_DB_pdo_driver extends CI_DB {

	/**
	 * Database driver
	 *
	 * @var	string
	 */
	public $dbdriver = 'pdo';

	/**
	 * Transaction enabled flag
	 *
	 * @var	bool
	 */
	public $trans_enabled = FALSE;

	/**
	 * PDO Options
	 *
	 * @var	array
	 */
	public $options = array();

	// --------------------------------------------------------------------

	/**
	 * Class constructor
	 *
	 * Validates the DSN string and/or detects the subdriver.
	 *
	 * @param	array	$params
	 * @return	void
	 */
	public function __construct($params)
	{
		parent::__construct($params);

		if (preg_match('/([^;]+):/', $this->dsn, $match) && count($match) === 2)
		{
			// If there is a minimum valid dsn string pattern found, we're done
			// This is for general PDO users, who tend to have a full DSN string.
			$this->subdriver = $match[1];
			return;
		}
		// Legacy support for DSN specified in the hostname field
		elseif (preg_match('/([^;]+):/', $this->hostname, $match) && count($match) === 2)
		{
			$this->dsn = $this->hostname;
			$this->hostname = NULL;
			$this->subdriver = $match[1];
			return;
		}
		elseif (in_array($this->subdriver, array('mssql', 'sybase'), TRUE))
		{
			$this->subdriver = 'dblib';
		}
		elseif ($this->subdriver === '4D')
		{
			$this->subdriver = '4d';
		}
		elseif ( ! in_array($this->subdriver, array('4d', 'cubrid', 'dblib', 'firebird', 'ibm', 'informix', 'mysql', 'oci', 'odbc', 'sqlite', 'sqlsrv'), TRUE))
		{
			log_message('error', 'PDO: Invalid or non-existent subdriver');

			if ($this->db_debug)
			{
				show_error('Invalid or non-existent PDO subdriver');
			}
		}

		$this->dsn = NULL;
	}

	// --------------------------------------------------------------------

	/**
	 * Database connection
	 *
	 * @param	bool	$persistent
	 * @return	object
	 */
	public function db_connect($persistent = FALSE)
	{
		$this->options[PDO::ATTR_PERSISTENT] = $persistent;

		// Connecting...
		try
		{
			return @new PDO($this->dsn, $this->username, $this->password, $this->options);
		}
		catch (PDOException $e)
		{
			if ($this->db_debug && empty($this->failover))
			{
				$this->display_error($e->getMessage(), '', TRUE);
			}

			return FALSE;
		}
	}

	// --------------------------------------------------------------------

	/**
	 * Persistent database connection
	 *
	 * @return	object
	 */
	public function db_pconnect()
	{
		return $this->db_connect(TRUE);
	}

	// --------------------------------------------------------------------

	/**
	 * Database version number
	 *
	 * @return	string
	 */
	public function version()
	{
		if (isset($this->data_cache['version']))
		{
			return $this->data_cache['version'];
		}
		elseif ( ! $this->conn_id)
		{
			$this->initialize();
		}

		// Not all subdrivers support the getAttribute() method
		try
		{
			return $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION);
		}
		catch (PDOException $e)
		{
			return parent::version();
		}
	}

	// --------------------------------------------------------------------

	/**
	 * Execute the query
	 *
	 * @param	string	$sql	SQL query
	 * @return	mixed
	 */
	protected function _execute($sql)
	{
		return $this->conn_id->query($sql);
	}

	// --------------------------------------------------------------------

	/**
	 * Begin Transaction
	 *
	 * @param	bool	$test_mode
	 * @return	bool
	 */
	public function trans_begin($test_mode = FALSE)
	{
		// When transactions are nested we only begin/commit/rollback the outermost ones
		if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
		{
			return TRUE;
		}

		// Reset the transaction failure flag.
		// If the $test_mode flag is set to TRUE transactions will be rolled back
		// even if the queries produce a successful result.
		$this->_trans_failure = ($test_mode === TRUE);

		return $this->conn_id->beginTransaction();
	}

	// --------------------------------------------------------------------

	/**
	 * Commit Transaction
	 *
	 * @return	bool
	 */
	public function trans_commit()
	{
		// When transactions are nested we only begin/commit/rollback the outermost ones
		if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
		{
			return TRUE;
		}

		return $this->conn_id->commit();
	}

	// --------------------------------------------------------------------

	/**
	 * Rollback Transaction
	 *
	 * @return	bool
	 */
	public function trans_rollback()
	{
		// When transactions are nested we only begin/commit/rollback the outermost ones
		if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
		{
			return TRUE;
		}

		return $this->conn_id->rollBack();
	}

	// --------------------------------------------------------------------

	/**
	 * Escape String
	 *
	 * @param	string	$str
	 * @param	bool	$like	Whether or not the string will be used in a LIKE condition
	 * @return	string
	 */
	public function escape_str($str, $like = FALSE)
	{
		if (is_array($str))
		{
			foreach ($str as $key => $val)
			{
				$str[$key] = $this->escape_str($val, $like);
			}

			return $str;
		}

		// Escape the string
		$str = $this->conn_id->quote($str);

		// If there are duplicated quotes, trim them away
		if ($str[0] === "'")
		{
			$str = substr($str, 1, -1);
		}

		// escape LIKE condition wildcards
		if ($like === TRUE)
		{
			return str_replace(array($this->_like_escape_chr, '%', '_'),
						array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
						$str);
		}

		return $str;
	}

	// --------------------------------------------------------------------

	/**
	 * Affected Rows
	 *
	 * @return	int
	 */
	public function affected_rows()
	{
		return is_object($this->result_id) ? $this->result_id->rowCount() : 0;
	}

	// --------------------------------------------------------------------

	/**
	 * Insert ID
	 *
	 * @param	string	$name
	 * @return	int
	 */
	public function insert_id($name = NULL)
	{
		return $this->conn_id->lastInsertId($name);
	}

	// --------------------------------------------------------------------

	/**
	 * Field data query
	 *
	 * Generates a platform-specific query so that the column data can be retrieved
	 *
	 * @param	string	$table
	 * @return	string
	 */
	protected function _field_data($table)
	{
		return 'SELECT TOP 1 * FROM '.$this->protect_identifiers($table);
	}

	// --------------------------------------------------------------------

	/**
	 * Error
	 *
	 * Returns an array containing code and message of the last
	 * database error that has occured.
	 *
	 * @return	array
	 */
	public function error()
	{
		$error = array('code' => '00000', 'message' => '');
		$pdo_error = $this->conn_id->errorInfo();

		if (empty($pdo_error[0]))
		{
			return $error;
		}

		$error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0];
		if (isset($pdo_error[2]))
		{
			 $error['message'] = $pdo_error[2];
		}

		return $error;
	}

	// --------------------------------------------------------------------

	/**
	 * Update_Batch statement
	 *
	 * Generates a platform-specific batch update string from the supplied data
	 *
	 * @param	string	$table	Table name
	 * @param	array	$values	Update data
	 * @param	string	$index	WHERE key
	 * @return	string
	 */
	protected function _update_batch($table, $values, $index)
	{
		$ids = array();
		foreach ($values as $key => $val)
		{
			$ids[] = $val[$index];

			foreach (array_keys($val) as $field)
			{
				if ($field !== $index)
				{
					$final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
				}
			}
		}

		$cases = '';
		foreach ($final as $k => $v)
		{
			$cases .= $k.' = CASE '."\n";

			foreach ($v as $row)
			{
				$cases .= $row."\n";
			}

			$cases .= 'ELSE '.$k.' END, ';
		}

		$this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);

		return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
	}

	// --------------------------------------------------------------------

	/**
	 * Truncate statement
	 *
	 * Generates a platform-specific truncate string from the supplied data
	 *
	 * If the database does not support the TRUNCATE statement,
	 * then this method maps to 'DELETE FROM table'
	 *
	 * @param	string	$table
	 * @return	string
	 */
	protected function _truncate($table)
	{
		return 'TRUNCATE TABLE '.$table;
	}

}

/* End of file pdo_driver.php */
/* Location: ./system/database/drivers/pdo/pdo_driver.php */


text:
unless [1,2].uniq{}.size == 1
  require 'backports/tools'

  class Array
    def uniq_with_block
      return uniq_without_block unless block_given?
      h = {}
      each do |elem|
        h[yield(elem)] ||= elem
      end
      h.values
    end
    Backports.alias_method_chain self, :uniq, :block
  end
end

unless [1,2].uniq!{}
  require 'backports/tools'

  class Array
    def uniq_with_block!
      replace self if frozen? # force error
      return uniq_without_block! unless block_given?
      u = uniq{|e| yield e}
      replace u unless u.size == size
    end
    Backports.alias_method_chain self, :uniq!, :block
  end
end