Java Code Examples for com.baidu.hugegraph.util.InsertionOrderUtil#newMap()

The following examples show how to use com.baidu.hugegraph.util.InsertionOrderUtil#newMap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InsertionOrderUtilTest.java    From hugegraph-common with Apache License 2.0 6 votes vote down vote up
@Test
public void testMapCopy() {
    Map<Integer, Integer> map = InsertionOrderUtil.newMap(3);
    map.put(4, 4);
    map.put(2, 2);
    map.put(5, 5);
    map.put(1, 1);
    map.put(3, 3);
    Assert.assertEquals(ImmutableList.of(4, 2, 5, 1, 3),
                        ImmutableList.copyOf(map.keySet()));

    Map<Integer, Integer> map2 = InsertionOrderUtil.newMap(map);
    map2.put(6, 6);
    map2.put(1, 7);
    Assert.assertEquals(ImmutableList.of(4, 2, 5, 1, 3, 6),
                        ImmutableList.copyOf(map2.keySet()));

    Assert.assertEquals(ImmutableList.of(4, 2, 5, 1, 3),
                        ImmutableList.copyOf(map.keySet()));
}
 
Example 2
Source File: HugeTraverser.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
public static <K, V extends Comparable<? super V>> Map<K, V> topN(
                                                             Map<K, V> map,
                                                             boolean sorted,
                                                             long limit) {
    if (sorted) {
        map = CollectionUtil.sortByValue(map, false);
    }
    if (limit == NO_LIMIT || map.size() <= limit) {
        return map;
    }
    Map<K, V> results = InsertionOrderUtil.newMap();
    long count = 0L;
    for (Map.Entry<K, V> entry : map.entrySet()) {
        results.put(entry.getKey(), entry.getValue());
        if (++count >= limit) {
            break;
        }
    }
    return results;
}
 
Example 3
Source File: MetricsAPI.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
@GET
@Timed
@Path("backend")
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed("admin")
public String backend(@Context GraphManager manager) {
    Map<String, Map<String, Object>> results = InsertionOrderUtil.newMap();
    for (String graph : manager.graphs()) {
        HugeGraph g = manager.graph(graph);
        Map<String, Object> metrics = InsertionOrderUtil.newMap();
        metrics.put(BackendMetrics.BACKEND, g.backend());
        try {
            metrics.putAll(g.metadata(null, "metrics"));
        } catch (Throwable e) {
            metrics.put(BackendMetrics.EXCEPTION, e.toString());
            LOG.debug("Failed to get backend metrics", e);
        }
        results.put(graph, metrics);
    }
    return JsonUtil.toJson(results);
}
 
Example 4
Source File: InMemoryDBTables.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@Override
protected Map<Id, BackendEntry> queryByIdPrefix(
                                Id start,
                                boolean inclusiveStart,
                                Id prefix,
                                Map<Id, BackendEntry> entries) {
    // Query edge(in a vertex) by v-id + column-name-prefix
    BackendEntry value = this.getEntryById(start, entries);
    if (value == null) {
        return Collections.emptyMap();
    }

    Map<Id, BackendEntry> rs = InsertionOrderUtil.newMap();

    // TODO: Compatible with BackendEntry
    TextBackendEntry entry = (TextBackendEntry) value;
    // Prefix edges in the vertex
    String startColumn = columnOfEdge(start);
    String prefixColumn = columnOfEdge(prefix);
    BackendEntry edges = new TextBackendEntry(HugeType.VERTEX,
                                              entry.id());
    edges.columns(entry.columnsWithPrefix(startColumn, inclusiveStart,
                                          prefixColumn));

    BackendEntry result = rs.get(entry.id());
    if (result == null) {
        rs.put(entry.id(), edges);
    } else {
        result.merge(edges);
    }

    return rs;
}
 
