text:
package com.wuyu.plugin.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.squareup.okhttp.*;
import com.squareup.okhttp.Request.Builder;
import com.wuyu.plugin.agent.AgentFactory;
import com.wuyu.plugin.agent.AgentType;

/**
 * organization www.wuyushuo.com
 * created on 2016/02/04 by elon su
 * email addr (</>git_wuyu@163.com)
 * @version 1.0.0
 */
public class HttpKitUtil {

    private static final Logger LOG = LoggerFactory.getLogger(HttpKitUtil.class);

    private static final String CHARSET = "UTF-8";

    static OkHttpClient client = new OkHttpClient();

    /**
     *
     * @param url
     * @param headers
     * @param params
     * @return
     */
    public static String get(String url, Map<String, String> headers, Map<String, String> params){
        return get(url, AgentFactory.getInstance().getUserAgent(AgentType.RANDOM), headers, params);
    }

    /**
     *
     * @param url
     * @param userAgent
     * @param headers
     * @param params
     * @return
     */
    public static String get(String url, String userAgent, Map<String, String> headers, Map<String, String> params){
        return get(url, userAgent, false, "localhost", 80, headers, null, params);
    }

    /**
     *
     * @param url
     * @param userAgent
     * @param isProxy
     * @param proxyHost
     * @param proxyPort
     * @param headers
     * @param params
     * @return
     */
    public static String get(String url, String userAgent, boolean isProxy, String proxyHost, int proxyPort,
                             Map<String, String> headers, Map<String, String> params){
        return get(url, userAgent, isProxy, proxyHost, proxyPort, headers, null, params);
    }

    /**
     *
     * @param url
     * @param userAgent
     * @param isProxy
     * @param proxyHost
     * @param proxyPort
     * @param headers
     * @param cookies
     * @param params
     * @return
     */
    public static String get(String url, String userAgent, boolean isProxy, String proxyHost, int proxyPort,
                             Map<String, String> headers, Map<String, String> cookies, Map<String, String> params){
        return get(url, userAgent, isProxy, proxyHost, proxyPort, headers, cookies, params, CHARSET);
    }

    /**
     *
     * @param url
     * @param userAgent
     * @param isProxy
     * @param proxyHost
     * @param proxyPort
     * @param headers
     * @param cookies
     * @param params
     * @param encoding
     * @return
     */
    public static String get(String url, String userAgent, boolean isProxy, String proxyHost, int proxyPort,
                             Map<String, String> headers, Map<String, String> cookies, Map<String, String> params, String encoding){
        try {
            OkHttpClient getClient = client.clone();
            getClient.setConnectTimeout(10, TimeUnit.SECONDS);
            getClient.setWriteTimeout(10, TimeUnit.SECONDS);
            getClient.setReadTimeout(30, TimeUnit.SECONDS);
            Builder builder = new Builder();
            if (isProxy) {
                getClient.setProxy(new java.net.Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)));
            }
            if (userAgent != null) {
                builder.header("User-Agent", userAgent);
            }
            if (headers != null) {
                for (String key : headers.keySet()) {
                    builder.header(key, headers.get(key));
                }
            }
            String cookie = "";
            if (cookies != null) {
                StringBuffer buffer = new StringBuffer();
                for (String key : cookies.keySet()) {
                    buffer.append(key + "=" + cookies.get(key) + ",");
                }
                cookie = buffer.toString();
            }
            builder.header("cookie", cookie);

            if (params != null) {
                StringBuffer sb = new StringBuffer();
                if (params != null && params.size() > 0) {
                    for (String key : params.keySet()) {
                        sb.append(key + "=" + params.get(key) + "&");
                    }
                    url = url + "?" + sb.substring(0, sb.length() - 1);
                }
            }
            builder.url(url);
            Request httpRequest = builder.build();

            Response response = getClient.newCall(httpRequest).execute();
            if (response.isSuccessful()) {
                ResponseBody responseBody = response.body();
                byte[] context = responseBody.bytes();
                return new String(context, encoding);
            }
        }catch (Exception e){
            LOG.error(e.getLocalizedMessage(), e);
        }
        return "";
    }

    /**
     *
     * @param url
     * @param headers
     * @param params
     * @return
     */
    public static String post(String url,Map<String, String> headers, Map<String, String> params){
        return post(url, AgentFactory.getInstance().getUserAgent(AgentType.RANDOM), headers, params);
    }

    /**
     *
     * @param url
     * @param userAgent
     * @param headers
     * @param params
     * @return
     */
    public static String post(String url, String userAgent, Map<String, String> headers, Map<String, String> params){
        return post(url, userAgent, false, "localhost", 80, headers, null, params);
    }

    /**
     *
     * @param url
     * @param userAgent
     * @param isProxy
     * @param proxyHost
     * @param proxyPort
     * @param headers
     * @param params
     * @return
     */
    public static String post(String url, String userAgent, boolean isProxy, String proxyHost, int proxyPort,
                              Map<String, String> headers, Map<String, String> params){
        return post(url, userAgent, isProxy, proxyHost, proxyPort, headers, null, params);
    }

    /**
     *
     * @param url
     * @param userAgent
     * @param isProxy
     * @param proxyHost
     * @param proxyPort
     * @param headers
     * @param cookies
     * @param params
     * @return
     */
    public static String post(String url, String userAgent, boolean isProxy, String proxyHost, int proxyPort,
                              Map<String, String> headers, Map<String, String> cookies, Map<String, String> params){
        return post(url, userAgent, isProxy, proxyHost, proxyPort, headers, cookies, params, CHARSET);
    }

    /**
     *
     * @param url
     * @param userAgent
     * @param isProxy
     * @param proxyHost
     * @param proxyPort
     * @param headers
     * @param cookies
     * @param params
     * @param encoding
     * @return
     */
    public static String post(String url, String userAgent, boolean isProxy, String proxyHost, int proxyPort,
                              Map<String, String> headers, Map<String, String> cookies, Map<String, String> params, String encoding){
        try {
            OkHttpClient postClient = client.clone();
            postClient.setConnectTimeout(10, TimeUnit.SECONDS);
            postClient.setWriteTimeout(10, TimeUnit.SECONDS);
            postClient.setReadTimeout(30, TimeUnit.SECONDS);
            Builder builder = new Builder();
            if (isProxy) {
                postClient.setProxy(new java.net.Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)));
            }
            if (userAgent != null) {
                builder.header("User-Agent", userAgent);
            }
            if (headers != null) {
                for (String key : headers.keySet()) {
                    builder.header(key, headers.get(key));
                }
            }
            String cookie = "";
            if (cookies != null) {
                StringBuffer buffer = new StringBuffer();
                for (String key : cookies.keySet()) {
                    buffer.append(key + "=" + cookies.get(key) + ",");
                }
                cookie = buffer.toString();
            }
            builder.header("cookie", cookie);

            if (params != null) {
                if (params != null) {
                    FormEncodingBuilder formBuilder = new FormEncodingBuilder();
                    for (String key : params.keySet()) {
                        formBuilder.add(key, params.get(key));
                    }
                    RequestBody formBody = formBuilder.build();
                    builder.post(formBody);
                }
            }
            builder.url(url);
            Request httpRequest = builder.build();
            Response response = postClient.newCall(httpRequest).execute();

            if (response.isSuccessful()) {
                ResponseBody responseBody = response.body();
                byte[] context = responseBody.bytes();
                return new String(context, encoding);
            }
        }catch (Exception e){
            LOG.error(e.getLocalizedMessage(), e);
        }
        return "";
    }
}


text:
module Lita
  # Handlers use this class to define HTTP routes for the built-in web
  # server.
  class HTTPRoute
    # An +HttpRouter::Route+ class used for dispatch.
    # @since 3.0.0
    ExtendedRoute = Class.new(HttpRouter::Route) do
      include HttpRouter::RouteHelper
      include HttpRouter::GenerationHelper
    end

    # The handler registering the route.
    # @return [Lita::Handler] The handler.
    attr_reader :handler_class

    # @param handler_class [Lita::Handler] The handler registering the route.
    def initialize(handler_class)
      @handler_class = handler_class
    end

    class << self
      private

      # @!macro define_http_method
      #   @method $1(path, method_name)
      #   Defines a new route with the "$1" HTTP method.
      #   @param path [String] The URL path component that will trigger the
      #     route.
      #   @param method_name [Symbol, String] The name of the instance method in
      #     the handler to call for the route.
      #   @return [void]
      def define_http_method(http_method)
        define_method(http_method) do |path, method_name, options = {}|
          create_route(http_method.to_s.upcase, path, method_name, options)
        end
      end
    end

    define_http_method :head
    define_http_method :get
    define_http_method :post
    define_http_method :put
    define_http_method :patch
    define_http_method :delete
    define_http_method :options
    define_http_method :link
    define_http_method :unlink

    private

    # Creates a new HTTP route.
    def create_route(http_method, path, method_name, options)
      route = ExtendedRoute.new
      route.path = path
      route.add_match_with(options)
      route.add_request_method(http_method)
      route.add_request_method("HEAD") if http_method == "GET"

      route.to do |env|
        request = Rack::Request.new(env)
        response = Rack::Response.new

        if request.head?
          response.status = 204
        else
          handler_class.new(env["lita.robot"]).public_send(method_name, request, response)
        end

        response.finish
      end

      handler_class.http_routes << route
    end
  end
end

redpajama_set_name: RedPajamaGithub


text:
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#include "pal_common.h"
#include "pal_threading.h"
#include "pal_time.h"

#include <pthread.h>

#if HAVE_MACH_ABSOLUTE_TIME
static LowLevelMutex s_lock(true /* abortOnFailure */, nullptr /* successRef */);

mach_timebase_info_data_t g_machTimebaseInfo = {};
bool g_isMachTimebaseInfoInitialized = false;

mach_timebase_info_data_t *InitializeTimebaseInfo()
{
    s_lock.Acquire();

    if (!g_isMachTimebaseInfoInitialized)
    {
        kern_return_t machRet = mach_timebase_info(&g_machTimebaseInfo);
        assert(machRet == KERN_SUCCESS);
        if (machRet == KERN_SUCCESS)
        {
            g_isMachTimebaseInfoInitialized = true;
        }
    }

    s_lock.Release();

    return g_isMachTimebaseInfoInitialized ? &g_machTimebaseInfo : nullptr;
}
#endif


text:
package info.rmapproject.core.rmapservice.impl.rdf4j;

import static info.rmapproject.core.model.event.RMapEventTargetType.DISCO;
import static info.rmapproject.core.model.impl.rdf4j.ORAdapter.uri2Rdf4jIri;
import static info.rmapproject.core.rmapservice.impl.rdf4j.ORMapQueriesLineage.findDerivativesfrom;
import static info.rmapproject.core.rmapservice.impl.rdf4j.ORMapQueriesLineage.findLineageProgenitor;
import static info.rmapproject.core.rmapservice.impl.rdf4j.ORMapQueriesLineage.getLineageMembers;
import static info.rmapproject.core.rmapservice.impl.rdf4j.ORMapQueriesLineage.getLineageMembersWithDates;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

import java.net.URI;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import info.rmapproject.core.CoreTestAbstract;
import info.rmapproject.core.model.RMapIri;
import info.rmapproject.core.model.impl.rdf4j.ORMapEventCreation;
import info.rmapproject.core.model.impl.rdf4j.ORMapEventDerivation;
import info.rmapproject.core.model.impl.rdf4j.ORMapEventUpdate;
import info.rmapproject.core.model.request.RequestEventDetails;
import info.rmapproject.core.rmapservice.impl.rdf4j.ORMapEventMgr;
import info.rmapproject.core.rmapservice.impl.rdf4j.triplestore.Rdf4jTriplestore;

/**
 * @author apb@jhu.edu
 */
public class ORMapQueriesLineageTest extends CoreTestAbstract {

    @Autowired
    private Rdf4jTriplestore ts;

    @Autowired
    private ORMapEventMgr eventmgr;

    URI discoURI;

    URI lineageURI;

    @Before
    public void init() {
        discoURI = randomURI();
        lineageURI = randomURI();
    }

    @Test
    public void viaCreateEventTest() {

        final ORMapEventCreation event = new ORMapEventCreation(
                uri2Rdf4jIri(randomURI()),
                new RequestEventDetails(randomURI()), DISCO,
                asList(new RMapIri(discoURI)));
        event.setEndTime(new Date());
        event.setLineageProgenitor(new RMapIri(lineageURI));

        eventmgr.createEvent(event, ts);

        assertEquals(lineageURI, findLineageProgenitor(discoURI, ts));
    }

    @Test
    public void viaUpdateEventTest() {

        final URI oldDiscoUri = randomURI();

        final ORMapEventCreation creation = new ORMapEventCreation(
                uri2Rdf4jIri(randomURI()),
                new RequestEventDetails(randomURI()), DISCO,
                asList(new RMapIri(oldDiscoUri)));
        creation.setEndTime(new Date());
        creation.setLineageProgenitor(new RMapIri(lineageURI));

        final ORMapEventUpdate update = new ORMapEventUpdate(
                uri2Rdf4jIri(randomURI()),
                new RequestEventDetails(randomURI()), DISCO,
                uri2Rdf4jIri(oldDiscoUri),
                uri2Rdf4jIri(discoURI));
        update.setEndTime(new Date());
        update.setLineageProgenitor(new RMapIri(lineageURI));

        eventmgr.createEvent(creation, ts);
        eventmgr.createEvent(update, ts);

        // Discovering the same lineage from both the new and old discos is expected
        assertEquals(lineageURI, findLineageProgenitor(oldDiscoUri, ts));
        assertEquals(lineageURI, findLineageProgenitor(discoURI, ts));
    }

