com.baidu.hugegraph.util.CollectionUtil Java Examples

The following examples show how to use com.baidu.hugegraph.util.CollectionUtil. 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: CollectionUtilTest.java    From hugegraph-common with Apache License 2.0 6 votes vote down vote up
@Test
public void testToSet() {
    Assert.assertThrows(NullPointerException.class, () -> {
        CollectionUtil.toSet(null);
    });

    Object array1 = new Integer[]{1, 2, 3};
    Assert.assertEquals(ImmutableSet.of(1, 2, 3),
                        CollectionUtil.toSet(array1));

    Object array2 = new String[]{"1", "2", "3"};
    Assert.assertEquals(ImmutableSet.of("1", "2", "3"),
                        CollectionUtil.toSet(array2));

    Set<Integer> set = ImmutableSet.of(1, 2, 3);
    Assert.assertEquals(ImmutableSet.of(1, 2, 3),
                        CollectionUtil.toSet(set));

    List<Integer> list = ImmutableList.of(1, 2, 3);
    Assert.assertEquals(ImmutableSet.of(1, 2, 3),
                        CollectionUtil.toSet(list));

    Assert.assertEquals(ImmutableSet.of(1), CollectionUtil.toSet(1));
}
 
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: HugeTraverser.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
public double jaccardSimilarity(Id vertex, Id other, Directions dir,
                                String label, long degree) {
    E.checkNotNull(vertex, "vertex id");
    E.checkNotNull(other, "the other vertex id");
    E.checkNotNull(dir, "direction");
    checkDegree(degree);

    Id labelId = this.getEdgeLabelId(label);

    Set<Id> sourceNeighbors = IteratorUtils.set(this.adjacentVertices(
                              vertex, dir, labelId, degree));
    Set<Id> targetNeighbors = IteratorUtils.set(this.adjacentVertices(
                              other, dir, labelId, degree));
    int interNum = CollectionUtil.intersect(sourceNeighbors,
                                            targetNeighbors).size();
    int unionNum = CollectionUtil.union(sourceNeighbors,
                                        targetNeighbors).size();
    return (double) interNum / unionNum;
}
 
Example #4
Source File: CollectionUtilTest.java    From hugegraph-common with Apache License 2.0 6 votes vote down vote up
@Test
public void testMapSortByStringValue() {
    Map<Integer, String> unordered = new HashMap<>();
    unordered.put(1, "D");
    unordered.put(2, "B");
    unordered.put(3, "E");
    unordered.put(4, "A");
    unordered.put(5, "C");

    Map<Integer, String> incrOrdered = CollectionUtil.sortByValue(unordered,
                                                                  true);
    Assert.assertEquals(ImmutableList.of("A", "B", "C", "D", "E"),
                        ImmutableList.copyOf(incrOrdered.values()));

    Map<Integer, String> decrOrdered = CollectionUtil.sortByValue(unordered,
                                                                  false);
    Assert.assertEquals(ImmutableList.of("E", "D", "C", "B", "A"),
                        ImmutableList.copyOf(decrOrdered.values()));
}
 
Example #5
Source File: CollectionUtilTest.java    From hugegraph-common with Apache License 2.0 6 votes vote down vote up
@Test
public void testMapSortByIntegerValue() {
    Map<String, Integer> unordered = new HashMap<>();
    unordered.put("A", 4);
    unordered.put("B", 2);
    unordered.put("C", 5);
    unordered.put("D", 1);
    unordered.put("E", 3);

    Map<String, Integer> incrOrdered = CollectionUtil.sortByValue(unordered,
                                                                  true);
    Assert.assertEquals(ImmutableList.of(1, 2, 3, 4, 5),
                        ImmutableList.copyOf(incrOrdered.values()));

    Map<String, Integer> decrOrdered = CollectionUtil.sortByValue(unordered,
                                                                  false);
    Assert.assertEquals(ImmutableList.of(5, 4, 3, 2, 1),
                        ImmutableList.copyOf(decrOrdered.values()));
}
 
