Example usage for java.util SortedMap entrySet

List of usage examples for java.util SortedMap entrySet

Introduction

In this page you can find the example usage for java.util SortedMap entrySet.

Prototype

Set<Map.Entry<K, V>> entrySet();

Source Link

Document

Returns a Set view of the mappings contained in this map.

Usage

From source file:org.apache.hadoop.hive.hbase.HiveHFileOutputFormat.java

@Override
public RecordWriter getHiveRecordWriter(final JobConf jc, final Path finalOutPath,
        Class<? extends Writable> valueClass, boolean isCompressed, Properties tableProperties,
        final Progressable progressable) throws IOException {

    // Read configuration for the target path, first from jobconf, then from table properties
    String hfilePath = getFamilyPath(jc, tableProperties);
    if (hfilePath == null) {
        throw new RuntimeException("Please set " + HFILE_FAMILY_PATH + " to target location for HFiles");
    }/*from w  w w.  j a va2s.  c  o m*/

    // Target path's last component is also the column family name.
    final Path columnFamilyPath = new Path(hfilePath);
    final String columnFamilyName = columnFamilyPath.getName();
    final byte[] columnFamilyNameBytes = Bytes.toBytes(columnFamilyName);
    final Job job = new Job(jc);
    setCompressOutput(job, isCompressed);
    setOutputPath(job, finalOutPath);

    // Create the HFile writer
    final org.apache.hadoop.mapreduce.TaskAttemptContext tac = ShimLoader.getHadoopShims()
            .newTaskAttemptContext(job.getConfiguration(), progressable);

    final Path outputdir = FileOutputFormat.getOutputPath(tac);
    final Path taskAttemptOutputdir = new FileOutputCommitter(outputdir, tac).getWorkPath();
    final org.apache.hadoop.mapreduce.RecordWriter<ImmutableBytesWritable, KeyValue> fileWriter = getFileWriter(
            tac);

    // Individual columns are going to be pivoted to HBase cells,
    // and for each row, they need to be written out in order
    // of column name, so sort the column names now, creating a
    // mapping to their column position.  However, the first
    // column is interpreted as the row key.
    String columnList = tableProperties.getProperty("columns");
    String[] columnArray = columnList.split(",");
    final SortedMap<byte[], Integer> columnMap = new TreeMap<byte[], Integer>(Bytes.BYTES_COMPARATOR);
    int i = 0;
    for (String columnName : columnArray) {
        if (i != 0) {
            columnMap.put(Bytes.toBytes(columnName), i);
        }
        ++i;
    }

    return new RecordWriter() {

        @Override
        public void close(boolean abort) throws IOException {
            try {
                fileWriter.close(null);
                if (abort) {
                    return;
                }
                // Move the hfiles file(s) from the task output directory to the
                // location specified by the user.
                FileSystem fs = outputdir.getFileSystem(jc);
                fs.mkdirs(columnFamilyPath);
                Path srcDir = taskAttemptOutputdir;
                for (;;) {
                    FileStatus[] files = fs.listStatus(srcDir, FileUtils.STAGING_DIR_PATH_FILTER);
                    if ((files == null) || (files.length == 0)) {
                        throw new IOException("No family directories found in " + srcDir);
                    }
                    if (files.length != 1) {
                        throw new IOException("Multiple family directories found in " + srcDir);
                    }
                    srcDir = files[0].getPath();
                    if (srcDir.getName().equals(columnFamilyName)) {
                        break;
                    }
                    if (files[0].isFile()) {
                        throw new IOException("No family directories found in " + taskAttemptOutputdir + ". "
                                + "The last component in hfile path should match column family name "
                                + columnFamilyName);
                    }
                }
                for (FileStatus regionFile : fs.listStatus(srcDir, FileUtils.STAGING_DIR_PATH_FILTER)) {
                    fs.rename(regionFile.getPath(), new Path(columnFamilyPath, regionFile.getPath().getName()));
                }
                // Hive actually wants a file as task output (not a directory), so
                // replace the empty directory with an empty file to keep it happy.
                fs.delete(taskAttemptOutputdir, true);
                fs.createNewFile(taskAttemptOutputdir);
            } catch (InterruptedException ex) {
                throw new IOException(ex);
            }
        }

        private void writeText(Text text) throws IOException {
            // Decompose the incoming text row into fields.
            String s = text.toString();
            String[] fields = s.split("\u0001");
            assert (fields.length <= (columnMap.size() + 1));
            // First field is the row key.
            byte[] rowKeyBytes = Bytes.toBytes(fields[0]);
            // Remaining fields are cells addressed by column name within row.
            for (Map.Entry<byte[], Integer> entry : columnMap.entrySet()) {
                byte[] columnNameBytes = entry.getKey();
                int iColumn = entry.getValue();
                String val;
                if (iColumn >= fields.length) {
                    // trailing blank field
                    val = "";
                } else {
                    val = fields[iColumn];
                    if ("\\N".equals(val)) {
                        // omit nulls
                        continue;
                    }
                }
                byte[] valBytes = Bytes.toBytes(val);
                KeyValue kv = new KeyValue(rowKeyBytes, columnFamilyNameBytes, columnNameBytes, valBytes);
                try {
                    fileWriter.write(null, kv);
                } catch (IOException e) {
                    LOG.error("Failed while writing row: " + s);
                    throw e;
                } catch (InterruptedException ex) {
                    throw new IOException(ex);
                }
            }
        }

        private void writePut(PutWritable put) throws IOException {
            ImmutableBytesWritable row = new ImmutableBytesWritable(put.getPut().getRow());
            SortedMap<byte[], List<Cell>> cells = put.getPut().getFamilyCellMap();
            for (Map.Entry<byte[], List<Cell>> entry : cells.entrySet()) {
                Collections.sort(entry.getValue(), new CellComparator());
                for (Cell c : entry.getValue()) {
                    try {
                        fileWriter.write(row, KeyValueUtil.copyToNewKeyValue(c));
                    } catch (InterruptedException e) {
                        throw (InterruptedIOException) new InterruptedIOException().initCause(e);
                    }
                }
            }
        }

        @Override
        public void write(Writable w) throws IOException {
            if (w instanceof Text) {
                writeText((Text) w);
            } else if (w instanceof PutWritable) {
                writePut((PutWritable) w);
            } else {
                throw new IOException("Unexpected writable " + w);
            }
        }
    };
}

