text:
import {Document} from '../../lib/Document';
import Collection, { _resetStartup } from '../../lib/Collection';
import StorageManager from '../../lib/StorageManager';
import CursorObservable from '../../lib/CursorObservable';
import CollectionDelegate from '../../lib/CollectionDelegate';
import chai, {expect} from 'chai';
import sinon from 'sinon';
chai.use(require('chai-as-promised'));
chai.use(require('sinon-chai'));
chai.should();

describe('Collection', () => {

  const _defaultIdGenerator = Collection.defaultIdGenerator();
  const _defaultDelegate = Collection.defaultDelegate();
  const _defaultStorageManager = Collection.defaultStorageManager();
  const _defaultIndexManager = Collection.defaultIndexManager();
  const _defaultCursor = Collection.defaultCursor();
  beforeEach(function () {
    _resetStartup(10);
    Collection.defaultCursor(_defaultCursor);
    Collection.defaultDelegate(_defaultDelegate);
    Collection.defaultStorageManager(_defaultStorageManager);
    Collection.defaultIndexManager(_defaultIndexManager);
    Collection.defaultIdGenerator(_defaultIdGenerator);
  });

  describe('#constructor', function () {
    it('should override defaults', function () {
      class NewStorageManager {}
      function new_id_generator() {}
      class NewIndexManager {}
      class NewDelegate {}

      const db = new Collection('test', {
        storageManager: NewStorageManager,
        idGenerator: new_id_generator,
        indexManager: NewIndexManager,
        delegate: NewDelegate,
      });

      db._lazyInitCollection();
      db.storageManager.should.be.an.instanceof(NewStorageManager);
      db.idGenerator.should.be.equal(new_id_generator);
      db.indexManager.should.be.an.instanceof(NewIndexManager);
      db.delegate.should.be.an.instanceof(NewDelegate);
    });
  });

  describe('#create', function () {
    it('should return the same object if it is an Object', function () {
      const db = new Collection('test');
      const raw = {a: 2, b: 3};
      const doc = db.create(raw);
      doc.should.be.equal(raw);
    });

    it('should make an object from string', function () {
      const db = new Collection('test');
      const doc = db.create('{"a": 2, "b": 3}');
      doc.a.should.be.equal(2);
      doc.b.should.be.equal(3);
    });
  });

  describe('#defaultCursor', function () {
    it('should set default cursor', function () {
      class NewCursor {}
      Collection.defaultCursor(NewCursor);
      Collection.defaultCursor().should.be.equal(NewCursor);
    });
    it('should initialize lazy', function () {
      const defColl = new Collection('test');
      expect(defColl.cursorClass).to.be.undefined;
      class NewCursor {}
      Collection.defaultCursor(NewCursor);
      defColl._lazyInitCollection();
      defColl.cursorClass.should.be.equal(NewCursor);
    });
  });

  describe('#defaultStorageManager', function () {
    it('should set default storage manager', function () {
      class NewStorageManager {}
      Collection.defaultStorageManager(NewStorageManager);
      Collection.defaultStorageManager().should.be.equal(NewStorageManager);
    });
    it('should upgrade all collections uses defaults', function () {
      const defColl = new Collection('test');
      expect(defColl.storageManager).to.be.undefined;
      class NewStorageManager {}
      Collection.defaultStorageManager(NewStorageManager);
      defColl.storage.should.be.instanceof(NewStorageManager);
    });
  });

  describe('#defaultIdGenerator', function () {
    it('should set default id generator', function () {
      function new_id_generator() {}
      Collection.defaultIdGenerator(new_id_generator);
      Collection.defaultIdGenerator().should.be.equal(new_id_generator);
    });
    it('should upgrade all collections uses defaults', function () {
      const defColl = new Collection('test');
      function new_id_generator() {}
      Collection.defaultIdGenerator(new_id_generator);
      defColl._lazyInitCollection();
      defColl.idGenerator.should.be.equal(new_id_generator);
    });
  });

  describe('#defaultDelegate', function () {
    it('should set default Delegate class', function () {
      class NewDelegate {}
      Collection.defaultDelegate(NewDelegate);
      Collection.defaultDelegate().should.be.equal(NewDelegate);
    });
    it('should upgrade all collections uses defaults', function () {
      const defColl = new Collection('test');
      class NewDelegate {}
      Collection.defaultDelegate(NewDelegate);
      defColl._lazyInitCollection();
      defColl.delegate.should.be.instanceof(NewDelegate);
    });
  });

  describe('#defaultIndexManager', function () {
    it('should set default index manager class', function () {
      class NewIndexManager {}
      Collection.defaultIndexManager(NewIndexManager);
      Collection.defaultIndexManager().should.be.equal(NewIndexManager);
    });
    it('should upgrade all collections uses defaults', function () {
      const defColl = new Collection('test');
      class NewIndexManager {}
      Collection.defaultIndexManager(NewIndexManager);
      defColl._lazyInitCollection();
      defColl.indexManager.should.be.instanceof(NewIndexManager);
    });
  });

  describe('#modelName', function () {
    it('should return name of the model', function () {
      const db = new Collection('test');
      db.modelName.should.be.equal('test');
    });
  });

  describe('#indexes', function () {
    it('should return indexes object', function () {
      const db = new Collection('test');
      db.indexes.should.have.ownProperty('_id');
    });
  });

  describe('#storage', function () {
    it('should return storage', function () {
      const db = new Collection('test');
      db.storage.should.not.equals(undefined);
    });
  });

  describe('#create', function () {
    it('should create new document by raw', function () {
      const db = new Collection('test');
      const doc = db.create({a: 1, b: 2, c: {d: {e: 3}}});
      doc.c.d.e.should.equals(3);
      doc.a.should.equals(1);
      doc.b.should.equals(2);
    });
  });

  describe('#insert', function () {
    it('should insert document and return new document id', function () {
      const db = new Collection('test');
      return db.insert({test: 'passed'}).then((docId) => {
        return db.findOne({_id: docId});
      }).then((doc) => {
        doc.test.should.to.be.equals('passed');
      });
    });

    it('should emit events', function (done) {
      const db = new Collection('test');

      var syncEmited = false;
      db.on('insert', () => {
        syncEmited.should.be.equals(true);
        done();
      })
      db.on('sync:insert', (doc, randomId) => {
        doc.should.to.have.ownProperty('test');
        doc.should.to.have.ownProperty('_id');
        syncEmited = true;
      })
      db.insert({test: 'passed'});
    });

    it('should be quiet for syncing', function (done) {
      const db = new Collection('test');
      db.on('insert', () => {
        done();
      })
      db.on('sync:insert', (doc, randomId) => {
        throw new Error();
      })
      db.insert({test: 'passed'}, {quiet: true});
    });

    it('should be quiet if options.quiet passed', function () {
      const db = new Collection('test');
      const cb = sinon.spy();
      db.on('sync:insert', cb);
      db.insert({a: 1}, {quiet: true});
      cb.should.have.callCount(0);
    });
  });

  describe('#insertAll', function () {
    it('should insert all documents from given array', function () {
      const db = new Collection('test');
      return db.insertAll([{a: 1}, {a: 2}, {a: 3}]).then((docIds) => {
        docIds.should.have.length(3);
        return db.find({_id: {$in: docIds}}).sort({a: 1}).exec();
      }).then((docs) => {
        docs.should.have.length(3);
        docs[0].a.should.be.equals(1);
        docs[1].a.should.be.equals(2);
        docs[2].a.should.be.equals(3);
      });
    });
  });

  describe('#remove', function () {
    it('should remove multiple documents by query', function () {
      const db = new Collection('test');
      return db.insertAll([{a: 1}, {a: 2}, {a: 3}]).then((docIds) => {
        return db.remove({a: {$in: [1, 3]}}, {multi: true});
      }).then((docs) => {
        expect(docs).to.be.an('array');
        docs.should.have.length(2);
        return db.ids();
      }).then((ids) => {
        expect(ids).to.be.an('array');
        ids.should.have.length(1);
        return db.find({_id: {$in: ids}}).exec();
      }).then((docs) => {
        expect(docs).to.be.an('array');
        docs.should.have.length(1);
        docs[0].a.should.to.be.equals(2);
      });
    });

    it('should remove only first document if multi not specified', function () {
      const db = new Collection('test');
      return db.insertAll([{a: 1}, {a: 2}, {a: 3}]).then((docIds) => {
        return db.remove({a: {$in: [3, 1]}});
      }).then((res) => {
        res.should.have.length(1);
        return db.find().then((docs) => {
          docs.should.have.length(2);
          docs[0].a.should.not.be.equal(res[0].a);
          docs[1].a.should.not.be.equal(res[0].a);
        })
      });
    });

    it('should emit events "remove" and "sync:remove"', function (done) {
      const db = new Collection('test');

      var removeEvents = 0;
      var syncEmited = false;
      db.on('remove', () => {
        syncEmited.should.be.equals(true);
        removeEvents += 1;
        if (removeEvents === 2) {
          done();
        }
      })
      db.on('sync:remove', (query, options) => {
        query.should.to.be.deep.equals({a: {$in: [1, 3]}});
        options.should.to.be.deep.equals({multi: true});
        syncEmited = true;
      })
      db.insertAll([{a: 1}, {a: 2}, {a: 3}]).then((docIds) => {
        db.remove({a: {$in: [1, 3]}}, {multi: true});
      });
    });

    it('should deindex a doucmnet', function () {
      const db = new Collection('test');
      return db.insertAll([{a: 1}, {a: 2}, {a: 3}]).then(() => {
        return db.remove({a: {$in: [1, 3]}}, {multi: true});
      }).then((removedDocs) => {
        //db.indexes.a.getAll().should.have.length(1);
        expect(removedDocs).to.be.an('array');
        removedDocs.should.have.length(2);
      });
    });

    it('should be quiet if options.quiet passed', function () {
      const db = new Collection('test');
      const cb = sinon.spy();
      db.on('sync:remove', cb);
      db.remove({}, {quiet: true, multi: true});
      cb.should.have.callCount(0);
    });

    it('should remove by primitive id type', function () {
      const db = new Collection('test');
      return db.insertAll([{a: 1, _id: '1'}, {a: 2, _id: 2}])
      .then(() => {
        return db.remove('1');
      }).then((removedDocs) => {
        removedDocs.should.have.length(1);
        removedDocs[0].should.be.deep.equal({a: 1, _id: '1'});
        return db.remove(2);
      }).then((removedDocs) => {
        removedDocs.should.have.length(1);
        removedDocs[0].should.be.deep.equal({a: 2, _id: 2});
      });
    });
  });

  describe('#update', function () {
    it('should update a document', function () {
      const db = new Collection('test');
      return db.insertAll([{a: 1}, {a: 2}, {a: 3}]).then(() => {
        return db.update({a: 1}, {$set: {a: 4}});
      }).then((result) => {
        result.modified.should.be.equals(1);
        result.updated[0].a.should.be.equals(4);
        return db.find({a: 4});
      }).then((result) => {
        expect(result).to.be.an('array');
        result.should.have.length(1);
        result[0].a.should.be.equals(4);
      });
    });

    it('should emit events and can be quiet', function () {
      const db = new Collection('test');
      const cb = sinon.spy();
      db.on('sync:update', cb);
      return db.update({}, {$set: {a: 1}}, {}).then(() => {
        cb.should.have.callCount(1);
      });
    });

    it('should be quiet if options.quiet passed', function () {
      const db = new Collection('test');
      const cb = sinon.spy();
      db.on('sync:update', cb);
      return db.update({}, {$set: {a: 1}}, {quiet: true}).then(() => {
        cb.should.have.callCount(0);
      });
    });

    it('should update index of a doucmnet', function () {
      // TODO
    });

    it('should update by primitive id type', function () {
      const db = new Collection('test');
      return db.insertAll([{a: 1, _id: '1'}, {a: 2, _id: 2}])
      .then(() => {
        return db.update('1', {$set: {a: 3}});
      }).then((res) => {
        res.updated.should.have.length(1);
        res.updated[0].should.be.deep.equal({a: 3, _id: '1'});
        return db.update(2, {$set: {a: 4}});
      }).then((res) => {
        res.updated.should.have.length(1);
        res.updated[0].should.be.deep.equal({a: 4, _id: 2});
      });
    });
  });

  describe('#update - upsert', function () {
    const db = new Collection('test');
    const testUpsert = (query, mod, expected, expectedId) => {
      it(`should upsert with ${JSON.stringify(query)} ${JSON.stringify(mod)},\n\textected ${JSON.stringify(expected)}`, function() {
        return db.remove({}, {multi: true}).then(() => {
          return db.update(query, mod, {upsert: true}).then((res) => {
            res.modified.should.be.equals(1);
            res.original.should.be.deep.equals([null]);
            res.updated.should.have.length(1);
            if (expectedId) {
              res.updated[0]._id.should.be.equal(expectedId);
            } else {
              expect(res.updated[0]._id).to.have.length(17);
            }
            delete res.updated[0]._id;
            res.updated[0].should.be.deep.equals(expected);
          });
        });
      });
    };

    testUpsert({a: 2}, {$set: {b: 3}}, {a: 2, b: 3})
    testUpsert({'a.b.c': 2}, {$set: {b: 3}}, {a: {b: {c: 2}}, b: 3})
    testUpsert({'a.b.c': 2, _id: {$in: [1,2,3]}}, {$set: {b: 3}}, {a: {b: {c: 2}}, b: 3})
    testUpsert({'a.b.c': 2, _id: 123}, {$set: {b: 3}}, {a: {b: {c: 2}}, b: 3}, 123)
    testUpsert({a: 2}, {b: 3}, {b: 3})
    testUpsert({a: 2}, {$unset: {a: 1}}, {})
    testUpsert({a: 2, _id: '123'}, {$setOnInsert: {a: 1, _id: '123'}}, {a: 1}, '123');
    testUpsert(null, {b: 3}, {b: 3});
    testUpsert('test', {b: 3}, {b: 3}, 'test');
    testUpsert({_id: null}, {b: 3}, {b: 3});
    testUpsert({_id: 'test'}, {b: 3}, {b: 3}, 'test');
  });

  describe('#findOne', function () {
    it('should find only one document', function () {
      const db = new Collection('test');
      return Promise.all([
        db.insert({a: 1}),
        db.insert({a: 2}),
        db.insert({a: 3}),
      ]).then((docs) => {
        return db.findOne({a: 2}, {}, {});
      }).then((doc) => {
        expect(doc).to.be.an('object');
        doc.a.should.be.equals(2);
      });
    });

    it('should return undefined if document not found', function () {
      const db = new Collection('test');
      return Promise.all([
        db.insert({a: 1}),
        db.insert({a: 2}),
        db.insert({a: 3}),
      ]).then((docs) => {
        return db.findOne({a: 5});
      }).then((doc) => {
        expect(doc).to.be.an('undefined');
      });
    });

    it('should find one by primitive id type', function () {
      const db = new Collection('test');
      return db.insertAll([{a: 1, _id: '1'}, {a: 2, _id: 2}])
      .then(() => {
        return db.findOne('1');
      }).then((res) => {
        res.should.be.deep.equal({a: 1, _id: '1'});
        return db.findOne(2);
      }).then((res) => {
        res.should.be.deep.equal({a: 2, _id: 2});
      });
    });
  });

  describe('#count', function () {
    it('should return count of documents by query', function () {
      const db = new Collection('test');
      return Promise.all([
        db.insert({a: 1}),
        db.insert({a: 2}),
        db.insert({a: 3}),
      ]).then((docs) => {
        return db.count({a: {$in: [1, 3]}}, {});
      }).then((num) => {
        num.should.be.equals(2);
      });
    });

    it('should return zero if no documents found', function () {
      const db = new Collection('test');
      return Promise.all([
        db.insert({a: 1}),
        db.insert({a: 2}),
        db.insert({a: 3}),
      ]).then((docs) => {
        return db.count({a: 5});
      }).then((num) => {
        num.should.be.equals(0);
      });
    });

    it('should count by primitive id type', function () {
      const db = new Collection('test');
      return db.insertAll([{a: 1, _id: '1'}, {a: 2, _id: 2}])
      .then(() => {
        return db.count('1');
      }).then((res) => {
        res.should.be.deep.equal(1);
        return db.count(2);
      }).then((res) => {
        res.should.be.deep.equal(1);
        return db.count('2');
      }).then((res) => {
        res.should.be.deep.equal(0);
        return db.count(1);
      }).then((res) => {
        res.should.be.deep.equal(0);
      });
    });
  });

  describe('#ids', function () {
    it('should return list of ids by given query', function () {
      const db = new Collection('test');
      return Promise.all([
        db.insert({a: 1}),
        db.insert({a: 2}),
        db.insert({a: 3}),
      ]).then((docs) => {
        return db.ids({a: {$in: [1, 3]}}, {});
      }).then((ids) => {
        expect(ids).to.be.an('array');
        ids.should.have.length(2);
      });
    });

    it('should return ids by primitive id type', function () {
      const db = new Collection('test');
      return Promise.all([
        db.insert({a: 1, _id: 1}),
        db.insert({a: 2, _id: 2}),
        db.insert({a: 3, _id: 3}),
      ]).then((docs) => {
        return db.ids(1);
      }).then((ids) => {
        expect(ids).to.be.an('array');
        ids.should.have.length(1);
        ids[0].should.be.equal(1);
        return db.ids('1');
      }).then((ids) => {
        expect(ids).to.be.an('array');
        ids.should.have.length(0);
      });
    });
  });
});


