text:
<?php declare(strict_types=1);
/**
 * This file is part of Swoft.
 *
 * @link     https://swoft.org
 * @document https://swoft.org/docs
 * @contact  group@swoft.org
 * @license  https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */

namespace App\Common;

use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Db\Connection\Connection;
use Swoft\Db\Contract\DbSelectorInterface;

/**
 * Class DbSelector
 *
 * @since 2.0
 *
 * @Bean()
 */
class DbSelector implements DbSelectorInterface
{
    /**
     * @param Connection $connection
     */
    public function select(Connection $connection): void
    {
        $selectIndex  = (int)context()->getRequest()->query('id', 0);
        $createDbName = $connection->getDb();

        if ($selectIndex == 0) {
            $selectIndex = '';
        }

        if ($createDbName == 'test2') {
            $createDbName = 'test';
        }

        $dbName = sprintf('%s%s', $createDbName, (string)$selectIndex);
        $connection->db($dbName);
    }
}


text:
<?xml version='1.0' encoding='UTF-8'?>
<Document>
  <Header>
    <SchemaName>Location</SchemaName>
    <Identifier>location_geonames_2750314</Identifier>
    <DocumentState>public</DocumentState>
    <TimeStamp>1390753462723</TimeStamp>
    <SummaryFields>
      <Title>NL > North Brabant > Nieuwe Heipolder</Title>
    </SummaryFields>
  </Header>
  <Body>
    <Location>
      <GeopoliticalHierarchy>NL,North Brabant,Nieuwe Heipolder</GeopoliticalHierarchy>
      <LocationName>
        <Appelation>Nieuwe Heipolder</Appelation>
        <LocationNameType>polder</LocationNameType>
        <Comments></Comments>
      </LocationName>
      <Coordinates>
        <Longitude>4.26205</Longitude>
        <Latitude>51.59811</Latitude>
      </Coordinates>
      <GeonamesURI>http://www.geonames.org/2750314</GeonamesURI>
      <WikipediaLink/>
      <DbpediaLink/>
    </Location>
  </Body>
</Document>

redpajama_set_name: RedPajamaGithub


text:
namespace DotWeb_Samples {

    public partial class _Default {
    }
}


text:
package com.mauriciotogneri.dry.compiler.lexical.tokens.delimiters;

import com.mauriciotogneri.dry.compiler.lexical.Lexeme;
import com.mauriciotogneri.dry.compiler.lexical.Token;

public class SpaceToken extends Token
{
    public SpaceToken(Lexeme lexeme)
    {
        super(lexeme);
    }
}


text:
package roachpb

import (
	"errors"
	"fmt"
	"math/rand"
	"strings"
)

// IsAdmin returns true iff the BatchRequest contains an admin request.
func (ba *BatchRequest) IsAdmin() bool {
	return ba.flags()&isAdmin != 0
}

// IsWrite returns true iff the BatchRequest contains a write.
func (ba *BatchRequest) IsWrite() bool {
	return (ba.flags() & isWrite) != 0
}

// IsReadOnly returns true if all requests within are read-only.
func (ba *BatchRequest) IsReadOnly() bool {
	flags := ba.flags()
	return (flags&isRead) != 0 && (flags&isWrite) == 0
}

// IsReverse returns true iff the BatchRequest contains a reverse request.
func (ba *BatchRequest) IsReverse() bool {
	return (ba.flags() & isReverse) != 0
}

// IsTransactionWrite returns true iff the BatchRequest contains a txn write.
func (ba *BatchRequest) IsTransactionWrite() bool {
	return (ba.flags() & isTxnWrite) != 0
}

// IsRange returns true iff the BatchRequest contains a range request.
func (ba *BatchRequest) IsRange() bool {
	return (ba.flags() & isRange) != 0
}

// GetArg returns the first request of the given type, if possible.
func (ba *BatchRequest) GetArg(method Method) (Request, bool) {
	// TODO(tschottdorf): when looking for EndTransaction, just look at the
	// last entry.
	for _, arg := range ba.Requests {
		if req := arg.GetInner(); req.Method() == method {
			return req, true
		}
	}
	return nil, false
}

// First returns the first response of the given type, if possible.
func (br *BatchResponse) First() Response {
	if len(br.Responses) > 0 {
		return br.Responses[0].GetInner()
	}
	return nil
}

// Header returns a pointer to the header.
func (br *BatchResponse) Header() *BatchResponse_Header {
	return &br.BatchResponse_Header
}

// GetIntents returns a slice of key pairs corresponding to transactional writes
// contained in the batch.
// TODO(tschottdorf): use roachpb.Span here instead of []Intent. Actually
// Intent should be Intents = {Txn, []Span} so that a []Span can
// be turned into Intents easily by just adding a Txn.
func (ba *BatchRequest) GetIntents() []Intent {
	var intents []Intent
	for _, arg := range ba.Requests {
		req := arg.GetInner()
		if !IsTransactionWrite(req) {
			continue
		}
		h := req.Header()
		intents = append(intents, Intent{Key: h.Key, EndKey: h.EndKey})
	}
	return intents
}

// ResetAll resets all the contained requests to their original state.
func (br *BatchResponse) ResetAll() {
	if br == nil {
		return
	}
	for _, rsp := range br.Responses {
		// TODO(tschottdorf) `rsp.Reset()` isn't enough because rsp
		// isn't a pointer.
		rsp.GetInner().Reset()
	}
}

// Combine implements the Combinable interface. It combines each slot of the
// given request into the corresponding slot of the base response. The number
// of slots must be equal and the respective slots must be combinable.
// TODO(tschottdorf): write tests.
func (br *BatchResponse) Combine(otherBatch *BatchResponse) error {
	if len(otherBatch.Responses) != len(br.Responses) {
		return errors.New("unable to combine batch responses of different length")
	}
	for i, l := 0, len(br.Responses); i < l; i++ {
		valLeft := br.Responses[i].GetInner()
		valRight := otherBatch.Responses[i].GetInner()
		args, lOK := valLeft.(Combinable)
		reply, rOK := valRight.(Combinable)
		if lOK && rOK {
			if err := args.Combine(reply.(Response)); err != nil {
				return err
			}
			continue
		}
		// If our slot is a NoopResponse, then whatever the other batch has is
		// the result. Note that the result can still be a NoopResponse, to be
		// filled in by a future Combine().
		if _, ok := valLeft.(*NoopResponse); ok {
			br.Responses[i] = otherBatch.Responses[i]
		}
	}
	return nil
}

// Add adds a request to the batch request.
func (ba *BatchRequest) Add(requests ...Request) {
	for _, args := range requests {
		union := RequestUnion{}
		if !union.SetValue(args) {
			panic(fmt.Sprintf("unable to add %T to batch request", args))
		}
		ba.Requests = append(ba.Requests, union)
	}
}

// Add adds a response to the batch response.
func (br *BatchResponse) Add(reply Response) {
	union := ResponseUnion{}
	if !union.SetValue(reply) {
		// TODO(tschottdorf) evaluate whether this should return an error.
		panic(fmt.Sprintf("unable to add %T to batch response", reply))
	}
	br.Responses = append(br.Responses, union)
}

// Methods returns a slice of the contained methods.
func (ba *BatchRequest) Methods() []Method {
	var res []Method
	for _, arg := range ba.Requests {
		res = append(res, arg.GetInner().Method())
	}
	return res
}

// CreateReply implements the Request interface. It's slightly different from
// the other implementations: It creates replies for each of the contained
// requests, wrapped in a BatchResponse.
func (ba *BatchRequest) CreateReply() *BatchResponse {
	br := &BatchResponse{}
	for _, union := range ba.Requests {
		br.Add(union.GetInner().CreateReply())
	}
	return br
}

func (ba *BatchRequest) flags() int {
	var flags int
	for _, union := range ba.Requests {
		flags |= union.GetInner().flags()
	}
	return flags
}

// Split separate the requests contained in a batch so that each subset of
// requests can be executed by a Store (without changing order). In particular,
// Admin and EndTransaction requests are always singled out and mutating
// requests separated from reads.
func (ba BatchRequest) Split() [][]RequestUnion {
	compatible := func(exFlags, newFlags int) bool {
		// If no flags are set so far, everything goes.
		if exFlags == 0 {
			return true
		}
		if (newFlags & isAlone) != 0 {
			return false
		}
		// Otherwise, the flags below must remain the same
		// with the new request added.
		// Note that we're not checking isRead: The invariants we're
		// enforcing are that a batch can't mix non-writes with writes.
		// Checking isRead would ConditionalPut and Put to conflict,
		// which is not what we want.
		const mask = isWrite | isAdmin | isReverse
		return (mask & exFlags) == (mask & newFlags)
	}
	var parts [][]RequestUnion
	for len(ba.Requests) > 0 {
		part := ba.Requests
		var gFlags int
		for i, union := range ba.Requests {
			flags := union.GetInner().flags()
			if !compatible(gFlags, flags) {
				part = ba.Requests[:i]
				break
			}
			gFlags |= flags
		}
		parts = append(parts, part)
		ba.Requests = ba.Requests[len(part):]
	}
	return parts
}

// String gives a brief summary of the contained requests and keys in the batch.
// TODO(tschottdorf): the key range is useful information, but requires `keys`.
// See #2198.
func (ba BatchRequest) String() string {
	var str []string
	for _, arg := range ba.Requests {
		req := arg.GetInner()
		h := req.Header()
		str = append(str, fmt.Sprintf("%s [%s,%s)", req.Method(), h.Key, h.EndKey))
	}
	return strings.Join(str, ", ")
}

// GetOrCreateCmdID returns the request header's command ID if available.
// Otherwise, creates a new ClientCmdID, initialized with current time
// and random salt.
func (ba BatchRequest) GetOrCreateCmdID(walltime int64) (cmdID ClientCmdID) {
	if !ba.CmdID.IsEmpty() {
		cmdID = ba.CmdID
	} else {
		cmdID = ClientCmdID{
			WallTime: walltime,
			Random:   rand.Int63(),
		}
	}
	return
}

// TraceID implements tracer.Traceable by returning the first nontrivial
// TraceID of the Transaction and CmdID.
func (ba BatchRequest) TraceID() string {
	if r := ba.Txn.TraceID(); r != "" {
		return r
	}
	return ba.CmdID.TraceID()
}

// TraceName implements tracer.Traceable and behaves like TraceID, but using
// the TraceName of the object delegated to.
func (ba BatchRequest) TraceName() string {
	if r := ba.Txn.TraceID(); r != "" {
		return ba.Txn.TraceName()
	}
	return ba.CmdID.TraceName()
}

// TODO(marc): we should assert
// var _ security.RequestWithUser = &BatchRequest{}
// here, but we need to break cycles first.

// GetUser implements security.RequestWithUser.
// KV messages are always sent by the node user.
func (*BatchRequest) GetUser() string {
	// TODO(marc): we should use security.NodeUser here, but we need to break cycles first.
	return "node"
}

// GoError returns the non-nil error from the proto.Error union.
func (br *BatchResponse) GoError() error {
	return br.Error.GoError()
}

// SetGoError converts the specified type into either one of the proto-
// defined error types or into an Error for all other Go errors.
func (br *BatchResponse) SetGoError(err error) {
	if err == nil {
		br.Error = nil
		return
	}
	br.Error = &Error{}
	br.Error.SetGoError(err)
}


text:
salt-book
=========

Book about getting started with salt


text:
using System;
using System.IO;
using Newtonsoft.Json.Linq;
using Noobot.Core.Configuration;

namespace OrchidBot.Configuration
{
    public class ConfigReader : IConfigReader
    {
        public string SlackApiKey()
        {
            return Properties.Settings.Default.SlackAPIKey;
        }

        public bool HelpEnabled()
        {
            return false;
        }

        public T GetConfigEntry<T>(string entryName)
        {
            //we are not passing any other settings to Noobot
            return default(T);
        }
    }
}


