Search in sources :

Example 11 with LongSet

use of com.carrotsearch.hppc.LongSet in project janusgraph by JanusGraph.

the class VertexIDAssignerTest method testCustomIdAssignment.

private void testCustomIdAssignment(CustomIdStrategy idStrategy) {
    LongSet vertexIds = new LongHashSet();
    final long maxCount = idAssigner.getIDManager().getVertexCountBound();
    long count = 1;
    for (int trial = 0; trial < 10; trial++) {
        final JanusGraph graph = getInMemoryGraph(true, true);
        int numVertices = 1000;
        final List<JanusGraphVertex> vertices = new ArrayList<>(numVertices);
        try {
            for (int i = 0; i < numVertices; i++, count++) {
                final long userVertexId;
                switch(idStrategy) {
                    case LOW:
                        userVertexId = count;
                        break;
                    case HIGH:
                        userVertexId = maxCount - count;
                        break;
                    default:
                        throw new RuntimeException("Unsupported custom id strategy: " + idStrategy);
                }
                final long id = idAssigner.getIDManager().toVertexId(userVertexId);
                JanusGraphVertex next = graph.addVertex(T.id, id, "user_id", userVertexId);
                vertices.add(next);
            }
            // Verify that ids are set, unique and consistent with user id basis
            for (JanusGraphVertex v : vertices) {
                assertTrue(v.hasId());
                long id = v.longId();
                assertTrue(id > 0 && id < Long.MAX_VALUE);
                assertTrue(vertexIds.add(id));
                assertEquals((long) v.value("user_id"), idAssigner.getIDManager().fromVertexId(id));
            }
        } finally {
            graph.tx().rollback();
            graph.close();
        }
    }
}
Also used : LongHashSet(com.carrotsearch.hppc.LongHashSet) LongSet(com.carrotsearch.hppc.LongSet) JanusGraph(org.janusgraph.core.JanusGraph) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) ArrayList(java.util.ArrayList)

Example 12 with LongSet

use of com.carrotsearch.hppc.LongSet in project janusgraph by JanusGraph.

the class EdgeSerializer method writeRelation.