    @Test
    public void viaDerivationTest() {

        final URI oldDiscoUri = randomURI();

        final ORMapEventCreation creation = new ORMapEventCreation(
                uri2Rdf4jIri(randomURI()),
                new RequestEventDetails(randomURI()), DISCO,
                Arrays.asList(new RMapIri(oldDiscoUri)));
        creation.setEndTime(new Date());
        creation.setLineageProgenitor(new RMapIri(randomURI()));

        final ORMapEventDerivation derivation = new ORMapEventDerivation(
                uri2Rdf4jIri(randomURI()),
                new RequestEventDetails(randomURI()), DISCO,
                uri2Rdf4jIri(oldDiscoUri), uri2Rdf4jIri(discoURI));

        derivation.setEndTime(new Date());
        derivation.setLineageProgenitor(new RMapIri(lineageURI));

        eventmgr.createEvent(creation, ts);
        eventmgr.createEvent(derivation, ts);

        assertEquals(lineageURI, findLineageProgenitor(discoURI, ts));
        assertNotEquals(lineageURI, findLineageProgenitor(oldDiscoUri, ts));
    }

    @Test
    public void viaDerivationOfUpdatedTest() {

        final URI firstDiscoURI = randomURI();
        final URI secondDiscoUri = randomURI();

        final ORMapEventCreation creation = new ORMapEventCreation(
                uri2Rdf4jIri(randomURI()),
                new RequestEventDetails(randomURI()), DISCO,
                asList(new RMapIri(firstDiscoURI)));
        creation.setEndTime(new Date());
        creation.setLineageProgenitor(new RMapIri(firstDiscoURI));

        final ORMapEventUpdate update = new ORMapEventUpdate(
                uri2Rdf4jIri(randomURI()),
                new RequestEventDetails(randomURI()), DISCO,
                uri2Rdf4jIri(firstDiscoURI),
                uri2Rdf4jIri(secondDiscoUri));
        update.setEndTime(new Date());
        update.setLineageProgenitor(new RMapIri(firstDiscoURI));

        final ORMapEventDerivation derivation = new ORMapEventDerivation(
                uri2Rdf4jIri(randomURI()),
                new RequestEventDetails(randomURI()), DISCO,
                uri2Rdf4jIri(secondDiscoUri), uri2Rdf4jIri(discoURI));
        derivation.setEndTime(new Date());
        derivation.setLineageProgenitor(new RMapIri(lineageURI));

        eventmgr.createEvent(creation, ts);
        eventmgr.createEvent(derivation, ts);
        eventmgr.createEvent(update, ts);

        assertEquals(lineageURI, findLineageProgenitor(discoURI, ts));
        assertNotEquals(lineageURI, findLineageProgenitor(firstDiscoURI, ts));
        assertNotEquals(lineageURI, findLineageProgenitor(secondDiscoUri, ts));
        assertEquals(findLineageProgenitor(firstDiscoURI, ts), findLineageProgenitor(secondDiscoUri, ts));
    }

    @Test
    public void lineageMembersStartwithCreationTest() {
        final URI firstDiscoURI = randomURI();
        final URI secondDiscoUri = randomURI();

        final ORMapEventCreation creation = new ORMapEventCreation(
                uri2Rdf4jIri(randomURI()),
                new RequestEventDetails(randomURI()), DISCO,
                asList(new RMapIri(firstDiscoURI)));
        creation.setEndTime(new Date(0));
        creation.setLineageProgenitor(new RMapIri(firstDiscoURI));

        final ORMapEventUpdate update = new ORMapEventUpdate(
                uri2Rdf4jIri(randomURI()),
                new RequestEventDetails(randomURI()), DISCO,
                uri2Rdf4jIri(firstDiscoURI),
                uri2Rdf4jIri(secondDiscoUri));
        update.setEndTime(new Date(1));
        update.setLineageProgenitor(new RMapIri(firstDiscoURI));

        final ORMapEventDerivation derivation = new ORMapEventDerivation(
                uri2Rdf4jIri(randomURI()),
                new RequestEventDetails(randomURI()), DISCO,
                uri2Rdf4jIri(secondDiscoUri), uri2Rdf4jIri(discoURI));
        derivation.setEndTime(new Date(2));
        derivation.setLineageProgenitor(new RMapIri(lineageURI));

        eventmgr.createEvent(creation, ts);
        eventmgr.createEvent(derivation, ts);
        eventmgr.createEvent(update, ts);

        final List<URI> members = getLineageMembers(firstDiscoURI, ts);
        final Map<Date, URI> dates = getLineageMembersWithDates(firstDiscoURI, ts);

        assertTrue(members.containsAll(asList(firstDiscoURI, secondDiscoUri)));
        assertTrue(dates.values().containsAll(members));
        assertEquals(2, members.size());
        assertEquals(2, dates.size());
    }

    @Test
    public void lineageMembersStartwithDerivationTest() {
        final URI firstDiscoURI = randomURI();
        final URI secondDiscoUri = randomURI();

        final ORMapEventDerivation creation = new ORMapEventDerivation(
                uri2Rdf4jIri(randomURI()),
                new RequestEventDetails(randomURI()), DISCO,
                uri2Rdf4jIri(randomURI()), uri2Rdf4jIri(firstDiscoURI));
        creation.setEndTime(new Date(0));
        creation.setLineageProgenitor(new RMapIri(firstDiscoURI));

        final ORMapEventUpdate update = new ORMapEventUpdate(
                uri2Rdf4jIri(randomURI()),
                new RequestEventDetails(randomURI()), DISCO,
                uri2Rdf4jIri(firstDiscoURI),
                uri2Rdf4jIri(secondDiscoUri));
        update.setEndTime(new Date(1));
        update.setLineageProgenitor(new RMapIri(firstDiscoURI));

        final ORMapEventDerivation derivation = new ORMapEventDerivation(
                uri2Rdf4jIri(randomURI()),
                new RequestEventDetails(randomURI()), DISCO,
                uri2Rdf4jIri(secondDiscoUri), uri2Rdf4jIri(discoURI));
        derivation.setEndTime(new Date(2));
        derivation.setLineageProgenitor(new RMapIri(lineageURI));

        eventmgr.createEvent(creation, ts);
        eventmgr.createEvent(derivation, ts);
        eventmgr.createEvent(update, ts);

        final List<URI> members = getLineageMembers(firstDiscoURI, ts);
        final Map<Date, URI> dates = getLineageMembersWithDates(firstDiscoURI, ts);

        assertTrue(members.containsAll(asList(firstDiscoURI, secondDiscoUri)));
        assertTrue(dates.values().containsAll(members));
        assertEquals(2, members.size());
        assertEquals(2, dates.size());
    }

    @Test
    public void findDerivativesTest() {

        // Lineage members
        final URI l1 = randomURI();
        final URI l2 = randomURI();
        final URI l3 = randomURI();

        // Derivatives
        final URI d1 = randomURI();
        final URI d3 = randomURI();

        // Derivative of derivative 1
        final URI dd1 = randomURI();

        // Now the initial lineage chain
        // Start with a derivative, since that's the most challenging
        final ORMapEventDerivation l1c = new ORMapEventDerivation(
                uri2Rdf4jIri(randomURI()),
                new RequestEventDetails(randomURI()), DISCO,
                uri2Rdf4jIri(randomURI()), uri2Rdf4jIri(l1));
        l1c.setEndTime(new Date(0));
        l1c.setLineageProgenitor(new RMapIri(l1));

        final ORMapEventUpdate l2u = new ORMapEventUpdate(
                uri2Rdf4jIri(randomURI()),
                new RequestEventDetails(randomURI()), DISCO,
                uri2Rdf4jIri(l1),
                uri2Rdf4jIri(l2));
        l2u.setEndTime(new Date(1));
        l2u.setLineageProgenitor(new RMapIri(l1));

        final ORMapEventUpdate l3u = new ORMapEventUpdate(
                uri2Rdf4jIri(randomURI()),
                new RequestEventDetails(randomURI()), DISCO,
                uri2Rdf4jIri(l2),
                uri2Rdf4jIri(l3));
        l3u.setEndTime(new Date(2));
        l3u.setLineageProgenitor(new RMapIri(l1));

        // Now create derivatives

        // ... of l1
        final ORMapEventDerivation l1d = new ORMapEventDerivation(
                uri2Rdf4jIri(randomURI()),
                new RequestEventDetails(randomURI()), DISCO,
                uri2Rdf4jIri(l1), uri2Rdf4jIri(d1));
        l1d.setEndTime(new Date(3));
        l1d.setLineageProgenitor(new RMapIri(randomURI()));

        // ... of l2
        final ORMapEventDerivation l3d = new ORMapEventDerivation(
                uri2Rdf4jIri(randomURI()),
                new RequestEventDetails(randomURI()), DISCO,
                uri2Rdf4jIri(l3), uri2Rdf4jIri(d3));
        l3d.setEndTime(new Date(4));
        l3d.setLineageProgenitor(new RMapIri(randomURI()));

        // ... of the derivative of l1 (a derivative of a derivative)
        final ORMapEventDerivation l1dd = new ORMapEventDerivation(
                uri2Rdf4jIri(randomURI()),
                new RequestEventDetails(randomURI()), DISCO,
                uri2Rdf4jIri(dd1), uri2Rdf4jIri(randomURI()));
        l1dd.setEndTime(new Date(5));
        l1dd.setLineageProgenitor(new RMapIri(randomURI()));

        asList(l1c, l2u, l3u, l1d, l3d, l1dd).forEach(e -> eventmgr.createEvent(e, ts));

        final Set<URI> derivatives = findDerivativesfrom(l1, ts);
        assertEquals(2, derivatives.size());
        assertTrue(derivatives.containsAll(asList(d1, d3)));

    }
}


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

namespace OneCog.Io.Onkyo.Console
{
    public class Options
    {
        [Option('h', "host", Required = true, HelpText = "Address of the device to connect to")]
        public string HostName { get; set; }

        [Option('p', "port", Required = false, DefaultValue = 60128, HelpText = "The port on which to connect to the device")]
        public int Port { get; set; }

        [Option('u', "unit", Required = false, DefaultValue = UnitType.Receiver, HelpText = "The type of device to connect to (i.e. Received)")]
        public UnitType UnitType { get; set; }

        public string GetUsage()
        {
            var help = new HelpText
            {
                Heading = new HeadingInfo("OneCog.Io.Onkyo.Console", Assembly.GetExecutingAssembly().ImageRuntimeVersion),
                Copyright = new CopyrightInfo("OneCog Solutions", 2014),
                AdditionalNewLineAfterOption = true,
                AddDashesToOption = true
            };
            help.AddPreOptionsLine("Usage: app -p Someone");
            help.AddOptions(this);
            return help;
        }

    }
}


text:
#include  "Core/Worker/Task.hh"

using namespace fwk;

/**
 *  ATask
 */

ATask::ATask(ATask::Source source):
  _source(source)
{}

ATask::~ATask(void) {}

ATask::Source ATask::getSource(void) const {
  return this->_source;
}

/**
 *  SimpleTask
 */

SimpleTask::SimpleTask(void):
  ATask(ATask::Source::SIMPLE),
  APooled<SimpleTask>(),
  _callback(nullptr),
  _cleanup(nullptr)
{}

SimpleTask::~SimpleTask(void) {
  this->reinit();
}

void  SimpleTask::reinit(void) {
  this->_callback = nullptr;
  this->_cleanup = nullptr;
}

void  SimpleTask::init(const std::function<void (void)>& cb) {
  this->_callback = cb;
  this->_cleanup = nullptr;
}

void  SimpleTask::init(const std::function<void (void)>& cb, const std::function<void (void)>& cl) {
  this->_callback = cb;
  this->_cleanup = cl;
}

/**
 *  EventTask
 */

EventTask::EventTask(void):
  ATask(ATask::Source::EVENT),
  APooled<EventTask>(),
  _key(nullptr),
  _callback(nullptr)
{}

EventTask::~EventTask(void) {
  this->reinit();
}

void  EventTask::reinit(void) {
  this->_key = nullptr;
  this->_callback = nullptr;
}

void  EventTask::init(const void* key, const std::function<void (void)>& callback) {
  this->_key = key;
  this->_callback = callback;
}

/**
 *  HttpTask
 */

HttpTask::HttpTask(void):
  ATask(ATask::Source::HTTP_CALLBACK),
  APooled<HttpTask>(),
  _callback(nullptr),
  _cleanup(nullptr),
  _response(nullptr)
{}

HttpTask::~HttpTask(void) {
  this->reinit();
}

void  HttpTask::reinit(void) {
  this->_callback = nullptr;
  this->_cleanup = nullptr;
  this->_response = nullptr;
}

void  HttpTask::init(const std::function<void (const HttpResponse*)>& cb, const std::function<void (void)>& cl, HttpResponse* resp) {
  this->_callback = cb;
  this->_cleanup = cl;
  this->_response = resp;
}

/**
 *  PeriodicTask
 */

PeriodicTask::PeriodicTask(void):
  ATask(ATask::Source::PERIODIC_TASK),
  APooled<PeriodicTask>(),
  _callback(nullptr),
  _cleanup(nullptr),
  _interval(),
  _off(true)
{}

PeriodicTask::~PeriodicTask(void) {
  this->reinit();
}

void  PeriodicTask::reinit(void) {
  this->_callback = nullptr;
  this->_cleanup = nullptr;
  this->_off = true;
}

void  PeriodicTask::init(const std::function<void(void)>& callback, const std::function<void(void)>& cleanup, const std::chrono::steady_clock::duration& interval) {
  this->_callback = callback;
  this->_cleanup = cleanup;
  this->_interval = interval;
  this->_off = false;
}