Example #6
Source File: CollectionUtilTest.java    From hugegraph-common with Apache License 2.0 6 votes vote down vote up
@Test
public void testMapSortByIntegerKey() {
    Map<Integer, String> unordered = new HashMap<>();
    unordered.put(4, "A");
    unordered.put(2, "B");
    unordered.put(5, "C");
    unordered.put(1, "D");
    unordered.put(3, "E");

    Map<Integer, String> incrOrdered = CollectionUtil.sortByKey(unordered,
                                                                true);
    Assert.assertEquals(ImmutableList.of(1, 2, 3, 4, 5),
                        ImmutableList.copyOf(incrOrdered.keySet()));

    Map<Integer, String> decrOrdered = CollectionUtil.sortByKey(unordered,
                                                                false);
    Assert.assertEquals(ImmutableList.of(5, 4, 3, 2, 1),
                        ImmutableList.copyOf(decrOrdered.keySet()));
}
 
Example #7
Source File: CollectionUtilTest.java    From hugegraph-common with Apache License 2.0 6 votes vote down vote up
@Test
public void testMapSortByStringKey() {
    Map<String, Integer> unordered = new HashMap<>();
    unordered.put("D", 1);
    unordered.put("B", 2);
    unordered.put("E", 3);
    unordered.put("A", 4);
    unordered.put("C", 5);

    Map<String, Integer> incrOrdered = CollectionUtil.sortByKey(unordered,
                                                                true);
    Assert.assertEquals(ImmutableList.of("A", "B", "C", "D", "E"),
                        ImmutableList.copyOf(incrOrdered.keySet()));

    Map<String, Integer> decrOrdered = CollectionUtil.sortByKey(unordered,
                                                                false);
    Assert.assertEquals(ImmutableList.of("E", "D", "C", "B", "A"),
                        ImmutableList.copyOf(decrOrdered.keySet()));
}
 
Example #8
Source File: CollectionUtilTest.java    From hugegraph-common with Apache License 2.0 6 votes vote down vote up
@Test
public void testHasIntersectionBetweenSetAndSet() {
    Set<Integer> first = new HashSet<>();
    first.add(1);
    first.add(2);
    first.add(3);

    Set<Integer> second = new HashSet<>();
    Assert.assertFalse(CollectionUtil.hasIntersection(first, second));

    second.add(4);
    Assert.assertFalse(CollectionUtil.hasIntersection(first, second));

    second.add(1);
    Assert.assertTrue(CollectionUtil.hasIntersection(first, second));
}
 
Example #9
Source File: CollectionUtilTest.java    From hugegraph-common with Apache License 2.0 6 votes vote down vote up
@Test
public void testHasIntersectionBetweenListAndSet() {
    List<Integer> first = new ArrayList<>();
    first.add(1);
    first.add(2);
    first.add(3);

    Set<Integer> second = new HashSet<>();
    Assert.assertFalse(CollectionUtil.hasIntersection(first, second));

    second.add(4);
    Assert.assertFalse(CollectionUtil.hasIntersection(first, second));

    second.add(1);
    Assert.assertTrue(CollectionUtil.hasIntersection(first, second));
}
 
Example #10
Source File: CollectionUtilTest.java    From hugegraph-common with Apache License 2.0 6 votes vote down vote up
@Test
public void testIntersectWithoutModifying() {
    List<Integer> first = new ArrayList<>();
    first.add(1);
    first.add(2);
    first.add(3);

    List<Integer> second = new ArrayList<>();

    second.add(4);
    second.add(5);
    Collection<Integer> results = CollectionUtil.intersect(first, second);
    Assert.assertEquals(0, results.size());
    Assert.assertEquals(3, first.size());

    second.add(3);
    results = CollectionUtil.intersect(first, second);
    Assert.assertEquals(1, results.size());
    Assert.assertEquals(3, first.size());

    second.add(1);
    second.add(2);
    results = CollectionUtil.intersect(first, second);
    Assert.assertEquals(3, results.size());
    Assert.assertEquals(3, first.size());
}
 
