/** * Execute query, apply statement's aggregation function, and return result iterator. * The aggregation function should be initialized via the statement's setAggregateFunction() * and should be located in a resource or a filesystem file. * <p> * The query executor puts results on a queue in separate threads. The calling thread * concurrently pops results off the queue through the ResultSet iterator. * The aggregation function is called on both server and client (final reduce). * Therefore, the Lua script file must also reside on both server and client. * * @param policy generic configuration parameters, pass in null for defaults * @param statement database query command * @throws AerospikeException if query fails */ public final ResultSet queryAggregate(QueryPolicy policy, Statement statement) throws AerospikeException { if (policy == null) { policy = queryPolicyDefault; } statement.prepare(true); QueryAggregateExecutor executor = new QueryAggregateExecutor(cluster, policy, statement, null); return executor.getResultSet(); }
/** * Retrieve next result. This method will block until a result is retrieved * or the query is cancelled. * * @return whether result exists - if false, no more results are available */ public final boolean next() throws AerospikeException { if (! valid) { executor.checkForException(); return false; } try { row = queue.take(); } catch (InterruptedException ie) { valid = false; if (Log.debugEnabled()) { Log.debug("ResultSet " + executor.statement.taskId + " take interrupted"); } return false; } if (row == END) { valid = false; executor.checkForException(); return false; } return true; }
/** * Close query. */ public final void close() { valid = false; // Check if more results are available. if (row != END && queue.poll() != END) { // Some query threads may still be running. Stop these threads. executor.stopThreads(new AerospikeException.QueryTerminated()); } }
public void run() { try { runThreads(); } catch (Exception e) { super.stopThreads(e); } finally { LuaCache.putInstance(lua); } }
public void runThreads() throws AerospikeException { try { // Start thread queries to each node. startThreads(); lua.loadPackage(statement); LuaValue[] args = new LuaValue[4 + statement.getFunctionArgs().length]; args[0] = lua.getFunction(statement.getFunctionName()); args[1] = LuaInteger.valueOf(2); args[2] = new LuaInputStream(inputQueue); args[3] = new LuaOutputStream(resultSet); int count = 4; for (Value value : statement.getFunctionArgs()) { args[count++] = value.getLuaValue(lua); } lua.call("apply_stream", args); } finally { // Send end command to user's result set. // If query was already cancelled, this put will be ignored. resultSet.put(ResultSet.END); } }
public QueryAggregateExecutor(Cluster cluster, QueryPolicy policy, Statement statement, Node node) throws AerospikeException { super(cluster, policy, statement, node); inputQueue = new ArrayBlockingQueue<LuaValue>(500); resultSet = new ResultSet(this, policy.recordQueueSize); // Work around luaj LuaInteger static initialization bug. // Calling LuaInteger.valueOf(long) is required because LuaValue.valueOf() does not have // a method that takes in a long parameter. The problem is directly calling // LuaInteger.valueOf(long) results in a static initialization error. // // If LuaValue.valueOf() is called before any luaj calls, then the static initializer in // LuaInteger will be initialized properly. LuaValue.valueOf(0); // Retrieve lua instance from cache. lua = LuaCache.getInstance(); try { // Initialize threads, but do not run yet. initializeThreads(); // Start Lua thread which reads from a queue, applies aggregate function and // writes to a result set. threadPool.execute(this); } catch (RuntimeException re) { // Put the lua instance back if thread creation fails. LuaCache.putInstance(lua); throw re; } }
public void run() { try { runThreads(); } catch (Exception e) { super.stopThreads(e); } finally { LuaCache.putInstance(lua); } }
public void runThreads() throws AerospikeException { try { // Start thread queries to each node. startThreads(); lua.loadPackage(statement); LuaValue[] args = new LuaValue[4 + statement.getFunctionArgs().length]; args[0] = lua.getFunction(statement.getFunctionName()); args[1] = LuaInteger.valueOf(2); args[2] = new LuaInputStream(inputQueue); args[3] = new LuaOutputStream(resultSet); int count = 4; for (Value value : statement.getFunctionArgs()) { args[count++] = value.getLuaValue(lua); } lua.call("apply_stream", args); } finally { // Send end command to user's result set. // If query was already cancelled, this put will be ignored. resultSet.put(ResultSet.END); } }
/** * Execute query, apply statement's aggregation function, and return result iterator. * The aggregation function should be initialized via the statement's setAggregateFunction() * and should be located in a resource or a filesystem file. * <p> * The query executor puts results on a queue in separate threads. The calling thread * concurrently pops results off the queue through the ResultSet iterator. * The aggregation function is called on both server and client (final reduce). * Therefore, the Lua script file must also reside on both server and client. * * @param policy generic configuration parameters, pass in null for defaults * @param statement database query command * @throws AerospikeException if query fails */ public final ResultSet queryAggregate(QueryPolicy policy, Statement statement) throws AerospikeException { if (policy == null) { policy = queryPolicyDefault; } statement.prepare(true); QueryAggregateExecutor executor = new QueryAggregateExecutor(cluster, policy, statement, null); return executor.getResultSet(); }
/** * Close query. */ public final void close() { valid = false; // Check if more results are available. if (row != END && queue.poll() != END) { // Some query threads may still be running. Stop these threads. executor.stopThreads(new AerospikeException.QueryTerminated()); } }
/** * Retrieve next result. This method will block until a result is retrieved * or the query is cancelled. * * @return whether result exists - if false, no more results are available */ public final boolean next() throws AerospikeException { if (! valid) { executor.checkForException(); return false; } try { row = queue.take(); } catch (InterruptedException ie) { valid = false; if (Log.debugEnabled()) { Log.debug("ResultSet " + executor.statement.taskId + " take interrupted"); } return false; } if (row == END) { valid = false; executor.checkForException(); return false; } return true; }
/** * Execute query on a single server node, apply statement's aggregation function, and return * result iterator. * The aggregation function should be initialized via the statement's setAggregateFunction() * and should be located in a resource or a filesystem file. * <p> * The query executor puts results on a queue in separate threads. The calling thread * concurrently pops results off the queue through the ResultSet iterator. * The aggregation function is called on both server and client (final reduce). * Therefore, the Lua script file must also reside on both server and client. * * @param policy generic configuration parameters, pass in null for defaults * @param statement database query command * @param node server node to execute query * @throws AerospikeException if query fails */ public final ResultSet queryAggregateNode(QueryPolicy policy, Statement statement, Node node) throws AerospikeException { if (policy == null) { policy = queryPolicyDefault; } statement.prepare(true); QueryAggregateExecutor executor = new QueryAggregateExecutor(cluster, policy, statement, node); return executor.getResultSet(); }
/** * Execute query on a single server node, apply statement's aggregation function, and return * result iterator. * The aggregation function should be initialized via the statement's setAggregateFunction() * and should be located in a resource or a filesystem file. * <p> * The query executor puts results on a queue in separate threads. The calling thread * concurrently pops results off the queue through the ResultSet iterator. * The aggregation function is called on both server and client (final reduce). * Therefore, the Lua script file must also reside on both server and client. * * @param policy generic configuration parameters, pass in null for defaults * @param statement database query command * @param node server node to execute query * @throws AerospikeException if query fails */ public final ResultSet queryAggregateNode(QueryPolicy policy, Statement statement, Node node) throws AerospikeException { if (policy == null) { policy = queryPolicyDefault; } statement.prepare(true); QueryAggregateExecutor executor = new QueryAggregateExecutor(cluster, policy, statement, node); return executor.getResultSet(); }