Example usage for java.util TreeMap lastKey

List of usage examples for java.util TreeMap lastKey

Introduction

In this page you can find the example usage for java.util TreeMap lastKey.

Prototype

public K lastKey() 

Source Link

Usage

From source file:hydrograph.ui.dataviewer.filter.FilterHelper.java

/**
 * Refresh group selections./*from  w  ww  .java2s . c o  m*/
 * 
 * @param tableViewer
 *            the table viewer
 * @param indexOfRow
 *            the index of row
 * @param addOrDeleteRow
 *            the add or delete row
 * @param groupSelectionMap
 *            the group selection map
 * @return true, if successful
 */
public boolean refreshGroupSelections(TableViewer tableViewer, int indexOfRow, String addOrDeleteRow,
        TreeMap<Integer, List<List<Integer>>> groupSelectionMap) {
    boolean isRemoveColumn = false;
    for (int key : groupSelectionMap.keySet()) {
        List<List<Integer>> groups = groupSelectionMap.get(key);
        boolean isNewIndexAddedorRemoved = false;
        for (List<Integer> grp : groups) {
            List<Integer> tempGrp = new ArrayList<>(grp);
            if ("ADD".equalsIgnoreCase(addOrDeleteRow)) {
                for (int i = 0; i < grp.size(); i++) {
                    if (grp.get(i) >= indexOfRow) {
                        grp.set(i, grp.get(i) + 1);
                    }
                }
                if (tempGrp.contains(indexOfRow)) {
                    if (!isNewIndexAddedorRemoved && tempGrp.get(0) != indexOfRow) {//other than starting index then add row.
                        grp.add(tempGrp.indexOf(indexOfRow), indexOfRow);
                        isNewIndexAddedorRemoved = true;
                    }
                }
            } else if ("DEL".equalsIgnoreCase(addOrDeleteRow)) {
                if (tempGrp.contains(indexOfRow)) {
                    if (!isNewIndexAddedorRemoved) {//other than starting index then add row.
                        grp.remove(grp.indexOf(indexOfRow));
                        if (grp.size() == 1) {
                            grp.clear();
                        }
                        if (rearrangeGroupsAfterDeleteRow(groupSelectionMap, grp)
                                && groupSelectionMap.lastKey() != key) {
                            grp.clear();
                        }
                        List tempList = new ArrayList<>();
                        for (List lst : groupSelectionMap.get(key)) {
                            tempList.addAll(lst);
                        }
                        if (tempList.isEmpty()) {
                            isRemoveColumn = true;
                        }
                    }
                    isNewIndexAddedorRemoved = true;
                }
                for (int i = 0; i < grp.size(); i++) {
                    if (grp.get(i) >= indexOfRow) {
                        grp.set(i, grp.get(i) - 1);
                    }
                }
            }
            tempGrp.clear();
        }
    }
    return isRemoveColumn;
}

From source file:com.nextgis.ngm_clink_monitoring.fragments.MapFragment.java

@Override
public void onSingleTapUp(MotionEvent event) {
    double dMinX = event.getX() - mTolerancePX;
    double dMaxX = event.getX() + mTolerancePX;
    double dMinY = event.getY() - mTolerancePX;
    double dMaxY = event.getY() + mTolerancePX;

    GeoEnvelope mapEnv = mMapView.screenToMap(new GeoEnvelope(dMinX, dMaxX, dMinY, dMaxY));
    if (null == mapEnv) {
        return;/*from  w ww.  j  a v  a 2  s.  com*/
    }

    //show actions dialog
    List<ILayer> layers = mMapView.getVectorLayersByType(GeoConstants.GTAnyCheck);

    TreeMap<Integer, Integer> priorityMap = new TreeMap<>();
    List<FoclVectorLayer> foclVectorLayers = new ArrayList<>();

    for (ILayer layer : layers) {
        if (!layer.isValid()) {
            continue;
        }
        ILayerView layerView = (ILayerView) layer;
        if (!layerView.isVisible()) {
            continue;
        }

        FoclVectorLayer foclVectorLayer = (FoclVectorLayer) layer;
        List<Long> items = foclVectorLayer.query(mapEnv);
        if (!items.isEmpty()) {
            foclVectorLayers.add(foclVectorLayer);

            int type = foclVectorLayer.getFoclLayerType();
            int priority;

            switch (type) {
            case FoclConstants.LAYERTYPE_FOCL_UNKNOWN:
            default:
                priority = 0;
                break;

            case FoclConstants.LAYERTYPE_FOCL_OPTICAL_CABLE:
                priority = 1;
                break;

            case FoclConstants.LAYERTYPE_FOCL_SPECIAL_TRANSITION:
                priority = 2;
                break;

            case FoclConstants.LAYERTYPE_FOCL_FOSC:
                priority = 3;
                break;

            case FoclConstants.LAYERTYPE_FOCL_OPTICAL_CROSS:
                priority = 4;
                break;

            case FoclConstants.LAYERTYPE_FOCL_ACCESS_POINT:
                priority = 5;
                break;

            case FoclConstants.LAYERTYPE_FOCL_REAL_OPTICAL_CABLE_POINT:
                priority = 6;
                break;

            case FoclConstants.LAYERTYPE_FOCL_REAL_FOSC:
                priority = 7;
                break;

            case FoclConstants.LAYERTYPE_FOCL_REAL_OPTICAL_CROSS:
                priority = 8;
                break;

            case FoclConstants.LAYERTYPE_FOCL_REAL_ACCESS_POINT:
                priority = 9;
                break;

            case FoclConstants.LAYERTYPE_FOCL_REAL_SPECIAL_TRANSITION_POINT:
                priority = 10;
                break;
            }

            if (!priorityMap.containsKey(priority)) {
                priorityMap.put(priority, type);
            }
        }
    }

    Integer type = null;
    if (!priorityMap.isEmpty()) {
        Integer key = priorityMap.lastKey();
        type = priorityMap.get(key);
    }

    if (null != type) {
        for (FoclVectorLayer layer : foclVectorLayers) {
            if (type == layer.getFoclLayerType()) {
                List<Long> items = layer.query(mapEnv);

                AttributesDialog attributesDialog = new AttributesDialog();
                attributesDialog.setKeepInstance(true);
                attributesDialog.setParams(layer, items.get(0));
                attributesDialog.show(getActivity().getSupportFragmentManager(),
                        FoclConstants.FRAGMENT_ATTRIBUTES);

                break;
            }
        }
    }
}

From source file:com.google.gwt.emultest.java.util.TreeMapTest.java

/**
 * Test method for 'java.util.TreeMap.TreeMap(SortedMap)'.
 *
 * @see java.util.TreeMap#TreeMap(SortedMap)
 *//*from  ww w .  j  a va 2s  .  c  o  m*/
public void testConstructor_SortedMap() {
    K[] keys = getKeys();
    V[] values = getValues();
    SortedMap<K, V> sourceMap = new TreeMap<K, V>();
    _assertEmpty(sourceMap);

    // populate the source map
    sourceMap.put(keys[0], values[0]);
    sourceMap.put(keys[1], values[1]);
    sourceMap.put(keys[2], values[2]);

    TreeMap<K, V> copyConstructed = new TreeMap<K, V>(sourceMap);
    _assertEquals(sourceMap, copyConstructed);

    Comparator<K> comp = Collections.reverseOrder(getComparator());
    TreeMap<K, V> reversedTreeMap = new TreeMap<K, V>(comp);
    reversedTreeMap.put(keys[0], values[0]);
    reversedTreeMap.put(keys[1], values[1]);
    TreeMap<K, V> anotherTreeMap = new TreeMap<K, V>(reversedTreeMap);
    assertTrue(anotherTreeMap.comparator() == comp);
    assertEquals(keys[1], anotherTreeMap.firstKey());
    assertEquals(keys[0], anotherTreeMap.lastKey());
}

