use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.
the class NeighborRankTraverser method contributeNewLayer.
private Ranks contributeNewLayer(List<Adjacencies> adjacencies, Ranks lastLayerRanks, int capacity) {
Ranks newLayerRanks = new Ranks(capacity);
for (Adjacencies adjacenciesV : adjacencies) {
Id source = adjacenciesV.source();
long degree = adjacenciesV.degree();
for (Node node : adjacenciesV.nodes()) {
double rank = newLayerRanks.getOrDefault(node.id(), 0.0);
rank += (lastLayerRanks.get(source) * this.alpha / degree);
newLayerRanks.put(node.id(), rank);
}
}
return newLayerRanks;
}
use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.
the class NeighborRankTraverser method neighborRank.
public List<Map<Id, Double>> neighborRank(Id source, List<Step> steps) {
E.checkNotNull(source, "source vertex id");
this.checkVertexExist(source, "source vertex");
E.checkArgument(!steps.isEmpty(), "The steps can't be empty");
MultivaluedMap<Id, Node> sources = newMultivalueMap();
sources.add(source, new Node(source, null));
boolean sameLayerTransfer = true;
long access = 0;
// Results: ranks of each layer
List<Ranks> ranks = newList();
ranks.add(Ranks.of(source, 1.0));
for (Step step : steps) {
Ranks lastLayerRanks = ranks.get(ranks.size() - 1);
Map<Id, Double> sameLayerIncrRanks = newMap();
List<Adjacencies> adjacencies = newList();
MultivaluedMap<Id, Node> newVertices = newMultivalueMap();
// Traversal vertices of previous level
for (Map.Entry<Id, List<Node>> entry : sources.entrySet()) {
Id vertex = entry.getKey();
Iterator<Edge> edges = this.edgesOfVertex(vertex, step.edgeStep);
Adjacencies adjacenciesV = new Adjacencies(vertex);
Set<Id> sameLayerNodesV = newIdSet();
Map<Integer, Set<Id>> prevLayerNodesV = newMap();
while (edges.hasNext()) {
HugeEdge edge = (HugeEdge) edges.next();
Id target = edge.id().otherVertexId();
// Determine whether it belongs to the same layer
if (this.belongToSameLayer(sources.keySet(), target, sameLayerNodesV)) {
continue;
}
/*
* Determine whether it belongs to the previous layers,
* if it belongs to, update the weight, but don't pass
* any more
*/
if (this.belongToPrevLayers(ranks, target, prevLayerNodesV)) {
continue;
}
for (Node n : entry.getValue()) {
// If have loop, skip target
if (n.contains(target)) {
continue;
}
Node newNode = new Node(target, n);
adjacenciesV.add(newNode);
// Add adjacent nodes to sources of next step
newVertices.add(target, newNode);
checkCapacity(this.capacity, ++access, "neighbor rank");
}
}
long degree = sameLayerNodesV.size() + prevLayerNodesV.size() + adjacenciesV.nodes().size();
if (degree == 0L) {
continue;
}
adjacenciesV.degree(degree);
adjacencies.add(adjacenciesV);
double incr = lastLayerRanks.getOrDefault(vertex, 0.0) * this.alpha / degree;
// Merge the increment of the same layer node
this.mergeSameLayerIncrRanks(sameLayerNodesV, incr, sameLayerIncrRanks);
// Adding contributions to the previous layers
this.contributePrevLayers(ranks, incr, prevLayerNodesV);
}
Ranks newLayerRanks;
if (sameLayerTransfer) {
// First contribute to last layer, then pass to the new layer
this.contributeLastLayer(sameLayerIncrRanks, lastLayerRanks);
newLayerRanks = this.contributeNewLayer(adjacencies, lastLayerRanks, step.capacity);
} else {
// First pass to the new layer, then contribute to last layer
newLayerRanks = this.contributeNewLayer(adjacencies, lastLayerRanks, step.capacity);
this.contributeLastLayer(sameLayerIncrRanks, lastLayerRanks);
}
ranks.add(newLayerRanks);
// Re-init sources
sources = newVertices;
}
return this.topRanks(ranks, steps);
}
use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.
the class NeighborRankTraverser method mergeSameLayerIncrRanks.
private void mergeSameLayerIncrRanks(Set<Id> sameLayerNodesV, double incr, Map<Id, Double> sameLayerIncrRanks) {
for (Id node : sameLayerNodesV) {
double oldRank = sameLayerIncrRanks.getOrDefault(node, 0.0);
sameLayerIncrRanks.put(node, oldRank + incr);
}
}
use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.
the class IndexLabelBuilder method createWithTask.
/**
* Create index label with async mode
*/
@Override
public SchemaElement.TaskWithSchema createWithTask() {
HugeType type = HugeType.INDEX_LABEL;
this.checkSchemaName(this.name);
return this.lockCheckAndCreateSchema(type, this.name, name -> {
IndexLabel indexLabel = this.indexLabelOrNull(name);
if (indexLabel != null) {
if (this.checkExist || !hasSameProperties(indexLabel)) {
throw new ExistedException(type, name);
}
return new SchemaElement.TaskWithSchema(indexLabel, IdGenerator.ZERO);
}
this.checkSchemaIdIfRestoringMode(type, this.id);
this.checkBaseType();
this.checkIndexType();
if (VertexLabel.OLAP_VL.name().equals(this.baseValue)) {
return new SchemaElement.TaskWithSchema(this.build(), IdGenerator.ZERO);
}
SchemaLabel schemaLabel = this.loadBaseLabel();
/*
* If new index label is prefix of existed index label, or has
* the same fields, fail to create new index label.
*/
this.checkFields(schemaLabel.properties());
this.checkRepeatIndex(schemaLabel);
Userdata.check(this.userdata, Action.INSERT);
// Async delete index label which is prefix of the new index label
// TODO: use event to replace direct call
Set<Id> removeTasks = this.removeSubIndex(schemaLabel);
indexLabel = this.build();
assert indexLabel.name().equals(name);
/*
* If not rebuild, just create index label and return.
* The actual indexes may be rebuilt later as needed
*/
if (!this.rebuild) {
indexLabel.status(SchemaStatus.CREATED);
this.graph().addIndexLabel(schemaLabel, indexLabel);
return new SchemaElement.TaskWithSchema(indexLabel, IdGenerator.ZERO);
}
// Create index label (just schema)
indexLabel.status(SchemaStatus.CREATING);
this.graph().addIndexLabel(schemaLabel, indexLabel);
try {
// Async rebuild index
Id rebuildTask = this.rebuildIndex(indexLabel, removeTasks);
E.checkNotNull(rebuildTask, "rebuild-index task");
return new SchemaElement.TaskWithSchema(indexLabel, rebuildTask);
} catch (Throwable e) {
this.updateSchemaStatus(indexLabel, SchemaStatus.INVALID);
throw e;
}
});
}
use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.
the class IndexLabelBuilder method removeSubIndex.
private Set<Id> removeSubIndex(SchemaLabel schemaLabel) {
Set<Id> overrideIndexLabelIds = InsertionOrderUtil.newSet();
for (Id id : schemaLabel.indexLabels()) {
IndexLabel old = this.graph().indexLabel(id);
if (!this.hasSubIndex(old)) {
continue;
}
List<String> oldFields = this.graph().mapPkId2Name(old.indexFields());
List<String> newFields = this.indexFields;
/*
* Remove the existed index label if:
* 1. new unique index label is subset of existed unique index label
* or
* 2. existed index label is prefix of new created index label
* (except for unique index)
*/
if (this.indexType.isUnique() && oldFields.containsAll(newFields) || !this.indexType.isUnique() && CollectionUtil.prefixOf(oldFields, newFields)) {
overrideIndexLabelIds.add(id);
}
}
Set<Id> tasks = InsertionOrderUtil.newSet();
for (Id id : overrideIndexLabelIds) {
Id task = this.graph().removeIndexLabel(id);
E.checkNotNull(task, "remove sub index label task");
tasks.add(task);
}
return tasks;
}
Aggregations