text:
def _update_config(cfg, args, task):
    if args.get('--ace-bin') is not None:
        cfg['ace-bin'] = args.get('--ace-bin')
    if args.get('-g') is not None:
        cfg['grammar'] = args.get('-g')
    if args.get('-n') is not None:
        cfg['num-results'] = args.get('-n')
    if args.get('--timeout') is not None:
        cfg['timeout'] = args.get('--timeout')
    if task == 'parse':
        if args.get('--max-chart-megabytes') is not None:
            cfg['max-chart-megabytes'] = args.get('--max-chart-megabytes')
        if args.get('--max-unpack-megabytes') is not None:
            cfg['max-unpack-megabytes'] = args.get('--max-unpack-megabytes')
        if args.get('-y') is not None:
            cfg['yy-mode'] = 'yes' if args.get('-y') else 'no'
    if task == 'generate':
        cfg['only-subsuming'] = 'yes' if args.get('--only-subsuming') else 'no'

redpajama_set_name: RedPajamaGithub


text:
package net.xenqtt.application;

import net.xenqtt.AppContext;
import net.xenqtt.XenqttUtil;
import net.xenqtt.mockbroker.MockBroker;

/**
 * Runs the {@link MockBroker} as a xenqtt command line application
 */
public final class MockBrokerApplication extends AbstractXenqttApplication {

	private MockBroker broker;

	/**
	 * @see net.xenqtt.application.XenqttApplication#start(net.xenqtt.AppContext)
	 */
	@Override
	public void start(AppContext arguments) {

		int timeout = arguments.getArgAsInt("t", 15);
		int port = arguments.getArgAsInt("p", 1883);
		int maxInFlightMessages = arguments.getArgAsInt("m", 50);
		boolean allowAnonymousAccess = arguments.isFlagSpecified("a");
		boolean ignoreCredentials = arguments.isFlagSpecified("i");

		broker = new MockBroker(null, timeout, port, allowAnonymousAccess, ignoreCredentials, false, maxInFlightMessages);

		System.out.println(arguments.isFlagSpecified("-u"));
		String credentials = arguments.getArgAsString("u", "");
		for (String creds : XenqttUtil.quickSplit(credentials, ',')) {
			String[] userpass = XenqttUtil.quickSplit(creds, ':');
			if (userpass.length != 2) {
				throw new IllegalArgumentException("Credentials could not be parsed: " + credentials);
			}
			broker.addCredentials(userpass[0], userpass[1]);
		}

		broker.init();
	}

	/**
	 * @see net.xenqtt.application.XenqttApplication#stop()
	 */
	@Override
	public void stop() {

		broker.shutdown(15000);
	}

	/**
	 * @see net.xenqtt.application.XenqttApplication#getOptsText()
	 */
	@Override
	public String getOptsText() {
		return "[-t timeout] [-p port] [-a] [-i] [-u user1:pass1,...usern:passn]";
	}

	/**
	 * @see net.xenqtt.application.XenqttApplication#getOptsUsageText()
	 */
	@Override
	public String getOptsUsageText() {
		return "\n\t-t timeout : Seconds to wait for an ack to a message with QoS > 0. Defaults to 15." //
				+ "\n\t-p port : Port to listen on. Defaults to 1883." //
				+ "\n\t-a : Allow anonymous access. Allows clients to connect with no credentials. This does not ignore" //
				+ "\n       credentials that are specified in the connect message. Specify -i for that feature." //
				+ "\n\t-i : Ignore credentials. Allows clients to connect with any credentials. This does not allow " //
				+ "\n       access when the connect message contains no credentials. Specify -a for that feature." //
				+ "\n\t-m : Max in-flight messages to a client. Defaults to 50." //
				+ "\n\t-u user:pass... : Credentials (usernames and passwords) a client can use to connet." //
		;
	}

	/**
	 * @see net.xenqtt.application.XenqttApplication#getSummary()
	 */
	@Override
	public String getSummary() {
		return "Run a mock MQTT broker. Useful in testing and debugging.";
	}

	/**
	 * @see net.xenqtt.application.XenqttApplication#getDescription()
	 */
	@Override
	public String getDescription() {
		return "The mock broker is an MQTT broker that facilitates communication between disparate clients. " //
				+ "The mock broker supports the MQTT protocol out-of-the-box (save for QoS 2 which is not presently supported). " //
				+ "In addition to full protocol support the mock broker also provides hooks for users to customize the behavior "//
				+ "of various operations. These include publishing, subscribing, and so forth.";
	}
}


text:
package collene;

import freedb.FreeDbEntry;
import freedb.FreeDbReader;
import com.google.common.base.Charsets;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Callable;

public class Freedb {

    private static final int MAX_ENTRIES = Integer.MAX_VALUE;
    private static final int BATCH_SIZE = 10;
    private static final boolean VERBOSE = false;
    private static PrintStream out = System.out;
    private static final String name = "index_freedb_cass_test";
    private static final String metaName = "meta_freedb_cass_test";

    public static void main(String args[]) throws Exception {
        //dumpGenres(args);
        //System.exit(0);

        if (args.length == 0) {
            System.out.println("Need to specify: { memory | file | cassandra }");
            System.exit(-1);
        }

        Callable<Directory> directoryBuilder = buildDirectory(args[0]);

        try {
            BuildIndex(directoryBuilder.call());
        } catch (LockObtainFailedException ex) {
            ex.printStackTrace(out);
            System.exit(-1);
        }
        out.println("\nWill now do an independent search\n");
        DoSearch(directoryBuilder.call());
        out.println("\nWill now do an independent search\n");
        DoSearch(directoryBuilder.call());
        out.println("\nWill now do an independent search\n");
        DoSearch(directoryBuilder.call());

        System.exit(0);
    }

