text:
package com.sun.xacml.cond;

import com.sun.xacml.ParsingException;
import com.sun.xacml.UnknownIdentifierException;

import java.net.URI;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.w3c.dom.Node;

/**
 * This is a basic implementation of FunctionFactory. It
 * implements the insertion and retrieval methods, but it doesn't actually
 * setup the factory with any functions. It also assumes a certain model
 * with regard to the different kinds of functions (Target, Condition, and
 * General). For this reason, you may want to re-use this class, or you
 * may want to extend FunctionFactory directly, if you're writing a new
 * factory implementation.
 *

 * Note that while this class is thread-safe on all creation methods, it
 * is not safe to add support for a new function while creating an instance
 * of a function. This follows from the assumption that most people will
 * initialize these factories up-front, and then start processing without
 * ever modifying the factories. If you need these mutual operations to
 * be thread-safe, then you should write a wrapper class that implements
 * the right synchronization.
 *
 * @since 1.2
 * @author Seth Proctor
 */
public class BaseFunctionFactory extends FunctionFactory
{

    // the backing maps for the Function objects or FunctionProxy objects
    private HashMap<String, Object> functionMap = null;

    // the superset factory chained to this factory
    private FunctionFactory superset = null;

    /**
     * Default constructor. No superset factory is used.
     */
    public BaseFunctionFactory() {
        this(null);
    }

    /**
     * Constructor that sets a "superset factory". This is useful since
     * the different function factories (Target, Condition, and General)
     * have a superset relationship (Condition functions are a superset
     * of Target functions, etc.). Adding a function to this factory will
     * automatically add the same function to the superset factory.
     *
     * @param superset the superset factory or null
     */
    public BaseFunctionFactory(FunctionFactory superset) {
        this.functionMap = new HashMap<String, Object>();

        this.superset = superset;
    }

    /**
     * Constructor that defines the initial functions supported by this
     * factory but doesn't use a superset factory.
     *
     * @param supportedFunctions a Set of Functions
     * @param supportedAbstractFunctions a mapping from URI to
     *                                   FunctionProxy
     */
    public BaseFunctionFactory(Set<Function> supportedFunctions,
                               Map<URI, FunctionProxy> supportedAbstractFunctions) {
        this(null, supportedFunctions, supportedAbstractFunctions);
    }

    /**
     * Constructor that defines the initial functions supported by this
     * factory and uses a superset factory. Note that the functions
     * supplied here are not propagated up to the superset factory, so
     * you must either make sure the superst factory is correctly
     * initialized or use BaseFunctionFactory(FunctionFactory)
     * and then manually add each function.
     *
     * @param superset the superset factory or null
     * @param supportedFunctions a Set of Functions
     * @param supportedAbstractFunctions a mapping from URI to
     *                                   FunctionProxy
     */
    public BaseFunctionFactory(FunctionFactory superset,
                               Set<Function> supportedFunctions,
                               Map<URI, FunctionProxy> supportedAbstractFunctions) {
        this(superset);

        Iterator<Function> it = supportedFunctions.iterator();
        while (it.hasNext()) {
            Function function = it.next();
            this.functionMap.put(function.getIdentifier().toString(),
                    function);
        }

        Iterator<Map.Entry<URI, FunctionProxy>> it2 = supportedAbstractFunctions.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<URI, FunctionProxy> entry =  it2.next();
            URI id = (URI)(entry.getKey());
            FunctionProxy proxy =
                (FunctionProxy)(entry.getValue());
            this.functionMap.put(id.toString(), proxy);
        }
    }

    /**
     * Adds the function to the factory. Most functions have no state, so
     * the singleton model used here is typically desireable. The factory will
     * not enforce the requirement that a Target or Condition matching function
     * must be boolean.  If a function is already contained in the factory, an
     * error will be thrown.
     *
     * @param function the Function to add to the factory
     *
     * @throws IllegalArgumentException if the function's identifier is already
     *                                  used or if the function is non-boolean
     *                                  (when this is a Target or Condition
     *                                  factory)
     */
    public void addFunction(Function function)
        throws IllegalArgumentException
    {
    	addFunction(function, false);
    }

    /**
     * Adds the function to the factory. Most functions have no state, so
     * the singleton model used here is typically desireable. The factory will
     * not enforce the requirement that a Target or Condition matching function
     * must be boolean.
     *
     * Allows to define if an already existing function should be overwritten
     * or not.
     *
     *      * @param function the Function to add to the factory
     *
     * @throws IllegalArgumentException if the function's identifier is already
     *                                  used or if the function is non-boolean
     *                                  (when this is a Target or Condition
     *                                  factory)
     */
	@Override
	public void addFunction(Function function, boolean overwrite)
		throws IllegalArgumentException {

        String id = function.getIdentifier().toString();

        // make sure this doesn't already exist
        if (! overwrite && this.functionMap.containsKey(id)) {
            throw new IllegalArgumentException("function already exists");
        }

        // add to the superset factory
        if (this.superset != null) {
            this.superset.addFunction(function);
        }

        // finally, add to this factory
        this.functionMap.put(id, function);

	}

    /**
     * Adds the abstract function proxy to the factory. This is used for
     * those functions which have state, or change behavior (for instance
     * the standard map function, which changes its return type based on
     * how it is used).
     *
     * @param proxy the FunctionProxy to add to the factory
     * @param identity the function's identifier
     *
     * @throws IllegalArgumentException if the function's identifier is already
     *                                  used
     */
    public void addAbstractFunction(FunctionProxy proxy,
                                    URI identity)
        throws IllegalArgumentException
    {
        String id = identity.toString();

        // make sure this doesn't already exist
        if (this.functionMap.containsKey(id)) {
            throw new IllegalArgumentException("function already exists");
        }
        // add to the superset factory
        if (this.superset != null) {
            this.superset.addAbstractFunction(proxy, identity);
        }
        // finally, add to this factory
        this.functionMap.put(id, proxy);
    }

    /**
     * Returns the function identifiers supported by this factory.
     *
     * @return a Set of Strings
     */
    public Set<String> getSupportedFunctions() {
        Set<String> set = new HashSet<String>(this.functionMap.keySet());

        if (this.superset != null) {
            set.addAll(this.superset.getSupportedFunctions());
        }
        return set;
    }

    /**
     * Tries to get an instance of the specified function.
     *
     * @param identity the name of the function
     *
     * @return  The function.
     *
     * @throws UnknownIdentifierException if the name isn't known
     * @throws FunctionTypeException if the name is known to map to an
     *                               abstract function, and should therefore
     *                               be created through createAbstractFunction
     */
    public Function createFunction(URI identity)
        throws UnknownIdentifierException, FunctionTypeException
    {
        return createFunction(identity.toString());
    }

    /**
     * Tries to get an instance of the specified function.
     *
     * @param identity the name of the function
     *
     * @throws UnknownIdentifierException if the name isn't known
     * @throws FunctionTypeException if the name is known to map to an
     *                               abstract function, and should therefore
     *                               be created through createAbstractFunction
     *
     * @return  The function.
     */
    public Function createFunction(String identity)
        throws UnknownIdentifierException, FunctionTypeException
    {
        Object entry = this.functionMap.get(identity);

        if (entry != null) {
            if (entry instanceof Function) {
                return (Function)entry;
            }
            // this is actually a proxy, which means the other create
            // method should have been called
            throw new FunctionTypeException("function is abstract");
        }
        // we couldn't find a match
        throw new UnknownIdentifierException("functions of type "
                + identity + " are not supported by this factory");
    }

    /**
     * Tries to get an instance of the specified abstract function.
     *
     * @param identity the name of the function
     * @param root the DOM root containing info used to create the function
     *
     * @throws UnknownIdentifierException if the name isn't known
     * @throws FunctionTypeException if the name is known to map to a
     *                               concrete function, and should therefore
     *                               be created through createFunction
     *
     * @return  The function.
     *
     * @throws ParsingException if the function can't be created with the
     *                          given inputs
     */
    public Function createAbstractFunction(URI identity, Node root)
        throws UnknownIdentifierException, ParsingException,
               FunctionTypeException
    {
        return createAbstractFunction(identity.toString(), root, null);
    }

    /**
     * Tries to get an instance of the specified abstract function.
     *
     * @param identity the name of the function
     * @param root the DOM root containing info used to create the function
     * @param xpathVersion the version specified in the contianing policy, or
     *                     null if no version was specified
     *
     * @throws UnknownIdentifierException if the name isn't known
     * @throws FunctionTypeException if the name is known to map to a
     *                               concrete function, and should therefore
     *                               be created through createFunction
     *
     * @return  The function.
     *
     * @throws ParsingException if the function can't be created with the
     *                          given inputs
     */
    public Function createAbstractFunction(URI identity, Node root,
                                           String xpathVersion)
        throws UnknownIdentifierException, ParsingException,
               FunctionTypeException
    {
        return createAbstractFunction(identity.toString(), root, xpathVersion);
    }

    /**
     * Tries to get an instance of the specified abstract function.
     *
     * @param identity the name of the function
     * @param root the DOM root containing info used to create the function
     *
     * @throws UnknownIdentifierException if the name isn't known
     * @throws FunctionTypeException if the name is known to map to a
     *                               concrete function, and should therefore
     *                               be created through createFunction
     *
     * @return  The function.
     *
     * @throws ParsingException if the function can't be created with the
     *                          given inputs
     */
    public Function createAbstractFunction(String identity, Node root)
        throws UnknownIdentifierException, ParsingException,
               FunctionTypeException
    {
        return createAbstractFunction(identity, root, null);
    }

    /**
     * Tries to get an instance of the specified abstract function.
     *
     * @param identity the name of the function
     * @param root the DOM root containing info used to create the function
     * @param xpathVersion the version specified in the contianing policy, or
     *                     null if no version was specified
     *
     * @return  The function.
     *
     * @throws UnknownIdentifierException if the name isn't known
     * @throws FunctionTypeException if the name is known to map to a
     *                               concrete function, and should therefore
     *                               be created through createFunction
     * @throws ParsingException if the function can't be created with the
     *                          given inputs
     */
    public Function createAbstractFunction(String identity, Node root,
                                           String xpathVersion)
            throws UnknownIdentifierException, ParsingException,
                   FunctionTypeException {
        Object entry = this.functionMap.get(identity);

        if (entry != null) {
            if (entry instanceof FunctionProxy) {
                try {
                    return ((FunctionProxy)entry).getInstance(root,
                                                              xpathVersion);
                } catch (Exception e) {
                    throw new ParsingException("couldn't create abstract" +
                                               " function " + identity, e);
                }
            }
            // this is actually a concrete function, which means that
            // the other create method should have been called
            throw new FunctionTypeException("function is concrete");
        }
        // we couldn't find a match
        throw new UnknownIdentifierException("abstract functions of " +
                                             "type " + identity +
                                             " are not supported by " +
                                             "this factory");
    }

}