Example #11
Source File: CollectionUtilTest.java    From hugegraph-common with Apache License 2.0 6 votes vote down vote up
@Test
public void testSubSet() {
    Set<Integer> originSet = ImmutableSet.of(1, 2, 3, 4, 5);

    Set<Integer> subSet = CollectionUtil.subSet(originSet, 1, 1);
    Assert.assertEquals(ImmutableSet.of(), subSet);

    subSet = CollectionUtil.subSet(originSet, 2, 4);
    Assert.assertEquals(ImmutableSet.of(3, 4), subSet);

    subSet = CollectionUtil.subSet(originSet, 2, 5);
    Assert.assertEquals(ImmutableSet.of(3, 4, 5), subSet);

    subSet = CollectionUtil.subSet(originSet, 0, 5);
    Assert.assertEquals(ImmutableSet.of(1, 2, 3, 4, 5), subSet);
}
 
Example #12
Source File: CollectionUtilTest.java    From hugegraph-common with Apache License 2.0 6 votes vote down vote up
@Test
public void testRandomSet() {
    Set<Integer> set = CollectionUtil.randomSet(0, 100, 10);
    for (int i : set) {
        Assert.assertTrue(0 <= i && i < 100);
    }

    // invalid min
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        CollectionUtil.randomSet(200, 100, 10);
    });

    // invalid count = 0
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        CollectionUtil.randomSet(1, 100, 0);
    });

    // invalid count > max - min
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        CollectionUtil.randomSet(1, 100, 100);
    });
}
 
Example #13
Source File: CollectionUtilTest.java    From hugegraph-common with Apache License 2.0 6 votes vote down vote up
@Test
public void testPrefixOf() {
    List<Integer> list = ImmutableList.of(1, 2, 3);

    List<Integer> list1 = ImmutableList.of();
    Assert.assertTrue(CollectionUtil.prefixOf(list1, list));

    List<Integer> list2 = ImmutableList.of(1, 2);
    Assert.assertTrue(CollectionUtil.prefixOf(list2, list));

    List<Integer> list3 = ImmutableList.of(1, 2, 3);
    Assert.assertTrue(CollectionUtil.prefixOf(list3, list));

    List<Integer> list4 = ImmutableList.of(1, 2, 3, 4);
    Assert.assertFalse(CollectionUtil.prefixOf(list4, list));
}
 
Example #14
Source File: CollectionUtilTest.java    From hugegraph-common with Apache License 2.0 6 votes vote down vote up
@Test
public void testToList() {
    Assert.assertThrows(NullPointerException.class, () -> {
        CollectionUtil.toList(null);
    });

    Object array1 = new Integer[]{1, 2, 3};
    Assert.assertEquals(ImmutableList.of(1, 2, 3),
                        CollectionUtil.toList(array1));

    Object array2 = new String[]{"1", "2", "3"};
    Assert.assertEquals(ImmutableList.of("1", "2", "3"),
                        CollectionUtil.toList(array2));

    Set<Integer> set = ImmutableSet.of(1, 2, 3);
    Assert.assertEquals(ImmutableList.of(1, 2, 3),
                        CollectionUtil.toList(set));

    List<Integer> list = ImmutableList.of(1, 2, 3);
    Assert.assertEquals(ImmutableList.of(1, 2, 3),
                        CollectionUtil.toList(list));

    Assert.assertEquals(ImmutableList.of("123"),
                        CollectionUtil.toList("123"));
}
 
Example #15
Source File: AbstractSource.java    From hugegraph-loader with Apache License 2.0 5 votes vote down vote up
@Override
public void check() throws IllegalArgumentException {
    if (this.header != null) {
        E.checkArgument(CollectionUtil.allUnique(this.header),
                        "The header can't contain duplicate columns, " +
                        "but got %s", this.header);
    }
}
 