    public static void dumpGenres(String args[]) throws Exception {
        out.println("dumping genres for fun");
        String freedbPath = "/Users/gdusbabek/Downloads/freedb-complete-20140701.tar.bz2";
        FreeDbReader reader = new FreeDbReader(new File(freedbPath), 50000);
        reader.start();
        Set<String> genres = new HashSet<String>();
        FreeDbEntry entry = reader.next();
        while (entry != null) {
            if (entry.getGenre() != null && entry.getGenre().length() > 0)
                genres.add(entry.getGenre().toLowerCase());
            entry = reader.next();
        }
        OutputStream out = new FileOutputStream("/tmp/genres.txt");
        for (String genre : genres) {
            try {
                out.write(genre.getBytes(Charsets.UTF_8));
                out.write('\n');
            } catch (Throwable th) {
                System.out.println(th.getMessage());
            }
        }
        out.close();
    }

    public static void DoSearch(Directory directory) throws Exception {

        out.println("I think these are the files:");
        for (String s : directory.listAll()) {
            out.println(s);
        }

        IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(directory));
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_9);
        QueryParser parser = new QueryParser(Version.LUCENE_4_9, "any", analyzer);
        for (int i = 0; i < 5; i++) {
            long searchStart = System.currentTimeMillis();
            Query query = parser.parse("morrissey");
            //Query query = parser.parse("Dance");
            TopDocs docs = searcher.search(query, 10);
            long searchEnd = System.currentTimeMillis();
            out.println(String.format("%s %d total hits in %d", directory.getClass().getSimpleName(), docs.totalHits, searchEnd - searchStart));
            long lookupStart = System.currentTimeMillis();
            for (ScoreDoc d : docs.scoreDocs) {
                Document doc = searcher.doc(d.doc);
                out.println(String.format("%d %.2f %d %s", d.doc, d.score, d.shardIndex, doc.getField("any").stringValue()));
            }
            long lookupEnd = System.currentTimeMillis();
            out.println(String.format("Document lookup took %d ms for %d documents", lookupEnd-lookupStart, docs.scoreDocs.length));
        }

        directory.close();
    }

    private static Callable<Directory> buildDirectory(final String type) throws IOException {
        return new Callable<Directory>() {

            private final CassandraIO baseIO = new CassandraIO(name, 8192, "collene", "cindex").start("127.0.0.1:9042");
            private final File tmpDir = TestUtil.getRandomTempDir();
            private final MemoryIO dataMemory = new MemoryIO(8192);
            private final MemoryIO metaMemory = new MemoryIO(256);

            @Override
            public Directory call() throws Exception {
                Directory directory = null;

                if ("cassandra".equals(type)) {
                    directory = ColDirectory.open(
                            name,
                            baseIO,
                            baseIO.clone(metaName)
                    );
                } else if ("file".equals(type)) {
                    System.out.println("Building index in " + tmpDir.getAbsolutePath());
                    directory = FSDirectory.open(tmpDir);
                } else if ("memory".equals(type)) {
                    directory = ColDirectory.open(
                            name,
                            dataMemory,
                            metaMemory
                    );
                } else {
                    directory = null;
                }
                return directory;
            }
        };
    }

    public static void BuildIndex(Directory directory) throws Exception {
        String freedbPath = "/Users/gdusbabek/Downloads/freedb-complete-20140701.tar.bz2";

        if (directory == null) {
            System.out.println("Need to specify: { memory | file | cassandra }. Did you misspell something?");
            System.exit(-1);
        }

        FreeDbReader reader = new FreeDbReader(new File(freedbPath), 50000);
        reader.start();

        long indexStart = System.currentTimeMillis();
        Collection<Document> documents = new ArrayList<Document>(BATCH_SIZE);
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_9);
        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_9, analyzer);
        config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
        IndexWriter writer = new IndexWriter(directory, config);

        // stop after this many documents.
        final int maxDocuments = 400000; //Integer.MAX_VALUE;

        FreeDbEntry entry = reader.next();
        int count = 0;
        while (entry != null && count < maxDocuments) {
            Document doc = new Document();
            String any = entry.toString();
            doc.add(new Field("any", any, TextField.TYPE_STORED));
            doc.add(new Field("artist", entry.getArtist(), TextField.TYPE_NOT_STORED));
            doc.add(new Field("album", entry.getAlbum(), TextField.TYPE_NOT_STORED));
            doc.add(new Field("title", entry.getTitle(), TextField.TYPE_NOT_STORED));
            doc.add(new Field("genre", entry.getGenre(), TextField.TYPE_NOT_STORED));
            doc.add(new Field("year", entry.getYear(), TextField.TYPE_NOT_STORED));
            for (int i = 0; i < entry.getTrackCount(); i++) {
                doc.add(new Field("track", entry.getTrack(i), TextField.TYPE_STORED));
            }
            documents.add(doc);
            if (VERBOSE) {
                out.println(any);
            }

            if (documents.size() == BATCH_SIZE) {
                //out.println(String.format("Adding batch at count %d", count));
                writer.addDocuments(documents);
                //out.println("done");
                documents.clear();
            }

            count +=1;
            if (count >= MAX_ENTRIES) {
                // done indexing.
                break;
            }
            entry = reader.next();

            if (count % 100000 == 0) {
                out.println(String.format("Indexed %d documents", count));

                // do a quick morrissey search for fun.
//                IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(ColDirectory.open(
//                                new CassandraIO(8192, "collene", "cindex").start("127.0.0.1:9042"),
//                                new CassandraIO(8192, "collene", "cmeta").start("127.0.0.1:9042"),
//                                new CassandraIO(8192, "collene", "clock").start("127.0.0.1:9042")
//                )));
                IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer, false));
                QueryParser parser = new QueryParser(Version.LUCENE_4_9, "any", analyzer);
                long searchStart = System.currentTimeMillis();
                Query query = parser.parse("morrissey");
                TopDocs docs = searcher.search(query, 10);
                long searchEnd = System.currentTimeMillis();
                out.println(String.format("%s %d total hits in %d", directory.getClass().getSimpleName(), docs.totalHits, searchEnd - searchStart));
                for (ScoreDoc d : docs.scoreDocs) {
                    out.println(String.format("%d %.2f %d", d.doc, d.score, d.shardIndex));
                }
            }
        }

        if (documents.size() > 0) {
            out.println(String.format("Adding batch at count %d", count));
            writer.addDocuments(documents);
            out.println("done");
            documents.clear();

            // do a quick morrissey search for fun.
            IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer, false));
            QueryParser parser = new QueryParser(Version.LUCENE_4_9, "any", analyzer);
            long searchStart = System.currentTimeMillis();
            Query query = parser.parse("morrissey");
            TopDocs docs = searcher.search(query, 10);
            long searchEnd = System.currentTimeMillis();
            out.println(String.format("%s %d total hits in %d", directory.getClass().getSimpleName(), docs.totalHits, searchEnd - searchStart));
            for (ScoreDoc d : docs.scoreDocs) {
                out.println(String.format("%d %.2f %d", d.doc, d.score, d.shardIndex));
            }
        }

        long indexTime = System.currentTimeMillis() - indexStart;
        out.println(String.format("Indexed %d things in %d ms (%s)", count, indexTime, directory.toString()));

//        long startMerge = System.currentTimeMillis();
//        writer.forceMerge(1, true);
//        long endMerge = System.currentTimeMillis();
//        out.println(String.format("merge took %d ms", endMerge-startMerge));
        out.println("I think these are the files:");
        for (String s : directory.listAll()) {
            out.println(s);
        }

        writer.close(true);
        directory.close();
    }
}


text:
package com.ramez.olit.sale;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.ramez.olit.sale.R;

public class MapsActivity extends MapActivity {
	private MapController myMapController;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);

		LinearLayout MLL=(LinearLayout) findViewById(R.id.menuLinearLayout);
		MLL.setBackgroundDrawable(null);

		LinearLayout NLL=(LinearLayout) findViewById(R.id.notificationLinearLayout);
		NLL.setBackgroundDrawable(null);

		LinearLayout MRL=(LinearLayout) findViewById(R.id.moreLinearLayout);
		MRL.setBackgroundDrawable(null);

		LinearLayout LLL=(LinearLayout) findViewById(R.id.locationLinearLayout);
		LLL.setBackgroundResource(R.drawable.tabbaritem);

		MenuSetter m=new MenuSetter();
		m.setMenuItems(MapsActivity.this,R.id.locationLinearLayout);

        final MapView mapsView = (MapView) findViewById(R.id.mapsView);
        final List<Overlay> mapOverlays = mapsView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.red);
        final MapsItemizedOverlay itemizedoverlay = new MapsItemizedOverlay(drawable, this);
        TextView T=(TextView) findViewById(R.id.titleText);
        T.setText(getIntent().getExtras().getString("District"));

        TextView T1=(TextView) findViewById(R.id.district);
        T1.setText(getIntent().getExtras().getString("District"));

        TextView T2=(TextView) findViewById(R.id.desc);
        T2.setText(getIntent().getExtras().getString("Street"));

        ImageButton callButon=(ImageButton) findViewById(R.id.callImageButton);
        callButon.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				initiatePopupWindow();

			}
		});

        GeoPoint point = new GeoPoint((int) (getIntent().getExtras().getFloat("Longitude") * 1e6), (int) (getIntent().getExtras().getFloat("Latitude") * 1e6));

        OverlayItem overlayitem = new OverlayItem(point, getIntent().getExtras().getString("District"), getIntent().getExtras().getString("Street"));
        mapOverlays.clear();

        itemizedoverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedoverlay);

        myMapController = mapsView.getController();
        myMapController.animateTo(point);
        myMapController.setZoom(17);

	}

	private void initiatePopupWindow() {
	    try {
//	        //We need to get the instance of the LayoutInflater, use the context of this activity
//	        LayoutInflater inflater = (LayoutInflater) MapsActivity.this
//	                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//	        //Inflate the view from a predefined XML layout
//	        View layout = inflater.inflate(R.layout.phonelayout,
//	                (ViewGroup) findViewById(R.id.popup_element));
//	        // create a 300px width and 470px height PopupWindow
//
//	        Display display = getWindowManager().getDefaultDisplay();
//
//
//	        pw = new PopupWindow(layout, display.getWidth(), 470, true);
//	        // display the popup in the center
//	        pw.showAtLocation(layout, Gravity.BOTTOM, 0, 0);
//
//
//	        ArrayList<String> Phones= getIntent().getExtras().getStringArrayList("Phones");
//
//
//	        if (Phones.size()>0){
//	        	Button B1=(Button) layout.findViewById(R.id.button1);
//	        	B1.setText(Phones.get(0).toString());
//	        }
//	        if (Phones.size()>1){
//	        	Button B2=(Button) layout.findViewById(R.id.Button01);
//	        	B2.setText(Phones.get(1).toString());
//	        }
//
//
////	        mResultText = (TextView) layout.findViewById(R.id.server_status_text);
//	        Button cancelButton = (Button) layout.findViewById(R.id.cancelButton);
////	        makeBlack(cancelButton);
//	        cancelButton.setOnClickListener(cancel_button_click_listener);
//

	    	ArrayList<String> Phones= getIntent().getExtras().getStringArrayList("Phones");
	       final CharSequence[] items= Phones.toArray(new CharSequence[Phones.size()]);//{"Foo", "Bar", "Baz"};

	    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
	    	builder.setTitle("Call");
	    	builder.setItems(items, new DialogInterface.OnClickListener() {
	    	    public void onClick(DialogInterface dialog, int item) {
	    	    	Intent call = new Intent(Intent.ACTION_DIAL);
	    	    	call.setData(Uri.parse("tel:" + items[item]));
	    	    	startActivity(call);
	    	    }
	    	});
	    	final AlertDialog alert = builder.create();
	    	alert.setButton("Cancel", new DialogInterface.OnClickListener() {

	    		  public void onClick(DialogInterface arg0, int arg1) {
	    		  		alert.dismiss();
	    		  }});
	    	alert.show();
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	}

//	private OnClickListener cancel_button_click_listener = new OnClickListener() {
//	    public void onClick(View v) {
//	        pw.dismiss();
//	    }
//	};

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}