text:
<?php

class Papi_Property_Gallery_Test extends Papi_Property_Test_Case {

	public $slug = 'gallery_test';

	public function get_value() {
		return [23];
	}

	public function get_expected() {
		return [23];
	}

	public function test_property_convert_type() {
		$this->assertEquals( 'array', $this->property->convert_type );
	}

	public function test_property_default_value() {
		$this->assertEquals( [], $this->property->default_value );
	}

	public function test_property_format_value() {
		$post_id = $this->factory->post->create( ['post_type' => 'attachment', 'post_mime_type' => 'image/jpeg'] );
		$meta    = [
			'width'      => 2900,
			'height'     => 1559,
			'file'       => '2011/12/press_image.jpg',
			'sizes'      => [
				'thumbnail' => [
					'file'      => 'press_image-150x150.jpg',
					'width'     => 150,
					'height'    => 150,
					'mime-type' => 'image/jpeg'
				]
			],
			'image_meta' => [
				'aperture'          => 5,
				'credit'            => '',
				'camera'            => 'Super',
				'caption'           => '',
				'created_timestamp'	=> 1323190643,
				'copyright'         => '',
				'focal_length'      => 35,
				'iso'               => 800,
				'shutter_speed'     => 0.016666666666667,
				'title'             => ''
			]
		];

		update_post_meta( $post_id, '_wp_attachment_metadata', $meta );
		update_post_meta( $post_id, '_wp_attachment_image_alt', 'alt text' );
		update_post_meta( $post_id, '_wp_attached_file', '2011/12/press_image.jpg' );
		$thumbnail_url = home_url( '/wp-content/uploads/2011/12/press_image-150x150.jpg' );

		tests_add_filter( 'image_downsize', function( $image, $attachment_id, $size ) use ( $thumbnail_url ) {
			return [$thumbnail_url, 150, 150, false];
		}, 10, 3 );

		$images = $this->property->format_value( [$post_id, $post_id], '', $post_id );

		$this->assertTrue( is_object( $images[0] ) );
		$this->assertEquals( 2900, $images[0]->width );
		$this->assertEquals( 1559, $images[0]->height );
		$this->assertEquals( '2011/12/press_image.jpg', $images[0]->file );

		$this->assertTrue( is_array( $images[0]->sizes ) );
		$this->assertTrue( isset( $images[0]->sizes['thumbnail'] ) );
		$this->assertEquals( 'press_image-150x150.jpg', $images[0]->sizes['thumbnail']['file'] );
		$this->assertEquals( 150, $images[0]->sizes['thumbnail']['width'] );
		$this->assertEquals( 150, $images[0]->sizes['thumbnail']['height'] );
		$this->assertEquals( 'image/jpeg', $images[0]->sizes['thumbnail']['mime-type'] );
		$this->assertEquals( $thumbnail_url, $images[0]->sizes['thumbnail']['url'] );

		$this->assertTrue( is_array( $images[0]->image_meta ) );
		$this->assertEquals( 5, $images[0]->image_meta['aperture'] );
		$this->assertEquals( '', $images[0]->image_meta['credit'] );
		$this->assertEquals( 'Super', $images[0]->image_meta['camera'] );
		$this->assertEquals( '', $images[0]->image_meta['caption'] );
		$this->assertEquals( 1323190643, $images[0]->image_meta['created_timestamp'] );
		$this->assertEquals( '', $images[0]->image_meta['copyright'] );
		$this->assertEquals( 35, $images[0]->image_meta['focal_length'] );
		$this->assertEquals( 800, $images[0]->image_meta['iso'] );
		$this->assertEquals( 0.016666666666667, $images[0]->image_meta['shutter_speed'] );
		$this->assertEquals( '', $images[0]->image_meta['title'] );

		$this->assertTrue( is_object( $images[1] ) );
		$this->assertEquals( 2900, $images[1]->width );
		$this->assertEquals( 1559, $images[1]->height );
		$this->assertEquals( '2011/12/press_image.jpg', $images[1]->file );

		$this->assertTrue( is_array( $images[1]->sizes ) );
		$this->assertTrue( isset( $images[1]->sizes['thumbnail'] ) );
		$this->assertEquals( 'press_image-150x150.jpg', $images[1]->sizes['thumbnail']['file'] );
		$this->assertEquals( 150, $images[1]->sizes['thumbnail']['width'] );
		$this->assertEquals( 150, $images[1]->sizes['thumbnail']['height'] );
		$this->assertEquals( 'image/jpeg', $images[1]->sizes['thumbnail']['mime-type'] );
		$this->assertEquals( $thumbnail_url, $images[1]->sizes['thumbnail']['url'] );

		$this->assertTrue( is_array( $images[1]->image_meta ) );
		$this->assertEquals( 5, $images[1]->image_meta['aperture'] );
		$this->assertEquals( '', $images[1]->image_meta['credit'] );
		$this->assertEquals( 'Super', $images[1]->image_meta['camera'] );
		$this->assertEquals( '', $images[1]->image_meta['caption'] );
		$this->assertEquals( 1323190643, $images[1]->image_meta['created_timestamp'] );
		$this->assertEquals( '', $images[1]->image_meta['copyright'] );
		$this->assertEquals( 35, $images[1]->image_meta['focal_length'] );
		$this->assertEquals( 800, $images[1]->image_meta['iso'] );
		$this->assertEquals( 0.016666666666667, $images[1]->image_meta['shutter_speed'] );
		$this->assertEquals( '', $images[1]->image_meta['title'] );
	}

	public function test_property_import_value() {
		$this->assertEmpty( $this->property->import_value( $this->get_value(), '', 0 ) );
	}

	public function test_property_options() {
		$this->assertEquals( 'gallery', $this->property->get_option( 'type' ) );
		$this->assertEquals( 'Gallery test', $this->property->get_option( 'title' ) );
		$this->assertEquals( 'papi_gallery_test', $this->property->get_option( 'slug' ) );
	}

	public function test_property_settings() {
		$this->assertTrue( $this->property->get_setting( 'multiple' ) );
	}
}

redpajama_set_name: RedPajamaGithub


text:
package org.vitrivr.adampro.query.distance

import breeze.linalg.{max, min}
import org.vitrivr.adampro.data.datatypes.vector.Vector._
import org.vitrivr.adampro.query.distance.Distance._
import org.vitrivr.adampro.utils.Logging

/**
  * ADAMpro
  *
  * Ivan Giangreco
  * June 2016
  *
  * from Julia: max(x - y) - min(x - y)
  */
object SpanNormDistance extends DistanceFunction with Logging with Serializable {
  override def apply(v1: MathVector, v2: MathVector, weights: Option[MathVector]): Distance = {
    if (weights.isDefined) {
      log.error("weights cannot be used with span norm distance and are ignored")
    }

    max(v1 - v2) - min(v1 - v2)
  }
}


text:
/usr/bin/mysqld_safe > /dev/null 2>&1 &

# Wait until Mysql is up
RET=1
while [[ RET -ne 0 ]]; do
    echo "=> Waiting for confirmation of MySQL service startup"
    sleep 5
    mysql -uroot -e "status" > /dev/null 2>&1
    RET=$?
done

# First admin user
# User: 'root'
# Pass: ''
mysql -uroot -e "UPDATE mysql.user SET Password=PASSWORD('') WHERE User='root'"
mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION"
mysql -uroot -e "flush privileges"

mysqladmin -uroot shutdown


text:
package graphene.enron.web.rest;

import graphene.dao.TransferDAO;
import graphene.model.query.EventQuery;
import graphene.model.view.events.SingleSidedEventRow;
import graphene.model.view.events.SingleSidedEvents;
import graphene.rest.ws.LedgerFreeTextRS;
import graphene.util.FastNumberUtils;

import java.util.List;

import org.apache.tapestry5.ioc.annotations.InjectService;

public class LedgerFreeTextRSImpl implements LedgerFreeTextRS {

	// TODO: Revisit the caching needs. This is not how the pros do it. --djue
	// private LedgerQuery prevQuery;

	@InjectService("Solr")
	private TransferDAO<SingleSidedEventRow, EventQuery> solr;

	@Override
	public SingleSidedEvents getTransactions(String account, int start,
			int limit, String minAmount, String maxAmount, String minSecs,
			String maxSecs, String comments, String sortColumn) {

		EventQuery q = new EventQuery();
		q.addId(account);
		q.setFirstResult(start);
		q.setMaxResult(limit);
		q.setMinAmount(Double.parseDouble(minAmount.isEmpty() ? "0" : minAmount));
		q.setMaxAmount(Double.parseDouble(maxAmount.isEmpty() ? "0" : maxAmount));

		q.setMinSecs(FastNumberUtils.parseLongWithCheck(minSecs, 0));
		q.setMaxSecs(FastNumberUtils.parseLongWithCheck(maxSecs, 0));

		// FIXME: we probably want this by default, but it should be a parameter
		q.setFindRelatedIds(true);

		q.setComments(comments);
		q.setSortAndDirection(sortColumn);
		List<SingleSidedEventRow> results;
		SingleSidedEvents lt = new SingleSidedEvents();
		try {
			results = solr.findByQuery(q);
			lt.setMultiUnit(true);
			lt.setRows(results);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// TODO: If we want any app-generated stats on ledgers that were read
		// in, do it here.
		return lt;
	}

}


text:
<?php

namespace Puli\Manager\Api\Php;

use OutOfBoundsException;
use Webmozart\Assert\Assert;

/**
 * A method of a {@link Clazz}.
 *
 * @since  1.0
 *
 * @author Bernhard Schussek <bschussek@gmail.com>
 */
class Method
{
    /**
     * @var Clazz
     */
    private $class;

