You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by xi...@apache.org on 2024/09/05 05:26:56 UTC

(tinkerpop) branch 3.7-dev updated: Add String IdManager (#2743)

This is an automated email from the ASF dual-hosted git repository.

xiazcy pushed a commit to branch 3.7-dev
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git


The following commit(s) were added to refs/heads/3.7-dev by this push:
     new 0f51caabf7 Add String IdManager (#2743)
0f51caabf7 is described below

commit 0f51caabf7a5434c0972e71a3c656c0c18970586
Author: Daniel Weber <da...@users.noreply.github.com>
AuthorDate: Thu Sep 5 07:26:51 2024 +0200

    Add String IdManager (#2743)
    
    Add an IdManager that creates stringified UUIDs and accepts any non-empty string
---
 CHANGELOG.asciidoc                                 |  1 +
 .../tinkergraph/structure/AbstractTinkerGraph.java | 28 ++++++++++++++++++++++
 .../tinkergraph/structure/IdManagerTest.java       | 24 +++++++++++++++++++
 3 files changed, 53 insertions(+)

diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc
index 76810fa315..f00204b921 100644
--- a/CHANGELOG.asciidoc
+++ b/CHANGELOG.asciidoc
@@ -37,6 +37,7 @@ This release also includes changes from <<release-3-6-8, 3.6.8>>.
 * Fixed an issue where missing necessary parameters for logging, resulting in '%!x(MISSING)' output in `gremlin-go`.
 * Added getter method to `ConcatStep`, `ConjoinStep`, `SplitGlobalStep` and `SplitLocalStep` for their private fields.
 * Gremlin Server docker containers shutdown gracefully when receiving a SIGTERM.
+* Added DefaultIdManager.STRING for proper string id creation/handling.
 * Allowed specification of an `Operator` as a reducer in `withSideEffect` when parsing with the grammar.
 
 [[release-3-7-2]]
diff --git a/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/AbstractTinkerGraph.java b/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/AbstractTinkerGraph.java
index 4ce7e1add9..bd37985fc7 100644
--- a/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/AbstractTinkerGraph.java
+++ b/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/AbstractTinkerGraph.java
@@ -563,6 +563,34 @@ public abstract class AbstractTinkerGraph implements Graph {
             public boolean allow(final Object id) {
                 return true;
             }
+        },
+        
+        /**
+         * Manages identifiers of type {@code String}.
+         */
+        STRING {
+            @Override
+            public String getNextId(final AbstractTinkerGraph graph) {
+                return java.util.UUID.randomUUID().toString();
+            }
+
+            @Override
+            public Object convert(final Object id) {
+                if (null == id)
+                    return null;
+                else  if (id instanceof String) {
+                    if (((String)id).isEmpty())
+                        throw new IllegalArgumentException("Expected a non-empty string but received an empty string.");
+
+                    return id;
+                } else
+                    throw new IllegalArgumentException(createErrorMessage(java.lang.String.class, id));
+            }
+
+            @Override
+            public boolean allow(final Object id) {
+                return id instanceof String && !((String)id).isEmpty();
+            }
         };
 
         private static String createErrorMessage(final Class<?> expectedType, final Object id) {
diff --git a/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/IdManagerTest.java b/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/IdManagerTest.java
index e9fe77efdc..296b0d85b0 100644
--- a/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/IdManagerTest.java
+++ b/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/IdManagerTest.java
@@ -78,4 +78,28 @@ public class IdManagerTest {
         final TinkerGraph.IdManager manager = TinkerGraph.DefaultIdManager.UUID;
         manager.convert(Double.NaN);
     }
+
+    @Test
+    public void shouldGenerateNiceErrorOnConversionOfJunkToString() {
+        exceptionRule.expect(IllegalArgumentException.class);
+        exceptionRule.expectMessage("Expected an id that is convertible to");
+        final TinkerGraph.IdManager manager = TinkerGraph.DefaultIdManager.STRING;
+        manager.convert(Double.NaN);
+    }
+
+    @Test
+    public void shouldGenerateNiceErrorOnConversionOfUUIDToString() {
+        exceptionRule.expect(IllegalArgumentException.class);
+        exceptionRule.expectMessage("Expected an id that is convertible to");
+        final TinkerGraph.IdManager manager = TinkerGraph.DefaultIdManager.STRING;
+        manager.convert(UUID.randomUUID());
+    }
+
+    @Test
+    public void shouldGenerateNiceErrorOnConversionOfEmptyStringToString() {
+        exceptionRule.expect(IllegalArgumentException.class);
+        exceptionRule.expectMessage("Expected a non-empty string but received an empty string.");
+        final TinkerGraph.IdManager manager = TinkerGraph.DefaultIdManager.STRING;
+        manager.convert("");
+    }
 }