text:
module.exports.conf = require('./config/checkconfig.conf.js');
var	http = require('http'),
	rrest = require('../');


text:
package utils

import org.joda.time.DateTime

object Joda {
  implicit def dateTimeOrdering: Ordering[DateTime] = Ordering.fromLessThan(_ isAfter _)
}


text:
package org.dvare.test.list;

import org.dvare.binding.data.InstancesBinding;
import org.dvare.binding.model.ContextsBinding;
import org.dvare.binding.model.TypeBinding;
import org.dvare.binding.rule.RuleBinding;
import org.dvare.config.RuleConfiguration;
import org.dvare.evaluator.RuleEvaluator;
import org.dvare.exceptions.interpreter.InterpretException;
import org.dvare.exceptions.parser.ExpressionParseException;
import org.dvare.expression.Expression;
import org.dvare.parser.ExpressionParser;
import org.dvare.test.dataobjects.ForEachOperation;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ForAllOperationTest {
    private static final Logger logger = LoggerFactory.getLogger(ForAllOperationTest.class);

    @Test
    public void forAllBetweenTest() throws ExpressionParseException, InterpretException {

        RuleConfiguration factory = new RuleConfiguration();

        String exp = "forAll self selfInstance | selfInstance.Variable1->substring(2,2)->toInteger() between [80,90] endForAll";

        TypeBinding typeBinding = ExpressionParser.translate(ForEachOperation.class);
        ContextsBinding contexts = new ContextsBinding();
        contexts.addContext("self", typeBinding);

        Expression expression = factory.getParser().fromString(exp, contexts);

        logger.info(expression.toString());

        RuleBinding rule = new RuleBinding(expression);

        List<ForEachOperation> dataset = new ArrayList<>();

        ForEachOperation eachOperation1 = new ForEachOperation();
        eachOperation1.setVariable1("D81");
        dataset.add(eachOperation1);

        ForEachOperation eachOperation2 = new ForEachOperation();
        eachOperation2.setVariable1("D85");
        dataset.add(eachOperation2);

        ForEachOperation eachOperation3 = new ForEachOperation();
        eachOperation3.setVariable1("D89");
        dataset.add(eachOperation3);

        InstancesBinding instancesBinding = new InstancesBinding();
        instancesBinding.addInstance("self", dataset);

        RuleEvaluator evaluator = factory.getEvaluator();
        boolean result = (Boolean) evaluator.evaluate(rule, instancesBinding);
        Assertions.assertTrue(result);

    }

    @Test
    public void forAllBetweenNotTest() throws ExpressionParseException, InterpretException {

        RuleConfiguration factory = new RuleConfiguration();

        String exp = "not forAll self selfInstance | selfInstance.Variable1->substring(2,2)->toInteger() between [80,90] endForAll";

        TypeBinding typeBinding = ExpressionParser.translate(ForEachOperation.class);
        ContextsBinding contexts = new ContextsBinding();
        contexts.addContext("self", typeBinding);

        Expression expression = factory.getParser().fromString(exp, contexts);
        RuleBinding rule = new RuleBinding(expression);

        List<ForEachOperation> dataSet = new ArrayList<>();

        ForEachOperation eachOperation1 = new ForEachOperation();
        eachOperation1.setVariable1("D81");
        dataSet.add(eachOperation1);

        ForEachOperation eachOperation2 = new ForEachOperation();
        eachOperation2.setVariable1("D45F");
        dataSet.add(eachOperation2);

        ForEachOperation eachOperation3 = new ForEachOperation();
        eachOperation3.setVariable1("D89");
        dataSet.add(eachOperation3);

        InstancesBinding instancesBinding = new InstancesBinding();
        instancesBinding.addInstance("self", dataSet);

        RuleEvaluator evaluator = factory.getEvaluator();
        boolean result = (Boolean) evaluator.evaluate(rule, instancesBinding);
        Assertions.assertTrue(result);

    }

    @Test
    public void forAllValuesBetweenTest() throws ExpressionParseException, InterpretException {

        RuleConfiguration factory = new RuleConfiguration();

        String exp = "forAll variable value:StringType | variable.value between [80,90] endForAll";

        ContextsBinding contexts = new ContextsBinding();
        contexts.addContext("self", "{variable:StringListType}");

        Expression expression = factory.getParser().fromString(exp, contexts);

        //System.out.println(expression);

        Assertions.assertNotNull(expression);

    }

    @Test
    public void forAllValuesInTest() throws ExpressionParseException, InterpretException {

        RuleConfiguration factory = new RuleConfiguration();

        String exp = "forAll Variable1 value:StringType | Variable1.value in ['D81','D45F','D89'] endForAll";

        ContextsBinding contexts = new ContextsBinding();
        contexts.addContext("self", ExpressionParser.translate(ForEachOperation.class));

        Expression expression = factory.getParser().fromString(exp, contexts);

        ForEachOperation eachOperation1 = new ForEachOperation();
        eachOperation1.setVariable1("D81");

        ForEachOperation eachOperation2 = new ForEachOperation();
        eachOperation2.setVariable1("D45F");

        ForEachOperation eachOperation3 = new ForEachOperation();
        eachOperation3.setVariable1("D89");

        InstancesBinding instancesBinding = new InstancesBinding();
        instancesBinding.addInstance("self", Arrays.asList(eachOperation1, eachOperation2, eachOperation3));

        RuleEvaluator evaluator = factory.getEvaluator();
        boolean result = (Boolean) evaluator.evaluate(new RuleBinding(expression), instancesBinding);
        Assertions.assertTrue(result);

    }

    @Test
    public void forAllInTest() throws ExpressionParseException, InterpretException {

        RuleConfiguration factory = new RuleConfiguration();

        String exp = "forAll Variable1 tmp.value:StringType | tmp.value in ['D81','D45F','D89'] endForAll";

        ContextsBinding contexts = new ContextsBinding();
        contexts.addContext("self", ExpressionParser.translate(ForEachOperation.class));

        Expression expression = factory.getParser().fromString(exp, contexts);

        ForEachOperation eachOperation1 = new ForEachOperation();
        eachOperation1.setVariable1("D81");

        ForEachOperation eachOperation2 = new ForEachOperation();
        eachOperation2.setVariable1("D45F");

        ForEachOperation eachOperation3 = new ForEachOperation();
        eachOperation3.setVariable1("D89");

        InstancesBinding instancesBinding = new InstancesBinding();
        instancesBinding.addInstance("self", Arrays.asList(eachOperation1, eachOperation2, eachOperation3));

        RuleEvaluator evaluator = factory.getEvaluator();
        boolean result = (Boolean) evaluator.evaluate(new RuleBinding(expression), instancesBinding);
        Assertions.assertTrue(result);

    }
}


text:
<?php
/**
 * @theClassAnnotation
 */
class ParseTestMethodAnnotationsMock
{
    use ParseTestMethodAnnotationsTrait;
}

/**
 * @theTraitAnnotation
 */
trait ParseTestMethodAnnotationsTrait
{
}


