text:
package io.fabric8.runtime.agent;

import io.fabric8.agent.download.DownloadManager;
import io.fabric8.agent.download.DownloadManagers;
import io.fabric8.agent.utils.AgentUtils;
import io.fabric8.api.Container;
import io.fabric8.api.FabricService;
import io.fabric8.api.Profile;
import io.fabric8.api.Profiles;
import io.fabric8.api.scr.AbstractComponent;
import io.fabric8.api.scr.ValidatingReference;
import io.fabric8.utils.Closeables;
import io.fabric8.utils.Strings;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.jar.Attributes;
import java.util.jar.JarInputStream;
import java.util.jar.ContainerManifest;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.management.MBeanServer;
import javax.management.ObjectName;

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.ConfigurationPolicy;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.karaf.features.Feature;
import org.jboss.gravia.container.tomcat.WebAppContextListener;
import org.jboss.gravia.provision.Provisioner;
import org.jboss.gravia.provision.ResourceHandle;
import org.jboss.gravia.provision.ResourceInstaller;
import org.jboss.gravia.provision.spi.DefaultInstallerContext;
import org.jboss.gravia.resource.Capability;
import org.jboss.gravia.resource.ContentNamespace;
import org.jboss.gravia.resource.DefaultResourceBuilder;
import org.jboss.gravia.resource.IdentityNamespace;
import org.jboss.gravia.resource.ContainerManifestBuilder;
import org.jboss.gravia.resource.MavenCoordinates;
import org.jboss.gravia.resource.Requirement;
import org.jboss.gravia.resource.Resource;
import org.jboss.gravia.resource.ResourceIdentity;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.Asset;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.impl.base.asset.ZipFileEntryAsset;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(name = "io.fabric8.runtime.agent.FabricAgent", label = "Fabric8 Runtime Agent", immediate = true, policy = ConfigurationPolicy.IGNORE, metatype = false)
public class FabricAgent extends AbstractComponent implements FabricAgentMXBean {
    private static final Logger LOGGER = LoggerFactory.getLogger(FabricAgent.class);

    private static final String SERVICE_COMPONENT = "Service-Component";
    private static final String GENRATED_RESOURCE_IDENTITY = "io.fabric8.generated.fabric-profile";
    private static final String GENRATED_MAVEN_COORDS = "mvn:io.fabric8.generated/fabric-profile/1.0.0/war";

    private static final Pattern VALID_COMPONENT_PATH_PATTERN = Pattern.compile("[_a-zA-Z0-9\\-\\./]+");

    @Reference(referenceInterface = MBeanServer.class)
    private final ValidatingReference<MBeanServer> mbeanServer = new ValidatingReference<MBeanServer>();

    @Reference(referenceInterface = Provisioner.class)
    private final ValidatingReference<Provisioner> provisioner = new ValidatingReference<Provisioner>();
    @Reference(referenceInterface = FabricService.class)
    private final ValidatingReference<FabricService> fabricService = new ValidatingReference<FabricService>();
    private final Runnable onConfigurationChange = new Runnable() {
        @Override
        public void run() {
            submitUpdateJob();
        }
    };

    private final ExecutorService executor = Executors.newSingleThreadExecutor(new NamedThreadFactory("fabric8-agent"));
    private final ExecutorService downloadExecutor = Executors.newSingleThreadExecutor(new NamedThreadFactory("fabric8-agent-downloader"));
    private ObjectName objectName;
    private Map<ResourceIdentity, ResourceHandle> resourcehandleMap = new ConcurrentHashMap<ResourceIdentity, ResourceHandle>();

    private ComponentContext componentContext;

    @Activate
    void activate(ComponentContext componentContext) {
        this.componentContext = componentContext;
        LOGGER.info("Activating");
        fabricService.get().trackConfiguration(onConfigurationChange);
        activateComponent();
        submitUpdateJob();
        try {
            MBeanServer anMBeanServer = mbeanServer.get();
            if (anMBeanServer != null) {
                if (objectName == null) {
                    objectName = new ObjectName("io.fabric8:type=RuntimeAgent");
                }
                anMBeanServer.registerMBean(this, objectName);
            } else {
                LOGGER.warn("No MBeanServer");
            }
        } catch (Exception e) {
            LOGGER.warn("Failed to register MBean " + objectName + ": " + e, e);
        }
    }

    @Deactivate
    void deactivate() {
        if (objectName != null) {
            try {
                MBeanServer anMBeanServer = mbeanServer.get();
                if (anMBeanServer != null) {
                    anMBeanServer.unregisterMBean(objectName);
                }
            } catch (Exception e) {
                LOGGER.warn("Failed to unregister MBean " + objectName + ": " + e, e);
            }
        }
        deactivateComponent();
        fabricService.get().untrackConfiguration(onConfigurationChange);
        executor.shutdown();
        try {
            executor.awaitTermination(1, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            // Ignore
        }
        executor.shutdownNow();
    }

    private void submitUpdateJob() {
        executor.submit(new Runnable() {
            @Override
            public void run() {
                if (isValid()) {
                    try {
                        updateInternal();
                    } catch (Exception e) {
                        LOGGER.warn("Caught:" + e, e);
                    }
                }
            }
        });
    }

    protected synchronized void updateInternal() {
        Profile effectiveProfile;
        try {
            Profile overlayProfile = fabricService.get().getCurrentContainer().getOverlayProfile();
            effectiveProfile = Profiles.getEffectiveProfile(fabricService.get(), overlayProfile);
        } catch (Exception ex) {
            LOGGER.warn("Failed to read container profile. This exception will be ignored..", ex);
            return;
        }

        List<String> resources = null;
        try {
            Map<String, File> artifacts = downloadProfileArtifacts(fabricService.get(), effectiveProfile);
            populateExplodedWar(artifacts);
            resources = updateProvisioning(artifacts, provisioner.get());
            updateStatus(Container.PROVISION_SUCCESS, null, resources);
        } catch (Throwable e) {
            if (isValid()) {
                LOGGER.warn("Exception updating provisioning: " + e, e);
                updateStatus(Container.PROVISION_ERROR, e, resources);
            } else {
                LOGGER.debug("Exception updating provisioning: " + e, e);
            }
        }
    }

    protected void updateStatus(String status, Throwable result, List<String> resources) {
        try {
            FabricService fs = fabricService.get();

            if (fs != null) {
                Container container = fs.getCurrentContainer();
                String e;
                if (result == null) {
                    e = null;
                } else {
                    StringWriter sw = new StringWriter();
                    result.printStackTrace(new PrintWriter(sw));
                    e = sw.toString();
                }
                if (resources != null) {
                    container.setProvisionList(resources);
                }
                container.setProvisionResult(status);
                container.setProvisionException(e);
            } else {
                LOGGER.info("FabricService not available");
            }
        } catch (Throwable e) {
            LOGGER.warn("Unable to set provisioning result");
        }
    }

    protected Map<String, File> downloadProfileArtifacts(FabricService fabric, Profile profile) throws Exception {
        updateStatus(Container.PROVISION_DOWNLOADING, null, null);
        Set<String> bundles = new LinkedHashSet<String>();
        Set<Feature> features = new LinkedHashSet<Feature>();
        bundles.addAll(profile.getBundles());

        DownloadManager downloadManager = DownloadManagers.createDownloadManager(fabric, downloadExecutor);
        AgentUtils.addFeatures(features, fabric, downloadManager, profile);
        //return AgentUtils.downloadProfileArtifacts(fabricService.get(), downloadManager, profile);
        return AgentUtils.downloadBundles(downloadManager, features, bundles,
                Collections.<String>emptySet());
    }

    protected void populateExplodedWar(Map<String, File> artifacts) throws IOException {
        updateStatus("populating profile war", null, null);
        ClassLoader original = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(FabricAgent.class.getClassLoader());
            final WebArchive archive = ShrinkWrap.create(WebArchive.class, "profile.war");
            final Set<String> components = new HashSet<String>();
            for (Map.Entry<String, File> entry : artifacts.entrySet()) {
                String name = entry.getKey();
                File f = entry.getValue();
                if (!isWar(name, f)) {
                    archive.addAsLibrary(f);
                    ContainerManifest mf = readContainerManifest(f);
                    if (mf.getMainAttributes().containsKey(new Attributes.Name(SERVICE_COMPONENT))) {
                        String serviceComponents = mf.getMainAttributes().getValue(SERVICE_COMPONENT);
                        for (String component : Strings.splitAndTrimAsList(serviceComponents, ",")) {
                            if (VALID_COMPONENT_PATH_PATTERN.matcher(component).matches()) {
                                archive.add(new ZipFileEntryAsset(new ZipFile(f, ZipFile.OPEN_READ), new ZipEntry(component)), component);
                                components.add(component);
                            }
                        }
                    }
                }
            }

            archive.addClass(WebAppContextListener.class);
            archive.addAsWebInfResource("web.xml");
            archive.addAsWebResource("context.xml", "META-INF/context.xml");
            archive.setContainerManifest(new Asset() {
                @Override
                public InputStream openStream() {
                    return new ContainerManifestBuilder()
                            .addIdentityCapability(GENRATED_RESOURCE_IDENTITY, "1.0.0")
                            .addContainerManifestHeader(SERVICE_COMPONENT, Strings.join(components, ",")).openStream();
                }
            });

            File profileWar = componentContext.getBundleContext().getDataFile("fabric-profile.war");
            archive.as(ZipExporter.class).exportTo(profileWar, true);
            artifacts.put(GENRATED_MAVEN_COORDS, profileWar);
        } finally {
            Thread.currentThread().setContextClassLoader(original);
        }
        updateStatus("populated profile war", null, null);
    }

