-
Notifications
You must be signed in to change notification settings - Fork 48
fix: support new HugeGraph edge id format #349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 4 commits
27b727a
91a04d9
252ea40
b84879d
bc84f0a
7fa84bd
5c792eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with this | ||
| * work for additional information regarding copyright ownership. The ASF | ||
| * licenses this file to You under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.hugegraph.computer.core.util; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
|
|
||
| import org.apache.hugegraph.driver.HugeClient; | ||
| import org.apache.hugegraph.driver.HugeClientBuilder; | ||
| import org.apache.hugegraph.rest.RestResult; | ||
| import org.apache.hugegraph.structure.schema.EdgeLabel; | ||
| import org.apache.hugegraph.util.JsonUtil; | ||
|
|
||
| import com.fasterxml.jackson.databind.BeanDescription; | ||
| import com.fasterxml.jackson.databind.DeserializationConfig; | ||
| import com.fasterxml.jackson.databind.deser.BeanDeserializerBuilder; | ||
| import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier; | ||
| import com.fasterxml.jackson.databind.module.SimpleModule; | ||
|
|
||
| public final class HugeClientUtil { | ||
|
|
||
| private static final AtomicBoolean COMPATIBILITY_REGISTERED = | ||
| new AtomicBoolean(false); | ||
|
|
||
| public static HugeClient newHugeClient(String url, String graph, | ||
| String username, String password) { | ||
| registerCompatibilityModule(); | ||
| return new HugeClientBuilder(url, graph).configUser(username, password) | ||
| .build(); | ||
| } | ||
|
|
||
| public static HugeClient newHugeClient(String url, String graph, | ||
| String username, String password, | ||
| int timeout) { | ||
| registerCompatibilityModule(); | ||
| return new HugeClientBuilder(url, graph).configUser(username, password) | ||
| .configTimeout(timeout) | ||
| .build(); | ||
| } | ||
|
|
||
| public static void registerCompatibilityModule() { | ||
| if (!COMPATIBILITY_REGISTERED.compareAndSet(false, true)) { | ||
| return; | ||
| } | ||
| RestResult.registerModule(newCompatibilityModule()); | ||
| JsonUtil.registerModule(newCompatibilityModule()); | ||
| } | ||
|
|
||
| private static SimpleModule newCompatibilityModule() { | ||
| SimpleModule module = new SimpleModule( | ||
| "hugegraph-computer-client-compatibility"); | ||
| module.setDeserializerModifier(new BeanDeserializerModifier() { | ||
|
|
||
| @Override | ||
| public BeanDeserializerBuilder updateBuilder( | ||
| DeserializationConfig config, BeanDescription beanDesc, | ||
| BeanDeserializerBuilder builder) { | ||
| if (EdgeLabel.class.equals(beanDesc.getBeanClass())) { | ||
| builder.addIgnorable("edgelabel_type"); | ||
| builder.addIgnorable("parent_label"); | ||
| builder.addIgnorable("links"); | ||
| } | ||
| return builder; | ||
| } | ||
| }); | ||
| return module; | ||
| } | ||
|
|
||
| private HugeClientUtil() { | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,10 +36,19 @@ | |
| import org.apache.hugegraph.computer.core.graph.value.NullValue; | ||
| import org.apache.hugegraph.computer.core.graph.value.StringValue; | ||
| import org.apache.hugegraph.computer.core.graph.value.Value; | ||
| import org.apache.hugegraph.structure.graph.Edge; | ||
| import org.apache.hugegraph.util.E; | ||
| import org.apache.hugegraph.util.SplicingIdGenerator; | ||
|
|
||
| public final class HugeConverter { | ||
|
|
||
| private static final int LEGACY_EDGE_ID_PARTS = 4; | ||
| private static final int PARENT_EDGE_ID_PARTS = 5; | ||
| private static final int DIRECTIONAL_EDGE_ID_PARTS = 6; | ||
| private static final int LEGACY_EDGE_NAME_INDEX = 2; | ||
| private static final int PARENT_EDGE_NAME_INDEX = 3; | ||
| private static final int DIRECTIONAL_EDGE_NAME_INDEX = 4; | ||
|
|
||
| private static final GraphFactory GRAPH_FACTORY = | ||
| ComputerContext.instance().graphFactory(); | ||
|
|
||
|
|
@@ -96,4 +105,27 @@ public static Properties convertProperties( | |
| } | ||
| return properties; | ||
| } | ||
|
|
||
| public static String convertEdgeName(Edge edge) { | ||
| E.checkArgumentNotNull(edge, "The edge can't be null"); | ||
| String edgeId = edge.id(); | ||
| if (edgeId == null) { | ||
| return edge.name(); | ||
| } | ||
|
|
||
| String[] parts = SplicingIdGenerator.split(edgeId); | ||
| if (parts.length == LEGACY_EDGE_ID_PARTS) { | ||
| return parts[LEGACY_EDGE_NAME_INDEX]; | ||
| } else if (parts.length == PARENT_EDGE_ID_PARTS) { | ||
| return parts[PARENT_EDGE_NAME_INDEX]; | ||
| } else if (parts.length == DIRECTIONAL_EDGE_ID_PARTS && | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❗️ High priority: this still does not parse the server-generated 6-part edge ID format correctly. This branch only accepts
So a valid current server ID like The current java-client invariant is also simpler: for 5/6-part IDs, Please align this parser with that invariant, or at least validate against the server tokens |
||
| isEdgeDirection(parts[1])) { | ||
| return parts[DIRECTIONAL_EDGE_NAME_INDEX]; | ||
| } | ||
| return edge.name(); | ||
| } | ||
|
|
||
| private static boolean isEdgeDirection(String part) { | ||
| return "EDGE_OUT".equals(part) || "EDGE_IN".equals(part); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with this | ||
| * work for additional information regarding copyright ownership. The ASF | ||
| * licenses this file to You under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.hugegraph.computer.core.input.hg; | ||
|
|
||
| import org.apache.hugegraph.computer.core.util.HugeClientUtil; | ||
| import org.apache.hugegraph.rest.RestResult; | ||
| import org.apache.hugegraph.structure.schema.EdgeLabel; | ||
| import org.apache.hugegraph.testutil.Assert; | ||
| import org.junit.Test; | ||
|
|
||
| public class HugeClientCompatibilityTest { | ||
|
|
||
| @Test | ||
| public void testReadEdgeLabelWithCurrentServerFields() { | ||
| HugeClientUtil.registerCompatibilityModule(); | ||
|
|
||
| String content = "{" + | ||
| "\"id\":1," + | ||
| "\"name\":\"link\"," + | ||
| "\"edgelabel_type\":\"NORMAL\"," + | ||
| "\"source_label\":\"user\"," + | ||
| "\"target_label\":\"user\"," + | ||
| "\"links\":[{\"user\":\"user\"}]," + | ||
| "\"frequency\":\"SINGLE\"," + | ||
| "\"sort_keys\":[]," + | ||
| "\"nullable_keys\":[]," + | ||
| "\"index_labels\":[]," + | ||
| "\"properties\":[]," + | ||
| "\"status\":\"CREATED\"," + | ||
| "\"ttl\":0," + | ||
| "\"enable_label_index\":true," + | ||
| "\"user_data\":{\"~create_time\":\"2026-06-22 15:26:42.781\"}" + | ||
| "}"; | ||
|
|
||
| EdgeLabel edgeLabel = new RestResult(200, content, null).readObject( | ||
| EdgeLabel.class); | ||
|
|
||
| Assert.assertEquals("link", edgeLabel.name()); | ||
| Assert.assertEquals("user", edgeLabel.sourceLabel()); | ||
| Assert.assertEquals("user", edgeLabel.targetLabel()); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This compatibility module ignores
edgelabel_type,parent_label, andlinksfor everyEdgeLabeldeserialization. That works for the pinned 1.3 client because those fields are unknown there, but in current java-client 1.7 they are real fields:sourceLabel()/targetLabel()derive fromlinks.A more version-tolerant approach would ignore unknown fields rather than explicitly dropping known current fields, for example with an
@JsonIgnoreProperties(ignoreUnknown = true)mix-in, or by conditionally adding ignorables only when the runtimeEdgeLabelclass does not expose those fields. The tests should prove both sides: old client does not fail on new server fields, and current client semantics do not loselinks.