From source file:org.apache.hadoop.hbase.regionserver.StripeStoreFileManager.java

/**
 * Loads initial store files that were picked up from some physical location pertaining to
 * this store (presumably). Unlike adding files after compaction, assumes empty initial
 * sets, and is forgiving with regard to stripe constraints - at worst, many/all files will
 * go to level 0.// w ww.  jav a2s .  c  om
 * @param storeFiles Store files to add.
 */
private void loadUnclassifiedStoreFiles(List<StoreFile> storeFiles) {
    LOG.debug("Attempting to load " + storeFiles.size() + " store files.");
    TreeMap<byte[], ArrayList<StoreFile>> candidateStripes = new TreeMap<byte[], ArrayList<StoreFile>>(
            MAP_COMPARATOR);
    ArrayList<StoreFile> level0Files = new ArrayList<StoreFile>();
    // Separate the files into tentative stripes; then validate. Currently, we rely on metadata.
    // If needed, we could dynamically determine the stripes in future.
    for (StoreFile sf : storeFiles) {
        byte[] startRow = startOf(sf), endRow = endOf(sf);
        // Validate the range and put the files into place.
        if (isInvalid(startRow) || isInvalid(endRow)) {
            insertFileIntoStripe(level0Files, sf); // No metadata - goes to L0.
            ensureLevel0Metadata(sf);
        } else if (!isOpen(startRow) && !isOpen(endRow) && nonOpenRowCompare(startRow, endRow) >= 0) {
            LOG.error("Unexpected metadata - start row [" + Bytes.toString(startRow) + "], end row ["
                    + Bytes.toString(endRow) + "] in file [" + sf.getPath() + "], pushing to L0");
            insertFileIntoStripe(level0Files, sf); // Bad metadata - goes to L0 also.
            ensureLevel0Metadata(sf);
        } else {
            ArrayList<StoreFile> stripe = candidateStripes.get(endRow);
            if (stripe == null) {
                stripe = new ArrayList<StoreFile>();
                candidateStripes.put(endRow, stripe);
            }
            insertFileIntoStripe(stripe, sf);
        }
    }
    // Possible improvement - for variable-count stripes, if all the files are in L0, we can
    // instead create single, open-ended stripe with all files.

    boolean hasOverlaps = false;
    byte[] expectedStartRow = null; // first stripe can start wherever
    Iterator<Map.Entry<byte[], ArrayList<StoreFile>>> entryIter = candidateStripes.entrySet().iterator();
    while (entryIter.hasNext()) {
        Map.Entry<byte[], ArrayList<StoreFile>> entry = entryIter.next();
        ArrayList<StoreFile> files = entry.getValue();
        // Validate the file start rows, and remove the bad ones to level 0.
        for (int i = 0; i < files.size(); ++i) {
            StoreFile sf = files.get(i);
            byte[] startRow = startOf(sf);
            if (expectedStartRow == null) {
                expectedStartRow = startRow; // ensure that first stripe is still consistent
            } else if (!rowEquals(expectedStartRow, startRow)) {
                hasOverlaps = true;
                LOG.warn("Store file doesn't fit into the tentative stripes - expected to start at ["
                        + Bytes.toString(expectedStartRow) + "], but starts at [" + Bytes.toString(startRow)
                        + "], to L0 it goes");
                StoreFile badSf = files.remove(i);
                insertFileIntoStripe(level0Files, badSf);
                ensureLevel0Metadata(badSf);
                --i;
            }
        }
        // Check if any files from the candidate stripe are valid. If so, add a stripe.
        byte[] endRow = entry.getKey();
        if (!files.isEmpty()) {
            expectedStartRow = endRow; // Next stripe must start exactly at that key.
        } else {
            entryIter.remove();
        }
    }

    // In the end, there must be open ends on two sides. If not, and there were no errors i.e.
    // files are consistent, they might be coming from a split. We will treat the boundaries
    // as open keys anyway, and log the message.
    // If there were errors, we'll play it safe and dump everything into L0.
    if (!candidateStripes.isEmpty()) {
        StoreFile firstFile = candidateStripes.firstEntry().getValue().get(0);
        boolean isOpen = isOpen(startOf(firstFile)) && isOpen(candidateStripes.lastKey());
        if (!isOpen) {
            LOG.warn("The range of the loaded files does not cover full key space: from ["
                    + Bytes.toString(startOf(firstFile)) + "], to ["
                    + Bytes.toString(candidateStripes.lastKey()) + "]");
            if (!hasOverlaps) {
                ensureEdgeStripeMetadata(candidateStripes.firstEntry().getValue(), true);
                ensureEdgeStripeMetadata(candidateStripes.lastEntry().getValue(), false);
            } else {
                LOG.warn("Inconsistent files, everything goes to L0.");
                for (ArrayList<StoreFile> files : candidateStripes.values()) {
                    for (StoreFile sf : files) {
                        insertFileIntoStripe(level0Files, sf);
                        ensureLevel0Metadata(sf);
                    }
                }
                candidateStripes.clear();
            }
        }
    }

    // Copy the results into the fields.
    State state = new State();
    state.level0Files = ImmutableList.copyOf(level0Files);
    state.stripeFiles = new ArrayList<ImmutableList<StoreFile>>(candidateStripes.size());
    state.stripeEndRows = new byte[Math.max(0, candidateStripes.size() - 1)][];
    ArrayList<StoreFile> newAllFiles = new ArrayList<StoreFile>(level0Files);
    int i = candidateStripes.size() - 1;
    for (Map.Entry<byte[], ArrayList<StoreFile>> entry : candidateStripes.entrySet()) {
        state.stripeFiles.add(ImmutableList.copyOf(entry.getValue()));
        newAllFiles.addAll(entry.getValue());
        if (i > 0) {
            state.stripeEndRows[state.stripeFiles.size() - 1] = entry.getKey();
        }
        --i;
    }
    state.allFilesCached = ImmutableList.copyOf(newAllFiles);
    this.state = state;
    debugDumpState("Files loaded");
}

From source file:snpviewer.SnpViewer.java

