Skip to content

Commit c1b3599

Browse files
committed
optimize: extract the common logic
1 parent d33123e commit c1b3599

3 files changed

Lines changed: 39 additions & 6 deletions

File tree

hugegraph-server/hugegraph-rocksdb/src/main/java/org/apache/hugegraph/backend/store/rocksdb/RocksDBTable.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,18 @@ protected BackendColumnIterator queryByIds(RocksDBSessions.Session session,
210210
return this.queryById(session, ids.iterator().next());
211211
}
212212

213+
// NOTE: this will lead to lazy create rocksdb iterator
214+
return BackendColumnIterator.wrap(new FlatMapperIterator<>(
215+
ids.iterator(), id -> this.queryById(session, id)
216+
));
217+
}
218+
219+
protected BackendColumnIterator queryByIdsWithGet(RocksDBSessions.Session session,
220+
Collection<Id> ids) {
221+
if (ids.size() == 1) {
222+
return this.queryById(session, ids.iterator().next());
223+
}
224+
213225
if (!session.hasChanges()) {
214226
return this.getByIds(session, ids);
215227
}

hugegraph-server/hugegraph-rocksdb/src/main/java/org/apache/hugegraph/backend/store/rocksdb/RocksDBTables.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.nio.ByteBuffer;
2121
import java.nio.ByteOrder;
22+
import java.util.Collection;
2223
import java.util.List;
2324

2425
import org.apache.hugegraph.backend.id.Id;
@@ -177,6 +178,12 @@ public Vertex(String database) {
177178
protected BackendColumnIterator queryById(RocksDBSessions.Session session, Id id) {
178179
return this.getById(session, id);
179180
}
181+
182+
@Override
183+
protected BackendColumnIterator queryByIds(RocksDBSessions.Session session,
184+
Collection<Id> ids) {
185+
return this.queryByIdsWithGet(session, ids);
186+
}
180187
}
181188

182189
public static class Edge extends RocksDBTable {
@@ -200,6 +207,12 @@ public static Edge in(String database) {
200207
protected BackendColumnIterator queryById(RocksDBSessions.Session session, Id id) {
201208
return this.getById(session, id);
202209
}
210+
211+
@Override
212+
protected BackendColumnIterator queryByIds(RocksDBSessions.Session session,
213+
Collection<Id> ids) {
214+
return this.queryByIdsWithGet(session, ids);
215+
}
203216
}
204217

205218
public static class IndexTable extends RocksDBTable {

hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/rocksdb/RocksDBTableQueryByIdsTest.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,19 +171,27 @@ public boolean hasChanges() {
171171
return true;
172172
}
173173

174+
@Override
175+
public byte[] get(String table, byte[] key) {
176+
throw new AssertionError(
177+
"reads should not be performed when hasChanges");
178+
}
179+
174180
@Override
175181
public BackendColumnIterator get(String table, List<byte[]> keys) {
176182
throw new AssertionError(
177183
"multi-get should not be called when hasChanges");
178184
}
179185
};
180186

181-
BackendColumnIterator iter = this.vertexTable.queryByIds(mockSession, ids);
182-
183-
Map<String, String> results = toResultMap(iter);
184-
Assert.assertEquals(2, results.size());
185-
Assert.assertEquals("value1", results.get("v1"));
186-
Assert.assertEquals("value2", results.get("v2"));
187+
try {
188+
BackendColumnIterator iter = this.vertexTable.queryByIds(mockSession, ids);
189+
// FlatMapperIterator is lazy; trigger evaluation to hit the mock
190+
iter.hasNext();
191+
Assert.fail("queryByIds should fail when session has pending changes");
192+
} catch (AssertionError e) {
193+
Assert.assertTrue(e.getMessage().contains("hasChanges"));
194+
}
187195
}
188196

189197
private Map<String, String> toResultMap(BackendColumnIterator iter) {

0 commit comments

Comments
 (0)