text:
import text_stats
#Text from 'http://en.wikipedia.org/wiki/Electric_charge'
wiki_text = """ Electric charge is the physical property of matter that causes
                it to experience a force when placed in an electromagnetic
                field. There are two types of electric charges: positive and
                negative. Positively charged substances are repelled from
                other positively charged substances, but attracted to
                negatively charged substances; negatively charged substances
                are repelled from negative and attracted to positive. An
                object is negatively charged if it has an excess of electrons,
                and is otherwise positively charged or uncharged. The SI
                derived unit of electric charge is the coulomb (C), although
                in electrical engineering it is also common to use the
                ampere-hour (Ah), and in chemistry it is common to use the
                elementary charge (e) as a unit. The symbol Q is often used to
                denote charge. The early knowledge of how charged substances
                interact is now called classical electrodynamics, and is still
                very accurate if quantum effects do not need to be considered.
                The electric charge is a fundamental conserved property of
                some subatomic particles, which determines their
                electromagnetic interaction. Electrically charged matter is
                influenced by, and produces, electromagnetic fields. The
                interaction between a moving charge and an electromagnetic
                field is the source of the electromagnetic force, which is one
                of the four fundamental forces (See also: magnetic field).
                Twentieth-century experiments demonstrated that electric
                charge is quantized; that is, it comes in integer multiples
                of individual small units called the elementary charge, e,
                approximately equal to 1.602×10−19 coulombs (except for
                particles called quarks, which have charges that are integer
                multiples of e/3). The proton has a charge of +e, and the
                electron has a charge of −e. The study of charged particles,
                and how their interactions are mediated by photons, is called
                quantum electrodynamics.
            """

#TODO add option for input parameters, maybe use file or url
def main():
    word_stats = text_stats.text_stats()
    word_stats.extract_stats(wiki_text)
    word_stats.print_sorted_dictionary(word_stats.freq_table)

if __name__ == '__main__':
    main()


text:
from phusion/baseimage
maintainer bbimber@gmail.com

# these will vary by LabKey version:
ENV SVN_BRANCH discvr17.3
ENV LK_HOME /labkey

#
# Create directories required for running LabKey Server
#
run mkdir -p /labkey/
run mkdir -p /labkey/bin
run mkdir -p /labkey/svn

# Update the Ubuntu and install required tools
RUN (apt-get update; \
     apt-get -y -q install wget tar unzip subversion wget python-software-properties software-properties-common; \
     apt-get clean -y)

# Install Java
RUN echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true | debconf-set-selections && \
	add-apt-repository -y ppa:webupd8team/java && \
	apt-get update && \
	apt-get install -y oracle-java8-installer && \
	rm -rf /var/cache/oracle-jdk8-installer

#rm -rf /var/lib/apt/lists/* && \

ENV JAVA_HOME /usr/lib/jvm/java-8-oracle
ENV PATH $PATH:$JAVA_HOME/bin

# this is not the ideal way to install all these tools, but the code is currently shared by non-docker usages
RUN svn co --username cpas --password cpas --no-auth-cache https://hedgehog.fhcrc.org/tor/stedi/branches/${SVN_BRANCH}/externalModules/labModules/SequenceAnalysis/pipeline_code ${LK_HOME}/svn/
RUN ${LK_HOME}/svn/sequence_tools_install.sh -d ${LK_HOME}
RUN rm -Rf ${LK_HOME}/tool_src

# Clean up
RUN rm -rf /var/lib/apt/lists/*

#note: need to sort out users/permissions
#-u labkey
#chown -R labkey:labkey $LK_HOME


text:
import csv
import config
import os as os

# Read the metadata of LibriSpeech dataset
# Put them in a dictionnary with the following structure
# {
#  [speaker_key:{'sex':M/F, 'chapters':[(int), ...]}, ..., speaker_key:{..},]
# }
def read_metadata(subset_name=config.data_subset):
    metadata = {}
    path_speakers = os.path.join(config.workdir, 'data/LibriSpeech/SPEAKERS.TXT')
    path_chapters = os.path.join(config.workdir, 'data/LibriSpeech/CHAPTERS.TXT')

    # Read speakers information
    with open(path_speakers) as csvfile:
        spamreader = csv.reader(csvfile, delimiter='|')
        for row in spamreader:
            # It's a comment
            if row[0].startswith(';'):
                continue
            # Read only keys from the dataset subset
            if row[2].strip() == subset_name:
                # Add the sex information for this key
                metadata[row[0].strip()] = dict({'sex':row[1].strip()})
                # Create the chapters array for next file
                metadata[row[0].strip()]['chapters'] = []

    # read chapters information
    with open(path_chapters) as csvfile:
        spamreader = csv.reader(csvfile, delimiter='|')
        for row in spamreader:
            # It's a comment
        	if row[0].startswith(';'):
        		continue
            # Read only keys from the dataset subset
        	if row[3].strip() == subset_name:
                # Add all the chapters for this speaker
        		metadata[row[1].strip()]['chapters'].append(row[0].strip())

    return metadata

# Extract keys with the designated sex
def get_sex_subset(metadata, sex):
    return [k for k, v in metadata.items() if v['sex'] == sex]

# Extract males keys
def males_keys(metadata):
    return get_sex_subset(metadata, 'M')

# Extract females keys
def females_keys(metadata):
    return get_sex_subset(metadata, 'F')


text:
import yeboInput from 'yebo-ember-storefront/components/yebo-input';

export default yeboInput;


text:
#ifndef TENSORFLOW_CORE_FRAMEWORK_REF_VAR_H_
#define TENSORFLOW_CORE_FRAMEWORK_REF_VAR_H_

#include <functional>

namespace tensorflow {
class OpKernelContext;

void AssignRefVariable(
    OpKernelContext* context, int input_ref_index, int output_ref_index,
    int value_index, bool use_locking, bool validate_shape,
    bool relax_constraints,
    std::function<void(OpKernelContext*, Tensor*, const Tensor&)> copy);
}  //  end namespace tensorflow

#endif  // TENSORFLOW_CORE_FRAMEWORK_REF_VAR_H_


text:
package com.jat.netty.reconnect;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import java.util.concurrent.TimeUnit;

public class NettyClient {

    public String host = "127.0.0.1"; // ip地址
    public int port = 6789; // 端口
    // 通过nio方式来接收连接和处理连接
    private EventLoopGroup group = new NioEventLoopGroup();
    public static 	NettyClient nettyClient = new NettyClient();

    /**唯一标记 */
    private boolean initFalg=true;

    public static void main(String[] args) {
        nettyClient.run();
    }

    /**
     * Netty创建全部都是实现自AbstractBootstrap。 客户端的是Bootstrap,服务端的则是 ServerBootstrap。
     **/
    public void run() {
        doConnect(new Bootstrap(), group);
    }

    /**
     * 重连
     */
    public void doConnect(Bootstrap bootstrap, EventLoopGroup eventLoopGroup) {
        ChannelFuture f = null;
        try {
            if (bootstrap != null) {
                bootstrap.group(eventLoopGroup);
                bootstrap.channel(NioSocketChannel.class);
                bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
                bootstrap.handler(new NettyClientFilter());
                bootstrap.remoteAddress(host, port);
                f = bootstrap.connect().addListener((ChannelFuture futureListener) -> {
                    final EventLoop eventLoop = futureListener.channel().eventLoop();
                    if (!futureListener.isSuccess()) {
                        System.out.println("与服务端断开连接!在20s之后准备尝试重连!");
                        eventLoop.schedule(() -> doConnect(new Bootstrap(), eventLoop), 20, TimeUnit.SECONDS);
                    }
                });
                if(initFalg){
                    System.out.println("Netty客户端启动成功!");
                    initFalg=false;
                }
                Channel channel = f.sync().channel();
                channel.writeAndFlush("netty Hello");
                System.out.println("zzzz");
                // 阻塞
                f.channel().closeFuture().sync();
            }
        } catch (Exception e) {
            System.out.println("客户端连接失败!"+e.getMessage());
        }

    }
}


text:
/*
 * logsumexp2_api.c
 *
 * Code generation for function 'logsumexp2_api'
 *
 * C source code generated on: Tue Jan 14 10:28:31 2014
 *
 */

/* Include files */
#include "rt_nonfinite.h"
#include "logsumexp2.h"
#include "logsumexp2_api.h"
#include "logsumexp2_emxutil.h"

/* Variable Definitions */
static emlrtRTEInfo h_emlrtRTEI = { 1, 1, "logsumexp2_api", "" };

/* Function Declarations */
static void b_emlrt_marshallIn(const emlrtStack *sp, const mxArray *u, const
  emlrtMsgIdentifier *parentId, emxArray_real_T *y);
static void c_emlrt_marshallIn(const emlrtStack *sp, const mxArray *src, const
  emlrtMsgIdentifier *msgId, emxArray_real_T *ret);
static void emlrt_marshallIn(const emlrtStack *sp, const mxArray *a, const
  char_T *identifier, emxArray_real_T *y);
static const mxArray *emlrt_marshallOut(emxArray_real_T *u);

/* Function Definitions */
static void b_emlrt_marshallIn(const emlrtStack *sp, const mxArray *u, const
  emlrtMsgIdentifier *parentId, emxArray_real_T *y)
{
  c_emlrt_marshallIn(sp, emlrtAlias(u), parentId, y);
  emlrtDestroyArray(&u);
}

static void c_emlrt_marshallIn(const emlrtStack *sp, const mxArray *src, const
  emlrtMsgIdentifier *msgId, emxArray_real_T *ret)
{
  int32_T iv3[2];
  boolean_T bv0[2];
  int32_T i1;
  static const boolean_T bv1[2] = { FALSE, TRUE };

  int32_T iv4[2];
  for (i1 = 0; i1 < 2; i1++) {
    iv3[i1] = 1 + 9999999 * i1;
    bv0[i1] = bv1[i1];
  }

  emlrtCheckVsBuiltInR2012b(sp, msgId, src, "double", FALSE, 2U, iv3, bv0, iv4);
  ret->size[0] = iv4[0];
  ret->size[1] = iv4[1];
  ret->allocatedSize = ret->size[0] * ret->size[1];
  ret->data = (real_T *)mxGetData(src);
  ret->canFreeData = FALSE;
  emlrtDestroyArray(&src);
}

static void emlrt_marshallIn(const emlrtStack *sp, const mxArray *a, const
  char_T *identifier, emxArray_real_T *y)
{
  emlrtMsgIdentifier thisId;
  thisId.fIdentifier = identifier;
  thisId.fParent = NULL;
  b_emlrt_marshallIn(sp, emlrtAlias(a), &thisId, y);
  emlrtDestroyArray(&a);
}

static const mxArray *emlrt_marshallOut(emxArray_real_T *u)
{
  const mxArray *y;
  static const int32_T iv2[2] = { 0, 0 };

  const mxArray *m2;
  y = NULL;
  m2 = mxCreateNumericArray(2, (int32_T *)&iv2, mxDOUBLE_CLASS, mxREAL);
  mxSetData((mxArray *)m2, (void *)u->data);
  mxSetDimensions((mxArray *)m2, u->size, 2);
  emlrtAssign(&y, m2);
  return y;
}

void logsumexp2_api(emlrtStack *sp, const mxArray * const prhs[1], const mxArray
                    *plhs[1])
{
  emxArray_real_T *a;
  emxArray_real_T *z;
  emlrtHeapReferenceStackEnterFcnR2012b(sp);
  emxInit_real_T(sp, &a, 2, &h_emlrtRTEI, TRUE);
  emxInit_real_T(sp, &z, 2, &h_emlrtRTEI, TRUE);

  /* Marshall function inputs */
  emlrt_marshallIn(sp, emlrtAlias(prhs[0]), "a", a);

  /* Invoke the target function */
  logsumexp2(sp, a, z);

  /* Marshall function outputs */
  plhs[0] = emlrt_marshallOut(z);
  z->canFreeData = FALSE;
  emxFree_real_T(&z);
  a->canFreeData = FALSE;
  emxFree_real_T(&a);
  emlrtHeapReferenceStackLeaveFcnR2012b(sp);
}

/* End of code generation (logsumexp2_api.c) */


text:
/*jshint globalstrict:false, strict:false */
/*global assertTrue, assertEqual, ArangoAgency */
'use strict';

////////////////////////////////////////////////////////////////////////////////
/// @brief test moving shards in the cluster
///
/// DISCLAIMER
///
/// Copyright 2016-2016 ArangoDB GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
///     http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author Max Neunhoeffer
/// @author Copyright 2016, ArangoDB GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////

const jsunity = require("jsunity");

