Copilot commented on code in PR #2982:
URL: https://github.com/apache/hugegraph/pull/2982#discussion_r3142093002


##########
hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/rocksdb/RocksDBTableQueryByIdsTest.java:
##########
@@ -0,0 +1,353 @@
+/*
+ * 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.unit.rocksdb;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.hugegraph.backend.id.Id;
+import org.apache.hugegraph.backend.id.IdGenerator;
+import org.apache.hugegraph.backend.store.BackendEntry.BackendColumn;
+import org.apache.hugegraph.backend.store.BackendEntry.BackendColumnIterator;
+import org.apache.hugegraph.backend.store.rocksdb.RocksDBSessions;
+import org.apache.hugegraph.backend.store.rocksdb.RocksDBTables;
+import org.apache.hugegraph.testutil.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.rocksdb.RocksDBException;
+
+public class RocksDBTableQueryByIdsTest extends BaseRocksDBUnitTest {
+
+    private static final String DATABASE = "db";
+
+    private TestVertexTable vertexTable;
+    private TestEdgeTable edgeOutTable;
+    private TestEdgeTable edgeInTable;
+
+    @Override
+    @Before
+    public void setup() throws RocksDBException {
+        super.setup();
+        this.vertexTable = new TestVertexTable(DATABASE);
+        this.edgeOutTable = new TestEdgeTable(true, DATABASE);
+        this.edgeInTable = new TestEdgeTable(false, DATABASE);
+        this.rocks.createTable(this.vertexTable.table());
+        this.rocks.createTable(this.edgeOutTable.table());
+        this.rocks.createTable(this.edgeInTable.table());
+    }
+
+    @Test
+    public void testVertexQueryByIdsWithAllExistingIds() {
+        Id id1 = IdGenerator.of("v1");
+        Id id2 = IdGenerator.of("v2");
+        Id id3 = IdGenerator.of("v3");
+
+        this.rocks.session().put(this.vertexTable.table(), id1.asBytes(), 
getBytes("value1"));
+        this.rocks.session().put(this.vertexTable.table(), id2.asBytes(), 
getBytes("value2"));
+        this.rocks.session().put(this.vertexTable.table(), id3.asBytes(), 
getBytes("value3"));
+        this.commit();
+
+        List<Id> ids = Arrays.asList(id1, id2, id3);
+        BackendColumnIterator iter = 
this.vertexTable.queryByIds(this.rocks.session(), ids);
+
+        Map<String, String> results = toResultMap(iter);
+
+        Assert.assertEquals(3, results.size());
+        Assert.assertEquals("value1", results.get("v1"));
+        Assert.assertEquals("value2", results.get("v2"));
+        Assert.assertEquals("value3", results.get("v3"));
+    }
+
+    @Test
+    public void testVertexQueryByIdsWithExistingAndMissingIdsMixed() {
+        Id id1 = IdGenerator.of("v1");
+        Id id2 = IdGenerator.of("v2");
+        Id id3 = IdGenerator.of("v3");
+
+        this.rocks.session().put(this.vertexTable.table(), id1.asBytes(), 
getBytes("value1"));
+        this.rocks.session().put(this.vertexTable.table(), id3.asBytes(), 
getBytes("value3"));
+        this.commit();
+
+        List<Id> ids = Arrays.asList(id1, id2, id3);
+        BackendColumnIterator iter = 
this.vertexTable.queryByIds(this.rocks.session(), ids);
+
+        Map<String, String> results = toResultMap(iter);
+
+        Assert.assertEquals(2, results.size());
+        Assert.assertEquals("value1", results.get("v1"));
+        Assert.assertEquals("value3", results.get("v3"));
+        Assert.assertFalse(results.containsKey("v2"));
+    }
+
+    @Test
+    public void testVertexQueryByIdsDedupsDuplicateIds() {
+        Id id1 = IdGenerator.of("v1");
+        Id id2 = IdGenerator.of("v2");
+
+        this.rocks.session().put(this.vertexTable.table(), id1.asBytes(), 
getBytes("value1"));
+        this.rocks.session().put(this.vertexTable.table(), id2.asBytes(), 
getBytes("value2"));
+        this.commit();
+
+        List<Id> ids = Arrays.asList(id1, id2, id1);
+        BackendColumnIterator iter = 
this.vertexTable.queryByIds(this.rocks.session(), ids);
+
+        Map<String, String> results = toResultMap(iter);
+
+        Assert.assertEquals(2, results.size());
+        Assert.assertEquals("value1", results.get("v1"));
+        Assert.assertEquals("value2", results.get("v2"));
+    }
+
+    @Test
+    public void testEdgeOutQueryByIdsWithAllExistingIds() {
+        Id id1 = IdGenerator.of("e1");
+        Id id2 = IdGenerator.of("e2");
+
+        this.rocks.session().put(this.edgeOutTable.table(), id1.asBytes(), 
getBytes("edge-value1"));
+        this.rocks.session().put(this.edgeOutTable.table(), id2.asBytes(), 
getBytes("edge-value2"));
+        this.commit();
+
+        List<Id> ids = Arrays.asList(id1, id2);
+        BackendColumnIterator iter = 
this.edgeOutTable.queryByIds(this.rocks.session(), ids);
+
+        Map<String, String> results = toResultMap(iter);
+
+        Assert.assertEquals(2, results.size());
+        Assert.assertEquals("edge-value1", results.get("e1"));
+        Assert.assertEquals("edge-value2", results.get("e2"));
+    }
+
+    @Test
+    public void testEdgeInQueryByIdsWithAllExistingIds() {
+        Id id1 = IdGenerator.of("e1");
+        Id id2 = IdGenerator.of("e2");
+
+        this.rocks.session().put(this.edgeInTable.table(), id1.asBytes(), 
getBytes("edge-value1"));
+        this.rocks.session().put(this.edgeInTable.table(), id2.asBytes(), 
getBytes("edge-value2"));
+        this.commit();
+
+        List<Id> ids = Arrays.asList(id1, id2);
+        BackendColumnIterator iter = 
this.edgeInTable.queryByIds(this.rocks.session(), ids);
+
+        Map<String, String> results = toResultMap(iter);
+
+        Assert.assertEquals(2, results.size());
+        Assert.assertEquals("edge-value1", results.get("e1"));
+        Assert.assertEquals("edge-value2", results.get("e2"));
+    }
+
+    @Test
+    public void testVertexQueryByIdsFallbackWhenHasChanges() {
+        Id id1 = IdGenerator.of("v1");
+        Id id2 = IdGenerator.of("v2");
+
+        this.rocks.session().put(this.vertexTable.table(), id1.asBytes(), 
getBytes("value1"));
+        this.rocks.session().put(this.vertexTable.table(), id2.asBytes(), 
getBytes("value2"));
+        this.commit();
+
+        List<Id> ids = Arrays.asList(id1, id2);
+        RocksDBSessions.Session mockSession = new 
DelegatingSession(this.rocks.session()) {
+            @Override
+            public boolean hasChanges() {
+                return true;
+            }
+
+            @Override
+            public BackendColumnIterator get(String table, List<byte[]> keys) {
+                throw new AssertionError(
+                        "multi-get should not be called when hasChanges");
+            }
+        };
+
+        BackendColumnIterator iter = this.vertexTable.queryByIds(mockSession, 
ids);
+
+        Map<String, String> results = toResultMap(iter);
+        Assert.assertEquals(2, results.size());
+        Assert.assertEquals("value1", results.get("v1"));
+        Assert.assertEquals("value2", results.get("v2"));

Review Comment:
   `testVertexQueryByIdsFallbackWhenHasChanges()` forces `hasChanges()` to 
return true, but all read operations still delegate to an underlying real 
session whose `hasChanges()` is false. This doesn’t model the actual “pending 
write batch” state (in `RocksDBStdSessions.StdSession`, `get/scan` assert 
`!hasChanges()`), so the test can pass even though reads would still be invalid 
when the real session has pending changes.
   
   Suggestion: either change the test to expect failure when 
`hasChanges()==true` (if reads are intentionally unsupported in that state), or 
rework it to use a session implementation that truly supports reads with 
pending writes (if that behavior is desired).
   ```suggestion
   
               @Override
               public BackendColumn get(String table, byte[] key) throws 
RocksDBException {
                   throw new AssertionError(
                           "reads should not be performed when hasChanges");
               }
           };
   
           try {
               this.vertexTable.queryByIds(mockSession, ids);
               Assert.fail("queryByIds should fail when session has pending 
changes");
           } catch (AssertionError e) {
               Assert.assertTrue(e.getMessage().contains("hasChanges"));
           }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to