    /**
     * @var string
     */
    private $name;

    /**
     * @var string
     */
    private $description;

    /**
     * @var Argument[]
     */
    private $arguments = array();

    /**
     * @var ReturnValue|null
     */
    private $returnValue;

    /**
     * @var string
     */
    private $body = '';

    /**
     * Creates a new method.
     *
     * @param string $name The method name.
     */
    public function __construct($name)
    {
        Assert::stringNotEmpty($name, 'The method name must be a non-empty string. Got: %s');

        $this->name = $name;
    }

    /**
     * Returns the class this method belongs to.
     *
     * @return Clazz The factory class.
     */
    public function getClass()
    {
        return $this->class;
    }

    /**
     * Sets the class this method belongs to.
     *
     * @param Clazz $class The factory class.
     *
     * @return static The current instance.
     */
    public function setClass(Clazz $class)
    {
        $this->class = $class;

        return $this;
    }

    /**
     * Returns the name of the method.
     *
     * @return string The method name.
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Returns the description of the method.
     *
     * @return string The method description.
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Sets the description of the method.
     *
     * @param string $description The method description.
     *
     * @return static The current instance.
     */
    public function setDescription($description)
    {
        Assert::stringNotEmpty($description, 'The method description must be a non-empty string. Got: %s');

        $this->description = $description;

        return $this;
    }

    /**
     * Returns the method arguments.
     *
     * @return Argument[] The arguments indexed by their names.
     */
    public function getArguments()
    {
        return $this->arguments;
    }

    /**
     * Returns the argument with the given name.
     *
     * @param string $name The argument name.
     *
     * @return Argument The argument.
     *
     * @throws OutOfBoundsException If the argument does not exist.
     */
    public function getArgument($name)
    {
        if (!isset($this->arguments[$name])) {
            throw new OutOfBoundsException(sprintf(
                'The argument "%s" does not exist.',
                $name
            ));
        }

        return $this->arguments[$name];
    }

    /**
     * Returns whether the method has any arguments.
     *
     * @return bool Returns `true` if the method has arguments and `false`
     *              otherwise.
     */
    public function hasArguments()
    {
        return count($this->arguments) > 0;
    }

    /**
     * Returns whether the argument with the given name exist.
     *
     * @param string $name The argument name.
     *
     * @return bool Returns `true` if an argument with that name exists and
     *              `false` otherwise.
     */
    public function hasArgument($name)
    {
        return isset($this->arguments[$name]);
    }

    /**
     * Sets the arguments of the method.
     *
     * Existing arguments are overwritten.
     *
     * @param Argument[] $arguments The arguments to set.
     *
     * @return static The current instance.
     */
    public function setArguments(array $arguments)
    {
        $this->arguments = array();

        $this->addArguments($arguments);

        return $this;
    }

    /**
     * Adds arguments to the method.
     *
     * Existing arguments are kept.
     *
     * @param Argument[] $arguments The arguments to add.
     *
     * @return static The current instance.
     */
    public function addArguments(array $arguments)
    {
        foreach ($arguments as $argument) {
            $this->addArgument($argument);
        }

        return $this;
    }

    /**
     * Adds an argument to the method.
     *
     * @param Argument $argument The argument to add.
     *
     * @return static The current instance.
     */
    public function addArgument(Argument $argument)
    {
        $this->arguments[$argument->getName()] = $argument;

        return $this;
    }

    /**
     * Removes an argument from the method.
     *
     * If the argument does not exist, this method does nothing.
     *
     * @param string $name The name of the argument.
     *
     * @return static The current instance.
     */
    public function removeArgument($name)
    {
        unset($this->arguments[$name]);

        return $this;
    }

    /**
     * Removes all arguments.
     *
     * @return static The current instance.
     */
    public function clearArguments()
    {
        $this->arguments = array();

        return $this;
    }

    /**
     * Returns the return value of the method.
     *
     * @return ReturnValue|null The return value or `null` if the method has
     *                          no return value.
     */
    public function getReturnValue()
    {
        return $this->returnValue;
    }

    /**
     * Sets the return value of the method.
     *
     * @param ReturnValue $returnValue The return value.
     *
     * @return static The current instance.
     */
    public function setReturnValue(ReturnValue $returnValue)
    {
        $this->returnValue = $returnValue;

        return $this;
    }

    /**
     * Returns whether the method has a return value.
     *
     * @return bool Returns `true` if the method has a return value and `false`
     *              otherwise.
     */
    public function hasReturnValue()
    {
        return null !== $this->returnValue;
    }

    /**
     * Removes the return value of the method.
     *
     * If the method has no return value, this method does nothing.
     *
     * @return static The current instance.
     */
    public function removeReturnValue()
    {
        $this->returnValue = null;

        return $this;
    }

    /**
     * Returns the method body.
     *
     * @return string The source code.
     */
    public function getBody()
    {
        return $this->body;
    }

    /**
     * Sets the method body.
     *
     * @param string $body The source code.
     *
     * @return static The current instance.
     */
    public function setBody($body)
    {
        Assert::stringNotEmpty($body, 'The method body must be a non-empty string. Got: %s');

        $this->body = $body;

        return $this;
    }

    /**
     * Adds source code to the method body.
     *
     * A newline is inserted between the current code and the added code.
     *
     * @param string $body The source code to add.
     *
     * @return static The current instance.
     */
    public function addBody($body)
    {
        Assert::stringNotEmpty($body, 'The method body must be a non-empty string. Got: %s');

        if ($this->body) {
            $this->body .= "\n".$body;
        } else {
            $this->body = $body;
        }

        return $this;
    }

    /**
     * Clears the method body.
     *
     * @return static The current instance.
     */
    public function clearBody()
    {
        $this->body = '';

        return $this;
    }
}


text:
// Copyright 2014 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.lib.bazel.rules.cpp;

import static com.google.devtools.build.lib.packages.Attribute.attr;
import static com.google.devtools.build.lib.packages.BuildType.LABEL;
import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST;
import static com.google.devtools.build.lib.packages.BuildType.TRISTATE;
import static com.google.devtools.build.lib.packages.ImplicitOutputsFunction.fromFunctions;
import static com.google.devtools.build.lib.packages.ImplicitOutputsFunction.fromTemplates;
import static com.google.devtools.build.lib.rules.cpp.CppFileTypes.ALWAYS_LINK_LIBRARY;
import static com.google.devtools.build.lib.rules.cpp.CppFileTypes.ALWAYS_LINK_PIC_LIBRARY;
import static com.google.devtools.build.lib.rules.cpp.CppFileTypes.ARCHIVE;
import static com.google.devtools.build.lib.rules.cpp.CppFileTypes.ASSEMBLER;
import static com.google.devtools.build.lib.rules.cpp.CppFileTypes.ASSEMBLER_WITH_C_PREPROCESSOR;
import static com.google.devtools.build.lib.rules.cpp.CppFileTypes.CPP_HEADER;
import static com.google.devtools.build.lib.rules.cpp.CppFileTypes.CPP_SOURCE;
import static com.google.devtools.build.lib.rules.cpp.CppFileTypes.C_SOURCE;
import static com.google.devtools.build.lib.rules.cpp.CppFileTypes.OBJECT_FILE;
import static com.google.devtools.build.lib.rules.cpp.CppFileTypes.PIC_ARCHIVE;
import static com.google.devtools.build.lib.rules.cpp.CppFileTypes.PIC_OBJECT_FILE;
import static com.google.devtools.build.lib.rules.cpp.CppFileTypes.SHARED_LIBRARY;
import static com.google.devtools.build.lib.rules.cpp.CppFileTypes.VERSIONED_SHARED_LIBRARY;
import static com.google.devtools.build.lib.syntax.Type.BOOLEAN;
import static com.google.devtools.build.lib.syntax.Type.STRING;
import static com.google.devtools.build.lib.syntax.Type.STRING_LIST;

import com.google.common.base.Predicates;
import com.google.devtools.build.lib.analysis.BaseRuleClasses;
import com.google.devtools.build.lib.analysis.PlatformConfiguration;
import com.google.devtools.build.lib.analysis.RuleDefinition;
import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.packages.Attribute;
import com.google.devtools.build.lib.packages.Attribute.LateBoundDefault;
import com.google.devtools.build.lib.packages.AttributeMap;
import com.google.devtools.build.lib.packages.BuildType;
import com.google.devtools.build.lib.packages.ImplicitOutputsFunction.SafeImplicitOutputsFunction;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.packages.RuleClass.Builder;
import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType;
import com.google.devtools.build.lib.packages.TriState;
import com.google.devtools.build.lib.rules.cpp.CcToolchain;
import com.google.devtools.build.lib.rules.cpp.CppConfiguration;
import com.google.devtools.build.lib.rules.cpp.CppFileTypes;
import com.google.devtools.build.lib.rules.cpp.CppHelper;
import com.google.devtools.build.lib.rules.cpp.CppRuleClasses;
import com.google.devtools.build.lib.rules.cpp.transitions.LipoContextCollectorTransition;
import com.google.devtools.build.lib.util.FileTypeSet;

/**
 * Rule class definitions for C++ rules.
 */
public class BazelCppRuleClasses {
  static final SafeImplicitOutputsFunction CC_LIBRARY_DYNAMIC_LIB =
      fromTemplates("%{dirname}lib%{basename}.so");

  static final SafeImplicitOutputsFunction CC_BINARY_IMPLICIT_OUTPUTS =
      fromFunctions(CppRuleClasses.CC_BINARY_STRIPPED, CppRuleClasses.CC_BINARY_DEBUG_PACKAGE);

