use of com.baidu.hugegraph.structure.HugeElement in project incubator-hugegraph by apache.
the class DeleteExpiredIndexJob method deleteExpiredIndex.
/*
* Delete expired element(if exist) of the index,
* otherwise just delete expired index only
*/
private void deleteExpiredIndex(HugeGraphParams graph, HugeIndex index) {
GraphTransaction tx = graph.graphTransaction();
HugeType type = index.indexLabel().queryType().isVertex() ? HugeType.VERTEX : HugeType.EDGE;
IdQuery query = new IdQuery(type);
query.query(index.elementId());
query.showExpired(true);
Iterator<?> elements = type.isVertex() ? tx.queryVertices(query) : tx.queryEdges(query);
if (elements.hasNext()) {
HugeElement element = (HugeElement) elements.next();
if (element.expiredTime() == index.expiredTime()) {
element.remove();
} else {
tx.removeIndex(index);
}
} else {
tx.removeIndex(index);
}
}
use of com.baidu.hugegraph.structure.HugeElement in project incubator-hugegraph by apache.
the class HugeGraphAuthProxy method verifyElemPermission.
private <V extends Element> V verifyElemPermission(HugePermission actionPerm, boolean throwIfNoPerm, Supplier<V> elementFetcher) {
return verifyResPermission(actionPerm, throwIfNoPerm, () -> {
String graph = this.hugegraph.name();
HugeElement elem = (HugeElement) elementFetcher.get();
@SuppressWarnings("unchecked") ResourceObject<V> r = (ResourceObject<V>) ResourceObject.of(graph, elem);
return r;
});
}
use of com.baidu.hugegraph.structure.HugeElement in project incubator-hugegraph by apache.
the class GraphTransaction method traverseByLabel.
private <T> void traverseByLabel(SchemaLabel label, Function<Query, Iterator<T>> fetcher, Consumer<T> consumer, boolean deleting) {
HugeType type = label.type() == HugeType.VERTEX_LABEL ? HugeType.VERTEX : HugeType.EDGE;
Query query = label.enableLabelIndex() ? new ConditionQuery(type) : new Query(type);
query.capacity(Query.NO_CAPACITY);
query.limit(Query.NO_LIMIT);
if (this.store().features().supportsQueryByPage()) {
query.page(PageInfo.PAGE_NONE);
}
if (label.hidden()) {
query.showHidden(true);
}
query.showDeleting(deleting);
query.showExpired(deleting);
if (label.enableLabelIndex()) {
// Support label index, query by label index by paging
assert query instanceof ConditionQuery;
((ConditionQuery) query).eq(HugeKeys.LABEL, label.id());
Iterator<T> iter = fetcher.apply(query);
try {
// Fetch by paging automatically
while (iter.hasNext()) {
consumer.accept(iter.next());
/*
* Commit per batch to avoid too much data in single commit,
* especially for Cassandra backend
*/
this.commitIfGtSize(GraphTransaction.COMMIT_BATCH);
}
// Commit changes if exists
this.commit();
} finally {
CloseableIterator.closeIterator(iter);
}
} else {
// Not support label index, query all and filter by label
if (query.paging()) {
query.limit(this.pageSize);
}
String page = null;
do {
Iterator<T> iter = fetcher.apply(query);
try {
while (iter.hasNext()) {
T e = iter.next();
SchemaLabel elemLabel = ((HugeElement) e).schemaLabel();
if (label.equals(elemLabel)) {
consumer.accept(e);
/*
* Commit per batch to avoid too much data in single
* commit, especially for Cassandra backend
*/
this.commitIfGtSize(GraphTransaction.COMMIT_BATCH);
}
}
// Commit changes of every page before next page query
this.commit();
if (query.paging()) {
page = PageInfo.pageState(iter).toString();
query.page(page);
}
} finally {
CloseableIterator.closeIterator(iter);
}
} while (page != null);
}
}
use of com.baidu.hugegraph.structure.HugeElement in project incubator-hugegraph by apache.
the class DeleteExpiredElementJob method execute.
@Override
public V execute() throws Exception {
LOG.debug("Delete expired elements: {}", this.elements);
HugeGraphParams graph = this.params();
GraphTransaction tx = graph.graphTransaction();
try {
for (HugeElement element : this.elements) {
element.remove();
}
tx.commit();
} catch (Exception e) {
tx.rollback();
LOG.warn("Failed to delete expired elements: {}", this.elements);
throw e;
} finally {
JOB_COUNTERS.jobCounter(graph.graph()).decrement();
}
return null;
}
use of com.baidu.hugegraph.structure.HugeElement in project incubator-hugegraph by apache.
the class IndexLabelRebuildJob method rebuildIndex.
private void rebuildIndex(SchemaLabel label, Collection<Id> indexLabelIds) {
SchemaTransaction schemaTx = this.params().schemaTransaction();
GraphTransaction graphTx = this.params().graphTransaction();
Consumer<?> indexUpdater = (elem) -> {
for (Id id : indexLabelIds) {
graphTx.updateIndex(id, (HugeElement) elem, false);
}
};
LockUtil.Locks locks = new LockUtil.Locks(schemaTx.graphName());
try {
locks.lockWrites(LockUtil.INDEX_LABEL_REBUILD, indexLabelIds);
Set<IndexLabel> ils = indexLabelIds.stream().map(this.graph()::indexLabel).collect(Collectors.toSet());
for (IndexLabel il : ils) {
if (il.status() == SchemaStatus.CREATING) {
continue;
}
schemaTx.updateSchemaStatus(il, SchemaStatus.REBUILDING);
}
this.removeIndex(indexLabelIds);
/*
* Note: Here must commit index transaction firstly.
* Because remove index convert to (id like <?>:personByCity):
* `delete from index table where label = ?`,
* But append index will convert to (id like Beijing:personByCity):
* `update index element_ids += xxx where field_value = ?
* and index_label_name = ?`,
* They have different id lead to it can't compare and optimize
*/
graphTx.commit();
try {
if (label.type() == HugeType.VERTEX_LABEL) {
@SuppressWarnings("unchecked") Consumer<Vertex> consumer = (Consumer<Vertex>) indexUpdater;
graphTx.traverseVerticesByLabel((VertexLabel) label, consumer, false);
} else {
assert label.type() == HugeType.EDGE_LABEL;
@SuppressWarnings("unchecked") Consumer<Edge> consumer = (Consumer<Edge>) indexUpdater;
graphTx.traverseEdgesByLabel((EdgeLabel) label, consumer, false);
}
graphTx.commit();
} catch (Throwable e) {
for (IndexLabel il : ils) {
schemaTx.updateSchemaStatus(il, SchemaStatus.INVALID);
}
throw e;
}
for (IndexLabel il : ils) {
schemaTx.updateSchemaStatus(il, SchemaStatus.CREATED);
}
} finally {
locks.unlock();
}
}
Aggregations