text:
""" Hybrid Authentication Module - Wraps BasicAuthAuthenticationPolicy and
    AuthTktAuthenticationPolicy to provide an authentication system whereby a
    user can authenticate on any view with Basic Auth, but will then receive a
    cookie with an auth ticket, and be able to use that for subsequent
    requests.

    Basically, with this scheme, every page becomes a login page.  The
    advantage is that scripts can use BasicAuth which is super-siple,
    but templates and JavaScript can use a Cookie which is faster and simpler
    for them.
"""

from pyramid.authentication import (BasicAuthAuthenticationPolicy,
                                    AuthTktAuthenticationPolicy)
from pyramid.httpexceptions import HTTPUnauthorized, HTTPRequestTimeout
from pyramid.security import remember

from eos_db import server

import warnings
import logging
log = logging.getLogger(__name__)

def add_cookie_callback(event):
    """ Add a cookie containing a security token to all response headers.
        This should be added to the configurator as a subscriber in addition to
        setting the authentication_policy.
    """

    #Suppress this warning which I already know about.  Note this sets the global
    #warnings filter so it's something of a nasty side-effect.
    warnings.filterwarnings("ignore", r'Behavior of MultiDict\.update\(\) has changed')
    def cookie_callback(request, response):
        """ Cookie callback. """

        if response.status[0] == '2':
             response.headers.update(remember(request,
                                              request.authenticated_userid))

    event.request.add_response_callback(cookie_callback)

class HybridAuthenticationPolicy():
    """ HybridAuthenticationPolicy. Called in the same way as other auth
        policies, but wraps Basic and AuthTkt.
        This policy also caches password lookups by remembering them in the
        request object.
    """

    def __init__(self, secret, realm='Realm', hardcoded=()):
        """ We need to initialise variables here for both forms of auth which
            we're planning on using.
            :param secret: A hashing secret for AuthTkt, which should be generated outside
                           the Pyhton process.
            :param realm: The Basic Auth realm which is probably set to eos_db.
            :param hardcoded: Triplets of user:password:group that should not be looked
                              up in the database.
        """
        self.hardcoded = { x[0]: (x[1],x[2]) for x in hardcoded }

        #DELETE ME
        #self.check = check   # Password check routine passed to the constructor.
        #self.realm = realm   # Basic Auth realm.

        # Now initialise both Auth Policies. AuthTkt has sha256 specified in
        # place of the default MD5 in order to suppress warnings about
        # security.
        self.bap = BasicAuthAuthenticationPolicy(check=self.passwordcheck,
                                                 realm=realm)
        self.tap = AuthTktAuthenticationPolicy(secret=secret,
                                               callback=self.groupfinder,
                                               cookie_name='auth_tkt',
                                               hashalg='sha256')

    #Utility functions to interact with eos_db.server
    def groupfinder(self, username, request):
        """ Return the user group (just one) associated with the user. This uses a server
            function to check which group a user has been associated with.
            This provides the standard callback wanted by AuthTktAuthenticationPolicy.
            An alternative would be to encode the groups in the Tkt.
            The mapping of groups to actual capabilities is stored in views.PermissionsMap
            """

        group = server.get_user_group(username)
        if group:
            return ["group:" + str(group)]

    def passwordcheck(self, login, password, request):
            """Password checking callback.
            """

            hc = self.hardcoded

            if login in hc and  hc[login][0] == password:
                    return ['group:' + hc[login][1]]

            elif server.check_password(login, password):
                user_group = server.get_user_group(login)
                log.debug("Found user group %s" % user_group)
                return ['group:' + user_group]

            else:
                log.debug("Password chack failed for user %s" % login)
                return None

    def unauthenticated_userid(self, request):
        """ Return the userid parsed from the auth ticket cookie. If this does
            not exist, then check the basic auth header, and return that, if it
            exists.
        """
        #Allow forcing the auth_tkt cookie.  Helpful for JS calls.
        #Maybe move this to a callback so it only ever happens once?
        if request.headers.get('auth_tkt'):
            request.cookies['auth_tkt'] = request.headers['auth_tkt']

        #Or, surely:
        return ( self.tap.unauthenticated_userid(request) or
                 self.bap.unauthenticated_userid(request) )

    def authenticated_userid(self, request):
        """ Return the Auth Ticket user ID if that exists. If not, then check
            for a user ID in Basic Auth.
        """
        try:
            return request.cached_authenticated_userid
        except:
            #Proceed to look-up then
            pass

        #Allow forcing the auth_tkt cookie.
        if request.headers.get('auth_tkt'):
            request.cookies['auth_tkt'] = request.headers['auth_tkt']

        request.cached_authenticated_userid = ( self.tap.unauthenticated_userid(request) or
                                                self.bap.unauthenticated_userid(request) )
        return request.cached_authenticated_userid

    def effective_principals(self, request):
        """ Returns the list of effective principles from the auth policy
        under which the user is currently authenticated. Auth ticket takes
        precedence. """

        try:
            return request.cached_effective_principals
        except:
            #Proceed to look-up then
            pass

        #Allow forcing the auth_tkt cookie.
        if request.headers.get('auth_tkt'):
            request.cookies['auth_tkt'] = request.headers['auth_tkt']

        userid = self.tap.authenticated_userid(request)
        if userid:
            request.cached_effective_principals = self.tap.effective_principals(request)
        else:
            request.cached_effective_principals = self.bap.effective_principals(request)

        return request.cached_effective_principals

    def remember(self, request, principal, **kw):
        """Causes the session info to be remembered by passing the appropriate
           AuthTkt into the response.
        """
        # We always rememeber by creating an AuthTkt, but only if there is something to remember
        # and if the user was not in the hard-coded list.
        if principal and principal not in self.hardcoded:
            return self.tap.remember(request, principal, **kw)
        else:
            return ()

    def forget(self, request):
        """ Forget both sessions. """

        return self.bap.forget(request) + self.tap.forget(request)

    def get_forbidden_view(self, request):
        """ Fire a 401 when authentication needed. """

        # FIXME - this doesn't distinguish between unauthenticated and
        # unauthorized.  Should it?
        if request.headers.get('auth_tkt'):
            return HTTPRequestTimeout()

        #print ("Access Forbidden")
        response = HTTPUnauthorized()
        response.headers.extend(self.bap.forget(request))
        return response


text:
var Q = require('q');
var _ = require('lodash');
var hue = require("node-hue-api"),
    HueApi = hue.HueApi,
    lightState = hue.lightState;

module.exports = function(options) {
    var api = new HueApi(options.hostname, options.username);

    var exec = function(alert, alertConfig, e) {
        var stateOn = lightState.create().on().white(500, 100);
        var stateOff = lightState.create().off();

        return Q()
        .then(function() {
            return api.setLightState(alertConfig.light, stateOff);
        })
        .delay(2000)
        .then(function() {
            return api.setLightState(alertConfig.light, stateOn);
        })
        .delay(2000)
        .then(function() {
            return api.setLightState(alertConfig.light, stateOff);
        })
    };

    return {
        id: "hue",
        title: "Philips Hue",
        execute: exec,
        options: {
            light: {
                type: "number",
                label: "Light",
                help: "Light ID"
            }
        }
    };
};

redpajama_set_name: RedPajamaGithub


text:
/* JExtensions v1.0.0 | (c) 2015 Avalon. | Licensed under the MIT license. */
if (!Date.prototype.equal) {
    Date.prototype.equal = function (date) {
        /// Compare if the dates are equal or not.
        /// <param name="date" type="Date">Date to compare.</param>
        /// <returns type="Boolean">Flag that indicates whether the dates are the same.</returns>
        if (typeof (date) === 'string') {
            return this.getTime() == (new Date(date).getTime());
        }
        return this.getTime() == date.getTime();
    }
}

if (!Date.prototype.isDateInTheFuture) {
    Date.prototype.isDateInTheFuture = function () {
        /// Determine if the date is in the future.
        /// <returns type="Boolean">Flag that indicates whether the date is in the future.</returns>
        var today = new Date();
        return this > today;
    }
}

if (!Date.prototype.isDateInThePast) {
    Date.prototype.isDateInThePast = function () {
        /// Determine if the date is in the past.
        /// <returns type="Boolean">Flag that indicates whether the date is in the past.</returns>
        var today = new Date();
        return this < today;
    }
}


text:
using System;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.ComponentModel.Design;
using EnvDTE;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;

