/** * Given a context related to the procedure invocation this method checks if the transaction is terminated in some way * * @param db * @return * */ public static boolean transactionIsTerminated(TerminationGuard db) { try { db.check(); return false; } catch (TransactionGuardException | TransactionTerminatedException | NotInTransactionException tge) { return true; } }
private void waitForThrottle() { long msSinceLastCall = currentTimeMillis() - lastCallTime; while (msSinceLastCall < throttleInMs) { try { terminationGuard.check(); long msToWait = throttleInMs - msSinceLastCall; Thread.sleep(Math.min(msToWait, 1000)); } catch (InterruptedException e) { // ignore } msSinceLastCall = currentTimeMillis() - lastCallTime; } lastCallTime = currentTimeMillis(); } }
@Procedure @Description("apoc.util.sleep(<duration>) | sleeps for <duration> millis, transaction termination is honored") public void sleep(@Name("duration") long duration) throws InterruptedException { long started = System.currentTimeMillis(); while (System.currentTimeMillis()-started < duration) { try { Thread.sleep(5); terminationGuard.check(); } catch (TransactionTerminatedException e) { return; } } }
private Object executeStatement(BlockingQueue<RowResult> queue, String stmt, Map<String, Object> params, boolean addStatistics, long timeout) throws InterruptedException { try (Result result = db.execute(stmt,params)) { long time = System.currentTimeMillis(); int row = 0; while (result.hasNext()) { terminationGuard.check(); queue.put(new RowResult(row++, result.next())); } if (addStatistics) { queue.offer(new RowResult(-1, toMap(result.getQueryStatistics(), System.currentTimeMillis() - time, row)), timeout,TimeUnit.SECONDS); } return row; } }
partition.add(o); if (partition.size() == batchSize) { terminationGuard.check(); futures.add(submit(db, statement, params, key, partition)); partition = new ArrayList<>(batchSize);
@Procedure public Stream<MapResult> parallel(@Name("fragment") String fragment, @Name("params") Map<String, Object> params, @Name("parallelizeOn") String key) { if (params == null) return run(fragment, params); if (key == null || !params.containsKey(key)) throw new RuntimeException("Can't parallelize on key " + key + " available keys " + params.keySet()); Object value = params.get(key); if (!(value instanceof Collection)) throw new RuntimeException("Can't parallelize a non collection " + key + " : " + value); final String statement = withParamMapping(fragment, params.keySet()); Collection<Object> coll = (Collection<Object>) value; return coll.parallelStream().flatMap((v) -> { terminationGuard.check(); Map<String, Object> parallelParams = new HashMap<>(params); parallelParams.replace(key, v); return db.execute(statement, parallelParams).stream().map(MapResult::new); }); /* params.entrySet().stream() .filter( e -> asCollection(e.getValue()).size() > 100) .map( (e) -> (Map.Entry<String,Collection>)(Map.Entry)e ) .max( (max,e) -> e.getValue().size() ) .map( (e) -> e.getValue().parallelStream().map( (v) -> { Map map = new HashMap<>(params); map.put(e.getKey(),as) })); return db.execute(statement,params).stream().map(MapResult::new); */ }