text:
//******************************************************************************************************
//  LogFileViewer.cs - Gbtc
//
//  Copyright © 2016, Grid Protection Alliance.  All Rights Reserved.
//
//  Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
//  the NOTICE file distributed with this work for additional information regarding copyright ownership.
//  The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
//  file except in compliance with the License. You may obtain a copy of the License at:
//
//      http://opensource.org/licenses/MIT
//
//  Unless agreed to in writing, the subject software distributed under the License is distributed on an
//  "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
//  License for the specific language governing permissions and limitations.
//
//  Code Modification History:
//  ----------------------------------------------------------------------------------------------------
//  11/01/2016 - Steven E. Chisholm
//       Generated original version of source code.
//
//******************************************************************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using GSF.Diagnostics;
using GSF.IO;
using LogFileViewer.Filters;
using LogFileViewer.Menu;
using LogFileViewer.Properties;

namespace LogFileViewer
{
    internal partial class LogFileViewer : Form
    {
        private BindingSource m_bindingSource;
        private DataTable m_dataTable;
        private bool[] m_visibleFlags;
        private List<LogMessageFilter> m_filters;
        private HashSet<string> m_filesToBold = new HashSet<string>();
        private DataGridViewCellStyle m_highlightStyle;

        private string m_logPath;
        SortedList<string, byte[]> m_savedFilters = new SortedList<string, byte[]>();

        public LogFileViewer(string logPath = null)
        {
            if (!Settings.Default.HasBeenUpgraded)
            {
                Settings.Default.Upgrade();
                Settings.Default.HasBeenUpgraded = true;
                Settings.Default.Save();
            }

            m_visibleFlags = new bool[9];

            if (logPath != null)
            {
                try
                {
                    if (!Directory.Exists(logPath))
                    {
                        logPath = null;
                    }
                }
                catch (Exception)
                {
                    logPath = null;
                }
            }

            m_logPath = logPath;
            m_filters = new List<LogMessageFilter>();

            InitializeComponent();

            m_bindingSource = new BindingSource();
            m_dataTable = new DataTable();
            m_dataTable.Columns.Add("Object", typeof(LogMessage));
            m_dataTable.Columns.Add("File Name", typeof(string));
            m_dataTable.Columns.Add("Time", typeof(DateTime));
            m_dataTable.Columns.Add("Level", typeof(string));
            m_dataTable.Columns.Add("Flags", typeof(string));
            m_dataTable.Columns.Add("Type", typeof(string));
            m_dataTable.Columns.Add("Stack Details", typeof(string));
            m_dataTable.Columns.Add("Event Name", typeof(string));
            m_dataTable.Columns.Add("Message", typeof(string));
            m_dataTable.Columns.Add("Details", typeof(string));
            m_dataTable.Columns.Add("Exception", typeof(string));
            m_dataTable.Columns.Add("_Show", typeof(bool));
            m_dataTable.Columns.Add("_Level", typeof(int));

            m_bindingSource.Filter = "_Show";
            m_bindingSource.DataSource = m_dataTable;
            dgvResults.DataSource = m_bindingSource;

            dgvResults.Columns["Time"].DefaultCellStyle.Format = "MM/dd/yyyy HH:mm:ss.fff";
            dgvResults.Columns["Object"].Visible = false;
            dgvResults.Columns["_Show"].Visible = false;
            dgvResults.Columns["_Level"].Visible = false;
            m_highlightStyle = new DataGridViewCellStyle(dgvResults.DefaultCellStyle);
            m_highlightStyle.ForeColor = Color.Blue;
            m_visibleFlags = GetVisibleFlags();
        }

        private void LogFileViewer_Load(object sender, EventArgs e)
        {
            typeof(DataGridView).InvokeMember("DoubleBuffered", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty, null, dgvResults, new object[] { true });
            LoadFilters();
        }

        private void RefreshDataSource()
        { }