namespace LookHub
{
    /// <summary>
    /// This is the class that implements the package exposed by this assembly.
    ///
    /// The minimum requirement for a class to be considered a valid package for Visual Studio
    /// is to implement the IVsPackage interface and register itself with the shell.
    /// This package uses the helper classes defined inside the Managed Package Framework (MPF)
    /// to do it: it derives from the Package class that provides the implementation of the
    /// IVsPackage interface and uses the registration attributes defined in the framework to
    /// register itself and its components with the shell.
    /// </summary>
    // This attribute tells the PkgDef creation utility (CreatePkgDef.exe) that this class is
    // a package.
    [PackageRegistration(UseManagedResourcesOnly = true)]
    // This attribute is used to register the informations needed to show the this package
    // in the Help/About dialog of Visual Studio.
    [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
    // This attribute is needed to let the shell know that this package exposes some menus.
    [ProvideMenuResource("Menus.ctmenu", 1)]
    [Guid(GuidList.guidLookHubPkgString)]
    public sealed class LookHubPackage : Package
    {
        /// <summary>
        /// Default constructor of the package.
        /// Inside this method you can place any initialization code that does not require
        /// any Visual Studio service because at this point the package object is created but
        /// not sited yet inside Visual Studio environment. The place to do all the other
        /// initialization is the Initialize method.
        /// </summary>
        public LookHubPackage()
        {
            Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", this.ToString()));
        }

        /////////////////////////////////////////////////////////////////////////////
        // Overriden Package Implementation
        #region Package Members

        /// <summary>
        /// Initialization of the package; this method is called right after the package is sited, so this is the place
        /// where you can put all the initilaization code that rely on services provided by VisualStudio.
        /// </summary>
        protected override void Initialize()
        {
            Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));
            base.Initialize();

            // Add our command handlers for menu (commands must exist in the .vsct file)
            OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
            if (null != mcs)
            {
                // Create the command for the menu item.
                CommandID showInGithubCommandId = new CommandID(GuidList.guidLookHubCmdSet, (int)PkgCmdIDList.cmdidShowInGithubCommand);
                MenuCommand showInGithubCommand = new MenuCommand(SowInGithubCallback, showInGithubCommandId);
                mcs.AddCommand(showInGithubCommand);

                CommandID copyGithubLinkCommandId = new CommandID(GuidList.guidLookHubCmdSet, (int)PkgCmdIDList.cmdidCopyGithubLinkCommand);
                MenuCommand copyGithubLinkCommand = new MenuCommand(CopyGithubLinkCallback, copyGithubLinkCommandId);
                mcs.AddCommand(copyGithubLinkCommand);
            }
        }
        #endregion

        private static T GetService<T>() where T : class
        {
            return (T)ServiceProvider.GlobalProvider.GetService(typeof(T));
        }

        /// <summary>
        /// This function is the callback used to execute a command when the a menu item is clicked.
        /// See the Initialize method to see how the menu item is associated to this function using
        /// the OleMenuCommandService service and the MenuCommand class.
        /// </summary>
        private void SowInGithubCallback(object sender, EventArgs e)
        {
            var envDte = GetService<DTE>();
            var outWindow = GetService<SVsOutputWindow>();

            new Worker(envDte, outWindow).ShowInGithub();
        }

        /// <summary>
        /// This function is the callback used to execute a command when the a menu item is clicked.
        /// See the Initialize method to see how the menu item is associated to this function using
        /// the OleMenuCommandService service and the MenuCommand class.
        /// </summary>
        private void CopyGithubLinkCallback(object sender, EventArgs e)
        {
            var envDte = GetService<DTE>();
            var outWindow = GetService<SVsOutputWindow>();
            new Worker(envDte, outWindow).CopyGithubLink();
        }
    }
}


text:
"use strict";
//
// WebGLNetworkDevice
//
var WebGLNetworkDevice = (function () {
    function WebGLNetworkDevice() {
    }
    WebGLNetworkDevice.prototype.createWebSocket = function (url, protocol) {
        var WebSocketConstructor = this.WebSocketConstructor;
        if (WebSocketConstructor) {
            var ws;
            if (protocol) {
                ws = new WebSocketConstructor(url, protocol);
            } else {
                ws = new WebSocketConstructor(url);
            }
            if (typeof ws.destroy === "undefined") {
                ws.destroy = function websocketDestroyFn() {
                    this.onopen = null;
                    this.onerror = null;
                    this.onclose = null;
                    this.onmessage = null;
                    this.close();
                };
            }
            return ws;
        } else {
            return null;
        }
    };

    WebGLNetworkDevice.prototype.update = function () {
    };

    WebGLNetworkDevice.create = function (params) {
        var nd = new WebGLNetworkDevice();
        return nd;
    };
    WebGLNetworkDevice.version = 1;
    return WebGLNetworkDevice;
})();

WebGLNetworkDevice.prototype.WebSocketConstructor = (window.WebSocket ? window.WebSocket : window.MozWebSocket);


text:
//===--- ReflectionContext.h - Swift Type Reflection Context ----*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Implements the context for reflection of values in the address space of a
// remote process.
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_REFLECTION_REFLECTIONCONTEXT_H
#define SWIFT_REFLECTION_REFLECTIONCONTEXT_H

#include "swift/Remote/MemoryReader.h"
#include "swift/Remote/MetadataReader.h"
#include "swift/Reflection/Records.h"
#include "swift/Reflection/TypeLowering.h"
#include "swift/Reflection/TypeRef.h"
#include "swift/Reflection/TypeRefBuilder.h"
#include "swift/Runtime/Unreachable.h"

#include <iostream>
#include <set>
#include <vector>
#include <unordered_map>