Example 5
Source File: HbaseMetrics.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
private static Map<String, Object> formatMetrics(
                                   ServerMetrics serverMetrics,
                                   List<RegionMetrics> regions) {
    Map<String, Object> metrics = InsertionOrderUtil.newMap();
    metrics.put("max_heap_size",
                serverMetrics.getMaxHeapSize().get(Size.Unit.MEGABYTE));
    metrics.put("used_heap_size",
                serverMetrics.getUsedHeapSize().get(Size.Unit.MEGABYTE));
    metrics.put("heap_size_unit", "MB");
    metrics.put("request_count", serverMetrics.getRequestCount());
    metrics.put("request_count_per_second",
                serverMetrics.getRequestCountPerSecond());
    metrics.put("regions", formatMetrics(regions));
    return metrics;
}
 
Example 6
Source File: ProfileAPI.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@GET
@Timed
@Produces(MediaType.APPLICATION_JSON)
public String getProfile(@Context Application application) {
    // May init multi times by multi threads, but no effect on the results
    if (SERVER_PROFILES != null) {
        return SERVER_PROFILES;
    }

    Map<String, Object> profiles = InsertionOrderUtil.newMap();
    profiles.put("service", SERVICE);
    profiles.put("version", CoreVersion.VERSION.toString());
    profiles.put("doc", DOC);
    profiles.put("api_doc", API_DOC);
    Set<String> apis = new TreeSet<>();
    for (Class<?> clazz : application.getClasses()) {
        if (!isAnnotatedPathClass(clazz)) {
            continue;
        }
        Resource resource = Resource.from(clazz);
        APICategory apiCategory = APICategory.parse(resource.getName());
        apis.add(apiCategory.dir);
    }
    profiles.put("apis", apis);
    SERVER_PROFILES = JsonUtil.toJson(profiles);
    return SERVER_PROFILES;
}
 
Example 7
Source File: BackendMutation.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
public void put(HugeType type, Id id, BackendAction mutation) {
    Map<Id, List<BackendAction>> table = this.mutations.get(type);
    if (table == null) {
        table = InsertionOrderUtil.newMap();
        this.mutations.put(type, table);
    }

    List<BackendAction> items = table.get(id);
    if (items == null) {
        items = new ArrayList<>();
        table.put(id, items);
    }

    items.add(mutation);
}
 
Example 8
Source File: RocksDBStore.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
protected Map<String, RocksDBSessions> tableDBMapping() {
    Map<String, RocksDBSessions> tableDBMap = InsertionOrderUtil.newMap();
    for (Entry<HugeType, String> e : this.tableDiskMapping.entrySet()) {
        String table = this.table(e.getKey()).table();
        RocksDBSessions db = db(e.getValue());
        tableDBMap.put(table, db);
    }
    return tableDBMap;
}
 
Example 9
Source File: InMemoryDBTables.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@Override
protected Map<Id, BackendEntry> queryByIdRange(
                                Id start,
                                boolean inclusiveStart,
                                Id end,
                                boolean inclusiveEnd,
                                Map<Id, BackendEntry> entries) {
    BackendEntry value = this.getEntryById(start, entries);
    if (value == null) {
        return Collections.emptyMap();
    }

    Map<Id, BackendEntry> rs = InsertionOrderUtil.newMap();

    // TODO: Compatible with BackendEntry
    TextBackendEntry entry = (TextBackendEntry) value;
    // Range edges in the vertex
    String startColumn = columnOfEdge(start);
    String endColumn = columnOfEdge(end);
    BackendEntry edges = new TextBackendEntry(HugeType.VERTEX,
                                              entry.id());
    edges.columns(entry.columnsWithRange(startColumn, inclusiveStart,
                                         endColumn, inclusiveEnd));

    BackendEntry result = rs.get(entry.id());
    if (result == null) {
        rs.put(entry.id(), edges);
    } else {
        result.merge(edges);
    }

    return rs;
}
 
Example 10
Source File: InsertionOrderUtilTest.java    From hugegraph-common with Apache License 2.0 5 votes vote down vote up
@Test
public void testMap() {
    Map<Integer, Integer> map = InsertionOrderUtil.newMap();
    map.put(4, 4);
    map.put(2, 2);
    map.put(5, 5);
    map.put(1, 1);
    map.put(3, 3);

    Assert.assertEquals(ImmutableList.of(4, 2, 5, 1, 3),
                        ImmutableList.copyOf(map.keySet()));
}
 