        private void BtnLoad_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dlg = new OpenFileDialog())
            {
                if (m_logPath != null)
                {
                    dlg.InitialDirectory = m_logPath;
                    dlg.RestoreDirectory = true;
                }

                dlg.Filter = "Log Files (*.logz)|*.logz|All Files (*.*)|*.*";
                dlg.Multiselect = true;

                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    LoadFiles(dlg.FileNames, false);
                }
            }
        }

        private void LoadFiles(string[] files, bool loadFiltered)
        {
            m_bindingSource.RaiseListChangedEvents = false;
            m_dataTable.Rows.Clear();
            foreach (string file in files)
            {
                string fileWithoutExtension = Path.GetFileNameWithoutExtension(file);
                List<LogMessage> messages = LogFileReader.Read(file);
                FilterFirstChanceExceptions(messages);
                foreach (LogMessage message in messages)
                {
                    if (!loadFiltered || IsShown(message))
                        AddRowToDataTable(message, fileWithoutExtension);
                }
            }
            m_bindingSource.RaiseListChangedEvents = true;
            m_bindingSource.ResetBindings(false);
            RefreshFilters();
        }

        private void AddRowToDataTable(LogMessage message, string fileWithoutExtension)
        {
            object[] items = new object[13];
            items[0] = message;
            items[1] = fileWithoutExtension;
            items[2] = message.UtcTime.ToLocalTime();
            items[3] = $"{message.Classification} - {message.Level}";
            items[4] = message.Flags.ToString();
            items[5] = message.TypeName;
            items[6] = message.InitialStackMessages.Union(message.CurrentStackMessages).ToString();
            items[7] = message.EventName;
            items[8] = message.Message;
            items[9] = message.Details;
            items[10] = message.ExceptionString;
            items[11] = true;
            items[12] = 4;
            m_dataTable.Rows.Add(items);
        }

        private void btnFilteredLoad_Click(object sender, EventArgs e)
        {
            using (var dlg = new OpenFileDialog())
            {
                if (m_logPath != null)
                {
                    dlg.InitialDirectory = m_logPath;
                    dlg.RestoreDirectory = true;
                }
                dlg.Filter = "Log Files (Compressed)|*.logz";
                dlg.Multiselect = true;
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    LoadFiles(dlg.FileNames, true);
                }
            }
        }

        private void dgvResults_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                LogMessage item = (LogMessage)dgvResults.Rows[e.RowIndex].Cells["Object"].Value;
                string fileName = (string)dgvResults.Rows[e.RowIndex].Cells["File Name"].Value;

                List<LogMessage> messages = new List<LogMessage>();
                messages.Add(item);

                List<Tuple<string, Func<LogMessageFilter>>> items = new List<Tuple<string, Func<LogMessageFilter>>>();

                switch (dgvResults.Columns[e.ColumnIndex].Name)
                {
                    case "File Name":
                        ContextMenuStrip menu = new ContextMenuStrip();
                        ToolStripButton button;
                        if (!m_filesToBold.Contains(fileName))
                        {
                            button = new ToolStripButton("Color");
                            button.Click += (send1, e1) =>
                                            {
                                                m_filesToBold.Add(fileName);
                                                dgvResults.Invalidate();
                                            };
                        }
                        else
                        {
                            button = new ToolStripButton("Remove Color");
                            button.Click += (send1, e1) =>
                            {
                                m_filesToBold.Remove(fileName);
                                dgvResults.Invalidate();
                            };
                        }
                        menu.Items.Add(button);
                        menu.ShowImageMargin = false;
                        menu.Width = 150;
                        menu.Show(dgvResults, dgvResults.PointToClient(Cursor.Position));
                        return;
                    case "Type":
                        items.AddRange(new TypeMenu(messages).GetMenuButtons());
                        break;
                    case "Event Name":
                        items.AddRange(new EventMenu(messages).GetMenuButtons());
                        break;
                    case "Time":
                        items.AddRange(new TimestampMenu(messages).GetMenuButtons());
                        break;
                    case "Level":
                        //MakeMenu(e, new MatchVerbose(item));
                        break;
                    case "Exception":
                        items.AddRange(new ExceptionMenu(messages).GetMenuButtons());
                        break;
                    case "Message":
                        items.AddRange(new MessageMenu(messages).GetMenuButtons());
                        break;
                    case "Stack Details":
                        //MakeMenu(e, new MatchStackMessages(item));
                        break;
                    default:
                        return;
                }

                if (items.Count > 0)
                    MakeMenu(e, items);

            }
        }

        private void MakeMenu(DataGridViewCellMouseEventArgs e, List<Tuple<string, Func<LogMessageFilter>>> items)
        {
            ContextMenuStrip menu = new ContextMenuStrip();

            foreach (Tuple<string, Func<LogMessageFilter>> item in items)
            {
                var subMenu = new ToolStripMenuItem(item.Item1);

                for (int x = 0; x <= 6; x++)
                {
                    ToolStripButton button = new ToolStripButton(((FilterLevel)x).Name());
                    button.Tag = (FilterLevel)x;
                    button.Click += (send1, e1) =>
                                    {
                                        LogMessageFilter filter = item.Item2();
                                        if (filter != null)
                                        {
                                            filter.FilterLevel = (FilterLevel)((ToolStripButton)send1).Tag;
                                            LstFilters.Items.Add(filter);
                                            m_filters.Add(filter);
                                            RefreshFilters();
                                        }
                                    };
                    subMenu.DropDownItems.Add(button);
                }
                menu.Items.Add(subMenu);

                ((ToolStripDropDownMenu)subMenu.DropDown).ShowImageMargin = false;
                subMenu.DropDown.Width = 150;

            }

            menu.ShowImageMargin = false;
            menu.Width = 150;
            menu.Show(dgvResults, dgvResults.PointToClient(Cursor.Position));
        }

        private void dgvResults_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && e.RowIndex >= 0)
            {
                LogMessage item = (LogMessage)dgvResults.Rows[e.RowIndex].Cells["Object"].Value;
                ShowError win = new ShowError();

                win.TxtMessage.Text = item.ToString();
                win.Show();
                win.TxtMessage.Select(0, 0);
            }
        }

        private void BtnCompactFiles_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dlgLoad = new OpenFileDialog())
            {
                if (m_logPath != null)
                {
                    dlgLoad.InitialDirectory = m_logPath;
                    dlgLoad.RestoreDirectory = true;
                }

                dlgLoad.Filter = "Log File (Compressed)|*.logz";
                dlgLoad.Multiselect = true;

                if (dlgLoad.ShowDialog() == DialogResult.OK)
                {
                    using (SaveFileDialog dlgSave = new SaveFileDialog())
                    {
                        if (m_logPath != null)
                        {
                            dlgLoad.InitialDirectory = m_logPath;
                            dlgLoad.RestoreDirectory = true;
                        }

                        dlgSave.Filter = "Log File (Compressed)|*.logz";

                        if (dlgSave.ShowDialog() == DialogResult.OK)
                        {
                            Stopwatch sw = new Stopwatch();
                            sw.Start();
                            LogFileCompactor.Compact(dlgLoad.FileNames, dlgSave.FileName);
                            sw.Stop();
                            MessageBox.Show(sw.Elapsed.TotalSeconds.ToString());
                        }
                    }
                }
            }
        }

        private void cmsFilters_Opening(object sender, CancelEventArgs e)
        {
            FillMenu();
        }

        public void LoadFilters()
        {
            try
            {
                string data = Settings.Default.SavedFilters;
                if (string.IsNullOrEmpty(data))
                    return;
                byte[] dataBytes = Convert.FromBase64String(data);
                MemoryStream ms = new MemoryStream(dataBytes);

                byte version = ms.ReadNextByte();
                switch (version)
                {
                    case 1:
                        SortedList<string, byte[]> filterSets = new SortedList<string, byte[]>();
                        int count = ms.ReadInt32();
                        while (count > 0)
                        {
                            count--;
                            string name = ms.ReadString();
                            byte[] filter = ms.ReadBytes();
                            filterSets[name] = filter;
                        }

                        m_savedFilters = filterSets;
                        break;
                    default:
                        throw new VersionNotFoundException();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void SaveFilters()
        {
            var ms = new MemoryStream();
            ms.Write((byte)1);
            ms.Write(m_savedFilters.Count);
            foreach (var filter in m_savedFilters)
            {
                ms.Write(filter.Key);
                ms.WriteWithLength(filter.Value);
            }

            Settings.Default.SavedFilters = Convert.ToBase64String(ms.ToArray());
            Settings.Default.Save();
        }

        public void FillMenu()
        {
            overwriteToolStripMenuItem.DropDownItems.Clear();
            foreach (var item in m_savedFilters)
            {
                var button = new ToolStripButton(item.Key);
                button.Width = 300;
                button.Click += (send1, e1) =>
                                { SaveCurrentTemplete(item.Key); };
                overwriteToolStripMenuItem.DropDownItems.Add(button);
            }

            loadToolStripMenuItem.DropDownItems.Clear();
            foreach (var item in m_savedFilters)
            {
                var button = new ToolStripButton(item.Key);
                button.Width = 300;
                button.Click += (send1, e1) =>
                                { LoadCurrentTemplete(item.Key); };
                loadToolStripMenuItem.DropDownItems.Add(button);
            }

            deleteToolStripMenuItem.DropDownItems.Clear();
            foreach (var item in m_savedFilters)
            {
                var button = new ToolStripButton(item.Key);
                button.Width = 300;
                button.Click += (send1, e1) =>
                                {
                                    m_savedFilters.Remove(item.Key);
                                    SaveFilters();

                                };
                deleteToolStripMenuItem.DropDownItems.Add(button);
            }
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string item = InputBox.Show("Provide a name.", "Save As", "Name");
            if (string.IsNullOrWhiteSpace(item))
                return;

            SaveCurrentTemplete(item);
        }

        private void SaveCurrentTemplete(string item)
        {
            var ms = new MemoryStream();
            ms.Write((byte)2);
            ms.Write(m_filters.Count);
            foreach (var filter in m_filters)
            {
                filter.Save(ms);
            }

            m_savedFilters[item] = ms.ToArray();

            SaveFilters();
        }

        private void LoadCurrentTemplete(string item)
        {
            List<LogMessageFilter> filters = new List<LogMessageFilter>();
            var ms = new MemoryStream(m_savedFilters[item]);
            byte version = ms.ReadNextByte();
            switch (version)
            {
                case 1:
                    return;
                case 2:
                    int count = ms.ReadInt32();
                    while (count > 0)
                    {
                        count--;
                        filters.Add(new LogMessageFilter(ms));
                    }

                    m_filters = filters;
                    LstFilters.Items.Clear();
                    LstFilters.Items.AddRange(filters.Cast<object>().ToArray());
                    RefreshFilters();
                    break;
                default:
                    throw new NotSupportedException();
            }

        }

        private void btnSaveSelected_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog dlgSave = new SaveFileDialog())
            {
                dlgSave.Filter = "Log File (Compressed)|*.logz";

                if (dlgSave.ShowDialog() == DialogResult.OK)
                {
                    using (var fileWriter = new LogFileWriter(dlgSave.FileName))
                    {
                        foreach (DataRow row in m_dataTable.Rows)
                        {
                            LogMessage message = row["Object"] as LogMessage;
                            bool shown = (bool)row["_Show"];

                            if (shown)
                            {
                                fileWriter.Write(message, false);
                            }
                        }
                    }
                }
            }
        }

        private void FilterFirstChanceExceptions(List<LogMessage> messages)
        {
            for (int x = 0; x < messages.Count; x++)
            {
                //Only first chance exceptions can be filtered.
                LogMessage firstMessage = messages[x];
                if (firstMessage.ExceptionString.Length > 0 && firstMessage.TypeName == "GSF.Diagnostics.Logger" && firstMessage.EventName == "First Chance App Domain Exception")
                {
                    //don't look ahead for more than 100ms.
                    DateTime stopAfter = firstMessage.UtcTime.AddTicks(100 * TimeSpan.TicksPerMillisecond);

                    //bool foundMatch = false;
                    for (int y = x + 1; y < messages.Count; y++)
                    {
                        LogMessage futureMessage = messages[y];
                        if (futureMessage.UtcTime > stopAfter)
                            break;

                        if (futureMessage.ManagedThreadID == firstMessage.ManagedThreadID && futureMessage.ExceptionString.Length > 0 &&
                            !(futureMessage.TypeName == "GSF.Diagnostics.Logger" && futureMessage.EventName == "First Chance App Domain Exception"))
                        {
                            if (AreExceptionsEqual(firstMessage, futureMessage))
                            {
                                messages.RemoveAt(x);
                                x--;
                                break;
                            }
                        }
                    }
                }
            }
        }

        private bool AreExceptionsEqual(LogMessage first, LogMessage future)
        {
            string firstError = TrimException(first.ExceptionString);
            string futureError = TrimException(future.ExceptionString);

            if (futureError.StartsWith(firstError))
            {
                return true;
            }

            string[] firstWords = firstError.Split(" \r\n:".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            List<string> foundWords = new List<string>();
            List<string> missingWords = new List<string>();
            List<string> ignoredWords = new List<string>();

            int position = 0;

            foreach (var word in firstWords)
            {
                if (position >= futureError.Length)
                {
                    missingWords.Add(word);
                }
                else
                {
                    int newPosition = futureError.IndexOf(word, position);
                    if (newPosition >= 0)
                    {
                        foundWords.Add(word);
                        position = newPosition + word.Length;
                    }
                    else
                    {
                        if (word[0] == '(' && word[word.Length - 1] == ')' && word.Contains("0x"))
                        {
                            ignoredWords.Add(word);
                        }
                        else
                        {
                            missingWords.Add(word);
                        }
                    }
                }
            }

            if (missingWords.Count > 0)
            {
                return false;
            }
            return true;
        }

        private string TrimException(string error)
        {
            int pos = error.IndexOf("WRN: Assembly binding");
            if (pos >= 0)
            {
                return error.Substring(0, pos).TrimEnd("\r\n ".ToCharArray());
            }
            return error;
        }

        private void LogFileViewer_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy;
        }

        private void LogFileViewer_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            LoadFiles(files, (e.KeyState & 8) == 8); //CTRL is pushed.
        }

        private void LstFilters_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                foreach (LogMessageFilter item in LstFilters.SelectedItems.Cast<LogMessageFilter>().ToArray())
                {
                    m_filters.Remove(item);
                    LstFilters.Items.Remove(item);
                }

                RefreshFilters();
            }
        }

        private void VisibleCheckedChanged(object sender, EventArgs e)
        {
            m_visibleFlags = GetVisibleFlags();
            RefreshVisible();
        }

        private void RefreshVisible()
        {
            var visible = GetVisibleFlags();

            m_bindingSource.RaiseListChangedEvents = false;
            foreach (DataRow row in m_dataTable.Rows)
            {
                int level = (int)row["_Level"];
                bool show = (bool)row["_Show"];

                if (show ^ visible[level])
                {
                    row["_show"] = !show;
                }
            }
            m_bindingSource.RaiseListChangedEvents = true;
            m_bindingSource.ResetBindings(false);
        }

        private bool[] GetVisibleFlags()
        {
            bool[] visible = new bool[7];
            visible[0] = RdoLowest.Checked;
            visible[1] = visible[0] || RdoLow.Checked;
            visible[2] = visible[1] || RdoBelowNormal.Checked;
            visible[3] = visible[2] || RdoNormal.Checked;
            visible[4] = visible[3] || RdoAboveNormal.Checked;
            visible[5] = visible[4] || RdoHigh.Checked;
            visible[6] = visible[5] || RdoHighest.Checked;
            return visible;
        }

        private void RefreshFilters()
        {
            var visible = GetVisibleFlags();
            m_bindingSource.RaiseListChangedEvents = false;
            foreach (DataRow row in m_dataTable.Rows)
            {
                LogMessage message = row["Object"] as LogMessage;
                int level = (int)row["_Level"];
                bool show = (bool)row["_Show"];
                int assignedLevel = GetLevel(message);

                if (level != assignedLevel)
                {
                    row["_Level"] = assignedLevel;
                }
                if (show ^ visible[assignedLevel])
                {
                    row["_show"] = !show;
                }
            }
            m_bindingSource.RaiseListChangedEvents = true;
            m_bindingSource.ResetBindings(false);
        }

        private int GetLevel(LogMessage message)
        {
            //Match is in reverse order.
            for (int x = m_filters.Count - 1; x >= 0; x--)
            {
                var filter = m_filters[x];
                if (filter.IsMatch(message))
                {
                    return (int)filter.FilterLevel;
                }
            }
            return (int)(FilterLevel.Normal);
        }

        private bool IsShown(LogMessage message)
        {
            return m_visibleFlags[GetLevel(message)];
        }

        private void dgvResults_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                DataGridViewRow row = dgvResults.Rows[e.RowIndex];
                string fileName = (string)dgvResults["File Name", e.RowIndex].Value;
                if (m_filesToBold.Contains(fileName))
                {
                    row.DefaultCellStyle = m_highlightStyle;
                }
                else
                {
                    row.DefaultCellStyle = dgvResults.DefaultCellStyle;
                }
            }
        }

        private void dgvResults_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex != -1 && (e.State & DataGridViewElementStates.Selected) != DataGridViewElementStates.Selected)
            {
                int value = (int)dgvResults["_Level", e.RowIndex].Value;
                Color color;
                switch (value)
                {
                    case 0:
                        color = Color.Silver;
                        break;
                    case 1:
                        color = Color.FromArgb(255, 128, 128);
                        break;
                    case 2:
                        color = Color.FromArgb(255, 128, 255);
                        break;
                    case 4:
                        color = Color.FromArgb(255, 255, 128);
                        break;
                    case 5:
                        color = Color.FromArgb(128, 255, 128);
                        break;
                    case 6:
                        color = Color.FromArgb(128, 255, 255);
                        break;
                    default:
                        return;
                }

                //fill gradient background
                using (SolidBrush gradientBrush = new SolidBrush(color))
                {
                    e.Graphics.FillRectangle(gradientBrush, e.CellBounds);
                }

                // paint rest of cell
                e.Paint(e.CellBounds, DataGridViewPaintParts.Border | DataGridViewPaintParts.ContentForeground);
                e.Handled = true;
            }
        }

        private void BtnAddFilter_Click(object sender, EventArgs e)
        {
            DataGridViewCell cell = dgvResults.SelectedCells.Cast<DataGridViewCell>().FirstOrDefault();
            if (cell == null)
            {
                MessageBox.Show("Select a message to base the filter on");
                return;
            }
            LogMessage message = dgvResults["Object", cell.RowIndex].Value as LogMessage;

            using (var win = new FilterEditorDialog(message, new LogMessageFilter()))
            {
                if (win.ShowDialog() == DialogResult.OK)
                {
                    LstFilters.Items.Add(win.FilterResult);
                    m_filters.Add(win.FilterResult);
                    RefreshFilters();
                }
            }
        }

        private void LstFilters_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (LstFilters.SelectedItem != null)
            {
                DataGridViewCell cell = dgvResults.SelectedCells.Cast<DataGridViewCell>().FirstOrDefault();
                if (cell == null)
                {
                    MessageBox.Show("Select a message to base the edits on");
                    return;
                }
                LogMessage message = dgvResults["Object", cell.RowIndex].Value as LogMessage;

                using (var win = new FilterEditorDialog(message, (LogMessageFilter)LstFilters.SelectedItem))
                {
                    if (win.ShowDialog() == DialogResult.OK)
                    {
                        m_filters[LstFilters.SelectedIndex] = win.FilterResult;
                        LstFilters.Items[LstFilters.SelectedIndex] = win.FilterResult;
                        RefreshFilters();
                    }
                }

            }
        }

    }
}


