text:
namespace Kafka.Client.Helper
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Reflection;
    using System.Threading;

    using Kafka.Client.Cfg;
    using Kafka.Client.Consumers;
    using Kafka.Client.Messages;
    using Kafka.Client.Producers;
    using Kafka.Client.Producers.Sync;
    using Kafka.Client.Requests;

    /// <summary>
    /// Temporaly add back for VERITAS
    /// This class should be obsolete.
    /// Please use KafkaSimpleManger and KafkaSimpleManagerConfiguration
    /// Manage Producer and Consumer configuration for one topic.
    /// Need use using while use this class.
    /// </summary>
    [CLSCompliant(false)]
    public class KafkaClientHelperWrapper : IDisposable
    {
        public static log4net.ILog Logger= log4net.LogManager.GetLogger(typeof(KafkaClientHelperWrapper));

        public IEnumerable<PartitionMetadata> PartitionsMetadata { get; private set; }

        public int PartitionCount
        {
            get
            {
                return this.PartitionsMetadata == null ? 0 : Enumerable.Count<PartitionMetadata>(this.PartitionsMetadata);
            }
        }

        public KafkaClientHelperConfiguration HelperConfiguration { get; private set; }

        public IDictionary<int, BrokerConfiguration> Leaders { get { return this.leaders; } }

        // the pool of syncProducer for TopicMetaData requests, which retrieve PartitionMetaData, including leaders and ISRs.
        private SyncProducerPool syncProducerPool = null;
        private List<ISyncProducer> syncProducerList = null;
        private int producerIndex = -1;
        private DateTime lastTimeBrokerListUpdated = DateTime.Now.AddYears(-1000);

        private string topic = string.Empty;
        private TopicMetadataRequest topicMetaRequest = null;

        // partitionIndex as the key; track leader for each partition
        private Dictionary<int, BrokerConfiguration> leaders = new Dictionary<int, BrokerConfiguration>();
        private DateTime lastTimeLeaderFound = DateTime.Now.AddYears(-1000);

        // brokerId as the key; producer configuration and producer for each broker
        private Dictionary<int, ProducerConfiguration> producerConfs = new Dictionary<int, ProducerConfiguration>();
        private Dictionary<int, Producer<int, Message>> producers = new Dictionary<int, Producer<int, Kafka.Client.Messages.Message>>();

        private Random random = new Random();

        // Track whether Dispose has been called.
        private bool disposed = false;

        /// <summary>
        /// KafkaClientHelperWrapper produces or consumes messages to/from a particular partition of a topic
        /// </summary>
        /// <param name="topic">target topic</param>
        /// <param name="helperConfiguration"></param>
        public KafkaClientHelperWrapper(string topic, KafkaClientHelperConfiguration helperConfiguration)
        {
            if (string.IsNullOrEmpty(topic))
            {
                throw new System.ArgumentNullException("topicParam");
            }

            if (helperConfiguration == null)
            {
                throw new System.ArgumentNullException("helperConfiguration");
            }

            if (helperConfiguration.LeaderConfig == null && string.IsNullOrEmpty(helperConfiguration.KafkaBrokerList) && helperConfiguration.ZookeeperConfig == null)
            {
                throw new System.ArgumentException("Leader and KafkaBrokerList and Zookeepr connection string are missing");
            }

            Logger.DebugFormat("KafkaClientHelperWrapper constructor start,topicParam={0},kafkaBrokerList={1},zookeeper={2}",
                topic, helperConfiguration.KafkaBrokerList, helperConfiguration.ZookeeperConfig);

            ServicePointManager.DefaultConnectionLimit = 5000;
            ServicePointManager.UseNagleAlgorithm = false;
            this.topic = topic;
            this.HelperConfiguration = helperConfiguration;

            if (this.HelperConfiguration.LeaderConfig != null)
            {
                this.leaders.Add(this.HelperConfiguration.LeaderPartition, this.HelperConfiguration.LeaderConfig);
                this.lastTimeLeaderFound = DateTime.Now;
            }

            this.topicMetaRequest = TopicMetadataRequest.Create(
                new string[] { this.topic },                                    // topic
                0,                                                              // API version id
                this.random.Next(int.MinValue, int.MaxValue),                   // correlation id
                Assembly.GetExecutingAssembly().ManifestModule.ToString());     // client id
        }

        /// <summary>
        /// Return ConsumerConfiguration for the targetPartition
        /// </summary>
        public ConsumerConfiguration GetConsumerConf(int partitionIndex)
        {
            this.ValidateConsumerLeaderInitialization(partitionIndex);

            // BufferSize default=65536; FetchSize default=1048576; MaxFetchsize=FetchSize * 10; Timeout default=-1; SocketTimeout=30 * 1000
            // Debug value: SocketTimeout = 3600 * 1000, Timeout = 3600 * 1000
            return new ConsumerConfiguration()
            {
                Timeout = this.HelperConfiguration.Timeout + 10,
                ReceiveTimeout = this.HelperConfiguration.Timeout + 20,
                Broker = this.leaders[partitionIndex],
                BufferSize = this.HelperConfiguration.MaxMessageSize,
                FetchSize = this.HelperConfiguration.MaxMessageSize,
                AutoCommit = false
            };
        }

        /// <summary>
        /// Return Consumer for the targetPartition
        /// </summary>
        /// <returns></returns>
        public Consumer GetKafkaConsumer(int partitionIndex)
        {
            this.ValidateConsumerLeaderInitialization(partitionIndex);

            return new Consumer(this.GetConsumerConf(partitionIndex), this.leaders[partitionIndex].Host, this.leaders[partitionIndex].Port);
        }

        /// <summary>
        /// Return ProducerConfiguration for the targetPartition
        /// </summary>
        /// <param name="partitionParam"></param>
        /// <returns></returns>
        public ProducerConfiguration GetProducerConf(int partitionParam)
        {
            int targetPartition = this.ValidateProducerLeaderTargetPartitionInialization(partitionParam);

            if (this.producerConfs.Count == 0 || !this.producerConfs.ContainsKey(this.leaders[targetPartition].BrokerId))
            {

            }

            return this.producerConfs[this.leaders[targetPartition].BrokerId];
        }

        /// <summary>
        /// Return Producer for the targetPartition
        /// </summary>
        /// <param name="partitionParam"></param>
        /// <returns></returns>
        public Producer<int, Message> GetKafkaProducer(int partitionParam,
            CompressionCodecs comparessionCodec,
            out ProducerConfiguration producerConfiguration)
        {
            int targetPartition = this.ValidateProducerLeaderTargetPartitionInialization(partitionParam);

            if (this.producers.Count == 0 || !this.producers.ContainsKey(this.leaders[targetPartition].BrokerId))
            {

                Logger.DebugFormat("GetProducerConf creatNew for leaderBrokerId={0},leaders.Count={1},targetPartition={2},leaderBrokerDetails={3}",
                   this.leaders[targetPartition].BrokerId, this.leaders.Count(), targetPartition, this.leaders[targetPartition]);

                // AckTimeout deafult=1, MaxMessageSize default = 1000000; ConnectionTimout default=5000; BufferSize default=102400, DefaultSocketTimeout=30 * 1000
                ProducerConfiguration producerConf = new ProducerConfiguration(new List<BrokerConfiguration>() { this.leaders[targetPartition] })
                {
                    // PartitionerClass = "Kafka.Client.Producers.Partitioning.ModPartitioner",
                    PartitionerClass = ProducerConfiguration.DefaultPartitioner,
                    //
                    // https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol#AGuideToTheKafkaProtocol-ProduceRequest
                    // RequiredAcks: This field indicates how many acknowledgements the servers should receive before responding to the request.
                    //     If it is 0 the server will not send any response (this is the only case where the server will not reply to a request).
                    //     If it is 1, the server will wait the data is written to the local log before sending a response.
                    //     If it is -1 the server will block until the message is committed by all in sync replicas before sending a response.
                    //     For any number > 1 the server will block waiting for this number of acknowledgements to occur (but the server will never wait for
                    //         more acknowledgements than there are in-sync replicas).
                    // Timeout: This provides a maximum time in milliseconds the server can await the receipt of the number of acknowledgements in RequiredAcks.
                    //      The timeout is not an exact limit on the request time for a few reasons:
                    //    (1) it does not include network latency,
                    //    (2) the timer begins at the beginning of the processing of this request so if many requests are queued due to server overload that wait time will not be included,
                    //    (3) we will not terminate a local write so if the local write time exceeds this timeout it will not be respected. To get a hard timeout of this type the client
                    //         should use the socket timeout.
                    RequiredAcks = -1,
                    CompressionCodec = comparessionCodec,
                    SendTimeout = this.HelperConfiguration.Timeout,
                    ReceiveTimeout = this.HelperConfiguration.Timeout,
                    AckTimeout = this.HelperConfiguration.AckTimeout,
                    MaxMessageSize = this.HelperConfiguration.MaxMessageSize,
                    BufferSize = this.HelperConfiguration.MaxMessageSize,
                    TotalNumPartitions = this.PartitionsMetadata != null ? this.PartitionsMetadata.Count() : this.HelperConfiguration.TotalNumPartitions
                };

                this.producerConfs.Add(this.leaders[targetPartition].BrokerId, producerConf);

                this.producers.Add(this.leaders[targetPartition].BrokerId
                    , new Producer<int, Kafka.Client.Messages.Message>(producerConf));
            }

            producerConfiguration = this.producerConfs[this.leaders[targetPartition].BrokerId];
            return this.producers[this.leaders[targetPartition].BrokerId];
        }

        /// <summary>
        /// Find a leader broker by issuing a topicMetaReqeust
        /// </summary>
        /// <returns>number of leaders
        public int CallKafkaAndFindLeader()
        {
            string s = string.Empty;

            // retry at least once
            int maxRetry = 1;
            int retryCount = 0;
            while (this.leaders.Count == 0 && retryCount < maxRetry)
            {
                SyncProducerConfiguration producerConfig = null;
                try
                {
                    Console.WriteLine("in try catch block");
                    if (this.syncProducerList == null)
                    {
                        // initialize SyncProducerPool
                        this.InitSyncProducerPool();
                    }

                    // retry each broker two times. Observed non-leader broker responds with error message, and some leader knows only a small set of partitions
                    maxRetry = this.syncProducerList == null ? maxRetry : this.syncProducerList.Count * 2;
                    if (retryCount > 0)
                    {
                        // not the first time, rotate the producer, wait a few seconds, before retry
                        this.RoundRobinSyncProducer(this.syncProducerList.Count);
                        Thread.Sleep(this.HelperConfiguration.MaxRetryWaitTime);
                    }

                    producerConfig = this.syncProducerList[this.producerIndex].Config;
                    Console.WriteLine("got producer config");
                    IEnumerable<TopicMetadata> topicMetaData = this.syncProducerList[this.producerIndex].Send(this.topicMetaRequest);
                    Console.WriteLine("got topic meta data");
                    if (topicMetaData.Count() == 0)
                    {
                        s = "FindLeader found ZERO count topicMetaData,topic={0},producerIndex[{1}]=[{2}]";
                        Logger.ErrorFormat(s, this.topic, this.producerIndex, KafkaClientHelperUtils.SyncProducerConfigToString(producerConfig));
                        continue;
                    }
                    else if (topicMetaData.First().PartitionsMetadata.Count() == 0)
                    {
                        s = "FindLeader found ZERO count partitionMetaData,topic={0},producerIndex[{1}]=[{2}]";
                        Logger.ErrorFormat(s, this.topic, this.producerIndex, KafkaClientHelperUtils.SyncProducerConfigToString(producerConfig));
                        continue;
                    }
                    else if (topicMetaData.Count() > 1)
                    {
                        s = "FindLeader found more than one topicData,topicMetaData.Count={0},topic={1},producerIndex[{2}]=[{3}],will use first one - expect only one";
                        Logger.ErrorFormat(s, topicMetaData.Count(), this.topic, this.producerIndex, KafkaClientHelperUtils.SyncProducerConfigToString(producerConfig));
                    }

                    this.PartitionsMetadata = topicMetaData.First().PartitionsMetadata.OrderBy(r => r.PartitionId);
                    this.leaders.Clear();
                    foreach (var m in this.PartitionsMetadata)
                    {
                        this.leaders.Add(m.PartitionId, new BrokerConfiguration()
                        {
                            BrokerId = m.Leader.Id,
                            Host = m.Leader.Host,
                            Port = m.Leader.Port
                        });
                    }

                    if (this.leaders.Count == 0)
                    {
                        s = "no leader found,topic={0},producerIndex[{1}]=[{2}],retryCount={3}";
                        Logger.ErrorFormat(s, this.topic, this.producerIndex, KafkaClientHelperUtils.SyncProducerConfigToString(producerConfig), retryCount);
                        continue;
                    }
                    else
                    {
                        s = "KafkaClientHelperWrapper[{0}] leader found,leaders.Count={1},topic={2},producerIndex[{3}]=[{4}],retryCount={5}";
                        Logger.DebugFormat(s, this.GetHashCode().ToString("X"), this.leaders.Count, this.topic, this.producerIndex,
                            KafkaClientHelperUtils.SyncProducerConfigToString(producerConfig), retryCount);
                        this.lastTimeLeaderFound = DateTime.Now;
                    }
                }
                catch (Exception e)
                {
                    s = "FindLeader hit exception,topic={0}, producerIndex[{1}]=[{2}],retryCount={3},maxRetry={4}";
                    Logger.Error(string.Format(s, this.topic, this.producerIndex, KafkaClientHelperUtils.SyncProducerConfigToString(producerConfig), retryCount, maxRetry), e);

                    if (retryCount >= maxRetry)
                    {
                        // reach maximum retry, bail
                        throw;
                    }
                }
                finally
                {
                    ++retryCount;
                }
            } // end of while loop

            return this.leaders.Count;
        }

        /// <summary>
        /// Implement IDisposable
        /// </summary>
        public void Dispose()
        {
            this.Dispose(true);
            // This object will be cleaned up by the Dispose method.
            // Therefore, you should call GC.SupressFinalize to
            // take this object off the finalization queue
            // and prevent finalization code for this object
            // from executing a second time.
            GC.SuppressFinalize(this);
        }

        // Dispose(bool disposing) executes in two distinct scen/arios.
        // If disposing equals true, the method has been called directly
        // or indirectly by a user's code. Managed and unmanaged resources
        // can be disposed.
        // If disposing equals false, the method has been called by the
        // runtime from inside the finalizer and you should not reference
        // other objects. Only unmanaged resources can be disposed.
        protected virtual void Dispose(bool disposing)
        {
            // Check to see if Dispose has already been called.
            if (!this.disposed)
            {
                // If disposing equals true, dispose all managed
                // and unmanaged resources.
                if (disposing)
                {
                    // Dispose managed resources.
                    this.ClearSyncProducerPool();
                    this.ClearLeader();
                    this.topic = string.Empty;
                    this.topicMetaRequest = null;
                    this.random = null;
                }

                // Call the appropriate methods to clean up
                // unmanaged resources here.
                // If disposing is false,
                // only the following code is executed.
                // Note disposing has been done.
                this.disposed = true;
            }
        }

        /// <summary>
        /// clear the leader and configurations dependent on the leader
        /// </summary>
        private void ClearLeader()
        {
            this.producerConfs.Clear();
            if (this.producers.Count > 0)
            {
                foreach (var v in this.producers)
                {
                    v.Value.Dispose();
                }
                this.producers.Clear();
            }

            this.leaders.Clear();
            this.PartitionsMetadata = null;
            Logger.DebugFormat("KafkaClientHelperWrapper[{0}] Leader cleared.", this.GetHashCode().ToString("X"));
        }

        /// <summary>
        /// Initialize SyncProducerPool with either the list of brokers provided by the caller or query Zookeeper
        /// </summary>
        private void InitSyncProducerPool()
        {
            if (this.syncProducerList != null)
            {
                // already initialized
                return;
            }

            if (!string.IsNullOrEmpty(this.HelperConfiguration.KafkaBrokerList))
            {
                // Honor KafkaBrokerList first
                this.syncProducerList = new List<ISyncProducer>();
                string[] brokers = this.HelperConfiguration.KafkaBrokerList.Split(new char[] { ',' });

                int i = 0;
                foreach (string v in brokers)
                {
                    i++;
                    string[] brokerParams = v.Split(new char[] { ':' });
                    int port = 0;
                    if (brokerParams.Count() == 2 && !string.IsNullOrEmpty(brokerParams[0]) && int.TryParse(brokerParams[1], out port))
                    {
                        var syncProducer = KafkaClientHelperUtils.TryCreateSyncProducer(i, brokerParams[0], port);
                        if (syncProducer != null)
                        {
                            this.syncProducerList.Add(syncProducer);
                        }
                    }
                }
            }
            else if (this.HelperConfiguration.ZookeeperConfig != null)
            {
                // Honor Zookeeper connection string only when KafkaBroker list not provided
                var producerConfig = new ProducerConfiguration(new List<BrokerConfiguration>());
                producerConfig.ZooKeeper = this.HelperConfiguration.ZookeeperConfig;
                //Backward compatible, so set to 1.
                producerConfig.SyncProducerOfOneBroker = 1;
                this.syncProducerPool = new SyncProducerPool(producerConfig);
                this.syncProducerList = this.syncProducerPool.GetShuffledProducers();
            }
            else if (this.HelperConfiguration.LeaderConfig != null)
            {
                // if only leader is provided, the leader to be added to syncProducerList
                var leader = this.HelperConfiguration.LeaderConfig;
                this.syncProducerList = new List<ISyncProducer>() { new SyncProducer(new SyncProducerConfiguration() { BrokerId = leader.BrokerId, Host = leader.Host, Port = leader.Port }) };
            }

            if (this.syncProducerList == null || this.syncProducerList.Count == 0)
            {
                string s = string.Format("KafkaClientHelperWrapper[{0}] SyncProducerPool Initialization produced empty syncProducer list,Kafka={1},Zookeeper={2}",
                    this.GetHashCode().ToString("X"), this.HelperConfiguration.KafkaBrokerList, this.HelperConfiguration.ZookeeperConfig);

                Logger.Debug(s);
                this.syncProducerList = null;
                throw new ArgumentException(s);
            }

            this.RoundRobinSyncProducer(this.syncProducerList.Count);
            this.lastTimeBrokerListUpdated = DateTime.Now;
            Logger.DebugFormat("KafkaClientHelperWrapper[{0}] SyncProducerPool initialized", this.GetHashCode().ToString("X"));
        }

        /// <summary>
        /// Reset syncProducer pool
        /// </summary>
        private void ClearSyncProducerPool()
        {
            if (this.syncProducerList != null)
            {
                this.syncProducerList.Clear();
                this.syncProducerList = null;
            }

            if (this.syncProducerPool != null)
            {
                this.syncProducerPool.Dispose();
                this.syncProducerPool = null;
            }

            this.producerIndex = 0;
            Logger.DebugFormat("KafkaClientHelperWrapper[{0}] SyncProducerPool cleared", this.GetHashCode().ToString("X"));
        }

        public int RoundRobinSyncProducer()
        {
            return this.RoundRobinSyncProducer(this.syncProducerList.Count);
        }

        private int RoundRobinSyncProducer(int count)
        {
            if (this.producerIndex == -1)
            {
                this.producerIndex = this.random.Next(0, count);
            }
            else
            {
                this.producerIndex = (this.producerIndex + 1) % count;
            }
            return this.producerIndex;
        }

        /// <summary>
        /// Ensure the leader for the producer of the target partition is initialized /
        /// </summary>
        /// <param name="partitionIndex"></param>
        /// <returns>targetPartition</returns>
        private int ValidateProducerLeaderTargetPartitionInialization(int partitionIndex)
        {
            if (this.leaders == null || this.leaders.Count == 0 || !this.leaders.ContainsKey(partitionIndex))
            {
                throw new IndexOutOfRangeException(string.Format("leaders not initialized,leadersCount={0},targetPartition={1}", this.leaders.Count, partitionIndex));
            }

            return partitionIndex;
        }

        /// <summary>
        /// Ensure the leader for the consumer is initialized
        /// </summary>
        private void ValidateConsumerLeaderInitialization(int partitionIndex)
        {
            if (partitionIndex < 0)
            {
                throw new IndexOutOfRangeException("For a consumer, partitionIndex must not be negative");
            }

            if (this.leaders == null || this.leaders.Count == 0 || !this.leaders.ContainsKey(partitionIndex))
            {
                throw new IndexOutOfRangeException("leaders not initialized");
            }
        }
    }
}

