text:
package ca.uhn.fhir.cli;

import ca.uhn.fhir.i18n.Msg;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.hl7.fhir.convertors.factory.VersionConvertorFactory_30_40;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r4.model.ConceptMap;
import org.hl7.fhir.r4.model.ConceptMap.ConceptMapGroupComponent;
import org.hl7.fhir.r4.model.ConceptMap.SourceElementComponent;
import org.hl7.fhir.r4.model.ConceptMap.TargetElementComponent;
import org.hl7.fhir.r4.model.Enumerations;
import org.hl7.fhir.r4.model.UriType;

import java.io.IOException;
import java.io.Reader;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;

import static org.apache.commons.lang3.StringUtils.defaultString;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

public class ImportCsvToConceptMapCommand extends AbstractImportExportCsvConceptMapCommand {
	// TODO: Don't use qualified names for loggers in HAPI CLI.
	private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ImportCsvToConceptMapCommand.class);

	protected static final String SOURCE_VALUE_SET_PARAM = "i";
	protected static final String SOURCE_VALUE_SET_PARAM_LONGOPT = "input";
	protected static final String SOURCE_VALUE_SET_PARAM_NAME = "input";
	protected static final String SOURCE_VALUE_SET_PARAM_DESC = "The source value set of the ConceptMap to be imported (i.e. ConceptMap.sourceUri).";
	protected static final String TARGET_VALUE_SET_PARAM = "o";
	protected static final String TARGET_VALUE_SET_PARAM_LONGOPT = "output";
	protected static final String TARGET_VALUE_SET_PARAM_NAME = "output";
	protected static final String TARGET_VALUE_SET_PARAM_DESC = "The target value set of the ConceptMap to be imported (i.e. ConceptMap.targetUri).";

	protected String sourceValueSet;
	protected String targetValueSet;

	private boolean hasElements;
	private boolean hasTargets;

	@Override
	public String getCommandDescription() {
		return "Imports a CSV file to a ConceptMap resource.";
	}

	@Override
	public String getCommandName() {
		return "import-csv-to-conceptmap";
	}

	@Override
	public Options getOptions() {
		Options options = super.getOptions();

		addRequiredOption(options, CONCEPTMAP_URL_PARAM, CONCEPTMAP_URL_PARAM_LONGOPT, CONCEPTMAP_URL_PARAM_NAME, CONCEPTMAP_URL_PARAM_DESC);
		// </editor-fold desc="Additional parameters.">
		addOptionalOption(options, SOURCE_VALUE_SET_PARAM, SOURCE_VALUE_SET_PARAM_LONGOPT, SOURCE_VALUE_SET_PARAM_NAME, SOURCE_VALUE_SET_PARAM_DESC);
		addOptionalOption(options, TARGET_VALUE_SET_PARAM, TARGET_VALUE_SET_PARAM_LONGOPT, TARGET_VALUE_SET_PARAM_NAME, TARGET_VALUE_SET_PARAM_DESC);
		// </editor-fold>
		addRequiredOption(options, FILE_PARAM, FILE_PARAM_LONGOPT, FILE_PARAM_NAME, FILE_PARAM_DESC);

		return options;
	}

	@Override
	protected void parseAdditionalParameters(CommandLine theCommandLine) {
		sourceValueSet = theCommandLine.getOptionValue(SOURCE_VALUE_SET_PARAM);
		if (isBlank(sourceValueSet)) {
			ourLog.info("Source value set is not specified (i.e. ConceptMap.sourceUri).");
		} else {
			ourLog.info("Specified source value set (i.e. ConceptMap.sourceUri): {}", sourceValueSet);
		}

		targetValueSet = theCommandLine.getOptionValue(TARGET_VALUE_SET_PARAM);
		if (isBlank(targetValueSet)) {
			ourLog.info("Target value set is not specified (i.e. ConceptMap.targetUri).");
		} else {
			ourLog.info("Specified target value set (i.e. ConceptMap.targetUri): {}", targetValueSet);
		}
	}

	@Override
	protected void process() throws ExecutionException {
		searchForConceptMapByUrl();
	}

	private void searchForConceptMapByUrl() throws ExecutionException {
		if (fhirVersion == FhirVersionEnum.DSTU3) {
			org.hl7.fhir.dstu3.model.ConceptMap conceptMap = convertCsvToConceptMapDstu3();

			ourLog.info("Searching for existing ConceptMap with specified URL (i.e. ConceptMap.url): {}", conceptMapUrl);
			MethodOutcome methodOutcome = client
				.update()
				.resource(conceptMap)
				.conditional()
				.where(org.hl7.fhir.dstu3.model.ConceptMap.URL.matches().value(conceptMapUrl))
				.execute();

			if (Boolean.TRUE.equals(methodOutcome.getCreated())) {
				ourLog.info("Created new ConceptMap: {}", methodOutcome.getId().getValue());
			} else {
				ourLog.info("Updated existing ConceptMap: {}", methodOutcome.getId().getValue());
			}
		} else if (fhirVersion == FhirVersionEnum.R4) {
			ConceptMap conceptMap = convertCsvToConceptMapR4();

			ourLog.info("Searching for existing ConceptMap with specified URL (i.e. ConceptMap.url): {}", conceptMapUrl);
			MethodOutcome methodOutcome = client
				.update()
				.resource(conceptMap)
				.conditional()
				.where(ConceptMap.URL.matches().value(conceptMapUrl))
				.execute();

			if (Boolean.TRUE.equals(methodOutcome.getCreated())) {
				ourLog.info("Created new ConceptMap: {}", methodOutcome.getId().getValue());
			} else {
				ourLog.info("Updated existing ConceptMap: {}", methodOutcome.getId().getValue());
			}
		}
	}

	private org.hl7.fhir.dstu3.model.ConceptMap convertCsvToConceptMapDstu3() throws ExecutionException {
		try {
			return (org.hl7.fhir.dstu3.model.ConceptMap) VersionConvertorFactory_30_40.convertResource(convertCsvToConceptMapR4());
		} catch (FHIRException fe) {
			throw new ExecutionException(Msg.code(1604), fe);
		}
	}

	private ConceptMap convertCsvToConceptMapR4() throws ExecutionException {
		ourLog.info("Converting CSV to ConceptMap...");
		ConceptMap retVal = new ConceptMap();
		try (
			Reader reader = getBufferedReader();
			CSVParser csvParser = new CSVParser(
				reader,
				CSVFormat
					.DEFAULT
					.withRecordSeparator("\n")
					.withHeader(Header.class)
					.withFirstRecordAsHeader()
					.withIgnoreHeaderCase()
					.withIgnoreEmptyLines()
					.withTrim())
		) {
			retVal.setUrl(conceptMapUrl);

			if (isNotBlank(sourceValueSet)) {
				retVal.setSource(new UriType(sourceValueSet));
			}

			if (isNotBlank(targetValueSet)) {
				retVal.setTarget(new UriType(targetValueSet));
			}

			TemporaryConceptMapGroup temporaryConceptMapGroup;
			TemporaryConceptMapGroupElement temporaryConceptMapGroupElement;
			Map<TemporaryConceptMapGroup, Map<TemporaryConceptMapGroupElement, Set<TemporaryConceptMapGroupElementTarget>>> groupMap = parseCsvRecords(csvParser);
			Map<TemporaryConceptMapGroupElement, Set<TemporaryConceptMapGroupElementTarget>> elementMap;
			Set<TemporaryConceptMapGroupElementTarget> targetSet;
			ConceptMapGroupComponent conceptMapGroupComponent;
			SourceElementComponent sourceElementComponent;
			TargetElementComponent targetElementComponent;
			for (Map.Entry<TemporaryConceptMapGroup, Map<TemporaryConceptMapGroupElement, Set<TemporaryConceptMapGroupElementTarget>>> groupEntry : groupMap.entrySet()) {

				hasElements = false;
				hasTargets = false;

				temporaryConceptMapGroup = groupEntry.getKey();
				conceptMapGroupComponent = new ConceptMapGroupComponent();

				if (temporaryConceptMapGroup.hasSource()) {
					conceptMapGroupComponent.setSource(temporaryConceptMapGroup.getSource());
				}

				if (temporaryConceptMapGroup.hasSourceVersion()) {
					conceptMapGroupComponent.setSourceVersion(temporaryConceptMapGroup.getSourceVersion());
				}

				if (temporaryConceptMapGroup.hasTarget()) {
					conceptMapGroupComponent.setTarget(temporaryConceptMapGroup.getTarget());
				}

				if (temporaryConceptMapGroup.hasTargetVersion()) {
					conceptMapGroupComponent.setTargetVersion(temporaryConceptMapGroup.getTargetVersion());
				}

				elementMap = groupEntry.getValue();
				for (Map.Entry<TemporaryConceptMapGroupElement, Set<TemporaryConceptMapGroupElementTarget>> elementEntry : elementMap.entrySet()) {

					temporaryConceptMapGroupElement = elementEntry.getKey();
					sourceElementComponent = new SourceElementComponent();

					if (temporaryConceptMapGroupElement.hasCode()) {
						sourceElementComponent.setCode(temporaryConceptMapGroupElement.getCode());
					}

					if (temporaryConceptMapGroupElement.hasDisplay()) {
						sourceElementComponent.setDisplay(temporaryConceptMapGroupElement.getDisplay());
					}

					targetSet = elementEntry.getValue();
					for (TemporaryConceptMapGroupElementTarget temporaryConceptMapGroupElementTarget : targetSet) {

						targetElementComponent = new TargetElementComponent();

						if (temporaryConceptMapGroupElementTarget.hasCode()) {
							targetElementComponent.setCode(temporaryConceptMapGroupElementTarget.getCode());
						}

						if (temporaryConceptMapGroupElementTarget.hasDisplay()) {
							targetElementComponent.setDisplay(temporaryConceptMapGroupElementTarget.getDisplay());
						}

						if (temporaryConceptMapGroupElementTarget.hasEquivalence()) {
							try {
								targetElementComponent.setEquivalence(Enumerations.ConceptMapEquivalence.fromCode(temporaryConceptMapGroupElementTarget.getEquivalence()));
							} catch (FHIRException fe) {
								throw new ExecutionException(Msg.code(1605), fe);
							}
						}

						if (temporaryConceptMapGroupElementTarget.hasComment()) {
							targetElementComponent.setComment(temporaryConceptMapGroupElementTarget.getComment());
						}

						if (temporaryConceptMapGroupElementTarget.hasValues()) {
							sourceElementComponent.addTarget(targetElementComponent);
							hasTargets = true;
						}
					}

					if (temporaryConceptMapGroupElement.hasValues() || hasTargets) {
						conceptMapGroupComponent.addElement(sourceElementComponent);
						hasElements = true;
					}
				}

				if (temporaryConceptMapGroup.hasValues() || hasElements || hasTargets) {
					retVal.addGroup(conceptMapGroupComponent);
				}
			}
		} catch (IOException e) {
			throw new InternalErrorException(Msg.code(1606) + e);
		}

		ourLog.info("Finished converting CSV to ConceptMap.");
		return retVal;
	}

	private Map<TemporaryConceptMapGroup, Map<TemporaryConceptMapGroupElement, Set<TemporaryConceptMapGroupElementTarget>>> parseCsvRecords(CSVParser theCsvParser) {
		Map<TemporaryConceptMapGroup, Map<TemporaryConceptMapGroupElement, Set<TemporaryConceptMapGroupElementTarget>>> retVal = new LinkedHashMap<>();

		TemporaryConceptMapGroup group;
		TemporaryConceptMapGroupElement element;
		TemporaryConceptMapGroupElementTarget target;
		Map<TemporaryConceptMapGroupElement, Set<TemporaryConceptMapGroupElementTarget>> elementMap;
		Set<TemporaryConceptMapGroupElementTarget> targetSet;

		for (CSVRecord csvRecord : theCsvParser) {

			group = new TemporaryConceptMapGroup(
				defaultString(csvRecord.get(Header.SOURCE_CODE_SYSTEM)),
				defaultString(csvRecord.get(Header.SOURCE_CODE_SYSTEM_VERSION)),
				defaultString(csvRecord.get(Header.TARGET_CODE_SYSTEM)),
				defaultString(csvRecord.get(Header.TARGET_CODE_SYSTEM_VERSION)));

			element = new TemporaryConceptMapGroupElement(
				defaultString(csvRecord.get(Header.SOURCE_CODE)),
				defaultString(csvRecord.get(Header.SOURCE_DISPLAY)));

			target = new TemporaryConceptMapGroupElementTarget(
				defaultString(csvRecord.get(Header.TARGET_CODE)),
				defaultString(csvRecord.get(Header.TARGET_DISPLAY)),
				defaultString(csvRecord.get(Header.EQUIVALENCE)),
				defaultString(csvRecord.get(Header.COMMENT)));

			if (!retVal.containsKey(group)) {
				targetSet = new LinkedHashSet<>();
				targetSet.add(target);

				elementMap = new LinkedHashMap<>();
				elementMap.put(element, targetSet);

				retVal.put(group, elementMap);
			} else if (!retVal.get(group).containsKey(element)) {
				targetSet = new LinkedHashSet<>();
				targetSet.add(target);

				retVal.get(group).put(element, targetSet);
			} else {
				retVal.get(group).get(element).add(target);
			}
		}

		return retVal;
	}
}


text:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

function onCreatureAppear(cid) npcHandler:onCreatureAppear(cid) end
function onCreatureDisappear(cid) npcHandler:onCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg) npcHandler:onCreatureSay(cid, type, msg) end
function onThink() npcHandler:onThink() end

local function greetCallback(cid)
	local player = Player(cid)
	if(player:getStorageValue(Storage.InServiceofYalahar.MrWestDoor) == 1) then
		npcHandler:setMessage(MESSAGE_GREET, "Wh .. What? How did you get here? Where are all the guards? You .. you could have killed me but yet you chose to talk? What a relief! ... So what brings you here my friend, if I might call you like that? ")
	elseif(player:getStorageValue(Storage.InServiceofYalahar.MrWestDoor) == 2) then
		npcHandler:setMessage(MESSAGE_GREET, "Murderer! But .. I give in, you won! ... Dictate me your conditions but please, I beg you, spare my life. What do you want?")
	end
	return true
end

local function creatureSayCallback(cid, type, msg)
	local player = Player(cid)
	if not npcHandler:isFocused(cid) then
		return false
	elseif(msgcontains(msg, "mission")) then
		if(player:getStorageValue(Storage.InServiceofYalahar.Questline) == 24) then
			if(player:getStorageValue(Storage.InServiceofYalahar.MrWestDoor) == 1) then
				npcHandler:say("Indeed, I can see the benefits of a mutual agreement. I will later read the details and send a letter to your superior. ", cid)
				player:setStorageValue(Storage.InServiceofYalahar.Questline, 25)
				player:setStorageValue(Storage.InServiceofYalahar.Mission04, 3) -- StorageValue for Questlog "Mission 04: Good to be Kingpin"
				player:setStorageValue(Storage.InServiceofYalahar.MrWestStatus, 1)
				npcHandler.topic[cid] = 0
			elseif(player:getStorageValue(Storage.InServiceofYalahar.MrWestDoor) == 2) then
				npcHandler:say("Yes, for the sake of my life I'll accept those terms. I know when I have lost. Tell your master I will comply with his orders. ", cid)
				player:setStorageValue(Storage.InServiceofYalahar.Questline, 25)
				player:setStorageValue(Storage.InServiceofYalahar.Mission04, 4) -- StorageValue for Questlog "Mission 04: Good to be Kingpin"
				player:setStorageValue(Storage.InServiceofYalahar.MrWestStatus, 2)
				npcHandler.topic[cid] = 0
			end
		end
	end
	return true
end

npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

redpajama_set_name: RedPajamaGithub


text:
package za.co.praxis.jtrac.domain;
// Generated Oct 18, 2013 2:28:49 PM by Hibernate Tools 3.4.0.CR1

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * ZReasonOutstanding generated by hbm2java
 */
@Entity
@Table(name = "z_reason_outstanding")//, schema = "dbo", catalog = "jtrac")
public class ZReasonOutstanding implements java.io.Serializable {

	private BigDecimal id;
	private String description;
	private Set<History> histories = new HashSet<History>(0);

	public ZReasonOutstanding() {
	}

	public ZReasonOutstanding(BigDecimal id) {
		this.id = id;
	}

	public ZReasonOutstanding(BigDecimal id, String description,
			Set<History> histories) {
		this.id = id;
		this.description = description;
		this.histories = histories;
	}

	@Id
	@Column(name = "id", unique = true, nullable = false, scale = 0)
	public BigDecimal getId() {
		return this.id;
	}

	public void setId(BigDecimal id) {
		this.id = id;
	}

	@Column(name = "description", length = 50)
	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "ZReasonOutstanding")
	public Set<History> getHistories() {
		return this.histories;
	}

	public void setHistories(Set<History> histories) {
		this.histories = histories;
	}

}


text:
"""
Unittests for pelagos_processing.config
"""

try:
    from configparser import ConfigParser
except ImportError:
    from ConfigParser import ConfigParser
import unittest

from pelagos_processing import config
from pelagos_processing.tests import testdata
from pelagos_processing import common

class DevNull(object):

    @staticmethod
    def write(self, *args, **kwargs):
        pass

class TestConfig(unittest.TestCase):

    def test_show(self):
        with open(testdata.sample_config) as f:
            actual = f.readlines()
        expected = config.show(testdata.sample_config, DevNull)
        self.assertEqual(expected, actual)

    def test_as_from_series(self):
        parsed = ConfigParser()
        parsed.read(testdata.sample_config)
        self.assertDictEqual(config.as_dict(parsed), config.as_dict(testdata.sample_config))
        self.assertDictEqual(config.as_dict(parsed), config.as_dict(config.from_path(testdata.sample_config)))

        as_dictionary = config.as_dict(testdata.sample_config)

        for section in parsed.sections():
            self.assertIn(section, as_dictionary)
            for option in parsed.options(section):
                self.assertEqual(common.string2type(parsed.get(section, option)), as_dictionary[section][option])

    def test_as_dict_from_path(self):
        loaded = config.as_dict(testdata.sample_config)
        self.assertIn('section1', loaded)
        self.assertIsInstance(loaded, dict)
        self.assertRaises(IOError, config.from_path, 'not a path--^^')

    def test_as_dict_from_configparser(self):
        loaded = config.as_dict(testdata.sample_config)
        self.assertIn('section1', loaded)
        self.assertIsInstance(loaded, dict)
        self.assertRaises(IOError, config.from_path, 'NOOOOOTHING')  # Not a path or ConfigParser instance

    def test_from_dict(self):
        config_dict = {
            'section1': {
                'key1': 'val1',
                'integer': 1
            }
        }
        loaded = config.from_dict(config_dict)
        self.assertTrue(loaded.has_section('section1'))
        self.assertIsInstance(loaded, ConfigParser)
        self.assertRaises(TypeError, config.from_dict, 'not a dictionary')

    def test_from_path(self):
        loaded = config.from_path(testdata.sample_config)
        self.assertTrue(loaded.has_section('section1'))
        self.assertIsInstance(loaded, ConfigParser)
        self.assertRaises(IOError, config.from_path, '.--asBad_PAA~~~th')


text:
using System;
using AudioSynthesis.Bank.Descriptors;
using AudioSynthesis.Synthesis;