text:
//
// Stroustrup - Programming Principles & Practice
//
// Chapter 25 Exercise 4 - Add Bitwise operations to the Ch 7 Calcultor
//
// According to a few sources online, bitwise operations have lesser precedence
// than addition and subtraction. Bitwise is added "above" expression, a
// statement() calls bitwise() which calls expression().
//
// Chapter 7 - Simple Calculator
//
// Drill - adds sqrt and pow funcitonality
// Ex 2 - allows underscores in variable names
// Ex 3 - provides constant variabls
// Ex 4 - moves Variable related functions to Symbol_table class
// Ex 5 - Token_stream reads \n as print
// Ex 6 - help page added (?)
// Ex 7 - help and quit commands expanded to strings
// Ex 8 - Grammar updated
// Ex 9 - Added sin and cos
//

/*
    The grammar for input is:

    Calculation:
        Statement
        Print
        Help
        Quit
        Calculation Statement

    Statemant:
        Declaration
        Expression

    Print:
        ;

    Quit:
        q

    Declaration:
        "let" Name "=" Expression

    Bitwise:
        Expression
        ~ Expression
        Expression & Expression
        Expression | Expression
        Expression ^ Expression

    Expression:
        Term
        Expression + Term
        Expression - Term

    Term:
        Secondary
        Term * Secondary
        Term / Secondary
        Term % Secondary

    Secondary:
        Primary "!"
        "Sqrt (" Expression ")"
        "Pow (" Expression "," Expression ")"

    Primary:
        Number
        ( Expression )
        { Expression }
        - Primary
        + Primary
        Variable
        "sqrt"( Expression )
        "pow(" Expression "," narrow_cast<int>(Expression) ")"
        "sin"( Expression )
        "cos"( Expression )

    Number:
        floating-point-literal

    Input comes from cin through the Token_stream called ts.
*/

#include "../text_lib/std_lib_facilities.h"

// §7.6.1 Symbolic constants
const char number = '8';
const char quit = 'q';
const char print = ';';
const char name = 'a';
const char let = 'L';           // declaration token
const char help = '?';
const char c_sin = 's';
const char c_cos = 'c';

const string prompt = "> ";
const string result = "= ";
const string declkey = "let";   //declaration keywork

// drill
const char square_root = '@';
const char exponent = '#';
const string sqrtkey = "sqrt";
const string expkey = "pow";
const string sinkey = "sin";
const string coskey = "cos";
const string quitkey = "quit";
const string helpkey = "help";

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

class Token {
public:
    char kind;
    double value;
    string name;

    Token(char k) : kind{k}, value{0} { }
    Token(char k, double v) : kind{k}, value{v} { }
    Token(char k, string n) : kind{k}, value{0}, name{n} { }
};

class Token_stream {
public:
    Token get();                // get a Token
    void putback(Token t);      // put a token back
    void ignore(char c);        // discard characters up to and including a c
private:
    bool full { false };        // is there a Token in the buffer?
    Token buffer {'0'};         // here is where putback() stores a Token
};

void Token_stream::ignore(char c)
    // c represents the kind of Token
{
    // first look in buffer
    if (full && c == buffer.kind) {
        full = false;
        return;
    }
    full = false;

    // now search for input
    char ch = 0;
    while (cin >> ch)
        if (ch == c) return;
}

void Token_stream::putback(Token t)
{
    buffer = t;                 // copy t to buffer
    full = true;                // buffer is now full
};

Token Token_stream::get()
{
    if (full) {             // do we already have a Token?
        full = false;       // remove Token from buffer
        return buffer;
    }

    char ch;
    cin.get(ch);            // look for any char including whitespace

    while (isspace(ch) && ch != '\n') cin.get(ch);

    switch (ch) {
        case '\n':
            return Token{print};
        case print:
        case quit:
        case help:
        case '(':
        case ')':
        case '{':
        case '}':
        case '!':
        case '+':
        case '-':
        case '*':
        case '/':
        case '%':
        case '=':
        case ',':
        case '~':
        case '&':
        case '|':
        case '^':
            return Token { ch };    // let each character represent itself
        case '.':
        case '0': case '1': case '2': case '3': case '4':
        case '5': case '6': case '7': case '8': case '9':
            {
                cin.putback(ch);    // put digit back into input stream
                double val;
                cin >> val;         // read floating-point number
                return Token { number, val };
            }
        default:
            if (isalpha(ch)) {
                string s;
                s += ch;
                while (cin.get(ch) &&
                        ((isalpha(ch) || isdigit(ch) || ch == '_')))
                    s += ch;
                cin.putback(ch);
                if (s == declkey) return Token{let};    // declaration keyword
                else if (s == sqrtkey) return Token{square_root};
                else if (s == expkey) return Token{exponent};
                else if (s == sinkey) return Token{c_sin};
                else if (s == coskey) return Token{c_cos};
                else if (s == quitkey) return Token{quit};
                else if (s == helpkey) return Token{help};
                else return Token{name, s};
            }
            error("Bad token");
    }
};

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

class Variable {
public:
    string name;
    double value;
    bool constant;
    Variable(string n, double v, bool c = false)
        : name{n}, value{v}, constant{c} { }
};

class Symbol_table {
    vector<Variable> var_table;
public:
    bool is_declared(string);
    double get_value(string);
    double set_value(string, double);
    double define_name(string, double, bool con = false);
};

bool Symbol_table::is_declared(string var)
    // is var already in var_table?
{
    for (const Variable& v : var_table)
        if (v.name == var) return true;
    return false;
}

double Symbol_table::get_value(string s)
    // return the value of the Variable named s
{
    for (const Variable& v : var_table)
        if (v.name == s) return v.value;
    error("get: undefined variable ", s);
}

double Symbol_table::set_value(string s, double d)
    // set the Variable named s to d
{
    for (Variable& v : var_table)
        if (v.name == s) {
            if (v.constant) error("Can't overwrite constant variable");
            v.value = d;
            return d;
        }
    error("set: undefined variable ", s);
}

double Symbol_table::define_name(string var, double val, bool con)
    // add {var,val,con} to var_table
{
    if (is_declared(var)) error(var, " declared twice");
    var_table.push_back(Variable{var,val,con});
    return val;
}

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// globals(?)

Symbol_table st;            // allows Variable storage and retrieval

Token_stream ts;            // provides get() and putback()

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// additional calculator functions

double bitwise();
double expression();        // forward declaration for primary to call

double calc_sqrt()
{
    char ch;
    if (cin.get(ch) && ch != '(') error("'(' expected");
    cin.putback(ch);
    double d = expression();
    if (d < 0) error("sqrt: negative val is imaginary");
    return sqrt(d);
}

double calc_pow()
{
    Token t = ts.get();
    if (t.kind != '(') error("'(' expected");
    double base = bitwise();
    t = ts.get();
    if (t.kind != ',') error("',' expected");
    int power = narrow_cast<int>(bitwise());
    t = ts.get();
    if (t.kind != ')') error("')' expected");
    return pow(base, power);
}

