Example usage for java.util LinkedList isEmpty

List of usage examples for java.util LinkedList isEmpty

Introduction

In this page you can find the example usage for java.util LinkedList isEmpty.

Prototype

boolean isEmpty();

Source Link

Document

Returns true if this list contains no elements.

Usage

From source file:org.apache.hyracks.algebricks.rewriter.rules.EnforceStructuralPropertiesRule.java

private void addLocalEnforcers(AbstractLogicalOperator op, int i,
        List<ILocalStructuralProperty> localProperties, boolean nestedPlan, IOptimizationContext context)
        throws AlgebricksException {
    if (AlgebricksConfig.DEBUG) {
        AlgebricksConfig.ALGEBRICKS_LOGGER
                .fine(">>>> Adding local enforcers for local props = " + localProperties + "\n");
    }/*w w  w.ja va2s. c o  m*/

    if (localProperties == null || localProperties.isEmpty()) {
        return;
    }

    Mutable<ILogicalOperator> topOp = new MutableObject<>();
    topOp.setValue(op.getInputs().get(i).getValue());
    LinkedList<LocalOrderProperty> oList = new LinkedList<>();

    for (ILocalStructuralProperty prop : localProperties) {
        switch (prop.getPropertyType()) {
        case LOCAL_ORDER_PROPERTY: {
            oList.add((LocalOrderProperty) prop);
            break;
        }
        case LOCAL_GROUPING_PROPERTY: {
            LocalGroupingProperty g = (LocalGroupingProperty) prop;
            Collection<LogicalVariable> vars = (g.getPreferredOrderEnforcer() != null)
                    ? g.getPreferredOrderEnforcer()
                    : g.getColumnSet();
            List<OrderColumn> orderColumns = new ArrayList<>();
            for (LogicalVariable v : vars) {
                OrderColumn oc = new OrderColumn(v, OrderKind.ASC);
                orderColumns.add(oc);
            }
            LocalOrderProperty lop = new LocalOrderProperty(orderColumns);
            oList.add(lop);
            break;
        }
        default: {
            throw new IllegalStateException();
        }
        }
    }
    if (!oList.isEmpty()) {
        topOp = enforceOrderProperties(oList, topOp, nestedPlan, context);
    }

    op.getInputs().set(i, topOp);
    OperatorPropertiesUtil.computeSchemaAndPropertiesRecIfNull((AbstractLogicalOperator) topOp.getValue(),
            context);
    OperatorManipulationUtil.setOperatorMode(op);
    printOp((AbstractLogicalOperator) topOp.getValue());
}

From source file:com.erudika.para.persistence.CassandraDAO.java

@Override
public <P extends ParaObject> List<P> readPage(String appid, Pager pager) {
    LinkedList<P> results = new LinkedList<P>();
    if (StringUtils.isBlank(appid)) {
        return results;
    }//  w  ww  .  ja va 2  s.  co m
    if (pager == null) {
        pager = new Pager();
    }
    try {
        Statement st = new SimpleStatement(
                "SELECT json FROM " + CassandraUtils.getTableNameForAppid(appid) + ";");
        st.setFetchSize(pager.getLimit());
        String lastPage = pager.getLastKey();
        if (lastPage != null) {
            if ("end".equals(lastPage)) {
                return results;
            } else {
                st.setPagingState(PagingState.fromString(lastPage));
            }
        }
        ResultSet rs = getClient().execute(st);
        PagingState nextPage = rs.getExecutionInfo().getPagingState();

        int remaining = rs.getAvailableWithoutFetching();
        for (Row row : rs) {
            P obj = fromRow(row.getString("json"));
            if (obj != null) {
                results.add(obj);
            }
            if (--remaining == 0) {
                break;
            }
        }

        if (nextPage != null) {
            pager.setLastKey(nextPage.toString());
        } else {
            pager.setLastKey("end");
        }

        if (!results.isEmpty()) {
            pager.setCount(pager.getCount() + results.size());
        }
    } catch (Exception e) {
        logger.error(null, e);
    }
    logger.debug("readPage() page: {}, results:", pager.getPage(), results.size());
    return results;
}

From source file:gsn.http.datarequest.DownloadData.java

@Override
public void outputResult(OutputStream os) {

    PrintWriter respond = new PrintWriter(os);
    Iterator<Entry<String, AbstractQuery>> iter = qbuilder.getSqlQueries().entrySet().iterator();
    Entry<String, AbstractQuery> nextSqlQuery;
    DataEnumerator de = null;//from  w w  w.  j a va2 s . co m
    try {
        if (ot == AllowedOutputType.xml) {
            respond.println("<result>");
        }

        while (iter.hasNext()) {
            nextSqlQuery = iter.next();
            Connection connection = null;

            connection = Main.getStorage(nextSqlQuery.getKey()).getConnection();
            de = Main.getStorage(nextSqlQuery.getKey()).streamedExecuteQuery(nextSqlQuery.getValue(), true,
                    connection);

            //get units in hash map
            HashMap<String, String> fieldToUnitMap = new HashMap<String, String>();
            VSensorConfig sensorConfig = Mappings.getVSensorConfig(nextSqlQuery.getKey());
            DataField[] dataFieldArray = sensorConfig.getOutputStructure();
            for (DataField df : dataFieldArray) {
                String unit = df.getUnit();
                if (unit == null || unit.trim().length() == 0)
                    unit = "";

                fieldToUnitMap.put(df.getName().toLowerCase(), unit);
            }

            logger.debug("Data Enumerator: " + de);
            if (ot == AllowedOutputType.csv) {
                respond.println("# vsname:" + nextSqlQuery.getKey());
                respond.println("# query:" + nextSqlQuery.getValue().getStandardQuery()
                        + (nextSqlQuery.getValue().getLimitCriterion() == null ? ""
                                : "(" + nextSqlQuery.getValue().getLimitCriterion() + ")"));
                for (KeyValue df : sensorConfig.getAddressing()) {
                    respond.println(
                            "# " + df.getKey().toString().toLowerCase() + ":" + df.getValue().toString());
                }
                respond.println("# description:" + sensorConfig.getDescription());
            } else if (ot == AllowedOutputType.xml) {
                respond.println("\t<!-- " + nextSqlQuery.getValue().getStandardQuery() + " -->");
                for (KeyValue df : sensorConfig.getAddressing()) {
                    respond.println(
                            "\t<!-- " + StringEscapeUtils.escapeXml(df.getKey().toString().toLowerCase()) + ":"
                                    + StringEscapeUtils.escapeXml(df.getValue().toString()) + " -->");
                }
                respond.println("\t<!-- description:"
                        + StringEscapeUtils.escapeXml(sensorConfig.getDescription()) + " -->");
                respond.println("\t<data vsname=\"" + nextSqlQuery.getKey() + "\">");
            }

            FieldsCollection fc = qbuilder.getVsnamesAndStreams().get(nextSqlQuery.getKey());
            boolean wantTimed = true;
            boolean firstLine = true;
            LinkedList<StreamElement> streamElements = new LinkedList<StreamElement>();
            while (de.hasMoreElements()) {
                streamElements.add(de.nextElement());
            }

            double valsPerVS = MAX_SAMPLE_VALUES / numberOfFieldsInRequest();
            if (requestParameters.containsKey("sample")
                    && "true".equalsIgnoreCase(requestParameters.get("sample")[0])
                    && streamElements.size() > valsPerVS) {
                //sampling
                int numOfVals = streamElements.size();
                int left = (int) valsPerVS;
                int valsForAvg = (int) Math.ceil(numOfVals / valsPerVS);

                if (requestParameters.containsKey("sampling_percentage")) {
                    try {
                        String percentageString = requestParameters.get("sampling_percentage")[0];
                        int percentage = Integer.parseInt(percentageString);

                        if (percentage > 0 && percentage <= 100 && numOfVals * percentage > 100) {
                            left = numOfVals * percentage / 100;
                            valsForAvg = (int) Math.ceil(numOfVals / left);
                        }
                    } catch (Exception e) {
                    }
                }

                while (!streamElements.isEmpty()) {

                    StreamElement se = null;
                    if (numOfVals > left) {
                        StreamElement[] seForSampling = new StreamElement[valsForAvg];
                        for (int i = 0; i < valsForAvg; i++) {
                            seForSampling[i] = streamElements.removeLast();
                        }
                        numOfVals -= valsForAvg;
                        left--;
                        se = sampleSkip(seForSampling);
                    } else {
                        se = streamElements.removeLast();
                    }

                    if (ot == AllowedOutputType.csv) {
                        formatCSVElement(respond, se, wantTimed, csvDelimiter, firstLine, fieldToUnitMap);
                    } else if (ot == AllowedOutputType.xml) {
                        formatXMLElement(respond, se, wantTimed, firstLine, fieldToUnitMap);
                    }
                    firstLine = false;
                }
            } else {
                while (!streamElements.isEmpty()) {
                    if (ot == AllowedOutputType.csv) {
                        formatCSVElement(respond, streamElements.removeLast(), wantTimed, csvDelimiter,
                                firstLine, fieldToUnitMap);
                    } else if (ot == AllowedOutputType.xml) {
                        formatXMLElement(respond, streamElements.removeLast(), wantTimed, firstLine,
                                fieldToUnitMap);
                    }
                    firstLine = false;
                }
            }

            if (ot == AllowedOutputType.xml)
                respond.println("\t</data>");
        }
        if (ot == AllowedOutputType.xml) {
            respond.println("</result>");
        }

    } catch (SQLException e) {
        logger.debug(e.getMessage());
    } finally {
        respond.flush();
        if (de != null)
            de.close();
    }
}

From source file:org.nuxeo.ecm.core.storage.sql.db.H2Functions.java

/**
 * Rebuild the complete descendants tree.
 *///from w w w  .  ja v  a 2s. co  m

public static void initDescendants(Connection conn) throws SQLException {
    LinkedList<String> todo = new LinkedList<String>();

    // find roots
    Statement s = conn.createStatement();
    String sql = "SELECT ID FROM REPOSITORIES";
    logDebug(sql);
    ResultSet rs = s.executeQuery(sql);
    while (rs.next()) {
        String rootId = rs.getString(1);
        todo.add(rootId);
    }
    rs.close();
    log.trace("SQL:   -> " + todo);
    if (todo.size() == 0) {
        // db not yet initialized, ignore
        s.close();
        return;
    }

    // truncate table
    String table = "DESCENDANTS";
    sql = String.format("TRUNCATE TABLE %s", table);
    logDebug(sql);
    s.execute(sql);
    s.close();

    // traverse from roots
    Map<String, Set<String>> ancestors = new HashMap<String, Set<String>>();
    Map<String, Set<String>> descendants = new HashMap<String, Set<String>>();
    do {
        String p = todo.remove();
        sql = "SELECT ID FROM HIERARCHY WHERE PARENTID = ? AND ISPROPERTY = 0";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, p);
        // logDebug(sql, p);
        rs = ps.executeQuery();
        // for each child
        while (rs.next()) {
            String c = rs.getString(1);
            todo.add(c);
            // child's ancestors
            Set<String> cans = ancestors.get(c);
            if (cans == null) {
                ancestors.put(c, cans = new HashSet<String>());
            }
            Set<String> pans = ancestors.get(p);
            if (pans != null) {
                cans.addAll(pans);
            }
            cans.add(p);
            // all ancestors have it as descendant
            for (String pp : cans) {
                Set<String> desc = descendants.get(pp);
                if (desc == null) {
                    descendants.put(pp, desc = new HashSet<String>());
                }
                desc.add(c);
            }
        }
        ps.close();
    } while (!todo.isEmpty());

    // insert descendants into table
    sql = String.format("INSERT INTO %s (ID, DESCENDANTID) VALUES (?, ?)", table);
    PreparedStatement ps = conn.prepareStatement(sql);
    int n = 0;
    for (Entry<String, Set<String>> e : descendants.entrySet()) {
        String p = e.getKey();
        for (String c : e.getValue()) {
            ps.setString(1, p);
            ps.setString(2, c);
            // logDebug(sql, p, c);
            ps.execute();
            n++;
        }
    }
    logDebug(String.format("-- inserted %s rows into %s", Long.valueOf(n), table));
    ps.close();
}