namespace AudioSynthesis.Bank.Components.Generators
{
    public class WhiteNoiseGenerator : Generator
    {
        private static readonly Random random = new Random();
        //--Methods
        public WhiteNoiseGenerator(GeneratorDescriptor description)
            : base(description)
        {
            if(end < 0)
                end = 1;
            if (start < 0)
                start = 0;
            if (loopEnd < 0)
                loopEnd = end;
            if (loopStart < 0)
                loopStart = start;
            if (genPeriod < 0)
                genPeriod = 1;
            if (root < 0)
                root = 69;
            freq = 440;
        }
        public override float GetValue(double phase)
        {
            return (float)((random.NextDouble() * 2.0) - 1.0);
        }
        public override void GetValues(GeneratorParameters generatorParams, float[] blockBuffer, double increment)
        {
            int proccessed = 0;
            do
            {
                int samplesAvailable = (int)Math.Ceiling((generatorParams.currentEnd - generatorParams.phase) / increment);
                if (samplesAvailable > blockBuffer.Length - proccessed)
                {
                    while (proccessed < blockBuffer.Length)
                    {
                        blockBuffer[proccessed++] = (float)((random.NextDouble() * 2.0) - 1.0);
                        generatorParams.phase += increment;
                    }
                }
                else
                {
                    int endProccessed = proccessed + samplesAvailable;
                    while (proccessed < endProccessed)
                    {
                        blockBuffer[proccessed++] = (float)((random.NextDouble() * 2.0) - 1.0);
                        generatorParams.phase += increment;
                    }
                    switch (generatorParams.currentState)
                    {
                        case GeneratorStateEnum.PreLoop:
                            generatorParams.currentStart = loopStart;
                            generatorParams.currentEnd = loopEnd;
                            generatorParams.currentState = GeneratorStateEnum.Loop;
                            break;
                        case GeneratorStateEnum.Loop:
                            generatorParams.phase += generatorParams.currentStart - generatorParams.currentEnd;
                            break;
                        case GeneratorStateEnum.PostLoop:
                            generatorParams.currentState = GeneratorStateEnum.Finished;
                            while (proccessed < blockBuffer.Length)
                                blockBuffer[proccessed++] = 0f;
                            break;
                    }
                }
            }
            while (proccessed < blockBuffer.Length);
        }
    }
}


text:
<?php

namespace porshkevich\seocore\controllers;

use yii\web\Controller;
/**
 * Description of DefaultController
 *
 * @author NeoSonic <neosonic@inbox.ru>
 */
class DefaultController  extends Controller {

	public function actionIndex()
    {
        return $this->render('index');
    }
}


text:
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2018 The Chromium Authors. All rights reserved.
     Use of this source code is governed by a BSD-style license that can be
     found in the LICENSE file. -->

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid android:color="@color/default_bg_color" />
    <stroke android:width="1dp" android:color="@macro/hairline_stroke_color"/>
    <corners android:radius="@dimen/default_rounded_corner_radius" />
</shape>


text:
import React, { PropTypes } from 'react';
import { connect, Provider } from 'react-redux';
import UploadDataStore from 'services/WizardStores/UploadData/UploadDataStore';
import UploadDataActionCreator from 'services/WizardStores/UploadData/ActionCreator';
import fileDownload from 'react-file-download';
require('./ViewDataStep.less');

const mapStateWithProps = (state) => {
  return {
    value: state.viewdata.data,
    isLoading: state.viewdata.loading
  };
};

const handleDownload = () => {
  const state = UploadDataStore.getState();

  fileDownload(state.viewdata.data, state.viewdata.filename);
};

let DataTextArea = ({value, isLoading}) => {
  value = value.substring(0, 10000);
  return (

          <button
            className="btn btn-link"
            onClick={handleDownload}
          >

          </button>

        {
          isLoading ?

            :

              {value}

        }

  );
};

DataTextArea.propTypes = {
  value: PropTypes.string,
  isLoading: PropTypes.bool
};

DataTextArea = connect(
  mapStateWithProps,
  null
)(DataTextArea);

export default function ViewDataStep() {
  let { filename, packagename, packageversion, data } = UploadDataStore.getState().viewdata;

  if (!data) {
    UploadDataActionCreator
      .fetchDefaultData({ filename, packagename, packageversion });
  }

  return (
    <Provider store={UploadDataStore}>
      <DataTextArea />
    </Provider>
  );
}


text:
class Post < ActiveRecord::Base
  has_many :page_views

  scope :published, -> { where(published: true) }
  scope_membership :published, as: :is_published?
end


text:
package com.hazelcast.map;

import com.hazelcast.config.Config;
import com.hazelcast.config.InMemoryFormat;
import com.hazelcast.config.MapConfig;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.ICompletableFuture;
import com.hazelcast.core.IMap;
import com.hazelcast.core.Offloadable;
import com.hazelcast.core.ReadOnly;
import com.hazelcast.nio.Address;
import com.hazelcast.spi.NodeEngine;
import com.hazelcast.spi.impl.operationservice.impl.OperationServiceImpl;
import com.hazelcast.test.HazelcastParametersRunnerFactory;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.TestHazelcastInstanceFactory;
import com.hazelcast.test.annotation.ParallelTest;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.Parameterized.UseParametersRunnerFactory;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import static com.hazelcast.config.InMemoryFormat.BINARY;
import static com.hazelcast.config.InMemoryFormat.OBJECT;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

@RunWith(Parameterized.class)
@UseParametersRunnerFactory(HazelcastParametersRunnerFactory.class)
@Category({QuickTest.class, ParallelTest.class})
public class EntryProcessorOffloadableTest extends HazelcastTestSupport {

    public static final String MAP_NAME = "EntryProcessorOffloadableTest";

    private static final int HEARTBEATS_INTERVAL_SEC = 2;

    private HazelcastInstance[] instances;

    @Parameter(0)
    public InMemoryFormat inMemoryFormat;

    @Parameter(1)
    public int syncBackupCount;

    @Parameter(2)
    public int asyncBackupCount;

    @Rule
    public ExpectedException expectedException = ExpectedException.none();

    @Parameters(name = "{index}: {0} sync={1} async={2}")
    public static Collection<Object[]> data() {
        return asList(new Object[][]{
                {BINARY, 0, 0}, {OBJECT, 0, 0},
                {BINARY, 1, 0}, {OBJECT, 1, 0},
                {BINARY, 0, 1}, {OBJECT, 0, 1},
        });
    }

    private boolean isBackup() {
        return syncBackupCount + asyncBackupCount > 0;
    }

    @Override
    public Config getConfig() {
        Config config = super.getConfig();
        MapConfig mapConfig = new MapConfig(MAP_NAME);
        mapConfig.setInMemoryFormat(inMemoryFormat);
        mapConfig.setAsyncBackupCount(asyncBackupCount);
        mapConfig.setBackupCount(syncBackupCount);
        config.addMapConfig(mapConfig);
        config.getProperties().setProperty("hazelcast.operation.call.timeout.millis",
                String.valueOf(HEARTBEATS_INTERVAL_SEC * 4 * 1000));
        return config;
    }

    @Before
    public void before() {
        TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
        instances = factory.newInstances(getConfig());
    }

    @Test
    public void testEntryProcessorWithKey_offloadable_setValue() {
        String key = generateKeyOwnedBy(instances[0]);
        SimpleValue givenValue = new SimpleValue(1);
        SimpleValue expectedValue = new SimpleValue(2);

        IMap<Object, Object> map = instances[1].getMap(MAP_NAME);
        map.put(key, givenValue);
        Object result = map.executeOnKey(key, new EntryIncOffloadable());

        assertEquals(expectedValue, map.get(key));
        assertBackupEventually(instances[1], MAP_NAME, key, isBackup() ? expectedValue : null);
        assertEquals(givenValue.i, result);

        instances[0].shutdown();
        assertEquals(expectedValue, map.get(key));
        assertEquals(givenValue.i, result);
    }

    private static class EntryIncOffloadable implements EntryProcessor<String, SimpleValue>, Offloadable, EntryBackupProcessor<String, SimpleValue> {
        @Override
        public Object process(final Map.Entry<String, SimpleValue> entry) {
            final SimpleValue value = entry.getValue();
            int result = value.i;
            value.i++;
            entry.setValue(value);
            return result;
        }

        @Override
        public EntryBackupProcessor<String, SimpleValue> getBackupProcessor() {
            return this;
        }

        @Override
        public String getExecutorName() {
            return Offloadable.OFFLOADABLE_EXECUTOR;
        }

        @Override
        public void processBackup(Map.Entry<String, SimpleValue> entry) {
            process(entry);
        }
    }

    @Test
    public void testEntryProcessorWithKey_offloadable_withoutSetValue() {
        String key = generateKeyOwnedBy(instances[0]);
        SimpleValue givenValue = new SimpleValue(1);
        SimpleValue expectedValue = new SimpleValue(1);

        IMap<Object, Object> map = instances[1].getMap(MAP_NAME);
        map.put(key, givenValue);
        Object result = map.executeOnKey(key, new EntryIncOffloadableNoSetValue());

        assertEquals(expectedValue, map.get(key));
        if (inMemoryFormat.equals(OBJECT)) {
            assertBackupEventually(instances[1], MAP_NAME, key, isBackup() ? new SimpleValue(2) : null);
        } else {
            assertBackupEventually(instances[1], MAP_NAME, key, isBackup() ? expectedValue : null);
        }
        assertEquals(givenValue.i, result);

        instances[0].shutdown();
        assertEquals(expectedValue, map.get(key));
    }

    private static class EntryIncOffloadableNoSetValue implements EntryProcessor<String, SimpleValue>, Offloadable, EntryBackupProcessor<String, SimpleValue> {
        @Override
        public Object process(final Map.Entry<String, SimpleValue> entry) {
            final SimpleValue value = entry.getValue();
            int result = value.i;
            value.i++;
            return result;
        }

        @Override
        public String getExecutorName() {
            return Offloadable.OFFLOADABLE_EXECUTOR;
        }

        @Override
        public EntryBackupProcessor getBackupProcessor() {
            return this;
        }

        @Override
        public void processBackup(Map.Entry<String, SimpleValue> entry) {
            process(entry);
        }
    }

    @Test
    public void testEntryProcessorWithKey_offloadableReadOnly_setValue() {
        String key = generateKeyOwnedBy(instances[0]);
        SimpleValue givenValue = new SimpleValue(1);

        IMap<Object, Object> map = instances[1].getMap(MAP_NAME);
        map.put(key, givenValue);

        expectedException.expect(UnsupportedOperationException.class);

        map.executeOnKey(key, new EntryIncOffloadableReadOnly());
    }

