在两个不同商店中的嵌套游标 [英] Nested Cursors over two different Stores
问题描述
我有以下代码:
...
Transaction xodusTransaction = xodusEnvironment.beginReadonlyTransaction();
Store leftStore = xodusEnvironment.openStore(leftName, StoreConfig.USE_EXISTING, xodusTransaction, false);
Store rightStore = xodusEnvironment.openStore(rightName, StoreConfig.USE_EXISTING, xodusTransaction, false);
try(Cursor leftCursor = leftStore.openCursor(xodusTransaction);
Cursor rightCursor = rightStore.openCursor(xodusTransaction)) {
while(leftCursor.getNext()) {
while(rightCursor.getNext()) {
// Do actual work with data from both stores
}
}
}
...
我希望内部循环将触发N * M次,其中N-leftStore的基数,M-rightStore的基数.
I expect that internal loop will be fired N*M times, where N - cardinality of leftStore and M - cardinality of rightStore.
在实践中,外部循环仅触发一次,内部循环触发M次.
On practice external loop fires only once and internal loop fires M-times.
如果我按照以下方式(更平滑嵌套循环)重写代码:
If I rewrite the code in following way (flattering nested loops):
...
while(leftCursor.getNext()) {
...
}
while(rightCursor.getNext()) {
...
}
...
然后,这两个循环都会按预期的方式为leftStore触发N次,为rightStore触发M次.
Then both loops fires as expected N-times for leftStore and M-times for rightStore.
问题是:是否可以使嵌套游标移动?如果是,请指导我.
The question is: is it possible to make nested cursor traveling? If yes, kindly please guide me.
谢谢!
-塔拉斯
推荐答案
一旦cursor.getNext()
返回false
(不存在 next 键/值对),它将永远不会返回true
此Cursor实例.要再次遍历商店,请重新打开光标.
Once cursor.getNext()
returned false
(there is no next key/value pair), it will never return true
for this Cursor instance. To traverse a Store again, reopen cursor.
这是代码以矩阵形式遍历两个商店,即两个商店中键/值对的所有成对组合:
Here is the code traversing two Stores as a matrix, i.e. all pairwise combinations of key/value pairs from both Stores:
try (Cursor leftCursor = leftStore.openCursor(txn)) {
while (leftCursor.getNext()) {
try (Cursor rightCursor = rightStore.openCursor(txn)) {
while (rightCursor.getNext()) {
// Do actual work with data from both stores
}
}
}
}
这篇关于在两个不同商店中的嵌套游标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!