  /**
   * Returns the STL prerequisite of the rule.
   *
   *
If rule has an implicit $stl_default attribute returns STL version set on the command line
   * or if not set, the value of the $stl_default attribute. Returns {@code null} otherwise.
   */
  public static final LateBoundDefault<?, Label> STL =
      LateBoundDefault.fromTargetConfiguration(
          CppConfiguration.class,
          null,
          (rule, attributes, cppConfig) -> {
            Label stl = null;
            if (attributes.has("$stl_default", BuildType.LABEL)) {
              Label stlConfigLabel = cppConfig.getStl();
              Label stlRuleLabel = attributes.get("$stl_default", BuildType.LABEL);
              if (stlConfigLabel == null) {
                stl = stlRuleLabel;
              } else if (!stlConfigLabel.equals(rule.getLabel()) && stlRuleLabel != null) {
                // prevents self-reference and a cycle through standard STL in the dependency graph
                stl = stlConfigLabel;
              }
            }
            return stl;
          });

  static final FileTypeSet ALLOWED_SRC_FILES =
      FileTypeSet.of(
          CPP_SOURCE,
          C_SOURCE,
          CPP_HEADER,
          ASSEMBLER_WITH_C_PREPROCESSOR,
          ASSEMBLER,
          ARCHIVE,
          PIC_ARCHIVE,
          ALWAYS_LINK_LIBRARY,
          ALWAYS_LINK_PIC_LIBRARY,
          SHARED_LIBRARY,
          VERSIONED_SHARED_LIBRARY,
          OBJECT_FILE,
          PIC_OBJECT_FILE);

  static final String[] DEPS_ALLOWED_RULES =
      new String[] {
        "cc_inc_library",
        "cc_library",
        "objc_library",
        "cc_proto_library",
      };

  /**
   * Common attributes for all rules that create C++ links. This may
   * include non-cc_* rules (e.g. py_binary).
   */
  public static final class CcLinkingRule implements RuleDefinition {
    @Override
    @SuppressWarnings("unchecked")
    public RuleClass build(Builder builder, RuleDefinitionEnvironment env) {
      return builder
          .add(
              attr(CcToolchain.CC_TOOLCHAIN_DEFAULT_ATTRIBUTE_NAME, LABEL)
                  .value(CppRuleClasses.ccToolchainAttribute(env)))
          .add(
              attr(CcToolchain.CC_TOOLCHAIN_TYPE_ATTRIBUTE_NAME, LABEL)
                  .value(CppRuleClasses.ccToolchainTypeAttribute(env)))
          .setPreferredDependencyPredicate(Predicates.<String>or(CPP_SOURCE, C_SOURCE, CPP_HEADER))
          .requiresConfigurationFragments(PlatformConfiguration.class)
          .addRequiredToolchains(CppHelper.getCcToolchainType(env.getToolsRepository()))
          .build();
    }

    @Override
    public Metadata getMetadata() {
      return RuleDefinition.Metadata.builder()
          .name("$cc_linking_rule")
          .type(RuleClassType.ABSTRACT)
          .build();
    }
  }

  /**
   * Common attributes for C++ rules.
   */
  public static final class CcBaseRule implements RuleDefinition {
    @Override
    public RuleClass build(Builder builder, RuleDefinitionEnvironment env) {
      return builder
          /*
          Add these options to the C++ compilation command.
          Subject to "Make variable" substitution and
          Bourne shell tokenization.

            Each string in this attribute is added in the given order to COPTS before
            compiling the binary target. The flags take effect only for compiling this target, not
            its dependencies, so be careful about header files included elsewhere.  All paths should
            be relative to the workspace, not to the current package.

            If the package declares the feature
            no_copts_tokenization, Bourne shell tokenization applies only to strings
            that consist of a single "Make" variable.

          */
          .add(attr("copts", STRING_LIST))
          .add(attr("$stl_default", LABEL).value(env.getToolsLabel("//tools/cpp:stl")))
          .add(attr(":stl", LABEL).value(STL))
          .build();
    }

    @Override
    public Metadata getMetadata() {
      return RuleDefinition.Metadata.builder()
          .name("$cc_base_rule")
          .type(RuleClassType.ABSTRACT)
          .ancestors(CcLinkingRule.class)
          .build();
    }
  }

  /**
   * Helper rule class.
   */
  public static final class CcDeclRule implements RuleDefinition {
    @Override
    public RuleClass build(Builder builder, RuleDefinitionEnvironment env) {
      return builder
          /*
          List of defines to add to the compile line.
          Subject to "Make" variable substitution and
          Bourne shell tokenization.
          Each string, which must consist of a single Bourne shell token,
          is prepended with -D (or /D on Windows) and added to
          COPTS.
          Unlike copts, these flags are added for the
          target and every rule that depends on it!  Be very careful, since this may have
          far-reaching effects.  When in doubt, add "-D" (or /D on Windows) flags to
          copts instead.
          */
          .add(attr("defines", STRING_LIST))
          /*
          List of include dirs to be added to the compile line.

          Subject to "Make variable" substitution.
          Each string is prepended with -isystem and added to COPTS.
          Unlike COPTS, these flags are added for this rule
          and every rule that depends on it. (Note: not the rules it depends upon!) Be
          very careful, since this may have far-reaching effects.  When in doubt, add
          "-I" flags to COPTS instead.

          Headers must be added to srcs or hdrs, otherwise they will not be available to dependent
          rules when compilation is sandboxed (the default).

          */
          .add(attr("includes", STRING_LIST))
          .add(
              attr(":lipo_context_collector", LABEL)
                  .cfg(LipoContextCollectorTransition.INSTANCE)
                  .value(CppRuleClasses.LIPO_CONTEXT_COLLECTOR)
                  .skipPrereqValidatorCheck())
          .build();
    }

    @Override
    public Metadata getMetadata() {
      return RuleDefinition.Metadata.builder()
          .name("$cc_decl_rule")
          .type(RuleClassType.ABSTRACT)
          .ancestors(BaseRuleClasses.RuleBase.class)
          .build();
    }
  }

  /**
   * Helper rule class.
   */
  public static final class CcRule implements RuleDefinition {
    @Override
    public RuleClass build(Builder builder, final RuleDefinitionEnvironment env) {
      return builder
          /*
          The list of C and C++ files that are processed to create the target.
          These are C/C++ source and header files, either non-generated (normal source
          code) or generated.

All .cc, .c, and .cpp files will
             be compiled. These might be generated files: if a named file is in
             the outs of some other rule, this rule
             will automatically depend on that other rule.

A .h file will not be compiled, but will be available for
             inclusion by sources in this rule. Both .cc and
             .h files can directly include headers listed in
             these srcs or in the hdrs of any rule listed in
             the deps argument.

All #included files must be mentioned in the
             srcs attribute of this rule, or in the
             hdrs attribute of referenced cc_library()s.
             The recommended style is for headers associated with a library to be
             listed in that library's hdrs attribute, and any remaining
             headers associated with this rule's sources to be listed in
             srcs. See "Header inclusion checking"
             for a more detailed description.

If a rule's name is in the srcs,
             then this rule automatically depends on that one.
             If the named rule's outs are C or C++
             source files, they are compiled into this rule;
             if they are library files, they are linked in.

            Permitted srcs file types:

C and C++ source files: .c, .cc, .cpp,
              .cxx, .c++, .C

C and C++ header files: .h, .hh, .hpp,
              .hxx, .inc

Assembler with C preprocessor: .S

Archive: .a, .pic.a

"Always link" library: .lo, .pic.lo

Shared library, versioned or unversioned: .so,
              .so.version

Object file: .o, .pic.o

            ...and any rules that produce those files.
            Different extensions denote different programming languages in
            accordance with gcc convention.

          */
          .add(
              attr("srcs", LABEL_LIST)
                  .direct_compile_time_input()
                  .allowedFileTypes(ALLOWED_SRC_FILES))
          /*
          The list of other libraries to be linked in to the binary target.

These can be cc_library, cc_inc_library, or
          objc_library targets.

          */
          .override(
              attr("deps", LABEL_LIST)
                  .allowedRuleClasses(DEPS_ALLOWED_RULES)
                  .allowedFileTypes(CppFileTypes.LINKER_SCRIPT)
                  .skipAnalysisTimeFileTypeCheck())
          .add(attr("reexport_deps", LABEL_LIST)
              .allowedRuleClasses(DEPS_ALLOWED_RULES)
              .allowedFileTypes())
          /*
          Add these flags to the C++ linker command.
          Subject to "Make" variable substitution,

          Bourne shell tokenization and
          label expansion.
          Each string in this attribute is added to LINKOPTS before
          linking the binary target.

            Each element of this list that does not start with $ or - is
            assumed to be the label of a target in deps.  The
            list of files generated by that target is appended to the linker
            options.  An error is reported if the label is invalid, or is
            not declared in deps.

          */
          .add(attr("linkopts", STRING_LIST))
          /*
          Remove matching options from the C++ compilation command.
          Subject to "Make" variable substitution.
          The value of this attribute is interpreted as a regular expression.
          Any preexisting COPTS that match this regular expression
          (including values explicitly specified in the rule's copts attribute) will be removed from
          COPTS for purposes of compiling this rule.
          This attribute should rarely be needed.
          */
          .add(attr("nocopts", STRING))
          /*
           For cc_binary and
           cc_test: link the binary in mostly-static
           mode. For cc_library.linkstatic: see below.

             By default this option is on for cc_binary and off for the rest.

             If enabled and this is a binary or test, this option tells the build tool to link in
             .a's instead of .so's for user libraries whenever possible.
             Some system libraries may still be linked dynamically, as are libraries for which
             there is no static library. So the resulting executable will still be dynamically
             linked, hence only mostly static.

There are really three different ways to link an executable:

 FULLY STATIC, in which everything is linked statically; e.g. "gcc -static
             foo.o libbar.a libbaz.a -lm".

             This mode is enabled by specifying -static in the
             linkopts attribute.

 MOSTLY STATIC, in which all user libraries are linked statically (if a static
             version is available), but where system libraries are linked dynamically, e.g.
             "gcc foo.o libfoo.a libbaz.a -lm".

             This mode is enabled by specifying linkstatic=1.

 DYNAMIC, in which all libraries are linked dynamically (if a dynamic version is
             available), e.g. "gcc foo.o libfoo.so libbaz.so -lm".

             This mode is enabled by specifying linkstatic=0.

           The linkstatic attribute has a different meaning if used on a
           cc_library() rule.
           For a C++ library, linkstatic=1 indicates that only
           static linking is allowed, so no .so will be produced.

           If linkstatic=0, then the build tool will create symlinks to
           depended-upon shared libraries in the *.runfiles area.

          */
          .add(attr("linkstatic", BOOLEAN).value(true))
          .override(
              attr("$stl_default", LABEL)
                  .value(
                      new Attribute.ComputedDefault() {
                        @Override
                        public Object getDefault(AttributeMap rule) {
                          // Every cc_rule depends implicitly on STL to make
                          // sure that the correct headers are used for inclusion.
                          // The only exception is STL itself,
                          // to avoid cycles in the dependency graph.
                          Label stl = env.getToolsLabel("//tools/cpp:stl");
                          return rule.getLabel().equals(stl) ? null : stl;
                        }
                      }))
          .build();
    }
    @Override
    public Metadata getMetadata() {
      return RuleDefinition.Metadata.builder()
          .name("$cc_rule")
          .type(RuleClassType.ABSTRACT)
          .ancestors(CcDeclRule.class, CcBaseRule.class)
          .build();
    }
  }