double calc_sin()
{
    char ch;
    if (cin.get(ch) && ch != '(') error("'(' expected");
    cin.putback(ch);
    double d = expression();
    if (d == 0 || d == 180) return 0;       // return true zero
    return sin(d*3.1415926535/180);
}

double calc_cos()
{
    char ch;
    if (cin.get(ch) && ch != '(') error("'(' expected");
    cin.putback(ch);
    double d = expression();
    if (d == 90 || d == 270) return 0;      // return 0 instead of 8.766e-11
    return cos(d*3.1415926535/180);
}

double handle_variable(Token& t)
{
    Token t2 = ts.get();
    if (t2.kind == '=')
        return st.set_value(t.name, expression());
    else {
        ts.putback(t2);
        return st.get_value(t.name);       // missing in text!
    }
}

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// input grammar functions

double primary()            // deal with numbers and parenthesis
    // ex 2 - added '{' case
{
    Token t = ts.get();
    switch (t.kind) {
        case '(':                   // handle '(' expression ')'
            {
                double d = bitwise();
                t = ts.get();
                if (t.kind != ')') error("')' expected");
                return d;
            }
        case '{':
            {
                double d = bitwise();
                t = ts.get();
                if (t.kind != '}') error("'}' expected");
                return d;
            }
        case number:
            return t.value;         // return the number's value
        case name:
            return handle_variable(t);
        case '-':
            return -primary();
        case '+':
            return primary();
        case square_root:
            return calc_sqrt();
        case exponent:
            return calc_pow();
        case c_sin:
            return calc_sin();
        case c_cos:
            return calc_cos();
        default:
            error("primary expected");
    }
}

double secondary()
    // ex 3 - Add a factorial operator '!'
{
    double left = primary();
    Token t = ts.get();

    while (true) {
        switch (t.kind) {
            case '!':
                if (left == 0) return 1;
                for (int i = left - 1; i > 0; --i) left *= i;
                t = ts.get();
                break;
            default:
                ts.putback(t);
                return left;
        }
    }
}

double term()               // deal with * and /
{
    double left = secondary();
    Token t = ts.get();             // get next token from Token_stream

    while (true) {
        switch (t.kind) {
            case '*':
                left *= secondary();
                t = ts.get();
                break;
            case '/':
                {
                    double d = secondary();
                    if (d == 0) error("divide by zero");
                    left /= d;
                    t = ts.get();
                    break;
                }
            case '%':
                {
                    double d = secondary();
                    if (d == 0) error("%: divide by zero");
                    left = fmod(left, d);
                    t = ts.get();
                    break;
                }
            default:
                ts.putback(t);      // put t back into the Token_stream
                return left;
        }
    }
}

double expression()         // deal with + and -
{
    double left = term();           // read and evaluate a term
    Token t = ts.get();             // get next token from Token_stream

    while (true) {
        switch (t.kind) {
            case '+':
                left += term();     // evaluate term and add
                t = ts.get();
                break;
            case '-':
                left -= term();     // evaluate term and subtract
                t = ts.get();
                break;
            default:
                ts.putback(t);      // put t back into the token stream
                return left;
        }
    }
}

double bitwise()            // deal with &, |, ^ and ~
    // bitwise operators only work on integers so all double results are cast
    // to integers via C-style casting
{
    Token t = ts.get();             // check for unary ~

    if (t.kind == '~')
        return ~(int)expression();
    else
        ts.putback(t);

    double left = expression();
    t = ts.get();

    while (true) {
        switch (t.kind) {
            case '&':
            case '|':
            case '^':
                {
                    int temp = int(left);

                    switch (t.kind) {
                        case '&':
                            temp &= int(expression());
                            break;
                        case '|':
                            temp |= int(expression());
                            break;
                        case '^':
                            temp ^= int(expression());
                            break;
                        default:
                            // no default
                            break;
                    }
                    left = temp;
                    t = ts.get();
                }
                break;
            default:
                ts.putback(t);
                return left;
        }
    }
}

double declaration()
    // assume we have seen "let"
    // handle: name = expression
    // declare a variable called "name" with the initial value "expression"
{
    Token t = ts.get();
    if (t.kind != name) error("name expected in declaration");
    string var_name = t.name;

    Token t2 = ts.get();
    if (t2.kind != '=') error("= missing in declaration of ", var_name);

    double d = bitwise();
    st.define_name(var_name, d);
    return d;
}

double statement()
{
    Token t = ts.get();
    switch (t.kind) {
        case let:
            return declaration();
        default:
            ts.putback(t);
            return bitwise();
    }
}

void print_help()
{
    cout << "Simple Calculator Manual\n"
         << "========================\n"
         << "This calculator program supports +, -, *, and / operations\n"
         << "Enter any form of compound statement followed by ';' for result\n"
         << "- ex: 4 + 1; (5-2)/{6*(8+14)}\n"
         << "The modulo operator % may be used on all numbers\n"
         << "An '!' placed after a value will calculate the factorial of it\n"
         << "- ex: 4! = 4 * 3 * 2 * 1\n"
         << "Square root and exponentiation are provided by 'sqrt' and 'pow'\n"
         << "- ex: sqrt(25) = 5, pow(5,2) = 25\n"
         << "Variable assignment is provided using the 'let' keyword:\n"
         << "- ex: let x = 37; x * 2 = 74; x = 4; x * 2 = 8\n\n";
}

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// expression evaluation loop
// §7.7 recovering from errors
void clean_up_mess()
{
    ts.ignore(print);
}

// §7.6.2 separate main's extra action
void calculate()
{
    while (cin)
        try {
            cout << prompt;
            Token t = ts.get();
            while (t.kind == print) t = ts.get();   // discard extra 'prints'
            if (t.kind == help) print_help();
            else if (t.kind == quit) return;
            else {
                ts.putback(t);
                cout << result << statement() << '\n';
            }
        }
        catch (exception& e) {
            cerr << e.what() << '\n';       // write error message to user
            clean_up_mess();
        }
}

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

int main()
try {
    st.define_name("pi", 3.1415926535, true);       // hardcoded constants
    st.define_name("e", 2.7182818284, true);

    cout << "Simple Calculator (type ? for help)\n";

    calculate();
    return 0;
}
catch(exception& e) {
    cerr << "Exception: " << e.what() << '\n';
    return 1;
}
catch(...) {
    cerr << "Unknown exception\n";
    return 2;
}

/*
    Feb 18, 2018

    Returning to this after months of moving on I can see where I got lost. I
    had followed the text all along but when I neared the end of Chapter 7 I
    could not figure out why my code wouldn't work as the text suggests.

    In §7.8.2 we alter the Token_stream to accept names, specifically in the
    default clause of our switch statement. Then we add constructors to Token
    to allow for the creation of variable names. What we don't cover in the
    text is modifying primary() to work with variable names.

    This was the cause of a major table_flipping moment for me and I hope that
    if someone reads this they can save themselves the frustration.
*/

redpajama_set_name: RedPajamaGithub


text:
"use strict";

const {Cc,Ci,Cr} = require("chrome");
const byteStreams = require("./byte-streams");
const textStreams = require("./text-streams");

// Flags passed when opening a file.  See nsprpub/pr/include/prio.h.
const OPEN_FLAGS = {
  RDONLY: parseInt("0x01"),
  WRONLY: parseInt("0x02"),
  CREATE_FILE: parseInt("0x08"),
  APPEND: parseInt("0x10"),
  TRUNCATE: parseInt("0x20"),
  EXCL: parseInt("0x80")
};

var dirsvc = Cc["@mozilla.org/file/directory_service;1"]
             .getService(Ci.nsIProperties);

function MozFile(path) {
  var file = Cc['@mozilla.org/file/local;1']
             .createInstance(Ci.nsILocalFile);
  file.initWithPath(path);
  return file;
}

function ensureReadable(file) {
  if (!file.isReadable())
    throw new Error("path is not readable: " + file.path);
}

function ensureDir(file) {
  ensureExists(file);
  if (!file.isDirectory())
    throw new Error("path is not a directory: " + file.path);
}

function ensureFile(file) {
  ensureExists(file);
  if (!file.isFile())
    throw new Error("path is not a file: " + file.path);
}

function ensureExists(file) {
  if (!file.exists())
    throw friendlyError(Cr.NS_ERROR_FILE_NOT_FOUND, file.path);
}

function friendlyError(errOrResult, filename) {
  var isResult = typeof(errOrResult) === "number";
  var result = isResult ? errOrResult : errOrResult.result;
  switch (result) {
  case Cr.NS_ERROR_FILE_NOT_FOUND:
    return new Error("path does not exist: " + filename);
  }
  return isResult ? new Error("XPCOM error code: " + errOrResult) : errOrResult;
}

exports.exists = function exists(filename) {
  return MozFile(filename).exists();
};

exports.isFile = function isFile(filename) {
  return MozFile(filename).isFile();
};

exports.read = function read(filename, mode) {
  if (typeof(mode) !== "string")
    mode = "";

  // Ensure mode is read-only.
  mode = /b/.test(mode) ? "b" : "";

  var stream = exports.open(filename, mode);
  try {
    var str = stream.read();
  }
  finally {
    stream.close();
  }

  return str;
};

exports.join = function join(base) {
  if (arguments.length < 2)
    throw new Error("need at least 2 args");
  base = MozFile(base);
  for (var i = 1; i < arguments.length; i++)
    base.append(arguments[i]);
  return base.path;
};

exports.dirname = function dirname(path) {
  var parent = MozFile(path).parent;
  return parent ? parent.path : "";
};

exports.basename = function basename(path) {
  var leafName = MozFile(path).leafName;

  // On Windows, leafName when the path is a volume letter and colon ("c:") is
  // the path itself.  But such a path has no basename, so we want the empty
  // string.
  return leafName == path ? "" : leafName;
};

exports.list = function list(path) {
  var file = MozFile(path);
  ensureDir(file);
  ensureReadable(file);

  var entries = file.directoryEntries;
  var entryNames = [];
  while(entries.hasMoreElements()) {
    var entry = entries.getNext();
    entry.QueryInterface(Ci.nsIFile);
    entryNames.push(entry.leafName);
  }
  return entryNames;
};