const arangodb = require("@arangodb");
const db = arangodb.db;
const _ = require("lodash");
const wait = require("internal").wait;
const supervisionState = require("@arangodb/cluster").supervisionState;
const deriveTestSuite = require('@arangodb/test-helper').deriveTestSuite;

// in the `useData` case, use this many documents:
const numDocuments = 1000;

function getDBServers() {
  var tmp = global.ArangoClusterInfo.getDBServers();
  var servers = [];
  for (var i = 0; i < tmp.length; ++i) {
    servers[i] = tmp[i].serverId;
  }
  return servers;
}

var servers = getDBServers();

////////////////////////////////////////////////////////////////////////////////
/// @brief test suite
////////////////////////////////////////////////////////////////////////////////

function MovingShardsSuite ({useData}) {
  if (typeof useData !== 'boolean') {
    throw new Error('MovingShardsSuite expects its parameter `useData` to be set and a boolean!');
  }
  var cn = "UnitTestMovingShards";
  var count = 0;
  var c = [];
  var dbservers = getDBServers();

////////////////////////////////////////////////////////////////////////////////
/// @brief find out servers for a collection
////////////////////////////////////////////////////////////////////////////////

  function findCollectionServers(database, collection) {
    var cinfo = global.ArangoClusterInfo.getCollectionInfo(database, collection);
    var shard = Object.keys(cinfo.shards)[0];
    return cinfo.shards[shard];
  }

////////////////////////////////////////////////////////////////////////////////
/// @brief wait for synchronous replication
////////////////////////////////////////////////////////////////////////////////

  function waitForSynchronousReplication(database) {
    console.info("Waiting for synchronous replication to settle...");
    global.ArangoClusterInfo.flush();
    for (var i = 0; i < c.length; ++i) {
      var cinfo = global.ArangoClusterInfo.getCollectionInfo(
        database, c[i].name());
      var shards = Object.keys(cinfo.shards);
      var replFactor = cinfo.shards[shards[0]].length;
      var count = 0;
      while (++count <= 180) {
        var ccinfo = shards.map(
          s => global.ArangoClusterInfo.getCollectionInfoCurrent(
            database, c[i].name(), s)
        );
        let replicas = ccinfo.map(s => s.servers.length);
        if (_.every(replicas, x => x === replFactor)) {
          console.info("Replication up and running!");
          break;
        }
        wait(0.5);
        global.ArangoClusterInfo.flush();
      }
      if (count > 120) {
        return false;
      }
    }
    return true;
  }

////////////////////////////////////////////////////////////////////////////////
/// @brief get cleaned out servers
////////////////////////////////////////////////////////////////////////////////

  function getCleanedOutServers() {
    var coordEndpoint =
        global.ArangoClusterInfo.getServerEndpoint("Coordinator0001");

    var request = require("@arangodb/request");
    var endpointToURL = require("@arangodb/cluster").endpointToURL;
    var url = endpointToURL(coordEndpoint);

    var res;
    try {
      var envelope =
          { method: "GET", url: url + "/_admin/cluster/numberOfServers" };
      res = request(envelope);
    } catch (err) {
      console.error(
        "Exception for GET /_admin/cluster/cleanOutServer:", err.stack);
      return {cleanedServers:[]};
    }
    if (res.statusCode !== 200) {
      return {cleanedServers:[]};
    }
    var body = res.body;
    if (typeof body === "string") {
      try {
        body = JSON.parse(body);
      } catch (err2) {
      }
    }
    if (typeof body !== "object" || !body.hasOwnProperty("cleanedServers") ||
        typeof body.cleanedServers !== "object") {
      return {cleanedServers:[]};
    }
    return body;
  }

////////////////////////////////////////////////////////////////////////////////
/// @brief display agency information in case of a bad outcome
////////////////////////////////////////////////////////////////////////////////

  function displayAgencyInformation() {
    var coordEndpoint =
        global.ArangoClusterInfo.getServerEndpoint("Coordinator0001");
    var request = require("@arangodb/request");
    var endpointToURL = require("@arangodb/cluster").endpointToURL;
    var url = endpointToURL(coordEndpoint);

    var res;
    try {
      var envelope = { method: "GET", url: url + "/_api/cluster/agency-dump" };
      res = request(envelope);
    } catch (err) {
      console.error(
        "Exception for GET /_api/cluster/agency-dump:", err.stack);
      return;
    }
    if (res.statusCode !== 200) {
      return;
    }
    var body = res.body;
    console.error("Agency state after disaster:", body);
  }

  function testServerNoLeader(id, fromCollNr, toCollNr) {
    if (fromCollNr === undefined) {
      fromCollNr = 0;
    }
    if (toCollNr === undefined) {
      toCollNr = c.length - 1;
    }
    for (var i = fromCollNr; i <= toCollNr; ++i) {
      global.ArangoClusterInfo.flush();
      var servers = findCollectionServers("_system", c[i].name());
      if (servers.indexOf(id) === 0 && servers.length !== 1) {
        return false;
      }

      // Now check current as well:
      var collInfo =
          global.ArangoClusterInfo.getCollectionInfo("_system", c[i].name());
      var shards = collInfo.shards;
      var collInfoCurr =
          Object.keys(shards).map(
            s => global.ArangoClusterInfo.getCollectionInfoCurrent(
              "_system", c[i].name(), s).servers);
      // quick hack here, if the list of servers has length one, we map it to -1
      // thus the check below will not return false in the case replFact === 1
      var idxs = collInfoCurr.map(l => (l.length > 1) ? l.indexOf(id) : -1);
      for (var j = 0; j < idxs.length; j++) {
        if (idxs[j] === 0) {
          return false;
        }
      }
    }
    return true;
  }

////////////////////////////////////////////////////////////////////////////////
/// @brief test whether or not a server is clean
////////////////////////////////////////////////////////////////////////////////

  function testServerEmpty(id, checkList, fromCollNr, toCollNr) {
    if (fromCollNr === undefined) {
      fromCollNr = 0;
    }
    if (toCollNr === undefined) {
      toCollNr = c.length - 1;
    }
    var count = 600;
    var ok = false;

    console.info("Waiting for server " + id + " to be cleaned out ...");

    if (checkList) {

      // Wait until the server appears in the list of cleanedOutServers:
      var obj;
      while (--count > 0) {
        obj = getCleanedOutServers();
        if (obj.cleanedServers.indexOf(id) >= 0) {
          ok = true;
          console.info(
            "Success: Server " + id + " cleaned out after " + (600-count) + " seconds");
          break;
        }
        wait(1.0);
      }

      if (!ok) {
        console.info(
          "Failed: Server " + id + " was not cleaned out. List of cleaned servers: ["
            + obj.cleanedServers + "]");
        displayAgencyInformation();
      }

    } else {

      for (var i = fromCollNr; i <= toCollNr; ++i) {

        while (--count > 0) {
          wait(1.0);
          global.ArangoClusterInfo.flush();
          var servers = findCollectionServers("_system", c[i].name());
          if (servers.indexOf(id) === -1) {
            // Now check current as well:
            var collInfo =
                global.ArangoClusterInfo.getCollectionInfo("_system", c[i].name());
            var shards = collInfo.shards;
            var collInfoCurr =
                Object.keys(shards).map(
                  s => global.ArangoClusterInfo.getCollectionInfoCurrent(
                    "_system", c[i].name(), s).servers);
            var idxs = collInfoCurr.map(l => l.indexOf(id));
            ok = true;
            for (var j = 0; j < idxs.length; j++) {
              if (idxs[j] !== -1) {
                ok = false;
              }
            }
            if (ok) {
              break;
            }
          }
        }
        if (!ok) {
          displayAgencyInformation();
          return false;
        }

      }

    }
    return ok;
  }

////////////////////////////////////////////////////////////////////////////////
/// @brief order the cluster to clean out a server:
////////////////////////////////////////////////////////////////////////////////

  function cleanOutServer(id) {
    var coordEndpoint =
        global.ArangoClusterInfo.getServerEndpoint("Coordinator0001");
    var request = require("@arangodb/request");
    var endpointToURL = require("@arangodb/cluster").endpointToURL;
    var url = endpointToURL(coordEndpoint);
    var body = {"server": id};
    var result;
    try {
      result = request({ method: "POST",
                         url: url + "/_admin/cluster/cleanOutServer",
                         body: JSON.stringify(body) });
    } catch (err) {
      console.error(
        "Exception for POST /_admin/cluster/cleanOutServer:", err.stack);
      return false;
    }
    console.info("cleanOutServer job:", JSON.stringify(body));
    console.info("result of request:", JSON.stringify(result.json));
    // Now wait until the job we triggered is finished:
    var count = 1200;   // seconds
    while (true) {
      var job = require("@arangodb/cluster").queryAgencyJob(result.json.id);
      console.info("Status of cleanOutServer job:", job.status);
      if (job.error === false && job.status === "Finished") {
        return result;
      }
      if (count-- < 0) {
        console.error(
          "Timeout in waiting for cleanOutServer to complete: "
          + JSON.stringify(body));
        return false;
      }
      require("internal").wait(1.0);
    }
  }

////////////////////////////////////////////////////////////////////////////////
/// @brief request a dbserver to resign:
////////////////////////////////////////////////////////////////////////////////

  function resignLeadership(id) {
    var coordEndpoint =
        global.ArangoClusterInfo.getServerEndpoint("Coordinator0001");
    var request = require("@arangodb/request");
    var endpointToURL = require("@arangodb/cluster").endpointToURL;
    var url = endpointToURL(coordEndpoint);
    var body = {"server": id};
    var result;
    try {
      result = request({ method: "POST",
                         url: url + "/_admin/cluster/resignLeadership",
                         body: JSON.stringify(body) });
    } catch (err) {
      console.error(
        "Exception for POST /_admin/cluster/resignLeadership:", err.stack);
      return false;
    }
    console.info("resignLeadership job:", JSON.stringify(body));
    console.info("result of request:", JSON.stringify(result.json));
    // Now wait until the job we triggered is finished:
    var count = 1200;   // seconds
    while (true) {
      var job = require("@arangodb/cluster").queryAgencyJob(result.json.id);
      console.info("Status of resignLeadership job:", job.status);
      if (job.error === false && job.status === "Finished") {
        return result;
      }
      if (count-- < 0) {
        console.error(
          "Timeout in waiting for resignLeadership to complete: "
          + JSON.stringify(body));
        return false;
      }
      require("internal").wait(1.0);
    }
  }

////////////////////////////////////////////////////////////////////////////////
/// @brief order the cluster to reduce number of db servers
////////////////////////////////////////////////////////////////////////////////

  function shrinkCluster(toNum) {
    var coordEndpoint =
        global.ArangoClusterInfo.getServerEndpoint("Coordinator0001");
    var request = require("@arangodb/request");
    var endpointToURL = require("@arangodb/cluster").endpointToURL;
    var url = endpointToURL(coordEndpoint);
    var body = {"numberOfDBServers":toNum};
    let res = {};
    try {
      res = request({ method: "PUT",
                      url: url + "/_admin/cluster/numberOfServers",
                      body: JSON.stringify(body) });
    } catch (err) {
      console.error(
        "Exception for PUT /_admin/cluster/numberOfServers:", err.stack);
      return false;
    }
    return res.hasOwnProperty("statusCode") && res.statusCode === 200;
  }

////////////////////////////////////////////////////////////////////////////////
/// @brief order the cluster to clean out a server:
////////////////////////////////////////////////////////////////////////////////

  function resetCleanedOutServers() {
    var coordEndpoint =
        global.ArangoClusterInfo.getServerEndpoint("Coordinator0001");
    var request = require("@arangodb/request");
    var endpointToURL = require("@arangodb/cluster").endpointToURL;
    var url = endpointToURL(coordEndpoint);
    var numberOfDBServers = servers.length;
    var body = {"cleanedServers":[], "numberOfDBServers":numberOfDBServers};
    try {
      var res = request({ method: "PUT",
                          url: url + "/_admin/cluster/numberOfServers",
                          body: JSON.stringify(body) });
      return res;
    }
    catch (err) {
      console.error(
        "Exception for PUT /_admin/cluster/numberOfServers:", err.stack);
      return false;
    }
  }

////////////////////////////////////////////////////////////////////////////////
/// @brief move a single shard
////////////////////////////////////////////////////////////////////////////////

  function moveShard(database, collection, shard, fromServer, toServer, dontwait) {
    var coordEndpoint =
        global.ArangoClusterInfo.getServerEndpoint("Coordinator0001");
    var request = require("@arangodb/request");
    var endpointToURL = require("@arangodb/cluster").endpointToURL;
    var url = endpointToURL(coordEndpoint);
    var body = {database, collection, shard, fromServer, toServer};
    var result;
    try {
      result = request({ method: "POST",
                         url: url + "/_admin/cluster/moveShard",
                         body: JSON.stringify(body) });
    } catch (err) {
      console.error(
        "Exception for PUT /_admin/cluster/moveShard:", err.stack);
      return false;
    }
    if (dontwait) {
      return result;
    }
    console.info("moveShard job:", JSON.stringify(body));
    console.info("result of request:", JSON.stringify(result.json));
    // Now wait until the job we triggered is finished:
    var count = 600;   // seconds
    while (true) {
      var job = require("@arangodb/cluster").queryAgencyJob(result.json.id);
      console.info("Status of moveShard job:", job.status);
      if (job.error === false && job.status === "Finished") {
        return result;
      }
      if (count-- < 0) {
        console.error(
          "Timeout in waiting for moveShard to complete: "
          + JSON.stringify(body));
        return false;
      }
      require("internal").wait(1.0);
    }
  }

////////////////////////////////////////////////////////////////////////////////
/// @brief Set supervision mode
////////////////////////////////////////////////////////////////////////////////

  function maintenanceMode(mode) {
    console.log("Switching supervision maintenance " + mode);
    var coordEndpoint =
        global.ArangoClusterInfo.getServerEndpoint("Coordinator0001");
    var request = require("@arangodb/request");
    var endpointToURL = require("@arangodb/cluster").endpointToURL;
    var url = endpointToURL(coordEndpoint);
    var req;
    try {
      req = request({ method: "PUT",
                      url: url + "/_admin/cluster/maintenance",
                      body: JSON.stringify(mode) });
    } catch (err) {
      console.error(
        "Exception for PUT /_admin/cluster/maintenance:", err.stack);
      return false;
    }
    console.log("Supervision maintenance is " + mode);
    return true;
  }

////////////////////////////////////////////////////////////////////////////////
/// @brief create some collections
////////////////////////////////////////////////////////////////////////////////

  function createSomeCollections(n, nrShards, replFactor, useData) {
    assertEqual('boolean', typeof useData);
    var systemCollServers = findCollectionServers("_system", "_graphs");
    console.info("System collections use servers:", systemCollServers);
    for (var i = 0; i < n; ++i) {
      ++count;
      while (true) {
        var name = cn + count;
        db._drop(name);
        var coll = db._create(name, {numberOfShards: nrShards,
                                     replicationFactor: replFactor,
                                     avoidServers: systemCollServers});

        if (useData) {
          // insert some documents
          coll.insert(_.range(0, numDocuments).map(v => ({ value: v, x: "someString" + v })));
        }

        var servers = findCollectionServers("_system", name);
        console.info("Test collection uses servers:", servers);
        if (_.intersection(systemCollServers, servers).length === 0) {
          c.push(coll);
          break;
        }
        console.info("Need to recreate collection to avoid system collection servers.");
        c.push(coll);
        waitForSynchronousReplication("_system");
        c.pop();
        console.info("Synchronous replication has settled, now dropping again.");
      }
    }
  }

  function checkCollectionContents() {
    const numDocs = useData ? numDocuments : 0;
    for(const collection of c) {
      assertEqual(numDocs, collection.count());
      const values = db._query(
        'FOR doc IN @@col SORT doc.value RETURN doc.value',
        { '@col': collection.name() }).toArray();
      for (const v of _.range(0, numDocs)) {
        assertEqual(v, values[v], values);
      }
    }
  }

////////////////////////////////////////////////////////////////////////////////
/// @brief find server not on a list
////////////////////////////////////////////////////////////////////////////////

  function findServerNotOnList(list) {
    var count = 0;
    while (list.indexOf(dbservers[count]) >= 0) {
      count += 1;
    }
    return dbservers[count];
  }

////////////////////////////////////////////////////////////////////////////////
/// @brief wait for Supervision to finish jobs
////////////////////////////////////////////////////////////////////////////////

  function waitForSupervision() {
    var count = 300;
    while (--count > 0) {
      var state = supervisionState();
      if (!state.error &&
          Object.keys(state.ToDo).length === 0 &&
          Object.keys(state.Pending).length === 0) {
        return true;
      }
      if (state.error) {
        console.warn("Waiting for supervision jobs to finish:",
                     "Currently no agency communication possible.");
      } else {
        console.info("Waiting for supervision jobs to finish:",
                     "ToDo jobs:", Object.keys(state.ToDo).length,
                     "Pending jobs:", Object.keys(state.Pending).length);
      }
      wait(1.0);
    }
    return false;
  }

////////////////////////////////////////////////////////////////////////////////
/// @brief the actual tests
////////////////////////////////////////////////////////////////////////////////

  return {

////////////////////////////////////////////////////////////////////////////////
/// @brief set up
////////////////////////////////////////////////////////////////////////////////

    setUp : function () {
      createSomeCollections(1, 1, 2, useData);
    },

////////////////////////////////////////////////////////////////////////////////
/// @brief tear down
////////////////////////////////////////////////////////////////////////////////

    tearDown : function () {
      for (var i = 0; i < c.length; ++i) {
        c[i].drop();
      }
      c = [];
      resetCleanedOutServers();
    },

////////////////////////////////////////////////////////////////////////////////
/// @brief check if a synchronously replicated collection gets online
////////////////////////////////////////////////////////////////////////////////

    testSetup : function () {
      for (var count = 0; count < 120; ++count) {
        dbservers = getDBServers();
        if (dbservers.length === 5) {
          assertTrue(waitForSynchronousReplication("_system"));
          checkCollectionContents();

          servers = dbservers;
          return;
        }
        console.log("Waiting for 5 dbservers to be present:", JSON.stringify(dbservers));
        wait(1.0);
      }
      assertTrue(false, "Timeout waiting for 5 dbservers.");
    },

////////////////////////////////////////////////////////////////////////////////
/// @brief cleaning out collection with one shard without replication
////////////////////////////////////////////////////////////////////////////////

    testShrinkNoReplication : function() {
      assertTrue(waitForSynchronousReplication("_system"));
      var _dbservers = servers;
      _dbservers.sort();
      assertTrue(shrinkCluster(4));
      assertTrue(testServerEmpty(_dbservers[4], true));
      assertTrue(waitForSupervision());
      assertTrue(shrinkCluster(3));
      assertTrue(testServerEmpty(_dbservers[3], true));
      assertTrue(waitForSupervision());
      assertTrue(shrinkCluster(2));
      assertTrue(testServerEmpty(_dbservers[2], true));
      assertTrue(waitForSupervision());
      checkCollectionContents();
    },

////////////////////////////////////////////////////////////////////////////////
/// @brief moving away a shard from a follower
////////////////////////////////////////////////////////////////////////////////

    testMoveShardFromFollower : function() {
      assertTrue(waitForSynchronousReplication("_system"));
      var servers = findCollectionServers("_system", c[0].name());
      var fromServer = servers[1];
      var toServer = findServerNotOnList(servers);
      var cinfo = global.ArangoClusterInfo.getCollectionInfo(
          "_system", c[0].name());
      var shard = Object.keys(cinfo.shards)[0];
      assertTrue(moveShard("_system", c[0]._id, shard, fromServer, toServer, false));
      assertTrue(testServerEmpty(fromServer), false);
      assertTrue(waitForSupervision());
      checkCollectionContents();
    },

////////////////////////////////////////////////////////////////////////////////
/// @brief moving away a shard from a leader
////////////////////////////////////////////////////////////////////////////////

    testMoveShardFromLeader : function() {
      assertTrue(waitForSynchronousReplication("_system"));
      var servers = findCollectionServers("_system", c[0].name());
      var fromServer = servers[0];
      var toServer = findServerNotOnList(servers);
      var cinfo = global.ArangoClusterInfo.getCollectionInfo(
          "_system", c[0].name());
      var shard = Object.keys(cinfo.shards)[0];
      assertTrue(moveShard("_system", c[0]._id, shard, fromServer, toServer, false));
      assertTrue(testServerEmpty(fromServer), false);
      assertTrue(waitForSupervision());
      checkCollectionContents();
    },

////////////////////////////////////////////////////////////////////////////////
/// @brief moving away a shard from a leader without replication
////////////////////////////////////////////////////////////////////////////////

    testMoveShardFromLeaderNoReplication : function() {
      createSomeCollections(1, 1, 1, useData);
      assertTrue(waitForSynchronousReplication("_system"));
      var servers = findCollectionServers("_system", c[1].name());
      var fromServer = servers[0];
      var toServer = findServerNotOnList(servers);
      var cinfo = global.ArangoClusterInfo.getCollectionInfo(
          "_system", c[1].name());
      var shard = Object.keys(cinfo.shards)[0];
      assertTrue(moveShard("_system", c[1]._id, shard, fromServer, toServer, false));
      assertTrue(testServerEmpty(fromServer, false, 1, 1));
      assertTrue(waitForSupervision());
      checkCollectionContents();
    },

////////////////////////////////////////////////////////////////////////////////
/// @brief moving away a shard from a follower with 3 replicas #1
////////////////////////////////////////////////////////////////////////////////

    testMoveShardFromFollowerRepl3_1 : function() {
      createSomeCollections(1, 1, 3, useData);
      assertTrue(waitForSynchronousReplication("_system"));
      var servers = findCollectionServers("_system", c[1].name());
      var fromServer = servers[1];
      var toServer = findServerNotOnList(servers);
      var cinfo = global.ArangoClusterInfo.getCollectionInfo(
          "_system", c[1].name());
      var shard = Object.keys(cinfo.shards)[0];
      assertTrue(moveShard("_system", c[1]._id, shard, fromServer, toServer, false));
      assertTrue(testServerEmpty(fromServer, false, 1, 1));
      assertTrue(waitForSupervision());
      checkCollectionContents();
    },

////////////////////////////////////////////////////////////////////////////////
/// @brief moving away a shard from a follower with 3 replicas #2
////////////////////////////////////////////////////////////////////////////////

    testMoveShardFromRepl3_2 : function() {
      createSomeCollections(1, 1, 3, useData);
      assertTrue(waitForSynchronousReplication("_system"));
      var servers = findCollectionServers("_system", c[1].name());
      var fromServer = servers[2];
      var toServer = findServerNotOnList(servers);
      var cinfo = global.ArangoClusterInfo.getCollectionInfo(
          "_system", c[1].name());
      var shard = Object.keys(cinfo.shards)[0];
      assertTrue(moveShard("_system", c[1]._id, shard, fromServer, toServer, false));
      assertTrue(testServerEmpty(fromServer, false, 1, 1));
      assertTrue(waitForSupervision());
      checkCollectionContents();
    },

////////////////////////////////////////////////////////////////////////////////
/// @brief moving away a shard from a leader with 3 replicas
////////////////////////////////////////////////////////////////////////////////

    testMoveShardFromLeaderRepl : function() {
      createSomeCollections(1, 1, 3, useData);
      assertTrue(waitForSynchronousReplication("_system"));
      var servers = findCollectionServers("_system", c[1].name());
      var fromServer = servers[0];
      var toServer = findServerNotOnList(servers);
      var cinfo = global.ArangoClusterInfo.getCollectionInfo(
          "_system", c[1].name());
      var shard = Object.keys(cinfo.shards)[0];
      assertTrue(moveShard("_system", c[1]._id, shard, fromServer, toServer, false));
      assertTrue(testServerEmpty(fromServer, false, 1, 1));
      assertTrue(waitForSupervision());
      checkCollectionContents();
    },

////////////////////////////////////////////////////////////////////////////////
/// @brief moving away a shard from a leader to a follower
////////////////////////////////////////////////////////////////////////////////

    testMoveShardFromLeaderToFollower : function() {
      createSomeCollections(1, 1, 3, useData);
      assertTrue(waitForSynchronousReplication("_system"));
      var servers = findCollectionServers("_system", c[1].name());
      var fromServer = servers[0];
      var toServer = servers[1];
      var cinfo = global.ArangoClusterInfo.getCollectionInfo(
          "_system", c[1].name());
      var shard = Object.keys(cinfo.shards)[0];
      assertTrue(moveShard("_system", c[1]._id, shard, fromServer, toServer, false));
      assertTrue(testServerNoLeader(fromServer, 1, 1));
      assertTrue(waitForSupervision());
      checkCollectionContents();
    },

////////////////////////////////////////////////////////////////////////////////
/// @brief resign leadership for a dbserver
////////////////////////////////////////////////////////////////////////////////

    testResignLeadership : function() {
      assertTrue(waitForSynchronousReplication("_system"));
      var servers = findCollectionServers("_system", c[0].name());
      var toResign = servers[1];
      assertTrue(resignLeadership(toResign));
      assertTrue(testServerNoLeader(toResign));
      assertTrue(waitForSupervision());
      checkCollectionContents();
    },

////////////////////////////////////////////////////////////////////////////////
/// @brief cleaning out a follower
////////////////////////////////////////////////////////////////////////////////

    testCleanOutFollower : function() {
      assertTrue(waitForSynchronousReplication("_system"));
      var servers = findCollectionServers("_system", c[0].name());
      var toClean = servers[1];
      assertTrue(cleanOutServer(toClean));
      assertTrue(testServerEmpty(toClean, true));
      assertTrue(waitForSupervision());
      checkCollectionContents();
    },

////////////////////////////////////////////////////////////////////////////////
/// @brief cleaning out a leader
////////////////////////////////////////////////////////////////////////////////

    testCleanOutLeader : function() {
      assertTrue(waitForSynchronousReplication("_system"));
      var servers = findCollectionServers("_system", c[0].name());
      var toClean = servers[0];
      assertTrue(cleanOutServer(toClean));
      assertTrue(testServerEmpty(toClean, true));
      assertTrue(waitForSupervision());
      checkCollectionContents();
    },

////////////////////////////////////////////////////////////////////////////////
/// @brief cleaning out with multiple collections
////////////////////////////////////////////////////////////////////////////////

    testCleanOutMultipleCollections : function() {
      createSomeCollections(10, 1, 2, useData);
      assertTrue(waitForSynchronousReplication("_system"));
      var servers = findCollectionServers("_system", c[1].name());
      var toClean = servers[0];
      assertTrue(cleanOutServer(toClean));
      assertTrue(testServerEmpty(toClean, true));
      assertTrue(waitForSupervision());
      checkCollectionContents();
    },

////////////////////////////////////////////////////////////////////////////////
/// @brief cleaning out with a collection with 3 replicas
////////////////////////////////////////////////////////////////////////////////

    testCleanOut3Replicas : function() {
      createSomeCollections(1, 1, 3, useData);
      assertTrue(waitForSynchronousReplication("_system"));
      var servers = findCollectionServers("_system", c[1].name());
      var toClean = servers[0];
      assertTrue(cleanOutServer(toClean));
      assertTrue(testServerEmpty(toClean, true));
      assertTrue(waitForSupervision());
      checkCollectionContents();
    },

////////////////////////////////////////////////////////////////////////////////
/// @brief cleaning out collection with multiple shards
////////////////////////////////////////////////////////////////////////////////

    testCleanOutMultipleShards : function() {
      createSomeCollections(1, 10, 2, useData);
      assertTrue(waitForSynchronousReplication("_system"));
      var servers = findCollectionServers("_system", c[1].name());
      var toClean = servers[1];
      assertTrue(cleanOutServer(toClean));
      assertTrue(testServerEmpty(toClean, true));
      assertTrue(waitForSupervision());
      checkCollectionContents();
    },

////////////////////////////////////////////////////////////////////////////////
/// @brief cleaning out collection with one shard without replication
////////////////////////////////////////////////////////////////////////////////

    testCleanOutNoReplication : function() {
      createSomeCollections(1, 1, 1, useData);
      assertTrue(waitForSynchronousReplication("_system"));
      var servers = findCollectionServers("_system", c[1].name());
      var toClean = servers[0];
      assertTrue(cleanOutServer(toClean));
      assertTrue(testServerEmpty(toClean, true));
      assertTrue(waitForSupervision());
      checkCollectionContents();
    },

////////////////////////////////////////////////////////////////////////////////
/// @brief pausing supervision for a couple of seconds
////////////////////////////////////////////////////////////////////////////////

    testMaintenanceMode : function() {
      createSomeCollections(1, 1, 3, useData);
      assertTrue(waitForSynchronousReplication("_system"));
      var servers = findCollectionServers("_system", c[1].name());
      var fromServer = servers[0];
      var toServer = findServerNotOnList(servers);
      var cinfo = global.ArangoClusterInfo.getCollectionInfo(
          "_system", c[1].name());
      var shard = Object.keys(cinfo.shards)[0];
      assertTrue(maintenanceMode("on"));
      assertTrue(moveShard("_system", c[1]._id, shard, fromServer, toServer, true));
      var first = global.ArangoAgency.transient([["/arango/Supervision/State"]])[0].
          arango.Supervision.State, state;
      var waitUntil = new Date().getTime() + 30.0*1000;
      while(true) {
        state = global.ArangoAgency.transient([["/arango/Supervision/State"]])[0].
          arango.Supervision.State;
        assertEqual(state.Timestamp, first.Timestamp);
        wait(5.0);
        if (new Date().getTime() > waitUntil) {
          break;
        }
      }
      assertTrue(maintenanceMode("off"));
      state = global.ArangoAgency.transient([["/arango/Supervision/State"]])[0].
        arango.Supervision.State;
      assertTrue(state.Timestamp !== first.Timestamp);
      assertTrue(testServerEmpty(fromServer, false, 1, 1));
      assertTrue(waitForSupervision());
      checkCollectionContents();
    },

  };
}

