text:
const should = require('should');

const LinkedList = require('../src/collections/LinkedList');
const Stack = require('../src/collections/Stack');
const Queue = require('../src/collections/Queue');
const BinaryHeapArray = require('../src/collections/BinaryHeapArray');
const BinarySearchTree = require('../src/collections/BinarySearchTree');
const RedBlackTree = require('../src/collections/RedBlackTree');
const ChainedHashTable = require('../src/collections/ChainedHashTable');
const LinearHashTable = require('../src/collections/LinearHashTable');
const Trie = require('../src/collections/Trie');
const TernarySearchTrie = require('../src/collections/TernarySearchTrie');
const Graph = require('../src/collections/Graph');

describe('collections', () => {
    let collection;

    describe('LinkedList', () => {

        const variations = [
            {
                name: 'Standard Implementation',
                options: null
            },
            {
                name: 'OPTION: AVOID_RECURSION',
                options: {
                    AVOID_RECURSION: true
                }
            }
        ];

        variations.forEach(variation => {
            context(variation.name, () => {

                beforeEach(() => {
                    collection = new LinkedList(variation.options);
                });

                it('correctly adds items', () => {
                    for (let i = 0; i < 10; i++) {
                        collection.insert(i);
                    }

                    for (let i = 0; i < 10; i++) {
                        collection.contains(i).should.be.true();
                    }
                    collection.contains(10).should.be.false();

                    collection.count.should.eql(10);
                    collection.isCountCorrect().should.be.true();
                });

                it('correctly removes items', () => {
                    for (let i = 0; i < 10; i++) {
                        collection.insert(i);
                    }

                    for (let i = 9; i >= 0; i--) {
                        collection.count.should.eql(i + 1);
                        collection.remove(i);
                    }

                    collection.count.should.eql(0);
                    collection.isCountCorrect().should.be.true();
                });
            });
        });
    });

    describe('Stack', () => {

        beforeEach(() => {
            collection = new Stack();
        });

        it('pushes new items', () => {
            for (let i = 0; i < 10; i++) {
                collection.count.should.eql(i);
                collection.push(i);
            }
            collection.count.should.eql(10);

            collection.push(null);
            collection.count.should.eql(11);

            collection.push();
            collection.count.should.eql(11);

            collection.isCountCorrect().should.be.true();
        });

        it('pops stored items', () => {
            for (let i = 0; i < 10; i++) {
                collection.push(i);
            }
            collection.push(null);
            collection.count.should.eql(11);
            should(collection.pop()).be.null();

            for (let i = 9; i >= 0; i--) {
                collection.pop().should.eql(i);
                collection.count.should.eql(i);
            }

            collection.isCountCorrect().should.be.true();
        });
    });

    describe('Queue', () => {

        beforeEach(() => {
            collection = new Queue();
        });

        it('enqueues new items', () => {
            for (let i = 0; i < 10; i++) {
                collection.count.should.eql(i);
                collection.enqueue(i);
            }
            collection.count.should.eql(10);

            collection.enqueue(null);
            collection.count.should.eql(11);

            collection.enqueue();
            collection.count.should.eql(11);

            collection.isCountCorrect().should.be.true();
        });

        it('dequeues stored items', () => {
            collection.enqueue(null);
            for (let i = 0; i < 10; i++) {
                collection.enqueue(i);
            }
            collection.count.should.eql(11);

            should(collection.dequeue()).be.null();
            for (let i = 0; i < 10; i++) {
                collection.dequeue().should.eql(i);
                collection.count.should.eql(9 - i);
            }

            collection.isCountCorrect().should.be.true();
        });
    });

    describe('BinaryHeapArray', () => {
        beforeEach(() => {
            collection = new BinaryHeapArray();
        });

        it('insert()', () => {
            const input = new Array(5);
            for (let i = 0; i < input.length; i++) {
                input[i] = Math.round(100 * Math.random());
            }

            let sortedAuditArray = [];
            for (let val of input) {
                collection.insert(val);
                sortedAuditArray.push(val);
                sortedAuditArray.sort((a, b) => a - b).reverse();

                collection.count.should.eql(sortedAuditArray.length);
                collection.peek().should.eql(sortedAuditArray[0]);
            }

            collection.isCountCorrect().should.be.true();
        });

        it('remove()', () => {
            const sortedAuditArray = new Array(5);
            for (let i = 0; i < sortedAuditArray.length; i++) {
                const data = Math.round(100 * Math.random());
                sortedAuditArray[i] = data;
                collection.insert(data);
            }
            sortedAuditArray.sort((a, b) => a - b).reverse();

            for (let i = sortedAuditArray.length; i > 0; i--) {
                let maxValue = collection.remove();
                maxValue.should.eql(sortedAuditArray.shift());

                collection.count.should.eql(sortedAuditArray.length);
                should(collection.peek()).eql(sortedAuditArray[0]);
            }

            collection.count.should.eql(0);
            collection.isCountCorrect().should.be.true();
        });
    });

    describe('BinarySearchTree', () => {
        const CITIES_DATA = require('./data/cities.json');

        beforeEach(() => {
            collection = new BinarySearchTree();
        });

        it('gets and puts data', () => {
            collection.put('foo', 'bar');
            collection.get('foo').should.eql('bar');
            collection.count.should.eql(1);

            collection.put('foo', 123);
            collection.get('foo').should.eql(123);
            collection.count.should.eql(1);

            collection.put('test', 'ok');
            collection.get('foo').should.eql(123);
            collection.get('test').should.eql('ok');
            collection.count.should.eql(2);
        });

        it('can remove data', () => {
            collection.put('foo', 'bar');
            collection.count.should.eql(1);
            collection.remove('foo');
            collection.count.should.eql(0);

            collection.put('test2', 'ok');
            collection.put('test1', 'ok');
            collection.put('test3', 'ok');
            collection.count.should.eql(3);
            collection.remove('test2');
            collection.count.should.eql(2);
            collection.remove('test3');
            collection.count.should.eql(1);
            collection.remove('test1');
            collection.count.should.eql(0);

            for (let CITY of CITIES_DATA) {
                collection.put(CITY.name, CITY.population);
            }
            collection.count.should.eql(10);
            collection.remove('Ottawa');
            collection.remove('Toronto');
            collection.remove('London');
            collection.remove('Windsor');
            collection.count.should.eql(6);

        });

        it('should return min()', () => {
            for (let CITY of CITIES_DATA) {
                collection.put(CITY.population, CITY.name);
            }

            const min = collection.min();
            min.key.should.eql(210891);
            min.value.should.eql('Windsor');
        });

        it('should return max()', () => {
            for (let CITY of CITIES_DATA) {
                collection.put(CITY.population, CITY.name);
            }

            const max = collection.max();
            max.key.should.eql(2615060);
            max.value.should.eql('Toronto');
        });

        it('should select 3rd biggest', () => {
            for (let CITY of CITIES_DATA) {
                collection.put(CITY.population, CITY.name);
            }

            const selected = collection.select(2);
            selected.key.should.eql(288301);
            selected.value.should.eql('Vaughan');
        });

        it('should rank() an element', () => {
            for (let CITY of CITIES_DATA) {
                collection.put(CITY.name, CITY.population);
            }

            collection.rank('Kitchener').should.eql(2);
            collection.rank('Mississauga').should.eql(5);
            collection.rank('Markham').should.eql(4);
        });

        it('should get next biggest element', () => {
            for (let CITY of CITIES_DATA) {
                collection.put(CITY.name, CITY.population);
            }

            collection.nextBiggest('Kitchener').key.should.eql('London');
            collection.nextBiggest('Markham').key.should.eql('Mississauga');
            collection.nextBiggest('Brampton').key.should.eql('Hamilton');
            collection.nextBiggest('Oshawa').key.should.eql('Ottawa');
            collection.nextBiggest('Ajax').key.should.eql('Brampton');
            collection.nextBiggest('Vaughan').key.should.eql('Windsor');
            should(collection.nextBiggest('Woodstock')).be.undefined();

        });

        it('should get last biggest element', () => {
            for (let CITY of CITIES_DATA) {
                collection.put(CITY.name, CITY.population);
            }

            collection.lastBiggest('Kitchener').key.should.eql('Hamilton');
            collection.lastBiggest('Markham').key.should.eql('London');
            collection.lastBiggest('Hamilton').key.should.eql('Brampton');
            collection.lastBiggest('Windsor').key.should.eql('Vaughan');
            collection.lastBiggest('Milton').key.should.eql('Markham');
            collection.lastBiggest('Burlington').key.should.eql('Brampton');
            should(collection.lastBiggest('Brampton')).be.undefined();
        });
    });

    describe('RedBlackTree', () => {
        const CITIES_DATA = require('./data/cities.json');

        beforeEach(() => {
            collection = new RedBlackTree();
        });

        it('gets and puts data', () => {
            collection.put('foo', 'bar');
            collection.get('foo').should.eql('bar');
            collection.count.should.eql(1);

            collection.put('foo', 123);
            collection.get('foo').should.eql(123);
            collection.count.should.eql(1);

            collection.put('test', 'ok');
            collection.get('foo').should.eql(123);
            collection.get('test').should.eql('ok');
            collection.count.should.eql(2);
        });

        it.skip('can remove data', () => {
            collection.put('foo', 'bar');
            collection.count.should.eql(1);
            collection.remove('foo');
            collection.count.should.eql(0);

            collection.put('test2', 'ok');
            collection.put('test1', 'ok');
            collection.put('test3', 'ok');
            collection.count.should.eql(3);
            collection.remove('test2');
            collection.count.should.eql(2);
            collection.remove('test3');
            collection.count.should.eql(1);
            collection.remove('test1');
            collection.count.should.eql(0);
            console.log(collection.keys);

            for (let CITY of CITIES_DATA) {
                collection.put(CITY.name, CITY.population);
            }
            collection.count.should.eql(10);

            console.log(collection.keys);
            collection.remove('Ottawa');
            console.log(collection.keys);
            collection.remove('Toronto');
            collection.remove('London');
            collection.remove('Windsor');
            collection.count.should.eql(6);

        });
    });

    describe('ChainedHashTable', () => {
        const CITIES_DATA = require('./data/cities.json');

        beforeEach(() => {
            collection = new ChainedHashTable();
        });

        it('gets and puts data', () => {
            collection.put('foo', 'bar');
            collection.get('foo').should.eql('bar');
            collection.count.should.eql(1);

            collection.put('foo', 123);
            collection.get('foo').should.eql(123);
            collection.count.should.eql(1);

            collection.put('test', 'ok');
            collection.get('foo').should.eql(123);
            collection.get('test').should.eql('ok');
            collection.count.should.eql(2);
        });

        it('can remove data', () => {
            collection.put('foo', 'bar');
            collection.count.should.eql(1);
            collection.remove('foo');
            collection.count.should.eql(0);

            collection.put('test2', 'ok');
            collection.put('test1', 'ok');
            collection.put('test3', 'ok');
            collection.count.should.eql(3);
            collection.remove('test2');
            collection.count.should.eql(2);
            collection.remove('test3');
            collection.count.should.eql(1);
            collection.remove('test1');
            collection.count.should.eql(0);

            for (let CITY of CITIES_DATA) {
                collection.put(CITY.name, CITY.population);
            }
            collection.count.should.eql(10);
            collection.remove('Ottawa');
            collection.remove('Toronto');
            collection.remove('London');
            collection.remove('Windsor');
            collection.count.should.eql(6);

        });
    });

    describe('LinearHashTable', () => {
        const CITIES_DATA = require('./data/cities.json');

        beforeEach(() => {
            collection = new LinearHashTable();
        });

        it('gets and puts data', () => {
            collection.put('foo', 'bar');
            collection.get('foo').should.eql('bar');
            collection.count.should.eql(1);

            collection.put('foo', 123);
            collection.get('foo').should.eql(123);
            collection.count.should.eql(1);

            collection.put('test', 'ok');
            collection.get('foo').should.eql(123);
            collection.get('test').should.eql('ok');
            collection.count.should.eql(2);

            [1, 2, 3, 4, 5, 6, 7, 8].forEach(key => {
                collection.put('key' + key, key);
            });

            should.throws(() => {
                collection.put('FAIL', 'BECAUSE_ITS_AT_CAPACITY');
            });
        });

        it('can remove data', () => {
            collection.put('foo', 'bar');
            collection.count.should.eql(1);
            collection.remove('foo');
            collection.count.should.eql(0);

            collection.put('test2', 'ok');
            collection.put('test1', 'ok');
            collection.put('test3', 'ok');
            collection.count.should.eql(3);
            collection.remove('test2');
            collection.count.should.eql(2);
            collection.remove('test3');
            collection.count.should.eql(1);
            collection.remove('test1');
            collection.count.should.eql(0);

            for (let CITY of CITIES_DATA) {
                collection.put(CITY.name, CITY.population);
            }
            collection.count.should.eql(10);
            collection.remove('Ottawa');
            collection.remove('Toronto');
            collection.remove('London');
            collection.remove('Windsor');
            collection.count.should.eql(6);
        });
    });

    const TrieTypes = {Trie, TernarySearchTrie};

    for (let t in TrieTypes) {
        describe(t, () => {
            const DATA = ['lady', 'ladies', 'ladybug', 'ladybun', 'man', 'men', 'manbug', 'manbun', 'banana'];

            beforeEach(() => {
                collection = new TrieTypes[t]();
            });

            it('gets and puts data', () => {
                collection.put('foo', 'bar');
                collection.get('foo').should.eql('bar');
                collection.count.should.eql(1);

                collection.put('foo', 123);
                collection.get('foo').should.eql(123);
                collection.count.should.eql(1);

                collection.put('test', 'ok');
                collection.get('foo').should.eql(123);
                collection.get('test').should.eql('ok');
                collection.count.should.eql(2);

                for (let i in DATA) {
                    collection.put(DATA[i], i);
                }
                for (let i in DATA) {
                    collection.get(DATA[i]).should.eql(i);
                }
                collection.count.should.eql(11);
            });

            it('can remove data', () => {
                collection.put('foo', 'bar');
                collection.count.should.eql(1);
                collection.remove('foo');
                collection.count.should.eql(0);

                collection.put('test2', 'ok');
                collection.put('test1', 'ok');
                collection.put('test3', 'ok');
                collection.count.should.eql(3);
                collection.remove('test2');
                collection.count.should.eql(2);
                collection.remove('test3');
                collection.count.should.eql(1);
                collection.remove('test1');
                collection.count.should.eql(0);

                for (let i in DATA) {
                    collection.put(DATA[i], i);
                }
                collection.count.should.eql(9);
                collection.remove('man');
                collection.count.should.eql(8);
                collection.remove('manb');
                collection.count.should.eql(8);
                collection.remove('ladies');
                collection.remove('banana');
                collection.count.should.eql(6);
            });
        });
    }

    describe('Graph (undirected)', () => {

        beforeEach(() => {
            collection = new Graph(7, {mode: Graph.OPTIONS.UNDIRECTED});
        });

        it('creates a graph', () => {
            collection.count.should.eql(7);

            collection.addEdge(0, 1);
            collection.addEdge(0, 2);
            collection.addEdge(0, 5);
            collection.addEdge(0, 6);
            collection.addEdge(3, 5);
            collection.addEdge(3, 4);
            collection.addEdge(4, 5);
            collection.addEdge(4, 6);

            collection.addEdge(1, 2);
            collection.removeEdge(1, 2);

            collection.addVertex();
            collection.addEdge(1, 7);
            collection.addVertex();
            collection.addEdge(7, 8);
            collection.addEdge(8, 3);
            collection.count.should.eql(9);

            collection.removeVertex(1);
            collection.count.should.eql(8);
        });

        it('breadth first search / traversal', () => {
            collection.addEdge(0, 1);
            collection.addEdge(0, 2);
            collection.addEdge(0, 5);
            collection.addEdge(0, 6);
            collection.addEdge(3, 5);
            collection.addEdge(3, 4);
            collection.addEdge(4, 5);
            collection.addEdge(4, 6);
            collection.addVertex();
            collection.addEdge(3, 7);
            collection.addVertex('TREASURE!');
            collection.addEdge(7, 8);

            collection.breadthFirstSearch(0, 'TREASURE!').should.eql(true);

            collection.addVertex();
            collection.addVertex('VOID');
            collection.addEdge(9, 10);

            collection.breadthFirstSearch(0, 'VOID').should.eql(false);

            console.log(collection.breadthFirstTraversal(0).map(v => v.id).join());
        });

        it('depth first search / traversal', () => {
            collection.addEdge(0, 1);
            collection.addEdge(0, 2);
            collection.addEdge(0, 5);
            collection.addEdge(0, 6);
            collection.addEdge(3, 5);
            collection.addEdge(3, 4);
            collection.addEdge(4, 5);
            collection.addEdge(4, 6);
            collection.addVertex();
            collection.addEdge(3, 7);
            collection.addVertex('TREASURE!');
            collection.addEdge(7, 8);

            collection.depthFirstSearch(0, 'TREASURE!').should.eql(true);

            collection.addVertex();
            collection.addVertex('VOID');
            collection.addEdge(9, 10);

            collection.depthFirstSearch(0, 'VOID').should.eql(false);

            console.log(collection.depthFirstTraversal(0).map(v => v.id).join());
        });

        it('is connected?', () => {
            collection.addEdge(0, 1);
            collection.addEdge(0, 2);
            collection.addEdge(0, 5);
            collection.addEdge(0, 6);
            collection.addEdge(3, 5);
            collection.addEdge(3, 4);
            collection.addEdge(4, 5);
            collection.addEdge(4, 6);
            collection.addVertex();
            collection.addEdge(3, 7);
            collection.addVertex('TREASURE!');
            collection.addEdge(7, 8);
            collection.addVertex();
            collection.addVertex('VOID');
            collection.addEdge(9, 10);

            collection.isConnected(0, 8).should.eql(4);
            collection.isConnected(0, 10).should.eql(false);
            collection.isConnected(9, 10).should.eql(1);
        });
    });
});