From source file:de.huxhorn.lilith.swing.MainFrame.java

private void setShowingStatusbar(boolean showingStatusbar) {
    statusBar.setVisible(showingStatusbar);
    // change for all other open windows
    {//from ww  w .j  av a 2 s. c  o m
        SortedMap<EventSource<LoggingEvent>, ViewContainer<LoggingEvent>> views = getSortedLoggingViews();
        for (Map.Entry<EventSource<LoggingEvent>, ViewContainer<LoggingEvent>> current : views.entrySet()) {
            setShowingStatusbar(current.getValue(), showingStatusbar);
        }
    }
    {
        SortedMap<EventSource<AccessEvent>, ViewContainer<AccessEvent>> views = getSortedAccessViews();
        for (Map.Entry<EventSource<AccessEvent>, ViewContainer<AccessEvent>> current : views.entrySet()) {
            setShowingStatusbar(current.getValue(), showingStatusbar);
        }
    }
}

From source file:de.huxhorn.lilith.swing.MainFrame.java

private void setShowingToolbar(boolean showingToolbar) {
    toolbar.setVisible(showingToolbar);/*from   www  . j a  va  2 s .co  m*/

    // change for all other open windows
    {
        SortedMap<EventSource<LoggingEvent>, ViewContainer<LoggingEvent>> views = getSortedLoggingViews();
        for (Map.Entry<EventSource<LoggingEvent>, ViewContainer<LoggingEvent>> current : views.entrySet()) {
            setShowingToolbar(current.getValue(), showingToolbar);
        }
    }
    {
        SortedMap<EventSource<AccessEvent>, ViewContainer<AccessEvent>> views = getSortedAccessViews();
        for (Map.Entry<EventSource<AccessEvent>, ViewContainer<AccessEvent>> current : views.entrySet()) {
            setShowingToolbar(current.getValue(), showingToolbar);
        }
    }
}

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