////////////////////////////////////////////////////////////////////////////////
/// @brief executes the test suite
////////////////////////////////////////////////////////////////////////////////

jsunity.run(function MovingShardsSuite_nodata() {
  let derivedSuite = {};
  deriveTestSuite(MovingShardsSuite({ useData: false }), derivedSuite, "_nodata");
  return derivedSuite;
});

jsunity.run(function MovingShardsSuite_data() {
  let derivedSuite = {};
  deriveTestSuite(MovingShardsSuite({ useData: true }), derivedSuite, "_data");
  return derivedSuite;
});

return jsunity.done();


text:
import datetime
import random

from splunk_eventgen.lib.generatorplugin import GeneratorPlugin
from splunk_eventgen.lib.logging_config import logger

class PerDayVolumeGenerator(GeneratorPlugin):
    def __init__(self, sample):
        GeneratorPlugin.__init__(self, sample)

    # TODO: Make this work with replay mode.
    def gen(self, count, earliest, latest, samplename=None):
        # count in this plugin is a measurement of byteself._sample.
        size = count
        logger.debug(
            "PerDayVolumeGenerator Called with a Size of: %s with Earliest: %s and Latest: %s"
            % (size, earliest, latest)
        )
        # very similar to the default generator.  only difference is we go by size instead of count.
        try:
            self._sample.loadSample()
            logger.debug("File sample loaded successfully.")
        except TypeError:
            logger.error(
                "Error loading sample file for sample '%s'" % self._sample.name
            )
            return

        logger.debug(
            "Generating sample '%s' in app '%s' with count %d, et: '%s', lt '%s'"
            % (self._sample.name, self._sample.app, size, earliest, latest)
        )
        startTime = datetime.datetime.now()

        # Pre-generate each event in the sample once, so we can replace tokens and calculate any volume differences
        preReplacementSize = sum(
            [len(event["_raw"]) for event in self._sample.sampleDict]
        )
        allEvents = self.replace_tokens(self._sample.sampleDict, earliest, latest)
        postReplacementSize = sum([len(event["_raw"]) for event in allEvents])
        replacementVolumeRatio = preReplacementSize / postReplacementSize
        size = size * replacementVolumeRatio
        logger.debug(
            "Token replacement size factor: {}, new interval target size: {}".format(
                replacementVolumeRatio, size
            )
        )
        del allEvents

        # Create a counter for the current byte size of the read in samples
        currentSize = 0

        # If we're random, fill random events from sampleDict into eventsDict
        eventsDict = []
        if self._sample.randomizeEvents:
            sdlen = len(self._sample.sampleDict)
            logger.debug(
                "Random filling eventsDict for sample '%s' in app '%s' with %d bytes"
                % (self._sample.name, self._sample.app, size)
            )
            while currentSize < size:
                currentevent = self._sample.sampleDict[random.randint(0, sdlen - 1)]
                eventsDict.append(currentevent)
                currentSize += len(currentevent["_raw"])

        # If we're bundlelines, create count copies of the sampleDict
        elif self._sample.bundlelines:
            logger.debug(
                "Bundlelines, filling eventsDict for sample '%s' in app '%s' with %d copies of sampleDict"
                % (self._sample.name, self._sample.app, size)
            )
            while currentSize <= size:
                sizeofsample = sum(
                    len(sample["_raw"]) for sample in self._sample.sampleDict
                )
                eventsDict.extend(self._sample.sampleDict)
                currentSize += sizeofsample

        # Otherwise fill count events into eventsDict or keep making copies of events out of sampleDict until
        # eventsDict is as big as count
        else:
            logger.debug("Simple replay in order, processing")
            # I need to check the sample and load events in order until the size is smaller than read events from file
            # or i've read the entire file.
            linecount = 0
            currentreadsize = 0
            linesinfile = len(self._sample.sampleDict)
            logger.debug("Lines in files: %s " % linesinfile)
            while currentreadsize <= size:
                targetline = linecount % linesinfile
                sizeremaining = size - currentreadsize
                targetlinesize = len(self._sample.sampleDict[targetline]["_raw"])
                if size < targetlinesize:
                    logger.error(
                        "Size is too small for sample {}. We need {} bytes but size of one event is {} bytes.".format(
                            self._sample.name, size, targetlinesize
                        )
                    )
                    break
                if targetlinesize <= sizeremaining:
                    currentreadsize += targetlinesize
                    eventsDict.append(self._sample.sampleDict[targetline])
                else:
                    break
                linecount += 1
            logger.debug(
                "Events fill complete for sample '%s' in app '%s' length %d"
                % (self._sample.name, self._sample.app, len(eventsDict))
            )

        # replace tokens and send events
        send_objects = self.replace_tokens(eventsDict, earliest, latest)
        del eventsDict
        self.send_events(send_objects, startTime)