public void writeRegionToFile(final String chromosome, final double start, final double end) {
    /* get coordinates of selection and report back
     * write SNPs in region to file//from  w  w  w . j av a  2 s.  co m
     */
    FileChooser fileChooser = new FileChooser();
    FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("Excel  (*.xlsx)", "*.xlsx");
    fileChooser.getExtensionFilters().add(extFilter);
    fileChooser.setTitle("Write region to Excel file (.xlsx)...");
    File rFile = fileChooser.showSaveDialog(mainWindow);
    if (rFile == null) {
        return;
    } else if (!rFile.getName().endsWith(".xlsx")) {
        rFile = new File(rFile.getAbsolutePath() + ".xlsx");
    }
    final File regionFile = rFile;
    final Task<Boolean> writeTask = new Task() {
        @Override
        protected Boolean call() throws Exception {
            try {

                updateProgress(-1, -1);
                ArrayList<SnpFile> bothFiles = new ArrayList<>();
                bothFiles.addAll(affFiles);
                bothFiles.addAll(unFiles);
                TreeMap<Integer, HashMap<String, String>> coordMap = new TreeMap();
                /*coordmap - key is position, key of hashmap 
                 * is input filename and value call
                 */
                HashMap<Integer, String> coordToId = new HashMap<>();
                double progress = 0;
                double total = bothFiles.size() * 5;
                try {
                    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(regionFile));
                    Workbook wb = new XSSFWorkbook();
                    Sheet sheet = wb.createSheet();
                    int rowNo = 0;
                    Row row = sheet.createRow(rowNo++);
                    for (SnpFile f : bothFiles) {
                        if (isCancelled()) {
                            return false;
                        }
                        updateProgress(++progress, total);
                        updateMessage("Reading region in " + f.inputFile.getName());
                        List<SnpFile.SnpLine> lines = f.getSnpsInRegion(chromosome, (int) start, (int) end);
                        for (SnpFile.SnpLine snpLine : lines) {
                            if (isCancelled()) {
                                return false;
                            }
                            Integer coord = snpLine.getPosition();
                            if (!coordMap.containsKey(coord)) {
                                coordMap.put(coord, new HashMap<String, String>());
                            }
                            String filename = f.inputFile.getName();
                            String rsId = snpLine.getId();
                            String call = snpLine.getCall();
                            coordMap.get(coord).put(filename, call);
                            coordToId.put(coord, rsId);
                        }
                    }
                    Cell cell = row.createCell(0);
                    cell.setCellValue(
                            "chr" + chromosome + ":" + coordMap.firstKey() + "-" + coordMap.lastKey());
                    row = sheet.createRow(rowNo++);
                    cell = row.createCell(0);
                    cell.setCellValue(
                            coordToId.get(coordMap.firstKey()) + ";" + coordToId.get(coordMap.lastKey()));
                    row = sheet.createRow(rowNo++);
                    int colNo = 0;
                    cell = row.createCell(colNo++);
                    cell.setCellValue("Position");
                    cell = row.createCell(colNo++);
                    cell.setCellValue("rsID");
                    for (SnpFile f : bothFiles) {
                        cell = row.createCell(colNo++);
                        if (f.getSampleName() != null && f.getSampleName().length() > 0) {
                            cell.setCellValue(f.getSampleName());
                        } else {
                            cell.setCellValue(f.getInputFileName());
                        }
                    }
                    progress = coordMap.size();
                    total = 5 * coordMap.size();
                    updateMessage("Writing region to file...");
                    for (Entry current : coordMap.entrySet()) {
                        if (isCancelled()) {
                            return false;
                        }
                        progress += 4;
                        updateProgress(progress, total);
                        row = sheet.createRow(rowNo++);
                        colNo = 0;
                        Integer coord = (Integer) current.getKey();
                        cell = row.createCell(colNo++);
                        cell.setCellValue(coord);
                        String rsId = coordToId.get(coord);
                        cell = row.createCell(colNo++);
                        cell.setCellValue(rsId);
                        HashMap<String, String> fileToCall = (HashMap<String, String>) current.getValue();
                        for (SnpFile f : bothFiles) {
                            cell = row.createCell(colNo++);
                            if (fileToCall.containsKey(f.inputFile.getName())) {
                                cell.setCellValue(fileToCall.get(f.inputFile.getName()));
                            } else {
                                cell.setCellValue("-");
                            }
                        }
                    }
                    CellRangeAddress[] regions = { new CellRangeAddress(0, rowNo, 2, 2 + bothFiles.size()) };
                    SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();

                    ConditionalFormattingRule rule1 = sheetCF
                            .createConditionalFormattingRule(ComparisonOperator.EQUAL, "\"AA\"");
                    PatternFormatting fill1 = rule1.createPatternFormatting();
                    fill1.setFillBackgroundColor(IndexedColors.LIGHT_GREEN.index);
                    fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
                    ConditionalFormattingRule rule2 = sheetCF
                            .createConditionalFormattingRule(ComparisonOperator.EQUAL, "\"BB\"");
                    PatternFormatting fill2 = rule2.createPatternFormatting();
                    fill2.setFillBackgroundColor(IndexedColors.PALE_BLUE.index);
                    fill2.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
                    ConditionalFormattingRule rule3 = sheetCF
                            .createConditionalFormattingRule(ComparisonOperator.EQUAL, "\"AB\"");
                    PatternFormatting fill3 = rule3.createPatternFormatting();
                    fill3.setFillBackgroundColor(IndexedColors.ROSE.index);
                    fill3.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
                    sheetCF.addConditionalFormatting(regions, rule3, rule2);
                    sheetCF.addConditionalFormatting(regions, rule1);
                    wb.write(out);
                    out.close();
                    return true;
                } catch (IOException ex) {
                    return false;
                }
            } catch (Exception ex) {
                return false;
            }
        }
    };//end of task

    setProgressMode(true);
    progressBar.progressProperty().bind(writeTask.progressProperty());
    progressMessage.textProperty().bind(writeTask.messageProperty());
    writeTask.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent e) {
            if (e.getSource().getValue() == true) {
                Dialogs.showInformationDialog(null,
                        "Region written to file " + "(" + regionFile.getName() + ") successfully",
                        "Region Written", "SNP Viewer");
            } else {
                Dialogs.showErrorDialog(null, "Region write failed.", "Write Failed", "SNP Viewer");
            }
            setProgressMode(false);
            progressBar.progressProperty().unbind();
            progressBar.progressProperty().set(0);
            progressMessage.textProperty().unbind();
            progressMessage.setText("");
            progressTitle.setText("");

        }

    });
    writeTask.setOnFailed(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent e) {
            setProgressMode(false);
            progressBar.progressProperty().unbind();
            progressBar.progressProperty().set(0);
            progressMessage.textProperty().unbind();
            progressMessage.setText("");
            progressTitle.setText("Region write failed!");
            Dialogs.showErrorDialog(null, "Error writing region to file\n", "Region write error", "SNP Viewer",
                    e.getSource().getException());

        }

    });
    writeTask.setOnCancelled(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent e) {
            progressMessage.setText("Region write cancelled");
            progressTitle.setText("Cancelled");
            setProgressMode(false);
            progressBar.progressProperty().unbind();
            progressBar.progressProperty().set(0);
            Dialogs.showErrorDialog(null, "Error writing region to file\n", "Region write error", "SNP Viewer");
        }

    });
    cancelButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent actionEvent) {
            writeTask.cancel();

        }
    });
    progressTitle.setText("Writing region to .xlsx file");
    new Thread(writeTask).start();
}

From source file:net.spfbl.http.ServerHTTP.java