exports.open = function open(filename, mode) {
  var file = MozFile(filename);
  if (typeof(mode) !== "string")
    mode = "";

  // File opened for write only.
  if (/w/.test(mode)) {
    if (file.exists())
      ensureFile(file);
    var stream = Cc['@mozilla.org/network/file-output-stream;1'].
                 createInstance(Ci.nsIFileOutputStream);
    var openFlags = OPEN_FLAGS.WRONLY |
                    OPEN_FLAGS.CREATE_FILE |
                    OPEN_FLAGS.TRUNCATE;
    var permFlags = parseInt("0644"); // u+rw go+r
    try {
      stream.init(file, openFlags, permFlags, 0);
    }
    catch (err) {
      throw friendlyError(err, filename);
    }
    return /b/.test(mode) ?
           new byteStreams.ByteWriter(stream) :
           new textStreams.TextWriter(stream);
  }

  // File opened for read only, the default.
  ensureFile(file);
  stream = Cc['@mozilla.org/network/file-input-stream;1'].
           createInstance(Ci.nsIFileInputStream);
  try {
    stream.init(file, OPEN_FLAGS.RDONLY, 0, 0);
  }
  catch (err) {
    throw friendlyError(err, filename);
  }
  return /b/.test(mode) ?
         new byteStreams.ByteReader(stream) :
         new textStreams.TextReader(stream);
};

exports.remove = function remove(path) {
  var file = MozFile(path);
  ensureFile(file);
  file.remove(false);
};

exports.mkpath = function mkpath(path) {
  var file = MozFile(path);
  if (!file.exists())
    file.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt("0755")); // u+rwx go+rx
  else if (!file.isDirectory())
    throw new Error("The path already exists and is not a directory: " + path);
};

exports.rmdir = function rmdir(path) {
  var file = MozFile(path);
  ensureDir(file);
  try {
    file.remove(false);
  }
  catch (err) {
    // Bug 566950 explains why we're not catching a specific exception here.
    throw new Error("The directory is not empty: " + path);
  }
};


text:
package org.hoteia.qalingo.core.web.handler.security;

import org.springframework.security.web.DefaultRedirectStrategy;

public class ExtRedirectStrategy extends DefaultRedirectStrategy {

}


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

import io.airlift.slice.Slice;
import io.airlift.slice.SliceOutput;
import io.airlift.slice.Slices;
import org.apache.hadoop.io.WritableUtils;
import org.testng.annotations.Test;

import java.io.IOException;

import static org.testng.Assert.assertEquals;

public class TestRcFileDecoderUtils
{
    @Test
    public void testVInt()
            throws Exception
    {
        Slice slice = Slices.allocate(100);
        SliceOutput output = slice.getOutput();

        assertVIntRoundTrip(output, 0);
        assertVIntRoundTrip(output, 1);
        assertVIntRoundTrip(output, -1);
        assertVIntRoundTrip(output, Integer.MAX_VALUE);
        assertVIntRoundTrip(output, Integer.MAX_VALUE + 1L);
        assertVIntRoundTrip(output, Integer.MAX_VALUE - 1L);
        assertVIntRoundTrip(output, Integer.MIN_VALUE);
        assertVIntRoundTrip(output, Integer.MIN_VALUE + 1L);
        assertVIntRoundTrip(output, Integer.MIN_VALUE - 1L);
        assertVIntRoundTrip(output, Long.MAX_VALUE);
        assertVIntRoundTrip(output, Long.MAX_VALUE - 1);
        assertVIntRoundTrip(output, Long.MIN_VALUE + 1);

        for (int value = -100_000; value < 100_000; value++) {
            assertVIntRoundTrip(output, value);
        }
    }

    private static void assertVIntRoundTrip(SliceOutput output, long value)
            throws IOException
    {
        Slice oldBytes = writeVintOld(output, value);

        long readValueOld = WritableUtils.readVLong(oldBytes.getInput());
        assertEquals(readValueOld, value);

        long readValueNew = RcFileDecoderUtils.readVInt(oldBytes, 0);
        assertEquals(readValueNew, value);

        long readValueNewStream = RcFileDecoderUtils.readVInt(oldBytes.getInput());
        assertEquals(readValueNewStream, value);
    }

    private static Slice writeVintOld(SliceOutput output, long value)
            throws IOException
    {
        output.reset();
        WritableUtils.writeVLong(output, value);
        Slice vLongOld = Slices.copyOf(output.slice());

        output.reset();
        RcFileDecoderUtils.writeVLong(output, value);
        Slice vLongNew = Slices.copyOf(output.slice());
        assertEquals(vLongNew, vLongOld);

        if (value == (int) value) {
            output.reset();
            WritableUtils.writeVInt(output, (int) value);
            Slice vIntOld = Slices.copyOf(output.slice());
            assertEquals(vIntOld, vLongOld);

            output.reset();
            RcFileDecoderUtils.writeVInt(output, (int) value);
            Slice vIntNew = Slices.copyOf(output.slice());
            assertEquals(vIntNew, vLongOld);
        }
        return vLongOld;
    }
}


text:
package modules.streaming;

import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import java.util.TreeSet;

import messages.ChunkMessage;
import messages.Message;
import modules.P2PClient;
import modules.overlays.MultipleTreeOverlayModule;
import utils.Utils;
import entites.VideoStreamChunk;
import experiment.frameworks.NodeAddress;

public class PushMultipleDescriptionTree extends StreamingModule {
  public PushMultipleDescriptionTree(final P2PClient client, final MultipleTreeOverlayModule<?> overlay, final Random r) {
    super(client, overlay, r);
  }

  long lastReceivedChunkIndex = 0;

  @Override public void handleMessage(final Message message) {
    super.handleMessage(message);
    if (message instanceof ChunkMessage) {
      final VideoStreamChunk chunk = ((ChunkMessage) message).chunk;
      lastReceivedChunkIndex = chunk.index;
      if (client.player.getVs() == null) {
        client.player.initVS(lastReceivedChunkIndex);
      }
      client.player.getVs().updateChunk(chunk);
      if (chunk.index < fromChunk || chunk.index > latestChunk) {
        // return;
        System.out.println("sending chunk " + chunk.index + " out of protocol bounds: " + fromChunk + "-" + latestChunk);
      }
      disseminateChunkToSons(chunk);
    }
  }

  private void disseminateChunkToSons(final VideoStreamChunk chunk) {
    final Map<NodeAddress, TreeSet<Integer>> sonToChunks = new TreeMap<NodeAddress, TreeSet<Integer>>();
    for (final int i : chunk.getDescriptions()) {
      for (final NodeAddress son : ((MultipleTreeOverlayModule<?>) mainOverlay).getSonNodes(i)) {
        Utils.checkExistence(sonToChunks, son, new TreeSet<Integer>());
        sonToChunks.get(son).add(i);
      }
    }
    for (final NodeAddress son : sonToChunks.keySet()) {
      network.send(new ChunkMessage(getMessageTag(), network.getAddress(), son, chunk.getMDChunk(sonToChunks.get(son))));
    }
  }

  @Override public void nextCycle() {
    super.nextCycle();
    if (!network.isServerMode()) {
      return;
    }
    final VideoStreamChunk chunk = client.player.getVs().getLatestChunk();
    if (chunk == null) {
      return;
    }
    if (chunk.index < fromChunk || chunk.index > latestChunk) {
      // return;
      System.out.println("sending chunk " + chunk.index + " out of protocol bounds: " + fromChunk + "-" + latestChunk);
    }
    disseminateChunkToSons(chunk);
  }

  @Override public boolean isOverlayConnected() {
    return mainOverlay.isOverlayConnected() && client.player.getVs() != null
        && client.player.getVs().getFirstMissingChunk() > lastReceivedChunkIndex;
  }
}


text:
package com.android.deskclock;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Displays text with no padding at the top.
 */
public class ZeroTopPaddingTextView extends TextView {
    private static final float NORMAL_FONT_PADDING_RATIO = 0.328f;
    // the bold fontface has less empty space on the top
    private static final float BOLD_FONT_PADDING_RATIO = 0.208f;

    private static final float NORMAL_FONT_BOTTOM_PADDING_RATIO = 0.25f;
    // the bold fontface has less empty space on the top
    private static final float BOLD_FONT_BOTTOM_PADDING_RATIO = 0.208f;

    private static final Typeface SAN_SERIF_BOLD = Typeface.create("san-serif", Typeface.BOLD);

    private int mPaddingRight = 0;

    public ZeroTopPaddingTextView(Context context) {
        this(context, null);
    }

    public ZeroTopPaddingTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZeroTopPaddingTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setIncludeFontPadding(false);
        updatePadding();
    }

    public void updatePadding() {
        float paddingRatio = NORMAL_FONT_PADDING_RATIO;
        float bottomPaddingRatio = NORMAL_FONT_BOTTOM_PADDING_RATIO;
        if (getTypeface().equals(SAN_SERIF_BOLD)) {
            paddingRatio = BOLD_FONT_PADDING_RATIO;
            bottomPaddingRatio = BOLD_FONT_BOTTOM_PADDING_RATIO;
        }
        // no need to scale by display density because getTextSize() already returns the font
        // height in px
        setPadding(0, (int) (-paddingRatio * getTextSize()), mPaddingRight,
                (int) (-bottomPaddingRatio * getTextSize()));
    }

    public void setPaddingRight(int padding) {
        mPaddingRight = padding;
        updatePadding();
    }
}


text:
export interface Rss2JsonResponseItem {
  /** The title of the item. */
  title?: string;
  /** The date when the item was published at. */
  pubDate?: string;
  /** The URL of the item. */
  link?: string;
  /** A string that uniquely identifies the item. */
  guid?: string;
  /** The author of the item. */
  author?: string;
  /** The thumbnail URL of the item. */
  thumbnail?: string;
  /** The item synopsis. */
  description?: string;
  /** The content of the item. */
  content?: string;
  /** Describes a media object that is attached to the item. */
  enclosure?: {
    [key: string]: any;
  };
  /** A list of categories that the item is in. */
  categories?: string[];
}


