use of com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx in project titan by thinkaurelius.
the class VertexListTest method testLists.
@Test
public void testLists() {
int num = 13;
TitanGraph g = TitanFactory.open("inmemory");
StandardTitanTx tx = (StandardTitanTx) g.newTransaction();
VertexLongList vll = new VertexLongList(tx);
VertexArrayList val = new VertexArrayList(tx);
for (int i = 0; i < num; i++) {
TitanVertex v = tx.addVertex();
vll.add(v);
val.add(v);
}
assertEquals(num, Iterables.size(vll));
assertEquals(num, Iterables.size(val));
vll.sort();
val.sort();
assertTrue(vll.isSorted());
assertTrue(val.isSorted());
for (Iterable<TitanVertex> iterable : new Iterable[] { val, vll }) {
Iterator<TitanVertex> iter = iterable.iterator();
TitanVertex previous = null;
for (int i = 0; i < num; i++) {
TitanVertex next = iter.next();
if (previous != null)
assertTrue(previous.longId() < next.longId());
previous = next;
}
try {
iter.next();
fail();
} catch (NoSuchElementException ex) {
}
}
tx.commit();
g.close();
}
use of com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx in project titan by thinkaurelius.
the class TitanSchemaVertex method getDefinition.
@Override
public TypeDefinitionMap getDefinition() {
TypeDefinitionMap def = definition;
if (def == null) {
def = new TypeDefinitionMap();
Iterable<TitanVertexProperty> ps;
if (isLoaded()) {
StandardTitanTx tx = tx();
ps = (Iterable) RelationConstructor.readRelation(this, tx.getGraph().getSchemaCache().getSchemaRelations(longId(), BaseKey.SchemaDefinitionProperty, Direction.OUT), tx);
} else {
ps = query().type(BaseKey.SchemaDefinitionProperty).properties();
}
for (TitanVertexProperty property : ps) {
TypeDefinitionDescription desc = property.valueOrNull(BaseKey.SchemaDefinitionDesc);
Preconditions.checkArgument(desc != null && desc.getCategory().isProperty());
def.setValue(desc.getCategory(), property.value());
}
assert def.size() > 0;
definition = def;
}
assert def != null;
return def;
}
use of com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx in project titan by thinkaurelius.
the class TitanSchemaVertex method name.
@Override
public String name() {
if (name == null) {
TitanVertexProperty<String> p;
if (isLoaded()) {
StandardTitanTx tx = tx();
p = (TitanVertexProperty) Iterables.getOnlyElement(RelationConstructor.readRelation(this, tx.getGraph().getSchemaCache().getSchemaRelations(longId(), BaseKey.SchemaName, Direction.OUT), tx), null);
} else {
p = Iterables.getOnlyElement(query().type(BaseKey.SchemaName).properties(), null);
}
Preconditions.checkState(p != null, "Could not find type for id: %s", longId());
name = p.value();
}
assert name != null;
return TitanSchemaCategory.getName(name);
}
use of com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx in project titan by thinkaurelius.
the class TitanH1OutputFormat method getRecordWriter.
@Override
public RecordWriter<NullWritable, VertexWritable> getRecordWriter(TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException {
synchronized (this) {
if (null == graph) {
Configuration hadoopConf = taskAttemptContext.getConfiguration();
ModifiableHadoopConfiguration mhc = ModifiableHadoopConfiguration.of(TitanHadoopConfiguration.MAPRED_NS, hadoopConf);
graph = (StandardTitanGraph) TitanFactory.open(mhc.getTitanGraphConf());
}
}
// returned by VertexProgram.getComputeKeys()
if (null == persistableKeys) {
try {
persistableKeys = VertexProgram.createVertexProgram(graph, ConfUtil.makeApacheConfiguration(taskAttemptContext.getConfiguration())).getElementComputeKeys();
log.debug("Set persistableKeys={}", Joiner.on(",").join(persistableKeys));
} catch (Exception e) {
log.debug("Unable to detect or instantiate vertex program", e);
persistableKeys = ImmutableSet.of();
}
}
StandardTitanTx tx = transactions.computeIfAbsent(taskAttemptContext.getTaskAttemptID(), id -> (StandardTitanTx) graph.newTransaction());
return new TitanH1RecordWriter(taskAttemptContext, tx, persistableKeys);
}
use of com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx in project titan by thinkaurelius.
the class StandardTransactionLogProcessor method fixSecondaryFailure.
private void fixSecondaryFailure(final StandardTransactionId txId, final TxEntry entry) {
logRecoveryMsg("Attempting to repair partially failed transaction [%s]", txId);
if (entry.entry == null) {
logRecoveryMsg("Trying to repair expired or unpersisted transaction [%s] (Ignore in startup)", txId);
return;
}
boolean userLogFailure = true;
boolean secIndexFailure = true;
final Predicate<String> isFailedIndex;
final TransactionLogHeader.Entry commitEntry = entry.entry;
final TransactionLogHeader.SecondaryFailures secFail = entry.failures;
if (secFail != null) {
userLogFailure = secFail.userLogFailure;
secIndexFailure = !secFail.failedIndexes.isEmpty();
isFailedIndex = new Predicate<String>() {
@Override
public boolean apply(@Nullable String s) {
return secFail.failedIndexes.contains(s);
}
};
} else {
isFailedIndex = Predicates.alwaysTrue();
}
// I) Restore external indexes
if (secIndexFailure) {
//1) Collect all elements (vertices and relations) and the indexes for which they need to be restored
final SetMultimap<String, IndexRestore> indexRestores = HashMultimap.create();
BackendOperation.execute(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
StandardTitanTx tx = (StandardTitanTx) graph.newTransaction();
try {
for (TransactionLogHeader.Modification modification : commitEntry.getContentAsModifications(serializer)) {
InternalRelation rel = ModificationDeserializer.parseRelation(modification, tx);
//Collect affected vertex indexes
for (MixedIndexType index : getMixedIndexes(rel.getType())) {
if (index.getElement() == ElementCategory.VERTEX && isFailedIndex.apply(index.getBackingIndexName())) {
assert rel.isProperty();
indexRestores.put(index.getBackingIndexName(), new IndexRestore(rel.getVertex(0).longId(), ElementCategory.VERTEX, getIndexId(index)));
}
}
//See if relation itself is affected
for (RelationType relType : rel.getPropertyKeysDirect()) {
for (MixedIndexType index : getMixedIndexes(relType)) {
if (index.getElement().isInstance(rel) && isFailedIndex.apply(index.getBackingIndexName())) {
assert rel.id() instanceof RelationIdentifier;
indexRestores.put(index.getBackingIndexName(), new IndexRestore(rel.id(), ElementCategory.getByClazz(rel.getClass()), getIndexId(index)));
}
}
}
}
} finally {
if (tx.isOpen())
tx.rollback();
}
return true;
}
}, readTime);
//2) Restore elements per backing index
for (final String indexName : indexRestores.keySet()) {
final StandardTitanTx tx = (StandardTitanTx) graph.newTransaction();
try {
BackendTransaction btx = tx.getTxHandle();
final IndexTransaction indexTx = btx.getIndexTransaction(indexName);
BackendOperation.execute(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
Map<String, Map<String, List<IndexEntry>>> restoredDocs = Maps.newHashMap();
for (IndexRestore restore : indexRestores.get(indexName)) {
TitanSchemaVertex indexV = (TitanSchemaVertex) tx.getVertex(restore.indexId);
MixedIndexType index = (MixedIndexType) indexV.asIndexType();
TitanElement element = restore.retrieve(tx);
if (element != null) {
graph.getIndexSerializer().reindexElement(element, index, restoredDocs);
} else {
//Element is deleted
graph.getIndexSerializer().removeElement(restore.elementId, index, restoredDocs);
}
}
indexTx.restore(restoredDocs);
indexTx.commit();
return true;
}
@Override
public String toString() {
return "IndexMutation";
}
}, persistenceTime);
} finally {
if (tx.isOpen())
tx.rollback();
}
}
}
// II) Restore log messages
final String logTxIdentifier = (String) commitEntry.getMetadata().get(LogTxMeta.LOG_ID);
if (userLogFailure && logTxIdentifier != null) {
TransactionLogHeader txHeader = new TransactionLogHeader(txCounter.incrementAndGet(), times.getTime(), times);
final StaticBuffer userLogContent = txHeader.serializeUserLog(serializer, commitEntry, txId);
BackendOperation.execute(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
final Log userLog = graph.getBackend().getUserLog(logTxIdentifier);
Future<Message> env = userLog.add(userLogContent);
if (env.isDone()) {
env.get();
}
return true;
}
}, persistenceTime);
}
}
Aggregations