text:
{-# LANGUAGE TemplateHaskell #-}
module ConsoleChess.Moves where

import qualified Data.Map as M
import Data.Maybe
import Data.Traversable
import Control.Lens

import ConsoleChess.Board
import ConsoleChess.Utils

data Move = Move { _from :: Coordinate Int, _to :: Coordinate Int }    deriving (Show,Eq)
data PlacedPiece = PlacedPiece { _piece :: Piece, _position :: Coordinate Int }  deriving (Show,Eq)
data GameState = GameState { _board :: Board, _moves :: [Move], _turn :: Color } deriving (Show,Eq)

makeLenses ''Move
makeLenses ''PlacedPiece
makeLenses ''GameState

initState :: GameState
initState = GameState initBoard [] White

isEatable :: Coordinate Int -> Color -> Board -> Bool
isEatable coord col board = maybe False (\p -> view color p /= col) piece
  where piece = coord `M.lookup` view pieces board

isOccupied :: Coordinate Int -> Board -> Bool
isOccupied coord board = isJust piece
  where piece = coord `M.lookup` view pieces board

isOnBoard :: Coordinate Int -> Bool
isOnBoard (Coordinate x y) = 1 <= x && x <= 8 && 1 <= y && y <= 8

-- | Create an infinite path in a certain direction from a starting point
pathFromStart :: Num a => (a, a) -> Coordinate a -> [Coordinate a]
pathFromStart direction start = iterate (updateCoordWithDirection direction) start

-- | Change a coordinate by adding a direction vector
updateCoordWithDirection :: Num a => (a,a) -> Coordinate a -> Coordinate a
updateCoordWithDirection (dx,dy) coord = coord & x +~ dx & y +~ dy

generates :: Piece -> [Coordinate Int -> [Coordinate Int]]
generates (Piece c Pawn)   = return $ \coord -> if c == Black then undefined else undefined
generates (Piece _ Bishop) = map pathFromStart directions
  where directions = (,) <$> [-1,1] <*> [-1,1]
generates (Piece _ King)   = map ((return .) . updateCoordWithDirection) directions
  where directions = filter (\(x,y) -> x /= 0 || y /= 0) $ (,) <$> [-1..1] <*> [-1..1]
generates (Piece _ Queen)  = map pathFromStart directions
  where directions = filter (\(x,y) -> x /= 0 || y /= 0) $ (,) <$> [-1..1] <*> [-1..1]
generates (Piece _ Rook)   = map pathFromStart directions
  where directions = [(0,1), (0,-1), (1,0), (-1,0)]
generates (Piece _ Knight) = map ((return .) . updateCoordWithDirection) directions
  where directions = [(x,y) | x <- dxy, y <- dxy, abs x /= abs y]
        dxy = [-2,-1,1,2]

getPiece :: Coordinate Int -> Board -> Maybe (PlacedPiece)
getPiece c (Board b) = c `M.lookup` b >>= \p -> Just $ PlacedPiece p c

movesForPiece :: PlacedPiece -> GameState -> [Move]
movesForPiece (PlacedPiece p pos) gs = map (Move pos) paths
  where b = gs ^. board
        paths = concatMap (\pathGen -> takeUntilWithHard hard soft $ pathGen pos) (generates p)
        hard = \coord -> not (isOnBoard coord) || (isOccupied coord b && not (isEatable coord (p ^. color) b))
        soft = \coord -> isEatable coord (p ^. color) b

possibleMoves :: GameState -> [Move]
possibleMoves gs = undefined


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

import com.facebook.airlift.testing.TestingTicker;
import com.facebook.presto.execution.SplitRunner;
import com.facebook.presto.execution.TaskId;
import com.facebook.presto.server.ServerConfig;
import com.facebook.presto.version.EmbedVersion;
import com.google.common.base.Ticker;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.SettableFuture;
import io.airlift.units.Duration;
import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.List;
import java.util.OptionalInt;
import java.util.concurrent.Future;
import java.util.concurrent.Phaser;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import static com.facebook.airlift.testing.Assertions.assertGreaterThan;
import static com.facebook.airlift.testing.Assertions.assertLessThan;
import static com.facebook.presto.execution.TaskManagerConfig.TaskPriorityTracking.QUERY_FAIR;
import static com.facebook.presto.execution.TaskManagerConfig.TaskPriorityTracking.TASK_FAIR;
import static com.facebook.presto.execution.executor.MultilevelSplitQueue.LEVEL_CONTRIBUTION_CAP;
import static com.facebook.presto.execution.executor.MultilevelSplitQueue.LEVEL_THRESHOLD_SECONDS;
import static com.google.common.collect.Iterables.getOnlyElement;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

public class TestTaskExecutor
{
    @Test(invocationCount = 100)
    public void testTasksComplete()
            throws Exception
    {
        TestingTicker ticker = new TestingTicker();
        TaskExecutor taskExecutor = new TaskExecutor(4, 8, 3, 4, TASK_FAIR, ticker);
        taskExecutor.start();
        ticker.increment(20, MILLISECONDS);

        try {
            TaskId taskId = new TaskId("test", 0, 0, 0);
            TaskHandle taskHandle = taskExecutor.addTask(taskId, () -> 0, 10, new Duration(1, MILLISECONDS), OptionalInt.empty());

            Phaser beginPhase = new Phaser();
            beginPhase.register();
            Phaser verificationComplete = new Phaser();
            verificationComplete.register();

            // add two jobs
            TestingJob driver1 = new TestingJob(ticker, new Phaser(1), beginPhase, verificationComplete, 10, 0);
            ListenableFuture<?> future1 = getOnlyElement(taskExecutor.enqueueSplits(taskHandle, true, ImmutableList.of(driver1)));
            TestingJob driver2 = new TestingJob(ticker, new Phaser(1), beginPhase, verificationComplete, 10, 0);
            ListenableFuture<?> future2 = getOnlyElement(taskExecutor.enqueueSplits(taskHandle, true, ImmutableList.of(driver2)));
            assertEquals(driver1.getCompletedPhases(), 0);
            assertEquals(driver2.getCompletedPhases(), 0);

            // verify worker have arrived but haven't processed yet
            beginPhase.arriveAndAwaitAdvance();
            assertEquals(driver1.getCompletedPhases(), 0);
            assertEquals(driver2.getCompletedPhases(), 0);
            ticker.increment(60, SECONDS);
            assertEquals(taskExecutor.getRunAwaySplitCount(), 0);
            ticker.increment(600, SECONDS);
            assertEquals(taskExecutor.getRunAwaySplitCount(), 2);
            verificationComplete.arriveAndAwaitAdvance();

            // advance one phase and verify
            beginPhase.arriveAndAwaitAdvance();
            assertEquals(driver1.getCompletedPhases(), 1);
            assertEquals(driver2.getCompletedPhases(), 1);

            verificationComplete.arriveAndAwaitAdvance();

            // add one more job
            TestingJob driver3 = new TestingJob(ticker, new Phaser(1), beginPhase, verificationComplete, 10, 0);
            ListenableFuture<?> future3 = getOnlyElement(taskExecutor.enqueueSplits(taskHandle, false, ImmutableList.of(driver3)));

            // advance one phase and verify
            beginPhase.arriveAndAwaitAdvance();
            assertEquals(driver1.getCompletedPhases(), 2);
            assertEquals(driver2.getCompletedPhases(), 2);
            assertEquals(driver3.getCompletedPhases(), 0);
            verificationComplete.arriveAndAwaitAdvance();

            // advance to the end of the first two task and verify
            beginPhase.arriveAndAwaitAdvance();
            for (int i = 0; i < 7; i++) {
                verificationComplete.arriveAndAwaitAdvance();
                beginPhase.arriveAndAwaitAdvance();
                assertEquals(beginPhase.getPhase(), verificationComplete.getPhase() + 1);
            }
            assertEquals(driver1.getCompletedPhases(), 10);
            assertEquals(driver2.getCompletedPhases(), 10);
            assertEquals(driver3.getCompletedPhases(), 8);
            future1.get(1, SECONDS);
            future2.get(1, SECONDS);
            verificationComplete.arriveAndAwaitAdvance();

            // advance two more times and verify
            beginPhase.arriveAndAwaitAdvance();
            verificationComplete.arriveAndAwaitAdvance();
            beginPhase.arriveAndAwaitAdvance();
            assertEquals(driver1.getCompletedPhases(), 10);
            assertEquals(driver2.getCompletedPhases(), 10);
            assertEquals(driver3.getCompletedPhases(), 10);
            future3.get(1, SECONDS);
            verificationComplete.arriveAndAwaitAdvance();

            assertEquals(driver1.getFirstPhase(), 0);
            assertEquals(driver2.getFirstPhase(), 0);
            assertEquals(driver3.getFirstPhase(), 2);

            assertEquals(driver1.getLastPhase(), 10);
            assertEquals(driver2.getLastPhase(), 10);
            assertEquals(driver3.getLastPhase(), 12);

            // no splits remaining
            ticker.increment(610, SECONDS);
            assertEquals(taskExecutor.getRunAwaySplitCount(), 0);
        }
        finally {
            taskExecutor.stop();
        }
    }

    @Test(invocationCount = 100)
    public void testQuantaFairness()
    {
        TestingTicker ticker = new TestingTicker();
        TaskExecutor taskExecutor = new TaskExecutor(1, 2, 3, 4, QUERY_FAIR, ticker);
        taskExecutor.start();
        ticker.increment(20, MILLISECONDS);

        try {
            TaskHandle shortQuantaTaskHandle = taskExecutor.addTask(new TaskId("short_quanta", 0, 0, 0), () -> 0, 10, new Duration(1, MILLISECONDS), OptionalInt.empty());
            TaskHandle longQuantaTaskHandle = taskExecutor.addTask(new TaskId("long_quanta", 0, 0, 0), () -> 0, 10, new Duration(1, MILLISECONDS), OptionalInt.empty());

            Phaser globalPhaser = new Phaser();

            TestingJob shortQuantaDriver = new TestingJob(ticker, new Phaser(), new Phaser(), globalPhaser, 10, 10);
            TestingJob longQuantaDriver = new TestingJob(ticker, new Phaser(), new Phaser(), globalPhaser, 10, 20);

            taskExecutor.enqueueSplits(shortQuantaTaskHandle, true, ImmutableList.of(shortQuantaDriver));
            taskExecutor.enqueueSplits(longQuantaTaskHandle, true, ImmutableList.of(longQuantaDriver));

            for (int i = 0; i < 11; i++) {
                globalPhaser.arriveAndAwaitAdvance();
            }

            assertTrue(shortQuantaDriver.getCompletedPhases() >= 7 && shortQuantaDriver.getCompletedPhases() <= 8);
            assertTrue(longQuantaDriver.getCompletedPhases() >= 3 && longQuantaDriver.getCompletedPhases() <= 4);

            globalPhaser.arriveAndDeregister();
        }
        finally {
            taskExecutor.stop();
        }
    }

    @Test(invocationCount = 100)
    public void testLevelMovement()
    {
        TestingTicker ticker = new TestingTicker();
        TaskExecutor taskExecutor = new TaskExecutor(2, 2, 3, 4, TASK_FAIR, ticker);
        taskExecutor.start();
        ticker.increment(20, MILLISECONDS);

        try {
            TaskHandle testTaskHandle = taskExecutor.addTask(new TaskId("test", 0, 0, 0), () -> 0, 10, new Duration(1, MILLISECONDS), OptionalInt.empty());

            Phaser globalPhaser = new Phaser();
            globalPhaser.bulkRegister(3);

            int quantaTimeMills = 500;
            int phasesPerSecond = 1000 / quantaTimeMills;
            int totalPhases = LEVEL_THRESHOLD_SECONDS[LEVEL_THRESHOLD_SECONDS.length - 1] * phasesPerSecond;
            TestingJob driver1 = new TestingJob(ticker, globalPhaser, new Phaser(), new Phaser(), totalPhases, quantaTimeMills);
            TestingJob driver2 = new TestingJob(ticker, globalPhaser, new Phaser(), new Phaser(), totalPhases, quantaTimeMills);

            taskExecutor.enqueueSplits(testTaskHandle, true, ImmutableList.of(driver1, driver2));

            int completedPhases = 0;
            for (int i = 0; i < (LEVEL_THRESHOLD_SECONDS.length - 1); i++) {
                for (; (completedPhases / phasesPerSecond) < LEVEL_THRESHOLD_SECONDS[i + 1]; completedPhases++) {
                    globalPhaser.arriveAndAwaitAdvance();
                }

                assertEquals(testTaskHandle.getPriority().getLevel(), i + 1);
            }

            globalPhaser.arriveAndDeregister();
        }
        finally {
            taskExecutor.stop();
        }
    }

    @Test(invocationCount = 100)
    public void testLevelMultipliers()
            throws Exception
    {
        TestingTicker ticker = new TestingTicker();
        TaskExecutor taskExecutor = new TaskExecutor(1, 3, 3, 4, TASK_FAIR, new MultilevelSplitQueue(2), ticker);
        taskExecutor.start();
        ticker.increment(20, MILLISECONDS);

        try {
            for (int i = 0; i < (LEVEL_THRESHOLD_SECONDS.length - 1); i++) {
                TaskHandle[] taskHandles = {
                        taskExecutor.addTask(new TaskId("test1", 0, 0, 0), () -> 0, 10, new Duration(1, MILLISECONDS), OptionalInt.empty()),
                        taskExecutor.addTask(new TaskId("test2", 0, 0, 0), () -> 0, 10, new Duration(1, MILLISECONDS), OptionalInt.empty()),
                        taskExecutor.addTask(new TaskId("test3", 0, 0, 0), () -> 0, 10, new Duration(1, MILLISECONDS), OptionalInt.empty())
                };

                // move task 0 to next level
                TestingJob task0Job = new TestingJob(ticker, new Phaser(1), new Phaser(), new Phaser(), 1, LEVEL_THRESHOLD_SECONDS[i + 1] * 1000);
                taskExecutor.enqueueSplits(
                        taskHandles[0],
                        true,
                        ImmutableList.of(task0Job));
                // move tasks 1 and 2 to this level
                TestingJob task1Job = new TestingJob(ticker, new Phaser(1), new Phaser(), new Phaser(), 1, LEVEL_THRESHOLD_SECONDS[i] * 1000);
                taskExecutor.enqueueSplits(
                        taskHandles[1],
                        true,
                        ImmutableList.of(task1Job));
                TestingJob task2Job = new TestingJob(ticker, new Phaser(1), new Phaser(), new Phaser(), 1, LEVEL_THRESHOLD_SECONDS[i] * 1000);
                taskExecutor.enqueueSplits(
                        taskHandles[2],
                        true,
                        ImmutableList.of(task2Job));

                task0Job.getCompletedFuture().get();
                task1Job.getCompletedFuture().get();
                task2Job.getCompletedFuture().get();

                // then, start new drivers for all tasks
                Phaser globalPhaser = new Phaser(2);
                int phasesForNextLevel = LEVEL_THRESHOLD_SECONDS[i + 1] - LEVEL_THRESHOLD_SECONDS[i];
                TestingJob[] drivers = new TestingJob[6];
                for (int j = 0; j < 6; j++) {
                    drivers[j] = new TestingJob(ticker, globalPhaser, new Phaser(), new Phaser(), phasesForNextLevel, 1000);
                }

                taskExecutor.enqueueSplits(taskHandles[0], true, ImmutableList.of(drivers[0], drivers[1]));
                taskExecutor.enqueueSplits(taskHandles[1], true, ImmutableList.of(drivers[2], drivers[3]));
                taskExecutor.enqueueSplits(taskHandles[2], true, ImmutableList.of(drivers[4], drivers[5]));

                // run all three drivers
                int lowerLevelStart = drivers[2].getCompletedPhases() + drivers[3].getCompletedPhases() + drivers[4].getCompletedPhases() + drivers[5].getCompletedPhases();
                int higherLevelStart = drivers[0].getCompletedPhases() + drivers[1].getCompletedPhases();
                while (Arrays.stream(drivers).noneMatch(TestingJob::isFinished)) {
                    globalPhaser.arriveAndAwaitAdvance();

                    int lowerLevelEnd = drivers[2].getCompletedPhases() + drivers[3].getCompletedPhases() + drivers[4].getCompletedPhases() + drivers[5].getCompletedPhases();
                    int lowerLevelTime = lowerLevelEnd - lowerLevelStart;
                    int higherLevelEnd = drivers[0].getCompletedPhases() + drivers[1].getCompletedPhases();
                    int higherLevelTime = higherLevelEnd - higherLevelStart;

                    if (higherLevelTime > 20) {
                        assertGreaterThan(lowerLevelTime, (higherLevelTime * 2) - 10);
                        assertLessThan(higherLevelTime, (lowerLevelTime * 2) + 10);
                    }
                }

                try {
                    globalPhaser.arriveAndDeregister();
                }
                catch (IllegalStateException e) {
                    // under high concurrency sometimes the deregister call can occur after completion
                    // this is not a real problem
                }
                taskExecutor.removeTask(taskHandles[0]);
                taskExecutor.removeTask(taskHandles[1]);
                taskExecutor.removeTask(taskHandles[2]);
            }
        }
        finally {
            taskExecutor.stop();
        }
    }

    @Test
    public void testTaskHandle()
    {
        TestingTicker ticker = new TestingTicker();
        TaskExecutor taskExecutor = new TaskExecutor(4, 8, 3, 4, QUERY_FAIR, ticker);
        taskExecutor.start();

        try {
            TaskId taskId = new TaskId("test", 0, 0, 0);
            TaskHandle taskHandle = taskExecutor.addTask(taskId, () -> 0, 10, new Duration(1, MILLISECONDS), OptionalInt.empty());

            Phaser beginPhase = new Phaser();
            beginPhase.register();
            Phaser verificationComplete = new Phaser();
            verificationComplete.register();

            TestingJob driver1 = new TestingJob(ticker, new Phaser(), beginPhase, verificationComplete, 10, 0);
            TestingJob driver2 = new TestingJob(ticker, new Phaser(), beginPhase, verificationComplete, 10, 0);

            // force enqueue a split
            taskExecutor.enqueueSplits(taskHandle, true, ImmutableList.of(driver1));
            assertEquals(taskHandle.getRunningLeafSplits(), 0);

            // normal enqueue a split
            taskExecutor.enqueueSplits(taskHandle, false, ImmutableList.of(driver2));
            assertEquals(taskHandle.getRunningLeafSplits(), 1);

            // let the split continue to run
            beginPhase.arriveAndDeregister();
            verificationComplete.arriveAndDeregister();
        }
        finally {
            taskExecutor.stop();
        }
    }

    @Test
    public void testLevelContributionCap()
    {
        MultilevelSplitQueue splitQueue = new MultilevelSplitQueue(2);
        TaskHandle handle0 = new TaskHandle(new TaskId("test0", 0, 0, 0), new TaskPriorityTracker(splitQueue), () -> 1, 1, new Duration(1, SECONDS), OptionalInt.empty());
        TaskHandle handle1 = new TaskHandle(new TaskId("test1", 0, 0, 0), new TaskPriorityTracker(splitQueue), () -> 1, 1, new Duration(1, SECONDS), OptionalInt.empty());

        for (int i = 0; i < (LEVEL_THRESHOLD_SECONDS.length - 1); i++) {
            long levelAdvanceTime = SECONDS.toNanos(LEVEL_THRESHOLD_SECONDS[i + 1] - LEVEL_THRESHOLD_SECONDS[i]);
            handle0.addScheduledNanos(levelAdvanceTime);
            assertEquals(handle0.getPriority().getLevel(), i + 1);

            handle1.addScheduledNanos(levelAdvanceTime);
            assertEquals(handle1.getPriority().getLevel(), i + 1);

            assertEquals(splitQueue.getLevelScheduledTime(i), 2 * Math.min(levelAdvanceTime, LEVEL_CONTRIBUTION_CAP));
            assertEquals(splitQueue.getLevelScheduledTime(i + 1), 0);
        }
    }

    @Test
    public void testUpdateLevelWithCap()
    {
        MultilevelSplitQueue splitQueue = new MultilevelSplitQueue(2);
        TaskHandle handle0 = new TaskHandle(new TaskId("test0", 0, 0, 0), new TaskPriorityTracker(splitQueue), () -> 1, 1, new Duration(1, SECONDS), OptionalInt.empty());

        long quantaNanos = MINUTES.toNanos(10);
        handle0.addScheduledNanos(quantaNanos);
        long cappedNanos = Math.min(quantaNanos, LEVEL_CONTRIBUTION_CAP);

        for (int i = 0; i < (LEVEL_THRESHOLD_SECONDS.length - 1); i++) {
            long thisLevelTime = Math.min(SECONDS.toNanos(LEVEL_THRESHOLD_SECONDS[i + 1] - LEVEL_THRESHOLD_SECONDS[i]), cappedNanos);
            assertEquals(splitQueue.getLevelScheduledTime(i), thisLevelTime);
            cappedNanos -= thisLevelTime;
        }
    }

    @Test(timeOut = 30_000)
    public void testMinMaxDriversPerTask()
    {
        int maxDriversPerTask = 2;
        MultilevelSplitQueue splitQueue = new MultilevelSplitQueue(2);
        TestingTicker ticker = new TestingTicker();
        TaskExecutor taskExecutor = new TaskExecutor(4, 16, 1, maxDriversPerTask, QUERY_FAIR, splitQueue, ticker);
        taskExecutor.start();
        try {
            TaskHandle testTaskHandle = taskExecutor.addTask(new TaskId("test", 0, 0, 0), () -> 0, 10, new Duration(1, MILLISECONDS), OptionalInt.empty());

            // enqueue all batches of splits
            int batchCount = 4;
            TestingJob[] splits = new TestingJob[8];
            Phaser[] phasers = new Phaser[batchCount];
            for (int batch = 0; batch < batchCount; batch++) {
                phasers[batch] = new Phaser();
                phasers[batch].register();
                TestingJob split1 = new TestingJob(ticker, new Phaser(), new Phaser(), phasers[batch], 1, 0);
                TestingJob split2 = new TestingJob(ticker, new Phaser(), new Phaser(), phasers[batch], 1, 0);
                splits[2 * batch] = split1;
                splits[2 * batch + 1] = split2;
                taskExecutor.enqueueSplits(testTaskHandle, false, ImmutableList.of(split1, split2));
            }

            // assert that the splits are processed in batches as expected
            for (int batch = 0; batch < batchCount; batch++) {
                // wait until the current batch starts
                waitUntilSplitsStart(ImmutableList.of(splits[2 * batch], splits[2 * batch + 1]));
                // assert that only the splits including and up to the current batch are running and the rest haven't started yet
                assertSplitStates(2 * batch + 1, splits);
                // complete the current batch
                phasers[batch].arriveAndDeregister();
            }
        }
        finally {
            taskExecutor.stop();
        }
    }

    @Test(timeOut = 30_000)
    public void testUserSpecifiedMaxDriversPerTask()
    {
        MultilevelSplitQueue splitQueue = new MultilevelSplitQueue(2);
        TestingTicker ticker = new TestingTicker();
        // create a task executor with min/max drivers per task to be 2 and 4
        TaskExecutor taskExecutor = new TaskExecutor(4, 16, 2, 4, TASK_FAIR, splitQueue, ticker);
        taskExecutor.start();
        try {
            // overwrite the max drivers per task to be 1
            TaskHandle testTaskHandle = taskExecutor.addTask(new TaskId("test", 0, 0, 0), () -> 0, 10, new Duration(1, MILLISECONDS), OptionalInt.of(1));

            // enqueue all batches of splits
            int batchCount = 4;
            TestingJob[] splits = new TestingJob[4];
            Phaser[] phasers = new Phaser[batchCount];
            for (int batch = 0; batch < batchCount; batch++) {
                phasers[batch] = new Phaser();
                phasers[batch].register();
                TestingJob split = new TestingJob(ticker, new Phaser(), new Phaser(), phasers[batch], 1, 0);
                splits[batch] = split;
                taskExecutor.enqueueSplits(testTaskHandle, false, ImmutableList.of(split));
            }

            // assert that the splits are processed in batches as expected
            for (int batch = 0; batch < batchCount; batch++) {
                // wait until the current batch starts
                waitUntilSplitsStart(ImmutableList.of(splits[batch]));
                // assert that only the splits including and up to the current batch are running and the rest haven't started yet
                assertSplitStates(batch, splits);
                // complete the current batch
                phasers[batch].arriveAndDeregister();
            }
        }
        finally {
            taskExecutor.stop();
        }
    }

    @Test
    public void testTaskExecutorRunawaySplitInterrupt()
            throws Exception
    {
        TaskExecutor taskExecutor = new TaskExecutor(
                8,
                16,
                3,
                4,
                TASK_FAIR,
                new Duration(1, SECONDS),
                elements -> elements.stream()
                        .anyMatch(element -> element.getFileName().equals("TestTaskExecutor.java")),
                new Duration(1, SECONDS),
                new EmbedVersion(new ServerConfig()),
                new MultilevelSplitQueue(2),
                Ticker.systemTicker());
        taskExecutor.start();

        try {
            TaskId taskId = new TaskId("foo", 0, 0, 0);
            TaskHandle taskHandle = taskExecutor.addTask(
                    taskId, () -> 1.0,
                    1,
                    new Duration(1, TimeUnit.SECONDS),
                    OptionalInt.of(1));
            MockSplitRunner mockSplitRunner = new MockSplitRunner();
            taskExecutor.enqueueSplits(taskHandle, false, ImmutableList.of(mockSplitRunner));
            mockSplitRunner.interrupted.get(60, TimeUnit.SECONDS);
        }
        finally {
            taskExecutor.stop();
        }
    }

    private void assertSplitStates(int endIndex, TestingJob[] splits)
    {
        // assert that splits up to and including endIndex are all started
        for (int i = 0; i <= endIndex; i++) {
            assertTrue(splits[i].isStarted());
        }

        // assert that splits starting from endIndex haven't started yet
        for (int i = endIndex + 1; i < splits.length; i++) {
            assertFalse(splits[i].isStarted());
        }
    }

    private static void waitUntilSplitsStart(List<TestingJob> splits)
    {
        while (splits.stream().anyMatch(split -> !split.isStarted())) {
            try {
                Thread.sleep(200);
            }
            catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new RuntimeException(e);
            }
        }
    }

    private static class TestingJob
            implements SplitRunner
    {
        private final TestingTicker ticker;
        private final Phaser globalPhaser;
        private final Phaser beginQuantaPhaser;
        private final Phaser endQuantaPhaser;
        private final int requiredPhases;
        private final int quantaTimeMillis;
        private final AtomicInteger completedPhases = new AtomicInteger();

        private final AtomicInteger firstPhase = new AtomicInteger(-1);
        private final AtomicInteger lastPhase = new AtomicInteger(-1);

        private final AtomicBoolean started = new AtomicBoolean();
        private final SettableFuture<?> completed = SettableFuture.create();

        public TestingJob(TestingTicker ticker, Phaser globalPhaser, Phaser beginQuantaPhaser, Phaser endQuantaPhaser, int requiredPhases, int quantaTimeMillis)
        {
            this.ticker = ticker;
            this.globalPhaser = globalPhaser;
            this.beginQuantaPhaser = beginQuantaPhaser;
            this.endQuantaPhaser = endQuantaPhaser;
            this.requiredPhases = requiredPhases;
            this.quantaTimeMillis = quantaTimeMillis;

            beginQuantaPhaser.register();
            endQuantaPhaser.register();

            if (globalPhaser.getRegisteredParties() == 0) {
                globalPhaser.register();
            }
        }

        private int getFirstPhase()
        {
            return firstPhase.get();
        }

        private int getLastPhase()
        {
            return lastPhase.get();
        }

        private int getCompletedPhases()
        {
            return completedPhases.get();
        }

        @Override
        public ListenableFuture<?> processFor(Duration duration)
        {
            started.set(true);
            ticker.increment(quantaTimeMillis, MILLISECONDS);
            globalPhaser.arriveAndAwaitAdvance();
            int phase = beginQuantaPhaser.arriveAndAwaitAdvance();
            firstPhase.compareAndSet(-1, phase - 1);
            lastPhase.set(phase);
            endQuantaPhaser.arriveAndAwaitAdvance();
            if (completedPhases.incrementAndGet() >= requiredPhases) {
                endQuantaPhaser.arriveAndDeregister();
                beginQuantaPhaser.arriveAndDeregister();
                globalPhaser.arriveAndDeregister();
                completed.set(null);
            }

            return Futures.immediateFuture(null);
        }

        @Override
        public String getInfo()
        {
            return "testing-split";
        }

        @Override
        public boolean isFinished()
        {
            return completed.isDone();
        }

        public boolean isStarted()
        {
            return started.get();
        }

        @Override
        public void close()
        {
        }

        public Future<?> getCompletedFuture()
        {
            return completed;
        }
    }

    private static class MockSplitRunner
            implements SplitRunner
    {
        private SettableFuture<Boolean> interrupted = SettableFuture.create();

        @Override
        public boolean isFinished()
        {
            return interrupted.isDone();
        }

        @Override
        public ListenableFuture<?> processFor(Duration duration)
        {
            while (true) {
                try {
                    Thread.sleep(1);
                }
                catch (InterruptedException e) {
                    break;
                }
            }
            interrupted.set(true);
            return Futures.immediateFuture(null);
        }

        @Override
        public String getInfo()
        {
            return "";
        }

        @Override
        public void close()
        {
        }
    }
}