  /** Implementation for the :lipo_context attribute. */
  static final LateBoundDefault<?, Label> LIPO_CONTEXT =
      LateBoundDefault.fromTargetConfiguration(
          CppConfiguration.class,
          null,
          (rule, attributes, cppConfig) -> {
            Label result = cppConfig.getLipoContextLabel();
            return (rule == null || rule.getLabel().equals(result)) ? null : result;
          });

  /**
   * Helper rule class.
   */
  public static final class CcLibraryBaseRule implements RuleDefinition {
    @Override
    public RuleClass build(Builder builder, RuleDefinitionEnvironment env) {
      return builder
          /*
           The list of header files published by
           this library to be directly included by sources in dependent rules.

This is the strongly preferred location for declaring header files that
             describe the interface for the library. These headers will be made
             available for inclusion by sources in this rule or in dependent rules.
             Headers not meant to be included by a client of this library should be
             listed in the srcs attribute instead, even if they are
             included by a published header. See "Header inclusion
             checking" for a more detailed description.

          */
          .add(
              attr("hdrs", LABEL_LIST)
                  .orderIndependent()
                  .direct_compile_time_input()
                  .allowedFileTypes(FileTypeSet.ANY_FILE))
          /*
          The prefix to strip from the paths of the headers of this rule.

When set, the headers in the hdrs attribute of this rule are accessible
          at their path with this prefix cut off.

If it's a relative path, it's taken as a package-relative one. If it's an absolute one,
          it's understood as a repository-relative path.

The prefix in the include_prefix attribute is added after this prefix is
          stripped.
           */
          .add(attr("strip_include_prefix", STRING))
          /*
          The prefix to add to the paths of the headers of this rule.

When set, the headers in the hdrs attribute of this rule are accessible
          at is the value of this attribute prepended to their repository-relative path.

The prefix in the strip_include_prefix attribute is removed before this
          prefix is added.
           */
          .add(attr("include_prefix", STRING))
          /*
           The list of header files published by
           this library to be textually included by sources in dependent rules.

This is the location for declaring header files that cannot be compiled on their own;
             that is, they always need to be textually included by other source files to build valid
             code.

           */
          .add(
              attr("textual_hdrs", LABEL_LIST)
                  .orderIndependent()
                  .direct_compile_time_input()
                  .legacyAllowAnyFileType())
          // TODO(bazel-team): document or remove.
          .add(attr("linkstamp", LABEL).allowedFileTypes(CPP_SOURCE, C_SOURCE))
          .build();
    }

    @Override
    public Metadata getMetadata() {
      return RuleDefinition.Metadata.builder()
          .name("$cc_library")
          .type(RuleClassType.ABSTRACT)
          .ancestors(CcRule.class)
          .build();
    }
  }

  /** Helper rule class. */
  public static final class CcBinaryBaseRule implements RuleDefinition {
    @Override
    public RuleClass build(Builder builder, RuleDefinitionEnvironment env) {
      return builder
          /*
          Override the default dependency on malloc.

            By default, C++ binaries are linked against //tools/cpp:malloc,
            which is an empty library so the binary ends up using libc malloc.
            This label must refer to a cc_library. If compilation is for a non-C++
            rule, this option has no effect. The value of this attribute is ignored if
            linkshared=1 is specified.

          */
          .add(
              attr("malloc", LABEL)
                  .value(env.getToolsLabel("//tools/cpp:malloc"))
                  .allowedFileTypes()
                  .allowedRuleClasses("cc_library"))
          .add(attr(":default_malloc", LABEL).value(CppRuleClasses.DEFAULT_MALLOC))
          /*
          Enable link stamping.
          Whether to encode build information into the binary. Possible values:

stamp = 1: Stamp the build information into the
              binary. Stamped binaries are only rebuilt when their dependencies
              change. Use this if there are tests that depend on the build
              information.

stamp = 0: Always replace build information by constant
              values. This gives good build result caching.

stamp = -1: Embedding of build information is controlled
              by the --[no]stamp flag.

          */
          .add(attr("stamp", TRISTATE).value(TriState.AUTO))
          .build();
    }

    @Override
    public Metadata getMetadata() {
      return RuleDefinition.Metadata.builder()
          .name("$cc_binary_base")
          .type(RuleClassType.ABSTRACT)
          .ancestors(CcRule.class)
          .build();
    }
  }
}


text:
import os
import contextlib
import os.path
import errno
import stat
import time
import mmap
from functools import partial
import logging
import fnmatch

from ftsdb import re # re or re2

from ftsdb import update_document, add_document, remove_document
from ftsdb import prefix_expr, logger, Cursor

# nly index the first N bytes of a file
MAX_FSIZE = 1024*1024

@contextlib.contextmanager
def get_bytes(fname, size):
    """
    yield a python Buffer mapping to the first MAX_FSIZE bytes of the given file
    """
    size = min(size, MAX_FSIZE)

    if size == 0:
        yield ''
        return

    # try to save some memory by using the OS buffers instead of copying
    # the file contents
    with open(fname, 'rb') as f:
        with contextlib.closing(mmap.mmap(f.fileno(), size, access=mmap.ACCESS_READ)) as mm:
            yield buffer(mm, 0, size)

def should_allow(exclusions, basename, dbpath):
    """
    returns whether a given file should be allowed to exist based on our
    exclusion list
    """
    # exclusions =:= [{type, pattern}]

    for typ, pattern in exclusions:
        if typ == 'simple':
            if basename == pattern:
                return False

        elif typ == 'glob':
            # on basename only?
            if fnmatch.fnmatch(basename, pattern):
                return False

        elif typ == 're':
            if pattern.search(dbpath):
                # match or search? basename or full path or dbpath?
                return False

    return True

def visitor(path, prefix, exclusions, cu, dirname, fnames):
    if logger.getEffectiveLevel() <= logging.DEBUG:
        logger.debug("Walking %s", dirname)
        fnames.sort() # makes the child 'walking' messages come out in an order the user expects

    remove = []

    for basename in fnames:
        fname = os.path.join(dirname, basename)

        assert fname.startswith(path)
        if prefix:
            assert fname.startswith(os.path.join(path, prefix))

        dbfname = fname[len(path)+1:]

        if exclusions and not should_allow(exclusions, basename, dbfname):
            remove.append(basename)
            continue

        try:
            st = os.stat(fname)
            mode = st.st_mode
            size = st[stat.ST_SIZE]
            if stat.S_ISDIR(mode):
                continue
            if not stat.S_ISREG(mode):
                logger.warn("Skipping non-regular file %s (%s)", dbfname, stat.S_IFMT(mode))
                continue
        except IOError as e:
            if e.errno == errno.ENOENT:
                # it was deleted in between
                continue
            raise

        cu.execute("INSERT INTO ondisk(path, dbpath, last_modified, size) VALUES (?, ?, ?, ?)",
                   (fname, dbfname, int(st[stat.ST_MTIME]), size))

    if remove and logger.getEffectiveLevel() <= logging.DEBUG:
        logger.debug("Removing %r from walk", list(remove))
    for r in remove:
        fnames.remove(r)

def tcount(c, tname):
    return c.execute("SELECT COUNT(*) FROM %s;" % tname).fetchone()[0]

def sync(conn, path, prefix, files = None):
    # path must be a full path on disk
    # prefix must be the full path on disk that we're syncing (or empty)

    start = time.time()

    news = updates = deletes = 0
    tnews = tupdates = tdeletes = 0 # for debug printing

    with Cursor(conn) as c, Cursor(conn) as cu:
        c.execute("""
                  CREATE TEMPORARY TABLE
                  ondisk (
                     path          TEXT PRIMARY KEY COLLATE BINARY,
                     dbpath        TEXT COLLATE BINARY,
                     last_modified INTEGER,
                     size          INTEGER
                  );
                  """)

        exclusions = []
        c.execute("SELECT type AS typ, expression AS e FROM exclusions;")
        for typ, expression in c:
            if typ == 're':
                expression = re.compile(expression)
            exclusions.append((typ, expression))

        wpath = path
        if prefix:
            wpath = os.path.join(path, prefix)

        if files is None:
            os.path.walk(wpath, partial(visitor, path, prefix, exclusions), cu)
        else:
            visitor(path, prefix, exclusions, cu, wpath, files)