void  PeriodicTask::stop() {
  this->_off = true;
}

/**
 *  DelayedTask
 */

DelayedTask::DelayedTask(void):
  APooled<DelayedTask>(),
  _task(nullptr),
  _timePoint()
{}

DelayedTask::~DelayedTask(void) {
  this->reinit();
}

void  DelayedTask::reinit(void) {
  this->_task = nullptr;
}

void  DelayedTask::init(ATask* task, const std::chrono::steady_clock::time_point& timepoint) {
  this->_task = task;
  this->_timePoint = timepoint;
}

void  DelayedTask::init(ATask* task, const std::chrono::steady_clock::duration& interval) {
  this->_task = task;
  this->_timePoint = std::chrono::steady_clock::now() + interval;
}

bool  DelayedTask::operator<(const DelayedTask& oth) const {
  return this->_timePoint < oth._timePoint;
}

bool  DelayedTask::operator>(const DelayedTask& oth) const {
  return this->_timePoint > oth._timePoint;
}

bool  DelayedTask::operator==(const ATask *oth) const {
  return this->_task == oth;
}


text:
package org.pentaho.di.core.osgi.api;

public interface NamedClusterSiteFile {
  String getSiteFileName();

  void setSiteFileName( String siteFileName );

  String getSiteFileContents();

  void setSiteFileContents( String siteFileContents );

  public NamedClusterSiteFile copy();
}


text:
package org.ovirt.engine.ui.webadmin.section.main.presenter.popup.vm;

import org.ovirt.engine.ui.common.presenter.AbstractModelBoundPopupPresenterWidget;
import org.ovirt.engine.ui.uicommonweb.models.vms.ExportVmModel;

import com.google.gwt.event.shared.EventBus;
import com.google.inject.Inject;

public class VmExportPopupPresenterWidget extends AbstractModelBoundPopupPresenterWidget<ExportVmModel, VmExportPopupPresenterWidget.ViewDef> {

    public interface ViewDef extends AbstractModelBoundPopupPresenterWidget.ViewDef<ExportVmModel> {
    }

    @Inject
    public VmExportPopupPresenterWidget(EventBus eventBus, ViewDef view) {
        super(eventBus, view);
    }

}


text:
package tikv

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"sort"
	"strconv"
	"strings"
	"sync"
	"sync/atomic"
	"time"
	"unsafe"

	"github.com/cznic/mathutil"
	"github.com/gogo/protobuf/proto"
	"github.com/pingcap/errors"
	"github.com/pingcap/failpoint"
	"github.com/pingcap/kvproto/pkg/coprocessor"
	"github.com/pingcap/kvproto/pkg/kvrpcpb"
	"github.com/pingcap/parser/terror"
	"github.com/pingcap/tidb/domain/infosync"
	"github.com/pingcap/tidb/errno"
	"github.com/pingcap/tidb/kv"
	"github.com/pingcap/tidb/metrics"
	"github.com/pingcap/tidb/store/tikv/tikvrpc"
	"github.com/pingcap/tidb/util/execdetails"
	"github.com/pingcap/tidb/util/logutil"
	"github.com/pingcap/tidb/util/memory"
	"github.com/pingcap/tipb/go-tipb"
	"go.uber.org/zap"
)

var (
	tikvTxnRegionsNumHistogramWithCoprocessor      = metrics.TiKVTxnRegionsNumHistogram.WithLabelValues("coprocessor")
	tikvTxnRegionsNumHistogramWithBatchCoprocessor = metrics.TiKVTxnRegionsNumHistogram.WithLabelValues("batch_coprocessor")
	coprCacheHistogramEvict                        = metrics.DistSQLCoprCacheHistogram.WithLabelValues("evict")
)

// CopClient is coprocessor client.
type CopClient struct {
	kv.RequestTypeSupportedChecker
	store           *tikvStore
	replicaReadSeed uint32
}

// Send builds the request and gets the coprocessor iterator response.
func (c *CopClient) Send(ctx context.Context, req *kv.Request, vars *kv.Variables, sessionMemTracker *memory.Tracker) kv.Response {
	if req.StoreType == kv.TiFlash && req.BatchCop {
		logutil.BgLogger().Debug("send batch requests")
		return c.sendBatch(ctx, req, vars)
	}
	ctx = context.WithValue(ctx, txnStartKey, req.StartTs)
	bo := NewBackofferWithVars(ctx, copBuildTaskMaxBackoff, vars)
	tasks, err := buildCopTasks(bo, c.store.regionCache, &copRanges{mid: req.KeyRanges}, req)
	if err != nil {
		return copErrorResponse{err}
	}
	it := &copIterator{
		store:           c.store,
		req:             req,
		concurrency:     req.Concurrency,
		finishCh:        make(chan struct{}),
		vars:            vars,
		memTracker:      req.MemTracker,
		replicaReadSeed: c.replicaReadSeed,
		rpcCancel:       NewRPCanceller(),
		maxID:           &maxIDHandler{},
	}
	it.maxID.maxID = 0
	it.minCommitTSPushed.data = make(map[uint64]struct{}, 5)
	it.tasks = tasks
	if it.concurrency > len(tasks) {
		it.concurrency = len(tasks)
	}
	if it.concurrency < 1 {
		// Make sure that there is at least one worker.
		it.concurrency = 1
	}

	if it.req.KeepOrder {
		it.sendRate = newRateLimit(2 * it.concurrency)
	} else {
		it.respChan = make(chan *copResponse, it.concurrency)
		it.sendRate = newRateLimit(it.concurrency)
	}
	it.actionOnExceed = newRateLimitAction(uint(cap(it.sendRate.token)), sync.NewCond(&sync.Mutex{}))
	if sessionMemTracker != nil {
		sessionMemTracker.FallbackOldAndSetNewAction(it.actionOnExceed)
	}

	if !it.req.Streaming {
		ctx = context.WithValue(ctx, RPCCancellerCtxKey{}, it.rpcCancel)
	}
	it.open(ctx)
	return it
}

// copTask contains a related Region and KeyRange for a kv.Request.
type copTask struct {
	id     uint32
	region RegionVerID
	ranges *copRanges

	respChan  chan *copResponse
	storeAddr string
	cmdType   tikvrpc.CmdType
	storeType kv.StoreType
}

func (r *copTask) String() string {
	return fmt.Sprintf("region(%d %d %d) ranges(%d) store(%s)",
		r.region.id, r.region.confVer, r.region.ver, r.ranges.len(), r.storeAddr)
}

// copRanges is like []kv.KeyRange, but may has extra elements at head/tail.
// It's for avoiding alloc big slice during build copTask.
type copRanges struct {
	first *kv.KeyRange
	mid   []kv.KeyRange
	last  *kv.KeyRange
}

func (r *copRanges) String() string {
	var s string
	r.do(func(ran *kv.KeyRange) {
		s += fmt.Sprintf("[%q, %q]", ran.StartKey, ran.EndKey)
	})
	return s
}

func (r *copRanges) len() int {
	var l int
	if r.first != nil {
		l++
	}
	l += len(r.mid)
	if r.last != nil {
		l++
	}
	return l
}

func (r *copRanges) at(i int) kv.KeyRange {
	if r.first != nil {
		if i == 0 {
			return *r.first
		}
		i--
	}
	if i < len(r.mid) {
		return r.mid[i]
	}
	return *r.last
}

func (r *copRanges) slice(from, to int) *copRanges {
	var ran copRanges
	if r.first != nil {
		if from == 0 && to > 0 {
			ran.first = r.first
		}
		if from > 0 {
			from--
		}
		if to > 0 {
			to--
		}
	}
	if to <= len(r.mid) {
		ran.mid = r.mid[from:to]
	} else {
		if from <= len(r.mid) {
			ran.mid = r.mid[from:]
		}
		if from < to {
			ran.last = r.last
		}
	}
	return &ran
}

func (r *copRanges) do(f func(ran *kv.KeyRange)) {
	if r.first != nil {
		f(r.first)
	}
	for _, ran := range r.mid {
		f(&ran)
	}
	if r.last != nil {
		f(r.last)
	}
}

func (r *copRanges) toPBRanges() []*coprocessor.KeyRange {
	ranges := make([]*coprocessor.KeyRange, 0, r.len())
	r.do(func(ran *kv.KeyRange) {
		ranges = append(ranges, &coprocessor.KeyRange{
			Start: ran.StartKey,
			End:   ran.EndKey,
		})
	})
	return ranges
}

// split ranges into (left, right) by key.
func (r *copRanges) split(key []byte) (*copRanges, *copRanges) {
	n := sort.Search(r.len(), func(i int) bool {
		cur := r.at(i)
		return len(cur.EndKey) == 0 || bytes.Compare(cur.EndKey, key) > 0
	})
	// If a range p contains the key, it will split to 2 parts.
	if n < r.len() {
		p := r.at(n)
		if bytes.Compare(key, p.StartKey) > 0 {
			left := r.slice(0, n)
			left.last = &kv.KeyRange{StartKey: p.StartKey, EndKey: key}
			right := r.slice(n+1, r.len())
			right.first = &kv.KeyRange{StartKey: key, EndKey: p.EndKey}
			return left, right
		}
	}
	return r.slice(0, n), r.slice(n, r.len())
}

// rangesPerTask limits the length of the ranges slice sent in one copTask.
const rangesPerTask = 25000

func buildCopTasks(bo *Backoffer, cache *RegionCache, ranges *copRanges, req *kv.Request) ([]*copTask, error) {
	start := time.Now()
	cmdType := tikvrpc.CmdCop
	if req.Streaming {
		cmdType = tikvrpc.CmdCopStream
	}

	if req.StoreType == kv.TiDB {
		return buildTiDBMemCopTasks(ranges, req)
	}

	rangesLen := ranges.len()
	var tasks []*copTask
	appendTask := func(regionWithRangeInfo *KeyLocation, ranges *copRanges) {
		// TiKV will return gRPC error if the message is too large. So we need to limit the length of the ranges slice
		// to make sure the message can be sent successfully.
		rLen := ranges.len()
		for i := 0; i < rLen; {
			nextI := mathutil.Min(i+rangesPerTask, rLen)
			tasks = append(tasks, &copTask{
				region: regionWithRangeInfo.Region,
				ranges: ranges.slice(i, nextI),
				// Channel buffer is 2 for handling region split.
				// In a common case, two region split tasks will not be blocked.
				respChan:  make(chan *copResponse, 2),
				cmdType:   cmdType,
				storeType: req.StoreType,
			})
			i = nextI
		}
	}

	err := splitRanges(bo, cache, ranges, appendTask)
	if err != nil {
		return nil, errors.Trace(err)
	}

	if req.Desc {
		reverseTasks(tasks)
	}
	if elapsed := time.Since(start); elapsed > time.Millisecond*500 {
		logutil.BgLogger().Warn("buildCopTasks takes too much time",
			zap.Duration("elapsed", elapsed),
			zap.Int("range len", rangesLen),
			zap.Int("task len", len(tasks)))
	}
	tikvTxnRegionsNumHistogramWithCoprocessor.Observe(float64(len(tasks)))
	return tasks, nil
}

func buildTiDBMemCopTasks(ranges *copRanges, req *kv.Request) ([]*copTask, error) {
	servers, err := infosync.GetAllServerInfo(context.Background())
	if err != nil {
		return nil, err
	}
	cmdType := tikvrpc.CmdCop
	if req.Streaming {
		cmdType = tikvrpc.CmdCopStream
	}
	tasks := make([]*copTask, 0, len(servers))
	for _, ser := range servers {
		if req.TiDBServerID > 0 && req.TiDBServerID != ser.ServerIDGetter() {
			continue
		}

		addr := ser.IP + ":" + strconv.FormatUint(uint64(ser.StatusPort), 10)
		tasks = append(tasks, &copTask{
			ranges:    ranges,
			respChan:  make(chan *copResponse, 2),
			cmdType:   cmdType,
			storeType: req.StoreType,
			storeAddr: addr,
		})
	}
	return tasks, nil
}

func splitRanges(bo *Backoffer, cache *RegionCache, ranges *copRanges, fn func(regionWithRangeInfo *KeyLocation, ranges *copRanges)) error {
	for ranges.len() > 0 {
		loc, err := cache.LocateKey(bo, ranges.at(0).StartKey)
		if err != nil {
			return errors.Trace(err)
		}

		// Iterate to the first range that is not complete in the region.
		var i int
		for ; i < ranges.len(); i++ {
			r := ranges.at(i)
			if !(loc.Contains(r.EndKey) || bytes.Equal(loc.EndKey, r.EndKey)) {
				break
			}
		}
		// All rest ranges belong to the same region.
		if i == ranges.len() {
			fn(loc, ranges)
			break
		}

		r := ranges.at(i)
		if loc.Contains(r.StartKey) {
			// Part of r is not in the region. We need to split it.
			taskRanges := ranges.slice(0, i)
			taskRanges.last = &kv.KeyRange{
				StartKey: r.StartKey,
				EndKey:   loc.EndKey,
			}
			fn(loc, taskRanges)

			ranges = ranges.slice(i+1, ranges.len())
			ranges.first = &kv.KeyRange{
				StartKey: loc.EndKey,
				EndKey:   r.EndKey,
			}
		} else {
			// rs[i] is not in the region.
			taskRanges := ranges.slice(0, i)
			fn(loc, taskRanges)
			ranges = ranges.slice(i, ranges.len())
		}
	}

	return nil
}