text:
module Spree
  class SalePrice < ActiveRecord::Base
    # TODO validations
    belongs_to :price, :class_name => "Spree::Price"
    has_one :calculator, :class_name => "Spree::Calculator", :as => :calculable, :dependent => :destroy
    accepts_nested_attributes_for :calculator
    validates :calculator, :presence => true

    #attr_accessible :value, :start_at, :end_at, :enabled

    scope :active, lambda {
      where("enabled = 't' AND (start_at <= ? OR start_at IS NULL) AND (end_at >= ? OR end_at IS NULL)", Time.now, Time.now)
    }

    # TODO make this work or remove it
    #def self.calculators
    #  Rails.application.config.spree.calculators.send(self.to_s.tableize.gsub('/', '_').sub('spree_', ''))
    #end

    def calculator_type
      calculator.class.to_s if calculator
    end

    def calculator_type=(calculator_type)
      clazz = calculator_type.constantize if calculator_type
      self.calculator = clazz.new if clazz and not self.calculator.is_a? clazz
    end

    def price
      calculator.compute self
    end

    def enable
      update_attribute(:enabled, true)
    end

    def disable
      update_attribute(:enabled, false)
    end

    def start(end_time = nil)
      end_time = nil if end_time.present? && end_time <= Time.now # if end_time is not in the future then make it nil (no end)
      attr = { end_at: end_time, enabled: true }
      attr[:start_at] = Time.now if self.start_at.present? && self.start_at > Time.now # only set start_at if it's not set in the past
      update_attributes(attr)
    end

    def stop
      update_attributes({ end_at: Time.now, enabled: false })
    end
  end
end


text:
var Promise = Promise || require("bluebird");

module.exports = function (knex) {
	this.knex = knex;

	return {
		getUser: function () {
			return knex("users")
			.then(function (rows){
				if (rows.length > 0) {
					return rows[0];
				} else {
					return false;
				}
			});
		}
	}
};


text:
package com.talobin.cardentry.interfaces;

/**
 * Created by hai on 5/4/15.
 */
public interface Callback {
    void onSuccess();

    void onError();

    public static class EmptyCallback implements Callback {

        @Override public void onSuccess() {
        }

        @Override public void onError() {
        }
    }
}


text:
from django_filters import CharFilter, FilterSet, ModelChoiceFilter, MultipleChoiceFilter, OrderingFilter

from .models import Annotation, Corpus, Tense, Label
from .utils import labels_to_choices