public void testSubMap_entrySet() {
    K[] keys = getSortedKeys();//from   ww  w  . jav a  2  s. co m
    V[] values = getSortedValues();
    NavigableMap<K, V> map = createNavigableMap();
    map.put(keys[0], values[0]);
    map.put(keys[1], values[1]);
    map.put(keys[2], values[2]);
    map.put(keys[3], values[3]);

    SortedMap<K, V> subMap = map.subMap(keys[1], keys[3]);
    Set<Entry<K, V>> entries = subMap.entrySet();
    assertEquals(2, subMap.size());
    assertEquals(subMap.size(), entries.size());
    assertFalse(entries.contains(new SimpleEntry<K, V>(keys[0], values[0])));
    assertTrue(entries.contains(new SimpleEntry<K, V>(keys[1], values[1])));
    assertTrue(entries.contains(new SimpleEntry<K, V>(keys[2], values[2])));
    assertFalse(entries.contains(new SimpleEntry<K, V>(keys[3], values[3])));

    entries.remove(new SimpleEntry<K, V>(keys[1], values[1]));
    assertEquals(3, map.size());
    assertEquals(subMap.size(), entries.size());
    assertFalse(entries.contains(new SimpleEntry<K, V>(keys[1], values[1])));
    assertFalse(subMap.containsKey(keys[1]));
    assertFalse(subMap.containsValue(values[1]));

    entries.clear();
    assertEquals(2, map.size());
    assertEquals(subMap.size(), entries.size());
    assertTrue(entries.isEmpty());
    assertTrue(subMap.isEmpty());

    subMap.put(keys[2], values[2]);
    assertEquals(1, subMap.size());
    assertEquals(subMap.size(), entries.size());

    subMap.put(keys[1], values[1]);
    Iterator<Entry<K, V>> it = entries.iterator();
    while (it.hasNext()) {
        Map.Entry<K, V> entry = it.next();
        subMap.containsKey(entry.getKey());
        subMap.containsValue(entry.getValue());
        it.remove();
    }
    try {
        it.next();
        fail("should throw NoSuchElementException");
    } catch (NoSuchElementException expected) {
    }
    assertEquals(2, map.size());
    assertEquals(0, subMap.size());
    assertEquals(subMap.size(), entries.size());

    map = createNavigableMap();
    Set<Entry<K, V>> entrySet = map.entrySet();
    map.put(keys[0], values[0]);
    map.put(keys[1], values[1]);
    map.put(keys[2], values[2]);
    assertEquals(map.size(), entrySet.size());
    _assertEquals(entrySet, map.entrySet());
    map.clear();
    assertEquals(map.size(), entrySet.size());
    _assertEquals(entrySet, map.entrySet());
    map.put(keys[0], values[0]);
    assertEquals(map.size(), entrySet.size());
    _assertEquals(entrySet, map.entrySet());
    entrySet.clear();
    assertEquals(map.size(), entrySet.size());
    _assertEquals(entrySet, map.entrySet());
}

From source file:de.interactive_instruments.ShapeChange.Target.FeatureCatalogue.FeatureCatalogue.java