namespace swift {
namespace reflection {

using swift::remote::MemoryReader;
using swift::remote::RemoteAddress;

template <typename Runtime>
class ReflectionContext
       : public remote::MetadataReader<Runtime, TypeRefBuilder> {
  using super = remote::MetadataReader<Runtime, TypeRefBuilder>;
  using super::readMetadata;
  using super::readObjCClassName;

  std::unordered_map<typename super::StoredPointer, const TypeInfo *> Cache;

public:
  using super::getBuilder;
  using super::readDemanglingForContextDescriptor;
  using super::readIsaMask;
  using super::readTypeFromMetadata;
  using super::readGenericArgFromMetadata;
  using super::readMetadataFromInstance;
  using typename super::StoredPointer;

  explicit ReflectionContext(std::shared_ptr<MemoryReader> reader)
    : super(std::move(reader)) {
    getBuilder().setSymbolicReferenceResolverReader(*this);
  }

  ReflectionContext(const ReflectionContext &other) = delete;
  ReflectionContext &operator=(const ReflectionContext &other) = delete;

  MemoryReader &getReader() {
    return *this->Reader;
  }

  unsigned getSizeOfHeapObject() {
    // This must match sizeof(HeapObject) for the target.
    return sizeof(StoredPointer) * 2;
  }

  void dumpAllSections(std::ostream &OS) {
    getBuilder().dumpAllSections();
  }

  void addReflectionInfo(ReflectionInfo I) {
    getBuilder().addReflectionInfo(I);
  }

  /// Return a description of the layout of a class instance with the given
  /// metadata as its isa pointer.
  const TypeInfo *getMetadataTypeInfo(StoredPointer MetadataAddress) {
    // See if we cached the layout already
    auto found = Cache.find(MetadataAddress);
    if (found != Cache.end())
      return found->second;

    auto &TC = getBuilder().getTypeConverter();

    const TypeInfo *TI = nullptr;

    auto TR = readTypeFromMetadata(MetadataAddress);
    auto kind = this->readKindFromMetadata(MetadataAddress);
    if (TR != nullptr && kind) {
      switch (*kind) {
      case MetadataKind::Class: {
        // Figure out where the stored properties of this class begin
        // by looking at the size of the superclass
        auto start =
            this->readInstanceStartAndAlignmentFromClassMetadata(MetadataAddress);

        // Perform layout
        if (start)
          TI = TC.getClassInstanceTypeInfo(TR, *start);

        break;
      }
      default:
        break;
      }
    }

    // Cache the result for future lookups
    Cache[MetadataAddress] = TI;
    return TI;
  }

  /// Return a description of the layout of a class instance with the given
  /// metadata as its isa pointer.
  const TypeInfo *getInstanceTypeInfo(StoredPointer ObjectAddress) {
    auto MetadataAddress = readMetadataFromInstance(ObjectAddress);
    if (!MetadataAddress)
      return nullptr;

    auto kind = this->readKindFromMetadata(*MetadataAddress);
    if (!kind)
      return nullptr;

    switch (*kind) {
    case MetadataKind::Class:
      return getMetadataTypeInfo(*MetadataAddress);

    case MetadataKind::HeapLocalVariable: {
      auto CDAddr = this->readCaptureDescriptorFromMetadata(*MetadataAddress);
      if (!CDAddr)
        return nullptr;

      // FIXME: Non-generic SIL boxes also use the HeapLocalVariable metadata
      // kind, but with a null capture descriptor right now (see
      // FixedBoxTypeInfoBase::allocate).
      //
      // Non-generic SIL boxes share metadata among types with compatible
      // layout, but we need some way to get an outgoing pointer map for them.
      auto *CD = getBuilder().getCaptureDescriptor(*CDAddr);
      if (CD == nullptr)
        return nullptr;

      auto Info = getBuilder().getClosureContextInfo(*CD, 0);

      return getClosureContextInfo(ObjectAddress, Info);
    }

    case MetadataKind::HeapGenericLocalVariable: {
      // Generic SIL @box type - there is always an instantiated metadata
      // pointer for the boxed type.
      if (auto Meta = readMetadata(*MetadataAddress)) {
        auto GenericHeapMeta =
          cast<TargetGenericBoxHeapMetadata<Runtime>>(Meta.getLocalBuffer());
        return getMetadataTypeInfo(GenericHeapMeta->BoxedType);
      }
      return nullptr;
    }

    case MetadataKind::ErrorObject:
      // Error boxed existential on non-Objective-C runtime target
      return nullptr;

    default:
      return nullptr;
    }
  }

  bool
  projectExistential(RemoteAddress ExistentialAddress,
                     const TypeRef *ExistentialTR,
                     const TypeRef **OutInstanceTR,
                     RemoteAddress *OutInstanceAddress) {
    if (ExistentialTR == nullptr)
      return false;

    auto ExistentialTI = getTypeInfo(ExistentialTR);
    if (ExistentialTI == nullptr)
      return false;

    auto ExistentialRecordTI = dyn_cast<const RecordTypeInfo>(ExistentialTI);
    if (ExistentialRecordTI == nullptr)
      return false;

    switch (ExistentialRecordTI->getRecordKind()) {
    // Class existentials have trivial layout.
    // It is itself the pointer to the instance followed by the witness tables.
    case RecordKind::ClassExistential:
      // This is just Builtin.UnknownObject
      *OutInstanceTR = ExistentialRecordTI->getFields()[0].TR;
      *OutInstanceAddress = ExistentialAddress;
      return true;

    // Opaque existentials fall under two cases:
    // If the value fits in three words, it starts at the beginning of the
    // container. If it doesn't, the first word is a pointer to a heap box.
    case RecordKind::OpaqueExistential: {
      auto Fields = ExistentialRecordTI->getFields();
      auto ExistentialMetadataField = std::find_if(Fields.begin(), Fields.end(),
                                   [](const FieldInfo &FI) -> bool {
        return FI.Name.compare("metadata") == 0;
      });
      if (ExistentialMetadataField == Fields.end())
        return false;

      // Get the metadata pointer for the contained instance type.
      // This is equivalent to:
      // auto PointerArray = reinterpret_cast<uintptr_t*>(ExistentialAddress);
      // uintptr_t MetadataAddress = PointerArray[Offset];
      auto MetadataAddressAddress
        = RemoteAddress(ExistentialAddress.getAddressData() +
                        ExistentialMetadataField->Offset);

      StoredPointer MetadataAddress = 0;
      if (!getReader().readInteger(MetadataAddressAddress, &MetadataAddress))
        return false;

      auto InstanceTR = readTypeFromMetadata(MetadataAddress);
      if (!InstanceTR)
        return false;

      *OutInstanceTR = InstanceTR;

      auto InstanceTI = getTypeInfo(InstanceTR);
      if (!InstanceTI)
        return false;

      if (InstanceTI->getSize() <= ExistentialMetadataField->Offset) {
        // The value fits in the existential container, so it starts at the
        // start of the container.
        *OutInstanceAddress = ExistentialAddress;
      } else {
        // Otherwise it's in a box somewhere off in the heap. The first word
        // of the container has the address to that box.
        StoredPointer BoxAddress = 0;

        if (!getReader().readInteger(ExistentialAddress, &BoxAddress))
          return false;

        // Address = BoxAddress + (sizeof(HeapObject) + alignMask) & ~alignMask)
        auto Alignment = InstanceTI->getAlignment();
        auto StartOfValue = BoxAddress + getSizeOfHeapObject();
        // Align.
        StartOfValue += Alignment - StartOfValue % Alignment;
        *OutInstanceAddress = RemoteAddress(StartOfValue);
      }
      return true;
    }
    case RecordKind::ErrorExistential: {
      // We have a pointer to an error existential, which is always heap object.

      auto MetadataAddress
        = readMetadataFromInstance(ExistentialAddress.getAddressData());

      if (!MetadataAddress)
        return false;

      bool isObjC = false;

      // If we can determine the Objective-C class name, this is probably an
      // error existential with NSError-compatible layout.
      std::string ObjCClassName;
      if (readObjCClassName(*MetadataAddress, ObjCClassName)) {
        if (ObjCClassName == "_SwiftNativeNSError")
          isObjC = true;
      } else {
        // Otherwise, we can check to see if this is a class metadata with the
        // kind value's least significant bit set, which indicates a pure
        // Swift class.
        auto Meta = readMetadata(*MetadataAddress);
        auto ClassMeta = dyn_cast<TargetClassMetadata<Runtime>>(Meta);
        if (!ClassMeta)
          return false;

        isObjC = ClassMeta->isPureObjC();
      }

      // In addition to the isa pointer and two 32-bit reference counts, if the
      // error existential is layout-compatible with NSError, we also need to
      // skip over its three word-sized fields: the error code, the domain,
      // and userInfo.
      StoredPointer InstanceMetadataAddressAddress
        = ExistentialAddress.getAddressData() +
          (isObjC ? 5 : 2) * sizeof(StoredPointer);

      // We need to get the instance's alignment info so we can get the exact
      // offset of the start of its data in the class.
      auto InstanceMetadataAddress =
        readMetadataFromInstance(InstanceMetadataAddressAddress);
      if (!InstanceMetadataAddress)
        return false;

      auto InstanceTR = readTypeFromMetadata(*InstanceMetadataAddress);
      if (!InstanceTR)
        return false;

      auto InstanceTI = getTypeInfo(InstanceTR);
      if (!InstanceTI)
        return false;

      // Now we need to skip over the instance metadata pointer and instance's
      // conformance pointer for Swift.Error.
      StoredPointer InstanceAddress = InstanceMetadataAddressAddress +
        2 * sizeof(StoredPointer);

      // Round up to alignment, and we have the start address of the
      // instance payload.
      auto Alignment = InstanceTI->getAlignment();
      InstanceAddress += Alignment - InstanceAddress % Alignment;

      *OutInstanceTR = InstanceTR;
      *OutInstanceAddress = RemoteAddress(InstanceAddress);

      return true;
    }
    default:
      return false;
    }
  }

  /// Return a description of the layout of a value with the given type.
  const TypeInfo *getTypeInfo(const TypeRef *TR) {
    return getBuilder().getTypeConverter().getTypeInfo(TR);
  }

private:
  const TypeInfo *getClosureContextInfo(StoredPointer Context,
                                        const ClosureContextInfo &Info) {
    RecordTypeInfoBuilder Builder(getBuilder().getTypeConverter(),
                                  RecordKind::ClosureContext);

    auto Metadata = readMetadataFromInstance(Context);
    if (!Metadata)
      return nullptr;

    // Calculate the offset of the first capture.
    // See GenHeap.cpp, buildPrivateMetadata().
    auto OffsetToFirstCapture =
        this->readOffsetToFirstCaptureFromMetadata(*Metadata);
    if (!OffsetToFirstCapture)
      return nullptr;

    // Initialize the builder.
    Builder.addField(*OffsetToFirstCapture, sizeof(StoredPointer),
                     /*numExtraInhabitants=*/0);

    // Skip the closure's necessary bindings struct, if it's present.
    auto SizeOfNecessaryBindings = Info.NumBindings * sizeof(StoredPointer);
    Builder.addField(SizeOfNecessaryBindings, sizeof(StoredPointer),
                     /*numExtraInhabitants=*/0);

    // FIXME: should be unordered_set but I'm too lazy to write a hash
    // functor
    std::set<std::pair<const TypeRef *, const MetadataSource *>> Done;
    GenericArgumentMap Subs;

    ArrayRef<const TypeRef *> CaptureTypes = Info.CaptureTypes;

    // Closure context element layout depends on the layout of the
    // captured types, but captured types might depend on element
    // layout (of previous elements). Use an iterative approach to
    // solve the problem.
    while (!CaptureTypes.empty()) {
      const TypeRef *OrigCaptureTR = CaptureTypes[0];

      // If we failed to demangle the capture type, we cannot proceed.
      if (OrigCaptureTR == nullptr)
        return nullptr;

      const TypeRef *SubstCaptureTR = nullptr;

      // If we have enough substitutions to make this captured value's
      // type concrete, or we know it's size anyway (because it is a
      // class reference or metatype, for example), go ahead and add
      // it to the layout.
      if (OrigCaptureTR->isConcreteAfterSubstitutions(Subs))
        SubstCaptureTR = OrigCaptureTR->subst(getBuilder(), Subs);
      else if (getBuilder().getTypeConverter().hasFixedSize(OrigCaptureTR))
        SubstCaptureTR = OrigCaptureTR;

      if (SubstCaptureTR != nullptr) {
        Builder.addField("", SubstCaptureTR);
        if (Builder.isInvalid())
          return nullptr;

        // Try the next capture type.
        CaptureTypes = CaptureTypes.slice(1);
        continue;
      }

      // Ok, we do not have enough substitutions yet. Perhaps we have
      // enough elements figured out that we can pick off some
      // metadata sources though, and use those to derive some new
      // substitutions.
      bool Progress = false;
      for (auto Source : Info.MetadataSources) {
        // Don't read a source more than once.
        if (Done.count(Source))
          continue;

        // If we don't have enough information to read this source
        // (because it is fulfilled by metadata from a capture at
        // at unknown offset), keep going.
        if (!isMetadataSourceReady(Source.second, Builder))
          continue;

        auto Metadata = readMetadataSource(Context, Source.second, Builder);
        if (!Metadata)
          return nullptr;

        auto *SubstTR = readTypeFromMetadata(*Metadata);
        if (SubstTR == nullptr)
          return nullptr;

        if (!TypeRef::deriveSubstitutions(Subs, Source.first, SubstTR))
          return nullptr;

        Done.insert(Source);
        Progress = true;
      }

      // If we failed to make any forward progress above, we're stuck
      // and cannot close out this layout.
      if (!Progress)
        return nullptr;
    }

    // Ok, we have a complete picture now.
    return Builder.build();
  }

  /// Checks if we have enough information to read the given metadata
  /// source.
  ///
  /// \param Builder Used to obtain offsets of elements known so far.
  bool isMetadataSourceReady(const MetadataSource *MS,
                             const RecordTypeInfoBuilder &Builder) {
    switch (MS->getKind()) {
    case MetadataSourceKind::ClosureBinding:
      return true;
    case MetadataSourceKind::ReferenceCapture: {
      unsigned Index = cast<ReferenceCaptureMetadataSource>(MS)->getIndex();
      return Index < Builder.getNumFields();
    }
    case MetadataSourceKind::MetadataCapture: {
      unsigned Index = cast<MetadataCaptureMetadataSource>(MS)->getIndex();
      return Index < Builder.getNumFields();
    }
    case MetadataSourceKind::GenericArgument: {
      auto Base = cast<GenericArgumentMetadataSource>(MS)->getSource();
      return isMetadataSourceReady(Base, Builder);
    }
    case MetadataSourceKind::Self:
    case MetadataSourceKind::SelfWitnessTable:
      return true;
    }

    swift_runtime_unreachable("Unhandled MetadataSourceKind in switch.");
  }

  /// Read metadata for a captured generic type from a closure context.
  ///
  /// \param Context The closure context in the remote process.
  ///
  /// \param MS The metadata source, which must be "ready" as per the
  /// above.
  ///
  /// \param Builder Used to obtain offsets of elements known so far.
  llvm::Optional<StoredPointer>
  readMetadataSource(StoredPointer Context,
                     const MetadataSource *MS,
                     const RecordTypeInfoBuilder &Builder) {
    switch (MS->getKind()) {
    case MetadataSourceKind::ClosureBinding: {
      unsigned Index = cast<ClosureBindingMetadataSource>(MS)->getIndex();

      // Skip the context's HeapObject header
      // (one word each for isa pointer and reference counts).
      //
      // Metadata and conformance tables are stored consecutively after
      // the heap object header, in the 'necessary bindings' area.
      //
      // We should only have the index of a type metadata record here.
      unsigned Offset = getSizeOfHeapObject() +
                        sizeof(StoredPointer) * Index;

      StoredPointer MetadataAddress;
      if (!getReader().readInteger(RemoteAddress(Context + Offset),
                                   &MetadataAddress))
        break;

      return MetadataAddress;
    }
    case MetadataSourceKind::ReferenceCapture: {
      unsigned Index = cast<ReferenceCaptureMetadataSource>(MS)->getIndex();

      // We should already have enough type information to know the offset
      // of this capture in the context.
      unsigned CaptureOffset = Builder.getFieldOffset(Index);

      StoredPointer CaptureAddress;
      if (!getReader().readInteger(RemoteAddress(Context + CaptureOffset),
                                   &CaptureAddress))
        break;

      // Read the requested capture's isa pointer.
      return readMetadataFromInstance(CaptureAddress);
    }
    case MetadataSourceKind::MetadataCapture: {
      unsigned Index = cast<MetadataCaptureMetadataSource>(MS)->getIndex();

      // We should already have enough type information to know the offset
      // of this capture in the context.
      unsigned CaptureOffset = Builder.getFieldOffset(Index);

      StoredPointer CaptureAddress;
      if (!getReader().readInteger(RemoteAddress(Context + CaptureOffset),
                                   &CaptureAddress))
        break;

      return CaptureAddress;
    }
    case MetadataSourceKind::GenericArgument: {
      auto *GAMS = cast<GenericArgumentMetadataSource>(MS);
      auto Base = readMetadataSource(Context, GAMS->getSource(), Builder);
      if (!Base)
        break;

      unsigned Index = GAMS->getIndex();
      auto Arg = readGenericArgFromMetadata(*Base, Index);
      if (!Arg)
        break;

      return *Arg;
    }
    case MetadataSourceKind::Self:
    case MetadataSourceKind::SelfWitnessTable:
      break;
    }

    return llvm::None;
  }
};

} // end namespace reflection
} // end namespace swift