Example 11
Source File: CassandraMetrics.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, Object> getMetrics() {
    Map<String, Object> results = InsertionOrderUtil.newMap();
    Set<Host> hosts = this.cluster.getMetadata().getAllHosts();
    results.put(NODES, hosts.size());
    for (Host host : hosts) {
        String address = host.getAddress().getHostAddress();
        results.put(address, this.getMetricsByHost(address));
    }
    return results;
}
 
Example 12
Source File: CassandraMetrics.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
private Map<String, Object> getMetricsByHost(String host) {
    Map<String, Object> metrics = InsertionOrderUtil.newMap();
    // JMX client operations for Cassandra.
    try (NodeProbe probe = this.newNodeProbe(host)) {
        MemoryUsage heapUsage = probe.getHeapMemoryUsage();
        metrics.put(MEM_USED, heapUsage.getUsed() / Bytes.MB);
        metrics.put(MEM_COMMITED, heapUsage.getCommitted() / Bytes.MB);
        metrics.put(MEM_MAX, heapUsage.getMax() / Bytes.MB);
        metrics.put(MEM_UNIT, "MB");
        metrics.put(DATA_SIZE, probe.getLoadString());
    } catch (Throwable e) {
        metrics.put(EXCEPTION, e.toString());
    }
    return metrics;
}
 
Example 13
Source File: Line.java    From hugegraph-loader with Apache License 2.0 5 votes vote down vote up
public Line(String rawLine, String[] names, Object[] values) {
    E.checkArgumentNotNull(rawLine, "The rawLine can't be null");
    E.checkArgumentNotNull(names, "The names can't be null");
    E.checkArgumentNotNull(values, "The values can't be null");
    E.checkArgument(names.length == values.length,
                    "The length of names %s should be same as values %s");
    this.rawLine = rawLine;
    this.names = names;
    this.values = values;
    this.keyValues = InsertionOrderUtil.newMap();
    for (int i = 0; i < this.names.length; i++) {
        this.keyValues.put(this.names[i], this.values[i]);
    }
}
 
Example 14
Source File: GraphTransaction.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@Override
protected void reset() {
    super.reset();

    // Clear mutation
    if (this.addedVertices == null || !this.addedVertices.isEmpty()) {
        this.addedVertices = InsertionOrderUtil.newMap();
    }
    if (this.removedVertices == null || !this.removedVertices.isEmpty()) {
        this.removedVertices = InsertionOrderUtil.newMap();
    }
    if (this.updatedVertices == null || !this.updatedVertices.isEmpty()) {
        this.updatedVertices = InsertionOrderUtil.newMap();
    }

    if (this.addedEdges == null || !this.addedEdges.isEmpty()) {
        this.addedEdges = InsertionOrderUtil.newMap();
    }
    if (this.removedEdges == null || !this.removedEdges.isEmpty()) {
        this.removedEdges = InsertionOrderUtil.newMap();
    }
    if (this.updatedEdges == null || !this.updatedEdges.isEmpty()) {
        this.updatedEdges = InsertionOrderUtil.newMap();
    }

    if (this.addedProps == null || !this.addedProps.isEmpty()) {
        this.addedProps = InsertionOrderUtil.newSet();
    }
    if (this.removedProps == null || !this.removedProps.isEmpty()) {
        this.removedProps = InsertionOrderUtil.newSet();
    }
    if (this.updatedOldestProps == null ||
        !this.updatedOldestProps.isEmpty()) {
        this.updatedOldestProps = InsertionOrderUtil.newSet();
    }
}
 
Example 15
Source File: InsertionOrderUtilTest.java    From hugegraph-common with Apache License 2.0 5 votes vote down vote up
@Test
public void testMapWithInitialCapacity() {
    Map<Integer, Integer> map = InsertionOrderUtil.newMap(3);
    map.put(4, 4);
    map.put(2, 2);
    map.put(5, 5);
    map.put(1, 1);
    map.put(3, 3);

    Assert.assertEquals(ImmutableList.of(4, 2, 5, 1, 3),
                        ImmutableList.copyOf(map.keySet()));
}
 