    private static class EntryIncOffloadableReadOnly implements EntryProcessor<String, SimpleValue>, Offloadable, ReadOnly,
            EntryBackupProcessor<String, SimpleValue> {
        @Override
        public Object process(final Map.Entry<String, SimpleValue> entry) {
            final SimpleValue value = entry.getValue();
            int result = value.i;
            value.i++;
            entry.setValue(value);
            return result;
        }

        @Override
        public EntryBackupProcessor<String, SimpleValue> getBackupProcessor() {
            return null;
        }

        @Override
        public String getExecutorName() {
            return Offloadable.OFFLOADABLE_EXECUTOR;
        }

        @Override
        public void processBackup(Map.Entry<String, SimpleValue> entry) {
            process(entry);
        }
    }

    @Test
    public void testEntryProcessorWithKey_offloadableReadOnly_withoutSetValue() {
        String key = generateKeyOwnedBy(instances[0]);
        SimpleValue givenValue = new SimpleValue(1);
        SimpleValue expectedValue = new SimpleValue(1);

        IMap<Object, Object> map = instances[1].getMap(MAP_NAME);
        map.put(key, givenValue);
        Object result = map.executeOnKey(key, new EntryIncOffloadableReadOnlyNoSetValue());

        assertEquals(expectedValue, map.get(key));
        assertBackupEventually(instances[1], MAP_NAME, key, isBackup() ? expectedValue : null);
        assertEquals(givenValue.i, result);

        instances[0].shutdown();
        assertEquals(expectedValue, map.get(key));
    }

    private static class EntryIncOffloadableReadOnlyNoSetValue implements EntryProcessor<String, SimpleValue>, Offloadable, ReadOnly,
            EntryBackupProcessor<String, SimpleValue> {
        @Override
        public Object process(final Map.Entry<String, SimpleValue> entry) {
            final SimpleValue value = entry.getValue();
            int result = value.i;
            value.i++;
            return result;
        }

        @Override
        public String getExecutorName() {
            return Offloadable.OFFLOADABLE_EXECUTOR;
        }

        @Override
        public EntryBackupProcessor getBackupProcessor() {
            return null;
        }

        @Override
        public void processBackup(Map.Entry<String, SimpleValue> entry) {
            process(entry);
        }
    }

    @Test
    public void testEntryProcessorWithKey_offloadableReadOnly_returnValue() {
        String key = generateKeyOwnedBy(instances[0]);
        SimpleValue givenValue = new SimpleValue(1);
        SimpleValue expectedValue = new SimpleValue(1);

        IMap<Object, Object> map = instances[1].getMap(MAP_NAME);
        map.put(key, givenValue);
        Object result = map.executeOnKey(key, new EntryIncOffloadableReadOnlyReturnValue());

        assertEquals(expectedValue, map.get(key));
        assertBackupEventually(instances[1], MAP_NAME, key, isBackup() ? expectedValue : null);
        assertEquals(givenValue.i, result);

        instances[0].shutdown();
        assertEquals(expectedValue, map.get(key));
    }

    private static class EntryIncOffloadableReadOnlyReturnValue implements EntryProcessor<String, SimpleValue>, Offloadable, ReadOnly,
            EntryBackupProcessor<String, SimpleValue> {
        @Override
        public Object process(final Map.Entry<String, SimpleValue> entry) {
            final SimpleValue value = entry.getValue();
            return value.i;
        }

        @Override
        public String getExecutorName() {
            return Offloadable.OFFLOADABLE_EXECUTOR;
        }

        @Override
        public EntryBackupProcessor getBackupProcessor() {
            return null;
        }

        @Override
        public void processBackup(Map.Entry<String, SimpleValue> entry) {
            process(entry);
        }
    }

    @Test
    public void testEntryProcessorWithKey_offloadableReadOnly_noReturnValue() {
        String key = generateKeyOwnedBy(instances[0]);
        SimpleValue givenValue = new SimpleValue(1);
        SimpleValue expectedValue = new SimpleValue(1);

        IMap<Object, Object> map = instances[1].getMap(MAP_NAME);
        map.put(key, givenValue);
        Object result = map.executeOnKey(key, new EntryIncOffloadableReadOnlyNoReturnValue());
        assertEquals(expectedValue, map.get(key));
        assertBackupEventually(instances[1], MAP_NAME, key, isBackup() ? expectedValue : null);
        assertNull(result);

        instances[0].shutdown();
        assertEquals(expectedValue, map.get(key));
    }

    private static class EntryIncOffloadableReadOnlyNoReturnValue implements EntryProcessor<String, SimpleValue>, Offloadable, ReadOnly,
            EntryBackupProcessor<String, SimpleValue> {
        @Override
        public Object process(final Map.Entry<String, SimpleValue> entry) {
            return null;
        }

        @Override
        public String getExecutorName() {
            return Offloadable.OFFLOADABLE_EXECUTOR;
        }

        @Override
        public EntryBackupProcessor getBackupProcessor() {
            return null;
        }

        @Override
        public void processBackup(Map.Entry<String, SimpleValue> entry) {
            process(entry);
        }
    }

    @Test
    public void testEntryProcessorWithKey_offloadableReadOnly_throwsException() {
        String key = generateKeyOwnedBy(instances[0]);
        SimpleValue givenValue = new SimpleValue(1);

        IMap<Object, Object> map = instances[1].getMap(MAP_NAME);
        map.put(key, givenValue);

        expectedException.expect(RuntimeException.class);
        map.executeOnKey(key, new EntryIncOffloadableReadOnlyException());
        assertFalse(map.isLocked(key));
    }

    private static class EntryIncOffloadableReadOnlyException implements EntryProcessor<String, SimpleValue>, Offloadable, ReadOnly,
            EntryBackupProcessor<String, SimpleValue> {
        @Override
        public Object process(final Map.Entry<String, SimpleValue> entry) {
            throw new RuntimeException("EP exception");
        }

        @Override
        public String getExecutorName() {
            return Offloadable.OFFLOADABLE_EXECUTOR;
        }

        @Override
        public EntryBackupProcessor getBackupProcessor() {
            return null;
        }

        @Override
        public void processBackup(Map.Entry<String, SimpleValue> entry) {
            process(entry);
        }
    }

    @Test
    public void testEntryProcessorWithKey_offloadableModifying_throwsException_keyNotLocked() {
        String key = generateKeyOwnedBy(instances[0]);
        SimpleValue givenValue = new SimpleValue(1);

        IMap<Object, Object> map = instances[1].getMap(MAP_NAME);
        map.put(key, givenValue);

        expectedException.expect(RuntimeException.class);
        map.executeOnKey(key, new EntryIncOffloadableException());

        assertFalse(map.isLocked(key));
    }

    @Test
    public void puts_multiple_entry_into_empty_map() {
        IMap map = instances[1].getMap(MAP_NAME);

        Set keySet = new HashSet(asList(1, 2, 3, 4, 5));

        Object value = -1;

        map.executeOnKeys(keySet, new EntryAdderOffloadable(value));

        for (Object key : keySet) {
            assertEquals(value, map.get(key));
        }
    }

    @Test
    public void puts_entry_into_empty_map() {
        IMap map = instances[1].getMap(MAP_NAME);

        Object key = 1;
        Object value = -1;

        map.executeOnKey(key, new EntryAdderOffloadable(value));

        assertEquals(value, map.get(key));
    }

    private static class EntryIncOffloadableException implements EntryProcessor<String, SimpleValue>, Offloadable,
            EntryBackupProcessor<String, SimpleValue> {
        @Override
        public Object process(final Map.Entry<String, SimpleValue> entry) {
            throw new RuntimeException("EP exception");
        }

        @Override
        public String getExecutorName() {
            return Offloadable.OFFLOADABLE_EXECUTOR;
        }

        @Override
        public EntryBackupProcessor getBackupProcessor() {
            return null;
        }

        @Override
        public void processBackup(Map.Entry<String, SimpleValue> entry) {
            process(entry);
        }
    }

    private static class EntryAdderOffloadable extends AbstractEntryProcessor implements Offloadable {

        private final Object value;

        public EntryAdderOffloadable(Object value) {
            this.value = value;
        }

        @Override
        public String getExecutorName() {
            return Offloadable.OFFLOADABLE_EXECUTOR;
        }

        @Override
        public Object process(Map.Entry entry) {
            entry.setValue(value);
            return null;
        }
    }