public void initialise(PackageInfo p, Model m, Options o, ShapeChangeResult r, boolean diagOnly)
        throws ShapeChangeAbortException {
    pi = p;/*from   w  w  w.ja  v a  2s .c om*/
    model = m;
    options = o;
    result = r;

    try {

        if (!initialised) {
            initialised = true;

            String pathToJavaExe_ = options.parameter(this.getClass().getName(), PARAM_JAVA_EXE_PATH);
            if (pathToJavaExe_ != null && pathToJavaExe_.trim().length() > 0) {
                pathToJavaExe = pathToJavaExe_.trim();
                if (!pathToJavaExe.startsWith("\"")) {
                    pathToJavaExe = "\"" + pathToJavaExe;
                }
                if (!pathToJavaExe.endsWith("\"")) {
                    pathToJavaExe = pathToJavaExe + "\"";
                }

                String jo_tmp = options.parameter(this.getClass().getName(), PARAM_JAVA_OPTIONS);
                if (jo_tmp != null && jo_tmp.trim().length() > 0) {
                    javaOptions = jo_tmp.trim();
                }

                /*
                 * check path - and potentially also options - by invoking
                 * the exe
                 */
                List<String> cmds = new ArrayList<String>();
                cmds.add(pathToJavaExe);
                if (javaOptions != null) {
                    cmds.add(javaOptions);
                }
                cmds.add("-version");

                ProcessBuilder pb = new ProcessBuilder(cmds);

                try {
                    Process proc = pb.start();

                    StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream());
                    StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream());

                    errorGobbler.start();
                    outputGobbler.start();

                    errorGobbler.join();
                    outputGobbler.join();

                    int exitVal = proc.waitFor();

                    if (exitVal != 0) {
                        if (errorGobbler.hasResult()) {
                            MessageContext mc = result.addFatalError(this, 21, StringUtils.join(cmds, " "),
                                    "" + exitVal);
                            mc.addDetail(this, 27, errorGobbler.getResult());
                        } else {
                            result.addFatalError(this, 21, StringUtils.join(cmds, " "), "" + exitVal);
                        }
                        throw new ShapeChangeAbortException();
                    }

                } catch (InterruptedException e) {
                    result.addFatalError(this, 22);
                    throw new ShapeChangeAbortException();
                }
            }

            encounteredAppSchemasByName = new TreeMap<String, Integer>();

            initialiseFromOptions();

            String s = null;

            Model refModel_tmp = getReferenceModel();

            if (refModel_tmp != null) {

                /*
                 * Ensure that IDs used in the reference model are unique to
                 * that model and do not get mixed up with the IDs of the
                 * input model.
                 * 
                 * REQUIREMENT for model diff: two objects with equal ID
                 * must represent the same model element. If a model element
                 * is deleted in the reference model, then a new model
                 * element in the input model must not have the same ID.
                 * 
                 * It looks like this cannot be guaranteed. Therefore we add
                 * a prefix to the IDs of the model elements in the
                 * reference model.
                 */
                refModel = new GenericModel(refModel_tmp);
                refModel_tmp.shutdown();

                refModel.addPrefixToModelElementIDs("refmodel_");
            }

            String xmlName = outputFilename + ".tmp.xml";

            // Check whether we can use the given output directory
            File outputDirectoryFile = new File(outputDirectory);
            boolean exi = outputDirectoryFile.exists();
            if (!exi) {
                outputDirectoryFile.mkdirs();
                exi = outputDirectoryFile.exists();
            }
            boolean dir = outputDirectoryFile.isDirectory();
            boolean wrt = outputDirectoryFile.canWrite();
            boolean rea = outputDirectoryFile.canRead();
            if (!exi || !dir || !wrt || !rea) {
                result.addFatalError(this, 12, outputDirectory);
                throw new ShapeChangeAbortException();
            }

            String encoding_ = encoding == null ? "UTF-8" : model.characterEncoding();

            OutputStream fout = new FileOutputStream(outputDirectory + "/" + xmlName);
            OutputStream bout = new BufferedOutputStream(fout, streamBufferSize);
            OutputStreamWriter outputXML = new OutputStreamWriter(bout, encoding_);

            writer = new XMLWriter(outputXML, encoding_);

            writer.forceNSDecl("http://www.w3.org/2001/XMLSchema-instance", "xsi");

            writer.startDocument();

            writer.processingInstruction("xml-stylesheet", "type='text/xsl' href='./html.xsl'");

            writer.comment("Feature catalogue created using ShapeChange");

            AttributesImpl atts = new AttributesImpl();
            atts.addAttribute("http://www.w3.org/2001/XMLSchema-instance", "noNamespaceSchemaLocation",
                    "xsi:noNamespaceSchemaLocation", "CDATA", "FC.xsd");
            writer.startElement("", "FeatureCatalogue", "", atts);

            s = options.parameter(this.getClass().getName(), "name");
            if (s != null && s.length() > 0)
                writer.dataElement("name", s);
            else
                writer.dataElement("name", "unknown");

            s = options.parameter(this.getClass().getName(), "scope");

            if (s != null && s.length() > 0)
                PrintLineByLine(s, "scope", null);
            else {
                writer.dataElement("scope", "unknown");
            }

            s = options.parameter(this.getClass().getName(), "versionNumber");
            if (s != null && s.length() > 0)
                writer.dataElement("versionNumber", s);
            else
                writer.dataElement("versionNumber", "unknown");

            s = options.parameter(this.getClass().getName(), "versionDate");
            if (s != null && s.length() > 0)
                writer.dataElement("versionDate", s);
            else
                writer.dataElement("versionDate", "unknown");

            s = options.parameter(this.getClass().getName(), "producer");
            if (s != null && s.length() > 0)
                writer.dataElement("producer", s);
            else
                writer.dataElement("producer", "unknown");
        }

        // we need to compute the diff for each application schema
        if (refModel != null) {

            SortedSet<PackageInfo> set = refModel.schemas(p.name());

            if (set.size() == 1) {

                /*
                 * Get the full names of classes (in lower case) from the
                 * input schema so that later we can look them up by their
                 * full name (within the schema, not in the model).
                 */
                inputSchemaClassesByFullNameInSchema = new HashMap<String, ClassInfo>();
                for (ClassInfo ci : model.classes(pi)) {
                    inputSchemaClassesByFullNameInSchema.put(ci.fullNameInSchema().toLowerCase(Locale.ENGLISH),
                            ci);
                }

                // compute diffs
                differ = new Differ();
                refPackage = set.iterator().next();
                SortedMap<Info, SortedSet<DiffElement>> pi_diffs = differ.diff(p, refPackage);

                // merge diffs for pi with existing diffs (from other
                // schemas)
                differ.merge(diffs, pi_diffs);

                // log the diffs found for pi
                for (Entry<Info, SortedSet<DiffElement>> me : pi_diffs.entrySet()) {

                    MessageContext mc = result.addInfo(
                            "Model difference - " + me.getKey().fullName().replace(p.fullName(), p.name()));

                    for (DiffElement diff : me.getValue()) {
                        String s = diff.change + " " + diff.subElementType;
                        if (diff.subElementType == ElementType.TAG)
                            s += "(" + diff.tag + ")";
                        if (diff.subElement != null)
                            s += " " + diff.subElement.name();
                        else if (diff.diff != null)
                            s += " " + (new diff_match_patch()).diff_prettyHtml(diff.diff);
                        else
                            s += " ???";
                        mc.addDetail(s);
                    }
                }

                /*
                 * switch to default xslt for html diff - unless the
                 * configuration explicitly names an XSLT file to use
                 */
                if (options.parameter(this.getClass().getName(), "xslhtmlFile") == null) {
                    xslhtmlfileName = DEFAULT_XSL_HTML_DIFF_FILE_NAME;
                }

            } else {
                result.addWarning(null, 308, p.name());
                refModel = null;
            }
        }

        writer.startElement("ApplicationSchema", "id", "_P" + pi.id());

        /*
         * Determine if app schema with same name has been encountered
         * before, and choose name accordingly
         */

        String nameForAppSchema = null;

        if (encounteredAppSchemasByName.containsKey(pi.name())) {
            int count = encounteredAppSchemasByName.get(pi.name()).intValue();
            count++;
            nameForAppSchema = pi.name() + " (" + count + ")";
            encounteredAppSchemasByName.put(pi.name(), new Integer(count));
        } else {
            nameForAppSchema = pi.name();
            encounteredAppSchemasByName.put(pi.name(), new Integer(1));
        }

        // now set the name of the application schema
        writer.dataElement("name", nameForAppSchema);

        String s = pi.definition();
        if (s != null && s.length() > 0) {
            PrintLineByLine(s, "definition", null);
        }
        s = pi.description();
        if (s != null && s.length() > 0) {
            PrintLineByLine(s, "description", null);
        }

        s = pi.version();
        if (s != null && s.length() > 0) {
            writer.dataElement("versionNumber", s);
        }

        writer.startElement("taggedValues");

        s = pi.taggedValue(TransformationConstants.TRF_TV_NAME_GENERATIONDATETIME);
        if (s != null && s.trim().length() > 0) {
            writer.dataElement(TransformationConstants.TRF_TV_NAME_GENERATIONDATETIME, PrepareToPrint(s));
        }

        writer.endElement("taggedValues");

        if (pi.getDiagrams() != null) {
            appendImageInfo(pi.getDiagrams());
        }

        writer.endElement("ApplicationSchema");

        /*
         * Check if there are any deletions of classes or packages that are
         * owned by the application schema package.
         */

        if (hasDiff(pi, ElementType.SUBPACKAGE, Operation.DELETE)) {

            Set<DiffElement> pkgdiffs = getDiffs(pi, ElementType.SUBPACKAGE, Operation.DELETE);

            for (DiffElement diff : pkgdiffs) {

                // child package was deleted
                PrintPackage((PackageInfo) diff.subElement, Operation.DELETE);
            }

        }

        printContainedPackages(pi);

        /*
         * NOTE: inserted or unchanged classes are handled in
         * process(ClassInfo) method
         */
        printDeletedClasses(pi);

    } catch (Exception e) {

        String msg = e.getMessage();
        if (msg != null) {
            result.addError(msg);
        }
        e.printStackTrace(System.err);
    }
}

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