    private static ContainerManifest readContainerManifest(File file) throws IOException {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            JarInputStream jis = new JarInputStream(fis);
            return jis.getContainerManifest();
        } finally {
            Closeables.closeQuietly(fis);
        }
    }

    protected List<String> updateProvisioning(Map<String, File> artifacts, Provisioner provisionService) throws Exception {
        ResourceInstaller resourceInstaller = provisionService.getResourceInstaller();
        Map<ResourceIdentity, Resource> installedResources = getInstalledResources(provisionService);
        Map<Requirement, Resource> requirements = new HashMap<Requirement, Resource>();
        Set<Map.Entry<String, File>> entries = artifacts.entrySet();
        List<Resource> resourcesToInstall = new ArrayList<Resource>();
        List<String> resourceUrisInstalled = new ArrayList<String>();
        updateStatus(Container.PROVISION_INSTALLING, null, null);
        for (Map.Entry<String, File> entry : entries) {
            String name = entry.getKey();
            File file = entry.getValue();

            String coords = name;
            int idx = coords.lastIndexOf(':');
            if (idx > 0) {
                coords = name.substring(idx + 1);
            }
            // lets switch to gravia's mvn coordinates
            coords = coords.replace('/', ':');
            MavenCoordinates mvnCoords = parse(coords);
            URL url = file.toURI().toURL();
            if (url == null) {
                LOGGER.warn("Could not find URL for file " + file);
                continue;
            }

            // TODO lets just detect wars for now for servlet engines - how do we decide on WildFly?

            boolean isShared = !isWar(name, file);
            Resource resource = findMavenResource(mvnCoords, url, isShared);
            if (resource == null) {
                LOGGER.warn("Could not find resource for " + mvnCoords + " and " + url);
            } else {
                ResourceIdentity identity = resource.getIdentity();
                Resource oldResource = installedResources.remove(identity);
                if (oldResource == null && !resourcehandleMap.containsKey(identity)) {
                    if (isShared) {
                        // TODO lest not deploy shared stuff for now since bundles throw an exception when trying to stop them
                        // which breaks the tests ;)
                        LOGGER.debug("TODO not installing " + (isShared ? "shared" : "non-shared") + " resource: " + identity);
                    } else {
                        LOGGER.info("Installing " + (isShared ? "shared" : "non-shared") + " resource: " + identity);
                        resourcesToInstall.add(resource);
                        resourceUrisInstalled.add(name);
                    }
                }
            }
        }

        for (Resource installedResource : installedResources.values()) {
            ResourceIdentity identity = installedResource.getIdentity();
            ResourceHandle resourceHandle = resourcehandleMap.get(identity);
            if (resourceHandle == null) {
                // TODO should not really happen when we can ask about the installed Resources
                LOGGER.warn("TODO: Cannot uninstall " + installedResource + " as we have no handle!");
            } else {
                LOGGER.info("Uninstalling " + installedResource);
                resourceHandle.uninstall();
                resourcehandleMap.remove(identity);
                LOGGER.info("Uninstalled " + installedResource);
            }
        }
        if (resourcesToInstall.size() > 0) {
            LOGGER.info("Installing " + resourcesToInstall.size() + " resource(s)");
            Set<ResourceHandle> resourceHandles = new LinkedHashSet<>();
            ResourceInstaller.Context context = new DefaultInstallerContext(resourcesToInstall, requirements);
            for (Resource resource : resourcesToInstall) {
                resourceHandles.add(resourceInstaller.installResource(context, resource));
            }

            LOGGER.info("Got " + resourceHandles.size() + " resource handle(s)");

            for (ResourceHandle resourceHandle : resourceHandles) {
                resourcehandleMap.put(resourceHandle.getResource().getIdentity(), resourceHandle);
            }
        }
        return resourceUrisInstalled;
    }

    protected Map<ResourceIdentity, Resource> getInstalledResources(Provisioner provisionService) {
        Map<ResourceIdentity, Resource> installedResources = new HashMap<ResourceIdentity, Resource>();

        // lets add the handles we already know about
        for (Map.Entry<ResourceIdentity, ResourceHandle> entry : resourcehandleMap.entrySet()) {
            installedResources.put(entry.getKey(), entry.getValue().getResource());
        }
        try {
            Iterator<Resource> resources = provisionService.getEnvironment().getResources();
            while (resources.hasNext()) {
                Resource resource = resources.next();
                installedResources.put(resource.getIdentity(), resource);
            }
        } catch (Throwable e) {
            LOGGER.warn("Ignoring error finding current resources: " + e, e);
        }
        return installedResources;
    }

    public Resource findMavenResource(MavenCoordinates mavenid, URL contentURL, boolean isShared) {
        LOGGER.debug("Find maven providers for: {}", mavenid);
        Resource result = null;
        if (contentURL != null) {
            DefaultResourceBuilder builder = new DefaultResourceBuilder();
            Capability identCap = builder.addIdentityCapability(mavenid);
            Capability ccap = builder.addCapability(ContentNamespace.CONTENT_NAMESPACE, null, null);
            ccap.getAttributes().put(ContentNamespace.CAPABILITY_URL_ATTRIBUTE, contentURL);
            if (isShared) {
                identCap.getAttributes().put(IdentityNamespace.CAPABILITY_SHARED_ATTRIBUTE, "true");
            } else {
                // lets default to using just the artifact id for the context path
                identCap.getAttributes().put("contextPath", mavenid.getArtifactId());
            }
            LOGGER.debug("Found maven resource: {}", result = builder.getResource());
        }

        return result;
    }

    private static boolean isWar(String name, File file) {
        return name.startsWith("war:") || name.contains("/war/") ||
                file.getName().toLowerCase().endsWith(".war");
    }

    //TODO: This needs to be fixed at gravia
    private static MavenCoordinates parse(String coordinates) {
        MavenCoordinates result;
        String[] parts = coordinates.split(":");
        if (parts.length == 3) {
            result =  MavenCoordinates.create(parts[0], parts[1], parts[2], null, null);
        } else if (parts.length == 4) {
            result = MavenCoordinates.create(parts[0], parts[1], parts[2], parts[3], null);
        } else if (parts.length == 5) {
            result = MavenCoordinates.create(parts[0], parts[1], parts[2], parts[3], parts[4]);
        } else {
            throw new IllegalArgumentException("Invalid coordinates: " + coordinates);
        }
        return result;
    }

    void bindMbeanServer(MBeanServer service) {
        this.mbeanServer.bind(service);
    }

    void unbindMbeanServer(MBeanServer service) {
        this.mbeanServer.unbind(service);
    }

    void bindProvisioner(Provisioner service) {
        this.provisioner.bind(service);
    }

    void unbindProvisioner(Provisioner service) {
        this.provisioner.unbind(service);
    }

    void bindFabricService(FabricService fabricService) {
        this.fabricService.bind(fabricService);
    }

    void unbindFabricService(FabricService fabricService) {
        this.fabricService.unbind(fabricService);
    }

    private static class NamedThreadFactory implements ThreadFactory {
        private static final AtomicInteger poolNumber = new AtomicInteger(1);
        private final ThreadGroup group;
        private final AtomicInteger threadNumber = new AtomicInteger(1);
        private final String namePrefix;

        NamedThreadFactory(String prefix) {
            SecurityManager s = System.getSecurityManager();
            group = (s != null) ? s.getThreadGroup() :
                    Thread.currentThread().getThreadGroup();
            namePrefix = prefix + "-" +
                    poolNumber.getAndIncrement() +
                    "-thread-";
        }

        public Thread newThread(Runnable r) {
            Thread t = new Thread(group, r,
                    namePrefix + threadNumber.getAndIncrement(),
                    0);
            if (t.isDaemon())
                t.setDaemon(false);
            if (t.getPriority() != Thread.NORM_PRIORITY)
                t.setPriority(Thread.NORM_PRIORITY);
            return t;
        }
    }
}


text:
package org.apache.commons.math3.linear;

/**
 * Default implementation of the {@link RealMatrixChangingVisitor} interface.
 * <p>
 * This class is a convenience to create custom visitors without defining all
 * methods. This class provides default implementations that do nothing.
 * </p>
 *
 * @version $Id: DefaultRealMatrixChangingVisitor.java 1244107 2012-02-14 16:17:55Z erans $
 * @since 2.0
 */
public class DefaultRealMatrixChangingVisitor implements RealMatrixChangingVisitor {
    /** {@inheritDoc} */
    public void start(int rows, int columns,
                      int startRow, int endRow, int startColumn, int endColumn) {
    }

    /** {@inheritDoc} */
    public double visit(int row, int column, double value) {
        return value;
    }

    /** {@inheritDoc} */
    public double end() {
        return 0;
    }
}

redpajama_set_name: RedPajamaGithub


text:
/*PLEASE DO NOT EDIT THIS CODE*/
/*This code was generated using the UMPLE 1.20.2.4305 modeling language!*/

// line 239 "diagram.ump"
public class Interaction
{

  //------------------------
  // MEMBER VARIABLES
  //------------------------

  //Interaction Attributes
  private SimulationSystem systemSnapshot;
  private boolean isSingular;

  //Interaction Associations
  private SimulationSystem simulationSystem;

  //------------------------
  // CONSTRUCTOR
  //------------------------

  public Interaction(SimulationSystem aSystemSnapshot, boolean aIsSingular, SimulationSystem aSimulationSystem)
  {
    systemSnapshot = aSystemSnapshot;
    isSingular = aIsSingular;
    boolean didAddSimulationSystem = setSimulationSystem(aSimulationSystem);
    if (!didAddSimulationSystem)
    {
      throw new RuntimeException("Unable to create interaction due to simulationSystem");
    }
  }

  //------------------------
  // INTERFACE
  //------------------------

  public boolean setSystemSnapshot(SimulationSystem aSystemSnapshot)
  {
    boolean wasSet = false;
    systemSnapshot = aSystemSnapshot;
    wasSet = true;
    return wasSet;
  }

  public boolean setIsSingular(boolean aIsSingular)
  {
    boolean wasSet = false;
    isSingular = aIsSingular;
    wasSet = true;
    return wasSet;
  }

  public SimulationSystem getSystemSnapshot()
  {
    return systemSnapshot;
  }

  public boolean getIsSingular()
  {
    return isSingular;
  }

  public SimulationSystem getSimulationSystem()
  {
    return simulationSystem;
  }

  public boolean setSimulationSystem(SimulationSystem aSimulationSystem)
  {
    boolean wasSet = false;
    if (aSimulationSystem == null)
    {
      return wasSet;
    }

    SimulationSystem existingSimulationSystem = simulationSystem;
    simulationSystem = aSimulationSystem;
    if (existingSimulationSystem != null && !existingSimulationSystem.equals(aSimulationSystem))
    {
      existingSimulationSystem.removeInteraction(this);
    }
    simulationSystem.addInteraction(this);
    wasSet = true;
    return wasSet;
  }

  public void delete()
  {
    SimulationSystem placeholderSimulationSystem = simulationSystem;
    this.simulationSystem = null;
    placeholderSimulationSystem.removeInteraction(this);
  }

  public String toString()
  {
	  String outputString = "";
    return super.toString() + "["+
            "isSingular" + ":" + getIsSingular()+ "]" + System.getProperties().getProperty("line.separator") +
            "  " + "systemSnapshot" + "=" + (getSystemSnapshot() != null ? !getSystemSnapshot().equals(this)  ? getSystemSnapshot().toString().replaceAll("  ","    ") : "this" : "null") + System.getProperties().getProperty("line.separator") +
            "  " + "simulationSystem = "+(getSimulationSystem()!=null?Integer.toHexString(System.identityHashCode(getSimulationSystem())):"null")
     + outputString;
  }
}


text:
const gulp = require('gulp');
const config = require('./config');

gulp.task('watch:srcFiles', () => {
  gulp.watch(config.PATHS.tsSrcFiles, ['scripts1:cjs', 'bundle:cjs']);
});

gulp.task('watch:testfiles', () => {
  gulp.watch(config.PATHS.tsTestFiles, ['scripts:test']);
});


text:
AMIE="../../../tools/amie_plus.jar"
TRAIN="../freebase_mtr100_mte100-train.txt"

RULEFILE="fb15k-rules-train.txt"

java -jar $AMIE -full -minhc 0.001 -minis 10 -mins 10 -pm support -verbose $TRAIN > $RULEFILE


text:
<?php
namespace empire\framework\localization;

/**
 * Represents a localizable string.
 *
 * Strings can have additional placeholders.
 *
 * Example:
 *
"There are currently :number users logged in"

 * with the placeholders
array('number' => 20)

 *
 * @author Tobias Hornberger [tobias.hornberger@falsemirror.de]
 * @version 1.0
 */
class String {