text:
@protocol  GGTableHeaderDelegate<NSObject>

-(void)eventClickHeaderSection:(NSNumber*)section;

@end

@interface UIView(GGTableHeader)

@property(nonatomic, assign) NSInteger mIdx;
@property(nonatomic, weak) id<GGTableHeaderDelegate> mDelegate;
-(void)addTapGesture;

@end

@interface ViewController : UIViewController

@end

redpajama_set_name: RedPajamaGithub


text:
/* global describe, test, it, expect */

import cheerio from 'cheerio'

export default function ({ app }, suiteName, render) {
  async function get$ (path, query) {
    const html = await render(path, query)
    return cheerio.load(html)
  }

  describe(suiteName, () => {
    test('renders a stateless component', async () => {
      const html = await render('/stateless')
      expect(html.includes('<meta charSet="utf-8" class="next-head"/>')).toBeTruthy()
      expect(html.includes('My component!')).toBeTruthy()
    })

    test('renders a stateful component', async () => {
      const $ = await get$('/stateful')
      const answer = $('#answer')
      expect(answer.text()).toBe('The answer is 42')
    })

    test('header helper renders header information', async () => {
      const html = await (render('/head'))
      expect(html.includes('<meta charSet="iso-8859-5" class="next-head"/>')).toBeTruthy()
      expect(html.includes('<meta content="my meta" class="next-head"/>')).toBeTruthy()
      expect(html.includes('I can haz meta tags')).toBeTruthy()
    })

    test('renders styled jsx', async () => {
      const $ = await get$('/styled-jsx')
      const styleId = $('#blue-box').attr('class')
      const style = $('style')

      expect(style.text().includes(`p.${styleId}{color:blue`)).toBeTruthy()
    })

    test('renders properties populated asynchronously', async () => {
      const html = await render('/async-props')
      expect(html.includes('Diego Milito')).toBeTruthy()
    })

    test('renders a link component', async () => {
      const $ = await get$('/link')
      const link = $('a[href="/about"]')
      expect(link.text()).toBe('About')
    })

    test('getInitialProps resolves to null', async () => {
      const $ = await get$('/empty-get-initial-props')
      const expectedErrorMessage = '"EmptyInitialPropsPage.getInitialProps()" should resolve to an object. But found "null" instead.'
      expect($('pre').text().includes(expectedErrorMessage)).toBeTruthy()
    })

    test('allows to import .json files', async () => {
      const html = await render('/json')
      expect(html.includes('Zeit')).toBeTruthy()
    })

    test('default export is not a React Component', async () => {
      const $ = await get$('/no-default-export')
      const pre = $('pre')
      expect(pre.text()).toMatch(/The default export is not a React Component/)
    })

    test('error', async () => {
      const $ = await get$('/error')
      expect($('pre').text()).toMatch(/This is an expected error/)
    })

    test('asPath', async () => {
      const $ = await get$('/nav/as-path', { aa: 10 })
      expect($('.as-path-content').text()).toBe('/nav/as-path?aa=10')
    })

    test('error 404', async () => {
      const $ = await get$('/non-existent')
      expect($('h1').text()).toBe('404')
      expect($('h2').text()).toBe('This page could not be found.')
    })

    describe('with the HOC based router', () => {
      it('should navigate as expected', async () => {
        const $ = await get$('/nav/with-hoc')

        expect($('#pathname').text()).toBe('Current path: /nav/with-hoc')
      })

      it('should include asPath', async () => {
        const $ = await get$('/nav/with-hoc')

        expect($('#asPath').text()).toBe('Current asPath: /nav/with-hoc')
      })
    })
  })
}


text:
package alluxio.master;