public StaticArrayEntry writeRelation(InternalRelation relation, InternalRelationType type, int position, TypeInspector tx) {
    assert type == relation.getType() || (type.getBaseType() != null && type.getBaseType().equals(relation.getType()));
    Direction dir = EdgeDirection.fromPosition(position);
    Preconditions.checkArgument(type.isUnidirected(Direction.BOTH) || type.isUnidirected(dir));
    long typeId = type.longId();
    DirectionID dirID = getDirID(dir, relation.isProperty() ? RelationCategory.PROPERTY : RelationCategory.EDGE);
    DataOutput out = serializer.getDataOutput(DEFAULT_CAPACITY);
    int valuePosition;
    IDHandler.writeRelationType(out, typeId, dirID, type.isInvisibleType());
    Multiplicity multiplicity = type.multiplicity();
    long[] sortKey = type.getSortKey();
    assert !multiplicity.isConstrained() || sortKey.length == 0 : type.name();
    int keyStartPos = out.getPosition();
    if (!multiplicity.isConstrained()) {
        writeInlineTypes(sortKey, relation, out, tx, InlineType.KEY);
    }
    int keyEndPos = out.getPosition();
    long relationId = relation.longId();
    // How multiplicity is handled for edges and properties is slightly different
    if (relation.isEdge()) {
        long otherVertexId = relation.getVertex((position + 1) % 2).longId();
        if (multiplicity.isConstrained()) {
            if (multiplicity.isUnique(dir)) {
                valuePosition = out.getPosition();
                VariableLong.writePositive(out, otherVertexId);
            } else {
                VariableLong.writePositiveBackward(out, otherVertexId);
                valuePosition = out.getPosition();
            }
            VariableLong.writePositive(out, relationId);
        } else {
            VariableLong.writePositiveBackward(out, otherVertexId);
            VariableLong.writePositiveBackward(out, relationId);
            valuePosition = out.getPosition();
        }
    } else {
        assert relation.isProperty();
        Preconditions.checkArgument(relation.isProperty());
        Object value = ((JanusGraphVertexProperty) relation).value();
        Preconditions.checkNotNull(value);
        PropertyKey key = (PropertyKey) type;
        assert key.dataType().isInstance(value);
        if (multiplicity.isConstrained()) {
            if (multiplicity.isUnique(dir)) {
                // Cardinality=SINGLE
                valuePosition = out.getPosition();
                writePropertyValue(out, key, value);
            } else {
                // Cardinality=SET
                writePropertyValue(out, key, value);
                valuePosition = out.getPosition();
            }
            VariableLong.writePositive(out, relationId);
        } else {
            assert multiplicity.getCardinality() == Cardinality.LIST;
            VariableLong.writePositiveBackward(out, relationId);
            valuePosition = out.getPosition();
            writePropertyValue(out, key, value);
        }
    }
    // Write signature
    long[] signature = type.getSignature();
    writeInlineTypes(signature, relation, out, tx, InlineType.SIGNATURE);
    // Write remaining properties
    LongSet writtenTypes = new LongHashSet(sortKey.length + signature.length);
    if (sortKey.length > 0 || signature.length > 0) {
        for (long id : sortKey) writtenTypes.add(id);
        for (long id : signature) writtenTypes.add(id);
    }
    LongArrayList remainingTypes = new LongArrayList(8);
    for (PropertyKey t : relation.getPropertyKeysDirect()) {
        if (!(t instanceof ImplicitKey) && !writtenTypes.contains(t.longId())) {
            remainingTypes.add(t.longId());
        }
    }
    // Sort types before writing to ensure that value is always written the same way
    long[] remaining = remainingTypes.toArray();
    Arrays.sort(remaining);
    for (long tid : remaining) {
        PropertyKey t = tx.getExistingPropertyKey(tid);
        writeInline(out, t, relation.getValueDirect(t), InlineType.NORMAL);
    }
    assert valuePosition > 0;
    return new StaticArrayEntry(type.getSortOrder() == Order.DESC ? out.getStaticBufferFlipBytes(keyStartPos, keyEndPos) : out.getStaticBuffer(), valuePosition);
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) LongArrayList(com.carrotsearch.hppc.LongArrayList) LongSet(com.carrotsearch.hppc.LongSet) EdgeDirection(org.janusgraph.graphdb.relations.EdgeDirection) Direction(org.apache.tinkerpop.gremlin.structure.Direction) StaticArrayEntry(org.janusgraph.diskstorage.util.StaticArrayEntry) LongHashSet(com.carrotsearch.hppc.LongHashSet) ImplicitKey(org.janusgraph.graphdb.types.system.ImplicitKey) DirectionID(org.janusgraph.graphdb.database.idhandling.IDHandler.DirectionID)

Example 13 with LongSet

use of com.carrotsearch.hppc.LongSet in project elasticsearch by elastic.

the class MinDocCountIT method setupSuiteScopeCluster.