    void assertBackupEventually(final HazelcastInstance instance, final String mapName, final Object key, Object expected) {
        assertEqualsEventually(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                return readFromMapBackup(instance, mapName, key);
            }
        }, expected);
    }

    @Test
    public void testEntryProcessorWithKey_offloadable_otherModifyingWillWait() throws InterruptedException {
        final String key = generateKeyOwnedBy(instances[0]);
        SimpleValue givenValue = new SimpleValue(1);
        SimpleValue expectedValue = new SimpleValue(4);

        final IMap<Object, Object> map = instances[0].getMap(MAP_NAME);
        map.put(key, givenValue);

        final CountDownLatch epStarted = new CountDownLatch(1);
        final CountDownLatch epStopped = new CountDownLatch(1);
        new Thread() {
            public void run() {
                map.executeOnKey(key, new EntryLatchModifying(epStarted, epStopped));
            }
        }.start();

        epStarted.await();
        map.executeOnKey(key, new EntryLatchVerifying(epStarted, epStopped, 4));

        // verified EPs not out-of-order, and not at the same time
        assertEqualsEventually(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                return map.get(key);
            }
        }, expectedValue);
    }

    private static class EntryLatchModifying implements EntryProcessor<String, SimpleValue>, Offloadable, EntryBackupProcessor<String, SimpleValue> {

        private final CountDownLatch start;
        private final CountDownLatch stop;

        public EntryLatchModifying(CountDownLatch start, CountDownLatch stop) {
            this.start = start;
            this.stop = stop;
        }

        @Override
        public Object process(final Map.Entry<String, SimpleValue> entry) {
            start.countDown();
            try {
                final SimpleValue value = entry.getValue();
                value.i++;
                entry.setValue(value);
                return null;
            } finally {
                stop.countDown();
            }
        }

        @Override
        public EntryBackupProcessor getBackupProcessor() {
            return null;
        }

        @Override
        public void processBackup(Map.Entry<String, SimpleValue> entry) {
            process(entry);
        }

        @Override
        public String getExecutorName() {
            return Offloadable.OFFLOADABLE_EXECUTOR;
        }

    }

    private static class EntryLatchVerifying implements EntryProcessor<String, SimpleValue>, Offloadable, EntryBackupProcessor<String, SimpleValue> {

        private final CountDownLatch otherStarted;
        private final CountDownLatch otherStopped;
        private final int valueToSet;

        public EntryLatchVerifying(CountDownLatch otherStarted, CountDownLatch otherStopped, final int value) {
            this.otherStarted = otherStarted;
            this.otherStopped = otherStopped;
            this.valueToSet = value;
        }

        @Override
        public Object process(final Map.Entry<String, SimpleValue> entry) {
            if (otherStarted.getCount() != 0 || otherStopped.getCount() != 0) {
                throw new RuntimeException("Wrong threading order");
            }

            final SimpleValue value = entry.getValue();
            value.i = valueToSet;
            entry.setValue(value);
            return null;
        }

        @Override
        public EntryBackupProcessor getBackupProcessor() {
            return null;
        }

        @Override
        public void processBackup(Map.Entry<String, SimpleValue> entry) {
            process(entry);
        }

        @Override
        public String getExecutorName() {
            return Offloadable.OFFLOADABLE_EXECUTOR;
        }
    }

    @Test
    public void testEntryProcessorWithKey_offloadable_otherReadingWillNotWait() throws InterruptedException {
        final String key = generateKeyOwnedBy(instances[0]);
        SimpleValue givenValue = new SimpleValue(1);
        SimpleValue expectedValue = new SimpleValue(2);

        final IMap<Object, Object> map = instances[0].getMap(MAP_NAME);
        map.put(key, givenValue);

        final CountDownLatch epStarted = new CountDownLatch(1);
        final CountDownLatch epWaitToProceed = new CountDownLatch(1);
        final CountDownLatch epStopped = new CountDownLatch(1);
        new Thread() {
            public void run() {
                map.executeOnKey(key, new EntryLatchModifyingOtherReading(epStarted, epWaitToProceed, epStopped));
            }
        }.start();

        epStarted.await();
        map.executeOnKey(key, new EntryLatchReadOnlyVerifyingWhileOtherWriting(epStarted, epWaitToProceed, epStopped));
        epStopped.await();

        // verified EPs not out-of-order, and not at the same time
        assertEqualsEventually(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                return map.get(key);
            }
        }, expectedValue);
    }

    private static class EntryLatchModifyingOtherReading implements EntryProcessor<String, SimpleValue>, Offloadable, EntryBackupProcessor<String, SimpleValue> {

        private final CountDownLatch start;
        private final CountDownLatch stop;
        private final CountDownLatch waitToProceed;

        public EntryLatchModifyingOtherReading(CountDownLatch start, CountDownLatch waitToProceed, CountDownLatch stop) {
            this.start = start;
            this.stop = stop;
            this.waitToProceed = waitToProceed;
        }

        @Override
        public Object process(final Map.Entry<String, SimpleValue> entry) {
            start.countDown();
            try {
                waitToProceed.await();
                final SimpleValue value = entry.getValue();
                value.i++;
                entry.setValue(value);
                return null;
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                stop.countDown();
            }
        }

        @Override
        public EntryBackupProcessor getBackupProcessor() {
            return null;
        }

        @Override
        public void processBackup(Map.Entry<String, SimpleValue> entry) {
            process(entry);
        }

        @Override
        public String getExecutorName() {
            return Offloadable.OFFLOADABLE_EXECUTOR;
        }

    }

    private static class EntryLatchReadOnlyVerifyingWhileOtherWriting implements EntryProcessor<String, SimpleValue>,
            EntryBackupProcessor<String, SimpleValue>, Offloadable, ReadOnly {

        private final CountDownLatch otherStarted;
        private final CountDownLatch otherWaitingToProceed;
        private final CountDownLatch otherStopped;

        public EntryLatchReadOnlyVerifyingWhileOtherWriting(CountDownLatch otherStarted, CountDownLatch otherWaitingToProceed,
                                                            CountDownLatch otherStopped) {
            this.otherStarted = otherStarted;
            this.otherWaitingToProceed = otherWaitingToProceed;
            this.otherStopped = otherStopped;
        }

        @Override
        public Object process(final Map.Entry<String, SimpleValue> entry) {
            if (otherStarted.getCount() != 0 || otherStopped.getCount() != 1) {
                throw new RuntimeException("Wrong threading order");
            }
            otherWaitingToProceed.countDown();
            return null;
        }

        @Override
        public EntryBackupProcessor getBackupProcessor() {
            return null;
        }

        @Override
        public void processBackup(Map.Entry<String, SimpleValue> entry) {
            process(entry);
        }

        @Override
        public String getExecutorName() {
            return Offloadable.OFFLOADABLE_EXECUTOR;
        }
    }

    private String init() {
        String key = generateKeyOwnedBy(instances[0]);
        SimpleValue givenValue = new SimpleValue(1);
        IMap<Object, Object> map = instances[1].getMap(MAP_NAME);
        map.put(key, givenValue);
        return key;
    }

    @Test
    public void testEntryProcessorWithKey_lockedVsUnlocked() {
        String key = init();
        IMap<Object, Object> map = instances[1].getMap(MAP_NAME);

        // not locked -> will offload
        String thread = (String) map.executeOnKey(key, new ThreadSneakingOffloadableEntryProcessor());
        assertTrue(thread.contains("cached.thread"));

        // locked -> won't offload
        map.lock(key);
        thread = (String) map.executeOnKey(key, new ThreadSneakingOffloadableEntryProcessor());
        assertTrue(thread.contains("partition-operation.thread"));
    }

    @Test
    public void testEntryProcessorWithKey_lockedVsUnlocked_ReadOnly() {
        String key = init();
        IMap<Object, Object> map = instances[1].getMap(MAP_NAME);

        // not locked -> will offload
        String thread = (String) map.executeOnKey(key, new ThreadSneakingOffloadableReadOnlyEntryProcessor());
        assertTrue(thread.contains("cached.thread"));

        // locked -> will offload
        map.lock(key);
        thread = (String) map.executeOnKey(key, new ThreadSneakingOffloadableReadOnlyEntryProcessor());
        assertTrue(thread.contains("cached.thread"));
    }

    private static class ThreadSneakingOffloadableEntryProcessor extends AbstractEntryProcessor<String, SimpleValue>
            implements Offloadable {
        @Override
        public Object process(Map.Entry<String, SimpleValue> entry) {
            // returns the name of thread it runs on
            return Thread.currentThread().getName();
        }

        @Override
        public String getExecutorName() {
            return OFFLOADABLE_EXECUTOR;
        }
    }

    private static class ThreadSneakingOffloadableReadOnlyEntryProcessor implements EntryProcessor<String, SimpleValue>,
            Offloadable, ReadOnly {
        @Override
        public Object process(Map.Entry<String, SimpleValue> entry) {
            // returns the name of thread it runs on
            return Thread.currentThread().getName();
        }

        @Override
        public EntryBackupProcessor getBackupProcessor() {
            return null;
        }

        @Override
        public String getExecutorName() {
            return OFFLOADABLE_EXECUTOR;
        }
    }

    @Test
    public void testEntryProcessorWithKey_localNotReentrant() throws ExecutionException, InterruptedException {
        String key = init();
        IMap<Object, SimpleValue> map = instances[1].getMap(MAP_NAME);
        int count = 100;

        // when
        List<ICompletableFuture> futures = new ArrayList<ICompletableFuture>();
        for (int i = 0; i < count; i++) {
            futures.add(map.submitToKey(key, new IncrementingOffloadableEP()));
        }
        for (ICompletableFuture future : futures) {
            future.get();
        }

        // then
        assertEquals(count + 1, map.get(key).i);
    }

    private static class IncrementingOffloadableEP implements EntryProcessor<String, SimpleValue>, Offloadable,
            EntryBackupProcessor<String, SimpleValue> {
        @Override
        public Object process(final Map.Entry<String, SimpleValue> entry) {
            SimpleValue value = entry.getValue();
            value.i++;
            entry.setValue(value);
            return null;
        }

        @Override
        public String getExecutorName() {
            return Offloadable.OFFLOADABLE_EXECUTOR;
        }

        @Override
        public EntryBackupProcessor getBackupProcessor() {
            return this;
        }

        @Override
        public void processBackup(Map.Entry<String, SimpleValue> entry) {
            process(entry);
        }
    }

    @Test
    public void testEntryProcessorWithKey_localNotReentrant_latchTest() throws ExecutionException, InterruptedException {
        String key = generateKeyOwnedBy(instances[0]);
        SimpleValue givenValue = new SimpleValue(1);
        IMap<Object, Object> map = instances[0].getMap(MAP_NAME);
        map.put(key, givenValue);

        CountDownLatch mayStart = new CountDownLatch(1);
        CountDownLatch stopped = new CountDownLatch(1);
        ICompletableFuture first = map.submitToKey(key, new EntryLatchAwaitingModifying(mayStart, stopped));
        mayStart.countDown();
        ICompletableFuture second = map.submitToKey(key, new EntryOtherStoppedVerifying(stopped));

        while (!(first.isDone() && second.isDone())) {
            sleepAtLeastMillis(1);
        }

        // verifies that the other has stopped before the first one started
        assertEquals(0L, second.get());
    }

    private static class EntryLatchAwaitingModifying implements EntryProcessor<String, SimpleValue>, Offloadable, EntryBackupProcessor<String, SimpleValue> {

        private final CountDownLatch mayStart;
        private final CountDownLatch stop;

        public EntryLatchAwaitingModifying(CountDownLatch mayStart, CountDownLatch stop) {
            this.mayStart = mayStart;
            this.stop = stop;
        }

        @Override
        public Object process(final Map.Entry<String, SimpleValue> entry) {
            try {
                mayStart.await();
            } catch (InterruptedException e) {
            }
            try {
                entry.setValue(entry.getValue());
                return null;
            } finally {
                stop.countDown();
            }
        }

        @Override
        public EntryBackupProcessor getBackupProcessor() {
            return null;
        }

        @Override
        public void processBackup(Map.Entry<String, SimpleValue> entry) {
            process(entry);
        }

        @Override
        public String getExecutorName() {
            return Offloadable.OFFLOADABLE_EXECUTOR;
        }

    }

    private static class EntryOtherStoppedVerifying implements EntryProcessor<String, SimpleValue>, Offloadable {

        private final CountDownLatch otherStopped;

        public EntryOtherStoppedVerifying(CountDownLatch otherStopped) {
            this.otherStopped = otherStopped;
        }

        @Override
        public Object process(final Map.Entry<String, SimpleValue> entry) {
            return otherStopped.getCount();
        }

        @Override
        public EntryBackupProcessor getBackupProcessor() {
            return null;
        }

        @Override
        public String getExecutorName() {
            return Offloadable.OFFLOADABLE_EXECUTOR;
        }
    }

    /**
     * <pre>
     * Given: Heart beats are configured to come each few seconds (i.e. one quarter of hazelcast.operation.call.timeout.millis
     *        - set in the {@code getConfig()} method)
     * When: An offloaded EntryProcessor takes a long time to run.
     * Then: Heart beats are still coming during the task is offloaded.
     * </pre>
     *
     * @see #getConfig()
     * @see #HEARTBEATS_INTERVAL_SEC
     */
    @Test
    public void testHeartBeatsComingWhenEntryPropcessorOffloaded() throws Exception {
        final String key = generateKeyOwnedBy(instances[1]);
        TimestampedSimpleValue givenValue = new TimestampedSimpleValue(1);

        final IMap<Object, Object> map = instances[0].getMap(MAP_NAME);
        map.put(key, givenValue);

        final Address instance1Address = instances[1].getCluster().getLocalMember().getAddress();
        final List<Long> heartBeatTimestamps = new LinkedList<Long>();
        Thread hbMonitorThread = new Thread() {
            public void run() {
                NodeEngine nodeEngine = HazelcastTestSupport.getNodeEngineImpl(instances[0]);
                OperationServiceImpl osImpl = (OperationServiceImpl) nodeEngine.getOperationService();
                Map<Address, AtomicLong> heartBeats = osImpl.getInvocationMonitor().getHeartbeatPerMember();
                long lastbeat = Long.MIN_VALUE;
                while (!isInterrupted()) {
                    AtomicLong timestamp = heartBeats.get(instance1Address);
                    if (timestamp != null) {
                        long newlastbeat = timestamp.get();
                        if (lastbeat != newlastbeat) {
                            lastbeat = newlastbeat;
                            heartBeatTimestamps.add(newlastbeat);
                        }
                    }
                    HazelcastTestSupport.sleepMillis(100);
                }
            }
        };

        final int secondsToRun = 8;
        try {
            hbMonitorThread.start();
            map.executeOnKey(key, new TimeConsumingOffloadableTask(secondsToRun));
        } finally {
            hbMonitorThread.interrupt();
        }

        int heartBeatCount = 0;
        TimestampedSimpleValue updatedValue = (TimestampedSimpleValue) map.get(key);
        for (long heartBeatTimestamp : heartBeatTimestamps) {
            if (heartBeatTimestamp > updatedValue.processStart
                    && heartBeatTimestamp < updatedValue.processEnd) {
                heartBeatCount++;
            }
        }

        assertTrue("Heartbeats should be received while offloadable entry processor is running", heartBeatCount > 0);
    }

    private static class TimeConsumingOffloadableTask implements EntryProcessor<String, TimestampedSimpleValue>, Offloadable,
                                                                 EntryBackupProcessor<String, TimestampedSimpleValue>,
                                                                 Serializable {

        private final int secondsToWork;

        private TimeConsumingOffloadableTask(int secondsToWork) {
            this.secondsToWork = secondsToWork;
        }

        @Override
        public Object process(final Map.Entry<String, TimestampedSimpleValue> entry) {
            final TimestampedSimpleValue value = entry.getValue();
            value.processStart = System.currentTimeMillis();
            long endTime = TimeUnit.SECONDS.toMillis(secondsToWork) + System.currentTimeMillis() + 500L;
            do {
                HazelcastTestSupport.sleepMillis(200);
                value.i++;
                entry.setValue(value);
            } while (System.currentTimeMillis() < endTime);
            value.i = 1;
            entry.setValue(value);
            value.processEnd = System.currentTimeMillis();
            return null;
        }

        @Override
        public EntryBackupProcessor<String, TimestampedSimpleValue> getBackupProcessor() {
            return this;
        }

        @Override
        public void processBackup(Map.Entry<String, TimestampedSimpleValue> entry) {
            process(entry);
        }

        @Override
        public String getExecutorName() {
            return Offloadable.OFFLOADABLE_EXECUTOR;
        }

    }

    private static class SimpleValue implements Serializable {

        int i;

        SimpleValue() {
        }

        SimpleValue(final int i) {
            this.i = i;
        }

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

            SimpleValue that = (SimpleValue) o;

            if (i != that.i) {
                return false;
            }

            return true;
        }

        @Override
        public String toString() {
            return "value: " + i;
        }
    }

    private static class TimestampedSimpleValue extends SimpleValue {
        private long processStart;
        private long processEnd;

        public TimestampedSimpleValue() {
            super();
        }

        public TimestampedSimpleValue(int i) {
            super(i);
        }
    }

}