Example #16
Source File: CollectionUtilTest.java    From hugegraph-common with Apache License 2.0 5 votes vote down vote up
@Test
public void testIntersectWithModifying() {
    Set<Integer> first = new HashSet<>();
    first.add(1);
    first.add(2);
    first.add(3);

    Set<Integer> second = new HashSet<>();
    second.add(1);
    second.add(2);
    second.add(3);

    Collection<Integer> results = CollectionUtil.intersectWithModify(
                                                 first, second);
    Assert.assertEquals(3, results.size());
    Assert.assertEquals(3, first.size());

    // The second set has "1", "2"
    second.remove(3);
    results = CollectionUtil.intersectWithModify(first, second);
    Assert.assertEquals(2, results.size());
    Assert.assertEquals(2, first.size());

    // The second set is empty
    second.remove(1);
    second.remove(2);
    results = CollectionUtil.intersectWithModify(first, second);
    Assert.assertEquals(0, results.size());
    Assert.assertEquals(0, first.size());
}
 
Example #17
Source File: GraphIndexTransaction.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@Watched(prefix = "index")
private IdHolder doJointIndex(IndexQueries queries) {
    if (queries.bigCapacity()) {
        LOG.warn("There is OOM risk if the joint operation is based on a " +
                 "large amount of data, please use single index + filter " +
                 "instead of joint index: {}", queries.rootQuery());
    }
    // All queries are joined with AND
    Set<Id> intersectIds = null;
    for (Map.Entry<IndexLabel, ConditionQuery> e : queries.entrySet()) {
        IndexLabel indexLabel = e.getKey();
        ConditionQuery query = e.getValue();
        if (!query.nolimit()) {
            // Increase limit for intersection
            increaseLimit(query);
        }
        Set<Id> ids = this.doIndexQuery(indexLabel, query).all();
        if (intersectIds == null) {
            intersectIds = ids;
        } else {
            CollectionUtil.intersectWithModify(intersectIds, ids);
        }
        if (intersectIds.isEmpty()) {
            break;
        }
    }
    return new FixedIdHolder(queries.asJointQuery(), intersectIds);
}
 
Example #18
Source File: VertexLabelBuilder.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void checkNullableKeys(Action action) {
    // Not using switch-case to avoid indent too much
    if (action == Action.ELIMINATE) {
        if (!this.nullableKeys.isEmpty()) {
            throw new NotAllowException(
                      "Not support to eliminate nullableKeys " +
                      "for vertex label currently");
        }
        return;
    }

    VertexLabel vertexLabel = this.vertexLabelOrNull(this.name);
    // The originProps is empty when firstly create vertex label
    List<String> originProps = vertexLabel == null ?
                               ImmutableList.of() :
                               this.graph()
                                   .mapPkId2Name(vertexLabel.properties());
    Set<String> appendProps = this.properties;

    E.checkArgument(CollectionUtil.union(originProps, appendProps)
                                  .containsAll(this.nullableKeys),
                    "The nullableKeys: %s to be created or appended " +
                    "must belong to the origin/new properties: %s/%s",
                    this.nullableKeys, originProps, appendProps);

    E.checkArgument(!CollectionUtil.hasIntersection(this.primaryKeys,
                                                    this.nullableKeys),
                    "The nullableKeys: %s are not allowed to " +
                    "belong to primaryKeys: %s of vertex label '%s'",
                    this.nullableKeys, this.primaryKeys, this.name);

    if (action == Action.APPEND) {
        Collection<String> newAddedProps = CollectionUtils.subtract(
                                           appendProps, originProps);
        E.checkArgument(this.nullableKeys.containsAll(newAddedProps),
                        "The new added properties: %s must be nullable",
                        newAddedProps);
    }
}
 