def load():
    return PerDayVolumeGenerator


text:
#ifndef __DALI_OBJECT_REGISTRY_H__
#define __DALI_OBJECT_REGISTRY_H__

// INTERNAL INCLUDES
#include <dali/public-api/object/handle.h>
#include <dali/public-api/signals/dali-signal.h>

namespace Dali
{
/**
 * @addtogroup dali_core_object
 * @{
 */

namespace Internal DALI_INTERNAL
{
class ObjectRegistry;
}

/**
 * @brief The ObjectRegistry notifies it's observers when an object is created.
 *
 * Handle to the created Object is passed in the call back function.
 * The Handle is passed as Dali::Object handle, which can be DownCast
 * to the appropriate type.
 *
 * Care should be taken to not store the handle in the Observer, as this will
 * have adverse effect on the life time of the Internal Object. The Handle
 * should only be used to connect to signals
 *
 * Usage:
 * ObjectRegistry registry = Stage::GetObjectRegistry();
 * registry.ObjectCreatedSignal().Connect( ObjectCreatedCallbackFunc );
 *
 * Signals
 * | %Signal Name     | Method                       |
 * |------------------|------------------------------|
 * | objectCreated    | @ref ObjectCreatedSignal()   |
 * | objectDestroyed  | @ref ObjectDestroyedSignal() |
 * @SINCE_1_0.0
 */
class DALI_IMPORT_API ObjectRegistry : public BaseHandle
{
public:

  // Typedefs

  /**
   * @brief Object created signal
   * @SINCE_1_0.0
   */
  typedef Signal< void ( BaseHandle ) > ObjectCreatedSignalType;