	/**
	 * The string text
	 *
	 * @var string
	 */
	private $text;

	/**
	 * An array with placeholders
	 *
	 * @var array
	 */
	private $placeholders;

	/**
	 * Constructs a new string.
	 *
	 * @param string $text the text
	 * @param array $placeholders an array with placeholders
	 */
	public function __construct($text, Array $placeholders = array()) {
		$this->text = $text;
		$this->placeholders = $placeholders;
	}

	/**
	 * Returns the text.
	 *
	 * @return string the text
	 */
	public function getText() {
		return $this->text;
	}

	/**
	 * Returns the replacements.
	 *
	 * @return array the replacements
	 */
	public function getPlaceholders() {
		return $this->placeholders;
	}

	/**
	 * Converts this string to a primitive string.
	 *
	 * @return string the string representation of this string
	 */
	public function __toString() {
		return $this->text;
	}

}


text:
#include <map>
#include <cerrno>
#include <cstring>
#include "restore.h"
#include "util.h"
#include "logger.h"
#include "meta.h"
#include "kfstree.h"
#include "replay.h"
#include "restore.h"
#include "entry.h"
#include "checkpoint.h"
#include "LayoutManager.h"

using namespace KFS;
int16_t minReplicasPerFile;

static bool
checkpoint_seq(deque <string> &c)
{
	c.pop_front();
	if (c.empty())
		return false;
	seq_t highest = toNumber(c.front());
	oplog.set_seqno(highest);
	return (highest >= 0);
}

static bool
checkpoint_fid(deque <string> &c)
{
	c.pop_front();
	if (c.empty())
		return false;
	fid_t highest = toNumber(c.front());
	fileID.setseed(highest);
	return (highest > 0);
}

static bool
checkpoint_chunkId(deque <string> &c)
{
	c.pop_front();
	if (c.empty())
		return false;
	fid_t highest = toNumber(c.front());
	chunkID.setseed(highest);
	return (highest > 0);
}

static bool
checkpoint_version(deque <string> &c)
{
	c.pop_front();
	if (c.empty())
		return false;
	int version = toNumber(c.front());
	return version == Checkpoint::VERSION;
}

static bool
checkpoint_time(deque <string> &c)
{
	c.pop_front();
	std::cout << "restoring from checkpoint of " << c.front() << '\n';
	return true;
}

static bool
checkpoint_log(deque <string> &c)
{
	c.pop_front();
	string s;
	while (!c.empty()) {
		s += c.front();
		c.pop_front();
		if (!c.empty())
			s += "/";
	}
	std::cout << "log file is " << s << '\n';
	replayer.openlog(s);
	return true;
}

bool
KFS::restore_chunkVersionInc(deque <string> &c)
{
	c.pop_front();
	if (c.empty())
		return false;
	chunkVersionInc = toNumber(c.front());
	return (chunkVersionInc >= 1);
}

static bool
restore_dentry(deque <string> &c)
{
	string name;
	fid_t id, parent;
	c.pop_front();
	bool ok = pop_name(name, "name", c, true);
	ok = pop_fid(id, "id", c, ok);
	ok = pop_fid(parent, "parent", c, ok);
	if (!ok)
		return false;

	MetaDentry *d = new MetaDentry(parent, name, id);
	return (metatree.insert(d) == 0);
}

static bool
restore_fattr(deque <string> &c)
{
	FileType type;
	fid_t fid;
	long long chunkcount;
	struct timeval mtime, ctime, crtime;
	int16_t numReplicas;

	bool ok = pop_type(type, "fattr", c, true);
	ok = pop_fid(fid, "id", c, ok);
	ok = pop_fid(chunkcount, "chunkcount", c, ok);
	ok = pop_short(numReplicas, "numReplicas", c, ok);
	ok = pop_time(mtime, "mtime", c, ok);
	ok = pop_time(ctime, "ctime", c, ok);
	ok = pop_time(crtime, "crtime", c, ok);
	if (!ok)
		return false;

	if (numReplicas < minReplicasPerFile)
		numReplicas = minReplicasPerFile;

	// chunkcount is an estimate; recompute it as we add chunks to the file.
	// reason for it being estimate: if a CP is in progress while the
	// metatree is updated, we have cases where the chunkcount is off by 1
	// and the checkpoint contains the newly added chunk.
	MetaFattr *f = new MetaFattr(type, fid, mtime, ctime, crtime,
					0, numReplicas);
	return (metatree.insert(f) == 0);
}

static bool
restore_chunkinfo(deque <string> &c)
{
	fid_t fid;
	chunkId_t cid;
	off_t offset;
	seq_t chunkVersion;

	c.pop_front();
	bool ok = pop_fid(fid, "fid", c, true);
	ok = pop_fid(cid, "chunkid", c, ok);
	ok = pop_offset(offset, "offset", c, ok);
	ok = pop_fid(chunkVersion, "chunkVersion", c, ok);
	if (!ok)
		return false;

	MetaChunkInfo *ch = new MetaChunkInfo(fid, offset, cid, chunkVersion);
	if (metatree.insert(ch) == 0) {
		MetaFattr *fa = metatree.getFattr(fid);

		assert(fa != NULL);
		fa->chunkcount++;
		gLayoutManager.AddChunkToServerMapping(cid, fid, NULL);
		return true;
	}
	return false;
}

static void
init_map(DiskEntry &e)
{
	e.add_parser("checkpoint", checkpoint_seq);
	e.add_parser("version", checkpoint_version);
	e.add_parser("fid", checkpoint_fid);
	e.add_parser("chunkId", checkpoint_chunkId);
	e.add_parser("time", checkpoint_time);
	e.add_parser("log", checkpoint_log);
	e.add_parser("chunkVersionInc", restore_chunkVersionInc);
	e.add_parser("dentry", restore_dentry);
	e.add_parser("fattr", restore_fattr);
	e.add_parser("chunkinfo", restore_chunkinfo);
}

/*!
 * \brief rebuild metadata tree from CP file cpname
 * \param[in] cpname	the CP file
 * \param[in] minReplicas  the desired # of replicas for each chunk of a file;
 *   if the values in the checkpoint file are below this threshold, then
 *   bump replication.
 * \return		true if successful
 */
bool
Restorer::rebuild(const string cpname, int16_t minReplicas)
{
	const int MAXLINE = 400;
	char line[MAXLINE];
	int lineno = 0;

	DiskEntry entrymap;
	init_map(entrymap);

	file.open(cpname.c_str());
	bool is_ok = !file.fail();

	minReplicasPerFile = minReplicas;

	while (is_ok && !file.eof()) {
		++lineno;
		file.getline(line, MAXLINE);
		is_ok = entrymap.parse(line);
		if (!is_ok)
			std::cerr << "Error at line " << lineno << ": "
					<< line << '\n';
	}

	file.close();
	return is_ok;
}


text:
@component('connection-label')
class ConnectionLabel extends polymer.Base {
  @property({ type: String, notify: true })
  name: String;

  @property({ type: Number })
  xTarget: number;

  @property({ type: Number })
  yTarget: number;

  mouseover: boolean = false;

  editing: boolean = false;

  circleClasses: string;
  editableClasses: string;
  trashButtClasses: string;
  editButtClasses: string;

  @observe("xTarget, yTarget")
  position(xTarget, yTarget) {
    var label = this.$.label;
    label.style.left = xTarget + "px";
    label.style.top = yTarget + "px";
  }

  @listen('mouseover')
  handleMouseover(e) {
    this.mouseover = true;
  }

  @listen('mouseout')
  handleMouseout(e) {
    this.mouseover = false;
  }

  handleEdit(e) {
    this.editing = true;
    this.$.editable.focus();
  }

  handleDelete(e) {
    this.fire("delete-clicked");
  }

  @observe('editableFocused')
  editableFocusChanged(focused) {
    this.editing = focused;
  }

  @observe("mouseover, name, editing")
  state(mouseover, name, editing) {
    if (!this.mouseover && this.name == "" && !this.editing) {
      this.circleClasses = "block";
      this.editableClasses = "none";
      this.trashButtClasses = "none";
      this.editButtClasses = "none";
    } else if (this.mouseover && this.name == "" && !this.editing) {
      this.circleClasses = "none";
      this.editableClasses = "none";
      this.trashButtClasses = "block";
      this.editButtClasses = "block";
    } else if (this.mouseover && this.name != "" && !this.editing) {
      this.circleClasses = "none";
      this.editableClasses = "block half";
      this.editButtClasses = "none";
      this.trashButtClasses = "block trashRight";
    } else if (!this.mouseover && this.name != "") {
      this.circleClasses = "none";
      this.editableClasses = "block";
      this.editButtClasses = "none";
      this.trashButtClasses = "none";
    } else if (this.editing) {
      this.circleClasses = "none";
      this.editableClasses = "block";
      this.editButtClasses = "none";
      this.trashButtClasses = "none";
    }
  }
}

ConnectionLabel.register();


text:
"""Implementation of statistics generators."""

import math
import random
from typing import Any, Callable, cast, Dict, Iterable, List, Optional, Text, Tuple

import apache_beam as beam
import pyarrow as pa
from tensorflow_data_validation import constants
from tensorflow_data_validation import types
from tensorflow_data_validation.arrow import arrow_util
from tensorflow_data_validation.utils import preprocessing_util
from tensorflow_data_validation.statistics import stats_options
from tensorflow_data_validation.statistics.generators import basic_stats_generator
from tensorflow_data_validation.statistics.generators import image_stats_generator
from tensorflow_data_validation.statistics.generators import lift_stats_generator
from tensorflow_data_validation.statistics.generators import natural_language_domain_inferring_stats_generator
from tensorflow_data_validation.statistics.generators import natural_language_stats_generator
from tensorflow_data_validation.statistics.generators import sparse_feature_stats_generator
from tensorflow_data_validation.statistics.generators import stats_generator
from tensorflow_data_validation.statistics.generators import time_stats_generator
from tensorflow_data_validation.statistics.generators import top_k_uniques_sketch_stats_generator
from tensorflow_data_validation.statistics.generators import top_k_uniques_stats_generator
from tensorflow_data_validation.statistics.generators import weighted_feature_stats_generator
from tensorflow_data_validation.utils import feature_partition_util
from tensorflow_data_validation.utils import metrics_util
from tensorflow_data_validation.utils import slicing_util

from tfx_bsl.arrow import table_util
from tfx_bsl.statistics import merge_util
from tfx_bsl.telemetry import collection

from tensorflow_metadata.proto.v0 import schema_pb2
from tensorflow_metadata.proto.v0 import statistics_pb2

_DEFAULT_MG_SKETCH_SIZE = 1024
_DEFAULT_KMV_SKETCH_SIZE = 16384