Example #19
Source File: EdgeLabelBuilder.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void checkNullableKeys(Action action) {
    // Not using switch-case to avoid indent too much
    if (action == Action.ELIMINATE) {
        if (!this.nullableKeys.isEmpty()) {
            throw new NotAllowException(
                      "Not support to eliminate nullableKeys " +
                      "for edge label currently");
        }
        return;
    }

    EdgeLabel edgeLabel = this.edgeLabelOrNull(this.name);
    // The originProps is empty when firstly create edge label
    List<String> originProps = edgeLabel == null ?
                               ImmutableList.of() :
                               this.graph()
                                   .mapPkId2Name(edgeLabel.properties());
    Set<String> appendProps = this.properties;

    E.checkArgument(CollectionUtil.union(originProps, appendProps)
                    .containsAll(this.nullableKeys),
                    "The nullableKeys: %s to be created or appended " +
                    "must belong to the origin/new properties: %s/%s ",
                    this.nullableKeys, originProps, appendProps);

    E.checkArgument(!CollectionUtil.hasIntersection(this.sortKeys,
                                                    this.nullableKeys),
                    "The nullableKeys: %s are not allowed to " +
                    "belong to sortKeys: %s of edge label '%s'",
                    this.nullableKeys, this.sortKeys, this.name);

    if (action == Action.APPEND) {
        Collection<String> newAddedProps = CollectionUtils.subtract(
                                           appendProps, originProps);
        E.checkArgument(this.nullableKeys.containsAll(newAddedProps),
                        "The new added properties: %s must be nullable",
                        newAddedProps);
    }
}
 
Example #20
Source File: IndexLabel.java    From hugegraph-client with Apache License 2.0 5 votes vote down vote up
@Override
public Builder by(String... fields) {
    E.checkArgument(this.indexLabel.fields.isEmpty(),
                    "Not allowed to assign index fields multi times");
    List<String> indexFields = Arrays.asList(fields);
    E.checkArgument(CollectionUtil.allUnique(indexFields),
                    "Invalid index fields %s, which contains some " +
                    "duplicate properties", indexFields);
    this.indexLabel.fields.addAll(indexFields);
    return this;
}
 
Example #21
Source File: CollectionUtilTest.java    From hugegraph-common with Apache License 2.0 5 votes vote down vote up
@Test
public void testAllUnique() {
    List<Integer> list = ImmutableList.of();
    Assert.assertTrue(CollectionUtil.allUnique(list));

    list = ImmutableList.of(1, 2, 3, 2, 3);
    Assert.assertFalse(CollectionUtil.allUnique(list));

    list = ImmutableList.of(1, 2, 3, 4, 5);
    Assert.assertTrue(CollectionUtil.allUnique(list));

    list = ImmutableList.of(1, 1, 1, 1, 1);
    Assert.assertFalse(CollectionUtil.allUnique(list));
}
 
Example #22
Source File: CollectionUtilTest.java    From hugegraph-common with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnion() {
    List<Integer> first = new ArrayList<>();
    first.add(1);
    first.add(2);

    Set<Integer> second = new HashSet<>();
    second.add(1);
    second.add(3);

    Set<Integer> results = CollectionUtil.union(first, second);
    Assert.assertEquals(3, results.size());
}
 
Example #23
Source File: IndexLabelBuilder.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
private void checkRepeatIndex(SchemaLabel schemaLabel,
                              IndexType... checkedTypes) {
    this.checkRepeatIndex(schemaLabel, CollectionUtil::prefixOf,
                          checkedTypes);
}
 
Example #24
Source File: GraphIndexTransaction.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
private boolean matchSearchIndexWords(String propValue, String fieldValue) {
    Set<String> propValues = this.segmentWords(propValue);
    Set<String> words = this.segmentWords(fieldValue);
    return CollectionUtil.hasIntersection(propValues, words);
}
 
Example #25
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 #26
Source File: SortByCountIdHolderList.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
@Override
public Set<Id> all() {
    return CollectionUtil.sortByValue(this.ids, false).keySet();
}