// SplitRegionRanges get the split ranges from pd region.
func SplitRegionRanges(bo *Backoffer, cache *RegionCache, keyRanges []kv.KeyRange) ([]kv.KeyRange, error) {
	ranges := copRanges{mid: keyRanges}

	var ret []kv.KeyRange
	appendRange := func(regionWithRangeInfo *KeyLocation, ranges *copRanges) {
		for i := 0; i < ranges.len(); i++ {
			ret = append(ret, ranges.at(i))
		}
	}

	err := splitRanges(bo, cache, &ranges, appendRange)
	if err != nil {
		return nil, errors.Trace(err)
	}
	return ret, nil
}

func reverseTasks(tasks []*copTask) {
	for i := 0; i < len(tasks)/2; i++ {
		j := len(tasks) - i - 1
		tasks[i], tasks[j] = tasks[j], tasks[i]
	}
}

type copIterator struct {
	store       *tikvStore
	req         *kv.Request
	concurrency int
	finishCh    chan struct{}

	// If keepOrder, results are stored in copTask.respChan, read them out one by one.
	tasks []*copTask
	// curr indicates the curr id of the finished copTask
	curr int

	// maxID indicates the max id of the running copTask
	maxID *maxIDHandler

	// sendRate controls the sending rate of copIteratorTaskSender
	sendRate *rateLimit

	// Otherwise, results are stored in respChan.
	respChan chan *copResponse

	vars *kv.Variables

	memTracker *memory.Tracker

	replicaReadSeed uint32

	rpcCancel *RPCCanceller

	wg sync.WaitGroup
	// closed represents when the Close is called.
	// There are two cases we need to close the `finishCh` channel, one is when context is done, the other one is
	// when the Close is called. we use atomic.CompareAndSwap `closed` to to make sure the channel is not closed twice.
	closed uint32

	minCommitTSPushed

	actionOnExceed *rateLimitAction
}

// copIteratorWorker receives tasks from copIteratorTaskSender, handles tasks and sends the copResponse to respChan.
type copIteratorWorker struct {
	taskCh   <-chan *copTask
	wg       *sync.WaitGroup
	store    *tikvStore
	req      *kv.Request
	respChan chan<- *copResponse
	finishCh <-chan struct{}
	vars     *kv.Variables
	clientHelper

	memTracker *memory.Tracker

	replicaReadSeed uint32

	sendRate *rateLimit

	actionOnExceed *rateLimitAction

	maxID *maxIDHandler
}

// copIteratorTaskSender sends tasks to taskCh then wait for the workers to exit.
type copIteratorTaskSender struct {
	taskCh   chan<- *copTask
	wg       *sync.WaitGroup
	tasks    []*copTask
	finishCh <-chan struct{}
	respChan chan<- *copResponse
	sendRate *rateLimit
}

type copResponse struct {
	pbResp   *coprocessor.Response
	detail   *CopRuntimeStats
	startKey kv.Key
	err      error
	respSize int64
	respTime time.Duration
}

const (
	sizeofExecDetails   = int(unsafe.Sizeof(execdetails.ExecDetails{}))
	sizeofCommitDetails = int(unsafe.Sizeof(execdetails.CommitDetails{}))
)

// GetData implements the kv.ResultSubset GetData interface.
func (rs *copResponse) GetData() []byte {
	return rs.pbResp.Data
}

// GetStartKey implements the kv.ResultSubset GetStartKey interface.
func (rs *copResponse) GetStartKey() kv.Key {
	return rs.startKey
}

func (rs *copResponse) GetCopRuntimeStats() *CopRuntimeStats {
	return rs.detail
}

// MemSize returns how many bytes of memory this response use
func (rs *copResponse) MemSize() int64 {
	if rs.respSize != 0 {
		return rs.respSize
	}

	// ignore rs.err
	rs.respSize += int64(cap(rs.startKey))
	if rs.detail != nil {
		rs.respSize += int64(sizeofExecDetails)
	}
	if rs.pbResp != nil {
		// Using a approximate size since it's hard to get a accurate value.
		rs.respSize += int64(rs.pbResp.Size())
	}
	return rs.respSize
}

func (rs *copResponse) RespTime() time.Duration {
	return rs.respTime
}

const minLogCopTaskTime = 300 * time.Millisecond

// run is a worker function that get a copTask from channel, handle it and
// send the result back.
func (worker *copIteratorWorker) run(ctx context.Context) {
	defer func() {
		worker.wg.Done()
		failpoint.Inject("testRateLimitActionMockWaitMax", func(val failpoint.Value) {
			if val.(bool) {
				// we need to prevent action from being closed before triggering action yet
				for worker.actionOnExceed.isEnabled() {
					time.Sleep(10 * time.Millisecond)
				}
			}
		})
		worker.actionOnExceed.close()
	}()
	for task := range worker.taskCh {
		respCh := worker.respChan
		if respCh == nil {
			respCh = task.respChan
		}
		worker.handleTask(ctx, task, respCh)
		failpoint.Inject("testRateLimitActionMockOtherExecutorConsume", func(val failpoint.Value) {
			if val.(bool) {
				// wait action being enabled and response channel become empty
				time.Sleep(20 * time.Millisecond)
				// simulate other executor consume and trigger oom action
				worker.memTracker.Consume(99999)
			}
		})
		close(task.respChan)
		worker.maxID.setMaxIDIfLarger(task.id)
		worker.actionOnExceed.destroyTokenIfNeeded(func() {
			worker.sendRate.putToken()
		})
		if worker.vars != nil && worker.vars.Killed != nil && atomic.LoadUint32(worker.vars.Killed) == 1 {
			return
		}
		select {
		case <-worker.finishCh:
			return
		default:
		}
	}
}

// open starts workers and sender goroutines.
func (it *copIterator) open(ctx context.Context) {
	taskCh := make(chan *copTask, 1)
	it.wg.Add(it.concurrency)
	// Start it.concurrency number of workers to handle cop requests.
	for i := 0; i < it.concurrency; i++ {
		worker := &copIteratorWorker{
			taskCh:   taskCh,
			wg:       &it.wg,
			store:    it.store,
			req:      it.req,
			respChan: it.respChan,
			finishCh: it.finishCh,
			vars:     it.vars,
			clientHelper: clientHelper{
				LockResolver:      it.store.lockResolver,
				RegionCache:       it.store.regionCache,
				minCommitTSPushed: &it.minCommitTSPushed,
				Client:            it.store.client,
			},

			memTracker: it.memTracker,

			replicaReadSeed: it.replicaReadSeed,
			sendRate:        it.sendRate,
			actionOnExceed:  it.actionOnExceed,
			maxID:           it.maxID,
		}
		go worker.run(ctx)
	}
	taskSender := &copIteratorTaskSender{
		taskCh:   taskCh,
		wg:       &it.wg,
		tasks:    it.tasks,
		finishCh: it.finishCh,
		sendRate: it.sendRate,
	}
	taskSender.respChan = it.respChan
	it.actionOnExceed.setEnabled(true)
	go taskSender.run()
}

func (sender *copIteratorTaskSender) run() {
	// Send tasks to feed the worker goroutines.
	for i, t := range sender.tasks {
		// we control the sending rate to prevent all tasks
		// being done (aka. all of the responses are buffered) by copIteratorWorker.
		// We keep the number of inflight tasks within the number of 2 * concurrency when Keep Order is true.
		// If KeepOrder is false, the number equals the concurrency.
		// It sends one more task if a task has been finished in copIterator.Next.
		exit := sender.sendRate.getToken(sender.finishCh)
		if exit {
			break
		}
		t.id = uint32(i)
		exit = sender.sendToTaskCh(t)
		if exit {
			break
		}
	}
	close(sender.taskCh)

	// Wait for worker goroutines to exit.
	sender.wg.Wait()
	if sender.respChan != nil {
		close(sender.respChan)
	}
}

func (it *copIterator) recvFromRespCh(ctx context.Context, respCh <-chan *copResponse) (resp *copResponse, ok bool, exit bool) {
	ticker := time.NewTicker(3 * time.Second)
	defer ticker.Stop()
	for {
		select {
		case resp, ok = <-respCh:
			if it.memTracker != nil && resp != nil {
				consumed := resp.MemSize()
				failpoint.Inject("testRateLimitActionMockConsume", func(val failpoint.Value) {
					if val.(bool) {
						consumed = 100
					}
				})
				it.memTracker.Consume(-consumed)
			}
			return
		case <-it.finishCh:
			exit = true
			return
		case <-ticker.C:
			if atomic.LoadUint32(it.vars.Killed) == 1 {
				resp = &copResponse{err: ErrQueryInterrupted}
				ok = true
				return
			}
		case <-ctx.Done():
			// We select the ctx.Done() in the thread of `Next` instead of in the worker to avoid the cost of `WithCancel`.
			if atomic.CompareAndSwapUint32(&it.closed, 0, 1) {
				close(it.finishCh)
			}
			exit = true
			return
		}
	}
}

func (sender *copIteratorTaskSender) sendToTaskCh(t *copTask) (exit bool) {
	select {
	case sender.taskCh <- t:
	case <-sender.finishCh:
		exit = true
	}
	return
}

func (worker *copIteratorWorker) sendToRespCh(resp *copResponse, respCh chan<- *copResponse, checkOOM bool) (exit bool) {
	if worker.memTracker != nil && checkOOM {
		consumed := resp.MemSize()
		failpoint.Inject("testRateLimitActionMockConsume", func(val failpoint.Value) {
			if val.(bool) {
				consumed = 100
			}
		})
		worker.memTracker.Consume(consumed)
	}
	select {
	case respCh <- resp:
	case <-worker.finishCh:
		exit = true
	}
	return
}

// Next returns next coprocessor result.
// NOTE: Use nil to indicate finish, so if the returned ResultSubset is not nil, reader should continue to call Next().
func (it *copIterator) Next(ctx context.Context) (kv.ResultSubset, error) {
	var (
		resp   *copResponse
		ok     bool
		closed bool
	)
	// wait unit at least 5 copResponse received.
	failpoint.Inject("testRateLimitActionMockWaitMax", func(val failpoint.Value) {
		if val.(bool) {
			// we only need to trigger oom at least once.
			if len(it.tasks) > 9 {
				for it.memTracker.MaxConsumed() < 500 {
					time.Sleep(10 * time.Millisecond)
				}
			}
		}
	})

	// If data order matters, response should be returned in the same order as copTask slice.
	// Otherwise all responses are returned from a single channel.
	if it.respChan != nil {
		// Get next fetched resp from chan
		resp, ok, closed = it.recvFromRespCh(ctx, it.respChan)
		if !ok || closed {
			it.actionOnExceed.close()
			return nil, nil
		}
		// The respCh has been drained out
		it.actionOnExceed.broadcastIfNeeded(len(it.respChan) < 1)
	} else {
		for {
			if it.curr >= len(it.tasks) {
				// Resp will be nil if iterator is finishCh.
				it.actionOnExceed.close()
				return nil, nil
			}
			task := it.tasks[it.curr]
			resp, ok, closed = it.recvFromRespCh(ctx, task.respChan)
			if closed {
				// Close() is already called, so Next() is invalid.
				return nil, nil
			}
			if ok {
				break
			}
			finishedTaskID := it.tasks[it.curr].id
			// Switch to next task.
			it.tasks[it.curr] = nil
			it.curr++
			maxID := it.maxID.getMaxID()
			// The tasks whose id is less than maxID are assumed that being sending to their task channel.
			// So the response channel would be thought as drained out if the current taskID is greater or equal than
			// the maxID as all the workers are being suspended at that time.
			it.actionOnExceed.broadcastIfNeeded(finishedTaskID >= maxID)
		}
	}

	if resp.err != nil {
		return nil, errors.Trace(resp.err)
	}

	err := it.store.CheckVisibility(it.req.StartTs)
	if err != nil {
		return nil, errors.Trace(err)
	}
	return resp, nil
}

// Associate each region with an independent backoffer. In this way, when multiple regions are
// unavailable, TiDB can execute very quickly without blocking
func chooseBackoffer(ctx context.Context, backoffermap map[uint64]*Backoffer, task *copTask, worker *copIteratorWorker) *Backoffer {
	bo, ok := backoffermap[task.region.id]
	if ok {
		return bo
	}
	newbo := NewBackofferWithVars(ctx, copNextMaxBackoff, worker.vars)
	backoffermap[task.region.id] = newbo
	return newbo
}

// handleTask handles single copTask, sends the result to channel, retry automatically on error.
func (worker *copIteratorWorker) handleTask(ctx context.Context, task *copTask, respCh chan<- *copResponse) {
	defer func() {
		r := recover()
		if r != nil {
			logutil.BgLogger().Error("copIteratorWork meet panic",
				zap.Reflect("r", r),
				zap.Stack("stack trace"))
			resp := &copResponse{err: errors.Errorf("%v", r)}
			// if panic has happened, set checkOOM to false to avoid another panic.
			worker.sendToRespCh(resp, respCh, false)
		}
	}()
	remainTasks := []*copTask{task}
	backoffermap := make(map[uint64]*Backoffer)
	for len(remainTasks) > 0 {
		curTask := remainTasks[0]
		bo := chooseBackoffer(ctx, backoffermap, curTask, worker)
		tasks, err := worker.handleTaskOnce(bo, curTask, respCh)
		if err != nil {
			resp := &copResponse{err: errors.Trace(err)}
			worker.sendToRespCh(resp, respCh, true)
			return
		}
		// test whether the ctx is cancelled
		if bo.vars != nil && bo.vars.Killed != nil && atomic.LoadUint32(bo.vars.Killed) == 1 {
			return
		}

		if len(tasks) > 0 {
			remainTasks = append(tasks, remainTasks[1:]...)
		} else {
			remainTasks = remainTasks[1:]
		}
	}
	if worker.store.coprCache != nil && worker.store.coprCache.cache.Metrics != nil {
		coprCacheHistogramEvict.Observe(float64(worker.store.coprCache.cache.Metrics.KeysEvicted()))
	}
}