private static String getControlPanel(Locale locale, User user, Long begin, String filter) {
    StringBuilder builder = new StringBuilder();
    if (begin == null && filter == null) {
        //            builder.append("<!DOCTYPE html>\n");
        builder.append("<html lang=\"");
        builder.append(locale.getLanguage());
        builder.append("\">\n");
        builder.append("  <head>\n");
        builder.append("    <meta charset=\"UTF-8\">\n");
        if (locale.getLanguage().toLowerCase().equals("pt")) {
            builder.append("    <title>Painel de controle do SPFBL</title>\n");
        } else {/*ww  w.j ava  2 s  .c om*/
            builder.append("    <title>SPFBL control panel</title>\n");
        }
        // Styled page.
        builder.append("    <style type=\"text/css\">\n");
        builder.append("      body {\n");
        builder.append("        margin:180px 0px 30px 0px;\n");
        builder.append("        background:lightgray;\n");
        builder.append("      }\n");
        builder.append("      iframe {\n");
        builder.append("        border-width: 0px 0px 0px 0px;\n");
        builder.append("        width:100%;\n");
        builder.append("        height:150px;\n");
        builder.append("      }\n");
        builder.append("      .header {\n");
        builder.append("        background-color:lightgray;\n");
        builder.append("        border-width: 0px 0px 0px 0px;\n");
        builder.append("        position:fixed;\n");
        builder.append("        top:0px;\n");
        builder.append("        margin:auto;\n");
        builder.append("        z-index:1;\n");
        builder.append("        width:100%;\n");
        builder.append("        height:180px;\n");
        builder.append("      }\n");
        builder.append("      .bottom {\n");
        builder.append("        background-color:lightgray;\n");
        builder.append("        border-width: 0px 0px 0px 0px;\n");
        builder.append("        position:fixed;\n");
        builder.append("        bottom:0px;\n");
        builder.append("        margin:auto;\n");
        builder.append("        z-index:1;\n");
        builder.append("        width:100%;\n");
        builder.append("        height:30px;\n");
        builder.append("      }\n");
        builder.append("      .button {\n");
        builder.append("          background-color: #4CAF50;\n");
        builder.append("          border: none;\n");
        builder.append("          color: white;\n");
        builder.append("          padding: 16px 32px;\n");
        builder.append("          text-align: center;\n");
        builder.append("          text-decoration: none;\n");
        builder.append("          display: inline-block;\n");
        builder.append("          font-size: 16px;\n");
        builder.append("          margin: 4px 2px;\n");
        builder.append("          -webkit-transition-duration: 0.4s;\n");
        builder.append("          transition-duration: 0.4s;\n");
        builder.append("          cursor: pointer;\n");
        builder.append("      }\n");
        builder.append("      .sender {\n");
        builder.append("          background-color: white; \n");
        builder.append("          color: black; \n");
        builder.append("          border: 2px solid #008CBA;\n");
        builder.append("          width: 100%;\n");
        builder.append("          word-wrap: break-word;\n");
        builder.append("      }\n");
        builder.append("      .sender:hover {\n");
        builder.append("          background-color: #008CBA;\n");
        builder.append("          color: white;\n");
        builder.append("      }\n");
        builder.append("      .highlight {\n");
        builder.append("        background: #b4b9d2;\n");
        builder.append("        color:black;\n");
        builder.append("        border-top: 1px solid #22262e;\n");
        builder.append("        border-bottom: 1px solid #22262e;\n");
        builder.append("      }\n");
        builder.append("      .highlight:nth-child(odd) td {\n");
        builder.append("        background: #b4b9d2;\n");
        builder.append("      }\n");
        builder.append("      .click {\n");
        builder.append("        cursor:pointer;\n");
        builder.append("        cursor:hand;\n");
        builder.append("      }\n");
        builder.append("      table {\n");
        builder.append("        background: white;\n");
        builder.append("        table-layout:fixed;\n");
        builder.append("        border-collapse: collapse;\n");
        builder.append("        word-wrap:break-word;\n");
        builder.append("        border-radius:3px;\n");
        builder.append("        border-collapse: collapse;\n");
        builder.append("        margin: auto;\n");
        builder.append("        padding:2px;\n");
        builder.append("        width: 100%;\n");
        builder.append("        box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);\n");
        builder.append("        animation: float 5s infinite;\n");
        builder.append("      }\n");
        builder.append("      th {\n");
        builder.append("        color:#FFFFFF;;\n");
        builder.append("        background:#1b1e24;\n");
        builder.append("        border-bottom:4px solid #9ea7af;\n");
        builder.append("        border-right: 0px;\n");
        builder.append("        font-size:16px;\n");
        builder.append("        font-weight: bold;\n");
        builder.append("        padding:4px;\n");
        builder.append("        text-align:left;\n");
        builder.append("        text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);\n");
        builder.append("        vertical-align:middle;\n");
        builder.append("        height:30px;\n");
        builder.append("      }\n");
        builder.append("      tr {\n");
        builder.append("        border-top: 1px solid #C1C3D1;\n");
        builder.append("        border-bottom-: 1px solid #C1C3D1;\n");
        builder.append("        font-size:16px;\n");
        builder.append("        font-weight:normal;\n");
        builder.append("        text-shadow: 0 1px 1px rgba(256, 256, 256, 0.1);\n");
        builder.append("      }\n");
        builder.append("      tr:nth-child(odd) td {\n");
        builder.append("        background:#EBEBEB;\n");
        builder.append("      }\n");
        builder.append("      td {\n");
        builder.append("        padding:2px;\n");
        builder.append("        vertical-align:middle;\n");
        builder.append("        font-size:16px;\n");
        builder.append("        text-shadow: -1px -1px 1px rgba(0, 0, 0, 0.1);\n");
        builder.append("        border-right: 1px solid #C1C3D1;\n");
        builder.append("      }\n");
        builder.append("      input[type=text], select {\n");
        builder.append("        width: 400px;\n");
        builder.append("        padding: 0px 4px;\n");
        builder.append("        margin: 1px 0;\n");
        builder.append("        display: inline-block;\n");
        builder.append("        background: #b4b9d2;\n");
        builder.append("        border: 1px solid #ccc;\n");
        builder.append("        border-radius: 4px;\n");
        builder.append("        box-sizing: border-box;\n");
        builder.append("      }\n");
        builder.append("    </style>\n");
        // JavaScript functions.
        TreeMap<Long, Query> queryMap = user.getQueryMap(null, null);
        builder.append(
                "    <script type=\"text/javascript\" src=\"https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js\"></script>\n");
        builder.append("    <script type=\"text/javascript\">\n");
        builder.append("      window.onbeforeunload = function () {\n");
        builder.append("        document.getElementById('filterField').value = '';\n");
        builder.append("        window.scrollTo(0, 0);\n");
        builder.append("      }\n");
        builder.append("      var last = ");
        if (queryMap.isEmpty()) {
            builder.append(0);
        } else {
            builder.append(queryMap.lastKey());
        }
        builder.append(";\n");
        builder.append("      var filterText = '';\n");
        builder.append("      function view(query) {\n");
        builder.append("        if (query == undefined || query == 0) {\n");
        builder.append("          var viewer = document.getElementById('viewer');\n");
        builder.append("          viewer.src = 'about:blank';\n");
        builder.append("          last = 0;\n");
        builder.append("        } else if (last != query) {\n");
        builder.append("          var viewer = document.getElementById('viewer');\n");
        builder.append("          viewer.addEventListener('load', function() {\n");
        builder.append("            if (document.getElementById(last)) {\n");
        builder.append("              document.getElementById(last).className = 'tr';\n");
        builder.append("              document.getElementById(last).className = 'click';\n");
        builder.append("            }\n");
        builder.append("            document.getElementById(query).className = 'highlight';\n");
        builder.append("            last = query;\n");
        builder.append("          });\n");
        builder.append("          viewer.src = '");
        builder.append(Core.getURL());
        builder.append("' + query;\n");
        builder.append("        }\n");
        builder.append("      }\n");
        builder.append("      function more(query) {\n");
        builder.append("        var rowMore = document.getElementById('rowMore');\n");
        builder.append("        rowMore.onclick = '';\n");
        builder.append("        rowMore.className = 'tr';\n");
        builder.append("        var columnMore = document.getElementById('columnMore');\n");
        if (locale.getLanguage().toLowerCase().equals("pt")) {
            builder.append("        columnMore.innerHTML = 'carregando mais registros';\n");
        } else {
            builder.append("        columnMore.innerHTML = 'loading more records';\n");
        }
        builder.append("        $.post(\n");
        builder.append("          '");
        builder.append(Core.getURL());
        builder.append(user.getEmail());
        builder.append("',\n");
        builder.append("          {filter:filterText,begin:query},\n");
        builder.append("          function(data, status) {\n");
        builder.append("            if (status == 'success') {\n");
        builder.append("              rowMore.parentNode.removeChild(rowMore);\n");
        builder.append("              $('#tableBody').append(data);\n");
        builder.append("            } else {\n");
        if (locale.getLanguage().toLowerCase().equals("pt")) {
            builder.append(
                    "              alert('Houve uma falha de sistema ao tentar realizar esta operao.');\n");
        } else {
            builder.append(
                    "              alert('There was a system crash while trying to perform this operation.');\n");
        }
        builder.append("            }\n");
        builder.append("          }\n");
        builder.append("        );\n");
        builder.append("      }\n");
        builder.append("      function refresh() {\n");
        builder.append("        filterText = document.getElementById('filterField').value;\n");
        builder.append("        $.post(\n");
        builder.append("          '");
        builder.append(Core.getURL());
        builder.append(user.getEmail());
        builder.append("',\n");
        builder.append("          {filter:filterText},\n");
        builder.append("          function(data, status) {\n");
        builder.append("            if (status == 'success') {\n");
        builder.append("              $('#tableBody').html(data);\n");
        builder.append("              view($('#tableBody tr').attr('id'));\n");
        builder.append("            } else {\n");
        if (locale.getLanguage().toLowerCase().equals("pt")) {
            builder.append(
                    "              alert('Houve uma falha de sistema ao tentar realizar esta operao.');\n");
        } else {
            builder.append(
                    "              alert('There was a system crash while trying to perform this operation.');\n");
        }
        builder.append("            }\n");
        builder.append("          }\n");
        builder.append("        );\n");
        builder.append("      }\n");
        builder.append("    </script>\n");
        builder.append("  </head>\n");
        // Body.
        builder.append("  <body>\n");
        builder.append("    <div class=\"header\">\n");
        if (queryMap.isEmpty()) {
            builder.append("      <iframe id=\"viewer\" src=\"about:blank\"></iframe>\n");
        } else {
            builder.append("      <iframe id=\"viewer\" src=\"");
            builder.append(Core.getURL());
            builder.append(queryMap.lastKey());
            builder.append("\"></iframe>\n");
        }
        // Construo da tabela de consultas.
        builder.append("      <table>\n");
        builder.append("        <thead>\n");
        builder.append("          <tr>\n");
        if (locale.getLanguage().toLowerCase().equals("pt")) {
            builder.append("            <th style=\"width:120px;\">Recepo</th>\n");
            builder.append("            <th>Origem</th>\n");
            builder.append("            <th>Remetente</th>\n");
            builder.append("            <th>Contedo</th>\n");
            builder.append("            <th>Entrega</th>\n");
        } else {
            builder.append("            <th style=\"width:160px;\">Reception</th>\n");
            builder.append("            <th style=\"width:auto;\">Source</th>\n");
            builder.append("            <th style=\"width:auto;\">Sender</th>\n");
            builder.append("            <th style=\"width:auto;\">Content</th>\n");
            builder.append("            <th style=\"width:auto;\">Delivery</th>\n");
        }
        builder.append("          </tr>\n");
        builder.append("        </thead>\n");
        builder.append("      </table>\n");
        builder.append("    </div>\n");
        if (queryMap.isEmpty()) {
            builder.append("    <table>\n");
            builder.append("      <tbody>\n");
            builder.append("        <tr>\n");
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                builder.append(
                        "          <td colspan=\"5\" align=\"center\">nenhum registro encontrado</td>\n");
            } else {
                builder.append("          <td colspan=\"5\" align=\"center\">no records found</td>\n");
            }
            builder.append("        </tr>\n");
            builder.append("      </tbody>\n");
            builder.append("    </table>\n");
        } else {
            DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale);
            GregorianCalendar calendar = new GregorianCalendar();
            Long nextQuery = null;
            while (queryMap.size() > User.QUERY_MAX_ROWS) {
                nextQuery = queryMap.pollFirstEntry().getKey();
            }
            builder.append("    <table>\n");
            builder.append("      <tbody id=\"tableBody\">\n");
            for (Long time : queryMap.descendingKeySet()) {
                User.Query query = queryMap.get(time);
                boolean highlight = time.equals(queryMap.lastKey());
                buildQueryRow(locale, builder, dateFormat, calendar, time, query, highlight);
            }
            if (nextQuery == null) {
                builder.append("      <tr>\n");
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    builder.append(
                            "        <td colspan=\"5\" align=\"center\">no foram encontrados outros registros</td>\n");
                } else {
                    builder.append("        <td colspan=\"5\" align=\"center\">no more records found</td>\n");
                }
                builder.append("      </tr>\n");
            } else {
                builder.append("        <tr id=\"rowMore\" class=\"click\" onclick=\"more('");
                builder.append(nextQuery);
                builder.append("')\">\n");
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    builder.append(
                            "          <td id=\"columnMore\" colspan=\"5\" align=\"center\">clique para ver mais registros</td>\n");
                } else {
                    builder.append(
                            "          <td id=\"columnMore\" colspan=\"5\" align=\"center\">click to see more records</td>\n");
                }
                builder.append("        </tr>\n");
            }
            builder.append("      </tbody>\n");
            builder.append("    </table>\n");
        }
        builder.append("    <div class=\"bottom\">\n");
        builder.append("      <table>\n");
        builder.append("        <tr>\n");
        if (locale.getLanguage().toLowerCase().equals("pt")) {
            builder.append(
                    "          <th>Pesquisar <input type=\"text\" id=\"filterField\" name=\"filterField\" onkeydown=\"if (event.keyCode == 13) refresh();\" autofocus></th>\n");
        } else {
            builder.append(
                    "          <th>Search <input type=\"text\" id=\"filterField\" name=\"filterField\" onkeydown=\"if (event.keyCode == 13) refresh();\" autofocus></th>\n");
        }
        builder.append("          <th style=\"text-align:right;\"><small>");
        builder.append(
                "Powered by <a target=\"_blank\" href=\"http://spfbl.net/\" style=\"color: #b4b9d2;\">SPFBL.net</a></small>");
        builder.append("</th>\n");
        builder.append("        </tr>\n");
        builder.append("      <table>\n");
        builder.append("    </div>\n");
        builder.append("  </body>\n");
        builder.append("</html>\n");
    } else {
        TreeMap<Long, Query> queryMap = user.getQueryMap(begin, filter);
        if (queryMap.isEmpty()) {
            builder.append("        <tr>\n");
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                builder.append(
                        "          <td colspan=\"5\" align=\"center\">nenhum registro encontrado</td>\n");
            } else {
                builder.append("          <td colspan=\"5\" align=\"center\">no records found</td>\n");
            }
            builder.append("        </tr>\n");
        } else {
            DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale);
            GregorianCalendar calendar = new GregorianCalendar();
            Long nextQuery = null;
            while (queryMap.size() > User.QUERY_MAX_ROWS) {
                nextQuery = queryMap.pollFirstEntry().getKey();
            }
            for (Long time : queryMap.descendingKeySet()) {
                User.Query query = queryMap.get(time);
                buildQueryRow(locale, builder, dateFormat, calendar, time, query, false);
            }
            if (nextQuery == null) {
                builder.append("        <tr>\n");
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    builder.append(
                            "          <td colspan=\"5\" align=\"center\">no foram encontrados outros registros</td>\n");
                } else {
                    builder.append("          <td colspan=\"5\" align=\"center\">no more records found</td>\n");
                }
                builder.append("        </tr>\n");
            } else {
                builder.append("        <tr id=\"rowMore\" class=\"click\" onclick=\"more('");
                builder.append(nextQuery);
                builder.append("')\">\n");
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    builder.append(
                            "          <td id=\"columnMore\" colspan=\"5\" align=\"center\">clique para ver mais registros</td>\n");
                } else {
                    builder.append(
                            "          <td id=\"columnMore\" colspan=\"5\" align=\"center\">click to see more records</td>\n");
                }
                builder.append("        </tr>\n");
            }
        }
    }
    return builder.toString();
}