From source file:org.mind.prebot.robot.PreBot.java

@Override
public void onMessage(String channel, String sender, String login, String hostname, String message) {
    if (!this.getIgnoredList().contains(sender)) {
        String years_str = "", months_str = "", days_str = "", hours_str = "", minutes_str = "",
                seconds_str = "";
        String[] tab = this.decryptData(message, true).trim().split(" ");
        if (tab.length > 0) {
            if (channel.equalsIgnoreCase(this.getParent().getIRCMindPreSearchChannel())
                    && (tab[0].equalsIgnoreCase("!pre") || tab[0].equalsIgnoreCase("!p"))) {
                if (tab.length > 1) {
                    String names = "";
                    for (Integer i = 1; i < tab.length; i++)
                        names += tab[i] + " ";
                    Release release = this.getMySQLManager().pre(names.trim());
                    if (release.getResults().equals(0))
                        this.sendMessage(channel,
                                this.encryptData("Nothing found for your search: " + names.trim()));
                    else {
                        if (release.getResults() > 1)
                            this.sendMessage(channel, this.encryptData(Colors.TEAL + "[ " + Colors.RED
                                    + release.getResults() + " results found! " + Colors.TEAL + "]"));
                        else
                            this.sendMessage(channel, this.encryptData(Colors.TEAL + "[ " + Colors.RED
                                    + release.getResults() + " result found! " + Colors.TEAL + "]"));
                        Integer years = release.getDiffDate() / 31536000;
                        Integer yearsMod = release.getDiffDate() % 31536000;
                        if (years == 1)
                            years_str = years + " year ";
                        else if (years > 1)
                            years_str = years + " years ";
                        Integer months = yearsMod / 2592000;
                        Integer monthsMod = yearsMod % 2592000;
                        if (months == 1)
                            months_str = months + " month ";
                        else if (months > 1)
                            months_str = months + " months ";
                        Integer days = monthsMod / 86400;
                        Integer daysMod = monthsMod % 86400;
                        if (days == 1)
                            days_str = days + " day ";
                        else if (days > 1)
                            days_str = days + " days ";
                        Integer hours = daysMod / 3600;
                        Integer hoursMod = daysMod % 3600;
                        if (hours == 1)
                            hours_str = hours + " hour ";
                        else if (hours > 1)
                            hours_str = hours + " hours ";
                        Integer minutes = hoursMod / 60;
                        if (minutes == 1)
                            minutes_str = minutes + " minute ";
                        else if (minutes > 1)
                            minutes_str = minutes + " minutes ";
                        Integer seconds = hoursMod % 60;
                        if (seconds == 1)
                            seconds_str = seconds + " second ";
                        else
                            seconds_str = seconds + " seconds ";
                        if (release.getChecked().equals("1") || release.getNuked().equals("0"))
                            this.sendMessage(channel,
                                    this.encryptData(Colors.TEAL + "[" + Colors.YELLOW + " PRED " + Colors.TEAL
                                            + "][ " + Utils.getCategoryCode(release.getCategory())
                                            + release.getCategory() + Colors.TEAL + " ][ " + Colors.LIGHT_GRAY
                                            + release.getName() + Colors.TEAL + " ][ " + Colors.LIGHT_GRAY
                                            + years_str + months_str + days_str + hours_str + minutes_str
                                            + seconds_str + "ago (" + release.getDate() + ") " + Colors.TEAL
                                            + "][ " + Colors.OLIVE + release.getSize() + Colors.TEAL + " ]"
                                            + (release.getChecked().equals("1") ? ""
                                                    : "[ " + Colors.RED + "UNCHECKED" + Colors.TEAL + " ]")));
                        else
                            this.sendMessage(channel,
                                    this.encryptData(Colors.TEAL + "[" + Colors.RED + " NUKED " + Colors.TEAL
                                            + "][ " + Utils.getCategoryCode(release.getCategory())
                                            + release.getCategory() + Colors.TEAL + " ][ " + Colors.LIGHT_GRAY
                                            + release.getName() + Colors.TEAL + " ][ " + Colors.LIGHT_GRAY
                                            + years_str + months_str + days_str + hours_str + minutes_str
                                            + seconds_str + "ago (" + release.getDate() + ") " + Colors.TEAL
                                            + "][ " + Colors.RED + release.getReason() + Colors.TEAL + " ][ "
                                            + Colors.OLIVE + release.getSize() + Colors.TEAL + " ]"));
                    }/*from  ww  w . j a v  a2s .  co  m*/
                }
            } else if (channel.equalsIgnoreCase(this.getParent().getIRCMindPreSearchChannel())
                    && (tab[0].equalsIgnoreCase("!dupenuke") || tab[0].equalsIgnoreCase("!dnu"))) {
                String names = "";
                String limit = "10";
                for (Integer i = 1; i < tab.length; i++) {
                    if (tab[i].contains("limit"))
                        limit = tab[i].substring(tab[i].indexOf("limit:") + 6, tab[i].length());
                    else
                        names += tab[i] + " ";
                }
                LinkedList<Release> releases = null;
                if (tab.length > 1)
                    releases = this.getMySQLManager().dupenuke(names.trim(), limit);
                else
                    releases = this.getMySQLManager().dupenuke("", limit);
                if (releases.isEmpty())
                    this.sendMessage(channel, this.encryptData(
                            "Nothing found for your search: " + (tab.length > 1 ? names.trim() : "")));
                else {
                    if (releases.get(0).getResults() > 1)
                        this.sendMessage(channel,
                                this.encryptData("Sending " + Colors.OLIVE + sender + Colors.LIGHT_GRAY
                                        + " last " + Colors.OLIVE + releases.get(0).getResults() + Colors.RED
                                        + " nuked " + Colors.LIGHT_GRAY + "results."));
                    else
                        this.sendMessage(channel,
                                this.encryptData("Sending " + Colors.OLIVE + sender + Colors.LIGHT_GRAY
                                        + " last " + Colors.OLIVE + releases.get(0).getResults() + Colors.RED
                                        + " nuked " + Colors.LIGHT_GRAY + "results"));
                    for (Release release : releases) {
                        Integer years = release.getDiffDate() / 31536000;
                        Integer yearsMod = release.getDiffDate() % 31536000;
                        if (years == 1)
                            years_str = years + " year ";
                        else if (years > 1)
                            years_str = years + " years ";
                        Integer months = yearsMod / 2592000;
                        Integer monthsMod = yearsMod % 2592000;
                        if (months == 1)
                            months_str = months + " month ";
                        else if (months > 1)
                            months_str = months + " months ";
                        Integer days = monthsMod / 86400;
                        Integer daysMod = monthsMod % 86400;
                        if (days == 1)
                            days_str = days + " day ";
                        else if (days > 1)
                            days_str = days + " days ";
                        Integer hours = daysMod / 3600;
                        Integer hoursMod = daysMod % 3600;
                        if (hours == 1)
                            hours_str = hours + " hour ";
                        else if (hours > 1)
                            hours_str = hours + " hours ";
                        Integer minutes = hoursMod / 60;
                        if (minutes == 1)
                            minutes_str = minutes + " minute ";
                        else if (minutes > 1)
                            minutes_str = minutes + " minutes ";
                        Integer seconds = hoursMod % 60;
                        if (seconds == 1)
                            seconds_str = seconds + " second ";
                        else
                            seconds_str = seconds + " seconds ";
                        this.sendMessage(sender,
                                this.encryptData(Colors.TEAL + "[" + Colors.RED + " NUKED " + Colors.TEAL
                                        + "][ " + Utils.getCategoryCode(release.getCategory())
                                        + release.getCategory() + Colors.TEAL + " ][ " + Colors.LIGHT_GRAY
                                        + release.getName() + Colors.TEAL + " ][ " + Colors.LIGHT_GRAY
                                        + years_str + months_str + days_str + hours_str + minutes_str
                                        + seconds_str + "ago (" + release.getDate() + ") " + Colors.TEAL + "][ "
                                        + Colors.RED + release.getReason() + Colors.TEAL + " ][ " + Colors.OLIVE
                                        + release.getSize() + Colors.TEAL + " ]"));
                    }
                }
            } else if (channel.equalsIgnoreCase(this.getParent().getIRCMindPreSearchChannel())
                    && (tab[0].equalsIgnoreCase("!dupe") || tab[0].equalsIgnoreCase("!d"))) {
                String names = "";
                String limit = "10";
                for (Integer i = 1; i < tab.length; i++) {
                    if (tab[i].contains("limit"))
                        limit = tab[i].substring(tab[i].indexOf("limit:") + 6, tab[i].length());
                    else
                        names += tab[i] + " ";
                }
                LinkedList<Release> releases = null;
                if (tab.length > 1)
                    releases = this.getMySQLManager().dupe(names.trim(), limit);
                else
                    releases = this.getMySQLManager().dupe("", limit);
                if (releases.isEmpty())
                    this.sendMessage(channel, this.encryptData(
                            "Nothing found for your search: " + (tab.length > 1 ? names.trim() : "")));
                else {
                    if (releases.get(0).getResults() > 1)
                        this.sendMessage(channel,
                                this.encryptData("Sending " + Colors.OLIVE + sender + Colors.LIGHT_GRAY
                                        + " last " + Colors.OLIVE + releases.get(0).getResults()
                                        + Colors.LIGHT_GRAY + " results."));
                    else
                        this.sendMessage(channel,
                                this.encryptData("Sending " + Colors.OLIVE + sender + Colors.LIGHT_GRAY
                                        + " last " + Colors.OLIVE + releases.get(0).getResults()
                                        + Colors.LIGHT_GRAY + " result."));
                    for (Release release : releases) {
                        Integer years = release.getDiffDate() / 31536000;
                        Integer yearsMod = release.getDiffDate() % 31536000;
                        if (years == 1)
                            years_str = years + " year ";
                        else if (years > 1)
                            years_str = years + " years ";
                        Integer months = yearsMod / 2592000;
                        Integer monthsMod = yearsMod % 2592000;
                        if (months == 1)
                            months_str = months + " month ";
                        else if (months > 1)
                            months_str = months + " months ";
                        Integer days = monthsMod / 86400;
                        Integer daysMod = monthsMod % 86400;
                        if (days == 1)
                            days_str = days + " day ";
                        else if (days > 1)
                            days_str = days + " days ";
                        Integer hours = daysMod / 3600;
                        Integer hoursMod = daysMod % 3600;
                        if (hours == 1)
                            hours_str = hours + " hour ";
                        else if (hours > 1)
                            hours_str = hours + " hours ";
                        Integer minutes = hoursMod / 60;
                        if (minutes == 1)
                            minutes_str = minutes + " minute ";
                        else if (minutes > 1)
                            minutes_str = minutes + " minutes ";
                        Integer seconds = hoursMod % 60;
                        if (seconds == 1)
                            seconds_str = seconds + " second ";
                        else
                            seconds_str = seconds + " seconds ";
                        if (release.getChecked().equals("1") || release.getNuked().equals("0"))
                            this.sendMessage(sender,
                                    this.encryptData(Colors.TEAL + "[" + Colors.YELLOW + " PRED " + Colors.TEAL
                                            + "][ " + Utils.getCategoryCode(release.getCategory())
                                            + release.getCategory() + Colors.TEAL + "][ " + Colors.LIGHT_GRAY
                                            + release.getName() + Colors.TEAL + "][ " + Colors.LIGHT_GRAY
                                            + years_str + months_str + days_str + hours_str + minutes_str
                                            + seconds_str + "ago (" + release.getDate() + ") " + Colors.TEAL
                                            + "][ " + Colors.OLIVE + release.getSize() + Colors.TEAL + " ]"
                                            + (release.getChecked().equals("1") ? ""
                                                    : "[ " + Colors.RED + "UNCHECKED" + Colors.TEAL + " ]")));
                        else
                            this.sendMessage(sender,
                                    this.encryptData(Colors.TEAL + "[" + Colors.RED + " NUKED " + Colors.TEAL
                                            + "][ " + Utils.getCategoryCode(release.getCategory())
                                            + release.getCategory() + Colors.TEAL + " ][ " + Colors.LIGHT_GRAY
                                            + release.getName() + Colors.TEAL + " ][ " + Colors.LIGHT_GRAY
                                            + years_str + months_str + days_str + hours_str + minutes_str
                                            + seconds_str + "ago (" + release.getDate() + ") " + Colors.TEAL
                                            + "][ " + Colors.RED + release.getReason() + Colors.TEAL + " ][ "
                                            + Colors.OLIVE + release.getSize() + Colors.TEAL + " ]"));
                    }
                }
            } else if (channel.equalsIgnoreCase(this.getParent().getIRCMindPreSearchChannel())
                    && (tab[0].equalsIgnoreCase("!nuke") || tab[0].equalsIgnoreCase("!nk"))) {
                if (this.getNukerList().contains(sender)) {
                    String names = "";
                    for (Integer i = 2; i < tab.length; i++)
                        names += tab[i] + " ";
                    if (tab.length > 2) {
                        Integer ret = this.getMySQLManager().nuke(tab[1], names.trim());
                        if (ret > 0)
                            this.sendMessage(channel,
                                    this.encryptData(Colors.OLIVE + tab[1] + Colors.LIGHT_GRAY
                                            + " has been successfully " + Colors.RED + "nuked"
                                            + Colors.LIGHT_GRAY + "!"));
                        else
                            this.sendMessage(channel,
                                    this.encryptData(Colors.OLIVE + tab[1] + Colors.LIGHT_GRAY
                                            + " has not been successfully " + Colors.RED + "nuked"
                                            + Colors.LIGHT_GRAY + "!"));
                    }
                } else
                    this.sendMessage(channel, this.encryptData(sender + ": You've to be a nuker to do this."));
            } else if (channel.equalsIgnoreCase(this.getParent().getIRCMindPreSearchChannel())
                    && (tab[0].equalsIgnoreCase("!unnuke") || tab[0].equalsIgnoreCase("!un"))) {
                if (this.getNukerList().contains(sender)) {
                    String names = "";
                    for (Integer i = 2; i < tab.length; i++)
                        names += tab[i] + " ";
                    if (tab.length > 2) {
                        Integer ret = this.getMySQLManager().unnuke(tab[1], names.trim());
                        if (ret > 0)
                            this.sendMessage(channel,
                                    this.encryptData(Colors.OLIVE + tab[1] + Colors.LIGHT_GRAY
                                            + " has been successfully " + Colors.DARK_GREEN + "unnuked"
                                            + Colors.LIGHT_GRAY + "!"));
                        else
                            this.sendMessage(channel,
                                    this.encryptData(Colors.OLIVE + tab[1] + Colors.LIGHT_GRAY
                                            + " has not been successfully " + Colors.DARK_GREEN + "unnuked"
                                            + Colors.LIGHT_GRAY + "!"));
                    }
                } else
                    this.sendMessage(channel, this.encryptData(sender + ": You've to be a nuker to do this."));
            } else if (channel.equalsIgnoreCase(this.getParent().getIRCMindPreSearchChannel())
                    && (tab[0].equalsIgnoreCase("!addpre") || tab[0].equalsIgnoreCase("!ap"))) {
                if (this.getNukerList().contains(sender)) {
                    String names = "";
                    for (Integer i = 3; i < tab.length; i++)
                        names += tab[i] + " ";
                    if (tab.length > 3) {
                        Integer ret = this.getMySQLManager().addpre(tab[1], tab[2], names.trim());
                        if (ret > 0)
                            this.sendMessage(channel,
                                    this.encryptData(Colors.OLIVE + tab[1] + Colors.LIGHT_GRAY
                                            + " has been successfully " + Colors.DARK_GREEN + "addpred"
                                            + Colors.LIGHT_GRAY + "!"));
                        else
                            this.sendMessage(channel,
                                    this.encryptData(Colors.OLIVE + tab[1] + Colors.LIGHT_GRAY
                                            + " has not been successfully " + Colors.DARK_GREEN + "addpred"
                                            + Colors.LIGHT_GRAY + "!"));
                    }
                } else
                    this.sendMessage(channel, this.encryptData(sender + ": You've to be a nuker to do this."));
            } else if (channel.equalsIgnoreCase(this.getParent().getIRCMindPreSearchChannel())
                    && (tab[0].equalsIgnoreCase("!delpre") || tab[0].equalsIgnoreCase("!dp"))) {
                if (this.getNukerList().contains(sender)) {
                    if (tab.length > 1) {
                        Integer ret = this.getMySQLManager().delpre(tab[1]);
                        if (ret > 0)
                            this.sendMessage(channel,
                                    this.encryptData(Colors.OLIVE + tab[1] + Colors.LIGHT_GRAY
                                            + " has been successfully " + Colors.DARK_GREEN + "delpred"
                                            + Colors.LIGHT_GRAY + "!"));
                        else
                            this.sendMessage(channel,
                                    this.encryptData(Colors.OLIVE + tab[1] + Colors.LIGHT_GRAY
                                            + " has not been successfully " + Colors.DARK_GREEN + "delpred"
                                            + Colors.LIGHT_GRAY + "!"));
                    }
                } else
                    this.sendMessage(channel, this.encryptData(sender + ": You've to be a nuker to do this."));
            } else if (channel.equalsIgnoreCase(this.getParent().getIRCMindChannel())
                    && tab[0].equalsIgnoreCase("!vdm")) {
                List<String> vdms = Utils.getMatcher(Utils.VDMRegex, Utils.getCode(Utils.VDMFeed),
                        Pattern.MULTILINE);
                if (!vdms.isEmpty()) {
                    String vdm = vdms.get(new Random().nextInt(vdms.size()));
                    vdm = StringEscapeUtils.unescapeHtml4(vdm);
                    vdm = vdm.substring(30).replaceAll("[\r\n]+", "").replaceAll(" {2,}", " ").trim();
                    this.sendMessage(channel, this.encryptData(Colors.TEAL + "[" + Colors.BROWN + " VDM "
                            + Colors.TEAL + "] :: [ " + Colors.DARK_GREEN + vdm + Colors.TEAL + " ]"));
                }
            } else if (channel.equalsIgnoreCase(this.getParent().getIRCMindChannel())
                    && tab[0].equalsIgnoreCase("!cnf")) {
                List<String> cnfs = Utils.getMatcher(Utils.CNFRegex, Utils.getCode(Utils.CNFPage),
                        Pattern.MULTILINE);
                if (!cnfs.isEmpty()) {
                    String cnf = cnfs.get(new Random().nextInt(cnfs.size()));
                    cnf = StringEscapeUtils.unescapeHtml4(cnf);
                    cnf = cnf.substring(cnf.indexOf(">") + 1, cnf.indexOf("</div>")).replaceAll("[\r\n]+", "")
                            .replaceAll(" {2,}", " ").trim();
                    this.sendMessage(channel, this.encryptData(Colors.TEAL + "[" + Colors.RED + " CNF "
                            + Colors.TEAL + "] :: [ " + Colors.DARK_GREEN + cnf + Colors.TEAL + " ]"));
                }
            } else if (channel.equalsIgnoreCase(this.getParent().getIRCMindChannel())
                    && (tab[0].equalsIgnoreCase("!slap") || tab[0].equalsIgnoreCase("!s"))) {
                if (this.getSlaps() < this.getSlapsRandom())
                    this.setSlaps(this.getSlaps() + 1);
                else {
                    this.kick(channel, sender, this.encryptData("Sorry, you loose this time ^^"));
                    this.setSlaps(0);
                    this.setSlapsRandom(new Random().nextInt(26));
                }
            } else if (tab[0].equalsIgnoreCase("!kick") || tab[0].equalsIgnoreCase("!k")) {
                if (sender.equals("BaYbEE")) {
                    if (tab.length > 2) {
                        String names = "";
                        for (Integer i = 2; i < tab.length; i++)
                            names += tab[i] + " ";
                        this.kick(channel, tab[1], this.encryptData(names.trim()));
                    } else
                        this.kick(channel, tab[1]);
                }
            } else if (tab[0].equalsIgnoreCase("!ban") || tab[0].equalsIgnoreCase("!b")) {
                if (sender.equals("BaYbEE")) {
                    if (tab.length > 1)
                        this.ban(channel, tab[1]);
                }
            } else if (tab[0].equalsIgnoreCase("!mode") || tab[0].equalsIgnoreCase("!m")) {
                if (sender.equals("BaYbEE")) {
                    if (tab.length > 2)
                        this.setMode(channel, tab[1] + " " + tab[2]);
                }
            } else if (tab[0].equalsIgnoreCase("!message") || tab[0].equalsIgnoreCase("!msg")) {
                if (sender.equals("BaYbEE")) {
                    if (tab.length > 2) {
                        String names = "";
                        for (Integer i = 2; i < tab.length; i++)
                            names += tab[i] + " ";
                        this.sendMessage(tab[1], this.encryptData(names.trim()));
                    }
                }
            } else if (tab[0].equalsIgnoreCase("!action") || tab[0].equalsIgnoreCase("!a")) {
                if (sender.equals("BaYbEE")) {
                    if (tab.length > 2) {
                        String names = "";
                        for (Integer i = 2; i < tab.length; i++)
                            names += tab[i] + " ";
                        this.sendAction(tab[1], this.encryptData(names.trim()));
                    }
                }
            } else if (tab[0].equalsIgnoreCase("!notice") || tab[0].equalsIgnoreCase("!n")) {
                if (sender.equals("BaYbEE")) {
                    if (tab.length > 2) {
                        String names = "";
                        for (Integer i = 2; i < tab.length; i++)
                            names += tab[i] + " ";
                        this.sendNotice(tab[1], this.encryptData(names.trim()));
                    }
                }
            } else if (tab[0].equalsIgnoreCase("!ignore") || tab[0].equalsIgnoreCase("!i")) {
                if (sender.equals("BaYbEE")) {
                    if (tab.length > 1) {
                        if (!this.getIgnoredList().contains(tab[1]))
                            this.getIgnoredList().add(tab[1]);
                    }
                }
            } else if (tab[0].equalsIgnoreCase("!unignore") || tab[0].equalsIgnoreCase("!ui")) {
                if (sender.equals("BaYbEE")) {
                    if (tab.length > 1) {
                        if (this.getIgnoredList().contains(tab[1]))
                            this.getIgnoredList().remove(tab[1]);
                    }
                }
            } else if (tab[0].equalsIgnoreCase("!addnuker") || tab[0].equalsIgnoreCase("!an")) {
                if (sender.equals("BaYbEE")) {
                    if (tab.length > 1) {
                        if (!this.getNukerList().contains(tab[1]))
                            this.getNukerList().add(tab[1]);
                    }
                }
            } else if (tab[0].equalsIgnoreCase("!delnuker") || tab[0].equalsIgnoreCase("!dn")) {
                if (sender.equals("BaYbEE")) {
                    if (tab.length > 1) {
                        if (this.getNukerList().contains(tab[1]))
                            this.getNukerList().remove(tab[1]);
                    }
                }
            } else if (channel.equalsIgnoreCase(this.getParent().getIRCMindPreSearchChannel())
                    && (tab[0].equalsIgnoreCase("!showrequest") || tab[0].equalsIgnoreCase("!sr"))) {
                if (tab.length > 1) {
                    String names = "";
                    for (Integer i = 1; i < tab.length; i++)
                        names += tab[i] + " ";
                    Request request = this.getMySQLManager().showrequest(names.trim());
                    if (request.getResults().equals(0))
                        this.sendMessage(channel,
                                this.encryptData("Nothing found for your search: " + names.trim()));
                    else {
                        if (request.getResults() > 1)
                            this.sendMessage(channel, this.encryptData(
                                    "\00310[\00304 " + request.getResults() + " results found! \00310]"));
                        else
                            this.sendMessage(channel, this.encryptData(
                                    "\00310[\00304 " + request.getResults() + " result found! \00310]"));
                        Integer years = request.getDiffDate() / 31536000;
                        Integer yearsMod = request.getDiffDate() % 31536000;
                        if (years == 1)
                            years_str = years + " year ";
                        else if (years > 1)
                            years_str = years + " years ";
                        Integer months = yearsMod / 2592000;
                        Integer monthsMod = yearsMod % 2592000;
                        if (months == 1)
                            months_str = months + " month ";
                        else if (months > 1)
                            months_str = months + " months ";
                        Integer days = monthsMod / 86400;
                        Integer daysMod = monthsMod % 86400;
                        if (days == 1)
                            days_str = days + " day ";
                        else if (days > 1)
                            days_str = days + " days ";
                        Integer hours = daysMod / 3600;
                        Integer hoursMod = daysMod % 3600;
                        if (hours == 1)
                            hours_str = hours + " hour ";
                        else if (hours > 1)
                            hours_str = hours + " hours ";
                        Integer minutes = hoursMod / 60;
                        if (minutes == 1)
                            minutes_str = minutes + " minute ";
                        else if (minutes > 1)
                            minutes_str = minutes + " minutes ";
                        Integer seconds = hoursMod % 60;
                        if (seconds == 1)
                            seconds_str = seconds + " second ";
                        else
                            seconds_str = seconds + " seconds ";
                        if (request.getFilled())
                            this.sendMessage(channel, this.encryptData("\00310[\00308 REQ \00310] [\00315 "
                                    + request.getRequest() + " \00310] [\00315 " + years_str + months_str
                                    + days_str + hours_str + minutes_str + seconds_str + "ago ("
                                    + (request.getRequestDate()) + ") \00310] [ \00307Requested by: \00315"
                                    + request.getRequestBy() + " \00310] [ \00307Filled by: \00315"
                                    + request.getFilledBy() + " \00310]"));
                        else
                            this.sendMessage(channel, this.encryptData("\00310[\00308 REQ \00310] [\00315 "
                                    + request.getRequest() + " \00310] [\00315 " + years_str + months_str
                                    + days_str + hours_str + minutes_str + seconds_str + "ago ("
                                    + request.getRequestDate() + ") \00310] [ \00307Requested by: \00315"
                                    + request.getRequestBy() + " \00310]"));
                    }
                }
            } else if (channel.equalsIgnoreCase(this.getParent().getIRCMindPreSearchChannel())
                    && (tab[0].equalsIgnoreCase("!addrequest") || tab[0].equalsIgnoreCase("!ar"))) {
                String names = "";
                for (Integer i = 1; i < tab.length; i++)
                    names += tab[i] + " ";
                if (tab.length > 1) {
                    Request request = new Request();
                    request.setRequest(names.trim());
                    request.setRequestBy(sender);
                    request.setRequestDate(null);
                    request.setFilled(false);
                    request.setFilledBy("");
                    Integer ret = this.getMySQLManager().addrequest(request);
                    if (ret > 0)
                        this.sendMessage(channel, this.encryptData("\00307" + names.trim()
                                + "\00315 has been successfully \00304requested\00315!"));
                    else
                        this.sendMessage(channel, this.encryptData("\00307" + names.trim()
                                + "\00315 has not been successfully \00304requested\00315!"));
                }
            } else if (channel.equalsIgnoreCase(this.getParent().getIRCMindPreSearchChannel())
                    && (tab[0].equalsIgnoreCase("!fillrequest") || tab[0].equalsIgnoreCase("!fr"))) {
                String names = "";
                for (Integer i = 1; i < tab.length; i++)
                    names += tab[i] + " ";
                if (tab.length > 1) {
                    Integer ret = this.getMySQLManager().fillrequest(names.trim(), sender);
                    if (ret > 0)
                        this.sendMessage(channel, this.encryptData(
                                "\00307" + names.trim() + "\00315 has been successfully \00304filled\00315!"));
                    else
                        this.sendMessage(channel, this.encryptData("\00307" + names.trim()
                                + "\00315 has not been successfully \0030filled\00315!"));
                }
            } else if (channel.equalsIgnoreCase(this.getParent().getIRCMindPreSearchChannel())
                    && (tab[0].equalsIgnoreCase("!duperequest") || tab[0].equalsIgnoreCase("!dr"))) {
                String names = "";
                String limit = "10";
                for (Integer i = 1; i < tab.length; i++) {
                    if (tab[i].contains("limit"))
                        limit = tab[i].substring(tab[i].indexOf("limit:") + 6, tab[i].length());
                    else
                        names += tab[i] + " ";
                }
                LinkedList<Request> requests = null;
                if (tab.length > 1)
                    requests = this.getMySQLManager().duperequest(names.trim(), limit);
                else
                    requests = this.getMySQLManager().duperequest("", limit);
                if (requests.isEmpty())
                    this.sendMessage(channel, this.encryptData(
                            "Nothing found for your search: " + (tab.length > 1 ? names.trim() : "")));
                else {
                    if (requests.get(0).getResults() > 1)
                        this.sendMessage(channel, this.encryptData("Sending \00307" + sender
                                + "\00315 last \00307" + requests.get(0).getResults() + "\00315 results."));
                    else
                        this.sendMessage(channel, this.encryptData("Sending \00307" + sender
                                + "\00315 last \00307" + requests.get(0).getResults() + "\00315 result."));
                    for (Request request : requests) {
                        Integer years = request.getDiffDate() / 31536000;
                        Integer yearsMod = request.getDiffDate() % 31536000;
                        if (years == 1)
                            years_str = years + " year ";
                        else if (years > 1)
                            years_str = years + " years ";
                        Integer months = yearsMod / 2592000;
                        Integer monthsMod = yearsMod % 2592000;
                        if (months == 1)
                            months_str = months + " month ";
                        else if (months > 1)
                            months_str = months + " months ";
                        Integer days = monthsMod / 86400;
                        Integer daysMod = monthsMod % 86400;
                        if (days == 1)
                            days_str = days + " day ";
                        else if (days > 1)
                            days_str = days + " days ";
                        Integer hours = daysMod / 3600;
                        Integer hoursMod = daysMod % 3600;
                        if (hours == 1)
                            hours_str = hours + " hour ";
                        else if (hours > 1)
                            hours_str = hours + " hours ";
                        Integer minutes = hoursMod / 60;
                        if (minutes == 1)
                            minutes_str = minutes + " minute ";
                        else if (minutes > 1)
                            minutes_str = minutes + " minutes ";
                        Integer seconds = hoursMod % 60;
                        if (seconds == 1)
                            seconds_str = seconds + " second ";
                        else
                            seconds_str = seconds + " seconds ";
                        if (request.getFilled())
                            this.sendMessage(sender, this.encryptData("\00310[\00308 REQ \00310] [\00315 "
                                    + request.getRequest() + " \00310] [\00315 " + years_str + months_str
                                    + days_str + hours_str + minutes_str + seconds_str + "ago ("
                                    + request.getRequestDate() + ") \00310] [ \00307Requested by: \00315"
                                    + request.getRequestBy() + " \00310] [ \00307Filled by: \00315"
                                    + request.getFilledBy() + " \00310]"));
                        else
                            this.sendMessage(sender, this.encryptData("\00310[\00308 REQ \00310] [\00315 "
                                    + request.getRequest() + " \00310] [\00315 " + years_str + months_str
                                    + days_str + hours_str + minutes_str + seconds_str + "ago ("
                                    + request.getRequestDate() + ") \00310] [ \00307Requested by: \00315"
                                    + request.getRequestBy() + " \00310]"));
                    }
                }
            } else if (channel.equalsIgnoreCase(this.getParent().getIRCMindPreSearchChannel())
                    && (tab[0].equalsIgnoreCase("!group") || tab[0].equalsIgnoreCase("!g"))) {
                Group group = this.getMySQLManager().group();
                this.sendMessage(channel,
                        this.encryptData(Colors.DARK_GRAY + "Total Releases: " + Colors.GREEN
                                + group.getTotalReleases() + Colors.DARK_GRAY + " Total Nuked: " + Colors.RED
                                + group.getTotalNukes() + Colors.DARK_GRAY + " Total Unuked: " + Colors.OLIVE
                                + group.getTotalUnnukes()));
                this.sendMessage(channel,
                        this.encryptData(Colors.DARK_GRAY + "First Pre: " + Colors.LIGHT_GRAY + "["
                                + Utils.getCategoryCode(group.getCategoryFirstPre())
                                + group.getCategoryFirstPre() + Colors.LIGHT_GRAY + "] " + group.getFirstPre()
                                + " [" + group.getDateFirstPre() + "]"));
                this.sendMessage(channel, this.encryptData(Colors.DARK_GRAY + "Last Pre: " + Colors.LIGHT_GRAY
                        + "[" + Utils.getCategoryCode(group.getCategoryLastPre()) + group.getCategoryLastPre()
                        + Colors.LIGHT_GRAY + "] " + group.getLastPre() + " [" + group.getDateLastPre() + "]"));
            } else {
                for (String t : tab) {
                    if (!Utils.getMatcher(Utils.URLRegex, t, Pattern.DOTALL).isEmpty()) {
                        String title = Utils.getTitleMatcher(Utils.getCode(t));
                        if (title != null) {
                            title = StringEscapeUtils.unescapeHtml4(title);
                            title = title.substring(7, title.length() - 8).replaceAll("[\r\n]+", "")
                                    .replaceAll(" {2,}", " ").trim();
                            this.sendMessage(channel,
                                    this.encryptData("\00310[\00303 Title:\00307 " + title + " \00310]"));
                        }
                    }
                }
            }
        }
    }
}