// handleTaskOnce handles single copTask, successful results are send to channel.
// If error happened, returns error. If region split or meet lock, returns the remain tasks.
func (worker *copIteratorWorker) handleTaskOnce(bo *Backoffer, task *copTask, ch chan<- *copResponse) ([]*copTask, error) {
	failpoint.Inject("handleTaskOnceError", func(val failpoint.Value) {
		if val.(bool) {
			failpoint.Return(nil, errors.New("mock handleTaskOnce error"))
		}
	})

	copReq := coprocessor.Request{
		Tp:        worker.req.Tp,
		StartTs:   worker.req.StartTs,
		Data:      worker.req.Data,
		Ranges:    task.ranges.toPBRanges(),
		SchemaVer: worker.req.SchemaVar,
	}

	var cacheKey []byte = nil
	var cacheValue *coprCacheValue = nil

	// If there are many ranges, it is very likely to be a TableLookupRequest. They are not worth to cache since
	// computing is not the main cost. Ignore such requests directly to avoid slowly building the cache key.
	if task.cmdType == tikvrpc.CmdCop && worker.store.coprCache != nil && worker.req.Cacheable && len(copReq.Ranges) < 10 {
		cKey, err := coprCacheBuildKey(&copReq)
		if err == nil {
			cacheKey = cKey
			cValue := worker.store.coprCache.Get(cKey)
			copReq.IsCacheEnabled = true
			if cValue != nil && cValue.RegionID == task.region.id && cValue.TimeStamp <= worker.req.StartTs {
				// Append cache version to the request to skip Coprocessor computation if possible
				// when request result is cached
				copReq.CacheIfMatchVersion = cValue.RegionDataVersion
				cacheValue = cValue
			} else {
				copReq.CacheIfMatchVersion = 0
			}
		} else {
			logutil.BgLogger().Warn("Failed to build copr cache key", zap.Error(err))
		}
	}

	req := tikvrpc.NewReplicaReadRequest(task.cmdType, &copReq, worker.req.ReplicaRead, &worker.replicaReadSeed, kvrpcpb.Context{
		IsolationLevel: pbIsolationLevel(worker.req.IsolationLevel),
		Priority:       kvPriorityToCommandPri(worker.req.Priority),
		NotFillCache:   worker.req.NotFillCache,
		HandleTime:     true,
		ScanDetail:     true,
		TaskId:         worker.req.TaskID,
	})
	req.StoreTp = task.storeType
	startTime := time.Now()
	if worker.Stats == nil {
		worker.Stats = make(map[tikvrpc.CmdType]*RPCRuntimeStats)
	}
	resp, rpcCtx, storeAddr, err := worker.SendReqCtx(bo, req, task.region, ReadTimeoutMedium, task.storeType, task.storeAddr)
	if err != nil {
		if task.storeType == kv.TiDB {
			err = worker.handleTiDBSendReqErr(err, task, ch)
			return nil, err
		}
		return nil, errors.Trace(err)
	}

	// Set task.storeAddr field so its task.String() method have the store address information.
	task.storeAddr = storeAddr
	costTime := time.Since(startTime)
	if costTime > minLogCopTaskTime {
		worker.logTimeCopTask(costTime, task, bo, resp)
	}
	metrics.TiKVCoprocessorHistogram.Observe(costTime.Seconds())

	if task.cmdType == tikvrpc.CmdCopStream {
		return worker.handleCopStreamResult(bo, rpcCtx, resp.Resp.(*tikvrpc.CopStreamResponse), task, ch, costTime)
	}

	// Handles the response for non-streaming copTask.
	return worker.handleCopResponse(bo, rpcCtx, &copResponse{pbResp: resp.Resp.(*coprocessor.Response)}, cacheKey, cacheValue, task, ch, nil, costTime)
}

type minCommitTSPushed struct {
	data map[uint64]struct{}
	sync.RWMutex
}

func (m *minCommitTSPushed) Update(from []uint64) {
	m.Lock()
	for _, v := range from {
		m.data[v] = struct{}{}
	}
	m.Unlock()
}

func (m *minCommitTSPushed) Get() []uint64 {
	m.RLock()
	defer m.RUnlock()
	if len(m.data) == 0 {
		return nil
	}

	ret := make([]uint64, 0, len(m.data))
	for k := range m.data {
		ret = append(ret, k)
	}
	return ret
}

// clientHelper wraps LockResolver and RegionRequestSender.
// It's introduced to support the new lock resolving pattern in the large transaction.
// In the large transaction protocol, sending requests and resolving locks are
// context-dependent. For example, when a send request meets a secondary lock, we'll
// call ResolveLock, and if the lock belongs to a large transaction, we may retry
// the request. If there is no context information about the resolved locks, we'll
// meet the secondary lock again and run into a deadloop.
type clientHelper struct {
	*LockResolver
	*RegionCache
	*minCommitTSPushed
	Client
	resolveLite bool
	RegionRequestRuntimeStats
}

// ResolveLocks wraps the ResolveLocks function and store the resolved result.
func (ch *clientHelper) ResolveLocks(bo *Backoffer, callerStartTS uint64, locks []*Lock) (int64, error) {
	var err error
	var resolvedLocks []uint64
	var msBeforeTxnExpired int64
	if ch.Stats != nil {
		defer func(start time.Time) {
			recordRegionRequestRuntimeStats(ch.Stats, tikvrpc.CmdResolveLock, time.Since(start))
		}(time.Now())
	}
	if ch.resolveLite {
		msBeforeTxnExpired, resolvedLocks, err = ch.LockResolver.resolveLocksLite(bo, callerStartTS, locks)
	} else {
		msBeforeTxnExpired, resolvedLocks, err = ch.LockResolver.ResolveLocks(bo, callerStartTS, locks)
	}
	if err != nil {
		return msBeforeTxnExpired, err
	}
	if len(resolvedLocks) > 0 {
		ch.minCommitTSPushed.Update(resolvedLocks)
		return 0, nil
	}
	return msBeforeTxnExpired, nil
}

// SendReqCtx wraps the SendReqCtx function and use the resolved lock result in the kvrpcpb.Context.
func (ch *clientHelper) SendReqCtx(bo *Backoffer, req *tikvrpc.Request, regionID RegionVerID, timeout time.Duration, sType kv.StoreType, directStoreAddr string) (*tikvrpc.Response, *RPCContext, string, error) {
	sender := NewRegionRequestSender(ch.RegionCache, ch.Client)
	if len(directStoreAddr) > 0 {
		sender.storeAddr = directStoreAddr
	}
	sender.Stats = ch.Stats
	req.Context.ResolvedLocks = ch.minCommitTSPushed.Get()
	resp, ctx, err := sender.SendReqCtx(bo, req, regionID, timeout, sType)
	return resp, ctx, sender.storeAddr, err
}

const (
	minLogBackoffTime   = 100
	minLogKVProcessTime = 100
	minLogKVWaitTime    = 200
)

func (worker *copIteratorWorker) logTimeCopTask(costTime time.Duration, task *copTask, bo *Backoffer, resp *tikvrpc.Response) {
	logStr := fmt.Sprintf("[TIME_COP_PROCESS] resp_time:%s txnStartTS:%d region_id:%d store_addr:%s", costTime, worker.req.StartTs, task.region.id, task.storeAddr)
	if bo.totalSleep > minLogBackoffTime {
		backoffTypes := strings.Replace(fmt.Sprintf("%v", bo.types), " ", ",", -1)
		logStr += fmt.Sprintf(" backoff_ms:%d backoff_types:%s", bo.totalSleep, backoffTypes)
	}
	var detail *kvrpcpb.ExecDetails
	if resp.Resp != nil {
		switch r := resp.Resp.(type) {
		case *coprocessor.Response:
			detail = r.ExecDetails
		case *tikvrpc.CopStreamResponse:
			// streaming request returns io.EOF, so the first CopStreamResponse.Response maybe nil.
			if r.Response != nil {
				detail = r.Response.ExecDetails
			}
		default:
			panic("unreachable")
		}
	}

	if detail != nil && detail.HandleTime != nil {
		processMs := detail.HandleTime.ProcessMs
		waitMs := detail.HandleTime.WaitMs
		if processMs > minLogKVProcessTime {
			logStr += fmt.Sprintf(" kv_process_ms:%d", processMs)
			if detail.ScanDetail != nil {
				logStr = appendScanDetail(logStr, "write", detail.ScanDetail.Write)
				logStr = appendScanDetail(logStr, "data", detail.ScanDetail.Data)
				logStr = appendScanDetail(logStr, "lock", detail.ScanDetail.Lock)
			}
			if detail.ScanDetailV2 != nil {
				logStr += fmt.Sprintf(" processed versions: %d", detail.ScanDetailV2.ProcessedVersions)
				logStr += fmt.Sprintf(" total versions: %d", detail.ScanDetailV2.TotalVersions)
				logStr += fmt.Sprintf(" delete skipped count: %d", detail.ScanDetailV2.RocksdbDeleteSkippedCount)
				logStr += fmt.Sprintf(" key skipped count: %d", detail.ScanDetailV2.RocksdbKeySkippedCount)
				logStr += fmt.Sprintf(" cache hit count: %d", detail.ScanDetailV2.RocksdbBlockCacheHitCount)
				logStr += fmt.Sprintf(" read count: %d", detail.ScanDetailV2.RocksdbBlockReadCount)
				logStr += fmt.Sprintf(" read byte: %d", detail.ScanDetailV2.RocksdbBlockReadByte)
			}
		}
		if waitMs > minLogKVWaitTime {
			logStr += fmt.Sprintf(" kv_wait_ms:%d", waitMs)
			if processMs <= minLogKVProcessTime {
				logStr = strings.Replace(logStr, "TIME_COP_PROCESS", "TIME_COP_WAIT", 1)
			}
		}
	}
	logutil.Logger(bo.ctx).Info(logStr)
}

func appendScanDetail(logStr string, columnFamily string, scanInfo *kvrpcpb.ScanInfo) string {
	if scanInfo != nil {
		logStr += fmt.Sprintf(" scan_total_%s:%d", columnFamily, scanInfo.Total)
		logStr += fmt.Sprintf(" scan_processed_%s:%d", columnFamily, scanInfo.Processed)
	}
	return logStr
}

func (worker *copIteratorWorker) handleCopStreamResult(bo *Backoffer, rpcCtx *RPCContext, stream *tikvrpc.CopStreamResponse, task *copTask, ch chan<- *copResponse, costTime time.Duration) ([]*copTask, error) {
	defer stream.Close()
	var resp *coprocessor.Response
	var lastRange *coprocessor.KeyRange
	resp = stream.Response
	if resp == nil {
		// streaming request returns io.EOF, so the first Response is nil.
		return nil, nil
	}
	for {
		remainedTasks, err := worker.handleCopResponse(bo, rpcCtx, &copResponse{pbResp: resp}, nil, nil, task, ch, lastRange, costTime)
		if err != nil || len(remainedTasks) != 0 {
			return remainedTasks, errors.Trace(err)
		}
		resp, err = stream.Recv()
		if err != nil {
			if errors.Cause(err) == io.EOF {
				return nil, nil
			}

			if err1 := bo.Backoff(boTiKVRPC, errors.Errorf("recv stream response error: %v, task: %s", err, task)); err1 != nil {
				return nil, errors.Trace(err)
			}

			// No coprocessor.Response for network error, rebuild task based on the last success one.
			if errors.Cause(err) == context.Canceled {
				logutil.BgLogger().Info("stream recv timeout", zap.Error(err))
			} else {
				logutil.BgLogger().Info("stream unknown error", zap.Error(err))
			}
			return worker.buildCopTasksFromRemain(bo, lastRange, task)
		}
		if resp.Range != nil {
			lastRange = resp.Range
		}
	}
}