From source file:org.apache.nutch.segment.SegmentMerger.java

/**
 * NOTE: in selecting the latest version we rely exclusively on the segment
 * name (not all segment data contain time information). Therefore it is extremely
 * important that segments be named in an increasing lexicographic order as
 * their creation time increases./*from   www. ja v  a2 s. com*/
 */
public void reduce(Text key, Iterator<MetaWrapper> values, OutputCollector<Text, MetaWrapper> output,
        Reporter reporter) throws IOException {
    CrawlDatum lastG = null;
    CrawlDatum lastF = null;
    CrawlDatum lastSig = null;
    Content lastC = null;
    ParseData lastPD = null;
    ParseText lastPT = null;
    String lastGname = null;
    String lastFname = null;
    String lastSigname = null;
    String lastCname = null;
    String lastPDname = null;
    String lastPTname = null;
    TreeMap<String, ArrayList<CrawlDatum>> linked = new TreeMap<String, ArrayList<CrawlDatum>>();
    while (values.hasNext()) {
        MetaWrapper wrapper = values.next();
        Object o = wrapper.get();
        String spString = wrapper.getMeta(SEGMENT_PART_KEY);
        if (spString == null) {
            throw new IOException("Null segment part, key=" + key);
        }
        SegmentPart sp = SegmentPart.parse(spString);
        if (o instanceof CrawlDatum) {
            CrawlDatum val = (CrawlDatum) o;
            // check which output dir it belongs to
            if (sp.partName.equals(CrawlDatum.GENERATE_DIR_NAME)) {
                if (lastG == null) {
                    lastG = val;
                    lastGname = sp.segmentName;
                } else {
                    // take newer
                    if (lastGname.compareTo(sp.segmentName) < 0) {
                        lastG = val;
                        lastGname = sp.segmentName;
                    }
                }
            } else if (sp.partName.equals(CrawlDatum.FETCH_DIR_NAME)) {
                if (lastF == null) {
                    lastF = val;
                    lastFname = sp.segmentName;
                } else {
                    // take newer
                    if (lastFname.compareTo(sp.segmentName) < 0) {
                        lastF = val;
                        lastFname = sp.segmentName;
                    }
                }
            } else if (sp.partName.equals(CrawlDatum.PARSE_DIR_NAME)) {
                if (val.getStatus() == CrawlDatum.STATUS_SIGNATURE) {
                    if (lastSig == null) {
                        lastSig = val;
                        lastSigname = sp.segmentName;
                    } else {
                        // take newer
                        if (lastSigname.compareTo(sp.segmentName) < 0) {
                            lastSig = val;
                            lastSigname = sp.segmentName;
                        }
                    }
                    continue;
                }
                // collect all LINKED values from the latest segment
                ArrayList<CrawlDatum> segLinked = linked.get(sp.segmentName);
                if (segLinked == null) {
                    segLinked = new ArrayList<CrawlDatum>();
                    linked.put(sp.segmentName, segLinked);
                }
                segLinked.add(val);
            } else {
                throw new IOException("Cannot determine segment part: " + sp.partName);
            }
        } else if (o instanceof Content) {
            if (lastC == null) {
                lastC = (Content) o;
                lastCname = sp.segmentName;
            } else {
                if (lastCname.compareTo(sp.segmentName) < 0) {
                    lastC = (Content) o;
                    lastCname = sp.segmentName;
                }
            }
        } else if (o instanceof ParseData) {
            if (lastPD == null) {
                lastPD = (ParseData) o;
                lastPDname = sp.segmentName;
            } else {
                if (lastPDname.compareTo(sp.segmentName) < 0) {
                    lastPD = (ParseData) o;
                    lastPDname = sp.segmentName;
                }
            }
        } else if (o instanceof ParseText) {
            if (lastPT == null) {
                lastPT = (ParseText) o;
                lastPTname = sp.segmentName;
            } else {
                if (lastPTname.compareTo(sp.segmentName) < 0) {
                    lastPT = (ParseText) o;
                    lastPTname = sp.segmentName;
                }
            }
        }
    }
    // perform filtering based on full merge record
    if (mergeFilters != null && !mergeFilters.filter(key, lastG, lastF, lastSig, lastC, lastPD, lastPT,
            linked.isEmpty() ? null : linked.lastEntry().getValue())) {
        return;
    }

    curCount++;
    String sliceName = null;
    MetaWrapper wrapper = new MetaWrapper();
    if (sliceSize > 0) {
        sliceName = String.valueOf(curCount / sliceSize);
        wrapper.setMeta(SEGMENT_SLICE_KEY, sliceName);
    }
    SegmentPart sp = new SegmentPart();
    // now output the latest values
    if (lastG != null) {
        wrapper.set(lastG);
        sp.partName = CrawlDatum.GENERATE_DIR_NAME;
        sp.segmentName = lastGname;
        wrapper.setMeta(SEGMENT_PART_KEY, sp.toString());
        output.collect(key, wrapper);
    }
    if (lastF != null) {
        wrapper.set(lastF);
        sp.partName = CrawlDatum.FETCH_DIR_NAME;
        sp.segmentName = lastFname;
        wrapper.setMeta(SEGMENT_PART_KEY, sp.toString());
        output.collect(key, wrapper);
    }
    if (lastSig != null) {
        wrapper.set(lastSig);
        sp.partName = CrawlDatum.PARSE_DIR_NAME;
        sp.segmentName = lastSigname;
        wrapper.setMeta(SEGMENT_PART_KEY, sp.toString());
        output.collect(key, wrapper);
    }
    if (lastC != null) {
        wrapper.set(lastC);
        sp.partName = Content.DIR_NAME;
        sp.segmentName = lastCname;
        wrapper.setMeta(SEGMENT_PART_KEY, sp.toString());
        output.collect(key, wrapper);
    }
    if (lastPD != null) {
        wrapper.set(lastPD);
        sp.partName = ParseData.DIR_NAME;
        sp.segmentName = lastPDname;
        wrapper.setMeta(SEGMENT_PART_KEY, sp.toString());
        output.collect(key, wrapper);
    }
    if (lastPT != null) {
        wrapper.set(lastPT);
        sp.partName = ParseText.DIR_NAME;
        sp.segmentName = lastPTname;
        wrapper.setMeta(SEGMENT_PART_KEY, sp.toString());
        output.collect(key, wrapper);
    }
    if (linked.size() > 0) {
        String name = linked.lastKey();
        sp.partName = CrawlDatum.PARSE_DIR_NAME;
        sp.segmentName = name;
        wrapper.setMeta(SEGMENT_PART_KEY, sp.toString());
        ArrayList<CrawlDatum> segLinked = linked.get(name);
        for (int i = 0; i < segLinked.size(); i++) {
            CrawlDatum link = segLinked.get(i);
            wrapper.set(link);
            output.collect(key, wrapper);
        }
    }
}