        logger.debug("Creating temporary index on ondisk(dbpath)")
        c.execute("CREATE INDEX tmp_ondisk_dbpath_idx ON ondisk(dbpath)")

        if logger.getEffectiveLevel() <= logging.DEBUG:
            logger.debug("Found %d files on disk", tcount(cu, "ondisk"))

        # now build three groups: new files to be added, missing files to be
        # deleted, and old files to be updated

        # updated ones
        cu.execute("""
            CREATE TEMPORARY VIEW updated_files AS
            SELECT f.docid AS docid,
                   od.path AS path,
                   od.last_modified AS last_modified,
                   od.size AS size
              FROM ondisk od, files f
             WHERE od.dbpath = f.path
               AND f.last_modified < od.last_modified
        """)
        if logger.getEffectiveLevel() <= logging.DEBUG:
            tupdates = tcount(cu, "updated_files")
            logger.debug("Prepared %d files for updating", tupdates)

        # new files to create
        cu.execute("""
            CREATE TEMPORARY VIEW created_files AS
            SELECT od.path AS path,
                   od.dbpath AS dbpath,
                   od.last_modified,
                   od.size AS size
              FROM ondisk od
             WHERE NOT EXISTS(SELECT 1 FROM files f1 WHERE od.dbpath = f1.path)
        """)
        if logger.getEffectiveLevel() <= logging.DEBUG:
            tnews = tcount(cu, "created_files")
            logger.debug("Prepared %d files for creation", tnews)

        # files that we've indexed in the past but don't exist anymore
        if files is None:
            # has to be a table instead of a view because parameters aren't allowed in views
            cu.execute("""
                CREATE TEMPORARY TABLE deletedocs AS
                SELECT f.docid AS docid,
                       f.path AS path
                  FROM files f
                 WHERE (? = '' OR f.path LIKE ? ESCAPE '\\') -- ESCAPE disables the LIKE optimization :(
                   AND NOT EXISTS(SELECT 1 FROM ondisk od WHERE od.dbpath = f.path)
            """, (prefix, prefix_expr(prefix)))
            if logger.getEffectiveLevel() <= logging.DEBUG:
                tdeletes = tcount(cu, "deletedocs")
                logger.debug("Prepared %d files for deletion", tdeletes)

        # set up our debugging progress-printing closure
        def printprogress(*a):
            pass
        if logger.getEffectiveLevel() <= logging.INFO:
            progresstotal = tnews + tupdates + tdeletes
            if progresstotal > 0:
                def printprogress(s, fname):
                    total = updates+news+deletes
                    percent = float(total)/progresstotal*100
                    logger.info("%d/%d (%.1f%%) %s: %s", total, progresstotal, percent, s, fname)

        # files that we've indexed in the past but don't exist anymore
        if files is None:
            c.execute("SELECT docid, path FROM deletedocs");
            for (docid, fname) in c:
                printprogress("Deleting", fname)
                remove_document(cu, docid)

                deletes += 1

        c.execute("SELECT docid, path, last_modified, size FROM updated_files;")
        for (docid, fname, last_modified, size) in c:
            printprogress("Updating %.2f" % (size/1024.0), fname)
            try:
                with get_bytes(fname, size) as bb:
                    update_document(cu, docid, last_modified, bb)
            except IOError as e:
                if e.errno in (errno.ENOENT, errno.EPERM):
                    logger.warning("Skipping %s: %s", fname, os.strerror(e.errno))
                else:
                    raise
                continue
            updates += 1

        # new files to create
        c.execute("SELECT path, dbpath, last_modified, size FROM created_files;")
        for (fname, dbpath, last_modified, size) in c:
            # is it safe to re-use the last_modified that we got before, or do
            # we need to re-stat() the file? reusing it like this could make a
            # race-condition whereby we never re-update that file
            printprogress("Adding %.1fk" % (size/1024.0), fname)
            try:
                with get_bytes(fname, size) as bb:
                    add_document(cu, dbpath, last_modified, bb)
            except IOError as e:
                if e.errno in (errno.ENOENT, errno.EPERM):
                    logger.warning("Skipping %s: %s", fname, os.strerror(e.errno))
                else:
                    raise
                continue
            news += 1

        logger.info("%d new documents, %d deletes, %d updates in %.2fs", news, deletes, updates, time.time()-start)

        cu.execute("DROP VIEW updated_files;")
        cu.execute("DROP VIEW created_files;")
        cu.execute("DROP TABLE IF EXISTS deletedocs;")
        cu.execute("DROP TABLE ondisk;")


text:
"""Helper functions to upload data to logdog."""

import logging
import os
import sys

from pylib import constants
from pylib.utils import decorators

sys.path.insert(0, os.path.abspath(os.path.join(
    constants.DIR_SOURCE_ROOT, 'tools', 'swarming_client')))
from libs.logdog import bootstrap # pylint: disable=import-error

@decorators.NoRaiseException(default_return_value='',
                             exception_message=('Ignore this exception. '
                                                'crbug.com/675666'))
def text(name, data):
  """Uploads text to logdog.

  Args:
    name: Name of the logdog stream.
    data: String with data you want to upload.

  Returns:
    Link to view uploaded text in logdog viewer.
  """
  logging.info('Writing text to logdog stream, %s', name)
  with get_logdog_client().text(name) as stream:
    stream.write(data)
    return stream.get_viewer_url()

@decorators.NoRaiseException(default_return_value=None,
                             exception_message=('Ignore this exception. '
                                                'crbug.com/675666'))
def open_text(name):
  """Returns a file like object which you can write to.

  Args:
    name: Name of the logdog stream.

  Returns:
    A file like object. close() file when done.
  """
  logging.info('Opening text logdog stream, %s', name)
  return get_logdog_client().open_text(name)

@decorators.NoRaiseException(default_return_value='',
                             exception_message=('Ignore this exception. '
                                                'crbug.com/675666'))
def binary(name, binary_path):
  """Uploads binary to logdog.

  Args:
    name: Name of the logdog stream.
    binary_path: Path to binary you want to upload.

  Returns:
    Link to view uploaded binary in logdog viewer.
  """
  logging.info('Writing binary to logdog stream, %s', name)
  with get_logdog_client().binary(name) as stream:
    with open(binary_path, 'r') as f:
      stream.write(f.read())
      return stream.get_viewer_url()

@decorators.NoRaiseException(default_return_value='',
                             exception_message=('Ignore this exception. '
                                                'crbug.com/675666'))
def get_viewer_url(name):
  """Get Logdog viewer URL.

  Args:
    name: Name of the logdog stream.

  Returns:
    Link to view uploaded binary in logdog viewer.
  """
  return get_logdog_client().get_viewer_url(name)

@decorators.Memoize
def get_logdog_client():
  logging.info('Getting logdog client.')
  return bootstrap.ButlerBootstrap.probe().stream_client()


text:
namespace notifications {

class UserActionHandler;
struct NotificationParams;

// Service to schedule a notification to display in the future. An internal
// throttling mechanism will be applied to limit the maximum notification shown
// to the user. Also user's interaction with the notification will affect the
// frequency of notification delivery.
class NotificationScheduleService : public KeyedService {
 public:
  // Schedules a notification to display.
  virtual void Schedule(
      std::unique_ptr<NotificationParams> notification_params) = 0;

  // Deletes notifications of a given |SchedulerClientType|.
  virtual void DeleteNotifications(SchedulerClientType type) = 0;

  // Queries an overview of notifications for a given
  // |SchedulerClientType| including impression details.
  virtual void GetClientOverview(
      SchedulerClientType type,
      ClientOverview::ClientOverviewCallback callback) = 0;

  // Returns NotificationBackgroundTaskScheduler Handler.
  virtual NotificationBackgroundTaskScheduler::Handler*
  GetBackgroundTaskSchedulerHandler() = 0;

  // Returns the user action handler to process notification events.
  virtual UserActionHandler* GetUserActionHandler() = 0;

  NotificationScheduleService(const NotificationScheduleService&) = delete;
  NotificationScheduleService& operator=(const NotificationScheduleService&) =
      delete;
  ~NotificationScheduleService() override = default;

 protected:
  NotificationScheduleService() = default;
};

}  // namespace notifications

#endif  // CHROME_BROWSER_NOTIFICATIONS_SCHEDULER_PUBLIC_NOTIFICATION_SCHEDULE_SERVICE_H_


text:
package ca.corefacility.bioinformatics.irida.service.user;

import java.util.Collection;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;

import ca.corefacility.bioinformatics.irida.exceptions.UserGroupWithoutOwnerException;
import ca.corefacility.bioinformatics.irida.model.project.Project;
import ca.corefacility.bioinformatics.irida.model.user.User;
import ca.corefacility.bioinformatics.irida.model.user.group.UserGroup;
import ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin;
import ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin.UserGroupRole;
import ca.corefacility.bioinformatics.irida.model.user.group.UserGroupProjectJoin;
import ca.corefacility.bioinformatics.irida.service.CRUDService;

/**
 * Service for working with {@link UserGroup}s.
 *
 */
public interface UserGroupService extends CRUDService<Long, UserGroup> {

	/**
	 * Get all of the users in the group.
	 *
	 * @param userGroup
	 *            the group to get users for.
	 * @return the users in the group.
	 */
	Collection<UserGroupJoin> getUsersForGroup(final UserGroup userGroup);

	/**
	 * Get all of the projects with group.
	 *
	 * @param userGroup the group to get projects for.
	 * @return the projects linked to the group.
	 */
	Collection<UserGroupProjectJoin> getProjectsWithUserGroup(final UserGroup userGroup);

	/**
	 * Add a user to the group with the specified role.
	 *
	 * @param user
	 *            the user to add to the group
	 * @param userGroup
	 *            the group to add the user to
	 * @param role
	 *            the role that the user should have in the group
	 * @return the relationship created between the user and group.
	 */
	UserGroupJoin addUserToGroup(final User user, final UserGroup userGroup, final UserGroupRole role);