class GenerateStatisticsImpl(beam.PTransform):
  """PTransform that applies a set of generators over input examples."""

  def __init__(
      self,
      options: stats_options.StatsOptions = stats_options.StatsOptions()
      ) -> None:
    self._options = options

  def expand(
      self, dataset: beam.PCollection[pa.RecordBatch]
  ) -> beam.PCollection[statistics_pb2.DatasetFeatureStatisticsList]:
    # Generate derived features, if applicable.
    if self._options.schema is not None:
      dataset, derivers_configured = preprocessing_util.add_derived_features(
          dataset, self._options.schema)
      if derivers_configured:
        metadata_generator = preprocessing_util.get_metadata_generator()
        assert metadata_generator is not None
        self._options.generators = self._options.generators or []
        self._options.generators.append(metadata_generator)

    # If a set of allowed features are provided, keep only those features.
    if self._options.feature_allowlist:
      dataset |= ('FilterFeaturesByAllowList' >> beam.Map(
          _filter_features, feature_allowlist=self._options.feature_allowlist))

    _ = dataset | 'TrackTotalBytes' >> collection.TrackRecordBatchBytes(
        constants.METRICS_NAMESPACE, 'record_batch_input_bytes')

    if self._options.slicing_config:
      slice_fns, slice_sqls = (
          slicing_util.convert_slicing_config_to_slice_functions_and_sqls(
              self._options.slicing_config))
    else:
      slice_fns, slice_sqls = (self._options.experimental_slice_functions,
                               self._options.experimental_slice_sqls)

    if slice_fns:
      # Add default slicing function.
      slice_functions = [slicing_util.default_slicer]
      slice_functions.extend(slice_fns)
      dataset = (
          dataset
          | 'GenerateSliceKeys' >> beam.FlatMap(
              slicing_util.generate_slices, slice_functions=slice_functions))
    elif slice_sqls:
      dataset = (
          dataset
          | 'GenerateSlicesSql' >> beam.ParDo(
              slicing_util.GenerateSlicesSqlDoFn(slice_sqls=slice_sqls)))
    else:
      dataset = (dataset
                 | 'KeyWithVoid' >> beam.Map(lambda v: (None, v)))
    _ = dataset | 'TrackDistinctSliceKeys' >> _TrackDistinctSliceKeys()  # pylint: disable=no-value-for-parameter
    return dataset | GenerateSlicedStatisticsImpl(self._options)

def _increment_counter(counter_name: Text, element: int):  # pylint: disable=invalid-name
  counter = beam.metrics.Metrics.counter(
      constants.METRICS_NAMESPACE, counter_name)
  counter.inc(element)
  return element

@beam.ptransform_fn
def _TrackDistinctSliceKeys(  # pylint: disable=invalid-name
    slice_keys_and_values: beam.PCollection[types.SlicedRecordBatch]
) -> beam.pvalue.PCollection[int]:
  """Gathers slice key telemetry post slicing."""

  return (slice_keys_and_values
          | 'ExtractSliceKeys' >> beam.Keys()
          | 'RemoveDuplicates' >> beam.Distinct()
          | 'Size' >> beam.combiners.Count.Globally()
          | 'IncrementCounter' >> beam.Map(
              lambda x: _increment_counter('num_distinct_slice_keys', x)))

class _YieldPlaceholderFn(beam.DoFn):
  """Yields a single empty proto if input (count) is zero."""

  def process(self, count: int):
    if count == 0:
      yield ('', statistics_pb2.DatasetFeatureStatistics())

@beam.ptransform_fn
def _AddPlaceholderStatistics(  # pylint: disable=invalid-name
    statistics: beam.PCollection[Tuple[
        types.SliceKey, statistics_pb2.DatasetFeatureStatistics]]):
  """Adds a placeholder empty dataset for empty input, otherwise noop."""
  count = statistics | beam.combiners.Count.Globally()
  maybe_placeholder = count | beam.ParDo(_YieldPlaceholderFn())
  return (statistics, maybe_placeholder) | beam.Flatten()

def _split_generator_types(
    generators: List[stats_generator.StatsGenerator], num_partitions: int
) -> Tuple[List[stats_generator.TransformStatsGenerator],
           List[stats_generator.CombinerStatsGenerator],
           List[stats_generator.CombinerStatsGenerator]]:
  """Split generators by type.

  Args:
    generators: A list of generators.
    num_partitions: The number of feature partitions to split by.

  Returns:
    A three tuple consisting of 1) TransformStatsGenerators 2)
    CombinerStatsGenerators that should not be feature-partitioned 3)
    CombinerStatsGenerators that should be feature-partitioned

  Raises:
    TypeError: If provided generators are not instances of
      TransformStatsGenerators or CombinerStatsGenerator.
  """
  transform_generators = []
  unpartitioned_combiners = []
  partitioned_combiners = []
  for generator in generators:
    if isinstance(generator, stats_generator.TransformStatsGenerator):
      transform_generators.append(generator)
    elif isinstance(generator, stats_generator.CombinerStatsGenerator):
      if num_partitions > 1:
        try:
          _ = generator._copy_for_partition_index(0, num_partitions)  # pylint: disable=protected-access
          partitioned_combiners.append(generator)
        except NotImplementedError:
          unpartitioned_combiners.append(generator)
      else:
        unpartitioned_combiners.append(generator)
    else:
      raise TypeError('Statistics generator must extend one of '
                      'CombinerStatsGenerator or TransformStatsGenerator, '
                      'found object of type %s' % generator.__class__.__name__)
  return transform_generators, unpartitioned_combiners, partitioned_combiners

# This transform will be used by the example validation API to compute
# statistics over anomalous examples. Specifically, it is used to compute
# statistics over examples found for each anomaly (i.e., the anomaly type
# will be the slice key).
class GenerateSlicedStatisticsImpl(beam.PTransform):
  """PTransform that applies a set of generators to sliced input examples."""

  def __init__(
      self,
      options: stats_options.StatsOptions = stats_options.StatsOptions(),
      is_slicing_enabled: bool = False,
      ) -> None:
    """Initializes GenerateSlicedStatisticsImpl.

    Args:
      options: `tfdv.StatsOptions` for generating data statistics.
      is_slicing_enabled: Whether to include slice keys in the resulting proto,
        even if slice functions or slicing SQL queries are not provided in
        `options`. If slice functions or slicing SQL queries are provided in
        `options`, slice keys are included regardless of this value.
    """
    self._options = options
    self._is_slicing_enabled = (
        is_slicing_enabled or
        bool(self._options.experimental_slice_functions) or
        bool(self._options.experimental_slice_sqls) or
        bool(self._options.slicing_config))

  def _to_partitioned_combiner_stats_generator_combine_fn(
      self, generators: List[stats_generator.CombinerStatsGenerator]
  ) -> List['_CombinerStatsGeneratorsCombineFn']:
    """Produce one CombineFn per partition wrapping partitioned generators."""
    if not generators:
      return []
    result = []
    for idx in range(self._options.experimental_num_feature_partitions):
      index_generators = [
          g._copy_for_partition_index(  # pylint: disable=protected-access
              idx, self._options.experimental_num_feature_partitions)
          for g in generators
      ]
      result.append(
          _CombinerStatsGeneratorsCombineFn(index_generators,
                                            self._options.desired_batch_size))
    return result

  def expand(self, dataset: beam.PCollection[types.SlicedRecordBatch]):
    # Collect telemetry on what generators are in use.
    generators = get_generators(self._options)
    generator_name_counts = {'generator_%s' % g.name: 1 for g in generators}
    _ = (
        dataset | metrics_util.IncrementJobCounters(generator_name_counts))

    # Handles generators by their type:
    #   - CombinerStatsGenerators will be wrapped in a single CombinePerKey by
    #     _CombinerStatsGeneratorsCombineFn.
    #   - TransformStatsGenerator will be invoked separately with `dataset`.
    result_protos = []
    (transform_generators, unpartitioned_combiners,
     partitioned_combiners) = _split_generator_types(
         generators, self._options.experimental_num_feature_partitions)
    # Set up combineFns.
    combine_fns = []
    if unpartitioned_combiners:
      combine_fns.append(
          _CombinerStatsGeneratorsCombineFn(unpartitioned_combiners,
                                            self._options.desired_batch_size))
    if partitioned_combiners:
      combine_fns.extend(
          self._to_partitioned_combiner_stats_generator_combine_fn(
              partitioned_combiners))

    # Apply transform generators.
    for generator in transform_generators:
      result_protos.append(dataset | generator.name >> generator.ptransform)

    # Apply combiner stats generators.
    # TODO(b/162543416): Obviate the need for explicit fanout.
    fanout = max(32,
                 5 * int(math.ceil(math.sqrt(len(combine_fns)))))
    for i, combine_fn in enumerate(combine_fns):
      result_protos.append(
          dataset
          | 'RunCombinerStatsGenerators[%d]' %
          i >> beam.CombinePerKey(combine_fn).with_hot_key_fanout(fanout))
    # TODO(b/154367505): Restore use of _AddPlaceholderStatistics.
    # TODO(b/217756011): Restore use of merge_dataset_feature_statistics_list.
    if self._options.experimental_result_partitions == 1:
      return (result_protos
              | 'FlattenFeatureStatistics' >> beam.Flatten()
              | 'AddSliceKeyToStatsProto' >> beam.Map(_add_slice_key,
                                                      self._is_slicing_enabled)
              | 'ToList' >> beam.combiners.ToList()
              | 'MergeDatasetFeatureStatisticsProtos' >> beam.Map(
                  merge_util.merge_dataset_feature_statistics))

    result_protos = result_protos | 'FlattenFeatureStatistics' >> beam.Flatten()
    result_protos = (
        result_protos
        | 'AddPlaceholderStatistics' >> _AddPlaceholderStatistics())  # pylint: disable=no-value-for-parameter
    # Combine result_protos into a configured number of partitions.
    return (result_protos
            | 'AddSliceKeyToStatsProto' >> beam.Map(_add_slice_key,
                                                    self._is_slicing_enabled)
            | 'MakeDatasetFeatureStatisticsListProto' >>
            beam.Map(_make_singleton_dataset_feature_statistics_list_proto)
            | 'SplitIntoFeaturePartitions' >> beam.ParDo(
                feature_partition_util.KeyAndSplitByFeatureFn(
                    self._options.experimental_result_partitions))
            | 'MergeStatsProtos' >> beam.CombinePerKey(
                merge_util.merge_dataset_feature_statistics_list)
            | 'Values' >> beam.Values())