text:
from PyQt4 import QtCore, QtGui
import PIL.ImageQt as ImageQt

from Chart import Chart
from imagePreProcessor import imagePreProcessor

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(QtGui.QMainWindow):

    def __init__(self):
        super(Ui_MainWindow, self).__init__()
        self.imgPreProc = imagePreProcessor()

    def setupUi(self, MainWindow):
        MainWindow.showMaximized()
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        #MainWindow.resize(1014, 682)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))

        self.gridLayout_4 = QtGui.QGridLayout(self.centralwidget)
        self.gridLayout_4.setObjectName(_fromUtf8("gridLayout_4"))

        self.frame_2 = QtGui.QFrame(self.centralwidget)
        self.frame_2.setFrameShape(QtGui.QFrame.StyledPanel)
        self.frame_2.setFrameShadow(QtGui.QFrame.Raised)
        self.frame_2.setObjectName(_fromUtf8("frame_2"))

        self.gridLayout_3 = QtGui.QGridLayout(self.frame_2)
        self.gridLayout_3.setObjectName(_fromUtf8("gridLayout_3"))

        self.scrollArea_2 = QtGui.QScrollArea(self.frame_2)
        self.scrollArea_2.setWidgetResizable(True)
        self.scrollArea_2.setObjectName(_fromUtf8("scrollArea_2"))

        self.scrollAreaWidgetContents_2 = QtGui.QWidget()
        self.scrollAreaWidgetContents_2.setGeometry(QtCore.QRect(0, 0, 459, 448))
        self.scrollAreaWidgetContents_2.setObjectName(_fromUtf8("scrollAreaWidgetContents_2"))

        self.gridLayout_6 = QtGui.QGridLayout(self.scrollAreaWidgetContents_2)
        self.gridLayout_6.setObjectName(_fromUtf8("gridLayout_6"))

        self.modifided_image = ImageWidget(self.scrollAreaWidgetContents_2)
        self.modifided_image.setObjectName(_fromUtf8("modifided_image"))

        self.gridLayout_6.addWidget(self.modifided_image, 0, 0, 1, 1)

        self.scrollArea_2.setWidget(self.scrollAreaWidgetContents_2)

        self.gridLayout_3.addWidget(self.scrollArea_2, 0, 0, 1, 1)
        self.gridLayout_4.addWidget(self.frame_2, 0, 1, 1, 1)

        self.frame = QtGui.QFrame(self.centralwidget)
        self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtGui.QFrame.Raised)
        self.frame.setObjectName(_fromUtf8("frame"))

        self.gridLayout_2 = QtGui.QGridLayout(self.frame)
        self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2"))

        self.scrollArea = QtGui.QScrollArea(self.frame)
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setObjectName(_fromUtf8("scrollArea"))

        self.scrollAreaWidgetContents = QtGui.QWidget()
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 459, 448))
        self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents"))

        self.gridLayout_5 = QtGui.QGridLayout(self.scrollAreaWidgetContents)
        self.gridLayout_5.setObjectName(_fromUtf8("gridLayout_5"))

        self.original_image = ImageWidget(self.scrollAreaWidgetContents)
        self.original_image.setObjectName(_fromUtf8("original_image"))

        self.gridLayout_5.addWidget(self.original_image, 0, 0, 1, 1)

        self.scrollArea.setWidget(self.scrollAreaWidgetContents)

        self.gridLayout_2.addWidget(self.scrollArea, 0, 0, 1, 1)
        self.gridLayout_4.addWidget(self.frame, 0, 0, 1, 1)

        MainWindow.setCentralWidget(self.centralwidget)

        self.dockWidget = QtGui.QDockWidget(MainWindow)
        self.dockWidget.setObjectName(_fromUtf8("dockWidget"))
        self.dockWidget.setFloating(True)
        self.dockWidget.setGeometry(QtCore.QRect(0, 0, 620, 220))

        self.dockWidgetContents = QtGui.QWidget()
        self.dockWidgetContents.setObjectName(_fromUtf8("dockWidgetContents"))

        self.horizontalLayout = QtGui.QHBoxLayout(self.dockWidgetContents)
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))

        self.frame_4 = QtGui.QFrame(self.dockWidgetContents)
        self.frame_4.setFrameShape(QtGui.QFrame.StyledPanel)
        self.frame_4.setFrameShadow(QtGui.QFrame.Raised)
        self.frame_4.setObjectName(_fromUtf8("frame_4"))

        self.gridLayout_8 = QtGui.QGridLayout(self.frame_4)
        self.gridLayout_8.setObjectName(_fromUtf8("gridLayout_8"))

        self.original_histogram = Chart(self.frame_4)
        self.original_histogram.setObjectName(_fromUtf8("original_histogram"))

        self.gridLayout_8.addWidget(self.original_histogram, 0, 0, 1, 1)

        self.horizontalLayout.addWidget(self.frame_4)

        self.frame_3 = QtGui.QFrame(self.dockWidgetContents)
        self.frame_3.setFrameShape(QtGui.QFrame.StyledPanel)
        self.frame_3.setFrameShadow(QtGui.QFrame.Raised)
        self.frame_3.setObjectName(_fromUtf8("frame_3"))

        self.gridLayout_7 = QtGui.QGridLayout(self.frame_3)
        self.gridLayout_7.setObjectName(_fromUtf8("gridLayout_7"))

        self.modifided_histogram = Chart(self.frame_3)
        self.modifided_histogram.setObjectName(_fromUtf8("modifided_histogram"))

        self.gridLayout_7.addWidget(self.modifided_histogram, 0, 0, 1, 1)

        self.horizontalLayout.addWidget(self.frame_3)

        self.dockWidget.setWidget(self.dockWidgetContents)
        MainWindow.addDockWidget(QtCore.Qt.DockWidgetArea(8), self.dockWidget)

        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1014, 22))
        self.menubar.setObjectName(_fromUtf8("menubar"))

        self.menuFile = QtGui.QMenu(self.menubar)
        self.menuFile.setObjectName(_fromUtf8("menuFile"))

        self.menuOperations = QtGui.QMenu(self.menubar)
        self.menuOperations.setObjectName(_fromUtf8("menuOperations"))
        self.menuOperations.setEnabled(False)

        self.menuHistogram_Modification = QtGui.QMenu(self.menuOperations)
        self.menuHistogram_Modification.setObjectName(_fromUtf8("menuHistogram_Modification"))

        self.menuSlide_Histogram = QtGui.QMenu(self.menuHistogram_Modification)
        self.menuSlide_Histogram.setObjectName(_fromUtf8("menuSlide_Histogram"))

        self.menuMapping_Equations = QtGui.QMenu(self.menuOperations)
        self.menuMapping_Equations.setObjectName(_fromUtf8("menuMapping_Equations"))

        self.menuLocal_Mask_Operations = QtGui.QMenu(self.menuOperations)
        self.menuLocal_Mask_Operations.setObjectName(_fromUtf8("menuLocal_Mask_Operations"))

        self.menuSegmentation = QtGui.QMenu(self.menuOperations)
        self.menuSegmentation.setObjectName(_fromUtf8("menuSegmentation"))

        MainWindow.setMenuBar(self.menubar)

        self.actionOpen = QtGui.QAction(MainWindow)
        self.actionOpen.setObjectName(_fromUtf8("action_Open"))
        self.actionOpen.setShortcut('Ctrl+O')
        self.actionOpen.triggered.connect(self.showOpen)

        self.actionExit = QtGui.QAction(MainWindow)
        self.actionExit.setObjectName(_fromUtf8("actionExit"))
        self.actionExit.setShortcut('Ctrl+Q')
        self.actionExit.setStatusTip('Exit application')
        self.actionExit.triggered.connect(QtGui.qApp.quit)

        self.menuFile.addAction(self.actionOpen)
        self.menuFile.addAction(self.actionExit)

        self.actionStretch_Histogram = QtGui.QAction(MainWindow)
        self.actionStretch_Histogram.setObjectName(_fromUtf8("actionStretch_Histogram"))
        self.actionStretch_Histogram.triggered.connect(self.stretchHisto)

        self.actionShrink_Histogram = QtGui.QAction(MainWindow)
        self.actionShrink_Histogram.setObjectName(_fromUtf8("actionShrink_Histogram"))
        self.actionShrink_Histogram.triggered.connect(self.shrinkHisto)

        self.actionLeft = QtGui.QAction(MainWindow)
        self.actionLeft.setObjectName(_fromUtf8("actionLeft"))
        self.actionLeft.triggered.connect(self.slidesLeftHisto)

        self.actionRight = QtGui.QAction(MainWindow)
        self.actionRight.setObjectName(_fromUtf8("actionRight"))
        self.actionRight.triggered.connect(self.slidesRightHisto)

        self.actionHistogram_Equalization = QtGui.QAction(MainWindow)
        self.actionHistogram_Equalization.setObjectName(_fromUtf8("actionHistogram_Equalization"))
        self.actionHistogram_Equalization.triggered.connect(self.histogramEqualization)

        self.actionNegative = QtGui.QAction(MainWindow)
        self.actionNegative.setObjectName(_fromUtf8("actionNegative"))
        self.actionNegative.triggered.connect(self.negative)

        self.actionMean_Local_Filtering = QtGui.QAction(MainWindow)
        self.actionMean_Local_Filtering.setObjectName(_fromUtf8("actionMean_Local_Filtering"))
        self.actionMean_Local_Filtering.triggered.connect(self.meanFilter)

        self.actionMedian_Local_Filtering = QtGui.QAction(MainWindow)
        self.actionMedian_Local_Filtering.setObjectName(_fromUtf8("actionMedian_Local_Filtering"))
        self.actionMedian_Local_Filtering.triggered.connect(self.medianFilter)

        self.actionSobel_Edge_Detector = QtGui.QAction(MainWindow)
        self.actionSobel_Edge_Detector.setObjectName(_fromUtf8("actionSobel_Edge_Detector"))
        self.actionSobel_Edge_Detector.triggered.connect(self.edgeDetection)

        self.menuSlide_Histogram.addAction(self.actionLeft)
        self.menuSlide_Histogram.addAction(self.actionRight)

        self.menuHistogram_Modification.addAction(self.actionStretch_Histogram)
        self.menuHistogram_Modification.addAction(self.actionShrink_Histogram)
        self.menuHistogram_Modification.addAction(self.menuSlide_Histogram.menuAction())
        self.menuHistogram_Modification.addAction(self.actionHistogram_Equalization)

        self.menuMapping_Equations.addAction(self.actionNegative)

        self.menuLocal_Mask_Operations.addAction(self.actionMean_Local_Filtering)
        self.menuLocal_Mask_Operations.addAction(self.actionMedian_Local_Filtering)

        self.menuSegmentation.addAction(self.actionSobel_Edge_Detector)

        self.menuOperations.addAction(self.menuHistogram_Modification.menuAction())
        self.menuOperations.addAction(self.menuMapping_Equations.menuAction())
        self.menuOperations.addAction(self.menuLocal_Mask_Operations.menuAction())
        self.menuOperations.addAction(self.menuSegmentation.menuAction())

        self.menubar.addAction(self.menuFile.menuAction())
        self.menubar.addAction(self.menuOperations.menuAction())

        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))

        self.menuFile.setTitle(_translate("MainWindow", "File", None))
        self.menuOperations.setTitle(_translate("MainWindow", "Operations", None))
        self.menuHistogram_Modification.setTitle(_translate("MainWindow", "Histogram Modification", None))
        self.menuSlide_Histogram.setTitle(_translate("MainWindow", "Slide Histogram", None))
        self.menuMapping_Equations.setTitle(_translate("MainWindow", "Mapping Equations", None))
        self.menuLocal_Mask_Operations.setTitle(_translate("MainWindow", "Local (Mask) Operations", None))
        self.menuSegmentation.setTitle(_translate("MainWindow", "Segmentation", None))

        self.actionOpen.setText(_translate("MainWindow", "Open", None))
        self.actionExit.setText(_translate("MainWindow", "Exit", None))
        self.actionStretch_Histogram.setText(_translate("MainWindow", "Stretch Histogram", None))
        self.actionShrink_Histogram.setText(_translate("MainWindow", "Shrink Histogram", None))
        self.actionLeft.setText(_translate("MainWindow", "Left", None))
        self.actionRight.setText(_translate("MainWindow", "Right", None))
        self.actionHistogram_Equalization.setText(_translate("MainWindow", "Histogram Equalization", None))
        self.actionNegative.setText(_translate("MainWindow", "Negative", None))
        self.actionMean_Local_Filtering.setText(_translate("MainWindow", "Mean Local Filtering", None))
        self.actionMedian_Local_Filtering.setText(_translate("MainWindow", "Median Local Filtering", None))
        self.actionSobel_Edge_Detector.setText(_translate("MainWindow", "Sobel Edge Detector", None))

    def showOpen(self):
        """Shows the file opening dialog."""
        filepath = QtGui.QFileDialog.getOpenFileName(
        self, 'Open File', '', 'All Files (*.*);;jpeg (*.jpeg);;jpg (*.jpg);;png (*.png)')

        if filepath:
            self.open(filepath)

    def showIntDialog(self, title, text):
        amount, ok = QtGui.QInputDialog.getInt(self, title, text)

        if ok:
            return amount
        else:
            return 0

    def open(self, filepath):
        self.imgPreProc.loadImage(str(filepath))
        self.original_image.Qimg = ImageQt.ImageQt(self.imgPreProc.img.convert("RGB") if self.imgPreProc.img.mode == "L" else self.imgPreProc.img)
        self.original_image.repaint()
        self.original_histogram.setData(self.imgPreProc.histo)
        self.refreshAll()
        self.menuOperations.setEnabled(True)

    def repaintImage(self):
        self.modifided_image.Qimg = ImageQt.ImageQt(self.imgPreProc.img.convert("RGB") if self.imgPreProc.img.mode == "L" else self.imgPreProc.img)
        self.modifided_image.repaint()

    def rebuildHisto(self):
        self.modifided_histogram.setData(self.imgPreProc.histo)

    def refreshAll(self):
        self.rebuildHisto()
        self.repaintImage()

    def stretchHisto(self):
        self.imgPreProc.stretchHisto()
        self.refreshAll()

    def shrinkHisto(self):
        minR = self.showIntDialog('Histogram Min Shrink', 'Shrink Min:')
        if minR:
            maxR = self.showIntDialog('Histogram Max Shrink', 'Shrink Max:')
            if maxR:
                self.imgPreProc.shrinkHisto(minR, maxR)
                self.refreshAll()
            else :
                QtGui.QMessageBox.critical(self, "Error", "Provide the the Max")
        else :
            QtGui.QMessageBox.critical(self, "Error", "Provide the the Min")

    def slidesLeftHisto(self):
        amount = self.showIntDialog('Slide Histogram, Left', 'Sliding Amount:')
        if amount:
            self.imgPreProc.slidesLeftHisto(amount)
            self.refreshAll()
        else :
            QtGui.QMessageBox.critical(self, "Error", "Provide the amount of sliding")

    def slidesRightHisto(self):
        amount = self.showIntDialog('Slide Histogram, Right', 'Sliding Amount:')
        if amount:
            self.imgPreProc.slidesRightHisto(amount)
            self.refreshAll()
        else :
            QtGui.QMessageBox.critical(self, "Error", "You have to fill the By")

    def histogramEqualization(self):
        self.imgPreProc.histogramEqualization()
        self.refreshAll()

    def negative(self):
        self.imgPreProc.negative()
        self.refreshAll()

    def meanFilter(self):
        self.imgPreProc.meanFilter()
        self.refreshAll()

    def medianFilter(self):
        self.imgPreProc.medianFilter()
        self.refreshAll()

    def edgeDetection(self):
        self.imgPreProc.edgeDetection()
        self.refreshAll()