class AnnotationFilter(FilterSet):
    corpus = ModelChoiceFilter(label='Corpus',
                               field_name='alignment__original_fragment__document__corpus',
                               queryset=Corpus.objects.all())
    source_tense = ModelChoiceFilter(label='Tense',
                                     field_name='alignment__original_fragment__tense',
                                     queryset=Tense.objects.all())
    source_labels = MultipleChoiceFilter(choices=Label.objects.all())
    word_in_source = CharFilter(label='Word in source',
                                field_name='alignment__original_fragment__sentence__word__word',
                                lookup_expr='iexact',
                                help_text='Filters words in the source text (case-insensitive)',
                                distinct=True)
    word_in_target = CharFilter(label='Word in translation',
                                field_name='alignment__translated_fragment__sentence__word__word',
                                lookup_expr='iexact',
                                help_text='Filters words in the translated text (case-insensitive)',
                                distinct=True)
    labels = MultipleChoiceFilter(choices=Label.objects.all())

    o = OrderingFilter(
        fields=(
            ('annotated_at', 'annotated_at'),
            ('last_modified_at', 'last_modified_at'),
            ('alignment__original_fragment__document', 'document'),
        ),
    )

    class Meta:
        model = Annotation
        fields = ['corpus', 'is_no_target', 'is_translation', 'source_tense', 'source_labels', 'word_in_source',
                  'tense', 'labels', 'word_in_target', 'annotated_by']

    def __init__(self, l1, l2, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.filters['is_no_target'].label = self.filters['is_no_target'].label[:-12]  # Strips HTML tag from label
        self.filters['source_tense'].queryset = Tense.objects.filter(language__iso=l1)
        self.filters['tense'].queryset = Tense.objects.filter(language__iso=l2)
        # match labels in the relevant languages, or labels that are not language specific
        l1_labels_queryset = Label.objects.filter(language__isnull=True) | Label.objects.filter(language__iso=l1)
        l2_labels_queryset = Label.objects.filter(language__isnull=True) | Label.objects.filter(language__iso=l2)
        if kwargs.get('data') and kwargs.get('data').get('corpus'):
            l1_labels_queryset = l1_labels_queryset.filter(key__corpora=kwargs.get('data').get('corpus'))
            l2_labels_queryset = l2_labels_queryset.filter(key__corpora=kwargs.get('data').get('corpus'))

        self.filters['source_labels'] = MultipleChoiceFilter(label='Labels',
                                                             field_name='alignment__original_fragment__labels',
                                                             choices=labels_to_choices(l1_labels_queryset))
        self.filters['labels'] = MultipleChoiceFilter(label='Labels',
                                                      field_name='labels',
                                                      choices=labels_to_choices(l2_labels_queryset))


text:
import os, sys
import time
import pymongo
from datetime import date, datetime
from pymongo import MongoClient
import pickle

utils_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(utils_path)

from utils import tokenizer
from utils_new import saveObject, uploadObject

from nltk.stem import SnowballStemmer
from nltk.stem.snowball import SpanishStemmer

stemmer = SpanishStemmer()
eng_stemmer = SnowballStemmer("english")

import pdb,ipdb

## MongoDB authentication and connection
client = MongoClient()
# conexion a nueva base de datos
db = client.JobDB
# Coleccion de trabajos
job_posts = db.core_col
tokenized = db.core_col_tokenized

def insert(_dict, key, val = 1):
	if key not in _dict:
		_dict[key] = val
	else:
		_dict[key] += val

if __name__ == '__main__':
	word_dict = {}

	data = job_posts.find().batch_size(10000)
	case = 1
	for post in data:
		id = post['_id']
		job = tokenized.find_one({'$and':[{'_id.description':id['description'] },
								          {'_id.details':id['details'] }
										 ]})
		if job:
			print("Ya esta")
			continue

		text = '\n'.join([post.get('title',''),post.get('description','')])
		if text=='':
		  continue

		tokens = tokenizer(text)
		new_item = {'_id': id, 'tokens':tokens}

		"""
		for sent in tokens:
			for word in sent:
				#if 'movilidad' in word.lower() and len(word) > 9 and word.lower() != 'movilidades':
				#	ipdb.set_trace()

				#try:
				#	stem = stemmer.stem(word.lower())
				#except:
				#	stem = eng_stemmer.stem(word.lower())
				#insert(stem_dict,stem)
				insert(word_dict,word.lower())
		"""

		if case % 1000 == 0:
			print("->",case)

		try:
			tokenized.insert({'_id': post['_id'], 'tokens':tokens})
		except:
			print("REPETIDO!")

		case += 1

	"""
	data = tokenized.find().batch_size(10000)
	case = 1
	for post in data:
		tokens = post['tokens']
		id = post['_id']

		if case % 1000 == 0:
			print("->",case)

		for sent in tokens:
			for word in sent:
				if "´practicante" in word:
					ipdb.set_trace()

				try:
					stem = stemmer.stem(word.lower())
				except:
					stem = eng_stemmer.stem(word.lower())
				insert(stem_dict,stem)
				insert(word_dict,word.lower())

		case += 1
	"""

	#saveObject(word_dict,'word_dict')
	#saveObject(stem_dict,'stem_dict')


text:
module Houston::Roadmaps
  class GoalsController < ApplicationController
    before_action :authenticate_user!
    before_action :find_goal
    attr_reader :goal

    layout "houston/roadmaps/application"

    def show
      authorize! :read, goal
      @project = goal.project

      roadmap = Roadmap.find_by_id(params[:roadmap_id]) if params[:roadmap_id]
      @milestone = roadmap.milestones.find { |milestone| milestone["type"] == "Goal" && milestone["id"] == goal.id } if roadmap
      @goal.name = @milestone["name"] if @milestone && !@milestone["name"].blank?

      @title = "#{goal.name} • #{@project.name}"

      @connectable_accounts = %w{todoist}
      authorization = Todoist.for(current_user).granted.with_scope("data:read_write").first
      if authorization
        @connectable_accounts.delete "todoist"
        authorization.sync! unless authorization.synced?
        @unattached_todo_lists = authorization.todolists.order(:archived, :name).map { |todolist| Houston::Roadmaps::TodolistPresenter.new(todolist) }
      end
    end

    def update
      authorize! :update, goal
      if goal.update_attributes(goal_attributes)
        render json: Houston::Roadmaps::GoalPresenter.new(goal)
      else
        render json: goal.errors, status: :unprocessable_entity
      end
    end

  private

    def find_goal
      @goal = Goal.unscoped.find(params[:id])
    end

    def goal_attributes
      params.pick(:name, :closed)
    end

  end
end


text:
<?xml version="1.0" encoding="UTF-8"?>
<project
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
        xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mkyong</groupId>
    <artifactId>spring-security-helloworld-annotation</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>SpringSecurity Hello World XML</name>

    <properties>
        <jdk.version>1.8</jdk.version>
        <spring.version>4.1.0.RELEASE</spring.version>
        <spring.security.version>3.2.5.RELEASE</spring.security.version>
        <jstl.version>1.2</jstl.version>
        <javax.servlet.version>3.1.0</javax.servlet.version>
        <maven.compiler.plugin.version>2.5.1</maven.compiler.plugin.version>

        <spring.data.jpa.version>1.7.0.RELEASE</spring.data.jpa.version>
        <hibernate.version>4.3.6.Final</hibernate.version>
        <commons.dbcp.version>1.4</commons.dbcp.version>
        <hibernate.validator.version>5.1.2.Final</hibernate.validator.version>
        <commons.lang3.version>3.3.2</commons.lang3.version>
        <postgresql.version>9.1-901-1.jdbc4</postgresql.version>
        <junit.version>4.11</junit.version>
        <mockito.version>1.9.5</mockito.version>
    </properties>

    <dependencies>

        <!--new-->

        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>${postgresql.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--*-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!--*-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>${spring.data.jpa.version}</version>
        </dependency>
        <!--*-->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <!--*-->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>${hibernate.validator.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>${commons.dbcp.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons.lang3.version}</version>
            <scope>test</scope>
        </dependency>

        <!--end new-->

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${javax.servlet.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- Spring 3 dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Spring Security -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${spring.security.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring.security.version}</version>
        </dependency>

        <!-- jstl for jsp page -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>${mockito.version}</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <finalName>SpringSecurityHelloWorld</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>


text:
<?php

namespace Users\UsersBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Medica
 *
 * @ORM\Table(name="medica")
 * @ORM\Entity
 */
class Medica
{
    /**
     * @var integer
     *
     * @ORM\Column(name="Idmedica", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $idmedica;

    /**
     * @var string
     *
     * @ORM\Column(name="Nommedica", type="string", length=255, nullable=true)
     */
    private $nommedica;

    /**
     * @var string
     *
     * @ORM\Column(name="Typemedica", type="string", length=255, nullable=true)
     */
    private $typemedica;

    /**
     * @var integer
     *
     * @ORM\Column(name="quantitemedica", type="integer", nullable=true)
     */
    private $quantitemedica;

    /**
     * @var float
     *
     * @ORM\Column(name="prix", type="float", precision=10, scale=0, nullable=false)
     */
    private $prix;

    /**
     * @var string
     *
     * @ORM\Column(name="libelle", type="string", length=225, nullable=false)
     */
    private $libelle;

    /**
     * Get idmedica
     *
     * @return integer
     */
    public function getIdmedica()
    {
        return $this->idmedica;
    }

    /**
     * Set nommedica
     *
     * @param string $nommedica
     * @return Medica
     */
    public function setNommedica($nommedica)
    {
        $this->nommedica = $nommedica;

        return $this;
    }

    /**
     * Get nommedica
     *
     * @return string
     */
    public function getNommedica()
    {
        return $this->nommedica;
    }

    /**
     * Set typemedica
     *
     * @param string $typemedica
     * @return Medica
     */
    public function setTypemedica($typemedica)
    {
        $this->typemedica = $typemedica;

        return $this;
    }

    /**
     * Get typemedica
     *
     * @return string
     */
    public function getTypemedica()
    {
        return $this->typemedica;
    }

    /**
     * Set quantitemedica
     *
     * @param integer $quantitemedica
     * @return Medica
     */
    public function setQuantitemedica($quantitemedica)
    {
        $this->quantitemedica = $quantitemedica;

        return $this;
    }

    /**
     * Get quantitemedica
     *
     * @return integer
     */
    public function getQuantitemedica()
    {
        return $this->quantitemedica;
    }

    /**
     * Set prix
     *
     * @param float $prix
     * @return Medica
     */
    public function setPrix($prix)
    {
        $this->prix = $prix;

        return $this;
    }

    /**
     * Get prix
     *
     * @return float
     */
    public function getPrix()
    {
        return $this->prix;
    }

    /**
     * Set libelle
     *
     * @param string $libelle
     * @return Medica
     */
    public function setLibelle($libelle)
    {
        $this->libelle = $libelle;

        return $this;
    }

    /**
     * Get libelle
     *
     * @return string
     */
    public function getLibelle()
    {
        return $this->libelle;
    }
}


text:
import { dirname, join, relative, sep } from 'path'
import { promises as fs } from 'fs'
import { ComponentDoc, Tag, ParamTag } from 'vue-docgen-api'
import { findFileCaseInsensitive } from './utils'

export function getExamplesFilePaths(
	tags: { [key: string]: (Tag | ParamTag)[] },
	componentDirname: string
): string[] {
	const exampleTags = [...(tags.example || []), ...(tags.examples || [])]

	return (
		exampleTags.map((exampleTag: ParamTag | Tag) =>
			isParamTag(exampleTag)
				? join(componentDirname, exampleTag.description as string)
				: join(componentDirname, exampleTag.content as string)
		) || []
	)
}

export default async function getDocsBlocks(
	absolutePath: string,
	doc: Pick<ComponentDoc, 'tags' | 'docsBlocks'>,
	getDocFileName: (file: string) => string | false,
	rootPath: string,
	editLinkLabel: string,
	getRepoEditUrl?: (path: string) => string
): Promise<string[]> {
	const docsBlocks = doc.docsBlocks || []

	const docFilePath = getRepoEditUrl
		? findFileCaseInsensitive(getDocFileName(absolutePath) || '')
		: getDocFileName(absolutePath)
	if (docFilePath) {
		docsBlocks.push(`${
			getRepoEditUrl
				? `
${editLinkLabel}
`
				: ''
		}
${await fs.readFile(docFilePath, 'utf8')}`)
	}

	// load @examples tags into the docsBlocks
	if (doc.tags?.example || doc.tags?.examples) {
		const componentDirname = dirname(absolutePath)
		const examplesFilePaths = getExamplesFilePaths(doc.tags, componentDirname)
		await Promise.all(
			examplesFilePaths.map(async examplePath => {
				const ep = getRepoEditUrl ? findFileCaseInsensitive(examplePath) : examplePath
				if (ep) {
					docsBlocks.push(`${
						getRepoEditUrl
							? `
${editLinkLabel}
`
							: ''
					}
${await fs.readFile(ep, 'utf8')}`)
				}
			})
		)
	}

	return docsBlocks
}

const separatorRE = new RegExp(`\\${sep}`, 'g')

function relativeUrl(rootPath: string, docFilePath: string): string {
	return relative(rootPath, docFilePath).replace(separatorRE, '/')
}

export function isParamTag(tag: ParamTag | Tag): tag is ParamTag {
	return !!(tag as ParamTag).description
}


text:
package com.softwaremill.bootzooka.test

import com.softwaremill.bootzooka.common.sql.SqlDatabase
import com.softwaremill.bootzooka.email.application.{DummyEmailService, EmailTemplatingEngine}
import com.softwaremill.bootzooka.user.application.{UserDao, UserService}
import com.softwaremill.bootzooka.user.domain.User
import org.scalatest.concurrent.ScalaFutures

import scala.concurrent.ExecutionContext

trait TestHelpersWithDb extends TestHelpers with ScalaFutures {

  lazy val emailService          = new DummyEmailService()
  lazy val emailTemplatingEngine = new EmailTemplatingEngine
  lazy val userDao               = new UserDao(sqlDatabase)
  lazy val userService           = new UserService(userDao, emailService, emailTemplatingEngine, passwordHashing)

  def sqlDatabase: SqlDatabase

  implicit lazy val ec: ExecutionContext = scala.concurrent.ExecutionContext.Implicits.global

  def newRandomStoredUser(password: Option[String] = None): User = {
    val u = newRandomUser(password)
    userDao.add(u).futureValue
    u
  }
}


text:
require 'packer/builders/amazon'

module Packer
  module Builders
    class AmazonInstance < Amazon # :nodoc:
    end
  end
end

# vim:set ft=ruby :


text:
package org.deepdive.test.unit

import anorm._
import org.scalatest._
import play.api.libs.json._
import org.deepdive.datastore.DataStoreUtils

class DatabaseUtilsSpec extends FunSpec {

  describe("Converting a JSON row to an Anorm Sequence") {

    it("should work") {
      val row = JsObject(Map[String, JsValue](
        "nullField" -> JsNull,
        "stringField" -> JsString("I am a string"),
        "longField" -> JsNumber(100),
        "booleanField" -> JsBoolean(true)
      ).toSeq)
      val result = DataStoreUtils.jsonRowToAnormSeq(row).toMap
      assert(result.mapValues(_.aValue) == Map(
        ("nullField", null),
        ("stringField", "I am a string"),
        ("longField", 100),
        ("booleanField", true)
      ))
    }
  }

}


text:
<!doctype html>
<title>undefined as argument</title>
<link rel=help href="http://www.whatwg.org/html/#dom-worker">
<link rel=help href="http://dev.w3.org/2006/webapi/WebIDL/#es-DOMString">


text:
package org.apache.accumulo.core.client.impl;

import static com.google.common.base.Preconditions.checkArgument;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;

import org.apache.accumulo.core.Constants;
import org.apache.accumulo.core.client.Instance;
import org.apache.accumulo.core.client.Scanner;
import org.apache.accumulo.core.data.Key;
import org.apache.accumulo.core.data.Range;
import org.apache.accumulo.core.data.Value;
import org.apache.accumulo.core.security.Authorizations;
import org.apache.accumulo.core.security.Credentials;
import org.apache.hadoop.io.Text;

/**
 * provides scanner functionality
 *
 * "Clients can iterate over multiple column families, and there are several mechanisms for limiting the rows, columns, and timestamps traversed by a scan. For
 * example, we could restrict [a] scan ... to only produce anchors whose columns match [a] regular expression ..., or to only produce anchors whose timestamps
 * fall within ten days of the current time."
 *
 */
public class ScannerImpl extends ScannerOptions implements Scanner {

  // keep a list of columns over which to scan
  // keep track of the last thing read
  // hopefully, we can track all the state in the scanner on the client
  // and just query for the next highest row from the tablet server

  private Instance instance;
  private Credentials credentials;
  private Authorizations authorizations;
  private Text table;

  private int size;

  private Range range;
  private boolean isolated = false;
  private long readaheadThreshold = Constants.SCANNER_DEFAULT_READAHEAD_THRESHOLD;

  public ScannerImpl(Instance instance, Credentials credentials, String table, Authorizations authorizations) {
    checkArgument(instance != null, "instance is null");
    checkArgument(credentials != null, "credentials is null");
    checkArgument(table != null, "table is null");
    checkArgument(authorizations != null, "authorizations is null");
    this.instance = instance;
    this.credentials = credentials;
    this.table = new Text(table);
    this.range = new Range((Key) null, (Key) null);
    this.authorizations = authorizations;

    this.size = Constants.SCAN_BATCH_SIZE;
  }

  @Override
  public synchronized void setRange(Range range) {
    checkArgument(range != null, "range is null");
    this.range = range;
  }

  @Override
  public synchronized Range getRange() {
    return range;
  }

  @Override
  public synchronized void setBatchSize(int size) {
    if (size > 0)
      this.size = size;
    else
      throw new IllegalArgumentException("size must be greater than zero");
  }

  @Override
  public synchronized int getBatchSize() {
    return size;
  }

  /**
   * Returns an iterator over an accumulo table. This iterator uses the options that are currently set on the scanner for its lifetime. So setting options on a
   * Scanner object will have no effect on existing iterators.
   */
  @Override
  public synchronized Iterator<Entry<Key,Value>> iterator() {
    return new ScannerIterator(instance, credentials, table, authorizations, range, size, getTimeOut(), this, isolated, readaheadThreshold);
  }

  @Override
  public synchronized void enableIsolation() {
    this.isolated = true;
  }

  @Override
  public synchronized void disableIsolation() {
    this.isolated = false;
  }

  @Deprecated
  @Override
  public void setTimeOut(int timeOut) {
    if (timeOut == Integer.MAX_VALUE)
      setTimeout(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
    else
      setTimeout(timeOut, TimeUnit.SECONDS);
  }

  @Deprecated
  @Override
  public int getTimeOut() {
    long timeout = getTimeout(TimeUnit.SECONDS);
    if (timeout >= Integer.MAX_VALUE)
      return Integer.MAX_VALUE;
    return (int) timeout;
  }

  @Override
  public synchronized void setReadaheadThreshold(long batches) {
    if (0 > batches) {
      throw new IllegalArgumentException("Number of batches before read-ahead must be non-negative");
    }

    readaheadThreshold = batches;
  }

  @Override
  public synchronized long getReadaheadThreshold() {
    return readaheadThreshold;
  }
}


text:
"""Represent a mongod / mongos instance"""

from pymongo import common
from pymongo.errors import ConfigurationError
from pymongo.read_preferences import ReadPreference

# Member states
PRIMARY = 1
SECONDARY = 2
ARBITER = 3
OTHER = 4

# TODO: rename 'Server' or 'ServerDescription'.
class Member(object):
    """Immutable representation of one server.

    :Parameters:
      - `host`: A (host, port) pair
      - `connection_pool`: A Pool instance
      - `ismaster_response`: A dict, MongoDB's ismaster response
      - `ping_time`: A MovingAverage instance
    """
    # For unittesting only. Use under no circumstances!
    _host_to_ping_time = {}

    def __init__(self, host, connection_pool, ismaster_response, ping_time):
        self.host = host
        self.pool = connection_pool
        self.ismaster_response = ismaster_response
        self.ping_time = ping_time
        self.is_mongos = (ismaster_response.get('msg') == 'isdbgrid')

        if ismaster_response['ismaster']:
            self.state = PRIMARY
        elif ismaster_response.get('secondary'):
            self.state = SECONDARY
        elif ismaster_response.get('arbiterOnly'):
            self.state = ARBITER
        else:
            self.state = OTHER

        self.set_name = ismaster_response.get('setName')
        self.tags = ismaster_response.get('tags', {})
        self.max_bson_size = ismaster_response.get(
            'maxBsonObjectSize', common.MAX_BSON_SIZE)
        self.max_message_size = ismaster_response.get(
            'maxMessageSizeBytes', 2 * self.max_bson_size)
        self.min_wire_version = ismaster_response.get(
            'minWireVersion', common.MIN_WIRE_VERSION)
        self.max_wire_version = ismaster_response.get(
            'maxWireVersion', common.MAX_WIRE_VERSION)
        self.max_write_batch_size = ismaster_response.get(
            'maxWriteBatchSize', common.MAX_WRITE_BATCH_SIZE)

        # self.min/max_wire_version is the server's wire protocol.
        # MIN/MAX_SUPPORTED_WIRE_VERSION is what PyMongo supports.
        if (
            # Server too new.
            common.MAX_SUPPORTED_WIRE_VERSION < self.min_wire_version
            # Server too old.
            or common.MIN_SUPPORTED_WIRE_VERSION > self.max_wire_version
        ):
            raise ConfigurationError(
                "Server at %s:%d uses wire protocol versions %d through %d, "
                "but PyMongo only supports %d through %d"
                % (self.host[0], self.host[1],
                   self.min_wire_version, self.max_wire_version,
                   common.MIN_SUPPORTED_WIRE_VERSION,
                   common.MAX_SUPPORTED_WIRE_VERSION))

    def clone_with(self, ismaster_response, ping_time_sample):
        """Get a clone updated with ismaster response and a single ping time.
        """
        ping_time = self.ping_time.clone_with(ping_time_sample)
        return Member(self.host, self.pool, ismaster_response, ping_time)

    @property
    def is_primary(self):
        return self.state == PRIMARY

    @property
    def is_secondary(self):
        return self.state == SECONDARY

    @property
    def is_arbiter(self):
        return self.state == ARBITER

    def get_avg_ping_time(self):
        """Get a moving average of this member's ping times.
        """
        if self.host in Member._host_to_ping_time:
            # Simulate ping times for unittesting
            return Member._host_to_ping_time[self.host]

        return self.ping_time.get()

    def matches_mode(self, mode):
        assert not self.is_mongos, \
            "Tried to match read preference mode on a mongos Member"

        if mode == ReadPreference.PRIMARY and not self.is_primary:
            return False

        if mode == ReadPreference.SECONDARY and not self.is_secondary:
            return False

        # If we're not primary or secondary, then we're in a state like
        # RECOVERING and we don't match any mode
        return self.is_primary or self.is_secondary

    def matches_tags(self, tags):
        """Return True if this member's tags are a superset of the passed-in
           tags. E.g., if this member is tagged {'dc': 'ny', 'rack': '1'},
           then it matches {'dc': 'ny'}.
        """
        for key, value in tags.items():
            if key not in self.tags or self.tags[key] != value:
                return False

        return True

    def matches_tag_sets(self, tag_sets):
        """Return True if this member matches any of the tag sets, e.g.
           [{'dc': 'ny'}, {'dc': 'la'}, {}]
        """
        for tags in tag_sets:
            if self.matches_tags(tags):
                return True

        return False

    def __str__(self):
        return '<Member "%s:%s" primary=%r>' % (
            self.host[0], self.host[1], self.is_primary)


text:
* {margin: 0; padding: 0; border-width: 0;}
body {margin: 8px; background-color: #e0ffff; font-color: black; font-family: serif; line-height: 133%; max-width: 1024px;}
a:link {color: blue;}
a:visited {color: darkmagenta;}
a:hover {background-color: #a0ffff;}
a:active {color: darkmagenta; overflow: hidden; outline:none; position: relative; top: 1px; left: 1px;}
abbr {border-width: 1px;}

p {margin: 0 0 0.3em 1em;}
i {margin: 0 0.3em 0 0;}
b {margin: 0 0.1em;}
em {font-style: normal; font-weight: bold; margin: 0 0.1em;}
strong {}
pre {border: 1px dashed gray; margin: 0.5em 1em; padding: 0.5em; line-height: 1.2em; font-size: 85%; font-family: "Consolas", "Courier New", monospace; background-color: white;}
pre span.c {color: green;}
pre span.k {color: blue;}
pre span.arg {font-style: italic;}
tt {margin: 0 0.2em; font-size: 0.85em; font-family: "Consolas", "Courier New", monospace; }
tt.arg {font-style: italic;}
ol {margin: 0.5em 2.5em;}
ul {margin: 0.5em 2em;}
dl {margin: 0.5em 1em;}
dd {margin: 0 2em;}
dt {font-size: 0.85em; font-family: "Consolas", "Courier New", monospace;}
dl.par dt {margin: 0.5em 0 0 0 ; font-style: italic; }
dl.ret dt {margin: 0.5em 0 0 0 ; font-size: 0.85em; font-family: "Consolas", "Courier New", monospace;}
hr {border-width: 1px; margin: 1em;}
div.abst {font-family: sans-serif;}
div.para {clear: both; font-family: serif;}
div.ret a {font-size: 0.85em; font-family: "Consolas", "Courier New", monospace; }
.equ {text-indent: 0; margin: 1em 2em 1em;}
.indent {margin-left: 2em;}
.rset {float: right; margin: 0.3em 0 0.5em 0.5em;}
.lset {float: left; margin: 0.3em 0.5em 0.5em 0.5em;}
ul.flat li {list-style-type: none; margin: 0;}
a.imglnk img {border: 1px solid;}
.iequ {white-space: nowrap; font-weight: bold;}
.clr {clear: both;}
.it {font-style: italic;}
.mfd {font-size: 0.7em; padding: 0 1px; border: 1px solid; white-space : nowrap}
.ral {text-align: right; }
.lal {text-align: left; }
.cal {text-align: center; }

h1 {line-height: 1em; font-size: 2em; font-family: sans-serif; padding: 0.3em 0 0.3em;}
p.hdd {float: right; text-align: right; margin-top: 0.5em;}
hr.hds {clear: both; margin-bottom: 1em;}

h2 {font-size: 2em; font-family: sans-serif; background-color: #d8d8FF; padding: 0.5em 0.5em; margin: 0 0 0.5em;}
h3 {font-size: 1.5em; font-family: sans-serif; margin: 1.5em 0 0.5em;}
div.doc h3 {border-color: #b0d8d8; border-style: solid; border-width: 0px 0px 4px 12px; padding: 4px; margin-top: 2em;}
h4 {font-size: 1.2em; font-family: sans-serif; margin: 1em 0 0.2em;}
h5 {font-size: 1em; font-family: sans-serif; margin: 0.5em 0 0em;}
small {font-size: 80%;}
.indent {margin-left: 2em;}

/* Tables */
table {margin: 0.5em 1em; border-collapse: collapse; border: 2px solid black; }
th {background-color: white; border-style: solid; border-width: 1px 1px 2px; border-color: black; padding: 0 3px; vertical-align: top; white-space: nowrap;}
td {background-color: white; border: 1px solid black; padding: 0 3px; vertical-align: top; line-height: 1.3em;}
table.lst td:first-child {font-size: 0.85em; font-family: "Consolas", "Courier New", monospace;}
table.lst2 td {font-size: 0.85em; font-family: "Consolas", "Courier New", monospace;}
table caption {font-family: sans-serif; font-weight: bold;}
tr.lst3 td { border-width: 2px 1px 1px; }

p.foot {clear: both; text-indent: 0; margin: 1em 0.5em 1em;}