public void testHeadMapLjava_lang_Object() {
    K[] keys = getSortedKeys();//  ww  w  . ja  v a2  s  . c  o  m
    V[] values = getSortedValues();
    NavigableMap<K, V> map = createNavigableMap();
    for (int i = 0; i < keys.length; i++) {
        map.put(keys[i], values[i]);
    }

    Map<K, V> head = map.headMap(keys[3]);
    assertEquals(3, head.size());
    assertTrue(head.containsKey(keys[0]));
    assertTrue(head.containsValue(values[1]));
    assertTrue(head.containsKey(keys[2]));

    if (useNullKey() && useNullValue()) {
        map.put(null, null);

        SortedMap<K, V> submap = map.headMap(null);
        assertEquals(0, submap.size());

        Set<K> keySet = submap.keySet();
        assertEquals(0, keySet.size());

        Set<Map.Entry<K, V>> entrySet = submap.entrySet();
        assertEquals(0, entrySet.size());

        Collection<V> valueCollection = submap.values();
        assertEquals(0, valueCollection.size());

        map.remove(null);
    }

    SortedMap<K, V> submap = map.headMap(getLessThanMinimumKey());
    assertEquals(submap.size(), 0);
    assertTrue(submap.isEmpty());
    try {
        submap.firstKey();
        fail("NoSuchElementException should be thrown");
    } catch (NoSuchElementException expected) {
    }

    try {
        submap.lastKey();
        fail("NoSuchElementException should be thrown");
    } catch (NoSuchElementException expected) {
    }

    try {
        submap.headMap(null);
        assertTrue("expected exception", useNullKey());
    } catch (NullPointerException e) {
        assertFalse("unexpected NPE", useNullKey());
    }
}

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