class ImageWidget(QtGui.QWidget):
    def __init__(self, parent):
        super(ImageWidget, self).__init__(parent)
        self.Qimg = None

    def paintEvent(self, event):
        painter = QtGui.QPainter(self)
        if self.Qimg :
            painter.drawImage(event.rect(), self.Qimg, event.rect())
        painter.end()


text: Register docs for private natial organizations


text:
/* eslint-disable */

'use strict';

/*::
import type { Fragment, ReaderFragment } from 'relay-runtime';
import type { FragmentType } from "relay-runtime";
declare export opaque type RelayResponseNormalizerTest10Fragment$fragmentType: FragmentType;
export type RelayResponseNormalizerTest10Fragment$data = {|
  +actors: ?$ReadOnlyArray<?{|
    +actors?: ?$ReadOnlyArray<?{|
      +name: ?string,
    |}>,
    +name?: ?string,
  |}>,
  +id: string,
  +$fragmentType: RelayResponseNormalizerTest10Fragment$fragmentType,
|};
export type RelayResponseNormalizerTest10Fragment$key = {
  +$data?: RelayResponseNormalizerTest10Fragment$data,
  +$fragmentSpreads: RelayResponseNormalizerTest10Fragment$fragmentType,
  ...
};
*/

var node/*: ReaderFragment*/ = (function(){
var v0 = {
  "alias": null,
  "args": null,
  "kind": "ScalarField",
  "name": "name",
  "storageKey": null
};
return {
  "argumentDefinitions": [],
  "kind": "Fragment",
  "metadata": null,
  "name": "RelayResponseNormalizerTest10Fragment",
  "selections": [
    {
      "alias": null,
      "args": null,
      "kind": "ScalarField",
      "name": "id",
      "storageKey": null
    },
    {
      "alias": null,
      "args": null,
      "concreteType": null,
      "kind": "LinkedField",
      "name": "actors",
      "plural": true,
      "selections": [
        {
          "kind": "InlineFragment",
          "selections": [
            (v0/*: any*/),
            {
              "kind": "Stream",
              "selections": [
                {
                  "alias": null,
                  "args": null,
                  "concreteType": null,
                  "kind": "LinkedField",
                  "name": "actors",
                  "plural": true,
                  "selections": [
                    (v0/*: any*/)
                  ],
                  "storageKey": null
                }
              ]
            }
          ],
          "type": "User",
          "abstractKey": null
        }
      ],
      "storageKey": null
    }
  ],
  "type": "Feedback",
  "abstractKey": null
};
})();