#endif // SWIFT_REFLECTION_REFLECTIONCONTEXT_H


text:
@interface MSMathException : NSException
@end

@interface MSMathDimensionMismatchException : MSMathException
@end

@interface MSMathIndexOutOfBounds : MSMathException
@end

#endif


text:
title: EViews; R
date: '2007-05-11T23:57:53+08:00'
slug: eviews-r
---

Tasks tomorrow:

1. Preparing a training course on EViews.
2. A sampling program (using R) for the China Customs is waiting for me.


text:
package mdns

import (
	"errors"
	"fmt"
	"strconv"
	"strings"
	"unicode"
	"unicode/utf8"
)

////////////////////////////////////////////////////////////////////////////////
// TOKENIZER

const (
	st_start = iota
	st_backslash
	st_digit
)

type tokenizer struct {
	State int
	in    []rune
	out   []byte
	store string
	pos   int
}

const (
	EOF rune = -(iota + 1)
)

var (
	ErrParseError = errors.New("Parse Error")
)

func NewTokenizer(src string) *tokenizer {
	this := new(tokenizer)
	this.in = []rune(src)
	this.out = make([]byte, 0)
	this.pos = 0
	this.State = st_start
	return this
}

func (this *tokenizer) Next() rune {
	if this.pos >= len(this.in) {
		return EOF
	} else {
		r := this.in[this.pos]
		this.pos++
		return r
	}
}

