使用 Java 8 Stream 的 DataProvider 查询中的 IndexOutOfBoundsException
IndexOutOfBoundsException in DataProvider query using Java 8 Stream
我想从后端延迟加载过滤后的数据。如果我从后端应用 returns 1 个元素(或未应用过滤器)的过滤器,一切正常。
如果获取的项目超过 1 个,它就会开始崩溃/抛出 OutOfBoundsException
。我使用 official Vaadin documentation 作为我实施的指南。
在Stream-API的.filter()
方法中抛出异常。我也知道异常的含义,尝试访问值的索引 >= 列表的最大大小,但我不确定如何解决这个问题。将第一个查询的限制设置为 limit-1 并没有解决它。
我认为回调中的第二个查询有问题,但我无法弄清楚问题出在哪里。是 Stream-API 问题还是第二个回调查询没有正常工作?
public DataProvider<T, String> createLazyLoadingProvider(List<T> data) {
DataProvider<T, String> lazyLoadingProvider
= DataProvider.fromFilteringCallbacks(query -> {
// First Query
final int offset = query.getOffset();
final int limit = query.getLimit();
final String filter = query.getFilter().orElse("");
return data
.stream()
.skip(offset)
.limit(limit)
.filter(e -> {
return e.toString().startsWith(filter);
});
}, query -> {
// Second query
return (int) data.stream()
.filter(e -> {
return e.toString().startsWith(filter);
})
.count();
});
return lazyLoadingProvider;
}
生产:
First Query
Offset: 0
Limit: 100
Callback-Filter: 3
Full Size of Data: 6005
From backend fetched Items: 5
-----------
Second Query
Offset: 0
Limit: 2147483647
Callback-Filter: 3
Max fetchable Items with filter applied: 381
堆栈跟踪:
java.lang.IndexOutOfBoundsException: Index 5 out of bounds for length
5 at
java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at
java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at
java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372) at
java.base/java.util.ArrayList.get(ArrayList.java:458) at
com.vaadin.flow.data.provider.DataCommunicator.lambda$getJsonItems(DataCommunicator.java:611)
at
java.base/java.util.stream.IntPipeline.accept(IntPipeline.java:180)
at
java.base/java.util.stream.Streams$RangeIntSpliterator.forEachRemaining(Streams.java:104)
at
java.base/java.util.Spliterator$OfInt.forEachRemaining(Spliterator.java:699)
at
java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at
java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at
java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
at
java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at
java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
at
com.vaadin.flow.data.provider.DataCommunicator.getJsonItems(DataCommunicator.java:613)
at
com.vaadin.flow.data.provider.DataCommunicator.collectChangesToSend(DataCommunicator.java:556)
at
com.vaadin.flow.data.provider.DataCommunicator.flush(DataCommunicator.java:473)
at
com.vaadin.flow.data.provider.DataCommunicator.lambda$requestFlushf364bb9(DataCommunicator.java:421)
at
com.vaadin.flow.internal.StateTree.lambda$runExecutionsBeforeClientResponse(StateTree.java:364)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1540) at
com.vaadin.flow.internal.StateTree.runExecutionsBeforeClientResponse(StateTree.java:361)
at
com.vaadin.flow.server.communication.UidlWriter.encodeChanges(UidlWriter.java:392)
at
com.vaadin.flow.server.communication.UidlWriter.createUidl(UidlWriter.java:182)
at
com.vaadin.flow.server.communication.UidlRequestHandler.writeUidl(UidlRequestHandler.java:116)
at
com.vaadin.flow.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:89)
at
com.vaadin.flow.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40)
at
com.vaadin.flow.server.VaadinService.handleRequest(VaadinService.java:1540)
at
com.vaadin.flow.server.VaadinServlet.service(VaadinServlet.java:246)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:750) at
org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:760)
at
org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1617)
at
org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:226)
at
org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1604)
at
org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:545)
at
org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
at
org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:536)
at
org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)
at
org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:235)
at
org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1592)
at
org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233)
at
org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1296)
at
org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:188)
at
org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:485)
at
org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1562)
at
org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:186)
at
org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1211)
at
org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at
org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:221)
at
org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:146)
at
org.eclipse.jetty.server.handler.gzip.GzipHandler.handle(GzipHandler.java:717)
at
org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:146)
at
org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)
at org.eclipse.jetty.server.Server.handle(Server.java:500) at
org.eclipse.jetty.server.HttpChannel.lambda$handle(HttpChannel.java:386)
at
org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:562)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:378)
at
org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:270)
at
org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103)
at
org.eclipse.jetty.io.ssl.SslConnection$DecryptedEndPoint.onFillable(SslConnection.java:543)
at
org.eclipse.jetty.io.ssl.SslConnection.onFillable(SslConnection.java:398)
at
org.eclipse.jetty.io.ssl.SslConnection.succeeded(SslConnection.java:161)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103)
at
org.eclipse.jetty.io.ChannelEndPoint.run(ChannelEndPoint.java:117)
at
org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:336)
at
org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:313)
at
org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:171)
at
org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:129)
at
org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:388)
at
org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:806)
at
org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:938)
at java.base/java.lang.Thread.run(Thread.java:834)
您需要在应用偏移量和限制之前过滤。
阐述:
假设您有 Apple, Cucumber, Lemon, Lettuce, Linseed, Lime, Tangerine, Tomato
项并希望过滤器仅包含以 L
.
开头的项
您的计数查询 returns 计数为 4 (Lemon, Lettuce, Linseed, Lime
)。
现在假设网格需要前两项,所以它发送偏移量 0 和限制 2。
您的代码所做的是选择前两项 Apple, Cucumber
,然后过滤它们,然后 returns 0 项。
它需要做的是过滤items得到Lemon, Lettuce, Linseed, Lime
,然后取offset 0和limit 2得到Lemon, Lettuce
.
我想从后端延迟加载过滤后的数据。如果我从后端应用 returns 1 个元素(或未应用过滤器)的过滤器,一切正常。
如果获取的项目超过 1 个,它就会开始崩溃/抛出 OutOfBoundsException
。我使用 official Vaadin documentation 作为我实施的指南。
在Stream-API的.filter()
方法中抛出异常。我也知道异常的含义,尝试访问值的索引 >= 列表的最大大小,但我不确定如何解决这个问题。将第一个查询的限制设置为 limit-1 并没有解决它。
我认为回调中的第二个查询有问题,但我无法弄清楚问题出在哪里。是 Stream-API 问题还是第二个回调查询没有正常工作?
public DataProvider<T, String> createLazyLoadingProvider(List<T> data) {
DataProvider<T, String> lazyLoadingProvider
= DataProvider.fromFilteringCallbacks(query -> {
// First Query
final int offset = query.getOffset();
final int limit = query.getLimit();
final String filter = query.getFilter().orElse("");
return data
.stream()
.skip(offset)
.limit(limit)
.filter(e -> {
return e.toString().startsWith(filter);
});
}, query -> {
// Second query
return (int) data.stream()
.filter(e -> {
return e.toString().startsWith(filter);
})
.count();
});
return lazyLoadingProvider;
}
生产:
First Query
Offset: 0
Limit: 100
Callback-Filter: 3
Full Size of Data: 6005
From backend fetched Items: 5
-----------
Second Query
Offset: 0
Limit: 2147483647
Callback-Filter: 3
Max fetchable Items with filter applied: 381
堆栈跟踪:
java.lang.IndexOutOfBoundsException: Index 5 out of bounds for length 5 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248) at java.base/java.util.Objects.checkIndex(Objects.java:372) at java.base/java.util.ArrayList.get(ArrayList.java:458) at com.vaadin.flow.data.provider.DataCommunicator.lambda$getJsonItems(DataCommunicator.java:611) at java.base/java.util.stream.IntPipeline.accept(IntPipeline.java:180) at java.base/java.util.stream.Streams$RangeIntSpliterator.forEachRemaining(Streams.java:104) at java.base/java.util.Spliterator$OfInt.forEachRemaining(Spliterator.java:699) at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578) at com.vaadin.flow.data.provider.DataCommunicator.getJsonItems(DataCommunicator.java:613) at com.vaadin.flow.data.provider.DataCommunicator.collectChangesToSend(DataCommunicator.java:556) at com.vaadin.flow.data.provider.DataCommunicator.flush(DataCommunicator.java:473) at com.vaadin.flow.data.provider.DataCommunicator.lambda$requestFlushf364bb9(DataCommunicator.java:421) at com.vaadin.flow.internal.StateTree.lambda$runExecutionsBeforeClientResponse(StateTree.java:364) at java.base/java.util.ArrayList.forEach(ArrayList.java:1540) at com.vaadin.flow.internal.StateTree.runExecutionsBeforeClientResponse(StateTree.java:361) at com.vaadin.flow.server.communication.UidlWriter.encodeChanges(UidlWriter.java:392) at com.vaadin.flow.server.communication.UidlWriter.createUidl(UidlWriter.java:182) at com.vaadin.flow.server.communication.UidlRequestHandler.writeUidl(UidlRequestHandler.java:116) at com.vaadin.flow.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:89) at com.vaadin.flow.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40) at com.vaadin.flow.server.VaadinService.handleRequest(VaadinService.java:1540) at com.vaadin.flow.server.VaadinServlet.service(VaadinServlet.java:246) at javax.servlet.http.HttpServlet.service(HttpServlet.java:750) at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:760) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1617) at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:226) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1604) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:545) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:536) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127) at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:235) at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1592) at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1296) at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:188) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:485) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1562) at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:186) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1211) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:221) at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:146) at org.eclipse.jetty.server.handler.gzip.GzipHandler.handle(GzipHandler.java:717) at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:146) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127) at org.eclipse.jetty.server.Server.handle(Server.java:500) at org.eclipse.jetty.server.HttpChannel.lambda$handle(HttpChannel.java:386) at org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:562) at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:378) at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:270) at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311) at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103) at org.eclipse.jetty.io.ssl.SslConnection$DecryptedEndPoint.onFillable(SslConnection.java:543) at org.eclipse.jetty.io.ssl.SslConnection.onFillable(SslConnection.java:398) at org.eclipse.jetty.io.ssl.SslConnection.succeeded(SslConnection.java:161) at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103) at org.eclipse.jetty.io.ChannelEndPoint.run(ChannelEndPoint.java:117) at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:336) at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:313) at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:171) at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:129) at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:388) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:806) at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:938) at java.base/java.lang.Thread.run(Thread.java:834)
您需要在应用偏移量和限制之前过滤。
阐述:
假设您有 Apple, Cucumber, Lemon, Lettuce, Linseed, Lime, Tangerine, Tomato
项并希望过滤器仅包含以 L
.
您的计数查询 returns 计数为 4 (Lemon, Lettuce, Linseed, Lime
)。
现在假设网格需要前两项,所以它发送偏移量 0 和限制 2。
您的代码所做的是选择前两项 Apple, Cucumber
,然后过滤它们,然后 returns 0 项。
它需要做的是过滤items得到Lemon, Lettuce, Linseed, Lime
,然后取offset 0和limit 2得到Lemon, Lettuce
.