redpajama_set_name: RedPajamaGithub


text:
***Warning: Project is experimental/pre-alpha and is NOT ready to be actually used***

A simple Node.js / Express server that converts YouTube playlists to Podcast RSS feeds, and acts as a proxy for the podcast items by piping extracted YouTube audio to the response (using [ytdl-core](https://github.com/fent/node-ytdl-core)). The streaming happens on the fly, and nothing is stored on disk. No transcoding through ffmpeg is needed.

## TODO
* [ ] Add tests to make sure the correct bytes are being piped out
* [ ] Figure out if there are (older) YouTube video that don't have AAC codecs available
* [ ] Major cleanup
* [ ] Fix total size glitches in iOS Podcast app
* [ ] Add tooling


text:
package org.kie.workbench.common.dmn.api.property.dmn;

import java.util.HashSet;

import org.junit.Test;
import org.kie.workbench.common.dmn.api.definition.v1_1.DMNModelInstrumentedBase;
import org.kie.workbench.common.dmn.api.property.dmn.types.BuiltInType;

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

public class QNameTest {

    private final static String NAMESPACE_URI = "namespace-uri";

    private final static String LOCAL_PART = "local-part";

    private final static String PREFIX = "prefix";

    private final static QName QNAME1 = new QName(NAMESPACE_URI, LOCAL_PART, PREFIX);

    private final static QName QNAME2 = new QName(NAMESPACE_URI, LOCAL_PART, PREFIX);

    private final static QName QNAME3 = new QName(NAMESPACE_URI, LOCAL_PART);

    @Test
    public void checkEquals() {
        assertEquals(QNAME1, QNAME2);
        assertEquals(QNAME1, QNAME3);
        assertEquals(QNAME2, QNAME3);
    }

    @Test
    public void checkHashCode() {
        assertEquals(QNAME1.hashCode(), QNAME2.hashCode());
        assertEquals(QNAME1.hashCode(), QNAME3.hashCode());
        assertEquals(QNAME2.hashCode(), QNAME3.hashCode());

        final HashSet<QName> qNames = new HashSet<>();

        qNames.add(QNAME1);

        assertTrue(qNames.contains(QNAME2));
        assertTrue(qNames.contains(QNAME3));
    }

    @Test
    public void testZeroArgumentConstructor() {
        final QName implicitQName = new QName();
        final QName explicitQName = new QName(QName.NULL_NS_URI,
                                              BuiltInType.UNDEFINED.getName(),
                                              DMNModelInstrumentedBase.Namespace.FEEL.getPrefix());

        assertEquals(explicitQName, implicitQName);
    }

    @Test
    public void testConstructorWithBuiltInTypeParameter() {
        final QName qname = new QName(BuiltInType.STRING);
        assertEquals(qname.getLocalPart(), BuiltInType.STRING.getName());
        assertEquals(qname.getNamespaceURI(), QName.NULL_NS_URI);
    }
}


text:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>com.xiaomi.linden</groupId>
        <artifactId>linden-parent</artifactId>
        <version>0.2.0-SNAPSHOT</version>
        <relativePath>../linden-parent/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>linden-thrift</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.twitter</groupId>
            <artifactId>libthrift</artifactId>
            <version>0.5.0-7</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.twitter</groupId>
            <artifactId>finagle-thrift_2.10</artifactId>
            <version>6.33.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.thrift</groupId>
                    <artifactId>libthrift</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.twitter</groupId>
                <artifactId>scrooge-maven-plugin</artifactId>
                <version>3.16.0</version>
                <configuration>
                    <language>java</language>
                    <thriftOpts>
                        <!-- add other Scrooge command line options using thriftOpts -->
                        <thriftOpt>--finagle</thriftOpt>
                        <!-- thriftOpt>-ostrich</thriftOpt-->
                    </thriftOpts>
                    <!-- tell scrooge to extract thrifts from these artifacts -->
                </configuration>
                <executions>
                    <execution>
                        <id>thrift-sources</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>


text:
namespace Envoy {
namespace Upstream {
MockRetryHostPredicate::MockRetryHostPredicate() = default;

MockRetryHostPredicate::~MockRetryHostPredicate() = default;

} // namespace Upstream
} // namespace Envoy


text:
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.261
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace MLifter.BusinessLayer.Properties {
    using System;

    ///
    ///   A strongly-typed resource class, for looking up localized strings, etc.
    ///
    // This class was auto-generated by the StronglyTypedResourceBuilder
    // class via a tool like ResGen or Visual Studio.
    // To add or remove a member, edit your .ResX file then rerun ResGen
    // with the /str option, or rebuild your VS project.
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    internal class Resources {

        private static global::System.Resources.ResourceManager resourceMan;

        private static global::System.Globalization.CultureInfo resourceCulture;

        [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        internal Resources() {
        }

        ///
        ///   Returns the cached ResourceManager instance used by this class.
        ///
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        internal static global::System.Resources.ResourceManager ResourceManager {
            get {
                if (object.ReferenceEquals(resourceMan, null)) {
                    global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("MLifter.BusinessLayer.Properties.Resources", typeof(Resources).Assembly);
                    resourceMan = temp;
                }
                return resourceMan;
            }
        }

        ///
        ///   Overrides the current thread's CurrentUICulture property for all
        ///   resource lookups using this strongly typed resource class.
        ///
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        internal static global::System.Globalization.CultureInfo Culture {
            get {
                return resourceCulture;
            }
            set {
                resourceCulture = value;
            }
        }

        ///
        ///   Looks up a localized string similar to MemoryLifter USB-Stick on {0}.
        ///
        internal static string CSH_CONNECTION_NAME {
            get {
                return ResourceManager.GetString("CSH_CONNECTION_NAME", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to
        ///
        ///        <title>{0}</title>
        ///
        ///
        ///        {1}
        ///
        ///.
        ///
        internal static string ERROR_PAGE {
            get {
                return ResourceManager.GetString("ERROR_PAGE", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to The pool is empty!.
        ///
        internal static string EXCEPTION_POOL_EMPTY {
            get {
                return ResourceManager.GetString("EXCEPTION_POOL_EMPTY", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to ?guid={0}.
        ///
        internal static string ExtensionURI_Suffix {
            get {
                return ResourceManager.GetString("ExtensionURI_Suffix", resourceCulture);
            }
        }

        internal static System.Drawing.Bitmap Folder {
            get {
                object obj = ResourceManager.GetObject("Folder", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }

        ///
        ///   Looks up a localized string similar to Empty module!.
        ///
        internal static string LEARNING_MODULES_PAGE_EMPTY_LM {
            get {
                return ResourceManager.GetString("LEARNING_MODULES_PAGE_EMPTY_LM", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to No Images!.
        ///
        internal static string LEARNING_MODULES_PAGE_NO_IMAGES {
            get {
                return ResourceManager.GetString("LEARNING_MODULES_PAGE_NO_IMAGES", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to localhost.
        ///
        internal static string LOCAL_MODULE {
            get {
                return ResourceManager.GetString("LOCAL_MODULE", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to ?id={0}.
        ///
        internal static string MediaURI_Suffix {
            get {
                return ResourceManager.GetString("MediaURI_Suffix", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to .key.
        ///
        internal static string STICK_KEY_EXTENSION {
            get {
                return ResourceManager.GetString("STICK_KEY_EXTENSION", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to LMKeys.
        ///
        internal static string STICK_KEY_FOLDER {
            get {
                return ResourceManager.GetString("STICK_KEY_FOLDER", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to Default.
        ///
        internal static string TEACHER_DEFAULT {
            get {
                return ResourceManager.GetString("TEACHER_DEFAULT", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to Mr. Nice Guy.
        ///
        internal static string TEACHER_NICEGUY {
            get {
                return ResourceManager.GetString("TEACHER_NICEGUY", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to Occasional Learner.
        ///
        internal static string TEACHER_OCCASIONAL {
            get {
                return ResourceManager.GetString("TEACHER_OCCASIONAL", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to Strict.
        ///
        internal static string TEACHER_STRICT {
            get {
                return ResourceManager.GetString("TEACHER_STRICT", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to Click button for example:.
        ///
        internal static string XSL_CLICKFORE {
            get {
                return ResourceManager.GetString("XSL_CLICKFORE", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to Click button for question:.
        ///
        internal static string XSL_CLICKFORQ {
            get {
                return ResourceManager.GetString("XSL_CLICKFORQ", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to The correct answer is:.
        ///
        internal static string XSL_CORRECT {
            get {
                return ResourceManager.GetString("XSL_CORRECT", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to Card has been demoted to Box 1!.
        ///
        internal static string XSL_DEMOTE_MESSAGE {
            get {
                return ResourceManager.GetString("XSL_DEMOTE_MESSAGE", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to Stylesheet Error.
        ///
        internal static string XSL_ERROR_CAPTION {
            get {
                return ResourceManager.GetString("XSL_ERROR_CAPTION", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to An error occurred when compiling {0}.

        ///Your stylesheet or linked parts may contain errors.

        ///If you are unsure what to do, restore the original file in "Tools - Style Editor"..
        ///
        internal static string XSL_ERROR_TEXT {
            get {
                return ResourceManager.GetString("XSL_ERROR_TEXT", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to Your answer was correct!.
        ///
        internal static string XSL_FEEDBACK_CORRECT {
            get {
                return ResourceManager.GetString("XSL_FEEDBACK_CORRECT", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to Your answer was wrong!.
        ///
        internal static string XSL_FEEDBACK_WRONG {
            get {
                return ResourceManager.GetString("XSL_FEEDBACK_WRONG", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to Click here to show the image..
        ///
        internal static string XSL_LISTENING_MODE_IMAGE_BUTTON_TEXT {
            get {
                return ResourceManager.GetString("XSL_LISTENING_MODE_IMAGE_BUTTON_TEXT", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to This card has reached the highest box..
        ///
        internal static string XSL_PROMOTE_HIGHEST_MESSAGE {
            get {
                return ResourceManager.GetString("XSL_PROMOTE_HIGHEST_MESSAGE", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to Card has been promoted to Box {box_number}!.
        ///
        internal static string XSL_PROMOTE_MESSAGE {
            get {
                return ResourceManager.GetString("XSL_PROMOTE_MESSAGE", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to Click image to show original size..
        ///
        internal static string XSL_RESIZE_PICTURE {
            get {
                return ResourceManager.GetString("XSL_RESIZE_PICTURE", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to Click image to show smaller size..
        ///
        internal static string XSL_RESTORE_PICTURE {
            get {
                return ResourceManager.GetString("XSL_RESTORE_PICTURE", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to Do you want to demote the card?.
        ///
        internal static string XSL_SELFASSESMENT_DEMOTE_MESSAGE {
            get {
                return ResourceManager.GetString("XSL_SELFASSESMENT_DEMOTE_MESSAGE", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to Do you want to promote the card?.
        ///
        internal static string XSL_SELFASSESMENT_PROMOTE_MESSAGE {
            get {
                return ResourceManager.GetString("XSL_SELFASSESMENT_PROMOTE_MESSAGE", resourceCulture);
            }
        }

        ///
        ///   Looks up a localized string similar to You entered:.
        ///
        internal static string XSL_YOUENTERED {
            get {
                return ResourceManager.GetString("XSL_YOUENTERED", resourceCulture);
            }
        }
    }
}


text:
#ifndef CYTH_PARSER_170211203515
#define CYTH_PARSER_170211203515

#include <list>

#include "cyth_AST.hpp"
#include "parser.hpp"

class cython_lexer : public csu::lexer<csu::token_data>
{
private:
    int STMT_id;
    int NEW_BLOCK_id;
    int END_BLOCK_id;
    int EOF_id;

    std::list<long> block_lengths;
    bool expecting_block;

public:
    cython_lexer(lex_func_t _EOF_action, std::shared_ptr< std::vector< std::shared_ptr<csu::DFA_state> > > _state_table,
                std::shared_ptr< std::vector< lex_func_t > > _actions, std::shared_ptr< std::vector< unsigned int> > _lexer_states);

    void set_identifiers(int _STMT_id, int _NEW_BLOCK_id, int _END_BLOCK_id, int _EOF_id);

    void expect_block();

    csu::token_data lex_eatnewline(csu::utf8_string& data, csu::location_span& loc);
    csu::token_data lex_newline(csu::utf8_string& data, csu::location_span& loc);
    csu::token_data lex_EOF( csu::location_span& loc );
};

//need to make this a global. No idea how not too.

class make_cyth_parser
{
private:
    csu::parser_generator cyth_parser_generator;

    int STMT_id;
    int NEW_BLOCK_id;
    int END_BLOCK_id;
    int EOF_id;

public:
    make_cyth_parser(bool do_file_IO=true);

    std::shared_ptr<csu::parser> get_parser(bool do_file_IO=true);
};

#endif


text:
module Nokogiri
  module XML
    class Document < Node

      attr_accessor :cstruct # :nodoc:

      def url # :nodoc:
        cstruct[:URL]
      end

      def root=(node) # :nodoc:
        LibXML.xmlDocSetRootElement(cstruct, node.cstruct)
        node
      end

      def root # :nodoc:
        ptr = LibXML.xmlDocGetRootElement(cstruct)
        ptr.null? ? nil : Node.wrap(LibXML::XmlNode.new(ptr))
      end

      def encoding=(encoding) # :nodoc:
        # TODO: if :encoding is already set, then it's probably getting leaked.
        cstruct[:encoding] = LibXML.xmlStrdup(encoding)
      end

      def encoding # :nodoc:
        cstruct[:encoding].read_string
      end

      def self.read_io(io, url, encoding, options) # :nodoc:
        wrap_with_error_handling do
          LibXML.xmlReadIO(IoCallbacks.reader(io), nil, nil, url, encoding, options)
        end
      end

      def self.read_memory(string, url, encoding, options) # :nodoc:
        wrap_with_error_handling do
          LibXML.xmlReadMemory(string, string.length, url, encoding, options)
        end
      end

      def dup(deep = 1) # :nodoc:
        dup_ptr = LibXML.xmlCopyDoc(cstruct, deep)
        return nil if dup_ptr.null?

        # xmlCopyDoc does not preserve document type. wtf?
        cstruct = LibXML::XmlDocumentCast.new(dup_ptr)
        cstruct[:type] = self.type

        self.class.wrap(dup_ptr)
      end

      class << self
        def new(*args) # :nodoc:
          version = args.first || "1.0"
          doc = wrap(LibXML.xmlNewDoc(version))
          doc.send :initialize, *args
          doc
        end

        def wrap(doc_struct) # :nodoc: #
          if doc_struct.is_a?(FFI::Pointer)
            # cast native pointers up into a doc cstruct
            return nil if doc_struct.null?
            doc_struct = LibXML::XmlDocument.new(doc_struct)
          end

          doc                  = self.allocate
          doc.cstruct          = doc_struct
          doc.cstruct.ruby_doc = doc
          doc.instance_eval { @decorators = nil; @node_cache = [] }
          doc.send :initialize
          doc
        end
      end

      private

      class << self
        def wrap_with_error_handling(&block) # :nodoc:
          error_list = []
          LibXML.xmlInitParser()
          LibXML.xmlResetLastError()
          LibXML.xmlSetStructuredErrorFunc(nil, SyntaxError.error_array_pusher(error_list))

          ptr = yield

          LibXML.xmlSetStructuredErrorFunc(nil, nil)

          if ptr.null?
            error = LibXML.xmlGetLastError()
            if error
              raise SyntaxError.wrap(error)
            else
              raise RuntimeError, "Could not parse document"
            end
          end

          document = wrap(ptr)
          document.errors = error_list
          return document
        end
      end

    end
  end
end


text:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace Figlotech.Core {

    public class SyncTimeStampSource {
        DateTime BaseTimeStamp { get; set; } = DateTime.UtcNow;
        Stopwatch Watch { get; set; }

        private SyncTimeStampSource() { }

        public DateTime GetUtc() {
            var retv = BaseTimeStamp + Watch.Elapsed;
            return retv;
        }

        // GetUtcNetworkTime is based on stackoverflow.com/questions/1193955
        public static async Task<SyncTimeStampSource> FromNtpServer(string ntpServer) {
            SyncTimeStampSource retv = new SyncTimeStampSource();
            Stopwatch requestTimer = new Stopwatch();
            requestTimer.Start();
            retv.BaseTimeStamp = await Fi.Tech.GetUtcNetworkTime(ntpServer);
            requestTimer.Stop();
            retv.BaseTimeStamp.Subtract(TimeSpan.FromMilliseconds(requestTimer.ElapsedMilliseconds / 2));
            retv.Watch = new Stopwatch();
            retv.Watch.Start();
            return retv;
        }

        public static async Task<SyncTimeStampSource> FromNtpServerCached(string ntpServer) {
            var cacheDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "FTH");
            Directory.CreateDirectory(cacheDir);
            var cacheFile = Path.Combine(cacheDir, "system.time");
            if(File.Exists(cacheFile)) {
                var bytes = File.ReadAllBytes(cacheFile);
                if(bytes.Length < sizeof(Int64) * 2) {
                    File.Delete(cacheFile);
                    return await FromNtpServerCached(ntpServer);
                }
                var localTimeSaved = new DateTime(BitConverter.ToInt64(bytes, 0));
                var remoteTimeSaved = new DateTime(BitConverter.ToInt64(bytes, sizeof(Int64)));
                if (
                    localTimeSaved.Ticks != new FileInfo(cacheFile).CreationTimeUtc.Ticks ||
                    localTimeSaved > DateTime.UtcNow ||
                    remoteTimeSaved > DateTime.UtcNow ||
                    DateTime.UtcNow - localTimeSaved > TimeSpan.FromHours(2)
                ) {
                    File.Delete(cacheFile);
                    return await FromNtpServerCached(ntpServer);
                }
                var offsetLocal = DateTime.UtcNow - localTimeSaved;
                var offsetRemote = remoteTimeSaved + offsetLocal;
                var retv = FromTicks(offsetRemote.Ticks);
                return retv;
            } else {
                var bytes = new byte[sizeof(Int64) * 2];
                var retv = await FromNtpServer(ntpServer);
                var remoteTime = (retv).BaseTimeStamp;
                var localTime = DateTime.UtcNow;
                Array.Copy(BitConverter.GetBytes(localTime.Ticks), 0, bytes, 0, sizeof(Int64));
                Array.Copy(BitConverter.GetBytes(remoteTime.Ticks), 0, bytes, sizeof(Int64), sizeof(Int64));
                File.WriteAllBytes(cacheFile, bytes);
                File.SetCreationTime(cacheFile, localTime);
                return retv;
            }
        }

        public static SyncTimeStampSource FromTicks(long ticks) {
            SyncTimeStampSource retv = new SyncTimeStampSource();
            retv.BaseTimeStamp = new DateTime(ticks, DateTimeKind.Utc);
            retv.Watch = new Stopwatch();
            retv.Watch.Start();
            return retv;
        }

        public static SyncTimeStampSource FromLocalTime() {
            SyncTimeStampSource retv = new SyncTimeStampSource();
            retv.BaseTimeStamp = DateTime.UtcNow;
            retv.Watch = new Stopwatch();
            retv.Watch.Start();
            return retv;
        }
    }
}


text:
faraday_config node['faraday']['user'] do
  home node['faraday']['home']
end


text:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:cxf="http://cxf.apache.org/core" xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">

	<context:annotation-config />

	<bean id="orgMappingProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean" >
		<property name="location" value="classpath:net/techreadiness/batch/org/org-import-mapping.properties" />
	</bean>

	<bean id="orgBinder" class="net.techreadiness.batch.Binder">
		<constructor-arg ref="orgMappingProperties" />
	</bean>

</beans>


text:
'use strict'

const fs = require('fs')
const send = require('../../client/send')
const run = require('../../client/run')
const stats = require('../../client/stats')
const dump = require('../../client/dump')
const jackal = require('../helpers/jackal')
const Provider = require('../helpers/provider')

describe('Happy Path Sequence Test', function () {
  let port, dbPath, options, providerOne, providerTwo

  before(function (done) {
    port = 8378
    dbPath = 'test/integration/api/db.json'
    options = {
      port: port,
      quiet: true,
      db: { path: dbPath }
    }

    jackal.start(options, done)
  })

  before(function (done) {
    providerOne = new Provider()
    providerOne.start({ port: 8379 }, done)
  })

  before(function (done) {
    providerTwo = new Provider()
    providerTwo.start({ port: 8380 }, done)
  })

  after(function (done) {
    fs.stat(dbPath, (err, stats) => {
      if (stats) { fs.unlink(dbPath, done) }
      else { done() }
    })
  })

  after(jackal.stop)

  after(function (done) {
    providerOne.stop(done)
  })

  after(function (done) {
    providerTwo.stop(done)
  })

  it('should error trying to send non existent contracts from the first consumer to jackal', function (done) {
    send(`http://localhost:${port}`, 'test/contracts/missing-contracts-file.json', {}, (err, res, body) => {
      expect(err).to.equal('Missing contract file test/contracts/missing-contracts-file.json')
      expect(res).to.not.exist
      expect(body).to.not.exist
      done()
    })
  })

  it('should exit gracefully trying to send non existent contracts from the first consumer to jackal with the skip flag set', function (done) {
    const expected = {
      message: 'Skipping no contracts, file not found: test/contracts/missing-contracts-file.json',
      status: 'SKIPPED',
      results: []
    }

    send(`http://localhost:${port}`, 'test/contracts/missing-contracts-file.json', { skipMissingContract: true }, (err, res, body) => {
      expect(err).to.not.exist
      expect(res).to.not.exist
      expect(body).to.eql(expected)
      done()
    })
  })

  it('should send invalid contracts from the first consumer to jackal and return the response', function (done) {
    const expected = {
      message: 'One or more contracts are invalid',
      status: 'INVALID',
      results: [
        { contract: 'provider_one/user_api/OK <- consumer', valid: true, errors: null },
        { contract: 'provider_one/receipt_api/OK <- consumer', valid: true, errors: null },
        { contract: 'provider_two/product_api/OK <- consumer', valid: false, errors: [ { name: 'ContractValidationError', message: '"request" is required' } ] }
      ]
    }

    send(`http://localhost:${port}`, 'test/contracts/consumer-invalid-missing-field.json', {}, (err, res, body) => {
      expect(err).to.not.exist
      expect(res.statusCode).to.equal(400)
      expect(body).to.eql(expected)
      done()
    })
  })

  it('should show no contracts currently in the jackal database', function (done) {
    const expected = { apiCount: 0, consumerCount: 0, consumers: [], contractCount: 0, providerCount: 0, providers: [] }

    stats(`http://localhost:${port}`, {}, (err, res, body) => {
      expect(err).to.not.exist
      expect(res.statusCode).to.equal(200)
      expect(body).to.eql(expected)
      done()
    })
  })

  it('should send valid, failing contracts from the first consumer to jackal and return the response', function (done) {
    const expected = {
      message: 'Failures Exist',
      status: 'FAILED',
      results: [
        { name: 'provider_one/user_api/OK', consumer: 'consumer', status: 'Pass', error: null },
        { name: 'provider_one/receipt_api/OK', consumer: 'consumer', status: 'Pass', error: null },
        { name: 'provider_two/product_api/OK', consumer: 'consumer', status: 'Fail', error: 'Contract failed: "description" must be a number\nresponse.statusCode: 200\nresponse.body: [{"id":1,"name":"Crutch","description":"Walking Aid"},{"id":2,"name":"Jackal","description":"Wild Animal"}]' }
      ]
    }

    send(`http://localhost:${port}`, 'test/contracts/consumer-valid-failing.json', {}, (err, res, body) => {
      expect(err).to.not.exist
      expect(res.statusCode).to.equal(200)
      expect(body).to.eql(expected)
      done()
    })
  })

  it('should show no contracts currently in the jackal database', function (done) {
    const expected = { apiCount: 0, consumerCount: 0, consumers: [], contractCount: 0, providerCount: 0, providers: [] }

    stats(`http://localhost:${port}`, {}, (err, res, body) => {
      expect(err).to.not.exist
      expect(res.statusCode).to.equal(200)
      expect(body).to.eql(expected)
      done()
    })
  })

  it('should send valid, passing contracts from the first consumer to jackal and return the response', function (done) {
    const expected = {
      message: 'All Passed',
      status: 'PASSED',
      results: [
        { name: 'provider_one/user_api/OK', consumer: 'consumer', status: 'Pass', error: null },
        { name: 'provider_one/receipt_api/OK', consumer: 'consumer', status: 'Pass', error: null },
        { name: 'provider_two/product_api/OK', consumer: 'consumer', status: 'Pass', error: null }
      ]
    }

    send(`http://localhost:${port}`, 'test/contracts/consumer-valid-passing.json', {}, (err, res, body) => {
      expect(err).to.not.exist
      expect(res.statusCode).to.equal(201)
      expect(body).to.eql(expected)
      done()
    })
  })

  it('should show three contracts currently in the jackal database', function (done) {
    const expected = { apiCount: 3, consumerCount: 1, consumers: [ 'consumer' ], contractCount: 3, providerCount: 2, providers: [ 'provider_one', 'provider_two' ] }

    stats(`http://localhost:${port}`, {}, (err, res, body) => {
      expect(err).to.not.exist
      expect(res.statusCode).to.equal(200)
      expect(body).to.eql(expected)
      done()
    })
  })

  it('should run the passing provider successfully and return the response', function (done) {
    const expected = {
      message: 'All Passed',
      status: 'PASSED',
      results: [
        { name: 'provider_one/user_api/OK', consumer: 'consumer', status: 'Pass', error: null },
        { name: 'provider_one/receipt_api/OK', consumer: 'consumer', status: 'Pass', error: null }
      ]
    }

    run(`http://localhost:${port}`, 'provider_one', {}, (err, res, body) => {
      expect(err).to.not.exist
      expect(res.statusCode).to.equal(200)
      expect(body).to.eql(expected)
      done()
    })
  })

  it('should run the second passing provider successfully and return the response', function (done) {
    const expected = {
      message: 'All Passed',
      status: 'PASSED',
      results: [
        { name: 'provider_two/product_api/OK', consumer: 'consumer', status: 'Pass', error: null }
      ]
    }

    run(`http://localhost:${port}`, 'provider_two', {}, (err, res, body) => {
      expect(err).to.not.exist
      expect(res.statusCode).to.equal(200)
      expect(body).to.eql(expected)
      done()
    })
  })

  it('should send valid, failing contracts from the first consumer to jackal and return the response', function (done) {
    const expected = {
      message: 'Failures Exist',
      status: 'FAILED',
      results: [
        { name: 'provider_one/user_api/OK', consumer: 'consumer', status: 'Pass', error: null },
        { name: 'provider_one/receipt_api/OK', consumer: 'consumer', status: 'Pass', error: null },
        { name: 'provider_two/product_api/OK', consumer: 'consumer', status: 'Fail', error: 'Contract failed: "description" must be a number\nresponse.statusCode: 200\nresponse.body: [{"id":1,"name":"Crutch","description":"Walking Aid"},{"id":2,"name":"Jackal","description":"Wild Animal"}]' }
      ]
    }

    send(`http://localhost:${port}`, 'test/contracts/consumer-valid-failing.json', {}, (err, res, body) => {
      expect(err).to.not.exist
      expect(res.statusCode).to.equal(200)
      expect(body).to.eql(expected)
      done()
    })
  })

  it('should still show three contracts currently in the jackal database', function (done) {
    const expected = { apiCount: 3, consumerCount: 1, consumers: [ 'consumer' ], contractCount: 3, providerCount: 2, providers: [ 'provider_one', 'provider_two' ] }

    stats(`http://localhost:${port}`, {}, (err, res, body) => {
      expect(err).to.not.exist
      expect(res.statusCode).to.equal(200)
      expect(body).to.eql(expected)
      done()
    })
  })

  it('should run the still passing first provider successfully and return the response', function (done) {
    const expected = {
      message: 'All Passed',
      status: 'PASSED',
      results: [
        { name: 'provider_one/user_api/OK', consumer: 'consumer', status: 'Pass', error: null },
        { name: 'provider_one/receipt_api/OK', consumer: 'consumer', status: 'Pass', error: null }
      ]
    }

    run(`http://localhost:${port}`, 'provider_one', {}, (err, res, body) => {
      expect(err).to.not.exist
      expect(res.statusCode).to.equal(200)
      expect(body).to.eql(expected)
      done()
    })
  })

  it('should run the still passing second provider successfully and return the response', function (done) {
    const expected = {
      message: 'All Passed',
      status: 'PASSED',
      results: [
        { name: 'provider_two/product_api/OK', consumer: 'consumer', status: 'Pass', error: null }
      ]
    }

    run(`http://localhost:${port}`, 'provider_two', {}, (err, res, body) => {
      expect(err).to.not.exist
      expect(res.statusCode).to.equal(200)
      expect(body).to.eql(expected)
      done()
    })
  })

  it('should get stats for the consumer', function (done) {
    const expected = { apiCount: 3, consumer: 'consumer', contractCount: 3, providerCount: 2, providers: [ 'provider_one', 'provider_two' ] }

    stats(`http://localhost:${port}`, { consumer: 'consumer' }, (err, res, body) => {
      expect(err).to.not.exist
      expect(res.statusCode).to.equal(200)
      expect(body).to.eql(expected)
      done()
    })
  })

  it('should get stats for the first provider', function (done) {
    const expected = { apiCount: 2, apis: [ 'user_api', 'receipt_api' ], consumerCount: 1, consumers: [ 'consumer' ], contractCount: 2, provider: 'provider_one' }

    stats(`http://localhost:${port}`, { provider: 'provider_one' }, (err, res, body) => {
      expect(err).to.not.exist
      expect(res.statusCode).to.equal(200)
      expect(body).to.eql(expected)
      done()
    })
  })

  it('should get stats for the second provider', function (done) {
    const expected = { apiCount: 1, apis: [ 'product_api' ], consumerCount: 1, consumers: [ 'consumer' ], contractCount: 1, provider: 'provider_two' }

    stats(`http://localhost:${port}`, { provider: 'provider_two' }, (err, res, body) => {
      expect(err).to.not.exist
      expect(res.statusCode).to.equal(200)
      expect(body).to.eql(expected)
      done()
    })
  })

  it('should dump the database', function (done) {
    dump(`http://localhost:${port}`, (err, res, body) => {
      expect(err).to.not.exist
      expect(res.statusCode).to.equal(200)
      expect(body).to.be.an('object')
      done()
    })
  })
})


text:
"""Contains TF-Slim code for training models.

This script contains various functions for training models which includes
creating train tensors, altering gradients and optimizing loss functions. The
following presents an example of how to use the learning module:

  g = tf.Graph()

  # Setup the model and losses
  images, labels = LoadData(...)
  predictions = CreateNetwork(images)
  total_loss = slim.losses.log_loss(predictions, labels)

  # Define the optimizer.
  optimizer = tf.train.MomentumOptimizer(FLAGS.learning_rate, FLAGS.momentum)

  # Set up training gradients and dependencies.
  train_tensor = slim.learning.create_train_tensor(total_loss, optimizer)

  # Run training.
  learning.train(train_tensor, my_log_dir)
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import sys
import time

from tensorflow.contrib.framework.python.ops import variables
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import ops
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import clip_ops
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import logging_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import variables as tf_variables
from tensorflow.python.platform import tf_logging as logging
from tensorflow.python.training import optimizer as tf_optimizer
from tensorflow.python.training import saver as tf_saver
from tensorflow.python.training import supervisor
from tensorflow.python.training import sync_replicas_optimizer
from tensorflow.python.training import training_util

__all__ = [
    'clip_gradient_norms',
    'multiply_gradients',
    'create_train_op',
    'train'
]

def clip_gradient_norms(gradients_to_variables, max_norm):
  """Clips the gradients by the given value.

  Args:
    gradients_to_variables: A list of gradient to variable pairs (tuples).
    max_norm: the maximum norm value.

  Returns:
    A list of clipped gradient to variable pairs.
  """
  clipped_grads_and_vars = []
  for grad, var in gradients_to_variables:
    if grad is not None:
      if isinstance(grad, ops.IndexedSlices):
        tmp = clip_ops.clip_by_norm(grad.values, max_norm)
        grad = ops.IndexedSlices(tmp, grad.indices, grad.dense_shape)
      else:
        grad = clip_ops.clip_by_norm(grad, max_norm)
    clipped_grads_and_vars.append((grad, var))
  return clipped_grads_and_vars

def multiply_gradients(grads_and_vars, gradient_multipliers):
  """Multiply specified gradients.

  Args:
    grads_and_vars: A list of gradient to variable pairs (tuples).
    gradient_multipliers: A map from either `Variables` or `Variable` op names
      to the coefficient by which the associated gradient should be scaled.

  Returns:
    The updated list of gradient to variable pairs.

  Raises:
    ValueError: If `grads_and_vars` is not a list or if `gradient_multipliers`
    is empty or None or if `gradient_multipliers` is not a dictionary.
  """
  if not isinstance(grads_and_vars, list):
    raise ValueError('`grads_and_vars` must be a list.')
  if not gradient_multipliers:
    raise ValueError('`gradient_multipliers` is empty.')
  if not isinstance(gradient_multipliers, dict):
    raise ValueError('`gradient_multipliers` must be a dict.')

  multiplied_grads_and_vars = []
  for grad, var in grads_and_vars:
    if var in gradient_multipliers or var.op.name in gradient_multipliers:
      key = var if var in gradient_multipliers else var.op.name
      if grad is None:
        raise ValueError('Requested multiple of `None` gradient.')

      if isinstance(grad, ops.IndexedSlices):
        tmp = grad.values * constant_op.constant(gradient_multipliers[key],
                                                 dtype=grad.dtype)
        grad = ops.IndexedSlices(tmp, grad.indices, grad.dense_shape)
      else:
        grad *= constant_op.constant(gradient_multipliers[key],
                                     dtype=grad.dtype)
    multiplied_grads_and_vars.append((grad, var))
  return multiplied_grads_and_vars

def add_gradients_summaries(grads_and_vars):
  """Add summaries to gradients.

  Args:
    grads_and_vars: A list of gradient to variable pairs (tuples).

  Returns:
    The list of created summaries.
  """
  summaries = []
  for grad, var in grads_and_vars:
    if grad is not None:
      if isinstance(grad, ops.IndexedSlices):
        grad_values = grad.values
      else:
        grad_values = grad
      summaries.append(logging_ops.histogram_summary(
          var.op.name + ':gradient', grad_values))
      summaries.append(logging_ops.histogram_summary(
          var.op.name + ':gradient_norm', clip_ops.global_norm([grad_values])))
    else:
      logging.info('Var %s has no gradient', var.op.name)

  return summaries

def create_train_op(
    total_loss,
    optimizer,
    global_step=None,
    update_ops=None,
    variables_to_train=None,
    clip_gradient_norm=0,
    summarize_gradients=False,
    gate_gradients=tf_optimizer.Optimizer.GATE_OP,
    aggregation_method=None,
    colocate_gradients_with_ops=False,
    gradient_multipliers=None):
  """Creates an `Operation` that evaluates the gradients and returns the loss.

  Args:
    total_loss: A `Tensor` representing the total loss.
    optimizer: A tf.Optimizer to use for computing the gradients.
    global_step: A `Tensor` representing the global step variable. If left as
      `None`, then slim.variables.global_step() is used.
    update_ops: an optional list of updates to execute. Note that the update_ops
      that are used are the union of those update_ops passed to the function and
      the value of slim.ops.GetUpdateOps(). Therefore, if `update_ops` is None,
      then the value of slim.ops.GetUpdateOps() is still used.
    variables_to_train: an optional list of variables to train. If None, it will
      default to all tf.trainable_variables().
    clip_gradient_norm: If greater than 0 then the gradients would be clipped
      by it.
    summarize_gradients: Whether or not add summaries for each gradient.
    gate_gradients: How to gate the computation of gradients. See tf.Optimizer.
    aggregation_method: Specifies the method used to combine gradient terms.
      Valid values are defined in the class `AggregationMethod`.
    colocate_gradients_with_ops: Whether or not to try colocating the gradients
      with the ops that generated them.
    gradient_multipliers: A dictionary of either `Variables` or `Variable` op
      names to the coefficient by which the associated gradient should be
      scaled.
  Returns:
    A `Tensor` that when evaluated, computes the gradients and returns the total
      loss value.
  """
  if global_step is None:
    global_step = variables.get_or_create_global_step()

  # Update ops use GraphKeys.UPDATE_OPS collection if update_ops is None.
  global_update_ops = set(ops.get_collection(ops.GraphKeys.UPDATE_OPS))
  if update_ops is None:
    update_ops = global_update_ops
  else:
    update_ops = set(update_ops)
  if not global_update_ops.issubset(update_ops):
    logging.warning('update_ops in create_train_op does not contain all the '
                    ' update_ops in GraphKeys.UPDATE_OPS')

  # Make sure update_ops are computed before total_loss.
  if update_ops:
    with ops.control_dependencies(update_ops):
      barrier = control_flow_ops.no_op(name='update_barrier')
    total_loss = control_flow_ops.with_dependencies([barrier], total_loss)

  if variables_to_train is None:
    # Default to tf.trainable_variables()
    variables_to_train = tf_variables.trainable_variables()
  else:
    # Make sure that variables_to_train are in tf.trainable_variables()
    for v in variables_to_train:
      assert v in tf_variables.trainable_variables()

  assert variables_to_train

  # Create the gradients. Note that apply_gradients adds the gradient
  # computation to the current graph.
  grads = optimizer.compute_gradients(
      total_loss, variables_to_train, gate_gradients=gate_gradients,
      aggregation_method=aggregation_method,
      colocate_gradients_with_ops=colocate_gradients_with_ops)

  # Scale gradients.
  if gradient_multipliers:
    grads = multiply_gradients(grads, gradient_multipliers)

  # Clip gradients.
  if clip_gradient_norm > 0:
    grads = clip_gradient_norms(grads, clip_gradient_norm)

  # Summarize gradients.
  if summarize_gradients:
    add_gradients_summaries(grads)

  # Create gradient updates.
  grad_updates = optimizer.apply_gradients(grads, global_step=global_step)

  # Make sure total_loss is valid.
  total_loss = array_ops.check_numerics(total_loss, 'LossTensor is inf or nan')

  # Ensure the train_tensor computes grad_updates.
  return control_flow_ops.with_dependencies([grad_updates], total_loss)

def _wait_for_step(sess, global_step, step):
  """Wait till the global step has reached at least 'step'.

  Args:
    sess: A session.
    global_step: A Tensor.
    step: Int.  The global step to reach.
  """
  while True:
    if training_util.global_step(sess, global_step) >= step:
      break
    time.sleep(1.0)

def train_loop(sv,
               sess,
               train_op,
               should_stop_op,
               should_log_op,
               global_step,
               cleanup_op=None):
  """Runs the training loop.

  Args:
    sv: The supervisor instance.
    sess: The session.
    train_op: An `Operation` that evaluates the gradients and returns the
      total loss.
    should_stop_op: A boolean `Tensor` that signals the end of training.
    should_log_op: A boolean `Tensor` that signals whether or not we should log
      the global step and current loss.
    global_step: A `Tensor` representing the global training step.
    cleanup_op: An operation to run if an exception is thrown.

  Returns:
    total_loss: The total loss value after training.
  """
  total_loss = 0

  try:
    while not sv.should_stop():
      start_time = time.time()
      total_loss, np_global_step = sess.run([train_op, global_step])
      time_elapsed = time.time() - start_time

      # TODO(nsilberman): figure out why we can't put this into sess.run. The
      # issue right now is that the stop check depends on the global step. The
      # increment of global step often happens via the train op, which used
      # created using optimizer.apply_gradients.
      #
      # Since running `train_op` causes the global step to be incremented, one
      # would expected that using a control dependency would allow the
      # should_stop check to be run in the same session.run call:
      #
      #   with ops.control_dependencies([train_op]):
      #     should_stop_op = ...
      #
      # However, this actually seems not to work on certain platforms.
      np_should_stop, np_should_log = sess.run([should_stop_op, should_log_op])

      if np_should_log:
        logging.info('global step %d: loss = %.4f (%.2f sec)',
                     np_global_step, total_loss, time_elapsed)
      if np_should_stop:
        break
  finally:
    if sv.is_chief and cleanup_op is not None:
      sess.run(cleanup_op)
  return total_loss

_USE_DEFAULT = 0

def train(
    train_op,
    logdir,
    log_every_n_steps=1,
    graph=None,
    master='',
    is_chief=True,
    global_step=None,
    number_of_steps=None,
    init_op=_USE_DEFAULT,
    init_feed_dict=None,
    init_fn=None,
    summary_op=_USE_DEFAULT,
    save_summaries_secs=600,
    startup_delay_steps=0,
    saver=None,
    save_interval_secs=600,
    sync_optimizer=None):
  """Runs a training loop using a TensorFlow supervisor.

  When the sync_optimizer is supplied, gradient updates are applied
  synchronously. Otherwise, gradient updates are applied asynchronous.

  Args:
    train_op: A `Tensor` that, when executed, will apply the gradients and
      return the loss value.
    logdir: the directory where training logs are written to.
    log_every_n_steps: The frequency, in terms of global steps, that the loss
      and global step and logged.
    graph: The graph to pass to the supervisor. If no graph is supplied the
      default graph is used.
    master: The BNS name of the tensorflow master.
    is_chief: Specifies whether or not the training is being run by the primary
      replica during replica training.
    global_step: The `Tensor` representing the global step. If left as `None`,
      then slim.variables.get_or_create_global_step() is used.
    number_of_steps: The max number of gradient steps to take during training.
      If the value is left as None, training proceeds indefinitely.
    init_op: The initialization operation.
    init_feed_dict: A feed dictionary to use when executing the `init_op`.
    init_fn: An optional callable to be executed after `init_op` is called. The
      callable must accept one argument, the session being initialized.
    summary_op: The summary operation.
    save_summaries_secs: How often, in seconds, to save summaries.
    startup_delay_steps: The number of steps to wait for before beginning. Note
      that this must be 0 if a sync_optimizer is supplied.
    saver: Saver to save checkpoints. If none, a default one will be created
      and used.
    save_interval_secs: How often, in seconds, to save the model to `logdir`.
    sync_optimizer: an instance of tf.train.SyncReplicasOptimizer. If the
      argument is supplied, gradient updates will be synchronous. If left as
      `None`, gradient updates will be asynchronous.

  Returns:
    the value of the loss function after training.

  Raises:
    ValueError: if `train_op` is empty or if `startup_delay_steps` is
      non-zero when `sync_optimizer` is supplied, or if `number_of_steps` is
      negative.
  """
  if train_op is None:
    raise ValueError('train_op cannot be None.')

  if sync_optimizer and startup_delay_steps > 0:
    raise ValueError(
        'startup_delay_steps must be zero when sync_optimizer is supplied.')

  if number_of_steps is not None and number_of_steps <= 0:
    raise ValueError(
        '`number_of_steps` must be either None or a positive number.')

  graph = graph or ops.get_default_graph()
  if global_step is None:
    global_step = variables.get_or_create_global_step()
  saver = saver or tf_saver.Saver()

  if init_op is None:
    init_op = control_flow_ops.group(
        tf_variables.initialize_all_variables(),
        tf_variables.initialize_local_variables(),
        tf_variables.initialize_all_tables())

  if summary_op == _USE_DEFAULT:
    summary_op = logging_ops.merge_all_summaries()

  local_init_op = None
  cleanup_op = None

  if is_chief and sync_optimizer:
    if not isinstance(sync_optimizer,
                      sync_replicas_optimizer.SyncReplicasOptimizer):
      raise ValueError(
          '`sync_optimizer` must be a tf.train.SyncReplicasOptimizer')

    # Need to create these BEFORE the supervisor finalizes the graph:
    local_init_op = sync_optimizer.get_init_tokens_op()
    chief_queue_runner = sync_optimizer.get_chief_queue_runner()
    cleanup_op = sync_optimizer.get_clean_up_op()

  if number_of_steps:
    should_stop_op = math_ops.greater_equal(global_step, number_of_steps)
  else:
    should_stop_op = constant_op.constant(False)

  should_log_op = math_ops.equal(
      math_ops.mod(global_step, log_every_n_steps), 0)

  sv = supervisor.Supervisor(
      graph=graph,
      is_chief=is_chief,
      logdir=logdir,
      init_op=init_op,
      init_feed_dict=init_feed_dict,
      local_init_op=local_init_op,
      summary_op=summary_op,
      global_step=global_step,
      saver=saver,
      save_summaries_secs=save_summaries_secs,
      save_model_secs=save_interval_secs,
      init_fn=init_fn)

  with sv.managed_session(master, start_standard_services=False) as sess:
    if is_chief:
      sv.start_standard_services(sess)
    elif not is_chief and startup_delay_steps > 0:
      _wait_for_step(sess, global_step,
                     min(startup_delay_steps, number_of_steps or sys.maxint))
    sv.start_queue_runners(sess)
    if is_chief and sync_optimizer:
      sv.start_queue_runners(sess, [chief_queue_runner])

    total_loss = train_loop(
        sv, sess, train_op, should_stop_op, should_log_op, global_step,
        cleanup_op)

    # This waits for service threads to finish.
    sv.Stop()

    if sv.is_chief:
      logging.info('Finished training! Saving model to disk.')
      sv.saver.save(sess, sv.save_path, global_step=sv.global_step)

    return total_loss


text:
package engine_test

import (
	"database/sql"
	"testing"

	"github.com/mlhoyt/ramsql/engine/log"

	_ "github.com/mlhoyt/ramsql/driver"
)

func TestJoinOrderBy(t *testing.T) {
	log.UseTestLogger(t)

	db, err := sql.Open("ramsql", "TestJoinOrderBy")
	if err != nil {
		t.Fatalf("sql.Open: %s", err)
	}
	defer db.Close()

	init := []string{
		`CREATE TABLE user (id BIGSERIAL, name TEXT)`,
		`CREATE TABLE address (id BIGSERIAL, user_id INT, value TEXT)`,
		`INSERT INTO user (name) VALUES ($$riri$$)`,
		`INSERT INTO user (name) VALUES ($$fifi$$)`,
		`INSERT INTO user (name) VALUES ($$loulou$$)`,
		`INSERT INTO address (user_id, value) VALUES (1, 'rue du puit')`,
		`INSERT INTO address (user_id, value) VALUES (1, 'rue du désert')`,
		`INSERT INTO address (user_id, value) VALUES (3, 'rue du chemin')`,
		`INSERT INTO address (user_id, value) VALUES (2, 'boulevard du con')`,
	}
	for _, q := range init {
		_, err := db.Exec(q)
		if err != nil {
			t.Fatalf("Cannot initialize test: %s", err)
		}
	}

	query := `SELECT user.name, address.value
			FROM user
			JOIN address ON address.user_id = user.id
			WHERE user.id = $1
			ORDER BY address.value ASC`
	rows, err := db.Query(query, 1)
	if err != nil {
		t.Fatalf("Cannot select with joined order by: %s", err)
	}
	defer rows.Close()

	n := 0
	for rows.Next() {
		n++
	}
	if n != 2 {
		t.Fatalf("Expected 2 rows, got %d", n)
	}
}

func TestJoinAs(t *testing.T) {
	log.UseTestLogger(t)

	db, err := sql.Open("ramsql", "TestJoinAs")
	if err != nil {
		t.Fatalf("sql.Open: %s", err)
	}
	defer db.Close()

	init := []string{
		`CREATE TABLE user (id BIGINT PRIMARY KEY AUTO_INCREMENT, name TEXT)`,
		`CREATE TABLE address (id BIGINT PRIMARY KEY AUTO_INCREMENT, user_id INT, value TEXT)`,
		`INSERT INTO user (name) VALUES ('foo')`,
		`INSERT INTO user (name) VALUES ('bar')`,
		`INSERT INTO address (user_id, value) VALUES (1, '123 First St')`,
		`INSERT INTO address (user_id, value) VALUES (2, '456 Second St')`,
		`INSERT INTO address (user_id, value) VALUES (2, 'Apt Alpha')`,
	}
	for _, q := range init {
		_, err := db.Exec(q)
		if err != nil {
			t.Fatalf("Cannot initialize test: %s", err)
		}
	}

	query := `SELECT user.id AS user_id, user.name AS user_name, address.value AS user_address
			FROM user
			JOIN address ON user.id = address.user_id
			WHERE user.id = ?`
	rows, err := db.Query(query, 2)
	if err != nil {
		t.Fatalf("Cannot select with joined as: %s", err)
	}
	defer rows.Close()

	n := 0
	for rows.Next() {
		var user_id int64
		var user_name string
		var user_address string

		err := rows.Scan(&user_id, &user_name, &user_address)
		if err != nil {
			t.Fatalf("Failed to scan row %d: %s", n, err)
		}

		n++
	}
	if n != 2 {
		t.Fatalf("Expected 2 rows, got %d", n)
	}
}

func TestJoinWithMixedAs(t *testing.T) {
	log.UseTestLogger(t)

	db, err := sql.Open("ramsql", "TestJoinWithMixedAs")
	if err != nil {
		t.Fatalf("sql.Open: %s", err)
	}
	defer db.Close()

	init := []string{
		`CREATE TABLE address (
       id BIGINT PRIMARY KEY AUTO_INCREMENT,
       value TEXT
     )`,
		`CREATE TABLE user (
       id BIGINT PRIMARY KEY AUTO_INCREMENT,
       name TEXT,
       address_id INT
     )`,
	}
	for _, q := range init {
		_, err := db.Exec(q)
		if err != nil {
			t.Fatalf("Cannot initialize test: %s", err)
		}
	}

	query := `SELECT
      user.id,
  		user.name,
  		address.id AS address_id,
  		address.value AS address_value
		FROM user
		INNER JOIN address
		ON user.address_id = address.id`
	// WHERE user.id = ?`
	// rows, err := db.Query(query, 2)
	rows, err := db.Query(query)
	if err != nil {
		t.Fatalf("Cannot select with joined as: %s", err)
	}
	defer rows.Close()

	rowColumns, err := rows.Columns()
	if err != nil {
		t.Fatal("failed to get rows.Columns() for comparison")
	}
	expectedRowColumns := []string{"id", "name", "address_id", "address_value"}
	if len(rowColumns) != len(expectedRowColumns) {
		t.Fatalf("expected %d row columns, got %d", len(expectedRowColumns), len(rowColumns))
	}
	for i := range expectedRowColumns {
		if expectedRowColumns[i] != rowColumns[i] {
			t.Errorf("at position %d expected column name %q, got %q", i, expectedRowColumns[i], rowColumns[i])
		}
	}
}

func TestMultipleJoin(t *testing.T) {
	log.UseTestLogger(t)

	db, err := sql.Open("ramsql", "TestMultipleJoin")
	if err != nil {
		t.Fatalf("sql.Open: %s", err)
	}
	defer db.Close()

	init := []string{
		`CREATE TABLE user (id BIGSERIAL, name TEXT)`,
		`CREATE TABLE address (id BIGSERIAL, user_id INT, value TEXT)`,
		`CREATE TABLE user_group (id BIGSERIAL, user_id INT, name TEXT)`,
		`INSERT INTO user (name) VALUES ($$riri$$)`,
		`INSERT INTO user (name) VALUES ($$fifi$$)`,
		`INSERT INTO user (name) VALUES ($$loulou$$)`,
		`INSERT INTO address (user_id, value) VALUES (1, 'rue du puit')`,
		`INSERT INTO address (user_id, value) VALUES (1, 'rue du désert')`,
		`INSERT INTO address (user_id, value) VALUES (3, 'rue du chemin')`,
		`INSERT INTO address (user_id, value) VALUES (2, 'boulevard du con')`,
		`INSERT INTO user_group (user_id, name) VALUES (1, 'toto')`,
		`INSERT INTO user_group (user_id, name) VALUES (2, 'toto')`,
		`INSERT INTO user_group (user_id, name) VALUES (3, 'lonely')`,
		`INSERT INTO user_group (user_id, name) VALUES (1, 'cowboy')`,
	}
	for _, q := range init {
		_, err := db.Exec(q)
		if err != nil {
			t.Fatalf("Cannot initialize test: %s", err)
		}
	}

	query := `SELECT user.name, address.value
			FROM user
			JOIN address ON address.user_id = user.id
			JOIN user_group ON user_group.user_id = address.user_id
			WHERE user_group.name = $1
			ORDER BY address.value ASC`
	rows, err := db.Query(query, "toto")
	if err != nil {
		t.Fatalf("Cannot select with 3 tables joined: %s", err)
	}
	defer rows.Close()

	n := 0
	for rows.Next() {
		n++
	}
	if n != 3 {
		t.Fatalf("Expected 3 rows, got %d", n)
	}

}

func TestJoinGroup(t *testing.T) {
	log.UseTestLogger(t)

	db, err := sql.Open("ramsql", "TestJoinGroup")
	if err != nil {
		t.Fatalf("sql.Open: %s", err)
	}
	defer db.Close()

	init := []string{
		`CREATE TABLE user (id BIGSERIAL, name TEXT)`,
		`CREATE TABLE group (id BIGSERIAL, name TEXT)`,
		`CREATE TABLE user_group (id BIGSERIAL, user_id INT, group_id INT)`,
		`INSERT INTO user (name) VALUES ($$riri$$)`,
		`INSERT INTO user (name) VALUES ($$fifi$$)`,
		`INSERT INTO user (name) VALUES ($$loulou$$)`,
		`INSERT INTO group (name) VALUES ('cowboys')`,
		`INSERT INTO group (name) VALUES ('troopers')`,
		`INSERT INTO group (name) VALUES ('toys')`,
		`INSERT INTO user_group (user_id, group_id) VALUES (1, 1)`,
		`INSERT INTO user_group (user_id, group_id) VALUES (2, 1)`,
	}
	for _, q := range init {
		_, err := db.Exec(q)
		if err != nil {
			t.Fatalf("Cannot initialize test: %s", err)
		}
	}

	query := `SELECT user.name FROM user
			JOIN user_group ON user.id = user_group.user_id
			WHERE user_group.group_id = $1
			ORDER BY user.name ASC`
	rows, err := db.Query(query, 1)
	if err != nil {
		t.Fatalf("Cannot select joined: %s", err)
	}
	defer rows.Close()

	n := 0
	for rows.Next() {
		n++
	}
	if n != 2 {
		t.Fatalf("Expected 2 rows, got %d", n)
	}

}


text:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

	<jdbc:embedded-database id="myDataSource" type="HSQL" />

	<bean id="myEmf"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="myDataSource" />
		<property name="packagesToScan" value="cz.jiripinkas.example.mailer.entity" />
		<property name="jpaVendorAdapter">
			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
				<property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
			</bean>
		</property>
		<property name="jpaProperties">
			<props>
				<prop key="hibernate.hbm2ddl.auto">create</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
	</bean>

	<tx:annotation-driven />

	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="myEmf" />
		<property name="dataSource" ref="myDataSource" />
	</bean>

</beans>


text:
package archive // import "github.com/buildpacks/pack/pkg/archive"

import (
	"archive/tar"
	"archive/zip"
	"io"
	"io/ioutil"
	"os"
	"path"
	"path/filepath"
	"time"

	"github.com/docker/docker/pkg/ioutils"
	"github.com/pkg/errors"
)

var NormalizedDateTime time.Time

func init() {
	NormalizedDateTime = time.Date(1980, time.January, 1, 0, 0, 1, 0, time.UTC)
}

type TarWriter interface {
	WriteHeader(hdr *tar.Header) error
	Write(b []byte) (int, error)
	Close() error
}

type TarWriterFactory interface {
	NewWriter(io.Writer) TarWriter
}

type defaultTarWriterFactory struct{}

func DefaultTarWriterFactory() TarWriterFactory {
	return defaultTarWriterFactory{}
}

func (defaultTarWriterFactory) NewWriter(w io.Writer) TarWriter {
	return tar.NewWriter(w)
}

func ReadDirAsTar(srcDir, basePath string, uid, gid int, mode int64, normalizeModTime, includeRoot bool, fileFilter func(string) bool) io.ReadCloser {
	return GenerateTar(func(tw TarWriter) error {
		return WriteDirToTar(tw, srcDir, basePath, uid, gid, mode, normalizeModTime, includeRoot, fileFilter)
	})
}

func ReadZipAsTar(srcPath, basePath string, uid, gid int, mode int64, normalizeModTime bool, fileFilter func(string) bool) io.ReadCloser {
	return GenerateTar(func(tw TarWriter) error {
		return WriteZipToTar(tw, srcPath, basePath, uid, gid, mode, normalizeModTime, fileFilter)
	})
}

func GenerateTar(genFn func(TarWriter) error) io.ReadCloser {
	return GenerateTarWithWriter(genFn, DefaultTarWriterFactory())
}

// GenerateTarWithTar returns a reader to a tar from a generator function using a writer from the provided factory.
// Note that the generator will not fully execute until the reader is fully read from. Any errors returned by the
// generator will be returned when reading the reader.
func GenerateTarWithWriter(genFn func(TarWriter) error, twf TarWriterFactory) io.ReadCloser {
	errChan := make(chan error)
	pr, pw := io.Pipe()

	go func() {
		tw := twf.NewWriter(pw)
		defer func() {
			if r := recover(); r != nil {
				tw.Close()
				pw.CloseWithError(errors.Errorf("panic: %v", r))
			}
		}()

		err := genFn(tw)

		closeErr := tw.Close()
		closeErr = aggregateError(closeErr, pw.CloseWithError(err))

		errChan <- closeErr
	}()

	closed := false
	return ioutils.NewReadCloserWrapper(pr, func() error {
		if closed {
			return errors.New("reader already closed")
		}

		var completeErr error

		// closing the reader ensures that if anything attempts
		// further reading it doesn't block waiting for content
		if err := pr.Close(); err != nil {
			completeErr = aggregateError(completeErr, err)
		}

		// wait until everything closes properly
		if err := <-errChan; err != nil {
			completeErr = aggregateError(completeErr, err)
		}

		closed = true
		return completeErr
	})
}

func aggregateError(base, addition error) error {
	if addition == nil {
		return base
	}

	if base == nil {
		return addition
	}

	return errors.Wrap(addition, base.Error())
}

func CreateSingleFileTarReader(path, txt string) io.ReadCloser {
	tarBuilder := TarBuilder{}
	tarBuilder.AddFile(path, 0644, NormalizedDateTime, []byte(txt))
	return tarBuilder.Reader(DefaultTarWriterFactory())
}

func CreateSingleFileTar(tarFile, path, txt string) error {
	tarBuilder := TarBuilder{}
	tarBuilder.AddFile(path, 0644, NormalizedDateTime, []byte(txt))
	return tarBuilder.WriteToPath(tarFile, DefaultTarWriterFactory())
}

// ErrEntryNotExist is an error returned if an entry path doesn't exist
var ErrEntryNotExist = errors.New("not exist")

// IsEntryNotExist detects whether a given error is of type ErrEntryNotExist
func IsEntryNotExist(err error) bool {
	return err == ErrEntryNotExist || errors.Cause(err) == ErrEntryNotExist
}

// ReadTarEntry reads and returns a tar file
func ReadTarEntry(rc io.Reader, entryPath string) (*tar.Header, []byte, error) {
	tr := tar.NewReader(rc)
	for {
		header, err := tr.Next()
		if err == io.EOF {
			break
		}
		if err != nil {
			return nil, nil, errors.Wrap(err, "failed to get next tar entry")
		}

		if path.Clean(header.Name) == entryPath {
			buf, err := ioutil.ReadAll(tr)
			if err != nil {
				return nil, nil, errors.Wrapf(err, "failed to read contents of '%s'", entryPath)
			}

			return header, buf, nil
		}
	}

	return nil, nil, errors.Wrapf(ErrEntryNotExist, "could not find entry path '%s'", entryPath)
}

// WriteDirToTar writes the contents of a directory to a tar writer. `basePath` is the "location" in the tar the
// contents will be placed. The includeRoot param sets the permissions and metadata on the root file.
func WriteDirToTar(tw TarWriter, srcDir, basePath string, uid, gid int, mode int64, normalizeModTime, includeRoot bool, fileFilter func(string) bool) error {
	if includeRoot {
		rootHeader := &tar.Header{
			Typeflag: tar.TypeDir,
			Name:     basePath,
			Mode:     mode,
		}
		finalizeHeader(rootHeader, uid, gid, mode, normalizeModTime)
		if err := tw.WriteHeader(rootHeader); err != nil {
			return err
		}
	}

	return filepath.Walk(srcDir, func(file string, fi os.FileInfo, err error) error {
		var relPath string
		if fileFilter != nil {
			relPath, err = filepath.Rel(srcDir, file)
			if err != nil {
				return err
			}
			if !fileFilter(relPath) {
				return nil
			}
		}
		if err != nil {
			return err
		}

		if fi.Mode()&os.ModeSocket != 0 {
			return nil
		}

		var header *tar.Header
		if fi.Mode()&os.ModeSymlink != 0 {
			target, err := os.Readlink(file)
			if err != nil {
				return err
			}

			// Ensure that symlinks have Linux link names, independent of source OS
			header, err = tar.FileInfoHeader(fi, filepath.ToSlash(target))
			if err != nil {
				return err
			}
		} else {
			header, err = tar.FileInfoHeader(fi, fi.Name())
			if err != nil {
				return err
			}
		}

		if relPath == "" {
			relPath, err = filepath.Rel(srcDir, file)
			if err != nil {
				return err
			}
		}
		if relPath == "." {
			return nil
		}

		header.Name = filepath.ToSlash(filepath.Join(basePath, relPath))
		finalizeHeader(header, uid, gid, mode, normalizeModTime)

		if err := tw.WriteHeader(header); err != nil {
			return err
		}

		if fi.Mode().IsRegular() {
			f, err := os.Open(filepath.Clean(file))
			if err != nil {
				return err
			}
			defer f.Close()

			if _, err := io.Copy(tw, f); err != nil {
				return err
			}
		}

		return nil
	})
}

// WriteZipToTar writes the contents of a zip file to a tar writer.
func WriteZipToTar(tw TarWriter, srcZip, basePath string, uid, gid int, mode int64, normalizeModTime bool, fileFilter func(string) bool) error {
	zipReader, err := zip.OpenReader(srcZip)
	if err != nil {
		return err
	}
	defer zipReader.Close()

	var fileMode int64
	for _, f := range zipReader.File {
		if fileFilter != nil && !fileFilter(f.Name) {
			continue
		}

		fileMode = mode
		if isFatFile(f.FileHeader) {
			fileMode = 0777
		}

		var header *tar.Header
		if f.Mode()&os.ModeSymlink != 0 {
			target, err := func() (string, error) {
				r, err := f.Open()
				if err != nil {
					return "", nil
				}
				defer r.Close()

				// contents is the target of the symlink
				target, err := ioutil.ReadAll(r)
				if err != nil {
					return "", err
				}

				return string(target), nil
			}()

			if err != nil {
				return err
			}

			header, err = tar.FileInfoHeader(f.FileInfo(), target)
			if err != nil {
				return err
			}
		} else {
			header, err = tar.FileInfoHeader(f.FileInfo(), f.Name)
			if err != nil {
				return err
			}
		}

		header.Name = filepath.ToSlash(filepath.Join(basePath, f.Name))
		finalizeHeader(header, uid, gid, fileMode, normalizeModTime)

		if err := tw.WriteHeader(header); err != nil {
			return err
		}

		if f.Mode().IsRegular() {
			err := func() error {
				fi, err := f.Open()
				if err != nil {
					return err
				}
				defer fi.Close()

				_, err = io.Copy(tw, fi)
				return err
			}()

			if err != nil {
				return err
			}
		}
	}

	return nil
}

func isFatFile(header zip.FileHeader) bool {
	var (
		creatorFAT  uint16 = 0 // nolint:revive
		creatorVFAT uint16 = 14
	)

	// This identifies FAT files, based on the `zip` source: https://golang.org/src/archive/zip/struct.go
	firstByte := header.CreatorVersion >> 8
	return firstByte == creatorFAT || firstByte == creatorVFAT
}

func finalizeHeader(header *tar.Header, uid, gid int, mode int64, normalizeModTime bool) {
	NormalizeHeader(header, normalizeModTime)
	if mode != -1 {
		header.Mode = mode
	}
	header.Uid = uid
	header.Gid = gid
}

// NormalizeHeader normalizes a tar.Header
//
// Normalizes the following:
// 	- ModTime
// 	- GID
// 	- UID
// 	- User Name
// 	- Group Name
func NormalizeHeader(header *tar.Header, normalizeModTime bool) {
	if normalizeModTime {
		header.ModTime = NormalizedDateTime
	}
	header.Uid = 0
	header.Gid = 0
	header.Uname = ""
	header.Gname = ""
}

// IsZip detects whether or not a File is a zip directory
func IsZip(path string) (bool, error) {
	r, err := zip.OpenReader(path)

	switch {
	case err == nil:
		r.Close()
		return true, nil
	case err == zip.ErrFormat:
		return false, nil
	default:
		return false, err
	}
}