def get_generators(options: stats_options.StatsOptions,
                   in_memory: bool = False
                  ) -> List[stats_generator.StatsGenerator]:
  """Initializes the list of stats generators, including custom generators.

  Args:
    options: A StatsOptions object.
    in_memory: Whether the generators will be used to generate statistics in
      memory (True) or using Beam (False).

  Returns:
    A list of stats generator objects.
  """
  generators = []
  if options.add_default_generators:
    generators.extend(_get_default_generators(options, in_memory))
  if options.generators:
    # Add custom stats generators.
    generators.extend(options.generators)
  if options.enable_semantic_domain_stats:
    semantic_domain_feature_stats_generators = [
        image_stats_generator.ImageStatsGenerator(),
        natural_language_domain_inferring_stats_generator
        .NLDomainInferringStatsGenerator(),
        time_stats_generator.TimeStatsGenerator(),
    ]
    # Wrap semantic domain feature stats generators as a separate combiner
    # stats generator, so that we can apply sampling only for those and other
    # feature stats generators are not affected by it.
    generators.append(
        CombinerFeatureStatsWrapperGenerator(
            semantic_domain_feature_stats_generators,
            sample_rate=options.semantic_domain_stats_sample_rate))
  if options.schema is not None:
    if _schema_has_sparse_features(options.schema):
      generators.append(
          sparse_feature_stats_generator.SparseFeatureStatsGenerator(
              options.schema))
    if _schema_has_natural_language_domains(options.schema):
      generators.append(
          natural_language_stats_generator.NLStatsGenerator(
              options.schema, options.vocab_paths,
              options.num_histogram_buckets,
              options.num_quantiles_histogram_buckets,
              options.num_rank_histogram_buckets))
    if options.schema.weighted_feature:
      generators.append(
          weighted_feature_stats_generator.WeightedFeatureStatsGenerator(
              options.schema))
    if options.label_feature and not in_memory:
      # The LiftStatsGenerator is not a CombinerStatsGenerator and therefore
      # cannot currenty be used for in_memory executions.
      generators.append(
          lift_stats_generator.LiftStatsGenerator(
              y_path=types.FeaturePath([options.label_feature]),
              schema=options.schema,
              example_weight_map=options.example_weight_map,
              output_custom_stats=True))

  # Replace all CombinerFeatureStatsGenerator with a single
  # CombinerFeatureStatsWrapperGenerator.
  feature_generators = [
      x for x in generators
      if isinstance(x, stats_generator.CombinerFeatureStatsGenerator)
  ]
  if feature_generators:
    generators = [
        x for x in generators
        if not isinstance(x, stats_generator.CombinerFeatureStatsGenerator)
    ] + [
        CombinerFeatureStatsWrapperGenerator(feature_generators)
    ]
  if in_memory:
    for generator in generators:
      if not isinstance(generator, stats_generator.CombinerStatsGenerator):
        raise TypeError('Statistics generator used in '
                        'generate_statistics_in_memory must '
                        'extend CombinerStatsGenerator, found object of '
                        'type %s.' % generator.__class__.__name__)
  return generators

def _get_default_generators(
    options: stats_options.StatsOptions, in_memory: bool = False
) -> List[stats_generator.StatsGenerator]:
  """Initializes default list of stats generators.

  Args:
    options: A StatsOptions object.
    in_memory: Whether the generators will be used to generate statistics in
      memory (True) or using Beam (False).

  Returns:
    A list of stats generator objects.
  """
  stats_generators = [
      basic_stats_generator.BasicStatsGenerator(
          schema=options.schema,
          example_weight_map=options.example_weight_map,
          num_values_histogram_buckets=options.num_values_histogram_buckets,
          num_histogram_buckets=options.num_histogram_buckets,
          num_quantiles_histogram_buckets=options
          .num_quantiles_histogram_buckets,
          epsilon=options.epsilon),
  ]
  if options.use_sketch_based_topk_uniques or in_memory:
    stats_generators.append(
        top_k_uniques_sketch_stats_generator.TopKUniquesSketchStatsGenerator(
            schema=options.schema,
            example_weight_map=options.example_weight_map,
            num_top_values=options.num_top_values,
            num_rank_histogram_buckets=options.num_rank_histogram_buckets,
            frequency_threshold=options.frequency_threshold,
            weighted_frequency_threshold=options.weighted_frequency_threshold,
            num_misragries_buckets=_DEFAULT_MG_SKETCH_SIZE,
            num_kmv_buckets=_DEFAULT_KMV_SKETCH_SIZE))
  else:
    stats_generators.append(
        top_k_uniques_stats_generator.TopKUniquesStatsGenerator(
            schema=options.schema,
            example_weight_map=options.example_weight_map,
            num_top_values=options.num_top_values,
            frequency_threshold=options.frequency_threshold,
            weighted_frequency_threshold=options.weighted_frequency_threshold,
            num_rank_histogram_buckets=options.num_rank_histogram_buckets),
    )
  return stats_generators

def _schema_has_sparse_features(schema: schema_pb2.Schema) -> bool:
  """Returns whether there are any sparse features in the specified schema."""

  def _has_sparse_features(
      feature_container: Iterable[schema_pb2.Feature]
  ) -> bool:
    """Helper function used to determine whether there are sparse features."""
    for f in feature_container:
      if isinstance(f, schema_pb2.SparseFeature):
        return True
      if f.type == schema_pb2.STRUCT:
        if f.struct_domain.sparse_feature:
          return True
        return _has_sparse_features(f.struct_domain.feature)
    return False

  if schema.sparse_feature:
    return True
  return _has_sparse_features(schema.feature)

def _schema_has_natural_language_domains(schema: schema_pb2.Schema) -> bool:
  """Returns whether there are features in the schema with a nl domain."""
  for f in schema.feature:
    if f.WhichOneof('domain_info') == 'natural_language_domain':
      return True
  return False

def _filter_features(
    record_batch: pa.RecordBatch,
    feature_allowlist: List[types.FeatureName]) -> pa.RecordBatch:
  """Removes features that are not on the allowlist.

  Args:
    record_batch: Input Arrow RecordBatch.
    feature_allowlist: A set of feature names to keep.

  Returns:
    An Arrow RecordBatch containing only features on the allowlist.
  """
  columns_to_select = []
  column_names_to_select = []
  for feature_name in feature_allowlist:
    col = arrow_util.get_column(record_batch, feature_name, missing_ok=True)
    if col is None:
      continue
    columns_to_select.append(col)
    column_names_to_select.append(feature_name)
  return pa.RecordBatch.from_arrays(columns_to_select, column_names_to_select)

def _add_slice_key(
    stats_proto_per_slice: Tuple[types.SliceKey,
                                 statistics_pb2.DatasetFeatureStatistics],
    is_slicing_enabled: bool
) -> statistics_pb2.DatasetFeatureStatistics:
  """Add slice key to stats proto."""
  result = statistics_pb2.DatasetFeatureStatistics()
  result.CopyFrom(stats_proto_per_slice[1])
  if is_slicing_enabled:
    result.name = stats_proto_per_slice[0]
  return result

def _make_singleton_dataset_feature_statistics_list_proto(
    statistics: statistics_pb2.DatasetFeatureStatistics
) -> statistics_pb2.DatasetFeatureStatisticsList:
  """Wrap statistics in a DatasetFeatureStatisticsList proto."""
  result = statistics_pb2.DatasetFeatureStatisticsList()
  new_stats_proto = result.datasets.add()
  new_stats_proto.CopyFrom(statistics)
  return result

class _CombinerStatsGeneratorsCombineFnAcc(object):
  """accumulator for _CombinerStatsGeneratorsCombineFn."""

  __slots__ = [
      'partial_accumulators', 'input_record_batches', 'curr_batch_size',
      'curr_byte_size'
  ]

  def __init__(self, partial_accumulators: List[Any]):
    # Partial accumulator states of the underlying CombinerStatsGenerators.
    self.partial_accumulators = partial_accumulators
    # Input record batches to be processed.
    self.input_record_batches = []
    # Current batch size.
    self.curr_batch_size = 0
    # Current total byte size of all the pa.RecordBatches accumulated.
    self.curr_byte_size = 0

@beam.typehints.with_input_types(pa.RecordBatch)
@beam.typehints.with_output_types(statistics_pb2.DatasetFeatureStatistics)
class _CombinerStatsGeneratorsCombineFn(beam.CombineFn):
  """A beam.CombineFn wrapping a list of CombinerStatsGenerators with batching.

  This wrapper does two things:
    1. Wraps a list of combiner stats generators. Its accumulator is a list
       of accumulators for each wrapped stats generators.
    2. Batches input examples before passing it to the underlying
       stats generators.

  We do this by accumulating examples in the combiner state until we
  accumulate a large enough batch, at which point we send them through the
  add_input step of each of the underlying combiner stats generators. When
  merging, we merge the accumulators of the stats generators and accumulate
  examples accordingly. We finally process any remaining examples
  before producing the final output value.

  This wrapper is needed to support slicing as we need the ability to
  perform slice-aware batching. But currently there is no way to do key-aware
  batching in Beam. Hence, this wrapper does batching and combining together.

  See also:
  BEAM-3737: Key-aware batching function
  (https://issues.apache.org/jira/browse/BEAM-3737).
  """

  # The combiner accumulates record batches from the upstream and merges them
  # when certain conditions are met. A merged record batch would allow better
  # vectorized processing, but we have to pay for copying and the RAM to
  # contain the merged record batch. If the total byte size of accumulated
  # record batches exceeds this threshold a merge will be forced to avoid
  # consuming too much memory.
  _MERGE_RECORD_BATCH_BYTE_SIZE_THRESHOLD = 20 << 20  # 20MiB

  def __init__(
      self,
      generators: List[stats_generator.CombinerStatsGenerator],
      desired_batch_size: Optional[int] = None) -> None:
    self._generators = generators

    # We really want the batch size to be adaptive like it is in
    # beam.BatchElements(), but there isn't an easy way to make it so.
    # TODO(b/73789023): Figure out how to make this batch size dynamic.
    if desired_batch_size and desired_batch_size > 0:
      self._desired_batch_size = desired_batch_size
    else:
      self._desired_batch_size = constants.DEFAULT_DESIRED_INPUT_BATCH_SIZE

    # Metrics
    self._combine_batch_size = beam.metrics.Metrics.distribution(
        constants.METRICS_NAMESPACE, 'combine_batch_size')
    self._combine_byte_size = beam.metrics.Metrics.distribution(
        constants.METRICS_NAMESPACE, 'combine_byte_size')
    self._num_compacts = beam.metrics.Metrics.counter(
        constants.METRICS_NAMESPACE, 'num_compacts')
    self._num_instances = beam.metrics.Metrics.counter(
        constants.METRICS_NAMESPACE, 'num_instances')

  def _for_each_generator(self,
                          func: Callable[..., Any],
                          *args: Iterable[Any]) -> List[Any]:
    """Apply `func` for each wrapped generators.

    Args:
      func: a function that takes N + 1 arguments where N is the size of `args`.
        the first argument is the stats generator.
      *args: Iterables parallel to wrapped stats generators (i.e. the i-th item
        corresponds to the self._generators[i]).
    Returns:
      A list whose i-th element is the result of
      func(self._generators[i], args[0][i], args[1][i], ...).
    """
    return [func(gen, *args_for_func) for gen, args_for_func in zip(
        self._generators, zip(*args))]

  def _should_do_batch(self, accumulator: _CombinerStatsGeneratorsCombineFnAcc,
                       force: bool) -> bool:
    curr_batch_size = accumulator.curr_batch_size
    if force and curr_batch_size > 0:
      return True

    if curr_batch_size >= self._desired_batch_size:
      return True

    if (accumulator.curr_byte_size >=
        self._MERGE_RECORD_BATCH_BYTE_SIZE_THRESHOLD):
      return True

    return False

  def _maybe_do_batch(
      self,
      accumulator: _CombinerStatsGeneratorsCombineFnAcc,
      force: bool = False) -> None:
    """Maybe updates accumulator in place.

    Checks if accumulator has enough examples for a batch, and if so, does the
    stats computation for the batch and updates accumulator in place.

    Args:
      accumulator: Accumulator. Will be updated in place.
      force: Force computation of stats even if accumulator has less examples
        than the batch size.
    """
    if self._should_do_batch(accumulator, force):
      self._combine_batch_size.update(accumulator.curr_batch_size)
      self._combine_byte_size.update(accumulator.curr_byte_size)
      if len(accumulator.input_record_batches) == 1:
        record_batch = accumulator.input_record_batches[0]
      else:
        record_batch = table_util.MergeRecordBatches(
            accumulator.input_record_batches)
      accumulator.partial_accumulators = self._for_each_generator(
          lambda gen, gen_acc: gen.add_input(gen_acc, record_batch),
          accumulator.partial_accumulators)
      del accumulator.input_record_batches[:]
      accumulator.curr_batch_size = 0
      accumulator.curr_byte_size = 0

  def setup(self):
    """Prepares each generator for combining."""
    for gen in self._generators:
      gen.setup()

  def create_accumulator(self) -> _CombinerStatsGeneratorsCombineFnAcc:
    return _CombinerStatsGeneratorsCombineFnAcc(
        [g.create_accumulator() for g in self._generators])

  def add_input(
      self, accumulator: _CombinerStatsGeneratorsCombineFnAcc,
      input_record_batch: pa.RecordBatch
  ) -> _CombinerStatsGeneratorsCombineFnAcc:
    accumulator.input_record_batches.append(input_record_batch)
    num_rows = input_record_batch.num_rows
    accumulator.curr_batch_size += num_rows
    accumulator.curr_byte_size += input_record_batch.nbytes
    self._maybe_do_batch(accumulator)
    self._num_instances.inc(num_rows)
    return accumulator

  def merge_accumulators(
      self,
      accumulators: Iterable[_CombinerStatsGeneratorsCombineFnAcc]
      ) -> _CombinerStatsGeneratorsCombineFnAcc:
    it = iter(accumulators)
    result = next(it)
    for accumulator in it:
      result.input_record_batches.extend(accumulator.input_record_batches)
      result.curr_batch_size += accumulator.curr_batch_size
      result.curr_byte_size += accumulator.curr_byte_size
      self._maybe_do_batch(result)
      result.partial_accumulators = self._for_each_generator(
          lambda gen, x, y: gen.merge_accumulators([x, y]),
          result.partial_accumulators,
          accumulator.partial_accumulators)

    return result

  def compact(
      self,
      accumulator: _CombinerStatsGeneratorsCombineFnAcc
      ) -> _CombinerStatsGeneratorsCombineFnAcc:
    self._maybe_do_batch(accumulator, force=True)
    accumulator.partial_accumulators = self._for_each_generator(
        lambda gen, acc: gen.compact(acc), accumulator.partial_accumulators)
    self._num_compacts.inc(1)
    return accumulator

  def extract_output(
      self, accumulator: _CombinerStatsGeneratorsCombineFnAcc
  ) -> statistics_pb2.DatasetFeatureStatistics:
    # Make sure we have processed all the examples.
    self._maybe_do_batch(accumulator, force=True)
    generator_outputs = self._for_each_generator(
        lambda gen, acc: gen.extract_output(acc),
        accumulator.partial_accumulators)
    # TODO(b/202910677): We should consider returning a list directly and not
    # merging at all.
    merged = merge_util.merge_dataset_feature_statistics(generator_outputs)
    if len(merged.datasets) != 1:
      raise ValueError(
          'Expected a single slice key in _CombinerStatsGeneratorsCombineFn, '
          'got %d' % len(merged.datasets))
    return merged.datasets[0]

