Example usage for java.util NavigableMap isEmpty

List of usage examples for java.util NavigableMap isEmpty

Introduction

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

Prototype

boolean isEmpty();

Source Link

Document

Returns true if this map contains no key-value mappings.

Usage

From source file:org.apache.tajo.storage.hbase.HBaseScanner.java

private Datum getDatum(Result result, int fieldId) throws IOException {
    byte[] value = null;
    if (isRowKeyMappings[fieldId]) {
        value = result.getRow();//from   w ww.  jav a  2s .  co m
        if (!isBinaryColumns[fieldId] && rowKeyFieldIndexes[fieldId] >= 0) {
            int rowKeyFieldIndex = rowKeyFieldIndexes[fieldId];

            byte[][] rowKeyFields = BytesUtils.splitPreserveAllTokens(value, rowKeyDelimiter,
                    columnMapping.getNumColumns());

            if (rowKeyFields.length < rowKeyFieldIndex) {
                return NullDatum.get();
            } else {
                value = rowKeyFields[rowKeyFieldIndex];
            }
        }
    } else {
        if (isColumnKeys[fieldId]) {
            NavigableMap<byte[], byte[]> cfMap = result.getFamilyMap(mappingColumnFamilies[fieldId][0]);
            if (cfMap != null) {
                Set<byte[]> keySet = cfMap.keySet();
                if (keySet.size() == 1) {
                    try {
                        return HBaseTextSerializerDeserializer.deserialize(schemaColumns[fieldId],
                                keySet.iterator().next());
                    } catch (Exception e) {
                        LOG.error(e.getMessage(), e);
                        throw new RuntimeException(e.getMessage(), e);
                    }
                } else {
                    StringBuilder sb = new StringBuilder();
                    sb.append("[");
                    int count = 0;
                    for (byte[] eachKey : keySet) {
                        if (count > 0) {
                            sb.append(", ");
                        }
                        Datum datum = HBaseTextSerializerDeserializer.deserialize(schemaColumns[fieldId],
                                eachKey);
                        sb.append("\"").append(datum.asChars()).append("\"");
                        count++;
                        if (count > MAX_LIST_SIZE) {
                            break;
                        }
                    }
                    sb.append("]");
                    return new TextDatum(sb.toString());
                }
            }
        } else if (isColumnValues[fieldId]) {
            NavigableMap<byte[], byte[]> cfMap = result.getFamilyMap(mappingColumnFamilies[fieldId][0]);
            if (cfMap != null) {
                Collection<byte[]> valueList = cfMap.values();
                if (valueList.size() == 1) {
                    try {
                        return HBaseTextSerializerDeserializer.deserialize(schemaColumns[fieldId],
                                valueList.iterator().next());
                    } catch (Exception e) {
                        LOG.error(e.getMessage(), e);
                        throw new RuntimeException(e.getMessage(), e);
                    }
                } else {
                    StringBuilder sb = new StringBuilder();
                    sb.append("[");
                    int count = 0;
                    for (byte[] eachValue : valueList) {
                        if (count > 0) {
                            sb.append(", ");
                        }
                        Datum datum = HBaseTextSerializerDeserializer.deserialize(schemaColumns[fieldId],
                                eachValue);
                        sb.append("\"").append(datum.asChars()).append("\"");
                        count++;
                        if (count > MAX_LIST_SIZE) {
                            break;
                        }
                    }
                    sb.append("]");
                    return new TextDatum(sb.toString());
                }
            }
        } else {
            if (mappingColumnFamilies[fieldId][1] == null) {
                NavigableMap<byte[], byte[]> cfMap = result.getFamilyMap(mappingColumnFamilies[fieldId][0]);
                if (cfMap != null && !cfMap.isEmpty()) {
                    int count = 0;
                    String delim = "";

                    if (cfMap.size() == 0) {
                        return NullDatum.get();
                    } else if (cfMap.size() == 1) {
                        // If a column family is mapped without column name like "cf1:" and the number of cells is one,
                        // return value is flat format not json format.
                        NavigableMap.Entry<byte[], byte[]> entry = cfMap.entrySet().iterator().next();
                        byte[] entryKey = entry.getKey();
                        byte[] entryValue = entry.getValue();
                        if (entryKey == null || entryKey.length == 0) {
                            try {
                                if (isBinaryColumns[fieldId]) {
                                    return HBaseBinarySerializerDeserializer.deserialize(schemaColumns[fieldId],
                                            entryValue);
                                } else {
                                    return HBaseTextSerializerDeserializer.deserialize(schemaColumns[fieldId],
                                            entryValue);
                                }
                            } catch (Exception e) {
                                LOG.error(e.getMessage(), e);
                                throw new RuntimeException(e.getMessage(), e);
                            }
                        }
                    }
                    StringBuilder sb = new StringBuilder();
                    sb.append("{");
                    for (NavigableMap.Entry<byte[], byte[]> entry : cfMap.entrySet()) {
                        byte[] entryKey = entry.getKey();
                        byte[] entryValue = entry.getValue();

                        String keyText = new String(entryKey);
                        String valueText = null;
                        if (entryValue != null) {
                            try {
                                if (isBinaryColumns[fieldId]) {
                                    valueText = HBaseBinarySerializerDeserializer
                                            .deserialize(schemaColumns[fieldId], entryValue).asChars();
                                } else {
                                    valueText = HBaseTextSerializerDeserializer
                                            .deserialize(schemaColumns[fieldId], entryValue).asChars();
                                }
                            } catch (Exception e) {
                                LOG.error(e.getMessage(), e);
                                throw new RuntimeException(e.getMessage(), e);
                            }
                        }
                        sb.append(delim).append("\"").append(keyText).append("\":\"").append(valueText)
                                .append("\"");
                        delim = ", ";
                        count++;
                        if (count > MAX_LIST_SIZE) {
                            break;
                        }
                    } //end of for
                    sb.append("}");
                    return new TextDatum(sb.toString());
                } else {
                    value = null;
                }
            } else {
                value = result.getValue(mappingColumnFamilies[fieldId][0], mappingColumnFamilies[fieldId][1]);
            }
        }
    }

    if (value == null) {
        return NullDatum.get();
    } else {
        try {
            if (isBinaryColumns[fieldId]) {
                return HBaseBinarySerializerDeserializer.deserialize(schemaColumns[fieldId], value);
            } else {
                return HBaseTextSerializerDeserializer.deserialize(schemaColumns[fieldId], value);
            }
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}

From source file:org.kiji.schema.tools.LayoutTool.java

/**
 * Dumps the history of layouts of a given table.
 *
 * @param admin kiji admin interface.//from w w  w  . ja  v a  2s .co  m
 * @throws Exception on error.
 */
private void history(KijiAdmin admin) throws Exception {
    // Gather all of the layouts stored in the metaTable.
    final NavigableMap<Long, KijiTableLayout> timedLayouts = getKiji().getMetaTable()
            .getTimedTableLayoutVersions(mTableName, mMaxVersions);
    if (timedLayouts.isEmpty()) {
        throw new RuntimeException("No such table: " + mTableName);
    }
    for (Map.Entry<Long, KijiTableLayout> entry : timedLayouts.entrySet()) {
        final long timestamp = entry.getKey();
        final KijiTableLayout layout = entry.getValue();
        final String json = ToJson.toJsonString(layout.getDesc());

        if (mWriteTo.isEmpty()) {
            System.out.printf("timestamp: %d:%n%s", timestamp, json);
        } else {
            final String fileName = String.format("%s-%d.json", mWriteTo, timestamp);
            final FileOutputStream fos = new FileOutputStream(fileName);
            try {
                fos.write(Bytes.toBytes(json));
            } finally {
                IOUtils.closeQuietly(fos);
            }
        }
    }
}

From source file:org.opennms.netmgt.rrd.model.RRDv3IT.java

/**
 * Test samples for a single RRA//from  www.j a  va  2 s.  c om
 *
 * @throws Exception the exception
 */
@Test
public void testSamplesSingleRRA() throws Exception {
    File source = new File("src/test/resources/sample-counter.xml");
    RRDv3 rrd = JaxbUtils.unmarshal(RRDv3.class, source);
    Assert.assertNotNull(rrd);
    NavigableMap<Long, List<Double>> samples = rrd.generateSamples(rrd.getRras().get(0));
    Assert.assertFalse(samples.isEmpty());
    long ts = 1441748400L;
    Double v1 = 600.0;
    Double v2 = 2.0;
    Assert.assertEquals(rrd.getRras().get(0).getRows().size(), samples.size());
    for (Map.Entry<Long, List<Double>> s : samples.entrySet()) {
        System.out.println(s);
        Assert.assertEquals(2, s.getValue().size());
        Assert.assertEquals(ts, (long) s.getKey());
        Assert.assertEquals(v1, s.getValue().get(0));
        Assert.assertEquals(v2, s.getValue().get(1));
        ts += 300L;
        v1 += 300.0 * v2;
        v2 += 1.0;
    }
}

From source file:org.opennms.netmgt.rrd.model.RRDv3IT.java

/**
 * Test samples for multiple RRAs (1)/*  w  ww.j av  a2s.  c  o m*/
 *
 * @throws Exception the exception
 */
@Test
public void testSamplesMultipleRRAs1() throws Exception {
    File source = new File("src/test/resources/sample-counter-rras.xml");
    RRDv3 rrd = JaxbUtils.unmarshal(RRDv3.class, source);
    Assert.assertNotNull(rrd);
    NavigableMap<Long, List<Double>> samples = rrd.generateSamples(rrd.getRras().get(1));
    Assert.assertFalse(samples.isEmpty());
    Assert.assertEquals(rrd.getRras().get(1).getRows().size(), samples.size());
}

From source file:org.wisdom.raml.visitor.RamlControllerVisitor.java

/**
 * Navigate through the Controller routes, and create {@link org.raml.model.Resource} from them.
 * If the <code>parent</code> is not null, then the created route will be added has children of the parent, otherwise
 * a new Resource is created and will be added directly to the <code>raml</code> model.
 *
 * @param routes The @{link ControllerRoute}
 * @param parent The parent {@link Resource}
 * @param raml   The {@link Raml} model/*from  www.  j a  v a 2  s.  c o  m*/
 */
private void navigateTheRoutes(NavigableMap<String, Collection<ControllerRouteModel<Raml>>> routes,
        Resource parent, Raml raml) {
    //nothing to see here
    if (routes == null || routes.isEmpty()) {
        return;
    }

    String headUri = routes.firstKey();
    LOGGER.debug("Routes " + routes.toString());
    LOGGER.debug("Parent " + parent);
    Collection<ControllerRouteModel<Raml>> siblings = routes.get(headUri);
    String relativeUri;

    Resource res = new Resource();
    if (parent != null) {
        res.setParentResource(parent);
        res.setParentUri(parent.getUri());
        //Get the relative part of the url
        relativeUri = normalizeActionPath(parent, headUri);
        res.setRelativeUri(relativeUri);
        parent.getResources().put(res.getRelativeUri(), res);
    } else {
        // We don't have a parent, check whether we should create one.
        if (headUri.endsWith("/")) {
            // We have to create a 'fake' parent when we have such kind of url: /foo/
            // We create a parent /foo and a sub-resource /, this is because /foo and /foo/ are different
            // Create a parent - this parent doest not have any action attached.
            String parentUri = normalizeParentPath(headUri);

            // However we do have a tricky case here, if parentURi == "/", we are the parent.
            if (!parentUri.equals("/")) {
                parent = new Resource();
                parent.setParentUri("");
                parent.setRelativeUri(parentUri);
                raml.getResources().put(parentUri, parent);

                // Now manage the current resource, it's uri is necessarily /
                relativeUri = "/";
                res.setParentUri(parent.getUri());
                res.setRelativeUri(relativeUri);
                parent.getResources().put(relativeUri, res);
            } else {
                // We are the root.
                res.setParentUri("");
                relativeUri = normalizeParentPath(headUri);
                res.setRelativeUri(relativeUri);
                raml.getResources().put(res.getRelativeUri(), res);
            }
        } else {
            // No parent
            res.setParentUri("");
            relativeUri = normalizeParentPath(headUri);
            res.setRelativeUri(relativeUri);
            raml.getResources().put(res.getRelativeUri(), res);
        }
    }

    //Add the action from the brother routes
    for (ControllerRouteModel<Raml> bro : siblings) {
        addActionFromRouteElem(bro, res);
    }

    //visit the children route
    NavigableMap<String, Collection<ControllerRouteModel<Raml>>> child = routes.tailMap(headUri, false);

    //no more route element
    if (child.isEmpty()) {
        return;
    }

    final String next = child.firstKey();
    final Resource maybeParent = findParent(next, raml);
    navigateTheRoutes(child, maybeParent, raml);
}