	/**
	 * Change the role for the {@link User} in the {@link UserGroup}.
	 *
	 * @param user
	 *            the user to change
	 * @param userGroup
	 *            the group to change
	 * @param role
	 *            the new role
	 * @return the modified relationship
	 * @throws UserGroupWithoutOwnerException
	 *             when the user group doesn't have an owner as a result of the
	 *             role change.
	 */
	UserGroupJoin changeUserGroupRole(final User user, final UserGroup userGroup, final UserGroupRole role)
			throws UserGroupWithoutOwnerException;

	/**
	 * Remove the {@link User} from the {@link UserGroup}.
	 *
	 * @param user
	 *            the user to remove
	 * @param userGroup
	 *            the group from which to remove the user.
	 * @throws UserGroupWithoutOwnerException
	 *             when the user group doesn't have an owner as a result of the
	 *             removal.
	 */
	void removeUserFromGroup(final User user, final UserGroup userGroup) throws UserGroupWithoutOwnerException;

	/**
	 * Filter the list of users in the {@link UserGroup} by username.
	 *
	 * @param username
	 *            the username string to filter on
	 * @param userGroup
	 *            the user group to filter for
	 * @param page
	 *            the current page
	 * @param size
	 *            the size of the page
	 * @param sort
	 *            the properties to sort on
	 * @return a page of {@link UserGroupJoin}.
	 */
	 Page<UserGroupJoin> filterUsersByUsername(final String username, final UserGroup userGroup, int page,
			int size, Sort sort);

	/**
	 * Get the set of {@link User} that are not currently in the
	 * {@link UserGroup}.
	 *
	 * @param userGroup the group to get the set of non-members
	 * @param filter    the search string to filter users by
	 * @return the list of users not in the group.
	 */
	List<User> getUsersNotInGroup(final UserGroup userGroup, final String filter);

	/**
	 * Get a page of {@link UserGroupProjectJoin} for a specific {@link Project}
	 * .
	 *
	 * @param searchName
	 *            the name to search with.
	 * @param project
	 *            the project
	 * @param page
	 *            the current page
	 * @param size
	 *            the size of the page
	 * @param sort
	 *            the properties to sort on
	 * @return a page of {@link UserGroupProjectJoin}.
	 */
	Page<UserGroupProjectJoin> getUserGroupsForProject(final String searchName, final Project project, int page,
			int size, Sort sort);

	/**
	 * Get a collection of {@link UserGroup} that aren't already on a
	 * {@link Project}.
	 *
	 * @param project
	 *            the project
	 * @param filter
	 *            the name to filter on
	 * @return the groups not already on the project.
	 */
	List<UserGroup> getUserGroupsNotOnProject(final Project project, final String filter);
}


text:
using System.Threading.Tasks;
using Recom.Bus.RabbitMQ;

namespace ConsoleApp
{
    public class TestSubscription : IMessageSubscriber
    {
        [RabbitSubscription(
            exchange: "TestExchange",
            queue: "ExplicitQueueName",
            routingKeys: new[] { "Key" })]
        public async Task AsyncMethod(SomeData data)
        {
            await Task.Delay(10);
            System.Console.WriteLine($"Async: {data.Date}");
        }

        [RabbitSubscription(
            exchange: "TestExchange",
            routingKeys: new[] { "Key", "Key2" })]
        public void SyncMethod(SomeData data)
        {
            System.Console.WriteLine($"Sync: {data.Date}");
        }

        [RabbitSubscription(
            routingKeys: new[] { "Default" })]
        public void Default(SomeData data)
        {
            System.Console.WriteLine($"Default: {data.Date}");
        }
    }
}


text:
class CreateNotifications < ActiveRecord::Migration
  def change
    create_table :notifications do |t|
      t.string :subject
      t.text :body
      t.integer :recipient_id
      t.integer :sender_id
      t.string :category

      t.timestamps null: false
    end
  end
end


text:
package org.unknown.web.controllers.subscriber.model;

/**
 * Author: Artem Voronov
 */
public class SubscriberProfile {
  private int ctn;
  private String name;
  private String email;
  private String activateDate; //formatted as 2017-02-24 12:45:00

  public SubscriberProfile() {
  }

  public SubscriberProfile(int ctn, String name, String email, String activateDate) {
    this.ctn = ctn;
    this.name = name;
    this.email = email;
    this.activateDate = activateDate;
  }

  public int getCtn() {
    return ctn;
  }

  public void setCtn(int ctn) {
    this.ctn = ctn;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  public String getActivateDate() {
    return activateDate;
  }

  public void setActivateDate(String activateDate) {
    this.activateDate = activateDate;
  }
}


text:
package states

import (
	"testing"

	"github.com/prometheus/client_golang/prometheus"

	"github.com/stretchr/testify/require"

	"github.com/ava-labs/avalanchego/database"
	"github.com/ava-labs/avalanchego/database/memdb"
	"github.com/ava-labs/avalanchego/ids"
	"github.com/ava-labs/avalanchego/utils/crypto"
	"github.com/ava-labs/avalanchego/utils/units"
	"github.com/ava-labs/avalanchego/vms/avm/fxs"
	"github.com/ava-labs/avalanchego/vms/avm/txs"
	"github.com/ava-labs/avalanchego/vms/components/avax"
	"github.com/ava-labs/avalanchego/vms/nftfx"
	"github.com/ava-labs/avalanchego/vms/propertyfx"
	"github.com/ava-labs/avalanchego/vms/secp256k1fx"
)

var (
	networkID uint32 = 10
	chainID          = ids.ID{5, 4, 3, 2, 1}
	assetID          = ids.ID{1, 2, 3}
	keys             = crypto.BuildTestKeys()
)

func TestTxState(t *testing.T) {
	require := require.New(t)

	db := memdb.New()
	parser, err := txs.NewParser([]fxs.Fx{
		&secp256k1fx.Fx{},
		&nftfx.Fx{},
		&propertyfx.Fx{},
	})
	require.NoError(err)

	stateIntf, err := NewTxState(db, parser, prometheus.NewRegistry())
	require.NoError(err)

	s := stateIntf.(*txState)

	_, err = s.GetTx(ids.Empty)
	require.Equal(database.ErrNotFound, err)

	tx := &txs.Tx{
		Unsigned: &txs.BaseTx{
			BaseTx: avax.BaseTx{
				NetworkID:    networkID,
				BlockchainID: chainID,
				Ins: []*avax.TransferableInput{{
					UTXOID: avax.UTXOID{
						TxID:        ids.Empty,
						OutputIndex: 0,
					},
					Asset: avax.Asset{ID: assetID},
					In: &secp256k1fx.TransferInput{
						Amt: 20 * units.KiloAvax,
						Input: secp256k1fx.Input{
							SigIndices: []uint32{
								0,
							},
						},
					},
				}},
			},
		},
	}

	err = tx.SignSECP256K1Fx(parser.Codec(), [][]*crypto.PrivateKeySECP256K1R{{keys[0]}})
	require.NoError(err)

	err = s.PutTx(ids.Empty, tx)
	require.NoError(err)

	loadedTx, err := s.GetTx(ids.Empty)
	require.NoError(err)
	require.Equal(tx.ID(), loadedTx.ID())

	s.txCache.Flush()

	loadedTx, err = s.GetTx(ids.Empty)
	require.NoError(err)
	require.Equal(tx.ID(), loadedTx.ID())

	err = s.DeleteTx(ids.Empty)
	require.NoError(err)

	_, err = s.GetTx(ids.Empty)
	require.Equal(database.ErrNotFound, err)

	s.txCache.Flush()

	_, err = s.GetTx(ids.Empty)
	require.Equal(database.ErrNotFound, err)
}


text:
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.chrome.browser.download;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import android.support.test.annotation.UiThreadTest;
import android.support.test.filters.SmallTest;
import android.support.test.rule.UiThreadTestRule;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.runner.RunWith;

import org.chromium.base.test.params.ParameterAnnotations.ClassParameter;
import org.chromium.base.test.params.ParameterAnnotations.UseRunnerDelegate;
import org.chromium.base.test.params.ParameterSet;
import org.chromium.base.test.params.ParameterizedRunner;
import org.chromium.base.test.util.DisabledTest;
import org.chromium.base.test.util.Feature;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.preferences.ChromePreferenceKeys;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
import org.chromium.chrome.test.ChromeJUnit4RunnerDelegate;
import org.chromium.chrome.test.util.browser.Features;
import org.chromium.components.offline_items_collection.ContentId;
import org.chromium.components.offline_items_collection.LegacyHelpers;
import org.chromium.components.offline_items_collection.OfflineItem.Progress;
import org.chromium.components.offline_items_collection.OfflineItemProgressUnit;
import org.chromium.components.offline_items_collection.PendingState;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * Tests of {@link DownloadNotificationService}.
 */
@RunWith(ParameterizedRunner.class)
@UseRunnerDelegate(ChromeJUnit4RunnerDelegate.class)
@Features.DisableFeatures({ChromeFeatureList.DOWNLOAD_NOTIFICATION_BADGE,
        ChromeFeatureList.DOWNLOAD_OFFLINE_CONTENT_PROVIDER})
public class DownloadNotificationServiceTest {
    private static final ContentId ID1 =
            LegacyHelpers.buildLegacyContentId(false, UUID.randomUUID().toString());
    private static final ContentId ID2 =
            LegacyHelpers.buildLegacyContentId(false, UUID.randomUUID().toString());
    private static final ContentId ID3 =
            LegacyHelpers.buildLegacyContentId(false, UUID.randomUUID().toString());

    @ClassParameter
    private static List<ParameterSet> sClassParams = Arrays.asList(
            new ParameterSet().value(false, false).name("GenericStatus"),
            new ParameterSet().value(true, false).name("EnableDescriptivePendingStatusOnly"),
            new ParameterSet().value(false, true).name("EnableDescriptiveFailStatusOnly"),
            new ParameterSet().value(true, true).name("EnableDescriptivePendingAndFailStatus"));

    @Rule
    public TestRule mFeaturesProcessor = new Features.JUnitProcessor();

    @Rule
    public UiThreadTestRule mUiThreadTestRule = new UiThreadTestRule();