def generate_partial_statistics_in_memory(
    record_batch: pa.RecordBatch, options: stats_options.StatsOptions,
    stats_generators: List[stats_generator.CombinerStatsGenerator]
) -> List[Any]:
  """Generates statistics for an in-memory list of examples.

  Args:
    record_batch: Arrow RecordBatch.
    options: Options for generating data statistics.
    stats_generators: A list of combiner statistics generators.

  Returns:
    A list of accumulators containing partial statistics.
  """
  result = []
  if options.feature_allowlist:
    columns, features = [], []
    for feature_name in options.feature_allowlist:
      c = arrow_util.get_column(record_batch, feature_name, missing_ok=True)
      if c is not None:
        columns.append(c)
        features.append(feature_name)
    record_batch = pa.RecordBatch.from_arrays(columns, features)
  for generator in stats_generators:
    result.append(
        generator.add_input(generator.create_accumulator(), record_batch))
  return result

def generate_statistics_in_memory(
    record_batch: pa.RecordBatch,
    options: stats_options.StatsOptions = stats_options.StatsOptions()
) -> statistics_pb2.DatasetFeatureStatisticsList:
  """Generates statistics for an in-memory list of examples.

  Args:
    record_batch: Arrow RecordBatch.
    options: Options for generating data statistics.

  Returns:
    A DatasetFeatureStatisticsList proto.
  """
  stats_generators = cast(List[stats_generator.CombinerStatsGenerator],
                          get_generators(options, in_memory=True))
  partial_stats = generate_partial_statistics_in_memory(record_batch, options,
                                                        stats_generators)
  return extract_statistics_output(partial_stats, stats_generators)

def extract_statistics_output(
    partial_stats: List[Any],
    stats_generators: List[stats_generator.CombinerStatsGenerator]
) -> statistics_pb2.DatasetFeatureStatisticsList:
  """Extracts final stats output from the accumulators holding partial stats."""

  # We call compact before extract_output to guarentee that `compact()` is
  # called at least once, for testing coverage.
  outputs = [
      gen.extract_output(gen.compact(stats))
      for (gen, stats) in zip(stats_generators, partial_stats)  # pytype: disable=attribute-error
  ]
  return merge_util.merge_dataset_feature_statistics(outputs)

# Type for the wrapper_accumulator of a CombinerFeatureStatsWrapperGenerator.
# See documentation below for more details.
WrapperAccumulator = Dict[types.FeaturePath, List[Any]]

class CombinerFeatureStatsWrapperGenerator(
    stats_generator.CombinerStatsGenerator):
  """A combiner that wraps multiple CombinerFeatureStatsGenerators.

  This combiner wraps multiple CombinerFeatureStatsGenerators by generating
  and updating wrapper_accumulators where:
  wrapper_accumulator[feature_path][feature_generator_index] contains the
  generator specific accumulator for the pair (feature_path,
  feature_generator_index).
  """

  def __init__(self,
               feature_stats_generators: List[
                   stats_generator.CombinerFeatureStatsGenerator],
               name: Text = 'CombinerFeatureStatsWrapperGenerator',
               schema: Optional[schema_pb2.Schema] = None,
               sample_rate: Optional[float] = None) -> None:
    """Initializes a CombinerFeatureStatsWrapperGenerator.

    Args:
      feature_stats_generators: A list of CombinerFeatureStatsGenerator.
      name: An optional unique name associated with the statistics generator.
      schema: An optional schema for the dataset.
      sample_rate: An optional sampling rate. If specified, statistics is
        computed over the sample.
    """
    super(CombinerFeatureStatsWrapperGenerator, self).__init__(name, schema)
    self._feature_stats_generators = feature_stats_generators
    self._sample_rate = sample_rate

  def _get_wrapped_accumulators(self, wrapper_accumulator: WrapperAccumulator,
                                feature_path: types.FeaturePath) -> List[Any]:
    """Initializes the feature_path key if it does not exist."""
    result = wrapper_accumulator.get(feature_path, None)
    if result is not None:
      return result
    # Note: This manual initialization could have been avoided if
    # wrapper_accumulator was a defaultdict, but this breaks pickling.
    result = [
        generator.create_accumulator()
        for generator in self._feature_stats_generators
    ]
    wrapper_accumulator[feature_path] = result
    return result

  def setup(self):
    """Prepares every CombinerFeatureStatsGenerator instance for combining."""
    for gen in self._feature_stats_generators:
      gen.setup()

  def create_accumulator(self) -> WrapperAccumulator:
    """Returns a fresh, empty wrapper_accumulator.

    Returns:
      An empty wrapper_accumulator.
    """
    return {}

  def add_input(self, wrapper_accumulator: WrapperAccumulator,
                input_record_batch: pa.RecordBatch) -> WrapperAccumulator:
    """Returns result of folding a batch of inputs into wrapper_accumulator.

    Args:
      wrapper_accumulator: The current wrapper accumulator.
      input_record_batch: An arrow RecordBatch representing a batch of examples,
      which should be added to the accumulator.

    Returns:
      The wrapper_accumulator after updating the statistics for the batch of
      inputs.
    """
    if self._sample_rate is not None and random.random() > self._sample_rate:
      return wrapper_accumulator

    for feature_path, feature_array, _ in arrow_util.enumerate_arrays(
        input_record_batch,
        example_weight_map=None,
        enumerate_leaves_only=True):
      wrapped_accumulators = self._get_wrapped_accumulators(
          wrapper_accumulator, feature_path)
      for index, generator in enumerate(self._feature_stats_generators):
        wrapped_accumulators[index] = generator.add_input(
            wrapped_accumulators[index], feature_path, feature_array)

    return wrapper_accumulator

  def merge_accumulators(
      self,
      wrapper_accumulators: Iterable[WrapperAccumulator]) -> WrapperAccumulator:
    """Merges several wrapper_accumulators to a single one.

    Args:
      wrapper_accumulators: The wrapper accumulators to merge.

    Returns:
      The merged accumulator.
    """
    result = self.create_accumulator()
    for wrapper_accumulator in wrapper_accumulators:
      for feature_path, accumulator_for_feature in wrapper_accumulator.items():
        wrapped_accumulators = self._get_wrapped_accumulators(
            result, feature_path)
        for index, generator in enumerate(self._feature_stats_generators):
          wrapped_accumulators[index] = generator.merge_accumulators(
              [wrapped_accumulators[index], accumulator_for_feature[index]])
    return result

  def compact(self,
              wrapper_accumulator: WrapperAccumulator) -> WrapperAccumulator:
    """Returns a compacted wrapper_accumulator.

    This overrides the base class's implementation. This is optionally called
    before an accumulator is sent across the wire.

    Args:
      wrapper_accumulator: The wrapper accumulator to compact.
    """
    for accumulator_for_feature in wrapper_accumulator.values():
      for index, generator in enumerate(self._feature_stats_generators):
        accumulator_for_feature[index] = generator.compact(
            accumulator_for_feature[index])

    return wrapper_accumulator

  def extract_output(self, wrapper_accumulator: WrapperAccumulator
                    ) -> statistics_pb2.DatasetFeatureStatistics:
    """Returns result of converting wrapper_accumulator into the output value.

    Args:
      wrapper_accumulator: The final wrapper_accumulator value.

    Returns:
      A proto representing the result of this stats generator.
    """
    result = statistics_pb2.DatasetFeatureStatistics()

    for feature_path, accumulator_for_feature in wrapper_accumulator.items():
      feature_stats = result.features.add()
      feature_stats.path.CopyFrom(feature_path.to_proto())
      for index, generator in enumerate(self._feature_stats_generators):
        feature_stats.MergeFrom(
            generator.extract_output(accumulator_for_feature[index]))
    return result


text:
const chroma = require('chroma-js');

const START_COLOR = 'red';
const END_COLOR = 'blue';

/**
 * Get a given frame from a gif.
 *
 * Handles looping indices and negative indices.
 *
 * @param imageData Data with frames.
 * @param index Input frame.
 * @param bounce Instead of overflowing, reverse so that the counter bounces between [0, len).
 */