// handleCopResponse checks coprocessor Response for region split and lock,
// returns more tasks when that happens, or handles the response if no error.
// if we're handling streaming coprocessor response, lastRange is the range of last
// successful response, otherwise it's nil.
func (worker *copIteratorWorker) handleCopResponse(bo *Backoffer, rpcCtx *RPCContext, resp *copResponse, cacheKey []byte, cacheValue *coprCacheValue, task *copTask, ch chan<- *copResponse, lastRange *coprocessor.KeyRange, costTime time.Duration) ([]*copTask, error) {
	if regionErr := resp.pbResp.GetRegionError(); regionErr != nil {
		if rpcCtx != nil && task.storeType == kv.TiDB {
			resp.err = errors.Errorf("error: %v", regionErr)
			worker.sendToRespCh(resp, ch, true)
			return nil, nil
		}
		errStr := fmt.Sprintf("region_id:%v, region_ver:%v, store_type:%s, peer_addr:%s, error:%s",
			task.region.id, task.region.ver, task.storeType.Name(), task.storeAddr, regionErr.String())
		if err := bo.Backoff(BoRegionMiss, errors.New(errStr)); err != nil {
			return nil, errors.Trace(err)
		}
		// We may meet RegionError at the first packet, but not during visiting the stream.
		return buildCopTasks(bo, worker.store.regionCache, task.ranges, worker.req)
	}
	if lockErr := resp.pbResp.GetLocked(); lockErr != nil {
		logutil.BgLogger().Debug("coprocessor encounters",
			zap.Stringer("lock", lockErr))
		msBeforeExpired, err1 := worker.ResolveLocks(bo, worker.req.StartTs, []*Lock{NewLock(lockErr)})
		if err1 != nil {
			return nil, errors.Trace(err1)
		}
		if msBeforeExpired > 0 {
			if err := bo.BackoffWithMaxSleep(boTxnLockFast, int(msBeforeExpired), errors.New(lockErr.String())); err != nil {
				return nil, errors.Trace(err)
			}
		}
		return worker.buildCopTasksFromRemain(bo, lastRange, task)
	}
	if otherErr := resp.pbResp.GetOtherError(); otherErr != "" {
		err := errors.Errorf("other error: %s", otherErr)
		logutil.BgLogger().Warn("other error",
			zap.Uint64("txnStartTS", worker.req.StartTs),
			zap.Uint64("regionID", task.region.id),
			zap.String("storeAddr", task.storeAddr),
			zap.Error(err))
		return nil, errors.Trace(err)
	}
	// When the request is using streaming API, the `Range` is not nil.
	if resp.pbResp.Range != nil {
		resp.startKey = resp.pbResp.Range.Start
	} else if task.ranges != nil && task.ranges.len() > 0 {
		resp.startKey = task.ranges.at(0).StartKey
	}
	if resp.detail == nil {
		resp.detail = new(CopRuntimeStats)
	}
	resp.detail.Stats = worker.Stats
	worker.Stats = nil
	resp.detail.BackoffTime = time.Duration(bo.totalSleep) * time.Millisecond
	resp.detail.BackoffSleep = make(map[string]time.Duration, len(bo.backoffTimes))
	resp.detail.BackoffTimes = make(map[string]int, len(bo.backoffTimes))
	for backoff := range bo.backoffTimes {
		backoffName := backoff.String()
		resp.detail.BackoffTimes[backoffName] = bo.backoffTimes[backoff]
		resp.detail.BackoffSleep[backoffName] = time.Duration(bo.backoffSleepMS[backoff]) * time.Millisecond
	}
	if rpcCtx != nil {
		resp.detail.CalleeAddress = rpcCtx.Addr
	}
	resp.respTime = costTime
	if pbDetails := resp.pbResp.ExecDetails; pbDetails != nil {
		if handleTime := pbDetails.HandleTime; handleTime != nil {
			resp.detail.WaitTime = time.Duration(handleTime.WaitMs) * time.Millisecond
			resp.detail.ProcessTime = time.Duration(handleTime.ProcessMs) * time.Millisecond
		}
		if scanDetailV2 := pbDetails.ScanDetailV2; scanDetailV2 != nil {
			copDetail := &execdetails.CopDetails{
				ProcessedKeys:             int64(scanDetailV2.ProcessedVersions),
				TotalKeys:                 int64(scanDetailV2.TotalVersions),
				RocksdbDeleteSkippedCount: scanDetailV2.RocksdbDeleteSkippedCount,
				RocksdbKeySkippedCount:    scanDetailV2.RocksdbKeySkippedCount,
				RocksdbBlockCacheHitCount: scanDetailV2.RocksdbBlockCacheHitCount,
				RocksdbBlockReadCount:     scanDetailV2.RocksdbBlockReadCount,
				RocksdbBlockReadByte:      scanDetailV2.RocksdbBlockReadByte,
			}
			resp.detail.CopDetail = copDetail
		} else if scanDetail := pbDetails.ScanDetail; scanDetail != nil {
			if scanDetail.Write != nil {
				resp.detail.CopDetail = &execdetails.CopDetails{
					ProcessedKeys: scanDetail.Write.Processed,
					TotalKeys:     scanDetail.Write.Total,
				}
			}
		}
	}
	if resp.pbResp.IsCacheHit {
		if cacheValue == nil {
			return nil, errors.New("Internal error: received illegal TiKV response")
		}
		// Cache hit and is valid: use cached data as response data and we don't update the cache.
		data := make([]byte, len(cacheValue.Data))
		copy(data, cacheValue.Data)
		resp.pbResp.Data = data
		resp.detail.CoprCacheHit = true
	} else {
		// Cache not hit or cache hit but not valid: update the cache if the response can be cached.
		if cacheKey != nil && resp.pbResp.CanBeCached && resp.pbResp.CacheLastVersion > 0 {
			if worker.store.coprCache.CheckAdmission(resp.pbResp.Data.Size(), resp.detail.ProcessTime) {
				data := make([]byte, len(resp.pbResp.Data))
				copy(data, resp.pbResp.Data)

				newCacheValue := coprCacheValue{
					Data:              data,
					TimeStamp:         worker.req.StartTs,
					RegionID:          task.region.id,
					RegionDataVersion: resp.pbResp.CacheLastVersion,
				}
				worker.store.coprCache.Set(cacheKey, &newCacheValue)
			}
		}
	}
	worker.sendToRespCh(resp, ch, true)
	return nil, nil
}

// CopRuntimeStats contains execution detail information.
type CopRuntimeStats struct {
	execdetails.ExecDetails
	RegionRequestRuntimeStats

	CoprCacheHit bool
}

func (worker *copIteratorWorker) handleTiDBSendReqErr(err error, task *copTask, ch chan<- *copResponse) error {
	errCode := errno.ErrUnknown
	errMsg := err.Error()
	if terror.ErrorEqual(err, ErrTiKVServerTimeout) {
		errCode = errno.ErrTiKVServerTimeout
		errMsg = "TiDB server timeout, address is " + task.storeAddr
	}
	selResp := tipb.SelectResponse{
		Warnings: []*tipb.Error{
			{
				Code: int32(errCode),
				Msg:  errMsg,
			},
		},
	}
	data, err := proto.Marshal(&selResp)
	if err != nil {
		return errors.Trace(err)
	}
	resp := &copResponse{
		pbResp: &coprocessor.Response{
			Data: data,
		},
		detail: &CopRuntimeStats{},
	}
	worker.sendToRespCh(resp, ch, true)
	return nil
}

func (worker *copIteratorWorker) buildCopTasksFromRemain(bo *Backoffer, lastRange *coprocessor.KeyRange, task *copTask) ([]*copTask, error) {
	remainedRanges := task.ranges
	if worker.req.Streaming && lastRange != nil {
		remainedRanges = worker.calculateRemain(task.ranges, lastRange, worker.req.Desc)
	}
	return buildCopTasks(bo, worker.store.regionCache, remainedRanges, worker.req)
}

// calculateRemain splits the input ranges into two, and take one of them according to desc flag.
// It's used in streaming API, to calculate which range is consumed and what needs to be retry.
// For example:
// ranges: [r1 --> r2) [r3 --> r4)
// split:      [s1   -->   s2)
// In normal scan order, all data before s1 is consumed, so the remain ranges should be [s1 --> r2) [r3 --> r4)
// In reverse scan order, all data after s2 is consumed, so the remain ranges should be [r1 --> r2) [r3 --> s2)
func (worker *copIteratorWorker) calculateRemain(ranges *copRanges, split *coprocessor.KeyRange, desc bool) *copRanges {
	if desc {
		left, _ := ranges.split(split.End)
		return left
	}
	_, right := ranges.split(split.Start)
	return right
}

func (it *copIterator) Close() error {
	if atomic.CompareAndSwapUint32(&it.closed, 0, 1) {
		close(it.finishCh)
	}
	it.rpcCancel.CancelAll()
	it.actionOnExceed.close()
	it.wg.Wait()
	return nil
}

type rateLimit struct {
	token chan struct{}
}

func newRateLimit(n int) *rateLimit {
	return &rateLimit{
		token: make(chan struct{}, n),
	}
}

func (r *rateLimit) getToken(done <-chan struct{}) (exit bool) {
	select {
	case <-done:
		return true
	case r.token <- struct{}{}:
		return false
	}
}

func (r *rateLimit) putToken() {
	select {
	case <-r.token:
	default:
		panic("put a redundant token")
	}
}

// copErrorResponse returns error when calling Next()
type copErrorResponse struct{ error }

func (it copErrorResponse) Next(ctx context.Context) (kv.ResultSubset, error) {
	return nil, it.error
}

func (it copErrorResponse) Close() error {
	return nil
}

// rateLimitAction an OOM Action which is used to control the token if OOM triggered. The token number should be
// set on initial. Each time the Action is triggered, one token would be destroyed. If the count of the token is less
// than 2, the action would be delegated to the fallback action.
type rateLimitAction struct {
	// enabled indicates whether the rateLimitAction is permitted to Action. 1 means permitted, 0 denied.
	enabled        uint32
	fallbackAction memory.ActionOnExceed
	// totalTokenNum indicates the total token at initial
	totalTokenNum uint
	cond          struct {
		*sync.Cond
		// exceeded indicates whether have encountered OOM situation.
		exceeded bool
		// remainingTokenNum indicates the count of tokens which still exists
		remainingTokenNum uint
		// isTokenDestroyed indicates whether there is one token has been isTokenDestroyed after Action been triggered
		isTokenDestroyed bool
		once             sync.Once
		// triggered indicates whether the action is triggered
		triggered bool
		// waitingWorkerCnt indicates the total count of workers which is under condition.Waiting
		waitingWorkerCnt uint
		// triggerCountForTest indicates the total count of the rateLimitAction's Action being executed
		triggerCountForTest uint
	}
}

func newRateLimitAction(totalTokenNumber uint, cond *sync.Cond) *rateLimitAction {
	return &rateLimitAction{
		totalTokenNum: totalTokenNumber,
		cond: struct {
			*sync.Cond
			exceeded            bool
			remainingTokenNum   uint
			isTokenDestroyed    bool
			once                sync.Once
			triggered           bool
			waitingWorkerCnt    uint
			triggerCountForTest uint
		}{
			Cond:              cond,
			exceeded:          false,
			remainingTokenNum: totalTokenNumber,
			once:              sync.Once{},
		},
	}
}

// Action implements ActionOnExceed.Action
func (e *rateLimitAction) Action(t *memory.Tracker) {
	failpoint.Inject("testRateLimitActionDisable", func(val failpoint.Value) {
		if val.(bool) {
			e.setEnabled(false)
		}
	})

	if !e.isEnabled() {
		if e.fallbackAction != nil {
			e.fallbackAction.Action(t)
		}
		return
	}
	e.conditionLock()
	defer e.conditionUnlock()
	e.cond.once.Do(func() {
		if e.cond.remainingTokenNum < 2 {
			e.setEnabled(false)
			logutil.BgLogger().Info("memory exceed quota, rateLimitAction delegate to fallback action",
				zap.Uint("total token count", e.totalTokenNum))
			if e.fallbackAction != nil {
				e.fallbackAction.Action(t)
			}
			return
		}
		failpoint.Inject("testRateLimitActionMockConsumeAndAssert", func(val failpoint.Value) {
			if val.(bool) {
				if e.cond.triggerCountForTest+e.cond.remainingTokenNum != e.totalTokenNum {
					panic("triggerCount + remainingTokenNum not equal to totalTokenNum")
				}
				if e.cond.waitingWorkerCnt > 0 {
					panic("waitingWorkerCnt not equal to 0")
				}
			}
		})
		logutil.BgLogger().Info("memory exceeds quota, destroy one token now.",
			zap.Int64("consumed", t.BytesConsumed()),
			zap.Int64("quota", t.GetBytesLimit()),
			zap.Uint("total token count", e.totalTokenNum),
			zap.Uint("remaining token count", e.cond.remainingTokenNum))
		e.cond.isTokenDestroyed = false
		e.cond.exceeded = true
		e.cond.triggerCountForTest++
		e.cond.triggered = true
	})
}

// SetLogHook implements ActionOnExceed.SetLogHook
func (e *rateLimitAction) SetLogHook(hook func(uint64)) {

}

// SetFallback implements ActionOnExceed.SetFallback
func (e *rateLimitAction) SetFallback(a memory.ActionOnExceed) {
	e.fallbackAction = a
}

// broadcastIfNeeded will broadcast the condition to recover all suspended workers when exceeded is enabled
// and one token have already been destroyed.
func (e *rateLimitAction) broadcastIfNeeded(needed bool) {
	if !needed {
		return
	}
	e.conditionLock()
	defer e.conditionUnlock()
	if !e.cond.exceeded {
		return
	}
	for !e.cond.isTokenDestroyed {
		e.cond.Wait()
	}
	e.cond.exceeded = false
	e.cond.Broadcast()
	e.unsafeInitOnce()
}

// destroyTokenIfNeeded will check the `exceed` flag after copWorker finished one task.
// If the exceed flag is true and there is no token been destroyed before, one token will be destroyed,
// or the token would be return back.
func (e *rateLimitAction) destroyTokenIfNeeded(returnToken func()) {
	e.conditionLock()
	defer e.conditionUnlock()
	if !e.cond.exceeded {
		returnToken()
		return
	}
	// If actionOnExceed has been triggered and there is no token have been destroyed before,
	// destroy one token.
	if !e.cond.isTokenDestroyed {
		e.cond.remainingTokenNum = e.cond.remainingTokenNum - 1
		e.cond.isTokenDestroyed = true
		e.cond.Broadcast()
		return
	}

	returnToken()
	// we suspend worker when `exceeded` is true until being notified by `broadcastIfNeeded`
	for e.cond.exceeded {
		e.cond.waitingWorkerCnt++
		e.cond.Wait()
		e.cond.waitingWorkerCnt--
	}
	e.unsafeInitOnce()
}

// unsafeInitOnce would init once if the condition is meet. This should be used under condition's lock.
func (e *rateLimitAction) unsafeInitOnce() {
	// only when all the waiting workers have been resumed, the Action could be initialized again.
	if e.cond.waitingWorkerCnt < 1 && e.cond.triggered {
		e.cond.triggered = false
		e.cond.once = sync.Once{}
	}
}

func (e *rateLimitAction) conditionLock() {
	e.cond.L.Lock()
}

func (e *rateLimitAction) conditionUnlock() {
	e.cond.L.Unlock()
}