From source file:edu.cornell.med.icb.clustering.QTClusterer.java

/**
 * Groups instances into clusters. Returns the indices of the instances
 * that belong to a cluster as an int array in the list result.
 *
 * @param calculator       The/*from  ww w  .  ja  va 2 s  .com*/
 *                         {@link edu.cornell.med.icb.clustering.SimilarityDistanceCalculator}
 *                         that should be used when clustering
 * @param qualityThreshold The QT clustering algorithm quality threshold (d)
 * @return The list of clusters.
 */
public List<int[]> cluster(final SimilarityDistanceCalculator calculator, final double qualityThreshold) {
    final ProgressLogger clusterProgressLogger = new ProgressLogger(LOGGER, logInterval, "instances clustered");
    clusterProgressLogger.displayFreeMemory = true;
    clusterProgressLogger.expectedUpdates = instanceCount;
    clusterProgressLogger.start("Starting to cluster " + instanceCount + " instances using "
            + parallelTeam.getThreadCount() + " threads.");

    // reset cluster results
    clusterCount = 0;
    // instanceList is the set "G" to cluster
    final LinkedList<Integer> instanceList = new LinkedList<Integer>();
    for (int i = 0; i < instanceCount; i++) {
        clusters[i].clear();

        // set each node in the instance list to it's
        // original position in the source data array
        instanceList.add(i);
    }

    final double ignoreDistance = calculator.getIgnoreDistance();

    // eliminate any instances that will never cluster with anything else
    final IntList singletonClusters = identifySingletonClusters(calculator, qualityThreshold, instanceList,
            clusterProgressLogger);

    final ProgressLogger innerLoopProgressLogger = new ProgressLogger(LOGGER, logInterval,
            "inner loop iterations");
    innerLoopProgressLogger.displayFreeMemory = false;

    final ProgressLogger outerLoopProgressLogger = new ProgressLogger(LOGGER, logInterval,
            "outer loop iterations");
    outerLoopProgressLogger.displayFreeMemory = true;

    try {
        // loop over instances until they have all been added to a cluster
        while (!instanceList.isEmpty()) {
            // cluster remaining instances to find the maximum cardinality
            for (int i = 0; i < instanceList.size(); i++) {
                candidateClusters[i].clear();
            }

            if (logOuterLoopProgress) {
                outerLoopProgressLogger.expectedUpdates = instanceList.size();
                outerLoopProgressLogger.start("Entering outer loop for " + instanceList.size() + " iterations");
            }

            // for each i in G (instance list)
            // find instance j such that distance i,j minimum
            parallelTeam.execute(new ParallelRegion() { // NOPMD

                @Override
                public void run() throws Exception { // NOPMD
                    // each thread will populate a different portion of the "candidateCluster"
                    // array so we shouldn't need to worry about concurrent access
                    execute(0, instanceList.size() - 1, new IntegerForLoop() {
                        @Override
                        public void run(final int first, final int last) {
                            if (LOGGER.isDebugEnabled()) {
                                LOGGER.debug("first = " + first + ", last = " + last);
                            }
                            for (int i = first; i <= last; i++) {
                                @SuppressWarnings("unchecked")
                                final LinkedList<Integer> notClustered = (LinkedList<Integer>) instanceList
                                        .clone();

                                // add the first instance to the next candidate cluster
                                final IntArrayList candidateCluster = candidateClusters[i];
                                candidateCluster.add(notClustered.remove(i));

                                if (logInnerLoopProgress) {
                                    innerLoopProgressLogger.expectedUpdates = notClustered.size();
                                    innerLoopProgressLogger.start(
                                            "Entering inner loop for " + notClustered.size() + " iterations");
                                }

                                // cluster the remaining instances to find the maximum
                                // cardinality find instance j such that distance i,j minimum
                                boolean done = false;
                                while (!done && !notClustered.isEmpty()) {
                                    // find the node that has minimum distance between the
                                    // current cluster and the instances that have not yet
                                    // been clustered.
                                    double minDistance = Double.POSITIVE_INFINITY;
                                    int minDistanceInstanceIndex = 0;
                                    int instanceIndex = 0;
                                    for (final int instance : notClustered) {
                                        double newDistance = ignoreDistance;

                                        final int[] cluster = candidateCluster.elements();
                                        for (int instanceInCluster = 0; instanceInCluster < candidateCluster
                                                .size(); instanceInCluster++) {
                                            final double a = calculator.distance(cluster[instanceInCluster],
                                                    instance);
                                            // if the distance of the instance will force the candidate cluster
                                            // to be larger than the cutoff value, we can stop here
                                            // because we know that this candidate cluster will be too large
                                            if (a >= minDistance) {
                                                newDistance = ignoreDistance;
                                                break;
                                            }
                                            final double b = newDistance;

                                            // This code is inlined from java.lang.Math.max(a, b)
                                            if (a != a) { // a is NaN
                                                newDistance = a;
                                            } else if (a == 0.0d && b == 0.0d
                                                    && Double.doubleToLongBits(a) == negativeZeroDoubleBits) {
                                                newDistance = b;
                                            } else if (a >= b) {
                                                newDistance = a;
                                            } else {
                                                newDistance = b;
                                            }
                                        }

                                        if (newDistance != ignoreDistance && newDistance < minDistance) {
                                            minDistance = newDistance;
                                            minDistanceInstanceIndex = instanceIndex;
                                        }
                                        instanceIndex++;
                                    }
                                    // grow clusters until min distance between new instance
                                    // and cluster reaches quality threshold
                                    // if (diameter(Ai U {j}) > d)
                                    if (minDistance > qualityThreshold) {
                                        done = true;
                                    } else {
                                        // remove the instance from the ones to be considered
                                        final int instance = notClustered.remove(minDistanceInstanceIndex);
                                        // and add it to the newly formed cluster
                                        candidateCluster.add(instance);
                                    }
                                    if (logInnerLoopProgress) {
                                        innerLoopProgressLogger.update();
                                    }
                                }
                                if (logInnerLoopProgress) {
                                    innerLoopProgressLogger.stop("Inner loop completed.");
                                }
                                if (logOuterLoopProgress) {
                                    outerLoopProgressLogger.update();
                                }
                            }
                        }
                    });
                }
            });

            if (logOuterLoopProgress) {
                outerLoopProgressLogger.stop("Outer loop completed.");
            }

            // identify cluster (set C) with maximum cardinality
            int maxCardinality = 0;
            int selectedClusterIndex = -1;
            for (int i = 0; i < instanceList.size(); i++) {
                final int size = candidateClusters[i].size();
                if (LOGGER.isTraceEnabled() && size > 0) {
                    LOGGER.trace("potential cluster " + i + ": " + ArrayUtils.toString(candidateClusters[i]));
                }
                if (size > maxCardinality) {
                    maxCardinality = size;
                    selectedClusterIndex = i;
                }
            }

            final IntArrayList selectedCluster = candidateClusters[selectedClusterIndex];

            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("adding " + selectedCluster.size() + " instances to cluster " + clusterCount);
            }
            // and add that cluster to the final result
            clusters[clusterCount].addAll(selectedCluster);

            // remove instances in cluster C so they are no longer considered
            instanceList.removeAll(selectedCluster);

            if (logClusterProgress) {
                final int selectedClusterSize = selectedCluster.size();
                int i = 0;
                while (i < selectedClusterSize - 1) {
                    clusterProgressLogger.lightUpdate();
                    i++;
                }
                // make sure there is at least one "full" update per loop
                if (i < selectedClusterSize) {
                    clusterProgressLogger.update();
                }
            }

            // we just created a new cluster
            clusterCount++;

            // next iteration is over (G - C)
        }
    } catch (RuntimeException e) {
        LOGGER.error("Caught runtime exception - rethrowing", e);
        throw e;
    } catch (Exception e) {
        LOGGER.error("Caught exception - rethrowing as ClusteringException", e);
        throw new ClusteringException(e);
    }

    // add singleton clusters to the end so the largest clusters are at the start of the list
    for (final int singleton : singletonClusters) {
        clusters[clusterCount].add(singleton);
        clusterCount++;
    }

    clusterProgressLogger.stop("Clustering completed.");
    return getClusters();
}