import alluxio.Constants;
import alluxio.client.file.FileSystem;
import alluxio.client.file.FileSystemContext;
import alluxio.conf.Configuration;
import alluxio.conf.PropertyKey;
import alluxio.grpc.GrpcServerAddress;
import alluxio.grpc.GrpcServerBuilder;
import alluxio.grpc.JournalDomain;
import alluxio.master.job.JobMaster;
import alluxio.master.journal.DefaultJournalMaster;
import alluxio.master.journal.JournalSystem;
import alluxio.master.journal.JournalUtils;
import alluxio.master.journal.raft.RaftJournalSystem;
import alluxio.master.journal.ufs.UfsJournalSingleMasterPrimarySelector;
import alluxio.underfs.JobUfsManager;
import alluxio.underfs.UfsManager;
import alluxio.util.CommonUtils.ProcessType;
import alluxio.util.URIUtils;
import alluxio.util.network.NetworkAddressUtils.ServiceType;
import alluxio.web.JobMasterWebServer;
import alluxio.web.WebServer;

import com.google.common.base.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.concurrent.NotThreadSafe;
import javax.annotation.concurrent.ThreadSafe;

/**
 * This class is responsible for initializing the different masters that are configured to run.
 */
@NotThreadSafe
public class AlluxioJobMasterProcess extends AlluxioSimpleMasterProcess {
  private static final Logger LOG = LoggerFactory.getLogger(AlluxioJobMasterProcess.class);

  /** The master managing all job related metadata. */
  protected JobMaster mJobMaster;
  protected DefaultJournalMaster mJournalMaster;

  AlluxioJobMasterProcess(JournalSystem journalSystem, PrimarySelector leaderSelector) {
    super("job", JournalDomain.JOB_MASTER, journalSystem, leaderSelector,
        ServiceType.JOB_MASTER_WEB, ServiceType.JOB_MASTER_RPC, PropertyKey.JOB_MASTER_HOSTNAME);
    FileSystemContext fsContext = FileSystemContext.create(Configuration.global());
    FileSystem fileSystem = FileSystem.Factory.create(fsContext);
    UfsManager ufsManager = new JobUfsManager();
    try {
      MasterContext<UfsManager> context =
          new MasterContext<>(mJournalSystem, leaderSelector, null, ufsManager);
      // Create master.
      mJobMaster = new JobMaster(context, fileSystem, fsContext, ufsManager);
      mJournalMaster = new DefaultJournalMaster(JournalDomain.JOB_MASTER, context);
    } catch (Exception e) {
      LOG.error("Failed to create job master", e);
      throw new RuntimeException("Failed to create job master", e);
    }
  }

  @Override
  List<AbstractMaster> getAbstractMasters() {
    return Arrays.asList(mJobMaster, mJournalMaster);
  }

  @Override
  public <T extends Master> T getMaster(Class<T> clazz) {
    if (clazz == JobMaster.class) {
      return (T) mJobMaster;
    } else {
      throw new RuntimeException(String.format("Could not find the master: %s", clazz));
    }
  }

  /**
   * @return the {@link JobMaster} for this process
   */
  public JobMaster getJobMaster() {
    return mJobMaster;
  }

  @Override
  WebServer createWebServer() {
    return new JobMasterWebServer(ServiceType.JOB_MASTER_WEB.getServiceName(), mWebBindAddress,
              this);
  }

  @Override
  GrpcServerBuilder createBaseRPCServer() {
    return GrpcServerBuilder
        .forAddress(GrpcServerAddress.create(mRpcConnectAddress.getHostName(), mRpcBindAddress),
            Configuration.global())
        .flowControlWindow(
            (int) Configuration.getBytes(PropertyKey.JOB_MASTER_NETWORK_FLOWCONTROL_WINDOW))
        .keepAliveTime(Configuration.getMs(PropertyKey.JOB_MASTER_NETWORK_KEEPALIVE_TIME_MS),
            TimeUnit.MILLISECONDS)
        .keepAliveTimeout(
            Configuration.getMs(PropertyKey.JOB_MASTER_NETWORK_KEEPALIVE_TIMEOUT_MS),
            TimeUnit.MILLISECONDS)
        .permitKeepAlive(
            Configuration.getMs(PropertyKey.JOB_MASTER_NETWORK_PERMIT_KEEPALIVE_TIME_MS),
            TimeUnit.MILLISECONDS)
        .maxInboundMessageSize((int) Configuration
            .getBytes(PropertyKey.JOB_MASTER_NETWORK_MAX_INBOUND_MESSAGE_SIZE));
  }

  /**
   * Factory for creating {@link AlluxioJobMasterProcess}.
   */
  @ThreadSafe
  static final class Factory {
    /**
     * @return a new instance of {@link AlluxioJobMasterProcess}
     */
    public static AlluxioJobMasterProcess create() {
      URI journalLocation = JournalUtils.getJournalLocation();
      JournalSystem journalSystem = new JournalSystem.Builder()
          .setLocation(URIUtils.appendPathOrDie(journalLocation, Constants.JOB_JOURNAL_NAME))
          .build(ProcessType.JOB_MASTER);
      if (Configuration.getBoolean(PropertyKey.ZOOKEEPER_ENABLED)) {
        Preconditions.checkState(!(journalSystem instanceof RaftJournalSystem),
            "Raft journal cannot be used with Zookeeper enabled");
        PrimarySelector primarySelector = PrimarySelector.Factory.createZkJobPrimarySelector();
        return new AlluxioJobMasterProcess(journalSystem, primarySelector);
      } else if (journalSystem instanceof RaftJournalSystem) {
        PrimarySelector primarySelector = ((RaftJournalSystem) journalSystem).getPrimarySelector();
        return new AlluxioJobMasterProcess(journalSystem, primarySelector);
      }
      return new AlluxioJobMasterProcess(journalSystem,
          new UfsJournalSingleMasterPrimarySelector());
    }

    private Factory() {} // prevent instantiation
  }
}


text:
"""This module contains an object that represents a Telegram MessageEntity."""

from telegram import User, TelegramObject

class MessageEntity(TelegramObject):
    """
    This object represents one special entity in a text message. For example, hashtags,
    usernames, URLs, etc.

    Attributes:
        type (:obj:`str`): Type of the entity.
        offset (:obj:`int`): Offset in UTF-16 code units to the start of the entity.
        length (:obj:`int`): Length of the entity in UTF-16 code units.
        url (:obj:`str`): Optional. Url that will be opened after user taps on the text.
        user (:class:`telegram.User`): Optional. The mentioned user.

    Args:
        type (:obj:`str`): Type of the entity. Can be mention (@username), hashtag, bot_command,
            url, email, bold (bold text), italic (italic text), code (monowidth string), pre
            (monowidth block), text_link (for clickable text URLs), text_mention (for users
            without usernames).
        offset (:obj:`int`): Offset in UTF-16 code units to the start of the entity.
        length (:obj:`int`): Length of the entity in UTF-16 code units.
        url (:obj:`str`, optional): For "text_link" only, url that will be opened after usertaps on
            the text.
        user (:class:`telegram.User`, optional): For "text_mention" only, the mentioned user.

    """

    def __init__(self, type, offset, length, url=None, user=None, **kwargs):
        # Required
        self.type = type
        self.offset = offset
        self.length = length
        # Optionals
        self.url = url
        self.user = user

    @classmethod
    def de_json(cls, data, bot):
        data = super(MessageEntity, cls).de_json(data, bot)

        if not data:
            return None

        data['user'] = User.de_json(data.get('user'), bot)

        return cls(**data)

    @classmethod
    def de_list(cls, data, bot):
        if not data:
            return list()

        entities = list()
        for entity in data:
            entities.append(cls.de_json(entity, bot))

        return entities

    MENTION = 'mention'
    """:obj:`str`: 'mention'"""
    HASHTAG = 'hashtag'
    """:obj:`str`: 'hashtag'"""
    BOT_COMMAND = 'bot_command'
    """:obj:`str`: 'bot_command'"""
    URL = 'url'
    """:obj:`str`: 'url'"""
    EMAIL = 'email'
    """:obj:`str`: 'email'"""
    BOLD = 'bold'
    """:obj:`str`: 'bold'"""
    ITALIC = 'italic'
    """:obj:`str`: 'italic'"""
    CODE = 'code'
    """:obj:`str`: 'code'"""
    PRE = 'pre'
    """:obj:`str`: 'pre'"""
    TEXT_LINK = 'text_link'
    """:obj:`str`: 'text_link'"""
    TEXT_MENTION = 'text_mention'
    """:obj:`str`: 'text_mention'"""
    ALL_TYPES = [
        MENTION, HASHTAG, BOT_COMMAND, URL, EMAIL, BOLD, ITALIC, CODE, PRE, TEXT_LINK, TEXT_MENTION
    ]
    """List[:obj:`str`]: List of all the types."""


text:
using System;
using Newtonsoft.Json;

namespace IronFoundry.VcapClient.V2.Models
{
    public class Event
    {
        [JsonProperty(PropertyName = "type")]
        public string TypeName { get; set; }

        [JsonProperty(PropertyName = "actor")]
        public Guid ActorId { get; set; }

        [JsonProperty(PropertyName = "actor_type")]
        public string ActorType { get; set; }

        [JsonProperty(PropertyName = "actee")]
        public Guid ActionId { get; set; }

        [JsonProperty(PropertyName = "actee_type")]
        public string ActionType { get; set; }

        [JsonProperty(PropertyName = "timestamp")]
        public DateTimeOffset TimeStamp { get; set; }

        [JsonProperty(PropertyName = "space_guid")]
        public Guid SpaceId { get; set; }

        [JsonProperty(PropertyName = "space_url")]
        public string SpaceUrl { get; set; }

        [JsonProperty(PropertyName = "metadata")]
        public EventMetadata MetadataEvent { get; set; }

        #region Nested classes
        public class EventMetadata
        {
            [JsonProperty(PropertyName = "changes")]
            public ChangedData ChangedData { get; set; }

            [JsonProperty(PropertyName = "footprints")]
            public FootPrint FootPrint { get; set; }

        }

        public class ChangedData
        {
            [JsonProperty(PropertyName = "environment_json")]
            public string EnvironmentInfo { get; set; }

            [JsonProperty(PropertyName = "instances")]
            public int NumberInstance { get; set; }

            [JsonProperty(PropertyName = "memory")]
            public long Memory { get; set; }

            [JsonProperty(PropertyName = "state")]
            public string State { get; set; }

            [JsonProperty(PropertyName = "name")]
            public string Name { get; set; }
        }

        public class FootPrint
        {
            [JsonProperty(PropertyName = "instances")]
            public int NumberInstance { get; set; }

            [JsonProperty(PropertyName = "memory")]
            public long Memory { get; set; }
        }
        #endregion
    }
}


text:
require 'test_helper'

class OpRenewCertRequestSoapBuilderTest < ActiveSupport::TestCase
  setup do
    @params = op_renew_certificate_params

    # Convert the keys here since the conversion is usually done by the client and these tests
    # bypass the client
    @params[:own_signing_certificate] = x509_certificate(@params[:own_signing_certificate])
    @params[:signing_private_key]     = rsa_key(@params[:signing_private_key])

    @doc = Nokogiri::XML(Sepa::SoapBuilder.new(@params).to_xml)
  end

  test "validates against schema" do
    assert_valid_against_schema 'soap.xsd', @doc
  end

  test 'application request is inserted properly' do
    ar_node = @doc.at("xmlns|ApplicationRequest", xmlns: 'http://mlp.op.fi/OPCertificateService')
    ar_doc  = Nokogiri::XML(decode(ar_node.content))

    assert ar_doc.respond_to?(:canonicalize)
    assert_equal @params[:customer_id], ar_doc.at_css("CustomerId").content
  end