func (this *tokenizer) Conv(tok rune) rune {
	switch {
	case tok == 'n':
		return '\n'
	case tok == 't':
		return '\t'
	case tok == 'r':
		return '\r'
	case tok == 'f':
		return '\f'
	case unicode.IsDigit(tok):
		return rune(0)
	default:
		return tok
	}
}

func (this *tokenizer) NewState(state int) error {
	if state != this.State && (state == st_start || state == st_backslash) && len(this.store) > 0 {
		// Change xFF into 0xFF
		if strings.HasPrefix(this.store, "x") {
			this.store = "0" + this.store
		}
		// Convert to a byte
		if value, err := strconv.ParseUint(this.store, 10, 32); err != nil {
			return ErrParseError
		} else if value > 0xFF {
			return ErrParseError
		} else {
			this.out = append(this.out, byte(value))
		}
		// Reset the store
		this.store = ""
	}
	this.State = state
	return nil
}

func (this *tokenizer) Append(tok rune, state int) error {
	if err := this.NewState(state); err != nil {
		return err
	} else {
		this.out = append(this.out, []byte(string(tok))...)
		return nil
	}
}

func (this *tokenizer) Store(tok rune, state int) error {
	if err := this.NewState(state); err != nil {
		return err
	} else {
		this.store += string(tok)
		return nil
	}
}

func (this *tokenizer) String() string {
	// Only return the parsed version if valid
	if utf8.Valid(this.out) {
		return string(this.out)
	} else {
		return string(this.in)
	}
}

////////////////////////////////////////////////////////////////////////////////
// QUOTE

func Quote(src string) string {
	// Tokenizer
	t := NewTokenizer(src)
	for tok := t.Next(); tok != EOF; tok = t.Next() {
		switch {
		case tok == ' ', tok == '@', tok == '.', tok == '\\':
			t.Append('\\', t.State)
			t.Append(tok, t.State)
		case tok == '\n':
			t.Append('\\', t.State)
			t.Append('n', t.State)
		case tok == '\r':
			t.Append('\\', t.State)
			t.Append('r', t.State)
		case tok == '\t':
			t.Append('\\', t.State)
			t.Append('t', t.State)
		case tok == '\f':
			t.Append('\\', t.State)
			t.Append('f', t.State)
		case unicode.IsDigit(tok), unicode.IsLetter(tok):
			fallthrough
		case tok == '_', tok == '-', tok == ':':
			t.Append(tok, t.State)
		default:
			for _, b := range []byte(string(tok)) {
				t.Append('\\', t.State)
				for _, d := range []rune(fmt.Sprintf("%03d", b)) {
					t.Append(d, t.State)
				}
			}
		}
	}
	return t.String()
}

////////////////////////////////////////////////////////////////////////////////
// UNQUOTE

// Unquote returns a bare string without quoted characters. The following
// Coversions happen:
// \\ \n \f \t \r Happen as normal
// \xFF returns a byte from hex
// \123 returns a byte from decimal
// \0123 returns a bype from octal
func Unquote(src string) (string, error) {
	// The simple case without any backslashes
	if strings.ContainsAny(src, "\\") == false {
		return src, nil
	}

	// Tokenizer
	t := NewTokenizer(src)
	for tok := t.Next(); tok != EOF; tok = t.Next() {
		switch t.State {
		case st_start:
			if tok == '\\' {
				if err := t.NewState(st_backslash); err != nil {
					return src, err
				}
			} else if err := t.Append(tok, st_start); err != nil {
				return src, err
			}
		case st_backslash:
			if ch := t.Conv(tok); ch != rune(0) {
				if err := t.Append(ch, st_start); err != nil {
					return src, err
				}
			} else if unicode.IsDigit(tok) || tok == 'x' {
				if err := t.Store(tok, st_digit); err != nil {
					return src, err
				}
			}
		case st_digit:
			if unicode.IsDigit(tok) {
				if err := t.Store(tok, st_digit); err != nil {
					return src, err
				}
			} else if tok == '\\' {
				if err := t.NewState(st_backslash); err != nil {
					return src, err
				}
			} else if err := t.Append(tok, st_start); err != nil {
				return src, err
			}
		}
	}

	// We should always end up in st_start state
	if t.State != st_start {
		return src, ErrParseError
	}

	// Eject last value
	if err := t.NewState(st_start); err != nil {
		return src, err
	}

	// Success
	return t.String(), nil
}


text:
package org.apache.druid.server.coordinator.helper;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.apache.druid.java.util.common.DateTimes;
import org.apache.druid.java.util.common.Intervals;
import org.apache.druid.java.util.common.guava.Comparators;
import org.apache.druid.server.coordinator.DataSourceCompactionConfig;
import org.apache.druid.timeline.DataSegment;
import org.apache.druid.timeline.VersionedIntervalTimeline;
import org.apache.druid.timeline.partition.NumberedShardSpec;
import org.apache.druid.timeline.partition.PartitionChunk;
import org.apache.druid.timeline.partition.ShardSpec;
import org.joda.time.Interval;
import org.joda.time.Period;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

@RunWith(Parameterized.class)
public class NewestSegmentFirstPolicyTest
{
  private static final String DATA_SOURCE = "dataSource";
  private static final long DEFAULT_SEGMENT_SIZE = 1000;
  private static final int DEFAULT_NUM_SEGMENTS_PER_SHARD = 4;

  @Parameterized.Parameters(name = "keepSegmentGranularity = {0}")
  public static Iterable<Object[]> constructorFeeder()
  {
    return ImmutableList.of(new Object[]{false}, new Object[]{true});
  }

  private final NewestSegmentFirstPolicy policy = new NewestSegmentFirstPolicy();
  private final boolean keepSegmentGranularity;

  public NewestSegmentFirstPolicyTest(boolean keepSegmentGranularity)
  {
    this.keepSegmentGranularity = keepSegmentGranularity;
  }

  @Test
  public void testLargeOffsetAndSmallSegmentInterval()
  {
    final Period segmentPeriod = new Period("PT1H");
    final CompactionSegmentIterator iterator = policy.reset(
        ImmutableMap.of(DATA_SOURCE, createCompactionConfig(10000, 100, new Period("P2D"))),
        ImmutableMap.of(
            DATA_SOURCE,
            createTimeline(
                new SegmentGenerateSpec(Intervals.of("2017-11-16T20:00:00/2017-11-17T04:00:00"), segmentPeriod),
                new SegmentGenerateSpec(Intervals.of("2017-11-14T00:00:00/2017-11-16T07:00:00"), segmentPeriod)
            )
        ),
        Collections.emptyMap()
    );

    assertCompactSegmentIntervals(
        iterator,
        segmentPeriod,
        Intervals.of("2017-11-14T00:00:00/2017-11-14T01:00:00"),
        Intervals.of("2017-11-15T03:00:00/2017-11-15T04:00:00"),
        true
    );
  }

  @Test
  public void testSmallOffsetAndLargeSegmentInterval()
  {
    final Period segmentPeriod = new Period("PT1H");
    final CompactionSegmentIterator iterator = policy.reset(
        ImmutableMap.of(DATA_SOURCE, createCompactionConfig(10000, 100, new Period("PT1M"))),
        ImmutableMap.of(
            DATA_SOURCE,
            createTimeline(
                new SegmentGenerateSpec(Intervals.of("2017-11-16T20:00:00/2017-11-17T04:00:00"), segmentPeriod),
                new SegmentGenerateSpec(Intervals.of("2017-11-14T00:00:00/2017-11-16T07:00:00"), segmentPeriod)
            )
        ),
        Collections.emptyMap()
    );

    assertCompactSegmentIntervals(
        iterator,
        segmentPeriod,
        Intervals.of("2017-11-16T20:00:00/2017-11-16T21:00:00"),
        Intervals.of("2017-11-17T02:00:00/2017-11-17T03:00:00"),
        false
    );

    assertCompactSegmentIntervals(
        iterator,
        segmentPeriod,
        Intervals.of("2017-11-14T00:00:00/2017-11-14T01:00:00"),
        Intervals.of("2017-11-16T06:00:00/2017-11-16T07:00:00"),
        true
    );
  }