From source file:org.apache.pig.backend.hadoop.executionengine.tez.TezDagBuilder.java

private Vertex newVertex(TezOperator tezOp) throws IOException, ClassNotFoundException, InterruptedException {
    ProcessorDescriptor procDesc = ProcessorDescriptor.create(tezOp.getProcessorName());

    // Pass physical plans to vertex as user payload.
    JobConf payloadConf = new JobConf(pigContextConf);

    // We do this so that dag.getCredentials(), job.getCredentials(),
    // job.getConfiguration().getCredentials() all reference the same Credentials object
    // Unfortunately there is no setCredentials() on Job
    payloadConf.setCredentials(dag.getCredentials());
    // We won't actually use this job, but we need it to talk with the Load Store funcs
    @SuppressWarnings("deprecation")
    Job job = new Job(payloadConf);
    payloadConf = (JobConf) job.getConfiguration();
    //TODO: Investigate. Setting as map writes empty output.
    //payloadConf.setBoolean(MRConfig.IS_MAP_PROCESSOR, tezOp.isUseMRMapSettings());
    payloadConf.setBoolean(MRConfiguration.MAPPER_NEW_API, true);
    payloadConf.setBoolean(MRConfiguration.REDUCER_NEW_API, true);
    payloadConf.setClass(MRConfiguration.INPUTFORMAT_CLASS, PigInputFormatTez.class, InputFormat.class);
    setOutputFormat(job);//from w ww.  j a  v  a 2s. c  o  m
    payloadConf.set("udf.import.list", serializedUDFImportList);
    payloadConf.set("exectype", "TEZ");
    payloadConf.setBoolean(PigImplConstants.PIG_EXECTYPE_MODE_LOCAL, pc.getExecType().isLocal());
    payloadConf.set(PigImplConstants.PIG_LOG4J_PROPERTIES, ObjectSerializer.serialize(pc.getLog4jProperties()));

    // Process stores
    LinkedList<POStore> stores = processStores(tezOp, payloadConf, job);

    Configuration inputPayLoad = null;
    Configuration outputPayLoad = null;

    if (!stores.isEmpty()) {
        outputPayLoad = new Configuration(payloadConf);
        outputPayLoad.set(JobControlCompiler.PIG_MAP_STORES,
                ObjectSerializer.serialize(new ArrayList<POStore>()));
    }

    if (!(tezOp.getLoaderInfo().getLoads().isEmpty())) {
        payloadConf.set(PigInputFormat.PIG_INPUTS, ObjectSerializer.serialize(tezOp.getLoaderInfo().getInp()));
        payloadConf.set(PigInputFormat.PIG_INPUT_SIGNATURES,
                ObjectSerializer.serialize(tezOp.getLoaderInfo().getInpSignatureLists()));
        payloadConf.set(PigInputFormat.PIG_INPUT_LIMITS,
                ObjectSerializer.serialize(tezOp.getLoaderInfo().getInpLimits()));
        inputPayLoad = new Configuration(payloadConf);
    }

    if (tezOp.getSampleOperator() != null) {
        payloadConf.set(PigProcessor.SAMPLE_VERTEX, tezOp.getSampleOperator().getOperatorKey().toString());
    }

    if (tezOp.getSortOperator() != null) {
        // Required by Sample Aggregation job for estimating quantiles
        payloadConf.set(PigProcessor.SORT_VERTEX, tezOp.getSortOperator().getOperatorKey().toString());
        // PIG-4162: Order by/Skew Join in intermediate stage.
        // Increasing order by parallelism may not be required as it is
        // usually followed by limit other than store. But would benefit
        // cases like skewed join followed by group by.
        if (tezOp.getSortOperator().getEstimatedParallelism() != -1
                && tezOp.getSortOperator().isIntermediateReducer()) {
            payloadConf.setLong(InputSizeReducerEstimator.BYTES_PER_REDUCER_PARAM, intermediateTaskInputSize);
        }

    }

    // Set parent plan for all operators in the Tez plan.
    new PhyPlanSetter(tezOp.plan).visit();

    // Set the endOfAllInput flag on the physical plan if certain operators that
    // use this property (such as STREAM) are present in the plan.
    EndOfAllInputSetter.EndOfAllInputChecker checker = new EndOfAllInputSetter.EndOfAllInputChecker(tezOp.plan);
    checker.visit();
    if (checker.isEndOfAllInputPresent()) {
        payloadConf.set(JobControlCompiler.END_OF_INP_IN_MAP, "true");
    }

    // Configure the classes for incoming shuffles to this TezOp
    // TODO: Refactor out resetting input keys, PIG-3957
    List<PhysicalOperator> roots = tezOp.plan.getRoots();
    if (roots.size() == 1 && roots.get(0) instanceof POPackage) {
        POPackage pack = (POPackage) roots.get(0);

        List<PhysicalOperator> succsList = tezOp.plan.getSuccessors(pack);
        if (succsList != null) {
            succsList = new ArrayList<PhysicalOperator>(succsList);
        }
        byte keyType = pack.getPkgr().getKeyType();
        tezOp.plan.remove(pack);
        payloadConf.set("pig.reduce.package", ObjectSerializer.serialize(pack));

        POShuffleTezLoad newPack = new POShuffleTezLoad(pack);
        if (tezOp.isSkewedJoin()) {
            newPack.setSkewedJoins(true);
        }
        tezOp.plan.add(newPack);

        boolean isMergedInput = false;
        // Set input keys for POShuffleTezLoad. This is used to identify
        // the inputs that are attached to the POShuffleTezLoad in the
        // backend.
        Map<Integer, String> localRearrangeMap = new TreeMap<Integer, String>();
        TezOperator from = null;
        for (TezOperator pred : mPlan.getPredecessors(tezOp)) {
            if (tezOp.getSampleOperator() != null && tezOp.getSampleOperator() == pred) {
                // skip sample vertex input
            } else {
                String inputKey = pred.getOperatorKey().toString();
                boolean isVertexGroup = false;
                if (pred.isVertexGroup()) {
                    isVertexGroup = true;
                    pred = mPlan.getOperator(pred.getVertexGroupMembers().get(0));
                }
                LinkedList<POLocalRearrangeTez> lrs = PlanHelper.getPhysicalOperators(pred.plan,
                        POLocalRearrangeTez.class);
                for (POLocalRearrangeTez lr : lrs) {
                    if (lr.isConnectedToPackage()
                            && lr.getOutputKey().equals(tezOp.getOperatorKey().toString())) {
                        localRearrangeMap.put((int) lr.getIndex(), inputKey);
                        if (isVertexGroup) {
                            isMergedInput = true;
                        }
                        from = pred;
                    }
                }
            }
        }
        for (Map.Entry<Integer, String> entry : localRearrangeMap.entrySet()) {
            newPack.addInputKey(entry.getValue());
        }

        if (succsList != null) {
            for (PhysicalOperator succs : succsList) {
                tezOp.plan.connect(newPack, succs);
            }
        }

        //POShuffleTezLoad accesses the comparator setting
        selectKeyComparator(keyType, payloadConf, tezOp, isMergedInput);

        if (tezOp.isUseSecondaryKey()) {
            TezEdgeDescriptor edge = tezOp.inEdges.get(from.getOperatorKey());
            // Currently only PigSecondaryKeyGroupingComparator is used in POShuffleTezLoad.
            // When PIG-4685: SecondaryKeyOptimizerTez does not optimize cogroup is fixed
            // in future, PigSecondaryKeyComparator will have to be used and that will require this.
            payloadConf.set("pig.secondarySortOrder", ObjectSerializer.serialize(edge.getSecondarySortOrder()));
        }

    }

    // set parent plan in all operators. currently the parent plan is really
    // used only when POStream, POSplit are present in the plan
    new PhyPlanSetter(tezOp.plan).visit();

    // Serialize the execution plan
    payloadConf.set(PigProcessor.PLAN, ObjectSerializer.serialize(tezOp.plan));

    udfContextSeparator.serializeUDFContext(payloadConf, tezOp);

    if (!pc.inIllustrator) {
        for (POStore store : stores) {
            // unset inputs for POStore, otherwise, map/reduce plan will be unnecessarily deserialized
            store.setInputs(null);
            store.setParentPlan(null);
        }
        // We put them in the reduce because PigOutputCommitter checks the
        // ID of the task to see if it's a map, and if not, calls the reduce
        // committers.
        payloadConf.set(JobControlCompiler.PIG_MAP_STORES,
                ObjectSerializer.serialize(new ArrayList<POStore>()));
        payloadConf.set(JobControlCompiler.PIG_REDUCE_STORES, ObjectSerializer.serialize(stores));
    }

    if (tezOp.isNeedEstimateParallelism()) {
        payloadConf.setBoolean(PigProcessor.ESTIMATE_PARALLELISM, true);
        log.info("Estimate quantile for sample aggregation vertex " + tezOp.getOperatorKey().toString());
    }

    // set various parallelism into the job conf for later analysis, PIG-2779
    payloadConf.setInt(PigImplConstants.REDUCER_DEFAULT_PARALLELISM, pc.defaultParallel);
    payloadConf.setInt(PigImplConstants.REDUCER_REQUESTED_PARALLELISM, tezOp.getRequestedParallelism());
    payloadConf.setInt(PigImplConstants.REDUCER_ESTIMATED_PARALLELISM, tezOp.getEstimatedParallelism());

    TezScriptState ss = TezScriptState.get();
    ss.addVertexSettingsToConf(dag.getName(), tezOp, payloadConf);

    // Take our assembled configuration and create a vertex
    UserPayload userPayload = TezUtils.createUserPayloadFromConf(payloadConf);
    TezDAGScriptInfo dagScriptInfo = TezScriptState.get().getDAGScriptInfo(dag.getName());
    String alias = dagScriptInfo.getAlias(tezOp);
    String aliasLocation = dagScriptInfo.getAliasLocation(tezOp);
    String features = dagScriptInfo.getPigFeatures(tezOp);
    String vertexInfo = aliasLocation + " (" + features + ")";
    procDesc.setUserPayload(userPayload).setHistoryText(TezUtils.convertToHistoryText(vertexInfo, payloadConf));

    String vmPluginName = null;
    Configuration vmPluginConf = null;

    // Set the right VertexManagerPlugin
    if (tezOp.getEstimatedParallelism() != -1) {
        if (tezOp.isGlobalSort() || tezOp.isSkewedJoin()) {
            if (tezOp.getVertexParallelism() == -1
                    && (tezOp.isGlobalSort() && getPlan().getPredecessors(tezOp).size() == 1
                            || tezOp.isSkewedJoin() && getPlan().getPredecessors(tezOp).size() == 2)) {
                // Set VertexManagerPlugin to PartitionerDefinedVertexManager, which is able
                // to decrease/increase parallelism of sorting vertex dynamically
                // based on the numQuantiles calculated by sample aggregation vertex
                vmPluginName = PartitionerDefinedVertexManager.class.getName();
                log.info("Set VertexManagerPlugin to PartitionerDefinedParallelismVertexManager for vertex "
                        + tezOp.getOperatorKey().toString());
            }
        } else {
            boolean containScatterGather = false;
            boolean containCustomPartitioner = false;
            for (TezEdgeDescriptor edge : tezOp.inEdges.values()) {
                if (edge.dataMovementType == DataMovementType.SCATTER_GATHER) {
                    containScatterGather = true;
                }
                if (edge.partitionerClass != null) {
                    containCustomPartitioner = true;
                }
            }
            if (containScatterGather && !containCustomPartitioner) {
                vmPluginConf = (vmPluginConf == null) ? new Configuration(pigContextConf) : vmPluginConf;
                // Use auto-parallelism feature of ShuffleVertexManager to dynamically
                // reduce the parallelism of the vertex
                if (payloadConf.getBoolean(PigConfiguration.PIG_TEZ_GRACE_PARALLELISM, true)
                        && !TezOperPlan.getGrandParentsForGraceParallelism(getPlan(), tezOp).isEmpty()
                        && tezOp.getCrossKeys() == null) {
                    vmPluginName = PigGraceShuffleVertexManager.class.getName();
                    tezOp.setUseGraceParallelism(true);
                    vmPluginConf.set("pig.tez.plan", getSerializedTezPlan());
                    vmPluginConf.set(PigImplConstants.PIG_CONTEXT, serializedPigContext);
                } else {
                    vmPluginName = ShuffleVertexManager.class.getName();
                }
                vmPluginConf.setBoolean(ShuffleVertexManager.TEZ_SHUFFLE_VERTEX_MANAGER_ENABLE_AUTO_PARALLEL,
                        true);
                // For Intermediate reduce, set the bytes per reducer to be block size.
                long bytesPerReducer = intermediateTaskInputSize;
                // If there are store statements, use BYTES_PER_REDUCER_PARAM configured by user.
                // If not as default use 384MB for group bys and 256 MB for joins. Not using
                // default 1G as that value was suited for mapreduce logic where numReducers=(map input size/bytesPerReducer).
                // In Tez, numReducers=(map output size/bytesPerReducer) we need lower values to avoid skews in reduce
                // as map input sizes are mostly always high compared to map output.
                if (stores.size() > 0) {
                    if (vmPluginConf.get(InputSizeReducerEstimator.BYTES_PER_REDUCER_PARAM) != null) {
                        bytesPerReducer = vmPluginConf.getLong(
                                InputSizeReducerEstimator.BYTES_PER_REDUCER_PARAM,
                                InputSizeReducerEstimator.DEFAULT_BYTES_PER_REDUCER);
                    } else if (tezOp.isGroupBy()) {
                        bytesPerReducer = SHUFFLE_BYTES_PER_REDUCER_GROUPBY_DEFAULT;
                    } else {
                        bytesPerReducer = SHUFFLE_BYTES_PER_REDUCER_DEFAULT;
                    }
                }
                vmPluginConf.setLong(ShuffleVertexManager.TEZ_SHUFFLE_VERTEX_MANAGER_DESIRED_TASK_INPUT_SIZE,
                        bytesPerReducer);
                log.info("Set auto parallelism for vertex " + tezOp.getOperatorKey().toString());
            }
        }
    }
    if (tezOp.isLimit()
            && (vmPluginName == null || vmPluginName.equals(PigGraceShuffleVertexManager.class.getName())
                    || vmPluginName.equals(ShuffleVertexManager.class.getName()))) {
        if (tezOp.inEdges.values().iterator().next().inputClassName.equals(UnorderedKVInput.class.getName())) {
            // Setting SRC_FRACTION to 0.00001 so that even if there are 100K source tasks,
            // limit job starts when 1 source task finishes.
            // If limit is part of a group by or join because their parallelism is 1,
            // we should leave the configuration with the defaults.
            vmPluginConf = (vmPluginConf == null) ? new Configuration(pigContextConf) : vmPluginConf;
            vmPluginConf.set(ShuffleVertexManager.TEZ_SHUFFLE_VERTEX_MANAGER_MIN_SRC_FRACTION, "0.00001");
            vmPluginConf.set(ShuffleVertexManager.TEZ_SHUFFLE_VERTEX_MANAGER_MAX_SRC_FRACTION, "0.00001");
            log.info("Set " + ShuffleVertexManager.TEZ_SHUFFLE_VERTEX_MANAGER_MIN_SRC_FRACTION
                    + " to 0.00001 for limit vertex " + tezOp.getOperatorKey().toString());
        }
    }

    int parallel = tezOp.getVertexParallelism();
    if (tezOp.isUseGraceParallelism()) {
        parallel = -1;
    }
    Resource resource = tezOp.isUseMRMapSettings() ? mapTaskResource : reduceTaskResource;

    Vertex vertex = Vertex.create(tezOp.getOperatorKey().toString(), procDesc, parallel, resource);

    if (tezOp.isUseMRMapSettings()) {
        vertex.setTaskLaunchCmdOpts(mapTaskLaunchCmdOpts);
        vertex.setTaskEnvironment(mapTaskEnv);
    } else {
        vertex.setTaskLaunchCmdOpts(reduceTaskLaunchCmdOpts);
        vertex.setTaskEnvironment(reduceTaskEnv);
    }

    MRToTezHelper.setVertexConfig(vertex, tezOp.isUseMRMapSettings(), globalConf);

    log.info("For vertex - " + tezOp.getOperatorKey().toString() + ": parallelism="
            + tezOp.getVertexParallelism() + ", memory=" + vertex.getTaskResource().getMemory() + ", java opts="
            + vertex.getTaskLaunchCmdOpts());
    log.info("Processing aliases: " + alias);
    log.info("Detailed locations: " + aliasLocation);
    log.info("Pig features in the vertex: " + features);
    // Right now there can only be one of each of these. Will need to be
    // more generic when there can be more.
    for (POLoad ld : tezOp.getLoaderInfo().getLoads()) {

        // TODO: These should get the globalConf, or a merged version that
        // keeps settings like pig.maxCombinedSplitSize
        Builder userPayLoadBuilder = MRRuntimeProtos.MRInputUserPayloadProto.newBuilder();

        InputSplitInfo inputSplitInfo = tezOp.getLoaderInfo().getInputSplitInfo();
        Map<String, LocalResource> additionalLocalResources = null;
        int spillThreshold = payloadConf.getInt(PigConfiguration.PIG_TEZ_INPUT_SPLITS_MEM_THRESHOLD,
                PigConfiguration.PIG_TEZ_INPUT_SPLITS_MEM_THRESHOLD_DEFAULT);

        // Currently inputSplitInfo is always InputSplitInfoMem at this point
        if (inputSplitInfo instanceof InputSplitInfoMem) {
            MRSplitsProto splitsProto = inputSplitInfo.getSplitsProto();
            int splitsSerializedSize = splitsProto.getSerializedSize();
            if (splitsSerializedSize > spillThreshold) {
                inputPayLoad.setBoolean(org.apache.tez.mapreduce.hadoop.MRJobConfig.MR_TEZ_SPLITS_VIA_EVENTS,
                        false);
                // Write splits to disk
                Path inputSplitsDir = FileLocalizer.getTemporaryPath(pc);
                log.info("Writing input splits to " + inputSplitsDir + " for vertex " + vertex.getName()
                        + " as the serialized size in memory is " + splitsSerializedSize + ". Configured "
                        + PigConfiguration.PIG_TEZ_INPUT_SPLITS_MEM_THRESHOLD + " is " + spillThreshold);
                inputSplitInfo = MRToTezHelper.writeInputSplitInfoToDisk((InputSplitInfoMem) inputSplitInfo,
                        inputSplitsDir, payloadConf, fs);
                additionalLocalResources = new HashMap<String, LocalResource>();
                MRToTezHelper.updateLocalResourcesForInputSplits(fs, inputSplitInfo, additionalLocalResources);
                inputSplitInDiskVertices.add(vertex.getName());
            } else {
                // Send splits via RPC to AM
                userPayLoadBuilder.setSplits(splitsProto);
            }
            //Free up memory
            tezOp.getLoaderInfo().setInputSplitInfo(null);
        }

        udfContextSeparator.serializeUDFContext(inputPayLoad, tezOp, UDFType.LOADFUNC);
        userPayLoadBuilder.setConfigurationBytes(TezUtils.createByteStringFromConf(inputPayLoad));

        vertex.setLocationHint(VertexLocationHint.create(inputSplitInfo.getTaskLocationHints()));
        vertex.addDataSource(ld.getOperatorKey().toString(),
                DataSourceDescriptor.create(
                        InputDescriptor.create(MRInput.class.getName())
                                .setUserPayload(UserPayload.create(
                                        userPayLoadBuilder.build().toByteString().asReadOnlyByteBuffer())),
                        InputInitializerDescriptor.create(MRInputSplitDistributor.class.getName()),
                        inputSplitInfo.getNumTasks(), dag.getCredentials(), null, additionalLocalResources));
    }

    // Union within a split can have multiple stores writing to same output
    Set<String> uniqueStoreOutputs = new HashSet<String>();
    for (POStore store : stores) {

        ArrayList<POStore> singleStore = new ArrayList<POStore>();
        singleStore.add(store);

        Configuration outPayLoad = new Configuration(outputPayLoad);
        udfContextSeparator.serializeUDFContext(outPayLoad, tezOp, store);
        outPayLoad.set(JobControlCompiler.PIG_REDUCE_STORES, ObjectSerializer.serialize(singleStore));

        OutputDescriptor storeOutDescriptor = OutputDescriptor.create(MROutput.class.getName())
                .setUserPayload(TezUtils.createUserPayloadFromConf(outPayLoad));
        if (tezOp.getVertexGroupStores() != null) {
            OperatorKey vertexGroupKey = tezOp.getVertexGroupStores().get(store.getOperatorKey());
            if (vertexGroupKey != null) {
                getPlan().getOperator(vertexGroupKey).getVertexGroupInfo()
                        .setStoreOutputDescriptor(storeOutDescriptor);
                continue;
            }
        }
        String outputKey = ((POStoreTez) store).getOutputKey();
        if (!uniqueStoreOutputs.contains(outputKey)) {
            vertex.addDataSink(outputKey.toString(), DataSinkDescriptor.create(storeOutDescriptor,
                    OutputCommitterDescriptor.create(MROutputCommitter.class.getName()), dag.getCredentials()));
            uniqueStoreOutputs.add(outputKey);
        }
    }

    // LoadFunc and StoreFunc add delegation tokens to Job Credentials in
    // setLocation and setStoreLocation respectively. For eg: HBaseStorage
    // InputFormat add delegation token in getSplits and OutputFormat in
    // checkOutputSpecs. For eg: FileInputFormat and FileOutputFormat
    if (stores.size() > 0) {
        new PigOutputFormat().checkOutputSpecs(job);
    }

    // else if(tezOp.isLimitAfterSort())
    // TODO: PIG-4049 If standalone Limit we need a new VertexManager or new input
    // instead of ShuffledMergedInput. For limit part of the sort (order by parallel 1) itself
    // need to enhance PartitionerDefinedVertexManager

    if (vmPluginName != null) {
        VertexManagerPluginDescriptor vmPluginDescriptor = VertexManagerPluginDescriptor.create(vmPluginName);
        if (vmPluginConf != null) {
            vmPluginDescriptor.setUserPayload(TezUtils.createUserPayloadFromConf(vmPluginConf));
        }
        vertex.setVertexManagerPlugin(vmPluginDescriptor);
    }
    // Reset udfcontext jobconf. It is not supposed to be set in the front end
    UDFContext.getUDFContext().addJobConf(null);
    return vertex;
}