text:
/*----------------------------------------------------------------------
  $Id: general.h 764 2007-10-23 23:44:49Z hubert@u.washington.edu $
  ----------------------------------------------------------------------*/

/* ========================================================================
 * Copyright 2006 University of Washington
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * ========================================================================
*/

#ifndef _GENERAL_INCLUDED
#define _GENERAL_INCLUDED

#include "system.h"

/*
 *  Generally useful definitions and constants
 */

/* Might as well be consistant */
#undef	FALSE
#define FALSE   0                       /* False, no, bad, etc.         */
#undef	TRUE
#define TRUE    1                       /* True, yes, good, etc.        */
#define ABORT   2                       /* Death, ^G, abort, etc.       */
#define	COUNT	3			/* For control-c command        */

#undef  MIN
#define MIN(a,b)	((a) < (b) ? (a) : (b))
#undef  MAX
#define MAX(a,b)	((a) > (b) ? (a) : (b))

#ifdef O_BINARY
#define STDIO_READ	"rb"
#define STDIO_APPEND	"a+b"
#else /* !O_BINARY */
#define O_BINARY	0
#define STDIO_READ	"r"
#define STDIO_APPEND	"a+"
#endif /* !O_BINARY */

#ifndef O_TEXT
#define O_TEXT		0
#endif /* !O_TEXT */

#ifndef _O_WTEXT
#define _O_WTEXT	0
#endif /* !O_WTEXT */

#ifndef _O_U8TEXT
#define _O_U8TEXT	0
#endif /* !O_U8TEXT */

/* Various character constants */
#define BACKSPACE	'\b'     	/* backspace character  */
#define TAB		'\t'            /* tab character        */
#define RETURN		'\r'     	/* carriage return char */
#define LINE_FEED	'\n'     	/* line feed character  */
#define FORMFEED	'\f'     	/* form feed (^L) char  */
#define COMMA		','		/* comma character      */
#define SPACE		' '		/* space character      */
#define ESCAPE		'\033'		/* the escape		*/
#define	BELL		'\007'		/* the bell		*/
#define LPAREN		'('		/* left parenthesis	*/
#define RPAREN		')'		/* right parenthesis	*/
#define BSLASH		'\\'		/* back slash		*/
#define QUOTE		'"'		/* double quote char	*/
#define DEL		'\177'		/* delete		*/

/*
 * These help with isspace when dealing with UCS-4 characters.
 * 0x3000 == Ideographic Space (as wide as a CJK character cell)
 * 0x2002 == En Space
 * 0x2003 == Em Space
 */
#define SPECIAL_SPACE(c)	((c) == 0x3000 || (c) == 0x2002 || (c) == 0x2003)

/* Longest foldername we ever expect */
#define MAXFOLDER      (128)

/* Various maximum field lengths, probably shouldn't be changed. */
#define MAX_FULLNAME     (100)
#define MAX_NICKNAME      (80)
#define MAX_ADDRESS      (500)
#define MAX_NEW_LIST     (500)  /* Max addrs to be added when creating list */
#define MAX_SEARCH       (100)  /* Longest string to search for             */
#define MAX_ADDR_EXPN   (1000)  /* Longest expanded addr                    */
#define MAX_ADDR_FIELD (10000)  /* Longest fully-expanded addr field        */

/*
 * Input timeout fudge factor
 */
#define IDLE_TIMEOUT	(8)
#define FUDGE		(30)	/* better be at least 20 */

/*
 * We use a 32 bit unsigned int to carry UCS-4 characters.
 * C-client uses unsigned long for this, so we have to do
 * some minor conversion at that interface. UCS-4 characters
 * are really only 21 bits. We do use the extra space for
 * defining some special values that a character might have.
 * In particular, the set of values like KEY_UP, KEY_RESIZE,
 * NO_OP_IDLE, F12, and so on are 32 bit quantities that don't
 * interfere with the actual character values. They are also
 * all positive values with the most significant bit set to 0,
 * so a 32 bit signed integer could hold them all.
 */
typedef UINT32 UCS;

/*
 * The type of an IMAP UID, which is a 32-bit unsigned int.
 * This could be UINT32 instead of unsigned long but we use
 * unsigned long because that's what c-client uses.
 */
typedef unsigned long imapuid_t;

#endif /* _GENERAL_INCLUDED */


text:
package com.twu.biblioteca;

import com.twu.biblioteca.service.Command;
import com.twu.biblioteca.unit.ConsoleInput;
import com.twu.biblioteca.unit.Message;

/**
 * Created by Thoughtworks on 6/6/15.
 */
public class Engine implements Runnable{
    public Engine() {
        new Thread(this).start();
    }

    @Override
    public void run() {
        Message.putln("Welcome To Biblioteca Library");
        while(true){
            CommandName.showAllCommandName();
            CommandFactory commandFactory = new CommandFactoryImpl();
            String commandNameString = ConsoleInput.getString();
            CommandNameFactory commandNameFactory = new CommandNameFactoryImpl();
            CommandName commandName = commandNameFactory.transStringToCommandName(commandNameString);
            Command command = commandFactory.transStringToCommand(commandName);
            command.process();
        }
    }
}


