Search in sources :

Example 6 with CollectionType

use of com.baidu.hugegraph.type.define.CollectionType in project incubator-hugegraph by apache.

the class IdSetTest method testIdSetIterator.

@Test
public void testIdSetIterator() {
    Random random = new Random();
    Set<Long> numbers = new HashSet<>();
    Set<UUID> uuids = new HashSet<>();
    Set<String> strings = new HashSet<>();
    for (CollectionType type : CollectionType.values()) {
        idSet = new IdSet(type);
        for (int i = 1; i < SIZE; i++) {
            long number = random.nextLong();
            numbers.add(number);
            idSet.add(IdGenerator.of(number));
        }
        for (int i = 0; i < SIZE; i++) {
            UUID uuid = UUID.randomUUID();
            uuids.add(uuid);
            idSet.add(IdGenerator.of(uuid));
        }
        for (int i = 0; i < SIZE; i++) {
            String string = RandomStringUtils.randomAlphanumeric(10);
            strings.add(string);
            idSet.add(IdGenerator.of(string));
        }
        Assert.assertEquals(numbers.size() + uuids.size() + strings.size(), idSet.size());
        LongHashSet numberIds = Whitebox.getInternalState(idSet, "numberIds");
        Assert.assertEquals(numbers.size(), numberIds.size());
        Set<Id> nonNumberIds = Whitebox.getInternalState(idSet, "nonNumberIds");
        Assert.assertEquals(uuids.size() + strings.size(), nonNumberIds.size());
        Iterator<Id> iterator = idSet.iterator();
        while (iterator.hasNext()) {
            Id id = iterator.next();
            if (id instanceof IdGenerator.LongId) {
                Assert.assertTrue(numbers.contains(id.asLong()));
            } else if (id instanceof IdGenerator.UuidId) {
                Assert.assertTrue(id.uuid() && uuids.contains(id.asObject()));
            } else {
                Assert.assertTrue(id instanceof IdGenerator.StringId);
                Assert.assertTrue(strings.contains(id.asString()));
            }
        }
        numbers.clear();
        uuids.clear();
        strings.clear();
        idSet.clear();
    }
}
Also used : IdGenerator(com.baidu.hugegraph.backend.id.IdGenerator) LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) Random(java.util.Random) IdSet(com.baidu.hugegraph.util.collection.IdSet) CollectionType(com.baidu.hugegraph.type.define.CollectionType) Id(com.baidu.hugegraph.backend.id.Id) UUID(java.util.UUID) HashSet(java.util.HashSet) LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) Test(org.junit.Test)

Example 7 with CollectionType

use of com.baidu.hugegraph.type.define.CollectionType in project incubator-hugegraph by apache.

the class IdSetTest method testIdSetWithMixedId.

@Test
public void testIdSetWithMixedId() {
    Random random = new Random();
    Set<Long> numbers = new HashSet<>();
    Set<UUID> uuids = new HashSet<>();
    Set<String> strings = new HashSet<>();
    for (CollectionType type : CollectionType.values()) {
        idSet = new IdSet(type);
        for (int i = 1; i < SIZE; i++) {
            long number = random.nextLong();
            numbers.add(number);
            idSet.add(IdGenerator.of(number));
        }
        for (int i = 0; i < SIZE; i++) {
            UUID uuid = UUID.randomUUID();
            uuids.add(uuid);
            idSet.add(IdGenerator.of(uuid));
        }
        for (int i = 0; i < SIZE; i++) {
            String string = RandomStringUtils.randomAlphanumeric(10);
            strings.add(string);
            idSet.add(IdGenerator.of(string));
        }
        Assert.assertEquals(numbers.size() + uuids.size() + strings.size(), idSet.size());
        LongHashSet numberIds = Whitebox.getInternalState(idSet, "numberIds");
        Assert.assertEquals(numbers.size(), numberIds.size());
        Set<Id> nonNumberIds = Whitebox.getInternalState(idSet, "nonNumberIds");
        Assert.assertEquals(uuids.size() + strings.size(), nonNumberIds.size());
        numbers.clear();
        uuids.clear();
        strings.clear();
        idSet.clear();
    }
}
Also used : LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) Random(java.util.Random) IdSet(com.baidu.hugegraph.util.collection.IdSet) CollectionType(com.baidu.hugegraph.type.define.CollectionType) Id(com.baidu.hugegraph.backend.id.Id) UUID(java.util.UUID) HashSet(java.util.HashSet) LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) Test(org.junit.Test)

Example 8 with CollectionType

use of com.baidu.hugegraph.type.define.CollectionType in project incubator-hugegraph by apache.

the class IdSetTest method testIdSetWithOnlyStringId.

@Test
public void testIdSetWithOnlyStringId() {
    Set<String> strings = new HashSet<>();
    for (CollectionType type : CollectionType.values()) {
        idSet = new IdSet(type);
        for (int i = 0; i < SIZE; i++) {
            String string = RandomStringUtils.randomAlphanumeric(10);
            strings.add(string);
            idSet.add(IdGenerator.of(string));
        }
        Assert.assertEquals(strings.size(), idSet.size());
        LongHashSet numberIds = Whitebox.getInternalState(idSet, "numberIds");
        Assert.assertTrue(numberIds.isEmpty());
        Set<Id> nonNumberIds = Whitebox.getInternalState(idSet, "nonNumberIds");
        Assert.assertEquals(strings.size(), nonNumberIds.size());
        strings.clear();
        idSet.clear();
    }
}
Also used : LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) IdSet(com.baidu.hugegraph.util.collection.IdSet) CollectionType(com.baidu.hugegraph.type.define.CollectionType) Id(com.baidu.hugegraph.backend.id.Id) HashSet(java.util.HashSet) LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) Test(org.junit.Test)

Aggregations

CollectionType (com.baidu.hugegraph.type.define.CollectionType)8 Test (org.junit.Test)8 Id (com.baidu.hugegraph.backend.id.Id)7 IdSet (com.baidu.hugegraph.util.collection.IdSet)7 HashSet (java.util.HashSet)7 LongHashSet (org.eclipse.collections.impl.set.mutable.primitive.LongHashSet)7 Random (java.util.Random)5 UUID (java.util.UUID)5 IdGenerator (com.baidu.hugegraph.backend.id.IdGenerator)1 CollectionFactory (com.baidu.hugegraph.util.collection.CollectionFactory)1