From source file:org.eclipse.che.vfs.impl.fs.FSMountPoint.java

private void doDelete(VirtualFileImpl virtualFile, String lockToken)
        throws ForbiddenException, ServerException {
    if (virtualFile.isFolder()) {
        final LinkedList<VirtualFile> q = new LinkedList<>();
        q.add(virtualFile);// w  ww . ja  v a  2  s  .  c  o m
        while (!q.isEmpty()) {
            for (VirtualFile child : doGetChildren((VirtualFileImpl) q.pop(), SERVICE_GIT_DIR_FILTER)) {
                // Check permission directly for current file only.
                // We already know parent may be deleted by current user otherwise we should not be here.
                if (!hasPermission((VirtualFileImpl) child, BasicPermissions.WRITE.value(), false)) {
                    throw new ForbiddenException(String
                            .format("Unable delete item '%s'. Operation not permitted. ", child.getPath()));
                }
                if (child.isFolder()) {
                    q.push(child);
                } else if (isLocked((VirtualFileImpl) child)) {
                    // Do not check lock token here. It checked only when remove file directly.
                    // If folder contains locked children it may not be deleted.
                    throw new ForbiddenException(
                            String.format("Unable delete item '%s'. Child item '%s' is locked. ",
                                    virtualFile.getPath(), child.getPath()));
                }
            }
        }
    }

    // unlock file
    if (virtualFile.isFile()) {
        final FileLock fileLock = checkIsLockValidAndGet(virtualFile);
        if (NO_LOCK != fileLock) {
            doUnlock(virtualFile, fileLock, lockToken);
        }
    }

    // clear caches
    clearAclCache();
    clearLockTokensCache();
    clearMetadataCache();

    final String path = virtualFile.getPath();
    boolean isFile = virtualFile.isFile();
    if (!deleteRecursive(virtualFile.getIoFile())) {
        LOG.error("Unable delete file {}", virtualFile.getIoFile());
        throw new ServerException(String.format("Unable delete item '%s'. ", path));
    }

    // delete ACL file
    final java.io.File aclFile = new java.io.File(ioRoot,
            toIoPath(getAclFilePath(virtualFile.getVirtualFilePath())));
    if (aclFile.delete()) {
        if (aclFile.exists()) {
            LOG.error("Unable delete ACL file {}", aclFile);
            throw new ServerException(String.format("Unable delete item '%s'. ", path));
        }
    }

    // delete metadata file
    final java.io.File metadataFile = new java.io.File(ioRoot,
            toIoPath(getMetadataFilePath(virtualFile.getVirtualFilePath())));
    if (metadataFile.delete()) {
        if (metadataFile.exists()) {
            LOG.error("Unable delete file metadata {}", metadataFile);
            throw new ServerException(String.format("Unable delete item '%s'. ", path));
        }
    }

    if (searcherProvider != null) {
        try {
            searcherProvider.getSearcher(this, true).delete(path, isFile);
        } catch (ServerException e) {
            LOG.error(e.getMessage(), e);
        }
    }
}