text:
package com.smoketurner.pipeline.application.core;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.event.S3EventNotification.S3BucketEntity;
import com.amazonaws.services.s3.event.S3EventNotification.S3Entity;
import com.amazonaws.services.s3.event.S3EventNotification.S3EventNotificationRecord;
import com.amazonaws.services.s3.event.S3EventNotification.S3ObjectEntity;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.S3Object;
import com.smoketurner.pipeline.application.exceptions.AmazonS3ConstraintException;
import com.smoketurner.pipeline.application.exceptions.AmazonS3ZeroSizeException;
import org.junit.Before;
import org.junit.Test;

public class AmazonS3DownloaderTest {

  private final AmazonS3Client mockS3 = mock(AmazonS3Client.class);
  private final AmazonS3Downloader downloader = new AmazonS3Downloader(mockS3);
  private S3EventNotificationRecord record;

  @Before
  public void setUp() {
    reset(mockS3);

    final S3BucketEntity bucket = new S3BucketEntity("bucket-name", null, null);
    final S3ObjectEntity object =
        new S3ObjectEntity("object-key", 100L, "object eTag", "object version", null);
    final S3Entity s3 = new S3Entity(null, bucket, object, null);
    record =
        new S3EventNotificationRecord(
            "us-east-1", null, "aws:s3", "1970-01-01T00:00:00.000Z", "2.0", null, null, s3, null);
  }

  @Test
  public void testFetch() throws Exception {
    final S3Object expected = new S3Object();
    expected.setKey("object-key");
    expected.setBucketName("bucket-name");
    expected.getObjectMetadata().setContentLength(100);

    when(mockS3.getObject(any(GetObjectRequest.class))).thenReturn(expected);

    final S3Object actual = downloader.fetch(record);
    verify(mockS3).getObject(any(GetObjectRequest.class));
    assertThat(actual).isEqualTo(expected);
  }

  @Test
  public void testFetchInvalidETag() throws Exception {
    when(mockS3.getObject(any(GetObjectRequest.class))).thenReturn(null);

    try {
      final S3Object download = downloader.fetch(record);
      assertThat(download).isNull();
      failBecauseExceptionWasNotThrown(AmazonS3ConstraintException.class);
    } catch (AmazonS3ConstraintException e) {
    }
    verify(mockS3).getObject(any(GetObjectRequest.class));
  }

  @Test
  public void testFetchZeroSize() throws Exception {
    final S3Object expected = new S3Object();
    expected.setKey("object-key");
    expected.setBucketName("bucket-name");

    when(mockS3.getObject(any(GetObjectRequest.class))).thenReturn(expected);

    try {
      final S3Object download = downloader.fetch(record);
      assertThat(download).isNotNull();
      failBecauseExceptionWasNotThrown(AmazonS3ZeroSizeException.class);
    } catch (AmazonS3ZeroSizeException e) {
    }
    verify(mockS3).getObject(any(GetObjectRequest.class));
  }

  @Test
  public void testFetchServiceException() throws Exception {
    when(mockS3.getObject(any(GetObjectRequest.class)))
        .thenThrow(new AmazonServiceException("error"));

    try {
      downloader.fetch(record);
      failBecauseExceptionWasNotThrown(AmazonServiceException.class);
    } catch (AmazonServiceException e) {
    }
    verify(mockS3).getObject(any(GetObjectRequest.class));
  }

  @Test
  public void testFetchClientException() throws Exception {
    when(mockS3.getObject(any(GetObjectRequest.class)))
        .thenThrow(new AmazonClientException("error"));

    try {
      downloader.fetch(record);
      failBecauseExceptionWasNotThrown(AmazonClientException.class);
    } catch (AmazonClientException e) {
    }
    verify(mockS3).getObject(any(GetObjectRequest.class));
  }

  @Test
  public void testIsGzipped() {
    assertThat(AmazonS3Downloader.isGZipped(null)).isFalse();

    final S3Object object = new S3Object();
    assertThat(AmazonS3Downloader.isGZipped(object)).isFalse();

    final ObjectMetadata metadata = object.getObjectMetadata();
    metadata.setContentEncoding("gzip");
    assertThat(AmazonS3Downloader.isGZipped(object)).isTrue();

    metadata.setContentEncoding("GZIP");
    assertThat(AmazonS3Downloader.isGZipped(object)).isTrue();

    metadata.setContentEncoding(" GzIP ");
    assertThat(AmazonS3Downloader.isGZipped(object)).isTrue();

    metadata.setContentEncoding(null);

    object.setKey("test");
    assertThat(AmazonS3Downloader.isGZipped(object)).isFalse();

    object.setKey("test.gz");
    assertThat(AmazonS3Downloader.isGZipped(object)).isTrue();

    object.setKey("test.gz   ");
    assertThat(AmazonS3Downloader.isGZipped(object)).isTrue();

    object.setKey("test.GZ ");
    assertThat(AmazonS3Downloader.isGZipped(object)).isTrue();
  }
}


text:
var controllers = require('./controllers');

var router = function(app) {

    app.get('/', controllers.main);
    app.get('/variables', controllers.getVariablePage);
    app.get('/extended', controllers.getExtended);
    app.get('/gallery', controllers.getGallery);
    app.post('/time', controllers.updateTime);
};

module.exports = router;


text:
@import Accelerate;

static CGFloat predictPosition(CFTimeInterval time, double *constants, la_count_t countOfConstants)
{
	CGFloat position = 0;
	for (la_count_t idx = 0; idx < countOfConstants; ++idx) {
		position += constants[idx] * pow(time, idx);
	}

	return position;
}

@interface MotionPredictingDragBehaviour ()

@property (nonatomic, strong, readonly) CADisplayLink *displayLink;
@property (nonatomic, strong, readonly) UIPanGestureRecognizer *recogniser;

@end

@implementation MotionPredictingDragBehaviour
{
	CADisplayLink *_displayLink;
	UIPanGestureRecognizer *_recogniser;

	double *_previousXPositions;
	double *_previousYPositions;
	double *_observationTimes;
	la_count_t _countOfPreviousPositions;
}

@synthesize view = _view;

- (void)dealloc
{
	free(_previousXPositions);
	free(_previousYPositions);
	free(_observationTimes);
}

- (instancetype)init
{
	return [self initWithFramesToPredict:0 polynomialDegree:0 maxObservations:0];
}

- (instancetype)initWithFramesToPredict:(NSUInteger)framesToPredict polynomialDegree:(NSUInteger)polynomialDegree maxObservations:(NSUInteger)maxObservations
{
	self = [super init];
	if (self == nil) return nil;

	_framesToPredict = framesToPredict;
	_polynomialDegree = polynomialDegree;
	_maxObservations = maxObservations;

	_previousXPositions = malloc(maxObservations * sizeof *_previousXPositions);
	_previousYPositions = malloc(maxObservations * sizeof *_previousYPositions);
	_observationTimes = malloc(maxObservations * sizeof *_observationTimes);

	return self;
}

- (NSString *)description
{
	return [NSString stringWithFormat:@"%lu %@\ndegree %lu\n%lu obs", (unsigned long)[self framesToPredict], [self framesToPredict] == 1 ? @"frame" : @"frames", (unsigned long)[self polynomialDegree], (unsigned long)[self maxObservations]];
}

- (void)setView:(UIView *)view
{
	_view = view;

	[view addGestureRecognizer:[self recogniser]];
}

- (void)handlePan:(UIPanGestureRecognizer *)recogniser
{
	switch ([recogniser state]) {
		case UIGestureRecognizerStatePossible:
			break;

		case UIGestureRecognizerStateBegan:
			_countOfPreviousPositions = 0;
			[[self displayLink] setPaused:NO];
			break;

		case UIGestureRecognizerStateChanged:
			break;

		case UIGestureRecognizerStateEnded: case UIGestureRecognizerStateCancelled: case UIGestureRecognizerStateFailed:
			[[self displayLink] setPaused:YES];
			break;
	}
}

- (void)update:(CADisplayLink *)sender
{
	UIPanGestureRecognizer *recogniser = [self recogniser];
	UIView *const view = [recogniser view];

	CGPoint const currentPosition = [recogniser locationInView:[view superview]];
	[self savePositionObservation:currentPosition];

	// Fallback
	[view setCenter:currentPosition];

	la_count_t const countOfColumns = [self polynomialDegree] + 1;

	if (_countOfPreviousPositions < countOfColumns) {
		return;
	}

	CFTimeInterval const now = CACurrentMediaTime();

	double *timeMatrixBuffer = malloc(countOfColumns * _countOfPreviousPositions * sizeof(double));
	for (la_count_t rowIndex = 0; rowIndex < _countOfPreviousPositions; ++rowIndex) {

		double const relativeTime = _observationTimes[rowIndex] - now;

		for (la_count_t colIndex = 0; colIndex < countOfColumns; ++colIndex) {
			timeMatrixBuffer[countOfColumns * rowIndex + colIndex] = pow(relativeTime, colIndex);
		}
	}
	la_object_t const timeMatrix = la_matrix_from_double_buffer(timeMatrixBuffer, _countOfPreviousPositions, countOfColumns, countOfColumns, LA_NO_HINT, LA_ATTRIBUTE_ENABLE_LOGGING);
	if (la_status(timeMatrix) != LA_SUCCESS) {
		NSLog(@"Could not create time matrix: %@", @(la_status(timeMatrix)));
	}
	free(timeMatrixBuffer);
	timeMatrixBuffer = NULL;

//	NSLog(@"timeMatrix: %@", LAObjectDescription(timeMatrix));

	la_object_t const xPositionsVector = la_vector_from_double_buffer(_previousXPositions, _countOfPreviousPositions, 1, LA_ATTRIBUTE_ENABLE_LOGGING);
	if (la_status(xPositionsVector) != LA_SUCCESS) {
		NSLog(@"Could not create x positions vector: %@", @(la_status(xPositionsVector)));
	}

//	NSLog(@"xPositionsVector: %@", LAObjectDescription(xPositionsVector));

	la_object_t const yPositionsVector = la_vector_from_double_buffer(_previousYPositions, _countOfPreviousPositions, 1, LA_ATTRIBUTE_ENABLE_LOGGING);
	if (la_status(yPositionsVector) != LA_SUCCESS) {
		NSLog(@"Could not create y positions vector: %@", @(la_status(yPositionsVector)));
	}

//	NSLog(@"yPositionsVector: %@", LAObjectDescription(yPositionsVector));

	la_object_t const xSolution = dh_la_solve(timeMatrix, xPositionsVector);
	if (xSolution == nil || la_status(xSolution) != LA_SUCCESS) {
		NSLog(@"Could not solve x equation: %@", @(la_status(xSolution)));
		return;
	}

	la_object_t const ySolution = dh_la_solve(timeMatrix, yPositionsVector);
	if (ySolution == nil || la_status(ySolution) != LA_SUCCESS) {
		NSLog(@"Could not solve y equation: %@", @(la_status(ySolution)));
		return;
	}

	double xSolValues[countOfColumns];
	la_status_t const xStatus = la_vector_to_double_buffer(xSolValues, 1, xSolution);
	if (xStatus != LA_SUCCESS) {
		NSLog(@"Could not read x values: %@", @(xStatus));
		return;
	}

	double ySolValues[countOfColumns];
	la_status_t const yStatus = la_vector_to_double_buffer(ySolValues, 1, ySolution);
	if (yStatus != LA_SUCCESS) {
		NSLog(@"Could not read y values: %@", @(yStatus));
		return;
	}

//	NSLog(@"SUCCESS");

	CFTimeInterval const future = [self framesToPredict] / 60.0;
	CGPoint const futurePosition = CGPointMake(predictPosition(future, xSolValues, countOfColumns), predictPosition(future, ySolValues, countOfColumns));

	[view setCenter:futurePosition];
}