func (e *rateLimitAction) close() {
	if !e.isEnabled() {
		return
	}
	e.setEnabled(false)
	e.conditionLock()
	defer e.conditionUnlock()
	e.cond.exceeded = false
	e.cond.isTokenDestroyed = true
	e.cond.waitingWorkerCnt = 0
	// broadcast the signal in order not to leak worker goroutine if it is being suspended
	e.cond.Broadcast()
}

func (e *rateLimitAction) setEnabled(enabled bool) {
	newValue := uint32(0)
	if enabled {
		newValue = uint32(1)
	}
	atomic.StoreUint32(&e.enabled, newValue)
}

func (e *rateLimitAction) isEnabled() bool {
	return atomic.LoadUint32(&e.enabled) > 0
}

type maxIDHandler struct {
	sync.Mutex
	maxID uint32
}

func (handler *maxIDHandler) getMaxID() uint32 {
	handler.Lock()
	defer handler.Unlock()
	return handler.maxID
}

func (handler *maxIDHandler) setMaxIDIfLarger(newID uint32) {
	handler.Lock()
	defer handler.Unlock()
	if newID > handler.maxID {
		handler.maxID = newID
	}
}


text:
<?php

use Faker\Generator as Faker;

$factory->define(\Spatie\Permission\Models\Permission::class, function (Faker $faker) {
    return [];
});


text:
//*****************************************************************************
//
//  File:       BooleanValuePropertyProvider.cs
//
//  Contents:   Definition for boolean value properties provider
//
//*****************************************************************************

using RibbonLib.Interop;

namespace RibbonLib.Controls.Properties
{
    /// <summary>
    /// Definition for boolean value properties provider interface
    /// </summary>
    public interface IBooleanValuePropertyProvider
    {
        /// <summary>
        /// Boolean value property
        /// </summary>
        bool BooleanValue { get; set; }
    }

    /// <summary>
    /// Implementation of IBooleanValuePropertyProvider
    /// </summary>
    public class BooleanValuePropertyProvider : BasePropertiesProvider, IBooleanValuePropertyProvider
    {
        /// <summary>
        /// BooleanValuePropertyProvider ctor
        /// </summary>
        /// <param name="ribbon">parent ribbon</param>
        /// <param name="commandId">ribbon control command id</param>
        public BooleanValuePropertyProvider(Ribbon ribbon, uint commandId)
            : base(ribbon, commandId)
        {
            // add supported properties
            _supportedProperties.Add(RibbonProperties.BooleanValue);
        }

        private bool? _booleanValue;

        /// <summary>
        /// Handles IUICommandHandler.UpdateProperty function for the supported properties
        /// </summary>
        /// <param name="key">The Property Key to update</param>
        /// <param name="currentValue">A pointer to the current value for key. This parameter can be NULL</param>
        /// <param name="newValue">When this method returns, contains a pointer to the new value for key</param>
        /// <returns>Returns S_OK if successful, or an error value otherwise</returns>
        public override HRESULT UpdateProperty(ref PropertyKey key, PropVariantRef currentValue, ref PropVariant newValue)
        {
            if (key == RibbonProperties.BooleanValue)
            {
                if (_booleanValue.HasValue)
                {
                    newValue.SetBool(_booleanValue.Value);
                }
            }

            return HRESULT.S_OK;
        }

        #region IBooleanValuePropertyProvider Members

        /// <summary>
        /// Boolean value property
        /// </summary>
        public bool BooleanValue
        {
            get
            {
                if (_ribbon.Initalized)
                {
                    PropVariant boolValue;
                    HRESULT hr = _ribbon.Framework.GetUICommandProperty(_commandID, ref RibbonProperties.BooleanValue, out boolValue);
                    if (NativeMethods.Succeeded(hr))
                    {
                        return (bool)boolValue.Value;
                    }
                }
                return _booleanValue.GetValueOrDefault();
            }
            set
            {
                _booleanValue = value;
                if (_ribbon.Initalized)
                {
                    PropVariant boolValue = PropVariant.FromObject(value);
                    HRESULT hr = _ribbon.Framework.SetUICommandProperty(_commandID, ref RibbonProperties.BooleanValue, ref boolValue);
                }
            }
        }

        #endregion
    }
}


text:
package org.wikipedia.readinglist.api;

import org.wikipedia.Site;
import org.wikipedia.readinglist.api.legacy.LegacyReadingListPageTitlesResponse;
import org.wikipedia.readinglist.api.legacy.LegacyReadingListsResponse;
import org.wikipedia.dataclient.RestAdapterFactory;

import retrofit.http.GET;
import retrofit.http.Query;

import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;

/**
 * Gets and posts collection related data from and to the server.
 */
public class ReadingListDataClient {
    private static final Site SITE = new Site("en.wikipedia.org");

    @NonNull private final Api client;

    public ReadingListDataClient() {
        client = RestAdapterFactory.newInstance(SITE).create(Api.class);
    }

    @VisibleForTesting
    public ReadingListDataClient(String baseUrl) {
        client = RestAdapterFactory.newInstance(SITE, baseUrl).create(Api.class);
    }

    /**
     * Gets the Collections of the current user.
     */
    public LegacyReadingListsResponse getReadingLists() {
        return client.getReadingLists();
    }

    /**
     * Gets the list of page titles of a single ReadingList of the current user.
     *
     * @param listId ID of the reading list to be retrieved
     */
    public LegacyReadingListPageTitlesResponse getMemberPages(int listId) {
        return client.getMemberPages(listId);
    }

    private interface Api {
        String ACTION_QUERY_LIST = "/w/api.php?format=json&formatversion=2&action=query&list=";

        @GET(ACTION_QUERY_LIST + "lists"
                + "&lstprop=label%7Cdescription%7Cpublic%7Creview%7Cimage%7Ccount%7Cupdated%7Cowner")
        LegacyReadingListsResponse getReadingLists();

        @GET(ACTION_QUERY_LIST + "listpages")
        LegacyReadingListPageTitlesResponse getMemberPages(@Query("lspid") int collectionId);
    }
}


text:
WordPress
=========

WordPress Projects


text:
require('angular').module('classified').factory('filterService', function ($http) {

    var filter = "";

    return {
        getFilter : function () {
            return filter;
        },
        setFilter : function (newfilter) {
            filter = newfilter;
        }
    };
});


text:
title: "2019-02-26のJS: yarn v2候補(berry)、Fastify v2、TSLintの今後"
author: "azu"
layout: post
date : 2019-02-26T02:02:23.716Z
category: JSer
tags:
- yarn
- node
- typescript
- eslint
- tslint

---

JSer.info #424 - [yarn](https://yarnpkg.com/) v2のソースコードとなるリポジトリが公開されています。

