Search in sources :

Example 1 with IdSet

use of com.baidu.hugegraph.util.collection.IdSet in project incubator-hugegraph by apache.

the class IndexLabelBuilder method checkFields.

private void checkFields(Set<Id> propertyIds) {
    List<String> fields = this.indexFields;
    E.checkNotEmpty(fields, "index fields", this.name);
    Set<Id> olapPks = new IdSet(CollectionType.EC);
    for (String field : fields) {
        PropertyKey pkey = this.propertyKeyOrNull(field);
        // In general this will not happen
        E.checkArgument(pkey != null, "Can't build index on undefined property key " + "'%s' for '%s': '%s'", field, this.baseType.readableName(), this.baseValue);
        E.checkArgument(pkey.aggregateType().isIndexable(), "The aggregate type %s is not indexable", pkey.aggregateType());
        if (pkey.cardinality().multiple()) {
            E.checkArgument(fields.size() == 1, "Not allowed to build union index on property" + " key '%s' whose cardinality is multiple", pkey.name());
        }
        if (pkey.olap()) {
            olapPks.add(pkey.id());
        }
    }
    if (!olapPks.isEmpty()) {
        E.checkArgument(olapPks.size() == 1, "Can't build index on multiple olap properties, " + "but got fields '%s' for index label '%s'", fields, this.name);
        E.checkArgument(olapPks.size() == fields.size(), "Can't build index on olap properties and oltp " + "properties in one index label, " + "but got fields '%s' for index label '%s'", fields, this.name);
        E.checkArgument(this.indexType == IndexType.SECONDARY || this.indexType == IndexType.RANGE, "Only secondary and range index can be built on " + "olap property, but got index type '%s' on olap " + "property key '%s' for index label '%s'", this.indexType, fields.get(0), this.name);
    }
    List<String> properties = this.graph().mapPkId2Name(propertyIds);
    E.checkArgument(properties.containsAll(fields), "Not all index fields '%s' are contained in " + "schema properties '%s'", fields, properties);
    // Range index must build on single numeric column
    if (this.indexType == IndexType.RANGE) {
        this.checkFields4Range();
    }
    // Search index must build on single text column
    if (this.indexType.isSearch()) {
        E.checkArgument(fields.size() == 1, "Search index can only build on " + "one field, but got %s fields: '%s'", fields.size(), fields);
        String field = fields.iterator().next();
        DataType dataType = this.graph().propertyKey(field).dataType();
        E.checkArgument(dataType.isText(), "Search index can only build on text property, " + "but got %s(%s)", dataType, field);
    }
}
Also used : IdSet(com.baidu.hugegraph.util.collection.IdSet) DataType(com.baidu.hugegraph.type.define.DataType) Id(com.baidu.hugegraph.backend.id.Id) PropertyKey(com.baidu.hugegraph.schema.PropertyKey)

Example 2 with IdSet

use of com.baidu.hugegraph.util.collection.IdSet in project incubator-hugegraph by apache.

the class IdSetTest method testIdSetRemove.

@Test
public void testIdSetRemove() {
    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());
        for (long number : numbers) {
            idSet.remove(IdGenerator.of(number));
        }
        Assert.assertEquals(nonNumberIds.size(), idSet.size());
        for (UUID uuid : uuids) {
            idSet.remove(IdGenerator.of(uuid));
        }
        for (String string : strings) {
            idSet.remove(IdGenerator.of(string));
        }
        Assert.assertTrue(idSet.isEmpty());
        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 3 with IdSet

use of com.baidu.hugegraph.util.collection.IdSet in project incubator-hugegraph by apache.

the class AbstractTransaction method injectOlapPkIfNeeded.

private void injectOlapPkIfNeeded(Query query) {
    if (!query.resultType().isVertex() || !this.graph.readMode().showOlap()) {
        return;
    }
    /*
         * Control olap access by auth, only accessible olap property key
         * will be queried
         */
    Set<Id> olapPks = new IdSet(CollectionType.EC);
    for (PropertyKey propertyKey : this.graph.graph().propertyKeys()) {
        if (propertyKey.olap()) {
            olapPks.add(propertyKey.id());
        }
    }
    query.olapPks(olapPks);
}
Also used : IdSet(com.baidu.hugegraph.util.collection.IdSet) Id(com.baidu.hugegraph.backend.id.Id) PropertyKey(com.baidu.hugegraph.schema.PropertyKey)