end


text:
package com.epam.reportportal.auth.integration.github;

import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.OAuth2Request;
import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;

import java.util.Map;

import static java.util.Optional.ofNullable;

/**
 * @author Ivan Budayeu
 */
public class ExternalOauth2TokenConverter extends DefaultAccessTokenConverter {

	static final String UPSTREAM_TOKEN = "upstream_token";

	@Override
	public Map<String, ?> convertAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
		Map<String, Object> params = (Map<String, Object>) super.convertAccessToken(token, authentication);
		ofNullable(authentication.getOAuth2Request()).map(r -> r.getExtensions().get(UPSTREAM_TOKEN))
				.ifPresent(uToken -> params.put(UPSTREAM_TOKEN, String.valueOf(uToken)));
		return params;
	}

	@Override
	public OAuth2Authentication extractAuthentication(Map<String, ?> map) {
		OAuth2Authentication oAuth2Authentication = super.extractAuthentication(map);
		ofNullable(oAuth2Authentication.getOAuth2Request()).map(OAuth2Request::getExtensions)
				.ifPresent(extensions -> ofNullable(map.get(UPSTREAM_TOKEN)).map(String::valueOf)
						.ifPresent(token -> extensions.put(UPSTREAM_TOKEN, token)));
		return oAuth2Authentication;
	}
}


text:
Mailpile.focus_search = function() {
    $("#search-query").focus(); return false;
};

/* Search - Action Select */
Mailpile.pile_action_select = function(item) {

    // Add To Data Model
    Mailpile.bulk_cache_add('messages_cache', item.data('mid'));

    // Add Tags
    var metadata = _.findWhere(Mailpile.instance.metadata, { mid: item.attr('data-mid') });
    if (metadata && metadata.tag_tids) {
      _.each(metadata.tag_tids, function(tid, key) {
        var tag = _.findWhere(Mailpile.instance.tags, { tid: tid });
        if (tag.type === 'tag') {
          if (_.indexOf(Mailpile.tags_cache, tag.tid) === -1) {
            Mailpile.tags_cache.push(tag.tid);
          }
        }
      });
    }

    // Update Bulk UI
    Mailpile.bulk_actions_update_ui();

    // Style & Select Checkbox
    item.removeClass('result').addClass('result-on')
        .data('state', 'selected')
        .find('td.checkbox input[type=checkbox]')
        .val('selected')
        .prop('checked', true);
};

/* Search - Action Unselect */
Mailpile.pile_action_unselect = function(item) {

    // Remove From Data Model
    Mailpile.bulk_cache_remove('messages_cache', item.data('mid'));

    // Remove Tags
    var metadata = _.findWhere(Mailpile.instance.metadata, { mid: item.attr('data-mid') });
    if (metadata && metadata.tag_tids) {
      _.each(metadata.tag_tids, function(tid, key) {
        var tag = _.findWhere(Mailpile.instance.tags, { tid: tid });
        if (tag.type === 'tag') {
          if (_.indexOf(Mailpile.tags_cache, tag.tid) > -1) {
            Mailpile.tags_cache = _.without(Mailpile.tags_cache, tag.tid);
          }
        }
      });
    }

    // Hide Actions
    Mailpile.bulk_actions_update_ui();

    // Style & Unselect Checkbox
    item.removeClass('result-on').addClass('result')
        .data('state', 'normal')
        .find('td.checkbox input[type=checkbox]')
        .val('normal')
        .prop('checked', false);
};

/* Search - Result List */
Mailpile.results_list = function() {
    // Navigation
    $('#btn-display-list').addClass('navigation-on');
    $('#btn-display-graph').removeClass('navigation-on');

    // Show & Hide View
    $('#pile-graph').hide('fast', function() {
        $('#form-pile-results').show('normal');
        $('#pile-results').show('fast');
        $('.pile-speed').show('normal');
        $('#footer').show('normal');
    });
};

Mailpile.update_search = function(ev) {
    $("#pile-newmessages-notification").slideUp("slow");
    console.log("Refreshing ", Mailpile.instance);
    url = "/api/0/search/as.jhtml?" + $.param(Mailpile.instance.state.query_args);
    $.getJSON(url, {}, function(data) {
        if (data.status == "success") {
            $("#content-view").html(data.result);
        }
        console.log(data);
    });
};

Mailpile.render_modal_tags = function() {
  if (Mailpile.messages_cache.length) {

    // Open Modal with selection options
    Mailpile.API.tags_get({}, function(data) {

      var tag_template = _.template($('#template-modal-tag-picker-item').html());
      var priority_html = '';
      var tags_html     = '';
      var archive_html  = '';

      /// Show tags in selected messages
      var selected_tids = {};
      _.each(Mailpile.messages_cache, function(mid, key) {
        var metadata = _.findWhere(Mailpile.instance.metadata, { mid: mid });
        if (metadata && metadata.tag_tids) {
          _.each(metadata.tag_tids, function(tid, key) {
            if (selected_tids[tid] === undefined) {
              selected_tids[tid] = 1;
            } else {
              selected_tids[tid]++;
            }
          });
        }
      });

      // Build Tags List
      _.each(data.result.tags, function(tag, key) {
        if (tag.display === 'priority' && tag.type === 'tag') {
          priority_data  = _.extend(tag, { selected: selected_tids });
          priority_html += tag_template(priority_data);
        }
        else if (tag.display === 'tag' && tag.type === 'tag') {
          tag_data   = _.extend(tag, { selected: selected_tids });
          tags_html += tag_template(tag_data);
        }
        else if (tag.display === 'archive' && tag.type === 'tag') {
          archive_data  = _.extend(tag, { selected: selected_tids });
          archive_html += tag_template(archive_data);
        }
      });

      var modal_template = _.template($("#modal-tag-picker").html());
      $('#modal-full').html(modal_template({ priority: priority_html, tags: tags_html, archive: archive_html }));
      $('#modal-full').modal(Mailpile.UI.ModalOptions);
    });

  } else {
    Mailpile.notification({ status: 'info', message: '{{_("No Messages Selected")}}' });
  }
};