From source file:com.netflix.ice.processor.BillingFileProcessor.java

@Override
protected void poll() throws Exception {

    TreeMap<DateTime, List<BillingFile>> filesToProcess = Maps.newTreeMap();
    Map<DateTime, List<BillingFile>> monitorFilesToProcess = Maps.newTreeMap();

    // list the tar.gz file in billing file folder
    for (int i = 0; i < config.billingS3BucketNames.length; i++) {
        String billingS3BucketName = config.billingS3BucketNames[i];
        String billingS3BucketPrefix = config.billingS3BucketPrefixes.length > i
                ? config.billingS3BucketPrefixes[i]
                : "";
        String accountId = config.billingAccountIds.length > i ? config.billingAccountIds[i] : "";
        String billingAccessRoleName = config.billingAccessRoleNames.length > i
                ? config.billingAccessRoleNames[i]
                : "";
        String billingAccessExternalId = config.billingAccessExternalIds.length > i
                ? config.billingAccessExternalIds[i]
                : "";

        logger.info("trying to list objects in billing bucket " + billingS3BucketName
                + " using assume role, and external id " + billingAccessRoleName + " "
                + billingAccessExternalId);
        List<S3ObjectSummary> objectSummaries = AwsUtils.listAllObjects(billingS3BucketName,
                billingS3BucketPrefix, accountId, billingAccessRoleName, billingAccessExternalId);
        logger.info("found " + objectSummaries.size() + " in billing bucket " + billingS3BucketName);
        TreeMap<DateTime, S3ObjectSummary> filesToProcessInOneBucket = Maps.newTreeMap();
        Map<DateTime, S3ObjectSummary> monitorFilesToProcessInOneBucket = Maps.newTreeMap();

        // for each file, download&process if not needed
        for (S3ObjectSummary objectSummary : objectSummaries) {

            String fileKey = objectSummary.getKey();
            DateTime dataTime = AwsUtils.getDateTimeFromFileNameWithTags(fileKey);
            boolean withTags = true;
            if (dataTime == null) {
                dataTime = AwsUtils.getDateTimeFromFileName(fileKey);
                withTags = false;//from   ww  w.  j  a  v a 2  s.co m
            }

            if (dataTime != null && !dataTime.isBefore(config.startDate)) {
                if (!filesToProcessInOneBucket.containsKey(dataTime)
                        || withTags && config.resourceService != null
                        || !withTags && config.resourceService == null)
                    filesToProcessInOneBucket.put(dataTime, objectSummary);
                else
                    logger.info("ignoring file " + objectSummary.getKey());
            } else {
                logger.info("ignoring file " + objectSummary.getKey());
            }
        }

        for (S3ObjectSummary objectSummary : objectSummaries) {
            String fileKey = objectSummary.getKey();
            DateTime dataTime = AwsUtils.getDateTimeFromFileNameWithMonitoring(fileKey);

            if (dataTime != null && !dataTime.isBefore(config.startDate)) {
                monitorFilesToProcessInOneBucket.put(dataTime, objectSummary);
            }
        }

        for (DateTime key : filesToProcessInOneBucket.keySet()) {
            List<BillingFile> list = filesToProcess.get(key);
            if (list == null) {
                list = Lists.newArrayList();
                filesToProcess.put(key, list);
            }
            list.add(new BillingFile(filesToProcessInOneBucket.get(key), accountId, billingAccessRoleName,
                    billingAccessExternalId, billingS3BucketPrefix));
        }

        for (DateTime key : monitorFilesToProcessInOneBucket.keySet()) {
            List<BillingFile> list = monitorFilesToProcess.get(key);
            if (list == null) {
                list = Lists.newArrayList();
                monitorFilesToProcess.put(key, list);
            }
            list.add(new BillingFile(monitorFilesToProcessInOneBucket.get(key), accountId,
                    billingAccessRoleName, billingAccessExternalId, billingS3BucketPrefix));
        }
    }

    for (DateTime dataTime : filesToProcess.keySet()) {
        startMilli = endMilli = dataTime.getMillis();
        init();

        boolean hasNewFiles = false;
        boolean hasTags = false;
        long lastProcessed = lastProcessTime(AwsUtils.monthDateFormat.print(dataTime));

        for (BillingFile billingFile : filesToProcess.get(dataTime)) {
            S3ObjectSummary objectSummary = billingFile.s3ObjectSummary;
            if (objectSummary.getLastModified().getTime() < lastProcessed) {
                logger.info("data has been processed. ignoring " + objectSummary.getKey() + "...");
                continue;
            }
            hasNewFiles = true;
        }

        if (!hasNewFiles) {
            logger.info("data has been processed. ignoring all files at "
                    + AwsUtils.monthDateFormat.print(dataTime));
            continue;
        }

        long processTime = new DateTime(DateTimeZone.UTC).getMillis();
        for (BillingFile billingFile : filesToProcess.get(dataTime)) {

            S3ObjectSummary objectSummary = billingFile.s3ObjectSummary;
            String fileKey = objectSummary.getKey();

            File file = new File(config.localDir, fileKey.substring(billingFile.prefix.length()));
            logger.info("trying to download " + fileKey + "...");
            boolean downloaded = AwsUtils.downloadFileIfChangedSince(objectSummary.getBucketName(),
                    billingFile.prefix, file, lastProcessed, billingFile.accountId, billingFile.accessRoleName,
                    billingFile.externalId);
            if (downloaded)
                logger.info("downloaded " + fileKey);
            else {
                logger.info("file already downloaded " + fileKey + "...");
            }

            logger.info("processing " + fileKey + "...");
            boolean withTags = fileKey.contains("with-resources-and-tags");
            hasTags = hasTags || withTags;
            processingMonitor = false;
            processBillingZipFile(file, withTags);
            logger.info("done processing " + fileKey);
        }

        if (monitorFilesToProcess.get(dataTime) != null) {
            for (BillingFile monitorBillingFile : monitorFilesToProcess.get(dataTime)) {

                S3ObjectSummary monitorObjectSummary = monitorBillingFile.s3ObjectSummary;
                if (monitorObjectSummary != null) {
                    String monitorFileKey = monitorObjectSummary.getKey();
                    logger.info("processing " + monitorFileKey + "...");
                    File monitorFile = new File(config.localDir,
                            monitorFileKey.substring(monitorFileKey.lastIndexOf("/") + 1));
                    logger.info("trying to download " + monitorFileKey + "...");
                    boolean downloaded = AwsUtils.downloadFileIfChangedSince(
                            monitorObjectSummary.getBucketName(), monitorBillingFile.prefix, monitorFile,
                            lastProcessed, monitorBillingFile.accountId, monitorBillingFile.accessRoleName,
                            monitorBillingFile.externalId);
                    if (downloaded)
                        logger.info("downloaded " + monitorFile);
                    else
                        logger.warn(monitorFile + "already downloaded...");
                    FileInputStream in = new FileInputStream(monitorFile);
                    try {
                        processingMonitor = true;
                        processBillingFile(monitorFile.getName(), in, true);
                    } catch (Exception e) {
                        logger.error("Error processing " + monitorFile, e);
                    } finally {
                        in.close();
                    }
                }
            }
        }

        if (dataTime.equals(filesToProcess.lastKey())) {
            int hours = (int) ((endMilli - startMilli) / 3600000L);
            logger.info("cut hours to " + hours);
            cutData(hours);
        }

        // now get reservation capacity to calculate upfront and un-used cost
        for (Ec2InstanceReservationPrice.ReservationUtilization utilization : Ec2InstanceReservationPrice.ReservationUtilization
                .values())
            processReservations(utilization);

        if (hasTags && config.resourceService != null)
            config.resourceService.commit();

        logger.info("archiving results for " + dataTime + "...");
        archive();
        logger.info("done archiving " + dataTime);

        updateProcessTime(AwsUtils.monthDateFormat.print(dataTime), processTime);
        if (dataTime.equals(filesToProcess.lastKey())) {
            sendOndemandCostAlert();
        }
    }

    logger.info("AWS usage processed.");
}

