Nested Cursors over two different Stores

天涯浪子 提交于 2019-12-24 03:37:18

问题


I have following code:

...

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
    }
  }
}
... 

I expect that internal loop will be fired N*M times, where N - cardinality of leftStore and M - cardinality of rightStore.

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()) {
 ...
}

...

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.

Thank you!

-Taras


回答1:


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
            }
        }
    }
}


来源:https://stackoverflow.com/questions/57166344/nested-cursors-over-two-different-stores

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!