/**
 * Test method for 'java.util.Map.put(Object, Object)'. This test shows some
 * bad behavior of the TreeMap class before JDK 7. A mapping with null key can
 * be put in but several methods are are unusable afterward.
 *
 * A SortedMap with natural ordering (no comparator) is supposed to throw a
 * null pointer exception if a null keys are "not supported". For a natural
 * ordered TreeMap before JDK 7, a null pointer exception is not thrown. But,
 * the map is left in a state where any other key based methods result in a
 * null pointer exception.//from  w  w  w  . j a  va  2s .  c  o  m
 *
 * @see java.util.Map#put(Object, Object)
 */
public void testPut_nullKey() {
    K[] keys = getSortedKeys();
    V[] values = getSortedValues();
    SortedMap<K, V> sortedMap = createNavigableMap();

    if (useNullKey()) {
        assertNull(sortedMap.put(null, values[0]));
        assertTrue(sortedMap.containsValue(values[0]));

        // the map methods the continue to function
        sortedMap.containsValue(null);
        sortedMap.containsValue(values[0]);
        sortedMap.entrySet();
        sortedMap.equals(createMap());
        sortedMap.hashCode();
        sortedMap.isEmpty();
        sortedMap.keySet();
        sortedMap.putAll(createMap());
        sortedMap.size();
        sortedMap.values();

        // all of the sorted map methods still function
        sortedMap.comparator();
        sortedMap.firstKey();
        sortedMap.lastKey();
        sortedMap.subMap(getLessThanMinimumKey(), getGreaterThanMaximumKey());
        sortedMap.headMap(getLessThanMinimumKey());
        sortedMap.tailMap(getLessThanMinimumKey());
    } else if (TestUtils.getJdkVersion() > 6) {
        // nulls are rejected immediately and don't poison the map anymore
        try {
            assertNull(sortedMap.put(null, values[0]));
            fail("should have thrown");
        } catch (NullPointerException e) {
            // expected outcome
        }
        try {
            assertNull(sortedMap.put(null, values[1]));
            fail("expected exception adding second null");
        } catch (NullPointerException e) {
            // expected outcome
        }
        try {
            sortedMap.containsKey(null);
            fail("expected exception on containsKey(null)");
        } catch (NullPointerException e) {
            // expected outcome
        }
        sortedMap.containsKey(keys[0]);
        try {
            sortedMap.get(null);
            fail("expected exception on get(null)");
        } catch (NullPointerException e) {
            // expected outcome
        }
        sortedMap.get(keys[0]);
        try {
            sortedMap.remove(null);
        } catch (NullPointerException e) {
            // expected
        }
        sortedMap.remove(keys[0]);
    } else {
        // before JDK 7, nulls poisoned the map
        try {
            assertNull(sortedMap.put(null, values[0]));
            // note: first null added is not required to throw NPE since no
            // comparisons are needed
        } catch (NullPointerException e) {
            // expected outcome
        }
        try {
            assertNull(sortedMap.put(null, values[1]));
            fail("expected exception adding second null");
        } catch (NullPointerException e) {
            // expected outcome
        }
        try {
            sortedMap.containsKey(null);
            fail("expected exception on containsKey(null)");
        } catch (NullPointerException e) {
            // expected outcome
        }
        try {
            sortedMap.containsKey(keys[0]);
            fail("expected exception on contains(key)");
        } catch (NullPointerException e) {
            // expected outcome
        }
        try {
            sortedMap.get(null);
            fail("expected exception on get(null)");
        } catch (NullPointerException e) {
            // expected outcome
        }
        try {
            sortedMap.get(keys[0]);
            fail("expected exception on get(key)");
        } catch (NullPointerException e) {
            // expected outcome
        }
        try {
            sortedMap.remove(null);
            fail("expected exception on remove(null)");
        } catch (NullPointerException e) {
            // expected outcome
        }
        try {
            sortedMap.remove(keys[0]);
            fail("expected exception on remove(key)");
        } catch (NullPointerException e) {
            // expected outcome
        }
    }
}

