Skip to content

Commit

Permalink
BREAKING CHANGE(server): support "parent & child" EdgeLabel type (apa…
Browse files Browse the repository at this point in the history
…che#2662)

HugeGraph supports the parent-child edge feature, meaning that an Edgelabel can have a subordinate type. Using the bank transfer graph as an example, transfers may include person-to-person transfers (person-to-person), person-to-company transfers (person-to-company), and company-to-company transfers (company-to-company). These three different types of transfers share a common operation, transfer.

In actual business scenarios, it is often necessary to retrieve all transfer edges with a single query. Currently, competitors can only manually split the transfer edge types, perform multiple queries, and then aggregate the results. With HugeGraph's parent-child edge feature, it is possible to query the corresponding person-to-person transfers, person-to-company transfers, and other sub-edge types, and also conveniently and efficiently retrieve all transfer-related edges at once.

PS: The parent-child edge feature for the cassandra and scylladb backends has been temporarily disabled through the store feature. 

> Code formatting will be done separately in the next PR.

Related to:
- apache#745
- apache#447
  • Loading branch information
VGalaxies authored Oct 9, 2024
1 parent 4274b72 commit f6f3708
Show file tree
Hide file tree
Showing 45 changed files with 2,003 additions and 408 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedDeque;

import com.google.common.base.Preconditions;

import org.apache.hugegraph.util.E;

public class ExtendableIterator<T> extends WrappedIterator<T> {
Expand Down Expand Up @@ -54,6 +56,15 @@ public ExtendableIterator<T> extend(Iterator<T> iter) {
return this;
}

public static <T> ExtendableIterator<T> concat(Iterator<T> lhs, Iterator<T> rhs) {
Preconditions.checkNotNull(lhs);
Preconditions.checkNotNull(rhs);
if (lhs instanceof ExtendableIterator) {
return ((ExtendableIterator<T>) lhs).extend(rhs);
}
return new ExtendableIterator<>(lhs, rhs);
}

@Override
public void close() throws Exception {
for (Iterator<T> iter : this.itors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,15 @@ public static Direction parseDirection(String direction) {

private Id getEdgeId(HugeGraph g, JsonEdge newEdge) {
String sortKeys = "";
Id labelId = g.edgeLabel(newEdge.label).id();
EdgeLabel edgeLabel = g.edgeLabel(newEdge.label);
E.checkArgument(!edgeLabel.edgeLabelType().parent(),
"The label of the created/updated edge is not allowed" +
" to be the parent type");
Id labelId = edgeLabel.id();
Id subLabelId = edgeLabel.id();
if (edgeLabel.edgeLabelType().sub()) {
labelId = edgeLabel.fatherId();
}
List<Id> sortKeyIds = g.edgeLabel(labelId).sortKeys();
if (!sortKeyIds.isEmpty()) {
List<Object> sortKeyValues = new ArrayList<>(sortKeyIds.size());
Expand All @@ -442,7 +450,7 @@ private Id getEdgeId(HugeGraph g, JsonEdge newEdge) {
sortKeys = ConditionQuery.concatValues(sortKeyValues);
}
EdgeId edgeId = new EdgeId(HugeVertex.getIdValue(newEdge.source),
Directions.OUT, labelId, sortKeys,
Directions.OUT, labelId, subLabelId, sortKeys,
HugeVertex.getIdValue(newEdge.target));
if (newEdge.id != null) {
E.checkArgument(edgeId.asString().equals(newEdge.id),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.hugegraph.HugeGraph;
Expand All @@ -32,6 +33,7 @@
import org.apache.hugegraph.define.Checkable;
import org.apache.hugegraph.schema.EdgeLabel;
import org.apache.hugegraph.schema.Userdata;
import org.apache.hugegraph.type.define.EdgeLabelType;
import org.apache.hugegraph.type.define.Frequency;
import org.apache.hugegraph.type.define.GraphMode;
import org.apache.hugegraph.util.E;
Expand Down Expand Up @@ -183,10 +185,16 @@ private static class JsonEdgeLabel implements Checkable {
public long id;
@JsonProperty("name")
public String name;
@JsonProperty("edgelabel_type")
public EdgeLabelType edgeLabelType;
@JsonProperty("parent_label")
public String fatherLabel;
@JsonProperty("source_label")
public String sourceLabel;
@JsonProperty("target_label")
public String targetLabel;
@JsonProperty("links")
public Set<Map<String, String>> links;
@JsonProperty("frequency")
public Frequency frequency;
@JsonProperty("properties")
Expand Down Expand Up @@ -223,12 +231,33 @@ private EdgeLabel.Builder convert2Builder(HugeGraph g) {
g, g.mode());
builder.id(this.id);
}
if (this.edgeLabelType == null) {
this.edgeLabelType = EdgeLabelType.NORMAL;
} else if (this.edgeLabelType.parent()) {
builder.asBase();
} else if (this.edgeLabelType.sub()) {
builder.withBase(this.fatherLabel);
} else {
E.checkArgument(this.edgeLabelType.normal(),
"Please enter a valid edge_label_type value " +
"in [NORMAL, PARENT, SUB]");
}
if (this.sourceLabel != null) {
builder.sourceLabel(this.sourceLabel);
}
if (this.targetLabel != null) {
builder.targetLabel(this.targetLabel);
}
if (this.links != null && !this.links.isEmpty()) {
for (Map<String, String> map : this.links) {
E.checkArgument(map.size() == 1,
"The map size must be 1, due to it is a " +
"pair");
Map.Entry<String, String> entry =
map.entrySet().iterator().next();
builder.link(entry.getKey(), entry.getValue());
}
}
if (this.frequency != null) {
builder.frequency(this.frequency);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

public class CassandraFeatures implements BackendFeatures {

@Override
public boolean supportsFatherAndSubEdgeLabel() {
return false;
}

@Override
public boolean supportsScanToken() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.hugegraph.backend.BackendException;
import org.apache.hugegraph.backend.id.EdgeId;
Expand Down Expand Up @@ -51,6 +53,8 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

import org.apache.hugegraph.util.HashUtil;

public class CassandraTables {

public static final String LABEL_INDEX = "label_index";
Expand Down Expand Up @@ -400,7 +404,9 @@ protected List<HugeKeys> pkColumnName() {

@Override
protected List<HugeKeys> idColumnName() {
return Arrays.asList(EdgeId.KEYS);
return Arrays.stream(EdgeId.KEYS)
.filter(key -> !Objects.equals(key, HugeKeys.SUB_LABEL))
.collect(Collectors.toList());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.tuple.Pair;
import org.apache.hugegraph.auth.AuthManager;
import org.apache.hugegraph.backend.id.Id;
import org.apache.hugegraph.backend.query.Query;
Expand Down Expand Up @@ -300,11 +303,25 @@ default Id[] mapElName2Id(String[] edgeLabels) {
Id[] ids = new Id[edgeLabels.length];
for (int i = 0; i < edgeLabels.length; i++) {
EdgeLabel edgeLabel = this.edgeLabel(edgeLabels[i]);
ids[i] = edgeLabel.id();
if (edgeLabel.hasFather()) {
ids[i] = edgeLabel.fatherId();
} else {
ids[i] = edgeLabel.id();
}
}
return ids;
}

default Set<Pair<String, String>> mapPairId2Name(
Set<Pair<Id, Id>> pairs) {
Set<Pair<String, String>> results = new HashSet<>(pairs.size());
for (Pair<Id, Id> pair : pairs) {
results.add(Pair.of(this.vertexLabel(pair.getLeft()).name(),
this.vertexLabel(pair.getRight()).name()));
}
return results;
}

default Id[] mapVlName2Id(String[] vertexLabels) {
Id[] ids = new Id[vertexLabels.length];
for (int i = 0; i < vertexLabels.length; i++) {
Expand All @@ -314,6 +331,14 @@ default Id[] mapVlName2Id(String[] vertexLabels) {
return ids;
}

default EdgeLabel[] mapElName2El(String[] edgeLabels) {
EdgeLabel[] els = new EdgeLabel[edgeLabels.length];
for (int i = 0; i < edgeLabels.length; i++) {
els[i] = this.edgeLabel(edgeLabels[i]);
}
return els;
}

static void registerTraversalStrategies(Class<?> clazz) {
TraversalStrategies strategies = TraversalStrategies.GlobalCache
.getStrategies(Graph.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@
package org.apache.hugegraph.backend.cache;

import java.util.Iterator;
import java.util.List;
import java.util.function.Function;

import org.apache.hugegraph.HugeGraph;
import org.apache.hugegraph.backend.id.Id;
import org.apache.hugegraph.backend.query.ConditionQuery;
import org.apache.hugegraph.backend.query.ConditionQueryFlatten;
import org.apache.hugegraph.backend.query.Query;
import org.apache.hugegraph.backend.store.BackendEntry;
import org.apache.hugegraph.backend.store.BackendFeatures;
Expand All @@ -28,6 +33,8 @@
import org.apache.hugegraph.backend.store.BackendStoreProvider;
import org.apache.hugegraph.backend.store.SystemSchemaStore;
import org.apache.hugegraph.config.HugeConfig;
import org.apache.hugegraph.iterator.ExtendableIterator;
import org.apache.hugegraph.iterator.MapperIterator;
import org.apache.hugegraph.type.HugeType;
import org.apache.hugegraph.util.StringEncoding;

Expand Down Expand Up @@ -177,6 +184,29 @@ public Iterator<BackendEntry> query(Query query) {
}
}

@Override
public Iterator<Iterator<BackendEntry>> query(Iterator<Query> queries,
Function<Query, Query> queryWriter,
HugeGraph hugeGraph) {
return new MapperIterator<>(queries, query -> {
assert query instanceof ConditionQuery;
List<ConditionQuery> flattenQueryList =
ConditionQueryFlatten.flatten((ConditionQuery) query);

if (flattenQueryList.size() > 1) {
ExtendableIterator<BackendEntry> itExtend
= new ExtendableIterator<>();
flattenQueryList.forEach(cq -> {
Query cQuery = queryWriter.apply(cq);
itExtend.extend(this.query(cQuery));
});
return itExtend;
} else {
return this.query(queryWriter.apply(query));
}
});
}

@Override
public Number queryNumber(Query query) {
return this.store.queryNumber(query);
Expand Down
Loading

0 comments on commit f6f3708

Please sign in to comment.