@Override
public void setupSuiteScopeCluster() throws Exception {
    assertAcked(client().admin().indices().prepareCreate("idx").addMapping("type", "s", "type=keyword").get());
    cardinality = randomIntBetween(8, 30);
    final List<IndexRequestBuilder> indexRequests = new ArrayList<>();
    final Set<String> stringTerms = new HashSet<>();
    final LongSet longTerms = new LongHashSet();
    for (int i = 0; i < cardinality; ++i) {
        String stringTerm;
        do {
            stringTerm = RandomStrings.randomAsciiOfLength(random(), 8);
        } while (!stringTerms.add(stringTerm));
        long longTerm;
        do {
            longTerm = randomInt(cardinality * 2);
        } while (!longTerms.add(longTerm));
        double doubleTerm = longTerm * Math.PI;
        String dateTerm = DateTimeFormat.forPattern("yyyy-MM-dd").print(new DateTime(2014, 1, ((int) longTerm % 20) + 1, 0, 0, DateTimeZone.UTC));
        final int frequency = randomBoolean() ? 1 : randomIntBetween(2, 20);
        for (int j = 0; j < frequency; ++j) {
            indexRequests.add(client().prepareIndex("idx", "type").setSource(jsonBuilder().startObject().field("s", stringTerm).field("l", longTerm).field("d", doubleTerm).field("date", dateTerm).field("match", randomBoolean()).endObject()));
        }
    }
    cardinality = stringTerms.size();
    indexRandom(true, indexRequests);
    ensureSearchable();
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) LongHashSet(com.carrotsearch.hppc.LongHashSet) LongSet(com.carrotsearch.hppc.LongSet) ArrayList(java.util.ArrayList) DateTime(org.joda.time.DateTime) HashSet(java.util.HashSet) LongHashSet(com.carrotsearch.hppc.LongHashSet)

Example 14 with LongSet

use of com.carrotsearch.hppc.LongSet in project cassandra by apache.

the class DynamicTokenTreeBuilder method add.

public void add(Long token, long keyPosition) {
    LongSet found = tokens.get(token);
    if (found == null)
        tokens.put(token, (found = new LongOpenHashSet(2)));
    found.add(keyPosition);
}
Also used : LongSet(com.carrotsearch.hppc.LongSet) LongOpenHashSet(com.carrotsearch.hppc.LongOpenHashSet)

Example 15 with LongSet

use of com.carrotsearch.hppc.LongSet in project cassandra by apache.

the class TokenTreeTest method buildSerializeAndGet.

public void buildSerializeAndGet(boolean isStatic) throws Exception {
    final long tokMin = 0;
    final long tokMax = 1000;
    final TokenTree tokenTree = generateTree(tokMin, tokMax, isStatic);
    for (long i = 0; i <= tokMax; i++) {
        TokenTree.OnDiskToken result = tokenTree.get(i, KEY_CONVERTER);
        Assert.assertNotNull("failed to find object for token " + i, result);
        LongSet found = result.getOffsets();
        Assert.assertEquals(1, found.size());
        Assert.assertEquals(i, found.toArray()[0]);
    }
    Assert.assertNull("found missing object", tokenTree.get(tokMax + 10, KEY_CONVERTER));
}
Also used : LongSet(com.carrotsearch.hppc.LongSet)

Aggregations

LongSet (com.carrotsearch.hppc.LongSet)21 LongHashSet (com.carrotsearch.hppc.LongHashSet)13 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)4 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)4 ExecutorService (java.util.concurrent.ExecutorService)4 Future (java.util.concurrent.Future)4 LongOpenHashSet (com.carrotsearch.hppc.LongOpenHashSet)3 File (java.io.File)3 ExecutionException (java.util.concurrent.ExecutionException)3 LongArrayList (com.carrotsearch.hppc.LongArrayList)2 ConsistentKeyIDAuthority (com.thinkaurelius.titan.diskstorage.idmanagement.ConsistentKeyIDAuthority)2 IDBlockSizer (com.thinkaurelius.titan.graphdb.database.idassigner.IDBlockSizer)2 Callable (java.util.concurrent.Callable)2 CombinedTerm (org.apache.cassandra.index.sasi.utils.CombinedTerm)2 MappedBuffer (org.apache.cassandra.index.sasi.utils.MappedBuffer)2 RandomAccessReader (org.apache.cassandra.io.util.RandomAccessReader)2 SequentialWriter (org.apache.cassandra.io.util.SequentialWriter)2 Direction (org.apache.tinkerpop.gremlin.structure.Direction)2 JanusGraph (org.janusgraph.core.JanusGraph)2