From source file:org.eclipse.che.vfs.impl.fs.FSMountPoint.java

private void doCopy(VirtualFileImpl source, VirtualFileImpl destination) throws ServerException {
    try {//from   w  ww  . j  a  va  2 s .  c o  m
        // First copy metadata (properties) for source.
        // If we do in this way and fail cause to any i/o or
        // other error client will see error and may try to copy again.
        // But if we successfully copy tree (or single file) and then
        // fail to copy metadata client may not try to copy again
        // because copy destination already exists.

        // NOTE: Don't copy lock and permissions, just files itself and metadata files.

        // Check recursively permissions of sources in case of folder
        // and add all item current user cannot read in skip list.
        java.io.FilenameFilter filter = null;
        if (source.isFolder()) {
            final LinkedList<VirtualFileImpl> skipList = new LinkedList<>();
            final LinkedList<VirtualFile> q = new LinkedList<>();
            q.add(source);
            while (!q.isEmpty()) {
                for (VirtualFile current : doGetChildren((VirtualFileImpl) q.pop(), SERVICE_GIT_DIR_FILTER)) {
                    // Check permission directly for current file only.
                    // We already know parent accessible for current user otherwise we should not be here.
                    // Ignore item if don't have permission to read it.
                    if (!hasPermission((VirtualFileImpl) current, BasicPermissions.READ.value(), false)) {
                        skipList.add((VirtualFileImpl) current);
                    } else {
                        if (current.isFolder()) {
                            q.add(current);
                        }
                    }
                }
            }
            if (!skipList.isEmpty()) {
                filter = new java.io.FilenameFilter() {
                    @Override
                    public boolean accept(java.io.File dir, String name) {
                        final String testPath = dir.getAbsolutePath() + java.io.File.separatorChar + name;
                        for (VirtualFileImpl skipFile : skipList) {
                            if (testPath.startsWith(skipFile.getIoFile().getAbsolutePath())) {
                                return false;
                            }
                            final java.io.File metadataFile = new java.io.File(ioRoot,
                                    toIoPath(getMetadataFilePath(skipFile.getVirtualFilePath())));
                            if (metadataFile.exists() && testPath.startsWith(metadataFile.getAbsolutePath())) {
                                return false;
                            }
                        }
                        return true;
                    }
                };
            }
        }

        final java.io.File sourceMetadataFile = new java.io.File(ioRoot,
                toIoPath(getMetadataFilePath(source.getVirtualFilePath())));
        final java.io.File destinationMetadataFile = new java.io.File(ioRoot,
                toIoPath(getMetadataFilePath(destination.getVirtualFilePath())));
        if (sourceMetadataFile.exists()) {
            nioCopy(sourceMetadataFile, destinationMetadataFile, filter);
        }
        nioCopy(source.getIoFile(), destination.getIoFile(), filter);

        if (searcherProvider != null) {
            try {
                searcherProvider.getSearcher(this, true).add(destination);
            } catch (ServerException e) {
                LOG.error(e.getMessage(), e); // just log about i/o error in index
            }
        }
    } catch (IOException e) {
        // Do nothing for file tree. Let client side decide what to do.
        // User may delete copied files (if any) and try copy again.
        String msg = String.format("Unable copy '%s' to '%s'. ", source, destination);
        LOG.error(msg + e.getMessage(), e); // More details in log but do not show internal error to caller.
        throw new ServerException(msg);
    }
}