  /**
   * @brief Object destroyed signal
   * @SINCE_1_0.0
   */
  typedef Signal< void ( const Dali::RefObject* ) > ObjectDestroyedSignalType;

  /**
   * @brief Allows the creation of an empty objectRegistry handle.
   *
   * To retrieve the current objectRegistry,
   * this handle can be set using Stage::GetCurrent().GetObjectRegistry().
   * @SINCE_1_0.0
   */
  ObjectRegistry();

  /**
   * @brief Destructor
   *
   * This is non-virtual since derived Handle types must not contain data or virtual methods.
   * @SINCE_1_0.0
   */
  ~ObjectRegistry();

  /**
   * @brief This copy constructor is required for (smart) pointer semantics.
   *
   * @SINCE_1_0.0
   * @param [in] handle A reference to the copied handle
   */
  ObjectRegistry(const ObjectRegistry& handle);

  /**
   * @brief This assignment operator is required for (smart) pointer semantics.
   *
   * @SINCE_1_0.0
   * @param [in] rhs  A reference to the copied handle
   * @return A reference to this
   */
  ObjectRegistry& operator=(const ObjectRegistry& rhs);

public: // Signals

  /**
   * @brief This signal is emitted when an object is created.
   *
   * A callback of the following type may be connected:
   * @code
   *   void YourCallbackName(BaseHandle object);
   * @endcode
   * @SINCE_1_0.0
   * @return The signal to connect to.
   * @pre The Object has been initialized.
   */
  ObjectCreatedSignalType& ObjectCreatedSignal();

  /**
   * @brief This signal is emitted when an object is destroyed.
   *
   * WARNING: Since this signal is emitted when the object is
   * in the process of being destroyed, the RefObject pointer
   * passed in the signal should not be modified in anyways.
   * And should NOT be used to create an handle. which will
   * affect the life time of this destroyed object and leads to
   * undefined behaviour.
   *
   * The only intended use is for Toolkit controls which want to
   * keep track of objects being created and destroyed for internal
   * bookkeeping.
   *
   * A callback of the following type may be connected:
   * @code
   *   void YourCallbackName(const Dali::RefObject* objectPointer);
   * @endcode
   * @SINCE_1_0.0
   * @return The signal to connect to.
   * @pre The Object has been initialized.
   */
  ObjectDestroyedSignalType& ObjectDestroyedSignal();

public: // Not intended for application developers

  /// @cond internal
  /**
   * @brief This constructor is used by Dali Get() method.
   *
   * @SINCE_1_0.0
   * @param [in] objectRegistry A pointer to a Dali resource
   */
  explicit DALI_INTERNAL ObjectRegistry(Internal::ObjectRegistry* objectRegistry);
  /// @endcond
};

/**
 * @}
 */
} // namespace Dali

#endif // __DALI_OBJECT_REGISTRY_H__


text:
/**
 * This script automatically creates a default Admin user when an
 * empty database is used for the first time. You can use this
 * technique to insert data into any List you have defined.
 */

exports.create = {
	User: [
		{ email: process.env.ADMIN_EMAIL, password: process.env.ADMIN_PASSWORD, name: { first: process.env.ADMIN_FIRSTNAME, last: process.env.ADMIN_LASTNAME }, isAdmin: true }
	]
};

/**
 * The following is the older version of this update script, it is
 * left here for reference as an example of how more complex updates
 * can be structured.
 */ //{ email: 'user@keystonejs.com', password: 'admin', name: { first: 'Admin', last: 'User' }, isAdmin: true }
/*
var keystone = require('keystone'),
	async = require('async'),
	User = keystone.list('User');

var admins = [
	{ email: 'user@keystonejs.com', password: 'admin', name: { first: 'Admin', last: 'User' } }
];

function createAdmin(admin, done) {

	var newAdmin = new User.model(admin);

	newAdmin.isAdmin = true;
	newAdmin.save(function(err) {
		if (err) {
			console.error("Error adding admin " + admin.email + " to the database:");
			console.error(err);
		} else {
			console.log("Added admin " + admin.email + " to the database.");
		}
		done(err);
	});

}

exports = module.exports = function(done) {
	async.forEach(admins, createAdmin, done);
};
*/


text:
package repmgrtests;

import com.sleepycat.db.Database;
import com.sleepycat.db.DatabaseConfig;
import com.sleepycat.db.DatabaseEntry;
import com.sleepycat.db.DatabaseType;
import com.sleepycat.db.Environment;
import com.sleepycat.db.EnvironmentConfig;
import com.sleepycat.db.EventHandlerAdapter;
import com.sleepycat.db.ReplicationHostAddress;
import com.sleepycat.db.ReplicationManagerAckPolicy;
import com.sleepycat.db.ReplicationManagerStartPolicy;
import com.sleepycat.db.ReplicationStats;
import com.sleepycat.db.ReplicationTimeoutType;
import com.sleepycat.db.StatsConfig;
import com.sleepycat.db.VerboseConfig;

import static org.junit.Assert.*;

import java.io.File;

public class V46impl implements TestMixedHeartbeats.Ops46, TestReverseConnect.Ops46 {
    private Config config;
    private Environment[] envs = new Environment[2];

    class MyEventHandler extends EventHandlerAdapter {
        private boolean done = false;
        private boolean panic = false;

        @Override
            synchronized public void handlePanicEvent() {
                done = true;
                panic = true;
                notifyAll();
            }

        @Override
            synchronized public void handleRepStartupDoneEvent() {
                done = true;
                notifyAll();
            }

        synchronized void await() throws Exception {
            while (!done) { wait(); }
            if (panic)
                throw new Exception("aborted by panic in DB");
        }
    }

    public void setConfig(Config c) { config = c; }

    public void createMaster(int site, boolean configureOther) throws Exception {
        EnvironmentConfig ec = makeBasicConfig();
        int p = config.getMyPort(site);
        ec.setReplicationManagerLocalSite(new ReplicationHostAddress("localhost", p));

        if (configureOther) {
            p = config.getOtherPort(site);
            ec.replicationManagerAddRemoteSite(new ReplicationHostAddress("localhost", p));
        }

        File masterDir = new File(config.getBaseDir(), "dir" + site);
        masterDir.mkdir();

        Environment master = new Environment(masterDir, ec);
        envs[site] = master;
        master.setReplicationTimeout(ReplicationTimeoutType.CONNECTION_RETRY,
                                     1000000); // be impatient
        master.replicationManagerStart(3, ReplicationManagerStartPolicy.REP_MASTER);

        DatabaseConfig dc = new DatabaseConfig();
        dc.setTransactional(true);
        dc.setAllowCreate(true);
        dc.setType(DatabaseType.BTREE);
        Database db = master.openDatabase(null, "test.db", null, dc);
        db.close();
    }

    public void establishClient(int site, boolean configureOther) throws Exception {
        EnvironmentConfig ec = makeBasicConfig();

        int p = config.getMyPort(site);
        ec.setReplicationManagerLocalSite(new ReplicationHostAddress("localhost", p));

        if (configureOther) {
            p = config.getOtherPort(site);
            ec.replicationManagerAddRemoteSite(new ReplicationHostAddress("localhost", p));
        }
        MyEventHandler monitor = new MyEventHandler();
        ec.setEventHandler(monitor);
        File clientDir = new File(config.getBaseDir(), "dir" + site);
        clientDir.mkdir();
        Environment client = new Environment(clientDir, ec);

        client.replicationManagerStart(3, ReplicationManagerStartPolicy.REP_CLIENT);
        monitor.await();

        assertTrue(client.getReplicationStats(StatsConfig.DEFAULT).getStartupComplete() == 1);

        client.close();
    }

    public void shutDown(int siteId) throws Exception {
        envs[siteId].close();
        envs[siteId] = null;
    }

    public void restart(int siteId) throws Exception {
        EnvironmentConfig ec = makeBasicConfig();
        int p = config.getMyPort(siteId);
        ec.setReplicationManagerLocalSite(new ReplicationHostAddress("localhost", p));

        p = config.getOtherPort(siteId);
        ec.replicationManagerAddRemoteSite(new ReplicationHostAddress("localhost", p));

        File dir = new File(config.getBaseDir(), "dir" + siteId);
        Environment e = new Environment(dir, ec);
        envs[siteId] = e;
        e.setReplicationTimeout(ReplicationTimeoutType.CONNECTION_RETRY,
                                     1000000); // be impatient
        e.replicationManagerStart(3, ReplicationManagerStartPolicy.REP_MASTER);
    }

    public void remove(int site) throws Exception {
        assertNull(envs[site]);

        EnvironmentConfig ec = makeBasicConfig();
        File dir = new File(config.getBaseDir(), "dir" + site);
        assertTrue(dir.exists());
        Environment.remove(dir, false, ec);
    }

    public boolean writeUpdates(int siteId, int txnCount) throws Exception {
        DatabaseConfig dc = new DatabaseConfig();
        dc.setTransactional(true);
        Database db = envs[siteId].openDatabase(null, "test.db", null, dc);

        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();
        value.setData("hello world".getBytes());
        for (int i=0; i<txnCount; i++) {
            String k = "The record number is: " + i;
            key.setData(k.getBytes());
            db.put(null, key, value);
        }

        db.close();
        return true;
    }

    public void checkMaxVersion() throws Exception {
        // For this test to make sense, we must be at version 4.5 or 4.6.
        //
        int major = Environment.getVersionMajor();
        int minor = Environment.getVersionMinor();
        assertTrue(major == 4 && (minor == 5 || minor == 6));
    }

    private EnvironmentConfig makeBasicConfig() {
        EnvironmentConfig ec = new EnvironmentConfig();
        ec.setAllowCreate(true);
        ec.setInitializeCache(true);
        ec.setInitializeLocking(true);
        ec.setInitializeLogging(true);
        ec.setInitializeReplication(true);
        ec.setTransactional(true);
        ec.setReplicationManagerAckPolicy(ReplicationManagerAckPolicy.ALL);
        ec.setRunRecovery(true);
        ec.setThreaded(true);
        ec.setReplicationNumSites(2);
        if (Boolean.getBoolean("VERB_REPLICATION"))
            ec.setVerbose(VerboseConfig.REPLICATION, true);
        return (ec);
    }

    public void restart(int siteId, File dir) throws Exception {
        throw new Exception("Not implemented");
    }
}


text:
package de.tomgrill.gdxtwitter.core;

import com.badlogic.gdx.net.HttpStatus;

public interface TwitterResponseListener {
	public void success(String data);

	public void apiError(HttpStatus response, String data);

	public void httpError(Throwable t);

	public void cancelled();
}


text:
namespace brotli {

void BuildHistograms(
    const std::vector<Command>& cmds,
    const BlockSplit& literal_split,
    const BlockSplit& insert_and_copy_split,
    const BlockSplit& dist_split,
    const uint8_t* ringbuffer,
    size_t pos,
    size_t mask,
    const std::vector<int>& context_modes,
    std::vector<HistogramLiteral>* literal_histograms,
    std::vector<HistogramCommand>* insert_and_copy_histograms,
    std::vector<HistogramDistance>* copy_dist_histograms) {
  BlockSplitIterator literal_it(literal_split);
  BlockSplitIterator insert_and_copy_it(insert_and_copy_split);
  BlockSplitIterator dist_it(dist_split);
  for (int i = 0; i < cmds.size(); ++i) {
    const Command &cmd = cmds[i];
    insert_and_copy_it.Next();
    (*insert_and_copy_histograms)[insert_and_copy_it.type_].Add(
        cmd.command_prefix_);
    for (int j = 0; j < cmd.insert_length_; ++j) {
      literal_it.Next();
      uint8_t prev_byte = pos > 0 ? ringbuffer[(pos - 1) & mask] : 0;
      uint8_t prev_byte2 = pos > 1 ? ringbuffer[(pos - 2) & mask] : 0;
      int context = (literal_it.type_ << kLiteralContextBits) +
          Context(prev_byte, prev_byte2, context_modes[literal_it.type_]);
      (*literal_histograms)[context].Add(ringbuffer[pos & mask]);
      ++pos;
    }
    pos += cmd.copy_length_;
    if (cmd.copy_length_ > 0 && cmd.distance_prefix_ != 0xffff) {
      dist_it.Next();
      int context = (dist_it.type_ << kDistanceContextBits) +
          ((cmd.copy_length_code_ > 4) ? 3 : cmd.copy_length_code_ - 2);
      (*copy_dist_histograms)[context].Add(cmd.distance_prefix_);
    }
  }
}

void BuildLiteralHistogramsForBlockType(
    const std::vector<Command>& cmds,
    const BlockSplit& literal_split,
    const uint8_t* ringbuffer,
    size_t pos,
    size_t mask,
    int block_type,
    int context_mode,
    std::vector<HistogramLiteral>* histograms) {
  BlockSplitIterator literal_it(literal_split);
  for (int i = 0; i < cmds.size(); ++i) {
    const Command &cmd = cmds[i];
    for (int j = 0; j < cmd.insert_length_; ++j) {
      literal_it.Next();
      if (literal_it.type_ == block_type) {
        uint8_t prev_byte = pos > 0 ? ringbuffer[(pos - 1) & mask] : 0;
        uint8_t prev_byte2 = pos > 1 ? ringbuffer[(pos - 2) & mask] : 0;
        int context = Context(prev_byte, prev_byte2, context_mode);
        (*histograms)[context].Add(ringbuffer[pos & mask]);
      }
      ++pos;
    }
    pos += cmd.copy_length_;
  }
}

}  // namespace brotli