    private MockDownloadNotificationService mDownloadNotificationService;
    private DownloadForegroundServiceManagerTest
            .MockDownloadForegroundServiceManager mDownloadForegroundServiceManager;
    private DownloadSharedPreferenceHelper mDownloadSharedPreferenceHelper;

    private final boolean mEnableOfflinePagesDescriptivePendingStatus;
    private final boolean mEnableOfflinePagesDescriptiveFailStatus;

    public DownloadNotificationServiceTest(boolean enableOfflinePagesDescriptivePendingStatus,
            boolean enableOfflinePagesDescriptiveFailStatus) {
        mEnableOfflinePagesDescriptivePendingStatus = enableOfflinePagesDescriptivePendingStatus;
        mEnableOfflinePagesDescriptiveFailStatus = enableOfflinePagesDescriptiveFailStatus;
    }

    private static DownloadSharedPreferenceEntry buildEntryStringWithGuid(ContentId contentId,
            int notificationId, String fileName, boolean metered, boolean autoResume) {
        return new DownloadSharedPreferenceEntry(
                contentId, notificationId, false, metered, fileName, autoResume, false);
    }

    @Before
    public void setUp() {
        if (mEnableOfflinePagesDescriptivePendingStatus) {
            Features.getInstance().enable(
                    ChromeFeatureList.OFFLINE_PAGES_DESCRIPTIVE_PENDING_STATUS);
        } else {
            Features.getInstance().disable(
                    ChromeFeatureList.OFFLINE_PAGES_DESCRIPTIVE_PENDING_STATUS);
        }
        if (mEnableOfflinePagesDescriptiveFailStatus) {
            Features.getInstance().enable(ChromeFeatureList.OFFLINE_PAGES_DESCRIPTIVE_FAIL_STATUS);
        } else {
            Features.getInstance().disable(ChromeFeatureList.OFFLINE_PAGES_DESCRIPTIVE_FAIL_STATUS);
        }
        DownloadNotificationService.clearResumptionAttemptLeft();
        mDownloadNotificationService = new MockDownloadNotificationService();
        mDownloadForegroundServiceManager =
                new DownloadForegroundServiceManagerTest.MockDownloadForegroundServiceManager();
        mDownloadNotificationService.setDownloadForegroundServiceManager(
                mDownloadForegroundServiceManager);
        mDownloadSharedPreferenceHelper = DownloadSharedPreferenceHelper.getInstance();
    }

    @After
    public void tearDown() {
        DownloadNotificationService.clearResumptionAttemptLeft();
        SharedPreferencesManager.getInstance().removeKey(
                ChromePreferenceKeys.DOWNLOAD_PENDING_DOWNLOAD_NOTIFICATIONS);
    }

    @Test
    @SmallTest
    @UiThreadTest
    @Feature({"Download"})
    public void testBasicDownloadFlow() {
        // Download is in-progress.
        mDownloadNotificationService.notifyDownloadProgress(ID1, "test",
                new Progress(1, 100L, OfflineItemProgressUnit.PERCENTAGE), 100L, 1L, 1L, true, true,
                false, null, null, false);
        mDownloadForegroundServiceManager.onServiceConnected();

        assertEquals(1, mDownloadNotificationService.getNotificationIds().size());
        int notificationId1 = mDownloadNotificationService.getLastNotificationId();
        assertTrue(mDownloadForegroundServiceManager.mDownloadUpdateQueue.containsKey(
                notificationId1));
        assertTrue(mDownloadNotificationService.mDownloadsInProgress.contains(ID1));

        // Download is paused.
        mDownloadNotificationService.notifyDownloadPaused(ID1, "test", true /* isResumable*/,
                false /* isAutoResumable */, true, false, null, null, false, false, false,
                PendingState.NOT_PENDING);

        assertEquals(1, mDownloadNotificationService.getNotificationIds().size());
        assertFalse(mDownloadForegroundServiceManager.mDownloadUpdateQueue.containsKey(
                notificationId1));
        assertFalse(mDownloadNotificationService.mDownloadsInProgress.contains(ID1));

        // Download is again in-progress.
        mDownloadNotificationService.notifyDownloadProgress(ID1, "test",
                new Progress(20, 100L, OfflineItemProgressUnit.PERCENTAGE), 100L, 1L, 1L, true,
                true, false, null, null, false);
        mDownloadForegroundServiceManager.onServiceConnected();

        assertEquals(1, mDownloadNotificationService.getNotificationIds().size());
        assertTrue(mDownloadForegroundServiceManager.mDownloadUpdateQueue.containsKey(
                notificationId1));
        assertTrue(mDownloadNotificationService.mDownloadsInProgress.contains(ID1));

        // Download is successful.
        mDownloadNotificationService.notifyDownloadSuccessful(
                ID1, "", "test", 1L, true, true, true, null, "", false, "", 0);
        assertEquals(1, mDownloadNotificationService.getNotificationIds().size());
        assertFalse(mDownloadForegroundServiceManager.mDownloadUpdateQueue.containsKey(
                notificationId1));
        assertFalse(mDownloadNotificationService.mDownloadsInProgress.contains(ID1));
    }

    @Test
    @SmallTest
    @UiThreadTest
    @Feature({"Download"})
    public void testDownloadPendingAndCancelled() {
        // Download is in-progress.
        mDownloadNotificationService.notifyDownloadProgress(ID1, "test",
                new Progress(1, 100L, OfflineItemProgressUnit.PERCENTAGE), 100L, 1L, 1L, true, true,
                false, null, null, false);
        mDownloadForegroundServiceManager.onServiceConnected();

        assertEquals(1, mDownloadNotificationService.getNotificationIds().size());
        int notificationId1 = mDownloadNotificationService.getLastNotificationId();
        assertTrue(mDownloadForegroundServiceManager.mDownloadUpdateQueue.containsKey(
                notificationId1));
        assertTrue(mDownloadNotificationService.mDownloadsInProgress.contains(ID1));

        // Download is interrupted and now is pending.
        mDownloadNotificationService.notifyDownloadPaused(ID1, "test", true /* isResumable */,
                true /* isAutoResumable */, true, false, null, null, false, false, false,
                PendingState.PENDING_NETWORK);
        assertEquals(1, mDownloadNotificationService.getNotificationIds().size());
        assertTrue(mDownloadForegroundServiceManager.mDownloadUpdateQueue.containsKey(
                notificationId1));
        assertFalse(mDownloadNotificationService.mDownloadsInProgress.contains(ID1));

        // Download is cancelled.
        mDownloadNotificationService.notifyDownloadCanceled(ID1, false);

        assertEquals(0, mDownloadNotificationService.getNotificationIds().size());
        assertFalse(mDownloadForegroundServiceManager.mDownloadUpdateQueue.containsKey(
                notificationId1));
        assertFalse(mDownloadNotificationService.mDownloadsInProgress.contains(ID1));
    }

    @Test
    @SmallTest
    @UiThreadTest
    @Feature({"Download"})
    public void testDownloadInterruptedAndFailed() {
        // Download is in-progress.
        mDownloadNotificationService.notifyDownloadProgress(ID1, "test",
                new Progress(1, 100L, OfflineItemProgressUnit.PERCENTAGE), 100L, 1L, 1L, true, true,
                false, null, null, false);
        mDownloadForegroundServiceManager.onServiceConnected();

        assertEquals(1, mDownloadNotificationService.getNotificationIds().size());
        int notificationId1 = mDownloadNotificationService.getLastNotificationId();
        assertTrue(mDownloadForegroundServiceManager.mDownloadUpdateQueue.containsKey(
                notificationId1));
        assertTrue(mDownloadNotificationService.mDownloadsInProgress.contains(ID1));

        // Download is interrupted but because it is not resumable, fails.
        mDownloadNotificationService.notifyDownloadPaused(ID1, "test", false /* isResumable*/,
                true /* isAutoResumable */, true, false, null, null, false, false, false,
                PendingState.PENDING_NETWORK);
        assertEquals(1, mDownloadNotificationService.getNotificationIds().size());
        assertFalse(mDownloadForegroundServiceManager.mDownloadUpdateQueue.containsKey(
                notificationId1));
        assertFalse(mDownloadNotificationService.mDownloadsInProgress.contains(ID1));
    }

    @Test
    @SmallTest
    @UiThreadTest
    @Feature({"Download"})
    @DisabledTest(message = "https://crbug.com/837298")
    public void testResumeAllPendingDownloads() {
        // Queue a few pending downloads.
        mDownloadSharedPreferenceHelper.addOrReplaceSharedPreferenceEntry(
                buildEntryStringWithGuid(ID1, 3, "success", false, true));
        mDownloadSharedPreferenceHelper.addOrReplaceSharedPreferenceEntry(
                buildEntryStringWithGuid(ID2, 4, "failed", true, true));
        mDownloadSharedPreferenceHelper.addOrReplaceSharedPreferenceEntry(
                buildEntryStringWithGuid(ID3, 5, "nonresumable", true, false));

        // Resume pending downloads when network is metered.
        DownloadManagerService.disableNetworkListenerForTest();
        DownloadManagerService.setIsNetworkMeteredForTest(true);
        mDownloadNotificationService.resumeAllPendingDownloads();

        assertEquals(1, mDownloadNotificationService.mResumedDownloads.size());
        assertEquals(ID2.id, mDownloadNotificationService.mResumedDownloads.get(0));

        // Resume pending downloads when network is not metered.
        mDownloadNotificationService.mResumedDownloads.clear();
        DownloadManagerService.setIsNetworkMeteredForTest(false);
        mDownloadNotificationService.resumeAllPendingDownloads();
        assertEquals(1, mDownloadNotificationService.mResumedDownloads.size());

        mDownloadSharedPreferenceHelper.removeSharedPreferenceEntry(ID1);
        mDownloadSharedPreferenceHelper.removeSharedPreferenceEntry(ID2);
        mDownloadSharedPreferenceHelper.removeSharedPreferenceEntry(ID3);
    }
}