- (void)savePositionObservation:(CGPoint)position
{
	if ([self maxObservations] == 0) {
		return;
	}

	if (_countOfPreviousPositions == [self maxObservations]) {
		// Remove top entries by shifting everything else up.
		memmove(_previousXPositions, _previousXPositions + 1, ([self maxObservations] - 1) * sizeof _previousXPositions[0]);
		memmove(_previousYPositions, _previousYPositions + 1, ([self maxObservations] - 1) * sizeof _previousYPositions[0]);
		memmove(_observationTimes,   _observationTimes + 1,   ([self maxObservations] - 1) * sizeof _observationTimes[0]);

		--_countOfPreviousPositions;
	}

	_previousXPositions[_countOfPreviousPositions] = (double)position.x;
	_previousYPositions[_countOfPreviousPositions] = (double)position.y;
	_observationTimes[_countOfPreviousPositions] = CACurrentMediaTime();

	++_countOfPreviousPositions;
}

#pragma mark -

- (CADisplayLink *)displayLink
{
	if (_displayLink) return _displayLink;

	_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(update:)];
	[_displayLink setPaused:YES];
	[_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];

	return _displayLink;
}

- (UIPanGestureRecognizer *)recogniser
{
	if (_recogniser) return _recogniser;

	_recogniser = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];

	return _recogniser;
}

@end


text:
var Config = require("./config");
const { Client } = require('@elastic/elasticsearch');
var moment = require('moment');
require("./extensions");
const { timeout } = require("./extensions");

module.exports = Database = function() {
    this.hostAddress = "http://search_database:9200";
}

var createUpsertOperation = function(album) {
    return [
        { update : { _id : album.id } },
        {
            script: {
                lang: "painless",
                source: "if(!ctx._source.tags.contains(params.new_tag)) { ctx._source.tags.add(params.new_tag); }",
                params: {
                    new_tag: album.tags[0]
                }
            },
            upsert: album
        }
    ]
};

var maxConnectionAttemptTime = 60*1000;

var tryConnection = async function(client, time) {
    if(time > maxConnectionAttemptTime) {
        throw new Error("Unable to connect to database");
    }

    try {
        await client.ping();
        await timeout(3000); // Just to make sure indexes are up and ready
    }
    catch(error) {
        await timeout(1500);
        await tryConnection(client, time + 1000);
    }
};

// var tagsearchMappings = {
//     index: "tagsearch",
//     body: {
//         mappings : {
//             albums : {
//                 properties : {
//                     tags : { type: "keyword", index: "not_analyzed" }
//                 }
//             },
//             tags : {
//                 properties : {
//                     lastUpdated : { type:"date", format: "basic_date_time_no_millis" }
//                 }
//             }
//         }
//     }
// }

var albumsIndexMappings = {
    index: "albums",
    body: {
        mappings : {
            properties : {
                tags : { type: "keyword", index: true }
            }
        }
    }
}

var tagsIndexMappings = {
    index: "tags",
    body: {
        mappings : {
            properties : {
                lastUpdated : { type: "date", format: "basic_date_time_no_millis" }
            }
        }
    }
}

Database.prototype = {
    createClient: function() {
        return new Client({
            node: this.hostAddress
        })
    },
    waitForConnection: async function() {
        var client = new Client({
            node: this.hostAddress,
            log: []
        });
        //return new Promise((resolve, reject) => reject());
        // return await client.ping({}, { requestTimeout: maxConnectionAttemptTime });
            // .catch(() => {
            //     new Error("Unable to connect to database");
            // });
        await tryConnection(client, 0);
    },
    createIfNeeded: async function() {
        var client = this.createClient();
        const { body: albumsIndexExists } = await client.indices.exists({ index: "albums" });
        const { body: tagsIndexExists } = await client.indices.exists({ index: "tags" });

        if(albumsIndexExists && tagsIndexExists)
            return "Index already exists, no need to create";

        let message = "";
        if(!albumsIndexExists){
            await client.indices.create(albumsIndexMappings);
            message += "Albums index does not exist, creating mappings. ";
        }
        if(!tagsIndexExists){
            await client.indices.create(tagsIndexMappings);
            message += "Tags index does not exist, creating mappings. ";
        }

        return message;
    },
    saveTag: async function(tag) {
        await this
            .createClient()
            .index({
                index: "tags",
                id: tag,
                body: {
                    lastUpdated: moment().format("YYYYMMDDTHHmmssZ")
                }
            });
    },
    saveAlbums: async function(tag, albums) {
        if(albums.length == 0) {
            return;
        }

        albums.forEach(album => { album.tags = [ tag ]});

        await this.createClient()
            .bulk({
                index: "albums",
                body: albums.map(album => createUpsertOperation(album)).flatten()
            });
    },
    getUnsavedTags: async function(tags) {
        var chunkedTags = tags.chunk(2);
        var total = [];
        db = this;
        for(const chunk of chunkedTags) {
            const results = await db
                .createClient()
                .search({
                    index: "tags",
                    size: chunk.length,
                    body: {
                        query: {
                            terms: {
                                _id: chunk
                            }
                        }
                    }
                })
            var savedTags = results.body.hits.hits.map(x => x._id);
            var unsavedTags = chunk.filter(tag => savedTags.indexOf(tag) == -1);
            total = total.concat(unsavedTags);
        }

        return total;
    },
    getTagWithOldestUpdate: async function() {
        const results = await this
            .createClient()
            .search({
                index: "tags",
                body: {
                    query: { match_all: { } },
                    size: 1,
                    sort: [ {
                            lastUpdated: {
                                order: "asc"
                            }
                        }
                    ]
                }
            });
        return results.body.hits.hits.map(x => x._id)[0]
    },
    getAlbumsByTags: async function(count, tags) {
        var terms = tags.map(tag => {
            return { term: { tags: tag } }
        });
        const results = await this
            .createClient()
            .search({
                index: "albums",
                body: {
                    query: {
                        bool: {
                            must: terms
                        }
                    },
                    size: count
                }
            });
        return results.body.hits.hits.map(x => x._source);
    },
    getTagCount: async function() {
        const { body } = await this
            .createClient()
            .count({
                index: "tags",
            })
        return body.count;
    },
    getAlbumCount: async function() {
        const { body } = await this
            .createClient()
            .count({
                index: "albums",
            });
        return body.count;
    }
}


text:
typedef union tag_bigram {
    uint64_t value;
    struct {
        uint32_t prev_class_id:32;
        uint32_t class_id:32;
    };
} tag_bigram_t;

KHASH_MAP_INIT_INT64(prev_tag_class_weights, class_weight_t)

KHASH_MAP_INIT_INT(feature_prev_tag_class_weights, khash_t(prev_tag_class_weights) *)

typedef struct crf_averaged_perceptron_trainer {
    crf_trainer_t *base_trainer;
    uint64_t num_updates;
    uint64_t num_errors;
    uint32_t iterations;
    uint64_t min_updates;
    // {feature_id => {class_id => class_weight_t}}
    khash_t(feature_class_weights) *weights;
    khash_t(feature_prev_tag_class_weights) *prev_tag_weights;
    khash_t(prev_tag_class_weights) *trans_weights;
    uint64_array *update_counts;
    uint64_array *prev_tag_update_counts;
    cstring_array *sequence_features;
    uint32_array *sequence_features_indptr;
    cstring_array *sequence_prev_tag_features;
    uint32_array *sequence_prev_tag_features_indptr;
    uint32_array *label_ids;
    uint32_array *viterbi;
} crf_averaged_perceptron_trainer_t;

crf_averaged_perceptron_trainer_t *crf_averaged_perceptron_trainer_new(size_t num_classes, size_t min_updates);

uint32_t crf_averaged_perceptron_trainer_predict(crf_averaged_perceptron_trainer_t *self, cstring_array *features);

bool crf_averaged_perceptron_trainer_train_example(crf_averaged_perceptron_trainer_t *self,
                                                   void *tagger,
                                                   void *context,
                                                   cstring_array *features,
                                                   cstring_array *prev_tag_features,
                                                   tagger_feature_function feature_function,
                                                   tokenized_string_t *tokenized,
                                                   cstring_array *labels
                                                   );

crf_t *crf_averaged_perceptron_trainer_finalize(crf_averaged_perceptron_trainer_t *self);

void crf_averaged_perceptron_trainer_destroy(crf_averaged_perceptron_trainer_t *self);

#endif