text:
module OrigenTesters
  module SmartestBasedTester
    class V93K
      require 'origen_testers/smartest_based_tester/base/pattern_master'
      class PatternMaster < Base::PatternMaster
        TEMPLATE = "#{Origen.root!}/lib/origen_testers/smartest_based_tester/v93k/templates/template.pmfl.erb"
      end
    end
  end
end


text:
package vapourdrive.resourcefoundation.recipes;

import net.minecraft.block.Block;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.oredict.ShapedOreRecipe;
import net.minecraftforge.oredict.ShapelessOreRecipe;
import vapourdrive.resourcefoundation.blocks.Mod_Blocks;
import vapourdrive.resourcefoundation.config.ConfigOptions;
import vapourdrive.resourcefoundation.items.EnumExist;
import vapourdrive.resourcefoundation.items.EnumNew;
import vapourdrive.resourcefoundation.items.EnumOre;
import vapourdrive.resourcefoundation.items.Mod_Items;

public class Recipes
{

	public static void init()
	{
		AddFromStorageRecipes("ingot", Mod_Items.ItemNugget);
		AddFromStorageRecipes("block", Mod_Items.ItemIngot);
		AddToStoragerecipes("nugget", Mod_Items.ItemIngot);
		AddToStoragerecipes("ingot", Item.getItemFromBlock(Mod_Blocks.BlockStorage));
		AddToPlateRecipe("ingot", Mod_Items.ItemPlate);
		AddToGearRecipe("ingot", Mod_Items.ItemGear);
		AddDustSmeltRecipe(Mod_Items.ItemDust, Mod_Items.ItemIngot);
		AddOreSmeltingRecipe(Mod_Blocks.BlockOre, Mod_Items.ItemIngot);
		AddDustAlloyRecipe();
	}

	private static void AddDustAlloyRecipe()
	{
		GameRegistry.addRecipe(new ShapelessOreRecipe(new ItemStack(Mod_Items.ItemDust, 2, EnumNew.ELECTRUM.ordinal()), "dustSilver", "dustGold"));
		GameRegistry.addRecipe(new ShapelessOreRecipe(new ItemStack(Mod_Items.ItemDust, 4, EnumNew.BRONZE.ordinal()), "dustTin", "dustCopper", "dustCopper", "dustCopper"));
		GameRegistry.addRecipe(new ShapelessOreRecipe(new ItemStack(Mod_Items.ItemDust, 3, EnumNew.INVAR.ordinal()), "dustIron", "dustNickel", "dustNickel"));
		GameRegistry.addRecipe(new ShapelessOreRecipe(new ItemStack(Mod_Items.ItemDust, 4, EnumNew.BRASS.ordinal()), "dustCopper", "dustCopper", "dustCopper", "dustZinc"));

	}

	private static void AddOreSmeltingRecipe(Block blockOre, Item itemIngot)
	{
		tryRegisterSmelting(new ItemStack(blockOre, 1, EnumOre.ALUMINUM.ordinal()), new ItemStack(itemIngot, 1, EnumExist.ALUMINUM.ordinal()), 0.3f, ConfigOptions.enabledOres, EnumOre.ALUMINUM.ordinal(), ConfigOptions.enabledIngots, EnumExist.ALUMINUM.ordinal());
		tryRegisterSmelting(new ItemStack(blockOre, 1, EnumOre.COPPER.ordinal()), new ItemStack(itemIngot, 1, EnumExist.COPPER.ordinal()), 0.3f, ConfigOptions.enabledOres, EnumOre.COPPER.ordinal(), ConfigOptions.enabledIngots, EnumExist.COPPER.ordinal());
		tryRegisterSmelting(new ItemStack(blockOre, 1, EnumOre.TIN.ordinal()), new ItemStack(itemIngot, 1, EnumExist.TIN.ordinal()), 0.3f, ConfigOptions.enabledOres, EnumOre.TIN.ordinal(), ConfigOptions.enabledIngots, EnumExist.TIN.ordinal());
		tryRegisterSmelting(new ItemStack(blockOre, 1, EnumOre.SILVER.ordinal()), new ItemStack(itemIngot, 1, EnumExist.SILVER.ordinal()), 0.3f, ConfigOptions.enabledOres, EnumOre.SILVER.ordinal(), ConfigOptions.enabledIngots, EnumExist.SILVER.ordinal());
		tryRegisterSmelting(new ItemStack(blockOre, 1, EnumOre.LEAD.ordinal()), new ItemStack(itemIngot, 1, EnumExist.LEAD.ordinal()), 0.3f, ConfigOptions.enabledOres, EnumOre.LEAD.ordinal(), ConfigOptions.enabledIngots, EnumExist.LEAD.ordinal());
		tryRegisterSmelting(new ItemStack(blockOre, 1, EnumOre.NICKEL.ordinal()), new ItemStack(itemIngot, 1, EnumExist.NICKEL.ordinal()), 0.3f, ConfigOptions.enabledOres, EnumOre.NICKEL.ordinal(), ConfigOptions.enabledIngots, EnumExist.NICKEL.ordinal());
		tryRegisterSmelting(new ItemStack(blockOre, 1, EnumOre.PLATINUM.ordinal()), new ItemStack(itemIngot, 1, EnumExist.PLATINUM.ordinal()), 0.3f, ConfigOptions.enabledOres, EnumOre.PLATINUM.ordinal(), ConfigOptions.enabledIngots, EnumExist.PLATINUM.ordinal());
		tryRegisterSmelting(new ItemStack(blockOre, 1, EnumOre.ZINC.ordinal()), new ItemStack(itemIngot, 1, EnumExist.ZINC.ordinal()), 0.3f, ConfigOptions.enabledOres, EnumOre.ZINC.ordinal(), ConfigOptions.enabledIngots, EnumExist.ZINC.ordinal());

	}

	private static void AddDustSmeltRecipe(Item itemDust, Item itemIngot)
	{
		tryRegisterSmelting(new ItemStack(itemDust, 1, EnumNew.COPPER.ordinal()), new ItemStack(itemIngot, 1, EnumExist.COPPER.ordinal()), 0.3f, ConfigOptions.enabledDusts, EnumNew.COPPER.ordinal(), ConfigOptions.enabledIngots, EnumExist.COPPER.ordinal());
		tryRegisterSmelting(new ItemStack(itemDust, 1, EnumNew.TIN.ordinal()), new ItemStack(itemIngot, 1, EnumExist.TIN.ordinal()), 0.3f, ConfigOptions.enabledDusts, EnumNew.TIN.ordinal(), ConfigOptions.enabledIngots, EnumExist.TIN.ordinal());
		tryRegisterSmelting(new ItemStack(itemDust, 1, EnumNew.ALUMINUM.ordinal()), new ItemStack(itemIngot, 1, EnumExist.ALUMINUM.ordinal()), 0.3f, ConfigOptions.enabledDusts, EnumNew.ALUMINUM.ordinal(), ConfigOptions.enabledIngots, EnumExist.ALUMINUM.ordinal());
		tryRegisterSmelting(new ItemStack(itemDust, 1, EnumNew.IRON.ordinal()), new ItemStack(Items.iron_ingot), 0.3f, ConfigOptions.enabledDusts, EnumNew.IRON.ordinal());
		tryRegisterSmelting(new ItemStack(itemDust, 1, EnumNew.BRONZE.ordinal()), new ItemStack(itemIngot, 1, EnumExist.BRONZE.ordinal()), 0.3f, ConfigOptions.enabledDusts, EnumNew.BRONZE.ordinal(), ConfigOptions.enabledIngots, EnumExist.BRONZE.ordinal());
		tryRegisterSmelting(new ItemStack(itemDust, 1, EnumNew.LEAD.ordinal()), new ItemStack(itemIngot, 1, EnumExist.LEAD.ordinal()), 0.3f, ConfigOptions.enabledDusts, EnumNew.LEAD.ordinal(), ConfigOptions.enabledIngots, EnumExist.LEAD.ordinal());
		tryRegisterSmelting(new ItemStack(itemDust, 1, EnumNew.SILVER.ordinal()), new ItemStack(itemIngot, 1, EnumExist.SILVER.ordinal()), 0.3f, ConfigOptions.enabledDusts, EnumNew.SILVER.ordinal(), ConfigOptions.enabledIngots, EnumExist.SILVER.ordinal());
		tryRegisterSmelting(new ItemStack(itemDust, 1, EnumNew.GOLD.ordinal()), new ItemStack(Items.gold_ingot), 0.3f, ConfigOptions.enabledDusts, EnumNew.GOLD.ordinal());
		tryRegisterSmelting(new ItemStack(itemDust, 1, EnumNew.ELECTRUM.ordinal()), new ItemStack(itemIngot, 1, EnumExist.ELECTRUM.ordinal()), 0.3f, ConfigOptions.enabledDusts, EnumNew.ELECTRUM.ordinal(), ConfigOptions.enabledIngots, EnumExist.ELECTRUM.ordinal());
		tryRegisterSmelting(new ItemStack(itemDust, 1, EnumNew.NICKEL.ordinal()), new ItemStack(itemIngot, 1, EnumExist.NICKEL.ordinal()), 0.3f, ConfigOptions.enabledDusts, EnumNew.NICKEL.ordinal(), ConfigOptions.enabledIngots, EnumExist.NICKEL.ordinal());
		tryRegisterSmelting(new ItemStack(itemDust, 1, EnumNew.INVAR.ordinal()), new ItemStack(itemIngot, 1, EnumExist.INVAR.ordinal()), 0.3f, ConfigOptions.enabledDusts, EnumNew.INVAR.ordinal(), ConfigOptions.enabledIngots, EnumExist.INVAR.ordinal());
		tryRegisterSmelting(new ItemStack(itemDust, 1, EnumNew.PLATINUM.ordinal()), new ItemStack(itemIngot, 1, EnumExist.PLATINUM.ordinal()), 0.3f, ConfigOptions.enabledDusts, EnumNew.PLATINUM.ordinal(), ConfigOptions.enabledIngots, EnumExist.PLATINUM.ordinal());
		tryRegisterSmelting(new ItemStack(itemDust, 1, EnumNew.STEEL.ordinal()), new ItemStack(itemIngot, 1, EnumExist.STEEL.ordinal()), 0.3f, ConfigOptions.enabledDusts, EnumNew.STEEL.ordinal(), ConfigOptions.enabledIngots, EnumExist.STEEL.ordinal());
		tryRegisterSmelting(new ItemStack(itemDust, 1, EnumNew.ZINC.ordinal()), new ItemStack(itemIngot, 1, EnumExist.ZINC.ordinal()), 0.3f, ConfigOptions.enabledDusts, EnumNew.ZINC.ordinal(), ConfigOptions.enabledIngots, EnumExist.ZINC.ordinal());
		tryRegisterSmelting(new ItemStack(itemDust, 1, EnumNew.BRASS.ordinal()), new ItemStack(itemIngot, 1, EnumExist.BRASS.ordinal()), 0.3f, ConfigOptions.enabledDusts, EnumNew.BRASS.ordinal(), ConfigOptions.enabledIngots, EnumExist.BRASS.ordinal());

	}