From source file:org.apache.accumulo.tserver.TabletServer.java

public static Pair<Text, KeyExtent> verifyTabletInformation(AccumuloServerContext context, KeyExtent extent,
        TServerInstance instance, SortedMap<Key, Value> tabletsKeyValues, String clientAddress, ZooLock lock)
        throws AccumuloSecurityException, DistributedStoreException, AccumuloException {

    log.debug("verifying extent " + extent);
    if (extent.isRootTablet()) {
        return verifyRootTablet(extent, instance);
    }//from  www.j a  v a 2 s .c  om
    String tableToVerify = MetadataTable.ID;
    if (extent.isMeta())
        tableToVerify = RootTable.ID;

    List<ColumnFQ> columnsToFetch = Arrays
            .asList(new ColumnFQ[] { TabletsSection.ServerColumnFamily.DIRECTORY_COLUMN,
                    TabletsSection.TabletColumnFamily.PREV_ROW_COLUMN,
                    TabletsSection.TabletColumnFamily.SPLIT_RATIO_COLUMN,
                    TabletsSection.TabletColumnFamily.OLD_PREV_ROW_COLUMN,
                    TabletsSection.ServerColumnFamily.TIME_COLUMN });

    ScannerImpl scanner = new ScannerImpl(context, tableToVerify, Authorizations.EMPTY);
    scanner.setRange(extent.toMetadataRange());

    TreeMap<Key, Value> tkv = new TreeMap<Key, Value>();
    for (Entry<Key, Value> entry : scanner)
        tkv.put(entry.getKey(), entry.getValue());

    // only populate map after success
    if (tabletsKeyValues == null) {
        tabletsKeyValues = tkv;
    } else {
        tabletsKeyValues.clear();
        tabletsKeyValues.putAll(tkv);
    }

    Text metadataEntry = extent.getMetadataEntry();

    Value dir = checkTabletMetadata(extent, instance, tabletsKeyValues, metadataEntry);
    if (dir == null)
        return null;

    Value oldPrevEndRow = null;
    for (Entry<Key, Value> entry : tabletsKeyValues.entrySet()) {
        if (TabletsSection.TabletColumnFamily.OLD_PREV_ROW_COLUMN.hasColumns(entry.getKey())) {
            oldPrevEndRow = entry.getValue();
        }
    }

    if (oldPrevEndRow != null) {
        SortedMap<Text, SortedMap<ColumnFQ, Value>> tabletEntries;
        tabletEntries = MetadataTableUtil.getTabletEntries(tabletsKeyValues, columnsToFetch);

        KeyExtent fke;
        try {
            fke = MasterMetadataUtil.fixSplit(context, metadataEntry, tabletEntries.get(metadataEntry),
                    instance, lock);
        } catch (IOException e) {
            log.error("Error fixing split " + metadataEntry);
            throw new AccumuloException(e.toString());
        }

        if (!fke.equals(extent)) {
            return new Pair<Text, KeyExtent>(null, fke);
        }

        // reread and reverify metadata entries now that metadata entries were fixed
        tabletsKeyValues.clear();
        return verifyTabletInformation(context, fke, instance, tabletsKeyValues, clientAddress, lock);
    }

    return new Pair<Text, KeyExtent>(new Text(dir.get()), null);
}