From source file:io.warp10.continuum.gts.GTSHelper.java

public static List<GeoTimeSerie> chunk(GeoTimeSerie gts, long lastchunk, long chunkwidth, long chunkcount,
        String chunklabel, boolean keepempty, long overlap) throws WarpScriptException {

    if (overlap < 0 || overlap > chunkwidth) {
        throw new WarpScriptException("Overlap cannot exceed chunk width.");
    }/*from w w w. ja va2  s . co  m*/

    //
    // Check if 'chunklabel' exists in the GTS labels
    //

    Metadata metadata = gts.getMetadata();

    if (metadata.getLabels().containsKey(chunklabel)) {
        throw new WarpScriptException(
                "Cannot operate on Geo Time Series which already have a label named '" + chunklabel + "'");
    }

    TreeMap<Long, GeoTimeSerie> chunks = new TreeMap<Long, GeoTimeSerie>();

    //
    // If GTS is bucketized, make sure bucketspan is less than boxwidth
    //

    boolean bucketized = GTSHelper.isBucketized(gts);

    if (bucketized) {
        if (gts.bucketspan > chunkwidth) {
            throw new WarpScriptException(
                    "Cannot operate on Geo Time Series with a bucketspan greater than the chunk width.");
        }
    } else {
        // GTS is not bucketized and has 0 values, if lastchunk was 0, return an empty list as we
        // are unable to produce chunks
        if (0 == gts.values && 0L == lastchunk) {
            return new ArrayList<GeoTimeSerie>();
        }
    }

    //
    // Set chunkcount to Integer.MAX_VALUE if it's 0
    //

    boolean zeroChunkCount = false;

    if (0 == chunkcount) {
        chunkcount = Integer.MAX_VALUE;
        zeroChunkCount = true;
    }

    //
    // Sort timestamps in reverse order so we can produce all chunks in O(n)
    //

    GTSHelper.sort(gts, true);

    //
    // Loop on the chunks
    //

    // Index in the timestamp array
    int idx = 0;

    long bucketspan = gts.bucketspan;
    int bucketcount = gts.bucketcount;
    long lastbucket = gts.lastbucket;

    //
    // If lastchunk is 0, use lastbucket or the most recent tick
    //

    if (0 == lastchunk) {
        if (isBucketized(gts)) {
            lastchunk = lastbucket;
        } else {
            // Use the most recent tick
            lastchunk = gts.ticks[0];
            // Make sure lastchunk is aligned on 'chunkwidth' boundary
            if (0 != (lastchunk % chunkwidth)) {
                lastchunk = lastchunk - (lastchunk % chunkwidth) + chunkwidth;
            }
        }
    }

    for (long i = 0; i < chunkcount; i++) {

        // If we have no more values and were not specified a chunk count, exit the loop, we're done
        if (idx >= gts.values && zeroChunkCount) {
            break;
        }

        // Compute chunk bounds
        long chunkend = lastchunk - i * chunkwidth;
        long chunkstart = chunkend - chunkwidth + 1;

        GeoTimeSerie chunkgts = new GeoTimeSerie(lastbucket, bucketcount, bucketspan, 16);

        // Set metadata for the GTS
        chunkgts.setMetadata(metadata);
        // Add 'chunklabel'
        chunkgts.getMetadata().putToLabels(chunklabel, Long.toString(chunkend));

        if (bucketized) {
            // Chunk is outside the GTS, it will be empty 
            if (lastbucket < chunkstart || chunkend <= lastbucket - (bucketcount * bucketspan)) {
                // Add the (empty) chunk if keepempty is true
                if (keepempty || overlap > 0) {
                    chunks.put(chunkend, chunkgts);
                }
                continue;
            }

            // Set the bucketized parameters in the GTS

            // If bucketspan does not divide chunkwidth, chunks won't be bucketized

            if (0 == chunkwidth % bucketspan) {
                chunkgts.bucketspan = bucketspan;
                chunkgts.lastbucket = chunkend;
                chunkgts.bucketcount = (int) ((chunkend - chunkstart + 1) / bucketspan);
            } else {
                chunkgts.bucketspan = 0L;
                chunkgts.lastbucket = 0L;
                chunkgts.bucketspan = 0;
            }
        }

        //
        // Add the datapoints which fall within the current chunk
        //

        // Advance until the current tick is before 'chunkend'       
        while (idx < gts.values && gts.ticks[idx] > chunkend) {
            idx++;
        }

        // We've exhausted the values
        if (idx >= gts.values) {
            // only add chunk if it's not empty or empty with 'keepempty' set to true
            if (0 != chunkgts.values || (keepempty || overlap > 0)) {
                chunks.put(chunkend, chunkgts);
            }
            continue;
        }

        // The current tick is before the beginning of the current chunk
        if (gts.ticks[idx] < chunkstart) {
            // only add chunk if it's not empty or empty with 'keepempty' set to true
            if (0 != chunkgts.values || (keepempty || overlap > 0)) {
                chunks.put(chunkend, chunkgts);
            }
            continue;
        }

        while (idx < gts.values && gts.ticks[idx] >= chunkstart) {
            GTSHelper.setValue(chunkgts, GTSHelper.tickAtIndex(gts, idx), GTSHelper.locationAtIndex(gts, idx),
                    GTSHelper.elevationAtIndex(gts, idx), GTSHelper.valueAtIndex(gts, idx), false);
            idx++;
        }

        // only add chunk if it's not empty or empty with 'keepempty' set to true
        if (0 != chunkgts.values || (keepempty || overlap > 0)) {
            chunks.put(chunkend, chunkgts);
        }
    }

    //
    // Handle overlapping is need be.
    // We need to iterate over all ticks and add datapoints to each GTS they belong to
    //

    if (overlap > 0) {

        //
        // Check if we need to add a first and a last chunk
        //

        long ts = GTSHelper.tickAtIndex(gts, 0);

        if (ts <= chunks.firstKey() - chunkwidth) {
            Entry<Long, GeoTimeSerie> currentFirst = chunks.firstEntry();
            GeoTimeSerie firstChunk = currentFirst.getValue().cloneEmpty();
            if (GTSHelper.isBucketized(currentFirst.getValue())) {
                firstChunk.lastbucket = firstChunk.lastbucket - firstChunk.bucketspan;
            }
            chunks.put(currentFirst.getKey() - chunkwidth, firstChunk);
        }

        ts = GTSHelper.tickAtIndex(gts, gts.values - 1);

        if (ts >= chunks.lastKey() - chunkwidth + 1 - overlap) {
            Entry<Long, GeoTimeSerie> currentLast = chunks.lastEntry();
            GeoTimeSerie lastChunk = currentLast.getValue().cloneEmpty();
            if (GTSHelper.isBucketized(currentLast.getValue())) {
                lastChunk.lastbucket = lastChunk.lastbucket + lastChunk.bucketspan;
            }
            chunks.put(currentLast.getKey() + chunkwidth, lastChunk);
        }

        //
        // Put all entries in a list so we can access them randomly
        //

        List<Entry<Long, GeoTimeSerie>> allchunks = new ArrayList<Entry<Long, GeoTimeSerie>>(chunks.entrySet());

        int[] currentSizes = new int[allchunks.size()];

        for (int i = 0; i < currentSizes.length; i++) {
            currentSizes[i] = allchunks.get(i).getValue().values;
        }

        //
        // Iterate over chunks, completing with prev and next overlaps
        // Remember the timestamps are in reverse order so far.
        //

        for (int i = 0; i < allchunks.size(); i++) {
            GeoTimeSerie current = allchunks.get(i).getValue();
            long lowerBound = allchunks.get(i).getKey() - chunkwidth + 1 - overlap;
            long upperBound = allchunks.get(i).getKey() + overlap;
            if (i > 0) {
                GeoTimeSerie prev = allchunks.get(i - 1).getValue();
                for (int j = 0; j < currentSizes[i - 1]; j++) {
                    long timestamp = GTSHelper.tickAtIndex(prev, j);
                    if (timestamp < lowerBound) {
                        break;
                    }
                    GTSHelper.setValue(current, timestamp, GTSHelper.locationAtIndex(prev, j),
                            GTSHelper.elevationAtIndex(prev, j), GTSHelper.valueAtIndex(prev, j), false);
                }
            }
            if (i < allchunks.size() - 1) {
                GeoTimeSerie next = allchunks.get(i + 1).getValue();
                for (int j = currentSizes[i + 1] - 1; j >= 0; j--) {
                    long timestamp = GTSHelper.tickAtIndex(next, j);
                    if (timestamp > upperBound) {
                        break;
                    }
                    GTSHelper.setValue(current, timestamp, GTSHelper.locationAtIndex(next, j),
                            GTSHelper.elevationAtIndex(next, j), GTSHelper.valueAtIndex(next, j), false);
                }
            }
        }
    }

    List<GeoTimeSerie> result = new ArrayList<GeoTimeSerie>();

    for (GeoTimeSerie g : chunks.values()) {
        if (!keepempty && 0 == g.values) {
            continue;
        }
        result.add(g);
    }

    return result;
}