  @Test
  public void testLargeGapInData()
  {
    final Period segmentPeriod = new Period("PT1H");
    final CompactionSegmentIterator iterator = policy.reset(
        ImmutableMap.of(DATA_SOURCE, createCompactionConfig(10000, 100, new Period("PT1H1M"))),
        ImmutableMap.of(
            DATA_SOURCE,
            createTimeline(
                new SegmentGenerateSpec(Intervals.of("2017-11-16T20:00:00/2017-11-17T04:00:00"), segmentPeriod),
                // larger gap than SegmentCompactorUtil.LOOKUP_PERIOD (1 day)
                new SegmentGenerateSpec(Intervals.of("2017-11-14T00:00:00/2017-11-15T07:00:00"), segmentPeriod)
            )
        ),
        Collections.emptyMap()
    );

    assertCompactSegmentIntervals(
        iterator,
        segmentPeriod,
        Intervals.of("2017-11-16T20:00:00/2017-11-16T21:00:00"),
        Intervals.of("2017-11-17T01:00:00/2017-11-17T02:00:00"),
        false
    );

    assertCompactSegmentIntervals(
        iterator,
        segmentPeriod,
        Intervals.of("2017-11-14T00:00:00/2017-11-14T01:00:00"),
        Intervals.of("2017-11-15T06:00:00/2017-11-15T07:00:00"),
        true
    );
  }

  @Test
  public void testSmallNumTargetCompactionSegments()
  {
    final Period segmentPeriod = new Period("PT1H");
    final CompactionSegmentIterator iterator = policy.reset(
        ImmutableMap.of(DATA_SOURCE, createCompactionConfig(10000, 5, new Period("PT1H1M"))),
        ImmutableMap.of(
            DATA_SOURCE,
            createTimeline(
                new SegmentGenerateSpec(Intervals.of("2017-11-16T20:00:00/2017-11-17T04:00:00"), segmentPeriod),
                // larger gap than SegmentCompactorUtil.LOOKUP_PERIOD (1 day)
                new SegmentGenerateSpec(Intervals.of("2017-11-14T00:00:00/2017-11-15T07:00:00"), segmentPeriod)
            )
        ),
        Collections.emptyMap()
    );

    assertCompactSegmentIntervals(
        iterator,
        segmentPeriod,
        Intervals.of("2017-11-16T20:00:00/2017-11-16T21:00:00"),
        Intervals.of("2017-11-17T01:00:00/2017-11-17T02:00:00"),
        false
    );

    assertCompactSegmentIntervals(
        iterator,
        segmentPeriod,
        Intervals.of("2017-11-14T00:00:00/2017-11-14T01:00:00"),
        Intervals.of("2017-11-15T06:00:00/2017-11-15T07:00:00"),
        true
    );
  }

  @Test
  public void testHugeShard()
  {
    final CompactionSegmentIterator iterator = policy.reset(
        ImmutableMap.of(DATA_SOURCE, createCompactionConfig(10000, 100, new Period("P1D"))),
        ImmutableMap.of(
            DATA_SOURCE,
            createTimeline(
                new SegmentGenerateSpec(
                    Intervals.of("2017-11-17T00:00:00/2017-11-18T03:00:00"),
                    new Period("PT1H"),
                    200,
                    DEFAULT_NUM_SEGMENTS_PER_SHARD
                ),
                new SegmentGenerateSpec(
                    Intervals.of("2017-11-09T00:00:00/2017-11-17T00:00:00"),
                    new Period("P2D"),
                    13000, // larger than target compact segment size
                    1
                ),
                new SegmentGenerateSpec(
                    Intervals.of("2017-11-05T00:00:00/2017-11-09T00:00:00"),
                    new Period("PT1H"),
                    200,
                    DEFAULT_NUM_SEGMENTS_PER_SHARD
                )
            )
        ),
        Collections.emptyMap()
    );

    Interval lastInterval = null;
    while (iterator.hasNext()) {
      final List<DataSegment> segments = iterator.next();
      lastInterval = segments.get(0).getInterval();

      Interval prevInterval = null;
      for (DataSegment segment : segments) {
        if (prevInterval != null && !prevInterval.getStart().equals(segment.getInterval().getStart())) {
          Assert.assertEquals(prevInterval.getEnd(), segment.getInterval().getStart());
        }

        prevInterval = segment.getInterval();
      }
    }

    Assert.assertNotNull(lastInterval);
    Assert.assertEquals(Intervals.of("2017-11-05T00:00:00/2017-11-05T01:00:00"), lastInterval);
  }

  @Test
  public void testManySegmentsPerShard()
  {
    final CompactionSegmentIterator iterator = policy.reset(
        ImmutableMap.of(DATA_SOURCE, createCompactionConfig(800000, 100, new Period("P1D"))),
        ImmutableMap.of(
            DATA_SOURCE,
            createTimeline(
                new SegmentGenerateSpec(
                    Intervals.of("2017-12-04T01:00:00/2017-12-05T03:00:00"),
                    new Period("PT1H"),
                    375,
                    80
                ),
                new SegmentGenerateSpec(
                    Intervals.of("2017-12-04T00:00:00/2017-12-04T01:00:00"),
                    new Period("PT1H"),
                    200,
                    150
                ),
                new SegmentGenerateSpec(
                    Intervals.of("2017-12-03T18:00:00/2017-12-04T00:00:00"),
                    new Period("PT6H"),
                    200000,
                    1
                ),
                new SegmentGenerateSpec(
                    Intervals.of("2017-12-03T11:00:00/2017-12-03T18:00:00"),
                    new Period("PT1H"),
                    375,
                    80
                )
            )
        ),
        Collections.emptyMap()
    );

    Interval lastInterval = null;
    while (iterator.hasNext()) {
      final List<DataSegment> segments = iterator.next();
      lastInterval = segments.get(0).getInterval();

      Interval prevInterval = null;
      for (DataSegment segment : segments) {
        if (prevInterval != null && !prevInterval.getStart().equals(segment.getInterval().getStart())) {
          Assert.assertEquals(prevInterval.getEnd(), segment.getInterval().getStart());
        }

        prevInterval = segment.getInterval();
      }
    }

    Assert.assertNotNull(lastInterval);
    Assert.assertEquals(Intervals.of("2017-12-03T11:00:00/2017-12-03T12:00:00"), lastInterval);
  }

  @Test
  public void testManySegmentsPerShard2()
  {
    final CompactionSegmentIterator iterator = policy.reset(
        ImmutableMap.of(DATA_SOURCE, createCompactionConfig(800000, 100, new Period("P1D"))),
        ImmutableMap.of(
            DATA_SOURCE,
            createTimeline(
                new SegmentGenerateSpec(
                    Intervals.of("2017-12-04T11:00:00/2017-12-05T05:00:00"),
                    new Period("PT1H"),
                    200,
                    150
                ),
                new SegmentGenerateSpec(
                    Intervals.of("2017-12-04T06:00:00/2017-12-04T11:00:00"),
                    new Period("PT1H"),
                    375,
                    80
                ),
                new SegmentGenerateSpec(
                    Intervals.of("2017-12-03T18:00:00/2017-12-04T06:00:00"),
                    new Period("PT12H"),
                    257000,
                    1
                ),
                new SegmentGenerateSpec(
                    Intervals.of("2017-12-03T11:00:00/2017-12-03T18:00:00"),
                    new Period("PT1H"),
                    200,
                    150
                ),
                new SegmentGenerateSpec(
                    Intervals.of("2017-12-02T19:00:00/2017-12-03T11:00:00"),
                    new Period("PT16H"),
                    257000,
                    1
                ),
                new SegmentGenerateSpec(
                    Intervals.of("2017-12-02T11:00:00/2017-12-02T19:00:00"),
                    new Period("PT1H"),
                    200,
                    150
                ),
                new SegmentGenerateSpec(
                    Intervals.of("2017-12-01T18:00:00/2017-12-02T11:00:00"),
                    new Period("PT17H"),
                    257000,
                    1
                ),
                new SegmentGenerateSpec(
                    Intervals.of("2017-12-01T09:00:00/2017-12-01T18:00:00"),
                    new Period("PT1H"),
                    200,
                    150
                )
            )
        ),
        Collections.emptyMap()
    );

    Assert.assertFalse(iterator.hasNext());
  }

