use of com.thinkaurelius.titan.graphdb.relations.RelationIdentifier in project titan by thinkaurelius.
the class TitanBlueprintsTransaction method edges.
@Override
public Iterator<Edge> edges(Object... eids) {
if (eids == null || eids.length == 0)
return (Iterator) getEdges().iterator();
ElementUtils.verifyArgsMustBeEitherIdorElement(eids);
RelationIdentifier[] ids = new RelationIdentifier[eids.length];
int pos = 0;
for (int i = 0; i < eids.length; i++) {
RelationIdentifier id = ElementUtils.getEdgeId(eids[i]);
if (id != null)
ids[pos++] = id;
}
if (pos == 0)
return Collections.emptyIterator();
if (pos < ids.length)
ids = Arrays.copyOf(ids, pos);
return (Iterator) getEdges(ids).iterator();
}
use of com.thinkaurelius.titan.graphdb.relations.RelationIdentifier 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);
}
}
use of com.thinkaurelius.titan.graphdb.relations.RelationIdentifier in project titan by thinkaurelius.
the class IndexSerializer method getIndexEntry.
private final Entry getIndexEntry(CompositeIndexType index, RecordEntry[] record, TitanElement element) {
DataOutput out = serializer.getDataOutput(1 + 8 + 8 * record.length + 4 * 8);
out.putByte(FIRST_INDEX_COLUMN_BYTE);
if (index.getCardinality() != Cardinality.SINGLE) {
VariableLong.writePositive(out, element.longId());
if (index.getCardinality() != Cardinality.SET) {
for (RecordEntry re : record) {
VariableLong.writePositive(out, re.relationId);
}
}
}
int valuePosition = out.getPosition();
if (element instanceof TitanVertex) {
VariableLong.writePositive(out, element.longId());
} else {
assert element instanceof TitanRelation;
RelationIdentifier rid = (RelationIdentifier) element.id();
long[] longs = rid.getLongRepresentation();
Preconditions.checkArgument(longs.length == 3 || longs.length == 4);
for (int i = 0; i < longs.length; i++) VariableLong.writePositive(out, longs[i]);
}
return new StaticArrayEntry(out.getStaticBuffer(), valuePosition);
}
use of com.thinkaurelius.titan.graphdb.relations.RelationIdentifier in project titan by thinkaurelius.
the class StandardTitanTx method getEdges.
@Override
public Iterable<TitanEdge> getEdges(RelationIdentifier... ids) {
verifyOpen();
if (ids == null || ids.length == 0)
return new VertexCentricEdgeIterable(getInternalVertices(), RelationCategory.EDGE);
if (null != config.getGroupName()) {
MetricManager.INSTANCE.getCounter(config.getGroupName(), "db", "getEdgesByID").inc();
}
List<TitanEdge> result = new ArrayList<>(ids.length);
for (RelationIdentifier id : ids) {
if (id == null)
continue;
TitanEdge edge = id.findEdge(this);
if (edge != null && !edge.isRemoved())
result.add(edge);
}
return result;
}
use of com.thinkaurelius.titan.graphdb.relations.RelationIdentifier in project titan by thinkaurelius.
the class TitanGraphTest method testImplicitKey.
/**
* Test the correct application of {@link com.thinkaurelius.titan.graphdb.types.system.ImplicitKey}
* to vertices, edges, and properties.
* <p/>
* Additionally tests RelationIdentifier since this is closely related to ADJACENT and TITANID implicit keys.
*/
@Test
public void testImplicitKey() {
TitanVertex v = graph.addVertex("name", "Dan"), u = graph.addVertex();
Edge e = v.addEdge("knows", u);
graph.tx().commit();
RelationIdentifier eid = (RelationIdentifier) e.id();
assertEquals(v.id(), v.value(ID_NAME));
assertEquals(eid, e.value(ID_NAME));
assertEquals("knows", e.value(LABEL_NAME));
assertEquals(BaseVertexLabel.DEFAULT_VERTEXLABEL.name(), v.value(LABEL_NAME));
assertCount(1, v.query().direction(Direction.BOTH).labels("knows").has(ID_NAME, eid).edges());
assertCount(0, v.query().direction(Direction.BOTH).labels("knows").has(ID_NAME, RelationIdentifier.get(new long[] { 4, 5, 6, 7 })).edges());
assertCount(1, v.query().direction(Direction.BOTH).labels("knows").has("~nid", eid.getRelationId()).edges());
assertCount(0, v.query().direction(Direction.BOTH).labels("knows").has("~nid", 110111).edges());
//Test edge retrieval
assertNotNull(getE(graph, eid));
assertEquals(eid, getE(graph, eid).id());
//Test adjacent constraint
assertEquals(1, v.query().direction(BOTH).has("~adjacent", u.id()).edgeCount());
assertCount(1, v.query().direction(BOTH).has("~adjacent", (int) getId(u)).edges());
try {
//Not a valid vertex
assertCount(0, v.query().direction(BOTH).has("~adjacent", 110111).edges());
fail();
} catch (IllegalArgumentException ex) {
}
}
Aggregations