List of usage examples for java.util.stream StreamSupport stream
public static <T> Stream<T> stream(Supplier<? extends Spliterator<T>> supplier, int characteristics, boolean parallel)
From source file:org.nuxeo.ecm.core.storage.marklogic.MarkLogicRepository.java
protected Stream<State> findAll(String ctsQuery, String... selects) { String query = ctsQuery;//w ww. ja va2 s . co m if (selects.length > 0) { query = "import module namespace extract = 'http://nuxeo.com/extract' at '/ext/nuxeo/extract.xqy';\n" + "let $paths := (" + Arrays.stream(selects).map(MarkLogicHelper::serializeKey) .map(select -> "\"" + MarkLogicHelper.DOCUMENT_ROOT_PATH + "/" + select + "\"") .collect(Collectors.joining(",\n")) + ")let $namespaces := ()\n" + "for $i in " + query + " return extract:extract-nodes($i, $paths, $namespaces)"; } if (log.isTraceEnabled()) { logQuery(query); } // Run query boolean completedAbruptly = true; Session session = xccContentSource.newSession(); try { // As we can get a lot of results don't buffer the result RequestOptions options = new RequestOptions(); options.setCacheResult(false); AdhocQuery request = session.newAdhocQuery(query, options); // We give 0 as characteristics because it's the value used by Iterable#spliterator() and according to // StreamSupport.stream(Supplier, int, boolean) documentation, characteristics must be equal to // supplier.get().characteristics() Supplier<Spliterator<ResultItem>> spliteratorSupplier = () -> { try { // ResultSequence will be closed by Session close (closed by stream closed) ResultSequence rs = session.submitRequest(request); Iterable<ResultItem> items = rs::iterator; return items.spliterator(); } catch (RequestException e) { throw new NuxeoException("An exception happened during xcc call", e); } }; Stream<State> stream = StreamSupport.stream(spliteratorSupplier, 0, false).onClose(session::close) .map(ResultItem::getItem).map(XdmItem::asInputStream) .map(MarkLogicStateDeserializer::deserialize); // the stream takes responsibility for closing the session completedAbruptly = false; return stream; } finally { if (completedAbruptly) { session.close(); } } }