From source file:org.apache.accumulo.server.tabletserver.Tablet.java

private Map<FileRef, Long> findChopFiles(SortedMap<FileRef, DataFileValue> files,
        Map<FileRef, Pair<Key, Key>> falks) {

    Map<FileRef, Long> result = new HashMap<FileRef, Long>();

    for (Entry<FileRef, DataFileValue> entry : files.entrySet()) {
        FileRef file = entry.getKey();//  w ww .  j  a  v a2  s .c  o  m

        Pair<Key, Key> pair = falks.get(file);
        if (pair == null) {
            // file was created or imported after we obtained the first an last keys... there
            // are a few options here... throw an exception which will cause the compaction to
            // retry and also cause ugly error message that the admin has to ignore... could
            // go get the first and last key, but this code is called while the tablet lock
            // is held... or just compact the file....
            result.put(file, entry.getValue().getSize());
        } else {
            Key first = pair.getFirst();
            Key last = pair.getSecond();
            // If first and last are null, it's an empty file. Add it to the compact set so it goes away.
            if ((first == null && last == null) || !this.extent.contains(first.getRow())
                    || !this.extent.contains(last.getRow())) {
                result.put(file, entry.getValue().getSize());
            }
        }
    }
    return result;

}

From source file:org.apache.hadoop.hbase.util.HBaseFsck.java

/**
 * Generate set of puts to add to new meta.  This expects the tables to be
 * clean with no overlaps or holes.  If there are any problems it returns null.
 *
 * @return An array list of puts to do in bulk, null if tables have problems
 *//*  w  w  w  .  j  a v  a2  s.  c o m*/
private ArrayList<Put> generatePuts(SortedMap<TableName, TableInfo> tablesInfo) throws IOException {
    ArrayList<Put> puts = new ArrayList<Put>();
    boolean hasProblems = false;
    for (Entry<TableName, TableInfo> e : tablesInfo.entrySet()) {
        TableName name = e.getKey();

        // skip "hbase:meta"
        if (name.compareTo(TableName.META_TABLE_NAME) == 0) {
            continue;
        }

        TableInfo ti = e.getValue();
        for (Entry<byte[], Collection<HbckInfo>> spl : ti.sc.getStarts().asMap().entrySet()) {
            Collection<HbckInfo> his = spl.getValue();
            int sz = his.size();
            if (sz != 1) {
                // problem
                LOG.error("Split starting at " + Bytes.toStringBinary(spl.getKey()) + " had " + sz
                        + " regions instead of exactly 1.");
                hasProblems = true;
                continue;
            }

            // add the row directly to meta.
            HbckInfo hi = his.iterator().next();
            HRegionInfo hri = hi.getHdfsHRI(); // hi.metaEntry;
            Put p = MetaEditor.makePutFromRegionInfo(hri);
            puts.add(p);
        }
    }
    return hasProblems ? null : puts;
}