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();
}
}
}
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);
}
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();
}
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);
}
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));
}
Aggregations