Example 16
Source File: InMemoryDBTables.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
private Iterator<BackendEntry> betweenQuery(Id indexLabelId,
                                            Object keyMax,
                                            boolean keyMaxEq,
                                            Object keyMin,
                                            boolean keyMinEq,
                                            HugeType type) {
    NavigableMap<Id, BackendEntry> rs = this.store();

    E.checkArgument(keyMin != null || keyMax != null,
                    "Please specify at least one condition");
    if (keyMin == null) {
        // Field value < keyMax
        keyMin = NumericUtil.minValueOf(keyMax.getClass());
    }
    Id min = HugeIndex.formatIndexId(type, indexLabelId, keyMin);

    if (keyMax == null) {
        // Field value > keyMin
        keyMaxEq = false;
        indexLabelId = IdGenerator.of(indexLabelId.asLong() + 1L);
        keyMax = NumericUtil.minValueOf(keyMin.getClass());
    }
    Id max = HugeIndex.formatIndexId(type, indexLabelId, keyMax);

    max = keyMaxEq ? rs.floorKey(max) : rs.lowerKey(max);
    if (max == null) {
        return QueryResults.emptyIterator();
    }

    Map<Id, BackendEntry> results = InsertionOrderUtil.newMap();
    Map.Entry<Id, BackendEntry> entry = keyMinEq ?
                                        rs.ceilingEntry(min) :
                                        rs.higherEntry(min);
    while (entry != null) {
        if (entry.getKey().compareTo(max) > 0) {
            break;
        }
        results.put(entry.getKey(), entry.getValue());
        entry = rs.higherEntry(entry.getKey());
    }
    return results.values().iterator();
}
 
Example 17
Source File: TableDefine.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
public TableDefine() {
    this.columns = InsertionOrderUtil.newMap();
    this.keys = InsertionOrderUtil.newList();
    this.typesMapping = ImmutableMap.of();
}
 
Example 18
Source File: TableDefine.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
public TableDefine(Map<String, String> typesMapping) {
    this.columns = InsertionOrderUtil.newMap();
    this.keys = InsertionOrderUtil.newList();
    this.typesMapping = typesMapping;
}
 
Example 19
Source File: GraphIndexTransaction.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
private long processSecondaryOrSearchIndexLeft(ConditionQuery query,
                                               HugeElement element) {
    Map<PropertyKey, Object> incorrectPKs = InsertionOrderUtil.newMap();
    HugeElement deletion = this.constructErrorElem(query, element,
                                                   incorrectPKs);
    if (deletion == null) {
        return 0;
    }

    // Delete unused index
    long count = 0;
    Set<Id> incorrectPkIds;
    for (IndexLabel il : relatedIndexLabels(deletion)) {
        incorrectPkIds = incorrectPKs.keySet().stream()
                                     .map(PropertyKey::id)
                                     .collect(Collectors.toSet());
        Collection<Id> incorrectIndexFields = CollectionUtil.intersect(
                                              il.indexFields(),
                                              incorrectPkIds);
        if (incorrectIndexFields.isEmpty()) {
            continue;
        }
        // Skip if search index is not wrong
        if (il.indexType().isSearch()) {
            Id field = il.indexField();
            String cond = deletion.<String>getPropertyValue(field);
            String actual = element.<String>getPropertyValue(field);
            if (this.tx.matchSearchIndexWords(actual, cond)) {
                /*
                 * If query by two search index, one is correct but
                 * the other is wrong, we should not delete the correct
                 */
                continue;
            }
        }
        // Delete index with error property
        this.tx.updateIndex(il.id(), deletion, true);
        // Rebuild index if delete correct index part
        if (il.indexType().isSecondary()) {
            /*
             * When it's a composite secondary index,
             * if the suffix property is wrong and the prefix property
             * is correct, the correct prefix part will be deleted,
             * so rebuild the index again with the correct property.
             */
            this.tx.updateIndex(il.id(), element, false);
        }
        this.tx.commit();
        if (this.deletedByError(element, incorrectIndexFields,
                                incorrectPKs)) {
            this.tx.updateIndex(il.id(), deletion, false);
            this.tx.commit();
        } else {
            count++;
        }
    }
    return count;
}
 
Example 20
Source File: SubCommands.java    From hugegraph-tools with Apache License 2.0 4 votes vote down vote up
public SubCommands() {
    this.commands = InsertionOrderUtil.newMap();
    this.initSubCommands();
}