const getFrame = (imageData, index, bounce) => {
    const len = imageData.frames.length;
    if (bounce) {
        index %= len * 2;
        if (index < 0)
            index = len * 2 - 1 - Math.abs(index);
        if (index >= len)
            index = len - 1 - (index - len);
    } else {
        index %= len;
        if (index < 0)
            index = len - 1 - Math.abs(index);
    }
    return index;
};

/**
 * Prepare a canvas for rendering a gif.
 */
const prepCanvas = (canvas, ctx, imageData) => {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    canvas.width = imageData.width;
    canvas.height = imageData.height;
};

const drawFrame = (ctx, imageData, frame, debug = false) => {
    if (debug) {
        ctx.fillStyle = chroma.mix(START_COLOR, END_COLOR, frame / imageData.frames.length, 'rgb').css();
        ctx.beginPath();
        ctx.rect(0, 0, imageData.width, imageData.height);
        ctx.fill();
    } else {
        ctx.drawImage(imageData.frames[frame].canvas, 0, 0);
    }
};

/**
 * Render gif using diagonal scanlines
 */
export const drawDiag = (canvas, ctx, imageData, initialFrame, increment, bounce, gridColumns, angle, debug = false) => {
    const radAngle = angle * (Math.PI / 180);
    const {width, height} = imageData;
    const diag = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));

    prepCanvas(canvas, ctx, imageData);

    for (let i = 0, numDraws = Math.ceil((diag / gridColumns) / 2); i <= numDraws; ++i) {
        const frame1 = getFrame(imageData, initialFrame + i * increment, bounce);
        const frame2 = getFrame(imageData, initialFrame - ((i + 1) * increment), bounce);

        // draw first
        ctx.save();
        {
            ctx.save();
            ctx.translate(width / 2, height / 2);
            ctx.rotate(radAngle);
            ctx.translate(-width / 2, -height / 2);
            ctx.beginPath();
            ctx.rect(-diag, height / 2 + (i * gridColumns), diag * 2, gridColumns);
            ctx.restore();

            ctx.clip();
            drawFrame(ctx, imageData, frame1, debug);
        }
        ctx.restore();

        // draw second
        ctx.save();
        {
            ctx.save();
            ctx.translate(width / 2, height / 2);
            ctx.rotate(radAngle);
            ctx.translate(-width / 2, -height / 2);
            ctx.beginPath();
            ctx.rect(-diag, height / 2 - (i * gridColumns), diag * 2, gridColumns);
            ctx.restore();
            ctx.clip();

            drawFrame(ctx, imageData, frame2, debug);
        }
        ctx.restore();
    }
};

/**
 * Render gif using a grid.
 */
export const drawGrid = (canvas, ctx, imageData, initialFrame, increment, bounce, columnWidth, columnHeight, debug = false) => {
    const {width, height} = imageData;
    prepCanvas(canvas, ctx, imageData);

    let i = initialFrame;
    for (let x = 0; x < width; x += columnWidth) {
        for (let y = 0; y < height; y += columnHeight) {
            const frame = getFrame(imageData, i, bounce);
            ctx.save();

            // Create clipping rect.
            ctx.beginPath();
            ctx.rect(x, y, columnWidth, columnHeight)
            ctx.clip();

            drawFrame(ctx, imageData, frame, debug);

            ctx.restore();

            i += increment;
        }
    }
};

/**
 * Render gif using circles.
 */
export const drawCircle = (canvas, ctx, imageData, initialFrame, increment, bounce, radiusStep, debug = false) => {
    const {width, height} = imageData;
    prepCanvas(canvas, ctx, imageData);

    let i = initialFrame;
    for (let r = 0, len = Math.max(width, height); r < len; r += radiusStep) {
        const frame = getFrame(imageData, i, bounce);
        ctx.save();

        // Create clipping circle.
        ctx.beginPath();
        ctx.arc(width / 2, height / 2, r + radiusStep, 0, Math.PI * 2, false);
        ctx.arc(width / 2, height / 2, r, 0, Math.PI * 2, true);
        ctx.clip()

        drawFrame(ctx, imageData, frame, debug);

        ctx.restore();

        i += increment;
    }
};

/**
 * Draw a scanlined gif for a set of options
 */
export const drawForOptions = (canvas, ctx, imageData, state) => {
    const increment = state.reverseFrameOrder ? -state.frameIncrement : state.frameIncrement;
    const frame = state.initialFrame + state.currentFrame;

    switch (state.mode) {
        case 'columns':
            return drawGrid(
                canvas,
                ctx,
                imageData,
                frame,
                increment,
                state.bounceFrameOrder,
                imageData.width / imageData.frames.length,
                imageData.height);

        case 'rows':
        default:
            return drawGrid(
                canvas,
                ctx,
                imageData,
                frame,
                increment,
                state.bounceFrameOrder,
                imageData.width,
                imageData.height / imageData.frames.length);

        case 'grid':
            return drawGrid(
                canvas,
                ctx,
                imageData,
                frame,
                increment,
                state.bounceFrameOrder,
                imageData.width / state.gridColumns,
                imageData.height / state.gridRows);

        case 'diagonal':
            return drawDiag(
                canvas,
                ctx,
                imageData,
                frame,
                increment,
                state.bounceFrameOrder,
                state.diagonalWidth,
                state.diagonalAngle);

        case 'circle':
            return drawCircle(
                canvas,
                ctx,
                imageData,
                frame,
                increment,
                state.bounceFrameOrder,
                state.radiusWidth);
    }
};


text:
package com.what2p.repositorys;

import com.what2p.domains.Payment;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * Payment的Repository类
 */
public interface PaymentRepository extends JpaRepository<Payment, Long> {
}


text:
It supports Java, Clojure, Scala, JRuby, and anything else that runs on the JVM.

