use of com.baidu.hugegraph.type.define.Directions in project incubator-hugegraph by apache.
the class BinarySerializer method writeQueryEdgePrefixCondition.
private Query writeQueryEdgePrefixCondition(ConditionQuery cq) {
int count = 0;
BytesBuffer buffer = BytesBuffer.allocate(BytesBuffer.BUF_EDGE_ID);
for (HugeKeys key : EdgeId.KEYS) {
Object value = cq.condition(key);
if (value != null) {
count++;
} else {
if (key == HugeKeys.DIRECTION) {
// Direction is null, set to OUT
value = Directions.OUT;
} else {
break;
}
}
if (key == HugeKeys.OWNER_VERTEX || key == HugeKeys.OTHER_VERTEX) {
writePartitionedId(HugeType.EDGE, (Id) value, buffer);
} else if (key == HugeKeys.DIRECTION) {
byte t = ((Directions) value).type().code();
buffer.write(t);
} else if (key == HugeKeys.LABEL) {
assert value instanceof Id;
buffer.writeId((Id) value);
} else if (key == HugeKeys.SORT_VALUES) {
assert value instanceof String;
buffer.writeStringWithEnding((String) value);
} else {
assert false : key;
}
}
if (count > 0) {
assert count == cq.conditionsSize();
return prefixQuery(cq, new BinaryId(buffer.bytes(), null));
}
return null;
}
use of com.baidu.hugegraph.type.define.Directions in project incubator-hugegraph by apache.
the class PersonalRankTraverser method calcNewRanks.
private Map<Id, Double> calcNewRanks(Set<Id> outSeeds, Set<Id> inSeeds, Id label, Map<Id, Double> ranks) {
Map<Id, Double> newRanks = newMap();
BiFunction<Set<Id>, Directions, Set<Id>> neighborIncrRanks;
neighborIncrRanks = (seeds, dir) -> {
Set<Id> tmpSeeds = newIdSet();
for (Id seed : seeds) {
Double oldRank = ranks.get(seed);
E.checkState(oldRank != null, "Expect rank of seed exists");
Iterator<Id> iter = this.adjacentVertices(seed, dir, label, this.degree);
List<Id> neighbors = IteratorUtils.list(iter);
long degree = neighbors.size();
if (degree == 0L) {
newRanks.put(seed, oldRank);
continue;
}
double incrRank = oldRank * this.alpha / degree;
// Collect all neighbors increment
for (Id neighbor : neighbors) {
tmpSeeds.add(neighbor);
// Assign an initial value when firstly update neighbor rank
double rank = newRanks.getOrDefault(neighbor, 0.0);
newRanks.put(neighbor, rank + incrRank);
}
}
return tmpSeeds;
};
Set<Id> tmpInSeeds = neighborIncrRanks.apply(outSeeds, Directions.OUT);
Set<Id> tmpOutSeeds = neighborIncrRanks.apply(inSeeds, Directions.IN);
outSeeds.addAll(tmpOutSeeds);
inSeeds.addAll(tmpInSeeds);
return newRanks;
}
use of com.baidu.hugegraph.type.define.Directions in project incubator-hugegraph by apache.
the class AllShortestPathsAPI method get.
@GET
@Timed
@Produces(APPLICATION_JSON_WITH_CHARSET)
public String get(@Context GraphManager manager, @PathParam("graph") String graph, @QueryParam("source") String source, @QueryParam("target") String target, @QueryParam("direction") String direction, @QueryParam("label") String edgeLabel, @QueryParam("max_depth") int depth, @QueryParam("max_degree") @DefaultValue(DEFAULT_MAX_DEGREE) long maxDegree, @QueryParam("skip_degree") @DefaultValue("0") long skipDegree, @QueryParam("capacity") @DefaultValue(DEFAULT_CAPACITY) long capacity) {
LOG.debug("Graph [{}] get shortest path from '{}', to '{}' with " + "direction {}, edge label {}, max depth '{}', " + "max degree '{}', skipped degree '{}' and capacity '{}'", graph, source, target, direction, edgeLabel, depth, maxDegree, skipDegree, capacity);
Id sourceId = VertexAPI.checkAndParseVertexId(source);
Id targetId = VertexAPI.checkAndParseVertexId(target);
Directions dir = Directions.convert(EdgeAPI.parseDirection(direction));
HugeGraph g = graph(manager, graph);
ShortestPathTraverser traverser = new ShortestPathTraverser(g);
List<String> edgeLabels = edgeLabel == null ? ImmutableList.of() : ImmutableList.of(edgeLabel);
HugeTraverser.PathSet paths = traverser.allShortestPaths(sourceId, targetId, dir, edgeLabels, depth, maxDegree, skipDegree, capacity);
return manager.serializer(g).writePaths("paths", paths, false);
}
use of com.baidu.hugegraph.type.define.Directions in project incubator-hugegraph by apache.
the class RamTable method query.
private Iterator<HugeEdge> query(ConditionQuery query) {
Id owner = query.condition(HugeKeys.OWNER_VERTEX);
assert owner != null;
Directions dir = query.condition(HugeKeys.DIRECTION);
if (dir == null) {
dir = Directions.BOTH;
}
Id label = query.condition(HugeKeys.LABEL);
if (label == null) {
label = IdGenerator.ZERO;
}
return this.query(owner.asLong(), dir, (int) label.asLong());
}
use of com.baidu.hugegraph.type.define.Directions in project incubator-hugegraph by apache.
the class JaccardSimilarityAPI method get.
@GET
@Timed
@Produces(APPLICATION_JSON_WITH_CHARSET)
public String get(@Context GraphManager manager, @PathParam("graph") String graph, @QueryParam("vertex") String vertex, @QueryParam("other") String other, @QueryParam("direction") String direction, @QueryParam("label") String edgeLabel, @QueryParam("max_degree") @DefaultValue(DEFAULT_MAX_DEGREE) long maxDegree) {
LOG.debug("Graph [{}] get jaccard similarity between '{}' and '{}' " + "with direction {}, edge label {} and max degree '{}'", graph, vertex, other, direction, edgeLabel, maxDegree);
Id sourceId = VertexAPI.checkAndParseVertexId(vertex);
Id targetId = VertexAPI.checkAndParseVertexId(other);
Directions dir = Directions.convert(EdgeAPI.parseDirection(direction));
HugeGraph g = graph(manager, graph);
double similarity;
try (JaccardSimilarTraverser traverser = new JaccardSimilarTraverser(g)) {
similarity = traverser.jaccardSimilarity(sourceId, targetId, dir, edgeLabel, maxDegree);
}
return JsonUtil.toJson(ImmutableMap.of("jaccard_similarity", similarity));
}
Aggregations