use of com.carrotsearch.hppc.LongHashSet in project titan by thinkaurelius.
the class IDAuthorityTest method checkBlock.
private void checkBlock(IDBlock block) {
assertTrue(blockSize < 10000);
LongSet ids = new LongHashSet((int) blockSize);
checkBlock(block, ids);
}
use of com.carrotsearch.hppc.LongHashSet in project titan by thinkaurelius.
the class IDAuthorityTest method testManyThreadsOneIDAuthority.
@Test
public void testManyThreadsOneIDAuthority() throws BackendException, InterruptedException, ExecutionException {
ExecutorService es = Executors.newFixedThreadPool(CONCURRENCY);
final IDAuthority targetAuthority = idAuthorities[0];
targetAuthority.setIDBlockSizer(new InnerIDBlockSizer());
final int targetPartition = 0;
final int targetNamespace = 2;
final ConcurrentLinkedQueue<IDBlock> blocks = new ConcurrentLinkedQueue<IDBlock>();
final int blocksPerThread = 40;
Assert.assertTrue(0 < blocksPerThread);
List<Future<Void>> futures = new ArrayList<Future<Void>>(CONCURRENCY);
// Start some concurrent threads getting blocks the same ID authority and same partition in that authority
for (int c = 0; c < CONCURRENCY; c++) {
futures.add(es.submit(new Callable<Void>() {
@Override
public Void call() {
try {
getBlock();
} catch (BackendException e) {
throw new RuntimeException(e);
}
return null;
}
private void getBlock() throws BackendException {
for (int i = 0; i < blocksPerThread; i++) {
IDBlock block = targetAuthority.getIDBlock(targetPartition, targetNamespace, GET_ID_BLOCK_TIMEOUT);
Assert.assertNotNull(block);
blocks.add(block);
}
}
}));
}
for (Future<Void> f : futures) {
try {
f.get();
} catch (ExecutionException e) {
throw e;
}
}
es.shutdownNow();
assertEquals(blocksPerThread * CONCURRENCY, blocks.size());
LongSet ids = new LongHashSet((int) blockSize * blocksPerThread * CONCURRENCY);
for (IDBlock block : blocks) checkBlock(block, ids);
}
use of com.carrotsearch.hppc.LongHashSet in project titan by thinkaurelius.
the class IDAuthorityTest method testMultiIDAcquisition.
@Test
public void testMultiIDAcquisition() throws Throwable {
final int numPartitions = MAX_NUM_PARTITIONS;
final int numAcquisitionsPerThreadPartition = 100;
final IDBlockSizer blockSizer = new InnerIDBlockSizer();
for (int i = 0; i < CONCURRENCY; i++) idAuthorities[i].setIDBlockSizer(blockSizer);
final List<ConcurrentLinkedQueue<IDBlock>> ids = new ArrayList<ConcurrentLinkedQueue<IDBlock>>(numPartitions);
for (int i = 0; i < numPartitions; i++) {
ids.add(new ConcurrentLinkedQueue<IDBlock>());
}
final int maxIterations = numAcquisitionsPerThreadPartition * numPartitions * 2;
final Collection<Future<?>> futures = new ArrayList<Future<?>>(CONCURRENCY);
ExecutorService es = Executors.newFixedThreadPool(CONCURRENCY);
Set<String> uids = new HashSet<String>(CONCURRENCY);
for (int i = 0; i < CONCURRENCY; i++) {
final IDAuthority idAuthority = idAuthorities[i];
final IDStressor stressRunnable = new IDStressor(numAcquisitionsPerThreadPartition, numPartitions, maxIterations, idAuthority, ids);
uids.add(idAuthority.getUniqueID());
futures.add(es.submit(stressRunnable));
}
// If this fails, it's likely to be a bug in the test rather than the
// IDAuthority (the latter is technically possible, just less likely)
assertEquals(CONCURRENCY, uids.size());
for (Future<?> f : futures) {
try {
f.get();
} catch (ExecutionException e) {
throw e.getCause();
}
}
for (int i = 0; i < numPartitions; i++) {
ConcurrentLinkedQueue<IDBlock> list = ids.get(i);
assertEquals(numAcquisitionsPerThreadPartition * CONCURRENCY, list.size());
LongSet idset = new LongHashSet((int) blockSize * list.size());
for (IDBlock block : list) checkBlock(block, idset);
}
es.shutdownNow();
}
use of com.carrotsearch.hppc.LongHashSet in project titan by thinkaurelius.
the class EdgeSerializer method writeRelation.
public StaticArrayEntry writeRelation(InternalRelation relation, InternalRelationType type, int position, TypeInspector tx) {
assert type == relation.getType() || 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 = ((TitanVertexProperty) 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;
StaticArrayEntry entry = new StaticArrayEntry(type.getSortOrder() == Order.DESC ? out.getStaticBufferFlipBytes(keyStartPos, keyEndPos) : out.getStaticBuffer(), valuePosition);
return entry;
}
use of com.carrotsearch.hppc.LongHashSet 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);
}
Aggregations