## Using
### Assets
If you use Maven, you can simply reference the assets below.  The latest
version can be found on in the maven repository for
[io.prometheus](http://mvnrepository.com/artifact/io.prometheus) and
[io.prometheus.client.utility](http://mvnrepository.com/artifact/io.prometheus.client.utility).

#### Simpleclient

```
<!-- The client -->
<dependency>
  <groupId>io.prometheus</groupId>
  <artifactId>simpleclient</artifactId>
  <version>0.0.6</version>
</dependency>
<!-- Hotspot JVM metrics-->
<dependency>
  <groupId>io.prometheus</groupId>
  <artifactId>simpleclient_hotspot</artifactId>
  <version>0.0.6</version>
</dependency>
<!-- Exposition servlet-->
<dependency>
  <groupId>io.prometheus</groupId>
  <artifactId>simpleclient_servlet</artifactId>
  <version>0.0.6</version>
</dependency>
<!-- Pushgateway exposition-->
<dependency>
  <groupId>io.prometheus</groupId>
  <artifactId>simpleclient_pushgateway</artifactId>
  <version>0.0.6</version>
</dependency>
```

#### Original client
```
<!-- The client -->
<dependency>
  <groupId>io.prometheus</groupId>
  <artifactId>client</artifactId>
  <version>0.0.6</version>
</dependency>
<!-- Hotspot 'jvmstat/perfdata' metrics -->
<dependency>
  <groupId>io.prometheus.client.utility</groupId>
  <artifactId>jvmstat</artifactId>
  <version>0.0.6</version>
</dependency>
<!-- Hotspot 'jvmstat/perfdata' metrics -->
<dependency>
  <groupId>io.prometheus.client.utility</groupId>
  <artifactId>jvmstat</artifactId>
  <version>0.0.6</version>
</dependency>
<!-- Hotspot VM metrics -->
<dependency>
  <groupId>io.prometheus.client.utility</groupId>
  <artifactId>metrics</artifactId>
  <version>0.0.6</version>
</dependency>
<!-- Exposition servlet -->
<dependency>
  <groupId>io.prometheus.client.utility</groupId>
  <artifactId>servlet</artifactId>
  <version>0.0.6</version>
</dependency>
```

### Getting Started
There are canonical examples defined in the class definition Javadoc of the client packages.

## Documentation
The client is canonically documented with Javadoc.  Running the following will produce local documentation
in _apidocs_ directories for you to read.

    $ mvn package

If you use the Mavenized version of the Prometheus client, you can also instruct Maven to download the Javadoc and
source artifacts.

Alternatively, you can also look at the generated [Java Client
Github Project Page](http://prometheus.github.io/client_java), but the raw
Javadoc in Java source in version control should be treated as the canonical
form of documentation.

## Maintenance of this Library
This suite is built and managed by [Maven](http://maven.apache.org), and the
artifacts are hosted on the [Sonatype OSS Asset Repository](https://docs.sonatype.org/display/Repository/Sonatype+OSS+Maven+Repository+Usage+Guide).

All contributions to this library must follow, as far as practical, the
prevalent patterns in the library for consistency and the following style
guide: [Google Java Style](http://goo.gl/FfwVsc).  Depending upon your
development environment, you may be able to find an automatic formatter
and adherence checker that follows these rules.

### Building

    $ mvn compile

### Testing

    $ mvn test

Please note that tests on Travis may be unreliable due to the absence of
installed Maven artifacts.  Ensure that the current snapshot version is
deployed to Sonatype OSS Repository.

###  Deployment
These steps below are only useful if you are in a release engineering capacity
and want to publicize these changes for external users.  You will also need to
have your local Maven setup correctly along with valid and public GPG key and
adequate authorization on the Sonatype OSS Repository to submit new artifacts,
be they _staging_ or _release_ ones.

You should read the [Sonatype OSS Apache Maven
Guide](http://central.sonatype.org/pages/apache-maven.html) before performing any of the following:

### Snapshot Deployment
    $ mvn clean deploy

#### Staging
    $ mvn release:clean release:prepare -Prelease
    $ mvn release:perform -Prelease

#### Release

Go to https://oss.sonatype.org/#stagingRepositories and Close the `ioprometheus-XXX` release.
Once it's closed, Release it. Wait for the new version to appear in
[The Central Repository](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22io.prometheus%22).

Send an email to the developer's mailing list announcing the release.

#### Documentation
Documentation can also be released to the public via the Github Pages subproduct
through the magic _gh-pages_ branch for a Github project.  Documentation is
generated via the following command:

    $ mvn javadoc:aggregate

It will need to be automatically merged into the _gh-pages_ branch, but that is
as simple as this:

    $ git checkout master
    $ mvn javadoc:aggregate
    $ git checkout gh-pages
    $ mv target/site/apidocs/ ./
    $ git status
    $ # Amend the branch as necessary.
    $ git commit
    $ git push

There is a Maven plugin to perform this work, but it is broken.  The
javadoc:aggregate step will emit documentation into
_target/site/apidocs_.  The reason that we use this aggregate step instead
of bare javadoc is that we want one comprehensive Javadoc emission that includes
all Maven submodules versus trying to manually concatenate this together.

Output documentation lives in the [Java Client Github Project
Page](http://prometheus.github.io/client_java).

## Contact
  * All of the core developers are accessible via the [Prometheus Developers Mailinglist](https://groups.google.com/forum/?fromgroups#!forum/prometheus-developers).

[![Build Status](https://travis-ci.org/prometheus/client_java.png?branch=master)](https://travis-ci.org/prometheus/client_java)


text:
let define: (...args: any[]) => void;
let loadAllModules: (mainScriptURL?: string) => void;

class Module {
    public imports: Module[] = [];
    public factoryArgs: any[] = [];
    public exports: any;
    public value: any;
    public firstImportedFrom: string;
    public changed: boolean = false;
    constructor(public id: string, public dependencies: string[],
                public factory: Function, public filename: string) {
        this.value = this.exports = {};
    }
}

(function() {

let modules: { [id: string]: Module } = {};

function resolvePath(base: string, path: string): string {

    // This function assumes that all / characters in a path will be separated by at least one
    // other character. We used to collapse multiple adjacent / characters into one, but this
    // breaks the scheme separator in URLs.

    let baseParts = base.split("/");
    let pathParts = path.split("/");

    if (baseParts.length == 0)
        throw new Error("Base filename has no parts");
    baseParts.pop();

    for (let i = 0; i < pathParts.length; i++) {
        if (pathParts[i] == ".") {
            // do nothing
        }
        else if (pathParts[i] == "..") {
            if (baseParts.length > 0)
                baseParts.pop();
            else
                baseParts.push("..");
        }
        else {
            baseParts.push(pathParts[i]);
        }
    }
    return baseParts.join("/");
}

function reqmodule(currentModuleId: string, name: string): Object {
    if (modules[name] == null)
        throw new Error("No such module: "+name);
    if ((modules[name].firstImportedFrom != null) && modules[name].changed)
        throw new Error("Attempt to load module "+name+", whose value has changed (from "+currentModuleId+
                        "; first import was from "+modules[name].firstImportedFrom+")");
    if (currentModuleId == null)
        modules[name].firstImportedFrom = "(unknown)";
    else
        modules[name].firstImportedFrom = currentModuleId;
    return modules[name].value;
}

function determineModuleFilename(): string {
    // This function is called if there is no module id parameter supplied to define(). We try to
    // determine the filename of the module based on the <script> element that loaded the code
    // containing the define() call.
    //
    // On all modern browsers except IE, there is a currentScript property on the document we can
    // use to find this element. To cater for IE, we also check for the last <script> element in
    // document order. In doing so, we make the assumption (regardless of browser) that scripts
    // are being loaded synchronously (that is, the "async" attribute is missing or set to "false").
    //
    // If we're running in a browser that supports currentScript, we do a sanity check to verify
    // that it's the same as the last <script> element, to help identify cases where this loader
    // is being used with <script> elements that are either asynchronous or inserted into the
    // document somewhere before the last such element.

    let currentScriptElement = document.currentScript;
    let scripts = document.getElementsByTagName("script");
    let scriptsLength = scripts.length;
    let lastScriptElement = (scriptsLength > 0) ? scripts[scriptsLength-1] : null;

    let currentScriptSrc: string = null;
    let lastScriptSrc: string = null;

    // Note: The instanceof expressions below are type guards only; they will always be true if
    // the non-null check in the first part of the if statement is true.
    if ((currentScriptElement != null) && (currentScriptElement instanceof HTMLScriptElement))
        currentScriptSrc = currentScriptElement.src;

    if ((lastScriptElement != null) && (lastScriptElement instanceof HTMLScriptElement))
        lastScriptSrc = lastScriptElement.src;

    if ((currentScriptSrc != null) && (currentScriptSrc != lastScriptSrc)) {
        let currentQuoted = JSON.stringify(currentScriptSrc);
        let lastQuoted = JSON.stringify(lastScriptSrc);
        throw new Error("document.currentScript ("+currentQuoted+") differs from the last "+
                        "script element in the document ("+lastQuoted+"). This may be caused "+
                        "by asynchronous loading; the current module definition logic assumes "+
                        "that all scripts are loaded synchronously");
    }

    // If, at this point, currentScriptSrc is null - that's ok. We're probably running in IE.
    // All we wanted it for was as a sanity check that it doesn't differ from the last script
    // element.
    if (lastScriptSrc == null)
        throw new Error("Cannot find <script> element associated with current module definition");

    return lastScriptSrc;
}

define = function(...args: any[]): void {
    let index = 0;
    let id: string = null;
    let dependencies: string[] = null;
    let factory: Function = null;
    if ((index < args.length) && (args[index] != null) && (typeof(args[index]) == "string"))
        id = args[index++];
    if ((index < args.length) && (args[index] != null) && (args[index] instanceof Array))
        dependencies = args[index++];
    if ((index < args.length) && (args[index] != null) && (args[index] instanceof Function))
        factory = args[index++];

    let moduleFilename: string = null;
    if (id == null) {
        moduleFilename = determineModuleFilename();
        if (moduleFilename == null)
            throw new Error("Cannot determine modile filename");
        id = moduleFilename.replace(/\.js$/,"");
    }
    else {
        moduleFilename = id+".js";
    }

    if (factory == null)
        throw new Error("No factory specified");

    if (dependencies == null)
        dependencies = ["require","exports","module"];

    modules[id] = new Module(id,dependencies,factory,moduleFilename);
}

let builtinNames: { [id: string]: boolean } = {
    "require": true,
    "exports": true,
    "module": true,
}

// This function is made available as a global function called require() after loadAllModules()
// has completed. The reason why it is not made available before then is that our loader, unlike
// require.js, does not support asynchronous loading. Assuming however that all of the required
// modules have been defined (e.g. via <script> element insertion), and loadAllModules() has been
// called, then require() can be used in the same way as in require.js and other AMD-based module
// loaders.
function postLoadRequire(mainScriptURL: string, names: string[], fun: () => any) {
    let baseDir: string = null;
    if (mainScriptURL != null) {
        let absScriptURL = resolvePath(window.location.href,mainScriptURL);
        baseDir = absScriptURL.replace(/[^/]*$/,"");
    }

    let args: string[] = [];
    for (let i = 0; i < names.length; i++) {
        let moduleName = (baseDir != null) ? resolvePath(baseDir,names[i]) : names[i];
        let importedModule = modules[moduleName];
        if (importedModule == null)
            throw new Error("No such module: "+moduleName);
        else
            args.push(importedModule.value);
    }

    fun.apply(null,args);
}

loadAllModules = function(mainScriptURL?: string): void {

    Object.keys(modules).sort().forEach(function(key) {
        let module = modules[key];
        module.dependencies.forEach(function(dep) {
            if (!builtinNames[dep]) {
                let resolved = resolvePath(module.filename,dep);
                let reference = modules[resolved];
                if (reference == null)
                    throw new Error("Module not found (from "+JSON.stringify(module.filename)+"): "+dep);
                module.imports.push(reference);
            }
        });
    })

    let remaining: { [id: string]: Module } = {};
    for (let id in modules) {
        let module = modules[id];
        remaining[id] = module;
    }

    let loadOrder: Module[] = [];

    let allIds: string[] = Object.keys(remaining).sort();
    let allModules: Module[] = allIds.map(function(id) { return remaining[id]; });
    let recurseCount = 0;
    allModules.forEach(function(topModule) {
        let stack: string[] = [];
        recurse(topModule);
        return;

        function recurse(module: Module): void {
            if (!remaining[module.id])
                return;
            let index = stack.indexOf(module.id);
            if (index >= 0)
                return; // Circular reference; ignore

            stack.push(module.id);
            module.imports.forEach(recurse);
            stack.pop();

            delete remaining[module.id];
            loadOrder.push(module);
            recurseCount++;
        }
    });

    loadOrder.forEach(function(module) {
        module.dependencies.forEach(function(dep) {
            if (dep == "require")
                module.factoryArgs.push(localRequire);
            else if (dep == "exports")
                module.factoryArgs.push(module.exports);
            else if (dep == "module")
                module.factoryArgs.push(null);
            else {
                module.factoryArgs.push(localRequire(dep));
            }

            function localRequire(relativeName: string) {
                let absoluteName = resolvePath(module.filename,relativeName);
                let importedModule = modules[absoluteName];
                if (importedModule == null)
                    throw new Error("No such module: "+absoluteName+" (while loading "+module.id+")");
                return importedModule.value;
            }
        });

        let result = module.factory.apply(null,module.factoryArgs);
        if (result != null) {
            module.value = result;
            if (module.firstImportedFrom != null)
                module.changed = true;
        }
    });

    // Now that we have loaded all of the modules, make require() available as a global function,
    // with the intention that it be used by JavaScript code embedded directly within the HTML
    // file. See demo-customloader.html for an example use case of this.
    let w: any = window;
    if (w.require != null)
        throw new Error("window.require is already defined");
    w.require = (names: string[], fun: () => any) => {
        postLoadRequire(mainScriptURL,names,fun);
    };
}

})();


text:
<!DOCTYPE html>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="关于一个菜鸡的个人博客">
    <meta name="keyword"  content="王翔, 王翔的博客">

    <meta name="baidu-site-verification" content="Wicj7SXyl6" />

    <meta http-equiv="Cache-Control" content="no-transform " />

    <link rel="shortcut icon" href="/img/favicon.ico">

    <title>About - 阿翔 | AXiang Blog</title>

    <link rel="canonical" href="http://localhost:5001/about/">

    <link rel="stylesheet" href="/css/bootstrap.min.css">

    <link rel="stylesheet" href="/css/hux-blog.css">

    <link rel="stylesheet" href="/css/axiang.css">

    <link rel="stylesheet" href="/css/syntax.css">

    <link href="http://cdn.staticfile.org/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">

            <button type="button" class="navbar-toggle">
                Toggle navigation

            </button>
            空的伸手摘星

                        Home

                        About

                        Portfolio

                        Tags

About

                    Hey, this is AXiang.

<select onchange= "onLanChange(this.options[this.options.selectedIndex].value)">
    <option value="0" selected> 中文 Chinese </option>
    <option value="1"> 英语 English </option>
</select>

        愿生命线满是星星

       Wonderful Time

        Let her go?
        Fear can hold you prisoner ,hope can set you free.A strong man can save himself,a great man can save another.

    -- The Summer.2018
    -- The Shawshank Redemption.1994

FRIENDS

AXiang Blog

NGA

ACFUN

                    Copyright © 空的伸手摘星 2048


text:
from beritest_tools import BaseBERITestCase
from nose.plugins.attrib import attr

#
# Test that csb raises a C2E exception if it does not have Permit_Store.
#

class test_cp2_x_csb_perm(BaseBERITestCase):
    @attr('capabilities')
    def test_cp2_x_csb_perm_1(self):
        '''Test csb did not write without Permit_Store permission'''
        self.assertRegisterEqual(self.MIPS.a0, 0x01,
            "csb wrote without Permit_Store permission")

    @attr('capabilities')
    def test_cp2_x_csb_perm_2(self):
        '''Test csb raises an exception when doesn't have Permit_Store permission'''
        self.assertRegisterEqual(self.MIPS.a2, 1,
            "csb did not raise an exception when didn't have Permit_Store permission")

    @attr('capabilities')
    def test_cp2_x_csb_perm_3(self):
        '''Test capability cause is set correctly when doesn't have Permit_Store permission'''
        self.assertRegisterEqual(self.MIPS.a3, 0x1301,
            "Capability cause was not set correctly when didn't have Permit_Store permission")