Example 4 with IdSet

use of com.baidu.hugegraph.util.collection.IdSet in project incubator-hugegraph by apache.

the class CollectionFactoryTest method testIdSet.

@Test
public void testIdSet() {
    factory = new CollectionFactory(CollectionType.EC);
    IdSet idSet = factory.newIdSet();
    Set<Id> ids = Whitebox.getInternalState(idSet, "nonNumberIds");
    Assert.assertInstanceOf(UnifiedSet.class, ids);
    Assert.assertInstanceOf(LongHashSet.class, Whitebox.getInternalState(idSet, "numberIds"));
    factory = new CollectionFactory(CollectionType.FU);
    idSet = factory.newIdSet();
    ids = Whitebox.getInternalState(idSet, "nonNumberIds");
    Assert.assertInstanceOf(ObjectOpenHashSet.class, ids);
    Assert.assertInstanceOf(LongHashSet.class, Whitebox.getInternalState(idSet, "numberIds"));
    factory = new CollectionFactory(CollectionType.JCF);
    idSet = factory.newIdSet();
    ids = Whitebox.getInternalState(idSet, "nonNumberIds");
    Assert.assertInstanceOf(HashSet.class, ids);
    Assert.assertInstanceOf(LongHashSet.class, Whitebox.getInternalState(idSet, "numberIds"));
    idSet = CollectionFactory.newIdSet(CollectionType.EC);
    ids = Whitebox.getInternalState(idSet, "nonNumberIds");
    Assert.assertInstanceOf(UnifiedSet.class, ids);
    Assert.assertInstanceOf(LongHashSet.class, Whitebox.getInternalState(idSet, "numberIds"));
    idSet = CollectionFactory.newIdSet(CollectionType.FU);
    ids = Whitebox.getInternalState(idSet, "nonNumberIds");
    Assert.assertInstanceOf(ObjectOpenHashSet.class, ids);
    Assert.assertInstanceOf(LongHashSet.class, Whitebox.getInternalState(idSet, "numberIds"));
    idSet = CollectionFactory.newIdSet(CollectionType.JCF);
    ids = Whitebox.getInternalState(idSet, "nonNumberIds");
    Assert.assertInstanceOf(HashSet.class, ids);
    Assert.assertInstanceOf(LongHashSet.class, Whitebox.getInternalState(idSet, "numberIds"));
}
Also used : CollectionFactory(com.baidu.hugegraph.util.collection.CollectionFactory) IdSet(com.baidu.hugegraph.util.collection.IdSet) Id(com.baidu.hugegraph.backend.id.Id) Test(org.junit.Test)

Example 5 with IdSet

use of com.baidu.hugegraph.util.collection.IdSet in project incubator-hugegraph by apache.

the class IdSetTest method testIdSetWithOnlyNumberId.

@Test
public void testIdSetWithOnlyNumberId() {
    Random random = new Random();
    Set<Long> numbers = 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));
        }
        Assert.assertEquals(numbers.size(), idSet.size());
        LongHashSet numberIds = Whitebox.getInternalState(idSet, "numberIds");
        Assert.assertEquals(numbers.size(), numberIds.size());
        Set<Id> nonNumberIds = Whitebox.getInternalState(idSet, "nonNumberIds");
        Assert.assertTrue(nonNumberIds.isEmpty());
        numbers.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) HashSet(java.util.HashSet) LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) Test(org.junit.Test)

Aggregations

Id (com.baidu.hugegraph.backend.id.Id)10 IdSet (com.baidu.hugegraph.util.collection.IdSet)10 Test (org.junit.Test)8 CollectionType (com.baidu.hugegraph.type.define.CollectionType)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 PropertyKey (com.baidu.hugegraph.schema.PropertyKey)2 IdGenerator (com.baidu.hugegraph.backend.id.IdGenerator)1 DataType (com.baidu.hugegraph.type.define.DataType)1 CollectionFactory (com.baidu.hugegraph.util.collection.CollectionFactory)1