use of com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx in project titan by thinkaurelius.
the class TitanSchemaVertex method getRelated.
@Override
public Iterable<Entry> getRelated(TypeDefinitionCategory def, Direction dir) {
assert dir == Direction.OUT || dir == Direction.IN;
ListMultimap<TypeDefinitionCategory, Entry> rels = dir == Direction.OUT ? outRelations : inRelations;
if (rels == null) {
ImmutableListMultimap.Builder<TypeDefinitionCategory, Entry> b = ImmutableListMultimap.builder();
Iterable<TitanEdge> edges;
if (isLoaded()) {
StandardTitanTx tx = tx();
edges = (Iterable) RelationConstructor.readRelation(this, tx.getGraph().getSchemaCache().getSchemaRelations(longId(), BaseLabel.SchemaDefinitionEdge, dir), tx);
} else {
edges = query().type(BaseLabel.SchemaDefinitionEdge).direction(dir).edges();
}
for (TitanEdge edge : edges) {
TitanVertex oth = edge.vertex(dir.opposite());
assert oth instanceof TitanSchemaVertex;
TypeDefinitionDescription desc = edge.valueOrNull(BaseKey.SchemaDefinitionDesc);
Object modifier = null;
if (desc.getCategory().hasDataType()) {
assert desc.getModifier() != null && desc.getModifier().getClass().equals(desc.getCategory().getDataType());
modifier = desc.getModifier();
}
b.put(desc.getCategory(), new Entry((TitanSchemaVertex) oth, modifier));
}
rels = b.build();
if (dir == Direction.OUT)
outRelations = rels;
else
inRelations = rels;
}
assert rels != null;
return rels.get(def);
}
use of com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx in project titan by thinkaurelius.
the class TitanLocalQueryOptimizerStrategy method apply.
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
if (!traversal.getGraph().isPresent())
return;
Graph graph = traversal.getGraph().get();
//If this is a compute graph then we can't apply local traversal optimisation at this stage.
StandardTitanGraph titanGraph = graph instanceof StandardTitanTx ? ((StandardTitanTx) graph).getGraph() : (StandardTitanGraph) graph;
final boolean useMultiQuery = traversal.getEngine().isStandard() && titanGraph.getConfiguration().useMultiQuery();
/*
====== VERTEX STEP ======
*/
TraversalHelper.getStepsOfClass(VertexStep.class, traversal).forEach(originalStep -> {
TitanVertexStep vstep = new TitanVertexStep(originalStep);
TraversalHelper.replaceStep(originalStep, vstep, traversal);
if (TitanTraversalUtil.isEdgeReturnStep(vstep)) {
HasStepFolder.foldInHasContainer(vstep, traversal);
}
assert TitanTraversalUtil.isEdgeReturnStep(vstep) || TitanTraversalUtil.isVertexReturnStep(vstep);
Step nextStep = TitanTraversalUtil.getNextNonIdentityStep(vstep);
if (nextStep instanceof RangeGlobalStep) {
int limit = QueryUtil.convertLimit(((RangeGlobalStep) nextStep).getHighRange());
vstep.setLimit(QueryUtil.mergeLimits(limit, vstep.getLimit()));
}
if (useMultiQuery) {
vstep.setUseMultiQuery(true);
}
});
/*
====== PROPERTIES STEP ======
*/
TraversalHelper.getStepsOfClass(PropertiesStep.class, traversal).forEach(originalStep -> {
TitanPropertiesStep vstep = new TitanPropertiesStep(originalStep);
TraversalHelper.replaceStep(originalStep, vstep, traversal);
if (vstep.getReturnType().forProperties()) {
HasStepFolder.foldInHasContainer(vstep, traversal);
}
if (useMultiQuery) {
vstep.setUseMultiQuery(true);
}
});
/*
====== EITHER INSIDE LOCAL ======
*/
TraversalHelper.getStepsOfClass(LocalStep.class, traversal).forEach(localStep -> {
Traversal.Admin localTraversal = ((LocalStep<?, ?>) localStep).getLocalChildren().get(0);
Step localStart = localTraversal.getStartStep();
if (localStart instanceof VertexStep) {
TitanVertexStep vstep = new TitanVertexStep((VertexStep) localStart);
TraversalHelper.replaceStep(localStart, vstep, localTraversal);
if (TitanTraversalUtil.isEdgeReturnStep(vstep)) {
HasStepFolder.foldInHasContainer(vstep, localTraversal);
HasStepFolder.foldInOrder(vstep, localTraversal, traversal, false);
}
HasStepFolder.foldInRange(vstep, localTraversal);
unfoldLocalTraversal(traversal, localStep, localTraversal, vstep, useMultiQuery);
}
if (localStart instanceof PropertiesStep) {
TitanPropertiesStep vstep = new TitanPropertiesStep((PropertiesStep) localStart);
TraversalHelper.replaceStep(localStart, vstep, localTraversal);
if (vstep.getReturnType().forProperties()) {
HasStepFolder.foldInHasContainer(vstep, localTraversal);
HasStepFolder.foldInOrder(vstep, localTraversal, traversal, false);
}
HasStepFolder.foldInRange(vstep, localTraversal);
unfoldLocalTraversal(traversal, localStep, localTraversal, vstep, useMultiQuery);
}
});
}
use of com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx in project titan by thinkaurelius.
the class StandardTitanGraph method closeInternal.
private synchronized void closeInternal() {
if (!isOpen)
return;
Map<TitanTransaction, RuntimeException> txCloseExceptions = new HashMap<>();
try {
//Unregister instance
String uniqueid = null;
try {
uniqueid = config.getUniqueGraphId();
ModifiableConfiguration globalConfig = GraphDatabaseConfiguration.getGlobalSystemConfig(backend);
globalConfig.remove(REGISTRATION_TIME, uniqueid);
} catch (Exception e) {
log.warn("Unable to remove graph instance uniqueid {}", uniqueid, e);
}
/* Assuming a couple of properties about openTransactions:
* 1. no concurrent modifications during graph shutdown
* 2. all contained txs are open
*/
for (StandardTitanTx otx : openTransactions) {
try {
otx.close();
} catch (RuntimeException e) {
// Catch and store these exceptions, but proceed wit the loop
// Any remaining txs on the iterator should get a chance to close before we throw up
log.warn("Unable to close transaction {}", otx, e);
txCloseExceptions.put(otx, e);
}
}
super.close();
IOUtils.closeQuietly(idAssigner);
IOUtils.closeQuietly(backend);
IOUtils.closeQuietly(queryCache);
IOUtils.closeQuietly(serializer);
} finally {
isOpen = false;
}
// Throw an exception if at least one transaction failed to close
if (1 == txCloseExceptions.size()) {
// TP3's test suite requires that this be of type ISE
throw new IllegalStateException("Unable to close transaction", Iterables.getOnlyElement(txCloseExceptions.values()));
} else if (1 < txCloseExceptions.size()) {
throw new IllegalStateException(String.format("Unable to close %s transactions (see warnings in log output for details)", txCloseExceptions.size()));
}
}
use of com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx in project titan by thinkaurelius.
the class CassandraGraphTest method testCustomConfigUsedByTx.
@Test
public void testCustomConfigUsedByTx() {
close();
WriteConfiguration wc = getConfiguration();
wc.set(ConfigElement.getPath(CASSANDRA_READ_CONSISTENCY), "ALL");
wc.set(ConfigElement.getPath(CASSANDRA_WRITE_CONSISTENCY), "ALL");
graph = (StandardTitanGraph) TitanFactory.open(wc);
StandardTitanTx tx = (StandardTitanTx) graph.buildTransaction().customOption(ConfigElement.getPath(CASSANDRA_READ_CONSISTENCY), "ONE").customOption(ConfigElement.getPath(CASSANDRA_WRITE_CONSISTENCY), "TWO").start();
assertEquals("ONE", tx.getTxHandle().getBaseTransactionConfig().getCustomOptions().get(AbstractCassandraStoreManager.CASSANDRA_READ_CONSISTENCY));
assertEquals("TWO", tx.getTxHandle().getBaseTransactionConfig().getCustomOptions().get(AbstractCassandraStoreManager.CASSANDRA_WRITE_CONSISTENCY));
tx.rollback();
}
use of com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx in project titan by thinkaurelius.
the class TitanGraphTest method testTransactionConfiguration.
@Test
public void testTransactionConfiguration() {
// Superficial tests for a few transaction builder methods
// Test read-only transaction
TitanTransaction readOnlyTx = graph.buildTransaction().readOnly().start();
try {
readOnlyTx.addVertex();
readOnlyTx.commit();
fail("Read-only transactions should not be able to add a vertex and commit");
} catch (Throwable t) {
if (readOnlyTx.isOpen())
readOnlyTx.rollback();
}
// Test custom log identifier
String logID = "spam";
StandardTitanTx customLogIDTx = (StandardTitanTx) graph.buildTransaction().logIdentifier(logID).start();
assertEquals(logID, customLogIDTx.getConfiguration().getLogIdentifier());
customLogIDTx.rollback();
// Test timestamp
Instant customTimestamp = Instant.ofEpochMilli(-42L);
StandardTitanTx customTimeTx = (StandardTitanTx) graph.buildTransaction().commitTime(customTimestamp).start();
assertTrue(customTimeTx.getConfiguration().hasCommitTime());
assertEquals(customTimestamp, customTimeTx.getConfiguration().getCommitTime());
customTimeTx.rollback();
}
Aggregations