From source file:view.EditorView.java

/**
 * Renders the current game and unconnected rooms in the view.
 *
 * @param autoLayout      If {@code true}, the rooms will be automatically laid out according to their topology.
 * @param onlyUpdateLines If {@code true}, only connecting lines between the rooms are rendered, rooms are left as they are. Useful if the user is currently moving the room around with the mouse.
 */// w  w w . ja  v a2s .  c  o  m
public void renderView(boolean autoLayout, boolean onlyUpdateLines) {
    int indexCorrection = 0;
    while (drawing.getChildren().size() > indexCorrection) {
        if (!onlyUpdateLines && !(drawing.getChildren().get(indexCorrection) instanceof ConnectionLine)) {
            drawing.getChildren().remove(indexCorrection);
        } else if (drawing.getChildren().get(indexCorrection) instanceof ConnectionLine) {
            // Check if line is still valid
            ((ConnectionLine) drawing.getChildren().get(indexCorrection)).updateLocation();
            indexCorrection++;
        } else {
            indexCorrection++;
        }
    }

    renderThreadPool.submit(() -> {
        // update the connection status of all rooms
        if (allRoomsAsList != null) {
            for (RoomRectangle room : allRoomsAsList) {
                updateConnectionStatusOfRoom(room);
            }
        }

        LinkedList<RoomRectangle> renderQueue = new LinkedList<>();

        // The distance between connected rooms
        double roomDistance = 50;

        RoomRectangle startRoom;
        if (allRoomsAsList == null) {
            // First time to render
            startRoom = new RoomRectangle(drawing, this.getCurrentGame().getCurrentRoom());
            allRoomsAsListCopy = new RoomRectangleList();
            allRoomsAsList = new RoomRectangleList();
            allRoomsAsList.add(startRoom);
        } else {
            startRoom = allRoomsAsList.findByRoom(this.getCurrentGame().getCurrentRoom());
            allRoomsAsListCopy = allRoomsAsList;
            if (!onlyUpdateLines) {
                allRoomsAsList = new RoomRectangleList();
            }
        }

        renderQueue.add(startRoom);

        // render unconnected rooms
        renderQueue.addAll(unconnectedRooms);

        while (!renderQueue.isEmpty()) {
            RoomRectangle currentRoom = renderQueue.remove();
            if (currentRoom == null) {
                FOKLogger.severe(EditorView.class.getName(),
                        "currentRoom == null means that the room was never added to allRoomsAsList and that means that we ran into a bug, so report it :(");
                Platform.runLater(() -> new ReportingDialog(stage.getScene()).show(AppConfig.gitHubUserName,
                        AppConfig.gitHubRepoName, new IllegalStateException(
                                "A room of the game was never added to allRoomsAsList. This is an internal bug and needs to be reported to the dev team. Please tell us at https://github.com/vatbub/zorkClone/issues what you did when this exception occurred.")));
            }

            //noinspection ConstantConditions
            if (!currentRoom.isRendered()) {
                if (!allRoomsAsList.contains(currentRoom)) {
                    allRoomsAsList.add(currentRoom);
                }
                currentRoom.setCustomParent(drawing);
                currentRoom.updateNameLabelPosition();
            }
            for (Map.Entry<WalkDirection, Room> entry : currentRoom.getRoom().getAdjacentRooms().entrySet()) {
                RoomRectangle newRoom;
                newRoom = allRoomsAsListCopy.findByRoom(entry.getValue());

                if (newRoom == null) {
                    // not rendered yet
                    newRoom = new RoomRectangle(drawing, entry.getValue());
                    allRoomsAsList.add(newRoom);
                }

                // Set room position
                if (autoLayout && !newRoom.isRendered()) {
                    switch (entry.getKey()) {
                    case NORTH:
                        newRoom.setY(currentRoom.getY() - newRoom.getHeight() - roomDistance);
                        newRoom.setX(currentRoom.getX() + currentRoom.getWidth() / 2 - newRoom.getWidth() / 2);
                        break;
                    case WEST:
                        newRoom.setY(currentRoom.getY());
                        newRoom.setX(currentRoom.getX() - newRoom.getWidth() - roomDistance);
                        break;
                    case EAST:
                        newRoom.setY(currentRoom.getY());
                        newRoom.setX(currentRoom.getX() + currentRoom.getWidth() + roomDistance);
                        break;
                    case SOUTH:
                        newRoom.setY(currentRoom.getY() + currentRoom.getHeight() + roomDistance);
                        newRoom.setX(currentRoom.getX() + currentRoom.getWidth() / 2 - newRoom.getWidth() / 2);
                        break;
                    case NORTH_WEST:
                        newRoom.setY(currentRoom.getY() - newRoom.getHeight() - roomDistance);
                        newRoom.setX(currentRoom.getX() - newRoom.getWidth() - roomDistance);
                        break;
                    case NORTH_EAST:
                        newRoom.setY(currentRoom.getY() - newRoom.getHeight() - roomDistance);
                        newRoom.setX(currentRoom.getX() + currentRoom.getWidth() + roomDistance);
                        break;
                    case SOUTH_WEST:
                        newRoom.setY(currentRoom.getY() + currentRoom.getHeight() + roomDistance);
                        newRoom.setX(currentRoom.getX() - newRoom.getWidth() - roomDistance);
                        break;
                    case SOUTH_EAST:
                        newRoom.setY(currentRoom.getY() + currentRoom.getHeight() + roomDistance);
                        newRoom.setX(currentRoom.getX() + currentRoom.getWidth() + roomDistance);
                        break;
                    }
                }

                ConnectionLine connectionLine = lineList.findByStartAndEndRoomIgnoreLineDirection(currentRoom,
                        newRoom);
                if (connectionLine == null) {
                    // create a new line
                    connectionLine = new ConnectionLine(currentRoom, newRoom);
                    connectionLine.setInvalidationRunnable(lineInvalidationRunnable);
                    lineList.add(connectionLine);

                    final Line connectionLineCopy = connectionLine;
                    Platform.runLater(() -> drawing.getChildren().add(connectionLineCopy));
                }

                ConnectionLine finalConnectionLine = connectionLine;
                Platform.runLater(finalConnectionLine::updateLocation);

                if (!newRoom.isRendered()) {
                    // render the child
                    renderQueue.add(newRoom);
                }
            }
        }

        // set the room count
        currentRoomCount = allRoomsAsList.size();
        allRoomsAsListCopy = null;
    });
}