if (__DEV__) {
  (node/*: any*/).hash = "8c389e04433fbf8aaa29700855a45351";
}

module.exports = ((node/*: any*/)/*: Fragment<
  RelayResponseNormalizerTest10Fragment$fragmentType,
  RelayResponseNormalizerTest10Fragment$data,
>*/);


text:
<!DOCTYPE html>

	<title>Tweet Sentiment to CSV</title>
	<meta name="viewport" content="width=device-width, initial-scale=1.0">

	<link href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/paper/bootstrap.min.css" rel="stylesheet" integrity="sha384-awusxf8AUojygHf2+joICySzB780jVvQaVCAt1clU3QsyAitLGul28Qxb2r1e5g+" crossorigin="anonymous">

Tweet Sentiment to CSV

Search for Tweets and download the data labeled with it's Polarity in CSV format

				<input type="text" id="query">
				<button id="search">Search</button>
				<button id="getcsv">Download CSV</button>

Loading...

			Made with ❤ | Github | Rohan Verma


text:
<?php

class testRuleDoesNotApplyToMethodArgumentUsedAsArrayIndex
{
    private static $staticAttributes = array();

    public function testRuleDoesNotApplyToMethodArgumentUsedAsArrayIndex(array $declaredClasses)
    {
        self::$staticAttributes[$declaredClasses[42]] = true;
    }
}