- [yarnpkg/berry: 📦🐈 A modern, fast, reliable, package manager; part of the Yarn organization](https://github.com/yarnpkg/berry)

次のIssueでも書かれているように、v1とは別のリポジトリにしているのはIssueのtriageをしやすくするためだそうです。

- [👑 Say hello to Yarn v2 · Issue #1 · yarnpkg/berry](https://github.com/yarnpkg/berry/issues/1 "👑 Say hello to Yarn v2 · Issue #1 · yarnpkg/berry")
- [Yarn's Future - v2 and beyond · Issue #6953 · yarnpkg/yarn](https://github.com/yarnpkg/yarn/issues/6953#issuecomment-466589196 "Yarn's Future - v2 and beyond · Issue #6953 · yarnpkg/yarn")

> The codename for these changes is Berry
> -- https://github.com/yarnpkg/yarn/issues/6953

"Berry"という名前はv2に向けた変更のコードネームです。

v2(berry)では[Yarn's Future - v2 and beyond](https://github.com/yarnpkg/yarn/issues/6953)で予定されていたものが実装されている段階のようです。

コードベースがTypeScriptで書き直されていて、monorepoになっています。
lockファイルをymlサブセットとなるような[syml](https://github.com/yarnpkg/berry/blob/master/packages/berry-parsers/sources/syml.ts)というフォーマットを作り実装しています。また、プラグインの仕組みが追加され、PnPやnpm(`package-lock.json`を扱う)などの処理はプラグインとして実装されています。
また、ワークスペースの改善として[Constraints](https://github.com/yarnpkg/berry/blob/master/docs/workspaces-constraints.md)というProlog(実際には[tau-prolog](https://github.com/jariazavalverde/tau-prolog))を使った制約を扱う[プラグイン](https://github.com/yarnpkg/berry/tree/master/packages/plugin-constraints)が追加されています。ワークスペース間の依存などのルールをPrologを使って記述して、より効率的に扱えるようにするのが目的のようです。

興味がある人は[Install](https://github.com/yarnpkg/berry#install)して試したり、[Issue](https://github.com/yarnpkg/berry/issues)を見てみると良いかもしれません。

----

[TypeScriptの2019 Juneロードマップ](https://github.com/Microsoft/TypeScript/issues/29288)でTSLintではなくESLintのサポートを強化していくという話が行われ、[typescript-eslint](https://github.com/typescript-eslint/typescript-eslint)というプロジェクトが作成され[ESLintチームと協力](https://eslint.org/blog/2019/01/future-typescript-eslint)していくことが発表されていました。

このような背景もあり、TSLintのメンテナーからTSLintの今後について記事が公開されています。

- [TSLint in 2019 – Palantir Blog – Medium](https://medium.com/palantir/tslint-in-2019-1a144c2317a9 "TSLint in 2019 – Palantir Blog – Medium")
- [Roadmap: TSLint -> ESLint · Issue #4534 · palantir/tslint](https://github.com/palantir/tslint/issues/4534 "Roadmap: TSLint -> ESLint · Issue #4534 · palantir/tslint")

TSLintは2019年中には非推奨となり(今すぐではないです)、今後はESLintへの移行パスを整備していくという話が書かれています。
[typescript-eslint](https://github.com/typescript-eslint/typescript-eslint)(ESLintのTypeScript対応)は開発途中であるため、TSLintとの互換となるようなルールがない部分もあります。そのため、TSLint自体もしばらくはメンテンスを続けていくという方針についても書かれています。

現状の[typescript-eslint](https://github.com/typescript-eslint/typescript-eslint)の動作については、次の記事も参照して見てください。

- [@typescript-eslint ことはじめ - teppeis blog](https://teppeis.hatenablog.com/entry/2019/02/typescript-eslint)

----

ヘッドライン

----

## Release Version 3.5.0 · knockout/knockout
[github.com/knockout/knockout/releases/tag/v3.5.0](https://github.com/knockout/knockout/releases/tag/v3.5.0 "Release Version 3.5.0 · knockout/knockout")

JavaScript library ReleaseNote

Knockout 3.5.0リリース。
`foreach`バインディングの挙動の変更とパフォーマンスの改善、`ko.when`がPromiseを返すようになるなど

----

## Release: npm@6.9.0-next.0 - 📣 announcements - npm forum
[npm.community/t/release-npm-6-9-0-next-0/5553](https://npm.community/t/release-npm-6-9-0-next-0/5553 "Release: npm@6.9.0-next.0 - 📣 announcements - npm forum")

npm ReleaseNote

npm 6.9.0-next.0リリース。
パッケージに別名をつけることができるpackage alias機能をサポート、`--before`オプションをサポートなど

- [install: add support for package aliases by zkat · Pull Request #3 · npm/cli](https://github.com/npm/cli/pull/3 "install: add support for package aliases by zkat · Pull Request #3 · npm/cli")

----

## The npm Blog — Managing JavaScript in the Enterprise
[blog.npmjs.org/post/182958759735/managing-javascript-in-the-enterprise](https://blog.npmjs.org/post/182958759735/managing-javascript-in-the-enterprise "The npm Blog — Managing JavaScript in the Enterprise")

npm news

npm Enterpriseの正式リリース。
SSO認証の対応やprivate registryのサポートなど

- [npm | Enterprise](https://www.npmjs.com/products/enterprise "npm | Enterprise")

----

## dsherret/conditional-type-checks: Types for testing TypeScript types.
[github.com/dsherret/conditional-type-checks](https://github.com/dsherret/conditional-type-checks "dsherret/conditional-type-checks: Types for testing TypeScript types.")

TypeScript library testing

Conditional Typeを使いTypeScriptの型Assertionを行うライブラリ

----

## Fastify v2.0.0 is here – Fastify – Medium
[medium.com/@fastifyjs/fastify-v2-0-0-is-here-8275d613fba4](https://medium.com/@fastifyjs/fastify-v2-0-0-is-here-8275d613fba4 "Fastify v2.0.0 is here – Fastify – Medium")

node.js ReleaseNote server

Node.jsフレームワークのFastify 2.0.0リリース。
`onError`、`preParsing`、`preSerialization`、`preValidation` hookの追加。
pluginのロードのタイムアウトを設定、シグネチャの一貫性の修正など

- [Release v2.0.0 · fastify/fastify](https://github.com/fastify/fastify/releases/tag/v2.0.0 "Release v2.0.0 · fastify/fastify")

----

## yarnpkg/berry: 📦🐈 A modern, fast, reliable, package manager; part of the Yarn organization
[github.com/yarnpkg/berry](https://github.com/yarnpkg/berry "yarnpkg/berry: 📦🐈 A modern, fast, reliable, package manager; part of the Yarn organization")

yarn news

yarn v2となるソースコードのリポジトリが公開された。
v1のリポジトリと分けているのは、issueをハンドリングしやすくするため。
現在はロードマップに予定した機能はある程度実装された状態で、一部の機能やドキュメントが足りていない状況。

- [Yarn's Future - v2 and beyond · Issue #6953 · yarnpkg/yarn](https://github.com/yarnpkg/yarn/issues/6953#issuecomment-466589196 "Yarn's Future - v2 and beyond · Issue #6953 · yarnpkg/yarn")
- [👑 Say hello to Yarn v2 · Issue #1 · yarnpkg/berry](https://github.com/yarnpkg/berry/issues/1 "👑 Say hello to Yarn v2 · Issue #1 · yarnpkg/berry")

----

アーティクル

----

## AddyOsmani.com - JavaScript Loading Priorities in Chrome
[addyosmani.com/blog/script-priorities/](https://addyosmani.com/blog/script-priorities/ "AddyOsmani.com - JavaScript Loading Priorities in Chrome")

JavaScript performance browser article

link rel=preloadとscript要素の defer/async属性を組み合わせ際のLoadingやExecution Priorityについてまとめた記事

----

## How @supports Works | CSS-Tricks
[css-tricks.com/how-supports-works/](https://css-tricks.com/how-supports-works/ "How @supports Works | CSS-Tricks")

CSS article

CSSの`@supports`の使い方やユースケースについて。
また、JavaScriptからのCSS Supportsを使ったFeature Detectについて

----

## Intelligent Tracking Prevention 2.1 | WebKit
[webkit.org/blog/8613/intelligent-tracking-prevention-2-1/](https://webkit.org/blog/8613/intelligent-tracking-prevention-2-1/ "Intelligent Tracking Prevention 2.1 | WebKit")

safari security article

Safari 12.1で導入されるITP 2.1について。 Partitioned Cookiesのサポート終了、document.cookieで作成したクッキーは生存期間が7日間のcapがつくように、DNTの削除など

----

## TSLint in 2019 – Palantir Blog – Medium
[medium.com/palantir/tslint-in-2019-1a144c2317a9](https://medium.com/palantir/tslint-in-2019-1a144c2317a9 "TSLint in 2019 – Palantir Blog – Medium")

TypeScript article Tools

TSLintの今後について。
今後TSLintは非推奨となり、TSLintからESLintへの移行パスを整備していくという話

----

サイト、サービス、ドキュメント

----

## React – A JavaScript library for building user interfaces
[ja.reactjs.org/](https://ja.reactjs.org/ "React – A JavaScript library for building user interfaces")

React 翻訳

Reactサイトの日本語訳

- [Is React Translated Yet? ¡Sí! Sim! はい! – React Blog](https://reactjs.org/blog/2019/02/23/is-react-translated-yet.html "Is React Translated Yet? ¡Sí! Sim! はい! – React Blog")

----

ソフトウェア、ツール、ライブラリ関係

----

## patr0nus/DeskGap: A cross-platform desktop app framework based on Node.js and the system webview
[github.com/patr0nus/DeskGap/](https://github.com/patr0nus/DeskGap/ "patr0nus/DeskGap: A cross-platform desktop app framework based on Node.js and the system webview")

node.js mac windows Electron

OSのWebViewの仕組みを利用したElectronライクなデスクトップアプリ作成フレームワーク。
Chromiumなどをbundleしないが、Node.jsをbundleしている。
フレームワーク上では制限されたElectronライクなAPIとNode.jsの機能が利用できる。

----

## klaussinani/qoa: 💬 Minimal interactive command-line prompts
[github.com/klaussinani/qoa](https://github.com/klaussinani/qoa "klaussinani/qoa: 💬 Minimal interactive command-line prompts")

node.js library console UI

ターミナルで対話的なインタフェースを提供するNode.jsライブラリ。
Confirm、Quiz、Secure Promptなど

----

## jariazavalverde/tau-prolog: An open source Prolog interpreter in JavaScript
[github.com/jariazavalverde/tau-prolog](https://github.com/jariazavalverde/tau-prolog "jariazavalverde/tau-prolog: An open source Prolog interpreter in JavaScript")

JavaScript program library

PrologインタプリタのJavaScript実装

----

## fagbokforlaget/simple-fs: Handles files on indexeddb like you would do in node.js (promise)
[github.com/fagbokforlaget/simple-fs](https://github.com/fagbokforlaget/simple-fs "fagbokforlaget/simple-fs: Handles files on indexeddb like you would do in node.js (promise)")

browser JavaScript library

IndexedDBを使ったファイルシステムライクなAPIを持ったブラウザ向けライブラリ

----

## namics/webpack-config-plugins: Provide best practices for webpack loader configurations
[github.com/namics/webpack-config-plugins](https://github.com/namics/webpack-config-plugins "namics/webpack-config-plugins: Provide best practices for webpack loader configurations")

webpack 設定

webpackのloader設定をまとめたプラグイン。
Babel、TypeScript、Sass、Font、Imageなどのビルドを行うプラグインが用意されている

----


text:
package timely.testing;

import org.apache.http.HttpHost;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import timely.client.http.HttpClient;

public class TimelyHttpsUtil {

    private CloseableHttpClient client = null;
    CookieStore cookieStore = new BasicCookieStore();

    public TimelyHttpsUtil(String trustStoreFile, String trustStoreType, String trustStorePass, String keyStoreFile,
            String keyStoreType, String keyStorePass) {
        client = HttpClient.get(trustStoreFile, trustStoreType, trustStorePass, keyStoreFile, keyStoreType,
                keyStorePass, cookieStore, false);
    }

    public CloseableHttpResponse query(MetricQuery query, String host, int port) {

        CloseableHttpResponse response = null;

        try {
            HttpHost httpHost = new HttpHost(host, port, "https");
            HttpPost httpPost = new HttpPost("https://" + host + ":" + Integer.toString(port) + "/api/query");
            httpPost.setEntity(new StringEntity(query.getJson()));
            response = client.execute(httpHost, httpPost);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

}


text:
Welcome to Nova Release Notes documentation!
==============================================

Contents
========

.. toctree::
   :maxdepth: 1

   unreleased
   rocky
   queens
   pike
   ocata
   newton
   mitaka
   liberty


text:
const unsigned TRIPLE_COUNT = 20*20*20;

struct TripleCount
	{
	unsigned m_uSeqCount;			// How many sequences have this triple?
	unsigned short *m_Counts;		// m_Counts[s] = nr of times triple found in seq s
	};
static TripleCount *TripleCounts;

// WARNING: Sequences MUST be stripped of gaps and upper case!
void DistKmer20_3(const SeqVect &v, DistFunc &DF)
	{
	const unsigned uSeqCount = v.Length();

	DF.SetCount(uSeqCount);
	if (0 == uSeqCount)
		return;
	for (unsigned uSeq1 = 0; uSeq1 < uSeqCount; ++uSeq1)
		{
		DF.SetDist(uSeq1, uSeq1, 0);
		for (unsigned uSeq2 = 0; uSeq2 < uSeq1; ++uSeq2)
			DF.SetDist(uSeq1, uSeq2, 0);
		}

	const unsigned uTripleArrayBytes = TRIPLE_COUNT*sizeof(TripleCount);
	TripleCounts = (TripleCount *) malloc(uTripleArrayBytes);
	if (0 == TripleCounts)
		Quit("Not enough memory (TripleCounts)");
	memset(TripleCounts, 0, uTripleArrayBytes);

	for (unsigned uWord = 0; uWord < TRIPLE_COUNT; ++uWord)
		{
		TripleCount &tc = *(TripleCounts + uWord);
		const unsigned uBytes = uSeqCount*sizeof(short);
		tc.m_Counts = (unsigned short *) malloc(uBytes);
		memset(tc.m_Counts, 0, uBytes);
		}

	for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
		{
		Seq &s = *(v[uSeqIndex]);
		const unsigned uSeqLength = s.Length();
		for (unsigned uPos = 0; uPos < uSeqLength - 2; ++uPos)
			{
			const unsigned uLetter1 = CharToLetterEx(s[uPos]);
			if (uLetter1 >= 20)
				continue;
			const unsigned uLetter2 = CharToLetterEx(s[uPos+1]);
			if (uLetter2 >= 20)
				continue;
			const unsigned uLetter3 = CharToLetterEx(s[uPos+2]);
			if (uLetter3 >= 20)
				continue;

			const unsigned uWord = uLetter1 + uLetter2*20 + uLetter3*20*20;
			assert(uWord < TRIPLE_COUNT);

			TripleCount &tc = *(TripleCounts + uWord);
			const unsigned uOldCount = tc.m_Counts[uSeqIndex];
			if (0 == uOldCount)
				++(tc.m_uSeqCount);

			++(tc.m_Counts[uSeqIndex]);
			}
		}

#if TRACE
	{
	Log("TripleCounts\n");
	unsigned uGrandTotal = 0;
	for (unsigned uWord = 0; uWord < TRIPLE_COUNT; ++uWord)
		{
		const TripleCount &tc = *(TripleCounts + uWord);
		if (0 == tc.m_uSeqCount)
			continue;

		const unsigned uLetter3 = uWord/(20*20);
		const unsigned uLetter2 = (uWord - uLetter3*20*20)/20;
		const unsigned uLetter1 = uWord%20;
		Log("Word %6u %c%c%c   %6u",
		  uWord,
		  LetterToCharAmino(uLetter1),
		  LetterToCharAmino(uLetter2),
		  LetterToCharAmino(uLetter3),
		  tc.m_uSeqCount);

		unsigned uSeqCountWithThisWord = 0;
		for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
			{
			const unsigned uCount = tc.m_Counts[uSeqIndex];
			if (uCount > 0)
				{
				++uSeqCountWithThisWord;
				Log(" %u=%u", uSeqIndex, uCount);
				uGrandTotal += uCount;
				}
			}
		if (uSeqCountWithThisWord != tc.m_uSeqCount)
			Log(" *** SQ ERROR *** %u %u", tc.m_uSeqCount, uSeqCountWithThisWord);
		Log("\n");
		}

	unsigned uTotalBySeqLength = 0;
	for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
		{
		Seq &s = *(v[uSeqIndex]);
		const unsigned uSeqLength = s.Length();
		uTotalBySeqLength += uSeqLength - 2;
		}
	if (uGrandTotal != uTotalBySeqLength)
		Log("*** TOTALS DISAGREE *** %u %u\n", uGrandTotal, uTotalBySeqLength);
	}
#endif

	const unsigned uSeqListBytes = uSeqCount*sizeof(unsigned);
	unsigned short *SeqList = (unsigned short *) malloc(uSeqListBytes);

	for (unsigned uWord = 0; uWord < TRIPLE_COUNT; ++uWord)
		{
		const TripleCount &tc = *(TripleCounts + uWord);
		if (0 == tc.m_uSeqCount)
			continue;

		unsigned uSeqCountFound = 0;
		memset(SeqList, 0, uSeqListBytes);

		for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
			{
			if (tc.m_Counts[uSeqIndex] > 0)
				{
				SeqList[uSeqCountFound] = uSeqIndex;
				++uSeqCountFound;
				if (uSeqCountFound == tc.m_uSeqCount)
					break;
				}
			}
		assert(uSeqCountFound == tc.m_uSeqCount);

		for (unsigned uSeq1 = 0; uSeq1 < uSeqCountFound; ++uSeq1)
			{
			const unsigned uSeqIndex1 = SeqList[uSeq1];
			const unsigned uCount1 = tc.m_Counts[uSeqIndex1];
			for (unsigned uSeq2 = 0; uSeq2 < uSeq1; ++uSeq2)
				{
				const unsigned uSeqIndex2 = SeqList[uSeq2];
				const unsigned uCount2 = tc.m_Counts[uSeqIndex2];
				const unsigned uMinCount = uCount1 < uCount2 ? uCount1 : uCount2;
				const double d = DF.GetDist(uSeqIndex1, uSeqIndex2);
				DF.SetDist(uSeqIndex1, uSeqIndex2, (float) (d + uMinCount));
				}
			}
		}
	delete[] SeqList;
	free(TripleCounts);

	unsigned uDone = 0;
	const unsigned uTotal = (uSeqCount*(uSeqCount - 1))/2;
	for (unsigned uSeq1 = 0; uSeq1 < uSeqCount; ++uSeq1)
		{
		DF.SetDist(uSeq1, uSeq1, 0.0);

		const Seq &s1 = *(v[uSeq1]);
		const unsigned uLength1 = s1.Length();

		for (unsigned uSeq2 = 0; uSeq2 < uSeq1; ++uSeq2)
			{
			const Seq &s2 = *(v[uSeq2]);
			const unsigned uLength2 = s2.Length();
			unsigned uMinLength = uLength1 < uLength2 ? uLength1 : uLength2;
			if (uMinLength < 3)
				{
				DF.SetDist(uSeq1, uSeq2, 1.0);
				continue;
				}

			const double dTripleCount = DF.GetDist(uSeq1, uSeq2);
			if (dTripleCount == 0)
				{
				DF.SetDist(uSeq1, uSeq2, 1.0);
				continue;
				}
			double dNormalizedTripletScore = dTripleCount/(uMinLength - 2);
			//double dEstimatedPairwiseIdentity = exp(0.3912*log(dNormalizedTripletScore));
			//if (dEstimatedPairwiseIdentity > 1)
			//	dEstimatedPairwiseIdentity = 1;
//			DF.SetDist(uSeq1, uSeq2, (float) (1.0 - dEstimatedPairwiseIdentity));
			DF.SetDist(uSeq1, uSeq2, (float) dNormalizedTripletScore);

#if	TRACE
			{
			Log("%s - %s  Triplet count = %g  Lengths %u, %u Estimated pwid = %g\n",
			  s1.GetName(), s2.GetName(), dTripleCount, uLength1, uLength2,
			  dEstimatedPairwiseIdentity);
			}
#endif
			if (uDone%1000 == 0)
				Progress(uDone, uTotal);
			}
		}
	ProgressStepsDone();
	}