  @Test
  public void testSkipUnknownDataSource()
  {
    final String unknownDataSource = "unknown";
    final Period segmentPeriod = new Period("PT1H");
    final CompactionSegmentIterator iterator = policy.reset(
        ImmutableMap.of(
            unknownDataSource,
            createCompactionConfig(10000, 100, new Period("P2D")),
            DATA_SOURCE,
            createCompactionConfig(10000, 100, new Period("P2D"))
        ),
        ImmutableMap.of(
            DATA_SOURCE,
            createTimeline(
                new SegmentGenerateSpec(Intervals.of("2017-11-16T20:00:00/2017-11-17T04:00:00"), segmentPeriod),
                new SegmentGenerateSpec(Intervals.of("2017-11-14T00:00:00/2017-11-16T07:00:00"), segmentPeriod)
            )
        ),
        Collections.emptyMap()
    );

    assertCompactSegmentIntervals(
        iterator,
        segmentPeriod,
        Intervals.of("2017-11-14T00:00:00/2017-11-14T01:00:00"),
        Intervals.of("2017-11-15T03:00:00/2017-11-15T04:00:00"),
        true
    );
  }

  @Test
  public void testIgnoreSingleSegmentToCompact()
  {
    final CompactionSegmentIterator iterator = policy.reset(
        ImmutableMap.of(DATA_SOURCE, createCompactionConfig(800000, 100, new Period("P1D"))),
        ImmutableMap.of(
            DATA_SOURCE,
            createTimeline(
                new SegmentGenerateSpec(
                    Intervals.of("2017-12-02T00:00:00/2017-12-03T00:00:00"),
                    new Period("P1D"),
                    200,
                    1
                ),
                new SegmentGenerateSpec(
                    Intervals.of("2017-12-01T00:00:00/2017-12-02T00:00:00"),
                    new Period("P1D"),
                    200,
                    1
                )
            )
        ),
        Collections.emptyMap()
    );

    Assert.assertFalse(iterator.hasNext());
  }

  @Test
  public void testClearSegmentsToCompactWhenSkippingSegments()
  {
    final long maxSizeOfSegmentsToCompact = 800000;
    final VersionedIntervalTimeline<String, DataSegment> timeline = createTimeline(
        new SegmentGenerateSpec(
            Intervals.of("2017-12-03T00:00:00/2017-12-04T00:00:00"),
            new Period("P1D"),
            maxSizeOfSegmentsToCompact / 2 + 10,
            1
        ),
        new SegmentGenerateSpec(
            Intervals.of("2017-12-02T00:00:00/2017-12-03T00:00:00"),
            new Period("P1D"),
            maxSizeOfSegmentsToCompact + 10, // large segment
            1
        ),
        new SegmentGenerateSpec(
            Intervals.of("2017-12-01T00:00:00/2017-12-02T00:00:00"),
            new Period("P1D"),
            maxSizeOfSegmentsToCompact / 3 + 10,
            2
        )
    );
    final CompactionSegmentIterator iterator = policy.reset(
        ImmutableMap.of(DATA_SOURCE, createCompactionConfig(maxSizeOfSegmentsToCompact, 100, new Period("P0D"))),
        ImmutableMap.of(DATA_SOURCE, timeline),
        Collections.emptyMap()
    );

    final List<DataSegment> expectedSegmentsToCompact = timeline
        .lookup(Intervals.of("2017-12-01/2017-12-02"))
        .stream()
        .flatMap(holder -> StreamSupport.stream(holder.getObject().spliterator(), false))
        .map(PartitionChunk::getObject)
        .collect(Collectors.toList());

    Assert.assertTrue(iterator.hasNext());
    Assert.assertEquals(expectedSegmentsToCompact, iterator.next());
    Assert.assertFalse(iterator.hasNext());
  }

  @Test
  public void testIfFirstSegmentIsInSkipOffset()
  {
    final VersionedIntervalTimeline<String, DataSegment> timeline = createTimeline(
        new SegmentGenerateSpec(
            Intervals.of("2017-12-02T14:00:00/2017-12-03T00:00:00"),
            new Period("PT5H"),
            40000,
            1
        )
    );

    final CompactionSegmentIterator iterator = policy.reset(
        ImmutableMap.of(DATA_SOURCE, createCompactionConfig(40000, 100, new Period("P1D"))),
        ImmutableMap.of(DATA_SOURCE, timeline),
        Collections.emptyMap()
    );

    Assert.assertFalse(iterator.hasNext());
  }

  @Test
  public void testIfFirstSegmentOverlapsSkipOffset()
  {
    final VersionedIntervalTimeline<String, DataSegment> timeline = createTimeline(
        new SegmentGenerateSpec(
            Intervals.of("2017-12-01T23:00:00/2017-12-03T00:00:00"),
            new Period("PT5H"),
            40000,
            1
        )
    );

    final CompactionSegmentIterator iterator = policy.reset(
        ImmutableMap.of(DATA_SOURCE, createCompactionConfig(40000, 100, new Period("P1D"))),
        ImmutableMap.of(DATA_SOURCE, timeline),
        Collections.emptyMap()
    );

    Assert.assertFalse(iterator.hasNext());
  }

  @Test
  public void testWithSkipIntervals()
  {
    final Period segmentPeriod = new Period("PT1H");
    final CompactionSegmentIterator iterator = policy.reset(
        ImmutableMap.of(DATA_SOURCE, createCompactionConfig(10000, 100, new Period("P1D"))),
        ImmutableMap.of(
            DATA_SOURCE,
            createTimeline(
                new SegmentGenerateSpec(Intervals.of("2017-11-16T20:00:00/2017-11-17T04:00:00"), segmentPeriod),
                new SegmentGenerateSpec(Intervals.of("2017-11-14T00:00:00/2017-11-16T07:00:00"), segmentPeriod)
            )
        ),
        ImmutableMap.of(
            DATA_SOURCE,
            ImmutableList.of(
                Intervals.of("2017-11-16T00:00:00/2017-11-17T00:00:00"),
                Intervals.of("2017-11-15T00:00:00/2017-11-15T20:00:00"),
                Intervals.of("2017-11-13T00:00:00/2017-11-14T01:00:00")
            )
        )
    );

    assertCompactSegmentIntervals(
        iterator,
        segmentPeriod,
        Intervals.of("2017-11-15T20:00:00/2017-11-15T21:00:00"),
        Intervals.of("2017-11-15T23:00:00/2017-11-16T00:00:00"),
        false
    );

    assertCompactSegmentIntervals(
        iterator,
        segmentPeriod,
        Intervals.of("2017-11-14T01:00:00/2017-11-14T02:00:00"),
        Intervals.of("2017-11-14T23:00:00/2017-11-15T00:00:00"),
        true
    );
  }

  @Test
  public void testHoleInSearchInterval()
  {
    final Period segmentPeriod = new Period("PT1H");
    final CompactionSegmentIterator iterator = policy.reset(
        ImmutableMap.of(DATA_SOURCE, createCompactionConfig(10000, 100, new Period("PT1H"))),
        ImmutableMap.of(
            DATA_SOURCE,
            createTimeline(
                new SegmentGenerateSpec(Intervals.of("2017-11-16T00:00:00/2017-11-17T00:00:00"), segmentPeriod)
            )
        ),
        ImmutableMap.of(
            DATA_SOURCE,
            ImmutableList.of(
                Intervals.of("2017-11-16T04:00:00/2017-11-16T10:00:00"),
                Intervals.of("2017-11-16T14:00:00/2017-11-16T20:00:00")
            )
        )
    );

    assertCompactSegmentIntervals(
        iterator,
        segmentPeriod,
        Intervals.of("2017-11-16T20:00:00/2017-11-16T21:00:00"),
        Intervals.of("2017-11-16T22:00:00/2017-11-16T23:00:00"),
        false
    );

    assertCompactSegmentIntervals(
        iterator,
        segmentPeriod,
        Intervals.of("2017-11-16T10:00:00/2017-11-16T11:00:00"),
        Intervals.of("2017-11-16T13:00:00/2017-11-16T14:00:00"),
        false
    );

    assertCompactSegmentIntervals(
        iterator,
        segmentPeriod,
        Intervals.of("2017-11-16T00:00:00/2017-11-16T01:00:00"),
        Intervals.of("2017-11-16T03:00:00/2017-11-16T04:00:00"),
        true
    );
  }