	private static void tryRegisterSmelting(ItemStack itemStack, ItemStack itemStack2, float f, boolean[] enabledItems, int i)
	{
		if (enabledItems[i])
		{
			GameRegistry.addSmelting(itemStack, itemStack2, f);
		}
	}

	private static void tryRegisterSmelting(ItemStack itemStack, ItemStack itemStack2, float f, boolean[] enabledItems1, int j, boolean[] enabledItems, int i)
	{
		if (enabledItems[i] && enabledItems1[j])
		{
			GameRegistry.addSmelting(itemStack, itemStack2, f);
		}
	}

	private static void AddToGearRecipe(String string, Item item)
	{
		for (int i = 0; i < ConfigOptions.NewType.length; i++)
		{
			if (ConfigOptions.enabledGears[i])
			{
				IRecipe recipe = new ShapedOreRecipe(new ItemStack(item, 1, i), new Object[]
				{
						" i ", "iIi", " i ", 'i', string + ConfigOptions.NewType[i], 'I', "ingotIron"
				});

				GameRegistry.addRecipe(recipe);
			}
		}
	}

	private static void AddToPlateRecipe(String string, Item item)
	{
		for (int i = 0; i < ConfigOptions.NewType.length; i++)
		{
			if (ConfigOptions.enabledPlates[i])
			{
				IRecipe recipe = new ShapedOreRecipe(new ItemStack(item, 2, i), new Object[]
				{
						"iii", 'i', string + ConfigOptions.NewType[i]
				});
				GameRegistry.addRecipe(recipe);
			}
		}
	}

	private static void AddToStoragerecipes(String string, Item item)
	{
		for (int i = 0; i < ConfigOptions.ExistingType.length; i++)
		{
			if (ConfigOptions.enabledStorageBlocks[i] && ConfigOptions.enabledIngots[i])
			{
				IRecipe recipe = new ShapedOreRecipe(new ItemStack(item, 1, i), new Object[]
				{
						"iii", "iii", "iii", 'i', string + ConfigOptions.ExistingType[i]
				});
				GameRegistry.addRecipe(recipe);
			}
		}
	}

	private static void AddFromStorageRecipes(String string, Item item)
	{
		for (int i = 0; i < ConfigOptions.ExistingType.length; i++)
		{
			if (ConfigOptions.enabledIngots[i] && ConfigOptions.enabledStorageBlocks[i])
			{
				IRecipe recipe = new ShapelessOreRecipe(new ItemStack(item, 9, i), new Object[]
				{
					string + ConfigOptions.ExistingType[i]
				});
				GameRegistry.addRecipe(recipe);
			}
		}

	}
}


text:
"""
You can set APP_CONFIG to point to this file to enable automatic reloading of
modules.
"""

reload = True


text:
"""
.. module:: skrf.time
========================================
time (:mod:`skrf.time`)
========================================

Time domain functions

.. autosummary::
   :toctree: generated/

   time_gate
   detect_span
   find_n_peaks
   indexes

"""
from .util import find_nearest_index
from scipy.ndimage import convolve1d
from scipy import signal
import numpy as npy
from numpy import fft
from typing import List

def indexes(y: npy.ndarray, thres: float = 0.3, min_dist: int = 1) -> npy.ndarray:
    """
    Peak detection routine.

    Finds the numeric index of the peaks in *y* by taking its first order difference. By using
    *thres* and *min_dist* parameters, it is possible to reduce the number of
    detected peaks. *y* must be signed.

    Parameters
    ----------
    y : ndarray (signed)
        1D amplitude data to search for peaks.
    thres : float between [0., 1.], optional
        Normalized threshold. Only the peaks with amplitude higher than the
        threshold will be detected. Default is 0.3
    min_dist : int, optional
        Minimum distance between each detected peak. The peak with the highest
        amplitude is preferred to satisfy this constraint. Default is 1

    Returns
    -------
    ndarray
        Array containing the numeric indexes of the peaks that were detected

    Notes
    -----
    This function was taken from peakutils-1.1.0
    http://pythonhosted.org/PeakUtils/index.html

    """
    #This function was taken from peakutils, and is covered
    # by the MIT license, included below:

    #The MIT License (MIT)

    #Copyright (c) 2014 Lucas Hermann Negri

    #Permission is hereby granted, free of charge, to any person obtaining a copy
    #of this software and associated documentation files (the "Software"), to deal
    #in the Software without restriction, including without limitation the rights
    #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    #copies of the Software, and to permit persons to whom the Software is
    #furnished to do so, subject to the following conditions:

    #The above copyright notice and this permission notice shall be included in
    #all copies or substantial portions of the Software.

    #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    #THE SOFTWARE.

    if isinstance(y, npy.ndarray) and npy.issubdtype(y.dtype, npy.unsignedinteger):
        raise ValueError("y must be signed")

    thres = thres * (npy.max(y) - npy.min(y)) + npy.min(y)
    min_dist = int(min_dist)

    # compute first order difference
    dy = npy.diff(y)

    # propagate left and right values successively to fill all plateau pixels (0-value)
    zeros, = npy.where(dy == 0)

    # check if the singal is totally flat
    if len(zeros) == len(y) - 1:
        return npy.array([])

    while len(zeros):
        # add pixels 2 by 2 to propagate left and right value onto the zero-value pixel
        zerosr = npy.hstack([dy[1:], 0.])
        zerosl = npy.hstack([0., dy[:-1]])

        # replace 0 with right value if non zero
        dy[zeros]=zerosr[zeros]
        zeros, = npy.where(dy == 0)

        # replace 0 with left value if non zero
        dy[zeros] = zerosl[zeros]
        zeros, = npy.where(dy == 0)

    # find the peaks by using the first order difference
    peaks = npy.where((npy.hstack([dy, 0.]) < 0.)
                     & (npy.hstack([0., dy]) > 0.)
                     & (y > thres))[0]

    # handle multiple peaks, respecting the minimum distance
    if peaks.size > 1 and min_dist > 1:
        highest = peaks[npy.argsort(y[peaks])][::-1]
        rem = npy.ones(y.size, dtype=bool)
        rem[peaks] = False

        for peak in highest:
            if not rem[peak]:
                sl = slice(max(0, peak - min_dist), peak + min_dist + 1)
                rem[sl] = True
                rem[peak] = False

        peaks = npy.arange(y.size)[~rem]

    return peaks

def find_n_peaks(x: npy.ndarray, n: int, thres: float = 0.9, **kwargs) -> List[int]:
    """
    Find a given number of peaks in a signal.

    Parameters
    ----------
    x : npy.ndarray
        signal
    n : int
        number of peaks to search for
    thres : float, optional
        threshold, default is 0.9
    **kwargs : optional keyword arguments passed to :func:`indexes`

    Returns
    -------
    peak_idxs : list of int
        List containing the numeric indexes of the peaks that were detected

    Raises
    ------
    ValueError
        If no peaks are found.
    """
    for dummy in range(10):

        idx = indexes(x, **kwargs)
        if len(idx) < n:
            thres *= .5

        else:
            peak_vals = sorted(x[idx], reverse=True)[:n]
            peak_idxs = [x.tolist().index(k) for k in peak_vals]

            return peak_idxs
    raise ValueError('Couldnt find %i peaks' % n)

def detect_span(ntwk) -> float:
    """
    Detect the correct time-span between two largest peaks.

    Parameters
    ----------
    ntwk : :class:`~skrf.network.Network`
        network to get data from

    Returns
    -------
    span : float
    """
    x = ntwk.s_time_db.flatten()
    p1, p2 = find_n_peaks(x, n=2)
    # distance to nearest neighbor peak
    span = abs(ntwk.frequency.t_ns[p1]-ntwk.frequency.t_ns[p2])
    return span

def time_gate(ntwk, start: float = None, stop: float = None, center: float = None, span: float = None,
              mode: str = 'bandpass', window=('kaiser', 6), media=None,
              boundary: str = 'reflect', return_all: bool = False):
    """
    Time-gate one-port s-parameters.

    The gate can be defined with start/stop times, or by
    center/span. all times are in units of nanoseconds. common
    windows are:

    * ('kaiser', 6)
    * 6 # integers are interpreted as kaiser beta-values
    * 'hamming'
    * 'boxcar'  # a straight up rect

    If no parameters are passed this will try to auto-gate the largest
    peak.

    Parameters
    ----------
    ntwk : :class:`~skrf.network.Network`
        network to operate on
    start : number, or None
        start of time gate, (ns).
    stop : number, or None
        stop of time gate (ns).
    center : number, or None
        center of time gate, (ns). If None, and span is given,
        the gate will be centered on the peak.
    span : number, or None
        span of time gate, (ns).  If None span will be half of the
        distance to the second tallest peak
    mode: ['bandpass','bandstop']
        mode of gate
    boundary:  {'reflect', 'constant', 'nearest', 'mirror', 'wrap'},
        passed to `scipy.ndimage.filters.convolve1d`
    window : string, float, or tuple
        passed to `window` arg of `scipy.signal.get_window`

    Note
    ----
    You cant gate things that are 'behind' strong reflections. This
    is due to the multiple reflections that occur.

    If `center!=0`, then the ntwk's time response is shifted
    to t=0, gated, then shifted back to where it was. This is
    done in frequency domain using `ntwk.delay()`. If the media being
    gated is dispersive (ie waveguide), then the gate `span` will be
    span at t=0, which is different.

    If you need to time-gate an N-port network, then you should
    gate each s-parameter independently.

    Returns
    -------
    ntwk : Network
        copy of ntwk with time-gated s-parameters

    .. warning::
        Depending on sharpness of the gate, the band edges may be
        inaccurate, due to properties of FFT. We do not re-normalize
        anything.

    """
    if ntwk.nports >1:
        raise ValueError('Time-gating only works on one-ports. try taking ntwk.s11 or ntwk.s21 first')

    if start is not None and stop is not None:
        start *= 1e-9
        stop *= 1e-9
        span = abs(stop-start)
        center = (stop+start)/2.

    else:
        if center is None:
            # they didnt provide center, so find the peak
            n = ntwk.s_time_mag.argmax()
            center = ntwk.frequency.t_ns[n]

        if span is None:
            span = detect_span(ntwk)

        center *= 1e-9
        span *= 1e-9
        start = center - span / 2.
        stop = center + span / 2.

    # find start/stop gate indices
    t = ntwk.frequency.t
    start_idx = find_nearest_index(t, start)
    stop_idx = find_nearest_index(t, stop)

    # create window
    window_width = abs(stop_idx - start_idx)
    window = signal.get_window(window, window_width)

    # create the gate by padding the window with zeros
    gate = npy.r_[npy.zeros(start_idx),
                           window,
                           npy.zeros(len(t) - stop_idx)]

    #FFT the gate, so we have it's frequency response, aka kernel
    kernel=fft.ifftshift(fft.fft(fft.fftshift(gate, axes=0), axis=0))
    kernel =abs(kernel).flatten() # take mag and flatten
    kernel=kernel/sum(kernel) # normalize kernel

    out = ntwk.copy()

    # conditionally delay ntwk, to center at t=0, this is
    # equivalent to gating at center.  (this is probably very inefficient)
    if center!=0:
        out = out.delay(-center*1e9, 'ns',port=0,media=media)

    # waste of code to handle convolve1d suck
    re = out.s_re[:,0,0]
    im = out.s_im[:,0,0]
    s = convolve1d(re,kernel, mode=boundary)+\
     1j*convolve1d(im,kernel, mode=boundary)
    out.s[:,0,0] = s
    # conditionally  un-delay ntwk
    if center!=0:
        out = out.delay(center*1e9, 'ns',port=0,media=media)

    if mode == 'bandstop':
        out = ntwk-out
    elif mode=='bandpass':
        pass
    else:
        raise ValueError('mode should be \'bandpass\' or \'bandstop\'')

    if return_all:
        # compute the gate ntwk and add delay
        gate_ntwk = out.s11.copy()
        gate_ntwk.s = kernel
        gate_ntwk= gate_ntwk.delay(center*1e9, 'ns', media=media)

        return {'gated_ntwk':out,
                'gate':gate_ntwk}
    else:
        return out