Mailpile.UI.Search.Draggable = function(element) {
  $(element).draggable({
    containment: 'body',
    appendTo: 'body',
    cursor: 'move',
    scroll: false,
    revert: false,
    opacity: 1,
    helper: function(event) {
      // FIXME: the word 'message' needs to updated as per Issue #666 mwhuahahaha
      if (Mailpile.messages_cache.length == 0) {
        drag_count = '1 message
';
      } else {
        drag_count = Mailpile.messages_cache.length + ' messages';
      }
      return $('
 Moving ' + drag_count + '
');
    },
    start: function(event, ui) {

      // Add Draggable MID
      Mailpile.bulk_cache_add('messages_cache', $(event.target).parent().data('mid'));

      // Update Bulk UI
      Mailpile.bulk_actions_update_ui();

    	// Style & Select Checkbox
    	$(event.target).parent().removeClass('result').addClass('result-on')
    	.data('state', 'selected')
    	.find('td.checkbox input[type=checkbox]')
    	.val('selected')
    	.prop('checked', true);
    },
    stop: function(event, ui) {}
  });
};

Mailpile.UI.Search.Dropable = function(element, accept) {
  $(element).droppable({
    accept: accept,
    hoverClass: 'result-hover',
    tolerance: 'pointer',
    drop: function(event, ui) {

      // Update Cache
      Mailpile.bulk_cache_add('messages_cache', $(event.target).data('mid'));

      // Save Update
      Mailpile.API.tag_post({ add: ui.draggable.data('tid'), mid: Mailpile.messages_cache }, function() {

        var tag = _.findWhere(Mailpile.instance.tags, { tid: ui.draggable.data('tid').toString() });
        var hex = Mailpile.theme.colors[tag.label_color];
        var updated = [];

        // Update Multiple Selected Messages
        if (Mailpile.messages_cache.length > 0) {
          $.each(Mailpile.messages_cache, function(key, mid) {
            updated.push(mid);
            $('#pile-message-' + mid).find('td.subject span.item-tags').append(' ' + tag.name + '');
          });
        }
      });
    }
  });
};


text:
<?php
return array(
	'clientid'=> array('client','clientid','CASCADE','CASCADE'),
);


text:
SpreeInvoiceGenerator.setup do |config|
  # Do not use this payment methods.
  config.except_payment        = ['Spree::PaymentMethod::Check']

  # Wkhtmltopdf binary file. Check by system command: which wkhtmltopdf.
  config.wkhtmltopdf_binary    = "/usr/bin/wkhtmltopdf"

  # Options which will be passed to wkhtmltopdf.
  config.wkhtmltopdf_options   = {
    template: "spree/invoice_prints/show.html.erb",
    margin: {
      top:    10,
      bottom: 10,
      left:   15,
      right:  15
    }
  }
end


text:
@interface PodsDummy_Pods_PNImagePickerViewController_Example : NSObject
@end
@implementation PodsDummy_Pods_PNImagePickerViewController_Example
@end


text:
SoftLayer API Python Client
===========================
.. image:: https://travis-ci.org/softlayer/softlayer-python.svg?branch=master
    :target: https://travis-ci.org/softlayer/softlayer-python

.. image:: https://landscape.io/github/softlayer/softlayer-python/master/landscape.svg
    :target: https://landscape.io/github/softlayer/softlayer-python/master

.. image:: https://badge.fury.io/py/SoftLayer.svg
    :target: http://badge.fury.io/py/SoftLayer

.. image:: https://coveralls.io/repos/github/softlayer/softlayer-python/badge.svg?branch=master
    :target: https://coveralls.io/github/softlayer/softlayer-python?branch=master

.. image:: https://build.snapcraft.io/badge/softlayer/softlayer-python.svg
    :target: https://build.snapcraft.io/user/softlayer/softlayer-python

This library provides a simple Python client to interact with `SoftLayer's
XML-RPC API <https://softlayer.github.io/reference/softlayerapi>`_.

A command-line interface is also included and can be used to manage various
SoftLayer products and services.

Documentation
-------------
Documentation for the Python client is available at
http://softlayer.github.io/softlayer-python/.

Additional API documentation can be found on the SoftLayer Development Network:

* `SoftLayer API reference
  <https://sldn.softlayer.com/reference/softlayerapi>`_
* `Object mask information and examples
  <https://sldn.softlayer.com/article/object-masks>`_
* `Code Examples
  <https://softlayer.github.io/python/>`_

Installation
------------
Install via pip:

.. code-block:: bash

	$ pip install softlayer

Or you can install from source. Download source and run:

.. code-block:: bash

	$ python setup.py install

Another (safer) method of installation is to use the published snap. Snaps are available for any Linux OS running snapd, the service that runs and manage snaps. Snaps are "auto-updating" packages and will not disrupt the current versions of libraries and software packages on your Linux-based system. To learn more, please visit: https://snapcraft.io/

To install the slcli snap:

.. code-block:: bash

	$ sudo snap install slcli

The most up-to-date version of this library can be found on the SoftLayer
GitHub public repositories at http://github.com/softlayer. For questions regarding the use of this library please post to Stack Overflow at https://stackoverflow.com/ and  your posts with "SoftLayer" so our team can easily find your post. To report a bug with this library please create an Issue on github.

InsecurePlatformWarning Notice
------------------------------
This library relies on the `requests <http://docs.python-requests.org/>`_ library to make HTTP requests. On Python versions below Python 2.7.9, requests has started emitting a security warning (InsecurePlatformWarning) due to insecurities with creating SSL connections. To resolve this, upgrade to Python 2.7.9+ or follow the instructions here: http://stackoverflow.com/a/29099439.

Getting Help
------------
Bugs and feature requests about this library should have a `GitHub issue <https://github.com/softlayer/softlayer-python/issues>`_ opened about them.

Issues with the Softlayer API itself should be addressed by opening a ticket.

Examples
--------

A curated list of examples on how to use this library can be found at `softlayer.github.io <https://softlayer.github.io/python/>`_

Debugging
---------
To get the exact API call that this library makes, you can do the following.

For the CLI, just use the -vvv option. If you are using the REST endpoint, this will print out a curl command that you can use, if using XML, this will print the minimal python code to make the request without the softlayer library.

.. code-block:: bash

  $ slcli -vvv vs list

If you are using the library directly in python, you can do something like this.

.. code-bock:: python

  import SoftLayer
  import logging

  class invoices():

      def __init__(self):
          self.client = SoftLayer.Client()
          debugger = SoftLayer.DebugTransport(self.client.transport)
          self.client.transport = debugger

      def main(self):
          mask = "mask[id]"
          account = self.client.call('Account', 'getObject', mask=mask);
          print("AccountID: %s" % account['id'])

      def debug(self):
          for call in self.client.transport.get_last_calls():
              print(self.client.transport.print_reproduceable(call))

  if __name__ == "__main__":
      main = example()
      main.main()
      main.debug()

System Requirements
-------------------
* Python 2.7, 3.3, 3.4, 3.5, 3.6, or 3.7.
* A valid SoftLayer API username and key.
* A connection to SoftLayer's private network is required to use
  our private network API endpoints.

Python Packages
---------------
* six >= 1.7.0
* ptable >= 0.9.2
* click >= 7
* requests >= 2.20.0
* prompt_toolkit >= 2
* pygments >= 2.0.0
* urllib3 >= 1.24

Copyright
---------
This software is Copyright (c) 2016-2018 SoftLayer Technologies, Inc.

See the bundled LICENSE file for more information.


text:
module Zuora
  class InvoicePayment < ZObject
    def invoice
      @invoice ||= Invoice.find(self.invoiceId)
    end

    def payment
      @payment ||= Payment.find(self.paymentId)
    end
  end
end


text:
package org.apache.sentry.core.model.indexer;

import org.apache.sentry.core.common.Authorizable;

public interface IndexerModelAuthorizable extends Authorizable {

  public enum AuthorizableType {
    Indexer
  };

  public AuthorizableType getAuthzType();
}


text:
Slipmat.Models.Record = Backbone.Model.extend({

  proto: "Record",
  urlRoot: "api/records",
  subview: JST["records/_record"],

  parse: function (payload) {
    var associations = [
          "artist",
          "label"
        ],
        attributes = [
          "image",
          "country",
          "genres",
          "tracks",
          "comments",
          "contributors",
          "num_collected",
          "num_wanted"
        ];

    associations.forEach(association => {
      if (payload[association]) {
        this[association]().set(payload[association]);
        delete payload[association];
      }
    });

    attributes.forEach(attribute => {
      if (payload[attribute]) {
        this["_" + attribute] = payload[attribute];
        delete payload[attribute];
      }
    });

    return payload;
  },

  save: function (key, val, options) {
    var year;

    if (key.record && key.record.year) {
      year = Number(key.record.year);
      if (!year) {
        delete key.record.year;
      } else {
        key.record.year = year;
      }
    }
    return Backbone.Model.prototype.save.call(this, key, val, options);
  },

  image: function () {
    this._image = this._image || Slipmat.defaultRecordImg;
    return this._image;
  },

  artist: function () {
    this._artist = this._artist || new Slipmat.Models.Artist();
    return this._artist;
  },

  label: function () {
    this._label = this._label || new Slipmat.Models.Label();
    return this._label;
  },

  country: function () {
    this._country = this._country || [];
    return this._country;
  },

  genres: function () {
    this._genres = this._genres || [];
    return this._genres;
  },

  tracks: function () {
    this._tracks = this._tracks || [];
    return this._tracks;
  },

  comments: function () {
    this._comments = this._comments || [];
    return this._comments;
  },

  contributors: function () {
    this._contributors = this._contributors || [];
    return this._contributors;
  },

  numCollected: function () {
    this._num_collected = this._num_collected || 0;
    return this._num_collected;
  },

  numWanted: function () {
    this._num_wanted = this._num_wanted || 0;
    return this._num_wanted;
  }

});


text:
#import "AWSRequest.h"

@class NSString;

@interface AWSS3GetBucketVersioningRequest : AWSRequest {
	NSString* _bucket;
}
@property(retain, nonatomic) NSString* bucket;
+ (id)JSONKeyPathsByPropertyKey;
- (void).cxx_destruct;
@end


text:
package org.apache.accumulo.monitor;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.accumulo.fate.util.UtilWaitThread.sleepUninterruptibly;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

import org.apache.accumulo.core.Constants;
import org.apache.accumulo.core.client.Connector;
import org.apache.accumulo.core.client.Instance;
import org.apache.accumulo.core.client.impl.MasterClient;
import org.apache.accumulo.core.client.impl.Table;
import org.apache.accumulo.core.conf.Property;
import org.apache.accumulo.core.conf.SiteConfiguration;
import org.apache.accumulo.core.gc.thrift.GCMonitorService;
import org.apache.accumulo.core.gc.thrift.GCStatus;
import org.apache.accumulo.core.master.thrift.MasterClientService;
import org.apache.accumulo.core.master.thrift.MasterMonitorInfo;
import org.apache.accumulo.core.master.thrift.TableInfo;
import org.apache.accumulo.core.master.thrift.TabletServerStatus;
import org.apache.accumulo.core.rpc.ThriftUtil;
import org.apache.accumulo.core.tabletserver.thrift.ActiveScan;
import org.apache.accumulo.core.tabletserver.thrift.TabletClientService.Client;
import org.apache.accumulo.core.trace.DistributedTrace;
import org.apache.accumulo.core.trace.Tracer;
import org.apache.accumulo.core.util.Daemon;
import org.apache.accumulo.core.util.HostAndPort;
import org.apache.accumulo.core.util.Pair;
import org.apache.accumulo.core.util.ServerServices;
import org.apache.accumulo.core.util.ServerServices.Service;
import org.apache.accumulo.core.zookeeper.ZooUtil;
import org.apache.accumulo.fate.util.LoggingRunnable;
import org.apache.accumulo.fate.zookeeper.ZooLock.LockLossReason;
import org.apache.accumulo.fate.zookeeper.ZooUtil.NodeExistsPolicy;
import org.apache.accumulo.fate.zookeeper.ZooUtil.NodeMissingPolicy;
import org.apache.accumulo.server.Accumulo;
import org.apache.accumulo.server.AccumuloServerContext;
import org.apache.accumulo.server.HighlyAvailableService;
import org.apache.accumulo.server.ServerOpts;
import org.apache.accumulo.server.client.HdfsZooInstance;
import org.apache.accumulo.server.conf.ServerConfigurationFactory;
import org.apache.accumulo.server.fs.VolumeManager;
import org.apache.accumulo.server.fs.VolumeManagerImpl;
import org.apache.accumulo.server.metrics.MetricsSystemHelper;
import org.apache.accumulo.server.monitor.LogService;
import org.apache.accumulo.server.problems.ProblemReports;
import org.apache.accumulo.server.problems.ProblemType;
import org.apache.accumulo.server.security.SecurityUtil;
import org.apache.accumulo.server.util.Halt;
import org.apache.accumulo.server.util.TableInfoUtil;
import org.apache.accumulo.server.util.time.SimpleTimer;
import org.apache.accumulo.server.zookeeper.ZooLock;
import org.apache.accumulo.server.zookeeper.ZooReaderWriter;
import org.apache.zookeeper.KeeperException;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.resource.Resource;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.logging.LoggingFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import org.glassfish.jersey.server.mvc.MvcFeature;
import org.glassfish.jersey.server.mvc.freemarker.FreemarkerMvcFeature;
import org.glassfish.jersey.servlet.ServletContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Serve master statistics with an embedded web server.
 */
public class Monitor implements HighlyAvailableService {
  private static final Logger log = LoggerFactory.getLogger(Monitor.class);

  private static final int REFRESH_TIME = 5;
  private static AtomicLong lastRecalc = new AtomicLong(0L);
  private static double totalIngestRate = 0.0;
  private static double totalQueryRate = 0.0;
  private static double totalScanRate = 0.0;
  private static long totalEntries = 0L;
  private static int totalTabletCount = 0;
  private static long totalHoldTime = 0;
  private static long totalLookups = 0;
  private static int totalTables = 0;
  public static HighlyAvailableService HA_SERVICE_INSTANCE = null;
  private static final AtomicBoolean monitorInitialized = new AtomicBoolean(false);

  private static class MaxList<T> extends LinkedList<Pair<Long,T>> {
    private static final long serialVersionUID = 1L;

    private long maxDelta;

    public MaxList(long maxDelta) {
      this.maxDelta = maxDelta;
    }

    @Override
    public boolean add(Pair<Long,T> obj) {
      boolean result = super.add(obj);

      if (obj.getFirst() - get(0).getFirst() > maxDelta)
        remove(0);

      return result;
    }

  }

  private static final int MAX_TIME_PERIOD = 60 * 60 * 1000;
  private static final List<Pair<Long,Double>> loadOverTime = Collections.synchronizedList(new MaxList<Double>(MAX_TIME_PERIOD));
  private static final List<Pair<Long,Double>> ingestRateOverTime = Collections.synchronizedList(new MaxList<Double>(MAX_TIME_PERIOD));
  private static final List<Pair<Long,Double>> ingestByteRateOverTime = Collections.synchronizedList(new MaxList<Double>(MAX_TIME_PERIOD));
  private static final List<Pair<Long,Integer>> minorCompactionsOverTime = Collections.synchronizedList(new MaxList<Integer>(MAX_TIME_PERIOD));
  private static final List<Pair<Long,Integer>> majorCompactionsOverTime = Collections.synchronizedList(new MaxList<Integer>(MAX_TIME_PERIOD));
  private static final List<Pair<Long,Double>> lookupsOverTime = Collections.synchronizedList(new MaxList<Double>(MAX_TIME_PERIOD));
  private static final List<Pair<Long,Integer>> queryRateOverTime = Collections.synchronizedList(new MaxList<Integer>(MAX_TIME_PERIOD));
  private static final List<Pair<Long,Integer>> scanRateOverTime = Collections.synchronizedList(new MaxList<Integer>(MAX_TIME_PERIOD));
  private static final List<Pair<Long,Double>> queryByteRateOverTime = Collections.synchronizedList(new MaxList<Double>(MAX_TIME_PERIOD));
  private static final List<Pair<Long,Double>> indexCacheHitRateOverTime = Collections.synchronizedList(new MaxList<Double>(MAX_TIME_PERIOD));
  private static final List<Pair<Long,Double>> dataCacheHitRateOverTime = Collections.synchronizedList(new MaxList<Double>(MAX_TIME_PERIOD));
  private static EventCounter lookupRateTracker = new EventCounter();
  private static EventCounter indexCacheHitTracker = new EventCounter();
  private static EventCounter indexCacheRequestTracker = new EventCounter();
  private static EventCounter dataCacheHitTracker = new EventCounter();
  private static EventCounter dataCacheRequestTracker = new EventCounter();

  private static volatile boolean fetching = false;
  private static MasterMonitorInfo mmi;
  private static Map<Table.ID,Map<ProblemType,Integer>> problemSummary = Collections.emptyMap();
  private static Exception problemException;
  private static GCStatus gcStatus;

  private static Instance instance;

  private static ServerConfigurationFactory config;
  private static AccumuloServerContext context;

  private static EmbeddedWebServer server;

  private ZooLock monitorLock;

  private static final String DEFAULT_INSTANCE_NAME = "(Unavailable)";
  public static final AtomicReference<String> cachedInstanceName = new AtomicReference<>(DEFAULT_INSTANCE_NAME);

  private static class EventCounter {

    Map<String,Pair<Long,Long>> prevSamples = new HashMap<>();
    Map<String,Pair<Long,Long>> samples = new HashMap<>();
    Set<String> serversUpdated = new HashSet<>();

    void startingUpdates() {
      serversUpdated.clear();
    }

    void updateTabletServer(String name, long sampleTime, long numEvents) {
      Pair<Long,Long> newSample = new Pair<>(sampleTime, numEvents);
      Pair<Long,Long> lastSample = samples.get(name);

      if (lastSample == null || !lastSample.equals(newSample)) {
        samples.put(name, newSample);
        if (lastSample != null) {
          prevSamples.put(name, lastSample);
        }
      }
      serversUpdated.add(name);
    }

    void finishedUpdating() {
      // remove any tablet servers not updated
      samples.keySet().retainAll(serversUpdated);
      prevSamples.keySet().retainAll(serversUpdated);
    }

    double calculateRate() {
      double totalRate = 0;

      for (Entry<String,Pair<Long,Long>> entry : prevSamples.entrySet()) {
        Pair<Long,Long> prevSample = entry.getValue();
        Pair<Long,Long> sample = samples.get(entry.getKey());

        totalRate += (sample.getSecond() - prevSample.getSecond()) / ((sample.getFirst() - prevSample.getFirst()) / (double) 1000);
      }

      return totalRate;
    }

    long calculateCount() {
      long count = 0;

      for (Entry<String,Pair<Long,Long>> entry : prevSamples.entrySet()) {
        Pair<Long,Long> prevSample = entry.getValue();
        Pair<Long,Long> sample = samples.get(entry.getKey());

        count += sample.getSecond() - prevSample.getSecond();
      }

      return count;
    }
  }

  public static void fetchData() {
    double totalIngestRate = 0.;
    double totalIngestByteRate = 0.;
    double totalQueryRate = 0.;
    double totalQueryByteRate = 0.;
    double totalScanRate = 0.;
    long totalEntries = 0;
    int totalTabletCount = 0;
    long totalHoldTime = 0;
    long totalLookups = 0;
    boolean retry = true;

    // only recalc every so often
    long currentTime = System.currentTimeMillis();
    if (currentTime - lastRecalc.get() < REFRESH_TIME * 1000)
      return;

    synchronized (Monitor.class) {
      // Learn our instance name asynchronously so we don't hang up if zookeeper is down
      if (cachedInstanceName.get().equals(DEFAULT_INSTANCE_NAME)) {
        SimpleTimer.getInstance(config.getSystemConfiguration()).schedule(new TimerTask() {
          @Override
          public void run() {
            synchronized (Monitor.class) {
              if (cachedInstanceName.get().equals(DEFAULT_INSTANCE_NAME)) {
                final String instanceName = HdfsZooInstance.getInstance().getInstanceName();
                if (null != instanceName) {
                  cachedInstanceName.set(instanceName);
                }
              }
            }
          }
        }, 0);
      }
    }

    synchronized (Monitor.class) {
      if (fetching)
        return;
      fetching = true;
    }

    try {
      while (retry) {
        MasterClientService.Iface client = null;
        try {
          client = MasterClient.getConnection(context);
          if (client != null) {
            mmi = client.getMasterStats(Tracer.traceInfo(), context.rpcCreds());
            retry = false;
          } else {
            mmi = null;
          }
          Monitor.gcStatus = fetchGcStatus();
        } catch (Exception e) {
          mmi = null;
          log.info("Error fetching stats: ", e);
        } finally {
          if (client != null) {
            MasterClient.close(client);
          }
        }
        if (mmi == null)
          sleepUninterruptibly(1, TimeUnit.SECONDS);
      }
      if (mmi != null) {
        int majorCompactions = 0;
        int minorCompactions = 0;

        lookupRateTracker.startingUpdates();
        indexCacheHitTracker.startingUpdates();
        indexCacheRequestTracker.startingUpdates();
        dataCacheHitTracker.startingUpdates();
        dataCacheRequestTracker.startingUpdates();

        for (TabletServerStatus server : mmi.tServerInfo) {
          TableInfo summary = TableInfoUtil.summarizeTableStats(server);
          totalIngestRate += summary.ingestRate;
          totalIngestByteRate += summary.ingestByteRate;
          totalQueryRate += summary.queryRate;
          totalScanRate += summary.scanRate;
          totalQueryByteRate += summary.queryByteRate;
          totalEntries += summary.recs;
          totalHoldTime += server.holdTime;
          totalLookups += server.lookups;
          majorCompactions += summary.majors.running;
          minorCompactions += summary.minors.running;
          lookupRateTracker.updateTabletServer(server.name, server.lastContact, server.lookups);
          indexCacheHitTracker.updateTabletServer(server.name, server.lastContact, server.indexCacheHits);
          indexCacheRequestTracker.updateTabletServer(server.name, server.lastContact, server.indexCacheRequest);
          dataCacheHitTracker.updateTabletServer(server.name, server.lastContact, server.dataCacheHits);
          dataCacheRequestTracker.updateTabletServer(server.name, server.lastContact, server.dataCacheRequest);
        }

        lookupRateTracker.finishedUpdating();
        indexCacheHitTracker.finishedUpdating();
        indexCacheRequestTracker.finishedUpdating();
        dataCacheHitTracker.finishedUpdating();
        dataCacheRequestTracker.finishedUpdating();

        int totalTables = 0;
        for (TableInfo tInfo : mmi.tableMap.values()) {
          totalTabletCount += tInfo.tablets;
          totalTables++;
        }
        Monitor.totalIngestRate = totalIngestRate;
        Monitor.totalTables = totalTables;
        totalIngestByteRate = totalIngestByteRate / 1000000.0;
        Monitor.totalQueryRate = totalQueryRate;
        Monitor.totalScanRate = totalScanRate;
        totalQueryByteRate = totalQueryByteRate / 1000000.0;
        Monitor.totalEntries = totalEntries;
        Monitor.totalTabletCount = totalTabletCount;
        Monitor.totalHoldTime = totalHoldTime;
        Monitor.totalLookups = totalLookups;

        ingestRateOverTime.add(new Pair<>(currentTime, totalIngestRate));
        ingestByteRateOverTime.add(new Pair<>(currentTime, totalIngestByteRate));

        double totalLoad = 0.;
        for (TabletServerStatus status : mmi.tServerInfo) {
          if (status != null)
            totalLoad += status.osLoad;
        }
        loadOverTime.add(new Pair<>(currentTime, totalLoad));

        minorCompactionsOverTime.add(new Pair<>(currentTime, minorCompactions));
        majorCompactionsOverTime.add(new Pair<>(currentTime, majorCompactions));

        lookupsOverTime.add(new Pair<>(currentTime, lookupRateTracker.calculateRate()));

        queryRateOverTime.add(new Pair<>(currentTime, (int) totalQueryRate));
        queryByteRateOverTime.add(new Pair<>(currentTime, totalQueryByteRate));

        scanRateOverTime.add(new Pair<>(currentTime, (int) totalScanRate));

        calcCacheHitRate(indexCacheHitRateOverTime, currentTime, indexCacheHitTracker, indexCacheRequestTracker);
        calcCacheHitRate(dataCacheHitRateOverTime, currentTime, dataCacheHitTracker, dataCacheRequestTracker);
      }
      try {
        Monitor.problemSummary = ProblemReports.getInstance(getContext()).summarize();
        Monitor.problemException = null;
      } catch (Exception e) {
        log.info("Failed to obtain problem reports ", e);
        Monitor.problemSummary = Collections.emptyMap();
        Monitor.problemException = e;
      }

    } finally {
      synchronized (Monitor.class) {
        fetching = false;
        lastRecalc.set(currentTime);
      }
    }
  }

  private static void calcCacheHitRate(List<Pair<Long,Double>> hitRate, long currentTime, EventCounter cacheHits, EventCounter cacheReq) {
    long req = cacheReq.calculateCount();
    if (req > 0)
      hitRate.add(new Pair<>(currentTime, cacheHits.calculateCount() / (double) cacheReq.calculateCount()));
    else
      hitRate.add(new Pair<Long,Double>(currentTime, null));
  }

  private static GCStatus fetchGcStatus() {
    GCStatus result = null;
    HostAndPort address = null;
    try {
      // Read the gc location from its lock
      ZooReaderWriter zk = ZooReaderWriter.getInstance();
      String path = ZooUtil.getRoot(instance) + Constants.ZGC_LOCK;
      List<String> locks = zk.getChildren(path, null);
      if (locks != null && locks.size() > 0) {
        Collections.sort(locks);
        address = new ServerServices(new String(zk.getData(path + "/" + locks.get(0), null), UTF_8)).getAddress(Service.GC_CLIENT);
        GCMonitorService.Client client = ThriftUtil.getClient(new GCMonitorService.Client.Factory(), address, new AccumuloServerContext(instance, config));
        try {
          result = client.getStatus(Tracer.traceInfo(), getContext().rpcCreds());
        } finally {
          ThriftUtil.returnClient(client);
        }
      }
    } catch (Exception ex) {
      log.warn("Unable to contact the garbage collector at " + address, ex);
    }
    return result;
  }

  public static void main(String[] args) throws Exception {
    final String app = "monitor";
    ServerOpts opts = new ServerOpts();
    opts.parseArgs(app, args);
    String hostname = opts.getAddress();
    SecurityUtil.serverLogin(SiteConfiguration.getInstance());

    VolumeManager fs = VolumeManagerImpl.get();
    instance = HdfsZooInstance.getInstance();
    config = new ServerConfigurationFactory(instance);
    context = new AccumuloServerContext(instance, config);
    log.info("Version " + Constants.VERSION);
    log.info("Instance " + instance.getInstanceID());
    MetricsSystemHelper.configure(Monitor.class.getSimpleName());
    Accumulo.init(fs, instance, config, app);
    Monitor monitor = new Monitor();
    // Servlets need access to limit requests when the monitor is not active, but Servlets are instantiated
    // via reflection. Expose the service this way instead.
    Monitor.HA_SERVICE_INSTANCE = monitor;
    DistributedTrace.enable(hostname, app, config.getSystemConfiguration());
    try {
      monitor.run(hostname);
    } finally {
      DistributedTrace.disable();
    }
  }

  private static long START_TIME;

  public void run(String hostname) {
    Monitor.START_TIME = System.currentTimeMillis();
    int ports[] = config.getSystemConfiguration().getPort(Property.MONITOR_PORT);
    for (int port : ports) {
      try {
        log.debug("Creating monitor on port {}", port);
        server = new EmbeddedWebServer(hostname, port);
        server.addServlet(getDefaultServlet(), "/resources/*");
        server.addServlet(getRestServlet(), "/rest/*");
        server.addServlet(getViewServlet(), "/*");
        server.start();
        break;
      } catch (Throwable ex) {
        log.error("Unable to start embedded web server", ex);
      }
    }
    if (!server.isRunning()) {
      throw new RuntimeException("Unable to start embedded web server on ports: " + Arrays.toString(ports));
    }

    try {
      getMonitorLock();
    } catch (Exception e) {
      log.error("Failed to get Monitor ZooKeeper lock");
      throw new RuntimeException(e);
    }

    String advertiseHost = hostname;
    if (advertiseHost.equals("0.0.0.0")) {
      try {
        advertiseHost = InetAddress.getLocalHost().getHostName();
      } catch (UnknownHostException e) {
        log.error("Unable to get hostname", e);
      }
    }
    log.debug("Using {} to advertise monitor location in ZooKeeper", advertiseHost);

    try {
      String monitorAddress = HostAndPort.fromParts(advertiseHost, server.getPort()).toString();
      ZooReaderWriter.getInstance().putPersistentData(ZooUtil.getRoot(instance) + Constants.ZMONITOR_HTTP_ADDR, monitorAddress.getBytes(UTF_8),
          NodeExistsPolicy.OVERWRITE);
      log.info("Set monitor address in zookeeper to {}", monitorAddress);
    } catch (Exception ex) {
      log.error("Unable to set monitor HTTP address in zookeeper", ex);
    }

    if (null != advertiseHost) {
      LogService.startLogListener(Monitor.getContext().getConfiguration(), instance.getInstanceID(), advertiseHost);
    } else {
      log.warn("Not starting log4j listener as we could not determine address to use");
    }

    new Daemon(new LoggingRunnable(log, new ZooKeeperStatus()), "ZooKeeperStatus").start();

    // need to regularly fetch data so plot data is updated
    new Daemon(new LoggingRunnable(log, new Runnable() {

      @Override
      public void run() {
        while (true) {
          try {
            Monitor.fetchData();
          } catch (Exception e) {
            log.warn("{}", e.getMessage(), e);
          }

          sleepUninterruptibly(333, TimeUnit.MILLISECONDS);
        }

      }
    }), "Data fetcher").start();

    new Daemon(new LoggingRunnable(log, new Runnable() {
      @Override
      public void run() {
        while (true) {
          try {
            Monitor.fetchScans();
          } catch (Exception e) {
            log.warn("{}", e.getMessage(), e);
          }
          sleepUninterruptibly(5, TimeUnit.SECONDS);
        }
      }
    }), "Scan scanner").start();

    monitorInitialized.set(true);
  }

  private ServletHolder getDefaultServlet() {
    return new ServletHolder(new DefaultServlet() {
      private static final long serialVersionUID = 1L;

      @Override
      public Resource getResource(String pathInContext) {
        return Resource.newClassPathResource("/org/apache/accumulo/monitor" + pathInContext);
      }
    });
  }

  private ServletHolder getViewServlet() {
    final ResourceConfig rc = new ResourceConfig().packages("org.apache.accumulo.monitor.view")
        .register(new LoggingFeature(java.util.logging.Logger.getLogger(this.getClass().getSimpleName()))).register(FreemarkerMvcFeature.class)
        .property(MvcFeature.TEMPLATE_BASE_PATH, "/org/apache/accumulo/monitor/templates").property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
    return new ServletHolder(new ServletContainer(rc));
  }

  private ServletHolder getRestServlet() {
    final ResourceConfig rc = new ResourceConfig().packages("org.apache.accumulo.monitor.rest")
        .register(new LoggingFeature(java.util.logging.Logger.getLogger(this.getClass().getSimpleName()))).register(JacksonFeature.class)
        .property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
    return new ServletHolder(new ServletContainer(rc));
  }

  public static class ScanStats {
    public final long scanCount;
    public final Long oldestScan;
    public final long fetched;

    ScanStats(List<ActiveScan> active) {
      this.scanCount = active.size();
      long oldest = -1;
      for (ActiveScan scan : active) {
        oldest = Math.max(oldest, scan.age);
      }
      this.oldestScan = oldest < 0 ? null : oldest;
      this.fetched = System.currentTimeMillis();
    }
  }

  static final Map<HostAndPort,ScanStats> allScans = new HashMap<>();

  public static Map<HostAndPort,ScanStats> getScans() {
    synchronized (allScans) {
      return new HashMap<>(allScans);
    }
  }

  public static void fetchScans() throws Exception {
    if (instance == null)
      return;
    Connector c = context.getConnector();
    for (String server : c.instanceOperations().getTabletServers()) {
      final HostAndPort parsedServer = HostAndPort.fromString(server);
      Client tserver = ThriftUtil.getTServerClient(parsedServer, context);
      try {
        List<ActiveScan> scans = tserver.getActiveScans(null, context.rpcCreds());
        synchronized (allScans) {
          allScans.put(parsedServer, new ScanStats(scans));
        }
      } catch (Exception ex) {
        log.debug("Failed to get active scans from {}", server, ex);
      } finally {
        ThriftUtil.returnClient(tserver);
      }
    }
    // Age off old scan information
    Iterator<Entry<HostAndPort,ScanStats>> entryIter = allScans.entrySet().iterator();
    long now = System.currentTimeMillis();
    while (entryIter.hasNext()) {
      Entry<HostAndPort,ScanStats> entry = entryIter.next();
      if (now - entry.getValue().fetched > 5 * 60 * 1000) {
        entryIter.remove();
      }
    }
  }

  /**
   * Get the monitor lock in ZooKeeper
   */
  private void getMonitorLock() throws KeeperException, InterruptedException {
    final String zRoot = ZooUtil.getRoot(instance);
    final String monitorPath = zRoot + Constants.ZMONITOR;
    final String monitorLockPath = zRoot + Constants.ZMONITOR_LOCK;

    // Ensure that everything is kosher with ZK as this has changed.
    ZooReaderWriter zoo = ZooReaderWriter.getInstance();
    if (zoo.exists(monitorPath)) {
      byte[] data = zoo.getData(monitorPath, null);
      // If the node isn't empty, it's from a previous install (has hostname:port for HTTP server)
      if (0 != data.length) {
        // Recursively delete from that parent node
        zoo.recursiveDelete(monitorPath, NodeMissingPolicy.SKIP);

        // And then make the nodes that we expect for the incoming ephemeral nodes
        zoo.putPersistentData(monitorPath, new byte[0], NodeExistsPolicy.FAIL);
        zoo.putPersistentData(monitorLockPath, new byte[0], NodeExistsPolicy.FAIL);
      } else if (!zoo.exists(monitorLockPath)) {
        // monitor node in ZK exists and is empty as we expect
        // but the monitor/lock node does not
        zoo.putPersistentData(monitorLockPath, new byte[0], NodeExistsPolicy.FAIL);
      }
    } else {
      // 1.5.0 and earlier
      zoo.putPersistentData(zRoot + Constants.ZMONITOR, new byte[0], NodeExistsPolicy.FAIL);
      if (!zoo.exists(monitorLockPath)) {
        // Somehow the monitor node exists but not monitor/lock
        zoo.putPersistentData(monitorLockPath, new byte[0], NodeExistsPolicy.FAIL);
      }
    }

    // Get a ZooLock for the monitor
    while (true) {
      MoniterLockWatcher monitorLockWatcher = new MoniterLockWatcher();
      monitorLock = new ZooLock(monitorLockPath);
      monitorLock.lockAsync(monitorLockWatcher, new byte[0]);

      monitorLockWatcher.waitForChange();

      if (monitorLockWatcher.acquiredLock) {
        break;
      }

      if (!monitorLockWatcher.failedToAcquireLock) {
        throw new IllegalStateException("monitor lock in unknown state");
      }

      monitorLock.tryToCancelAsyncLockOrUnlock();

      sleepUninterruptibly(getContext().getConfiguration().getTimeInMillis(Property.MONITOR_LOCK_CHECK_INTERVAL), TimeUnit.MILLISECONDS);
    }

    log.info("Got Monitor lock.");
  }

  /**
   * Async Watcher for monitor lock
   */
  private static class MoniterLockWatcher implements ZooLock.AsyncLockWatcher {

    boolean acquiredLock = false;
    boolean failedToAcquireLock = false;

    @Override
    public void lostLock(LockLossReason reason) {
      Halt.halt("Monitor lock in zookeeper lost (reason = " + reason + "), exiting!", -1);
    }

    @Override
    public void unableToMonitorLockNode(final Throwable e) {
      Halt.halt(-1, new Runnable() {
        @Override
        public void run() {
          log.error("No longer able to monitor Monitor lock node", e);
        }
      });

    }

    @Override
    public synchronized void acquiredLock() {
      if (acquiredLock || failedToAcquireLock) {
        Halt.halt("Zoolock in unexpected state AL " + acquiredLock + " " + failedToAcquireLock, -1);
      }

      acquiredLock = true;
      notifyAll();
    }

    @Override
    public synchronized void failedToAcquireLock(Exception e) {
      log.warn("Failed to get monitor lock " + e);

      if (acquiredLock) {
        Halt.halt("Zoolock in unexpected state FAL " + acquiredLock + " " + failedToAcquireLock, -1);
      }

      failedToAcquireLock = true;
      notifyAll();
    }

    public synchronized void waitForChange() {
      while (!acquiredLock && !failedToAcquireLock) {
        try {
          wait();
        } catch (InterruptedException e) {}
      }
    }
  }

  public static MasterMonitorInfo getMmi() {
    return mmi;
  }

  public static int getTotalTables() {
    return totalTables;
  }

  public static int getTotalTabletCount() {
    return totalTabletCount;
  }

  public static long getTotalEntries() {
    return totalEntries;
  }

  public static double getTotalIngestRate() {
    return totalIngestRate;
  }

  public static double getTotalQueryRate() {
    return totalQueryRate;
  }

  public static double getTotalScanRate() {
    return totalScanRate;
  }

  public static long getTotalHoldTime() {
    return totalHoldTime;
  }

  public static Exception getProblemException() {
    return problemException;
  }

  public static Map<Table.ID,Map<ProblemType,Integer>> getProblemSummary() {
    return problemSummary;
  }

  public static GCStatus getGcStatus() {
    return gcStatus;
  }

  public static long getTotalLookups() {
    return totalLookups;
  }

  public static long getStartTime() {
    return START_TIME;
  }

  public static List<Pair<Long,Double>> getLoadOverTime() {
    return new ArrayList<>(loadOverTime);
  }

  public static List<Pair<Long,Double>> getIngestRateOverTime() {
    return new ArrayList<>(ingestRateOverTime);
  }

  public static List<Pair<Long,Double>> getIngestByteRateOverTime() {
    return new ArrayList<>(ingestByteRateOverTime);
  }

  public static List<Pair<Long,Integer>> getMinorCompactionsOverTime() {
    return new ArrayList<>(minorCompactionsOverTime);
  }

  public static List<Pair<Long,Integer>> getMajorCompactionsOverTime() {
    return new ArrayList<>(majorCompactionsOverTime);
  }

  public static List<Pair<Long,Double>> getLookupsOverTime() {
    return new ArrayList<>(lookupsOverTime);
  }

  public static double getLookupRate() {
    return lookupRateTracker.calculateRate();
  }

  public static List<Pair<Long,Integer>> getQueryRateOverTime() {
    return new ArrayList<>(queryRateOverTime);
  }

  public static List<Pair<Long,Integer>> getScanRateOverTime() {
    return new ArrayList<>(scanRateOverTime);
  }

  public static List<Pair<Long,Double>> getQueryByteRateOverTime() {
    return new ArrayList<>(queryByteRateOverTime);
  }

  public static List<Pair<Long,Double>> getIndexCacheHitRateOverTime() {
    return new ArrayList<>(indexCacheHitRateOverTime);
  }

  public static List<Pair<Long,Double>> getDataCacheHitRateOverTime() {
    return new ArrayList<>(dataCacheHitRateOverTime);
  }

  public static AccumuloServerContext getContext() {
    return context;
  }

  @Override
  public boolean isActiveService() {
    return monitorInitialized.get();
  }

}


text:
/**
 * Created by Deakin on 2017/5/8 0008.
 */
export * from './feedback-list.component';


text:
package vcs

//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.
//
// Code generated by Alibaba Cloud SDK Code Generator.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.

import (
	"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
	"github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
)

// UploadImage invokes the vcs.UploadImage API synchronously
func (client *Client) UploadImage(request *UploadImageRequest) (response *UploadImageResponse, err error) {
	response = CreateUploadImageResponse()
	err = client.DoAction(request, response)
	return
}

// UploadImageWithChan invokes the vcs.UploadImage API asynchronously
func (client *Client) UploadImageWithChan(request *UploadImageRequest) (<-chan *UploadImageResponse, <-chan error) {
	responseChan := make(chan *UploadImageResponse, 1)
	errChan := make(chan error, 1)
	err := client.AddAsyncTask(func() {
		defer close(responseChan)
		defer close(errChan)
		response, err := client.UploadImage(request)
		if err != nil {
			errChan <- err
		} else {
			responseChan <- response
		}
	})
	if err != nil {
		errChan <- err
		close(responseChan)
		close(errChan)
	}
	return responseChan, errChan
}

// UploadImageWithCallback invokes the vcs.UploadImage API asynchronously
func (client *Client) UploadImageWithCallback(request *UploadImageRequest, callback func(response *UploadImageResponse, err error)) <-chan int {
	result := make(chan int, 1)
	err := client.AddAsyncTask(func() {
		var response *UploadImageResponse
		var err error
		defer close(result)
		response, err = client.UploadImage(request)
		callback(response, err)
		result <- 1
	})
	if err != nil {
		defer close(result)
		callback(nil, err)
		result <- 0
	}
	return result
}

// UploadImageRequest is the request struct for api UploadImage
type UploadImageRequest struct {
	*requests.RpcRequest
	ImageUrl string `position:"Body" name:"ImageUrl"`
}

// UploadImageResponse is the response struct for api UploadImage
type UploadImageResponse struct {
	*responses.BaseResponse
	Code      string `json:"Code" xml:"Code"`
	Message   string `json:"Message" xml:"Message"`
	RequestId string `json:"RequestId" xml:"RequestId"`
	Data      string `json:"Data" xml:"Data"`
}

// CreateUploadImageRequest creates a request to invoke UploadImage API
func CreateUploadImageRequest() (request *UploadImageRequest) {
	request = &UploadImageRequest{
		RpcRequest: &requests.RpcRequest{},
	}
	request.InitWithApiInfo("Vcs", "2020-05-15", "UploadImage", "", "")
	request.Method = requests.POST
	return
}

// CreateUploadImageResponse creates a response to parse from UploadImage response
func CreateUploadImageResponse() (response *UploadImageResponse) {
	response = &UploadImageResponse{
		BaseResponse: &responses.BaseResponse{},
	}
	return
}


text:
@interface WYNewsListController : UIViewController

@end


text:
\begin {figure}[hbtp]
\centering
\includegraphics[width=\textwidth]{output/Q3_50_ALL_WITH_1_OVER_NCORES/plot_Q3_50_bestmodels.eps}
\caption{Completion time vs ncores for query Q3 with datasize 50}
\label{fig:all_linear_Q3_50}
\end {figure}


text:
<Page xmlns="http://schemas.nativescript.org/tns.xsd"
      loaded="pageLoaded"
      android:actionBarHidden="true">
  <WebView src="{{ status }}" />
</Page>


text:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="@style/Theme.Leanback" />
</resources>


text:
package service;

/**
 *
 * @author Scott
 */
public interface Config {
    public String SERVICE_HOSTNAME = "localhost";
    public int INTUITIVE_BIDDER_SERVICE_PORT = 3000;
    public int BIDDING_SERVICE_PORT = 3001;
    public int INTUITIVE_BIDDER_SERVICE_UDP_PORT = 6000;
    public int BIDDING_SERVICE_UDP_PORT = 6001;

    public String DATA_DIR_PATH = "data";
    public String ATTESTATION_DIR_PATH = "attestations";
    public String DOWNLOADS_DIR_PATH = "downloads";
    public String KEYPAIR_DIR_PATH = "keypairs";

    public String INITIAL_HASH = "default";
    public String DIGEST_ALGORITHM = "SHA-1";

    public boolean ENABLE_MULTITHREAD_EXECUTING = false;
    public int NUM_PROCESSORS = Runtime.getRuntime().availableProcessors();
}


text:
<?php

namespace Maghead\Schema;

use SplObjectStorage;
use ArrayObject;
use Maghead\Schema\Relationship\BelongsTo;
use CLIFramework\Logger;

class ClassInstanceMap extends ArrayObject
{
    public function add($obj)
    {
        $this[get_class($obj)] = $obj;
    }
}

class SchemaDependencyResolver
{
    protected $resolved;

    protected $resolvedStorage;

    protected $traced;

    protected $logger;

    protected $classMap;

    public function __construct(Logger $logger)
    {
        $this->logger = $logger;
    }

    public function resolve(SchemaCollection $classes)
    {
        $this->resolved = new SchemaCollection([]);
        $this->resolvedStorage = new SplObjectStorage;
        $this->traced = new SplObjectStorage;

        $schemas = $classes->evaluate();

        $this->classMap = new ClassInstanceMap;
        foreach ($schemas as $schema) {
            $this->classMap->add($schema);
        }

        $this->logger->debug("start tracing ...");
        foreach ($schemas as $schema) {
            $this->traceUp($schema);
        }

        return $this->resolved;
    }

    protected function traceUp(DeclareSchema $schema)
    {
        $this->logger->debug("trace {$schema}");
        $this->logger->indent();
        $this->traced->attach($schema);

        $rels = $schema->getRelations();
        foreach ($rels as $relKey => $rel) {
            if ($rel instanceof BelongsTo) {
                $foreignSchemaClass = $rel['foreign_schema'];
                if (!isset($this->classMap[$foreignSchemaClass])) {
                    $this->classMap->add(new $foreignSchemaClass);
                }

                $fs = $this->classMap[$foreignSchemaClass];
                if (!$this->traced->contains($fs)) {
                    $this->logger->debug("found belongs to relationship {$schema}.{$relKey} => {$foreignSchemaClass}");
                    $this->traceUp($fs);
                } else {
                    $this->logger->debug("already traced {$schema}.{$relKey} => {$foreignSchemaClass}");
                }
            }
        }

        if (!$this->resolvedStorage->contains($schema)) {
            $this->logger->debug("adding {$schema}");
            $this->resolved[] = $schema;
            $this->resolvedStorage->attach($schema);
        }

        $this->logger->unIndent();
    }
}


text:
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>

#define closesocket(s) close(s)

typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;

/* STRUCTURE DU SERVEUR */
SOCKADDR_IN srv = { 0 };

/* STRUCTURE DU CLIENT */
SOCKADDR_IN cl = { 0 };

/* DECLARATION DES VARIABLES */
#define BUFSIZE 16
unsigned char buffer[BUFSIZE];
int clsize = sizeof(cl);
unsigned char bufferHexa[BUFSIZE * 2 + 1];
unsigned char tmp[BUFSIZE * 2 + 1];
unsigned char cycle_buff[BUFSIZE * 2 + 1];
unsigned char cycle_buff_2[BUFSIZE * 2 + 1];
unsigned char tst[BUFSIZE];
int i, len;
char * pos;
char cmd[255];
int forkPid;
int port = 1470;

int main() {

  /* INITIALISATION DU SOCKET */
  SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);

  if ((forkPid = fork()) < 0)
	perror("fork()");

  if (sock == -1) {
    perror("socket()");
  }

  if (!forkPid)
  {

  /* DEFINITION DE LA STRUCTURE */
  srv.sin_addr.s_addr = htonl(INADDR_ANY);
  srv.sin_family = AF_INET;
  srv.sin_port = htons(port);

  /* ECOUTE */
  if ( bind(sock, (SOCKADDR *) &srv, sizeof(srv)) == -1) {
    puts("erreur");
    perror("bind()");
  }

  /* ATTENTE DE RECEPTION ET TRAITEMENT */
  strcpy(cycle_buff,"");
  while (1) {

    recvfrom(sock, buffer, sizeof(buffer), 0, (SOCKADDR *)&cl, &clsize);
    strncpy(tmp, bufferHexa, sizeof(bufferHexa));

    bufferHexa[0] = 0;

    for (i=0;i<clsize;i++) {
      sprintf(bufferHexa, "%s%0.2X", bufferHexa, buffer[i]);
    }

	// FULL Frame Received on empty buffer
    printf("***70 Frame*** php /var/www/domocan/bin/recv.php %s\n", bufferHexa);
	sprintf(cmd, "php /var/www/smartcan/bin/rx-DOMOCAN3.php %s &", bufferHexa);
    system(cmd);
	memset(bufferHexa, '\0', sizeof(bufferHexa));

  }

  closesocket(sock);
  }

}