Copilot commented on code in PR #2982:
URL: https://github.com/apache/hugegraph/pull/2982#discussion_r3323564134
##########
hugegraph-server/hugegraph-rocksdb/src/main/java/org/apache/hugegraph/backend/store/rocksdb/RocksDBTable.java:
##########
@@ -215,6 +216,22 @@ protected BackendColumnIterator
queryByIds(RocksDBSessions.Session session,
));
}
+ protected BackendColumnIterator queryByIdsWithGet(RocksDBSessions.Session
session,
+ Collection<Id> ids) {
+ if (ids.size() == 1) {
+ return this.queryById(session, ids.iterator().next());
+ }
+
+ if (!session.hasChanges()) {
+ return this.getByIds(session, ids);
+ }
+
+ // NOTE: this will lead to lazy create rocksdb iterator
+ return BackendColumnIterator.wrap(new FlatMapperIterator<>(
+ ids.iterator(), id -> this.queryById(session, id)
+ ));
+ }
Review Comment:
`queryByIdsWithGet()` checks `session.hasChanges()` but then falls back to
`queryById()` per id. For Vertex/Edge, `queryById()` delegates to `getById()`
which ultimately calls `RocksDBSessions.Session#get()`, and
`RocksDBStdSessions.StdSession#get()/scan()` explicitly `assert !hasChanges()`.
So this fallback doesn't actually make reads safe when there are pending
changes; it only makes behavior depend on iterator evaluation order. Consider
failing fast with a clear exception (or otherwise guaranteeing a read-capable
session) instead of returning a lazy iterator here.
##########
hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/rocksdb/RocksDBTableQueryByIdsTest.java:
##########
@@ -0,0 +1,361 @@
+/*
+ * 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 byte[] get(String table, byte[] key) {
+ throw new AssertionError(
+ "reads should not be performed when hasChanges");
+ }
+
Review Comment:
This override makes the test unable to distinguish the intended fallback
behavior: both the per-id path and the multi-get path will fail. If the goal is
to ensure multi-get isn't used when `hasChanges()` is true, the single-key
`get()` should delegate (so the per-id fallback can succeed) while only the
multi-get override throws.
##########
hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/rocksdb/RocksDBTableQueryByIdsTest.java:
##########
@@ -0,0 +1,361 @@
+/*
+ * 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 byte[] get(String table, byte[] key) {
+ throw new AssertionError(
+ "reads should not be performed when hasChanges");
+ }
+
+ @Override
+ public BackendColumnIterator get(String table, List<byte[]> keys) {
+ throw new AssertionError(
+ "multi-get should not be called when hasChanges");
+ }
+ };
+
+ try {
+ BackendColumnIterator iter =
this.vertexTable.queryByIds(mockSession, ids);
+ // FlatMapperIterator is lazy; trigger evaluation to hit the mock
+ iter.hasNext();
+ Assert.fail("queryByIds should fail when session has pending
changes");
+ } catch (AssertionError e) {
+ Assert.assertTrue(e.getMessage().contains("hasChanges"));
+ }
+ }
Review Comment:
The current try/catch only asserts that *some* AssertionError occurs and
that the message contains "hasChanges", which will be true whether
`queryByIds()` uses multi-get or per-id reads. After allowing single-key reads
(see above), the test can assert the query succeeds and thereby reliably
verifies that multi-get isn't invoked when `hasChanges()` is true.
##########
hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/rocksdb/RocksDBTableQueryByIdsTest.java:
##########
@@ -0,0 +1,361 @@
+/*
+ * 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 byte[] get(String table, byte[] key) {
+ throw new AssertionError(
+ "reads should not be performed when hasChanges");
+ }
+
Review Comment:
This override makes the test unable to distinguish the intended fallback
behavior: both the per-id path and the multi-get path will fail. If the goal is
to ensure multi-get isn't used when `hasChanges()` is true, the single-key
`get()` should delegate (so the per-id fallback can succeed) while only the
multi-get override throws.
##########
hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/rocksdb/RocksDBTableQueryByIdsTest.java:
##########
@@ -0,0 +1,361 @@
+/*
+ * 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 byte[] get(String table, byte[] key) {
+ throw new AssertionError(
+ "reads should not be performed when hasChanges");
+ }
+
+ @Override
+ public BackendColumnIterator get(String table, List<byte[]> keys) {
+ throw new AssertionError(
+ "multi-get should not be called when hasChanges");
+ }
+ };
+
+ try {
+ BackendColumnIterator iter =
this.vertexTable.queryByIds(mockSession, ids);
+ // FlatMapperIterator is lazy; trigger evaluation to hit the mock
+ iter.hasNext();
+ Assert.fail("queryByIds should fail when session has pending
changes");
+ } catch (AssertionError e) {
+ Assert.assertTrue(e.getMessage().contains("hasChanges"));
+ }
+ }
Review Comment:
The current try/catch only asserts that *some* AssertionError occurs and
that the message contains "hasChanges", which will be true whether
`queryByIds()` uses multi-get or per-id reads. After allowing single-key reads
(see above), the test can assert the query succeeds and thereby reliably
verifies that multi-get isn't invoked when `hasChanges()` is true.
--
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]