  private static void assertCompactSegmentIntervals(
      CompactionSegmentIterator iterator,
      Period segmentPeriod,
      Interval from,
      Interval to,
      boolean assertLast
  )
  {
    Interval expectedSegmentIntervalStart = to;
    while (iterator.hasNext()) {
      final List<DataSegment> segments = iterator.next();

      final Interval firstInterval = segments.get(0).getInterval();
      Assert.assertTrue(
          "Intervals should be same or abutting",
          segments.stream().allMatch(
              segment -> segment.getInterval().isEqual(firstInterval) || segment.getInterval().abuts(firstInterval)
          )
      );

      final List<Interval> expectedIntervals = new ArrayList<>(segments.size());
      for (int i = 0; i < segments.size(); i++) {
        if (i > 0 && i % DEFAULT_NUM_SEGMENTS_PER_SHARD == 0) {
          expectedSegmentIntervalStart = new Interval(segmentPeriod, expectedSegmentIntervalStart.getStart());
        }
        expectedIntervals.add(expectedSegmentIntervalStart);
      }
      expectedIntervals.sort(Comparators.intervalsByStartThenEnd());

      Assert.assertEquals(
          expectedIntervals,
          segments.stream().map(DataSegment::getInterval).collect(Collectors.toList())
      );

      if (expectedSegmentIntervalStart.equals(from)) {
        break;
      }
      expectedSegmentIntervalStart = new Interval(segmentPeriod, expectedSegmentIntervalStart.getStart());
    }

    if (assertLast) {
      Assert.assertFalse(iterator.hasNext());
    }
  }

  private static VersionedIntervalTimeline<String, DataSegment> createTimeline(
      SegmentGenerateSpec... specs
  )
  {
    VersionedIntervalTimeline<String, DataSegment> timeline = new VersionedIntervalTimeline<>(
        String.CASE_INSENSITIVE_ORDER
    );

    final String version = DateTimes.nowUtc().toString();

    final List<SegmentGenerateSpec> orderedSpecs = Arrays.asList(specs);
    orderedSpecs.sort((s1, s2) -> Comparators.intervalsByStartThenEnd().compare(s1.totalInterval, s2.totalInterval));
    Collections.reverse(orderedSpecs);

    for (SegmentGenerateSpec spec : orderedSpecs) {
      Interval remaininInterval = spec.totalInterval;

      while (!Intervals.isEmpty(remaininInterval)) {
        final Interval segmentInterval;
        if (remaininInterval.toDuration().isLongerThan(spec.segmentPeriod.toStandardDuration())) {
          segmentInterval = new Interval(spec.segmentPeriod, remaininInterval.getEnd());
        } else {
          segmentInterval = remaininInterval;
        }

        for (int i = 0; i < spec.numSegmentsPerShard; i++) {
          final ShardSpec shardSpec = new NumberedShardSpec(spec.numSegmentsPerShard, i);
          final DataSegment segment = new DataSegment(
              DATA_SOURCE,
              segmentInterval,
              version,
              null,
              ImmutableList.of(),
              ImmutableList.of(),
              shardSpec,
              0,
              spec.segmentSize
          );
          timeline.add(
              segmentInterval,
              version,
              shardSpec.createChunk(segment)
          );
        }

        remaininInterval = SegmentCompactorUtil.removeIntervalFromEnd(remaininInterval, segmentInterval);
      }
    }

    return timeline;
  }

  private DataSourceCompactionConfig createCompactionConfig(
      long targetCompactionSizeBytes,
      int numTargetCompactionSegments,
      Period skipOffsetFromLatest
  )
  {
    return new DataSourceCompactionConfig(
        DATA_SOURCE,
        keepSegmentGranularity,
        0,
        targetCompactionSizeBytes,
        targetCompactionSizeBytes,
        null,
        numTargetCompactionSegments,
        skipOffsetFromLatest,
        null,
        null
    );
  }

  private static class SegmentGenerateSpec
  {
    private final Interval totalInterval;
    private final Period segmentPeriod;
    private final long segmentSize;
    private final int numSegmentsPerShard;

    SegmentGenerateSpec(Interval totalInterval, Period segmentPeriod)
    {
      this(totalInterval, segmentPeriod, DEFAULT_SEGMENT_SIZE, DEFAULT_NUM_SEGMENTS_PER_SHARD);
    }

    SegmentGenerateSpec(Interval totalInterval, Period segmentPeriod, long segmentSize, int numSegmentsPerShard)
    {
      Preconditions.checkArgument(numSegmentsPerShard >= 1);
      this.totalInterval = totalInterval;
      this.segmentPeriod = segmentPeriod;
      this.segmentSize = segmentSize;
      this.numSegmentsPerShard = numSegmentsPerShard;
    }
  }
}


text:
'use strict';

var grunt = require('grunt');
var helpers = require('./helpers');

exports.chmod = {
  setUp: function(done) {
    // setup here if necessary
    done();
  },

  default_options: function(test) {
    test.expect(1);

    var actual = grunt.file.readJSON('tmp/default_options.json');
    var expected = grunt.file.readJSON('test/expected/default_options.json');
    test.deepEqual(actual, expected, 'Task should fail when relying on the default options.');

    test.done();
  },

  custom_options_without_string_mode: function(test) {
    test.expect(1);

    var actual = grunt.file.readJSON('tmp/custom_options_without_string_mode.json');
    var expected = grunt.file.readJSON('test/expected/custom_options_without_string_mode.json');
    test.deepEqual(actual, expected, 'Task should fail when setting a non-string `mode` on the custom options.');

    test.done();
  },

  custom_options_with_empty_mode: function(test) {
    test.expect(1);

    var actual = grunt.file.readJSON('tmp/custom_options_with_empty_mode.json');
    var expected = grunt.file.readJSON('test/expected/custom_options_with_empty_mode.json');
    test.deepEqual(actual, expected, 'Task should fail when setting an empty string `mode` on the custom options.');

    test.done();
  },

  custom_options_with_invalid_mode: function(test) {
    test.expect(1);

    var actual = grunt.file.readJSON('tmp/custom_options_with_invalid_mode.json');
    var expected = grunt.file.readJSON('test/expected/custom_options_with_invalid_mode.json');
    test.deepEqual(actual, expected, 'Task should fail when setting an invalid string `mode` on the custom options.');

    test.done();
  },

  custom_options_nonexistent_file: function(test) {
    test.expect(1);

    var actual = grunt.file.readJSON('tmp/custom_options_nonexistent_file.json');
    var expected = grunt.file.readJSON('test/expected/custom_options_nonexistent_file.json');
    test.deepEqual(actual, expected, 'Task should pass but not modify any file permissions when there are no files that match.');

    test.done();
  },

  custom_options_file: function(test) {
    test.expect(2);

    var actual = grunt.file.readJSON('tmp/custom_options_file.json');
    var expected = grunt.file.readJSON('test/expected/custom_options_file.json');
    test.deepEqual(actual, expected, 'Task should pass and modify the file permissions of all files that match.');

    var rawMode = require('fs').statSync('tmp/custom_options_file.js').mode;
    var numericMode = helpers.permsFromMode(rawMode);

    var actualPerms = '' + numericMode;
    var expectedPerms = '444';
    test.strictEqual(actualPerms, expectedPerms, 'Permissions should match.');

    test.done();
  },

  custom_options_dir: function(test) {
    test.expect(2);

    var actual = grunt.file.readJSON('tmp/custom_options_dir.json');
    var expected = grunt.file.readJSON('test/expected/custom_options_dir.json');
    test.deepEqual(actual, expected, 'Task should pass and modify the directory permissions of all directories that match.');

    var rawMode = require('fs').statSync('tmp/custom_options_dir/').mode;
    var numericMode = helpers.permsFromMode(rawMode);

    var actualPerms = '' + numericMode;
    var expectedPerms = '444';
    test.strictEqual(actualPerms, expectedPerms, 'Permissions should match.');

    test.done();
  }

};


text:
'use strict';
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import {withRouter} from 'react-router';
import DevicePage from '../components/DevicePage';
import * as apiActions from '../actions/api/devices';
import * as commandApiActions from '../actions/api/commands';
import * as snackbarActions from '../actions/ui/snackbar';
import * as cmdActions from '../actions/api/commands';

const mapDispatchToProps = (dispatch) => {
  return {
    api: bindActionCreators(apiActions, dispatch),
    commandsApi: bindActionCreators(commandApiActions, dispatch),
    snackbar: bindActionCreators(snackbarActions, dispatch),
    cmd: bindActionCreators(cmdActions, dispatch)
  };
};

function mapStateToProps (state) {
  return {
    ...state.device
  };
}

const routedDevice = withRouter(DevicePage);

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(routedDevice);


text:
export default {
  REINDEX_URL: '',
};