public final class DefaultGraphTransformationEngine extends Object implements GraphTransformationEngine
ComputeKey
into ComputeResult
via
GraphComputation
. This engine is able to asynchronously run graph based computation,
reusing results when possible. Note that the computation dependency graph must be an acyclic
graph.
This engine is able to deal with dependencies in the computation graph by having Transformer
request dependent results of other transformations through GraphComputation.discoverPreliminaryDeps(ComputeKey)
and GraphComputation.discoverDeps(ComputeKey, ComputationEnvironment)
. The engine guarantees that
all dependencies are completed before performing the transformation.
This engine allows for multiple stages of transformations via different GraphComputation
s. These are specified during construction of the engine by supplying multiple
GraphComputationStage
s. Different stages are allowed to depend on other stages, as long
as the computation nodes do not form a cyclic dependency.
GraphComputation.discoverPreliminaryDeps(ComputeKey)
will be ran first to discover a
set of dependencies based only on the ComputeKey
. The keys are then computed, and the
results passed via the ComputationEnvironment
to GraphComputation.discoverDeps(ComputeKey, ComputationEnvironment)
for a second stage of
dependency discovery, where the dependency discovery can use the dependencies previously
specified. The results of dependencies returned via both GraphComputation.discoverPreliminaryDeps(ComputeKey)
and GraphComputation.discoverDeps(ComputeKey, ComputationEnvironment)
will be available for the
GraphComputation.transform(ComputeKey, ComputationEnvironment)
operation.
Transformations also should never block waiting for each other in any manner. If required to
wait, the transformation must declare it through GraphComputation.discoverPreliminaryDeps(ComputeKey)
or GraphComputation.discoverDeps(ComputeKey, ComputationEnvironment)
The transformation is incremental, so cached portions of the transformation will be used
whenever possible based on ComputeKey.equals()
. Therefore, ComputeKey
should be
immutable, and have deterministic equals. For future perspective, we want to have ComputeKey
be serializable, so that we can eventually send keys to be computed remotely.
A custom cache can be supplied to the engine to cache the computation as desired.
Transformations will be applied asynchronously, so independent transformations can be executed in parallel. It is therefore important that transformations are thread safe.
By using all callback based operations and queue based operations, this engine will also
reduce stack usage, eliminating stack overflow for large graph computations, provided that the
GraphComputation
itself does not stack overflow within its GraphComputation.discoverPreliminaryDeps(ComputeKey)
, GraphComputation.discoverDeps(ComputeKey, ComputationEnvironment)
, and GraphComputation.transform(ComputeKey, ComputationEnvironment)
methods.
Constructor and Description |
---|
DefaultGraphTransformationEngine(com.google.common.collect.ImmutableList<GraphComputationStage<?,?>> stages,
int estimatedNumOps,
DepsAwareExecutor<? super ComputeResult,?> executor)
Constructs a
DefaultGraphTransformationEngine with the given transformations. |
Modifier and Type | Method and Description |
---|---|
void |
close()
Shuts down the engine and rejects any future computations
|
<KeyType extends ComputeKey<ResultType>,ResultType extends ComputeResult> |
compute(KeyType key)
Asynchronously computes the result for the given key
|
<KeyType extends ComputeKey<ResultType>,ResultType extends ComputeResult> |
computeAll(Set<KeyType> keys)
Asynchronously computes the result for multiple keys
|
<KeyType extends ComputeKey<ResultType>,ResultType extends ComputeResult> |
computeAllUnchecked(Set<KeyType> keys)
Synchronously computes the result for multiple keys
|
<KeyType extends ComputeKey<ResultType>,ResultType extends ComputeResult> |
computeUnchecked(KeyType key)
Synchronously computes the given key
|
public DefaultGraphTransformationEngine(com.google.common.collect.ImmutableList<GraphComputationStage<?,?>> stages, int estimatedNumOps, DepsAwareExecutor<? super ComputeResult,?> executor)
DefaultGraphTransformationEngine
with the given transformations.stages
- all of the available transformation stages this engine can executeestimatedNumOps
- the estimated number of operations this engine will execute given a
computation, to reserve the size of its computation indexexecutor
- the custom DepsAwareExecutor
the engine uses to execute taskspublic void close()
GraphTransformationEngine
close
in interface GraphTransformationEngine
close
in interface AutoCloseable
public final <KeyType extends ComputeKey<ResultType>,ResultType extends ComputeResult> Future<ResultType> compute(KeyType key)
GraphTransformationEngine
compute
in interface GraphTransformationEngine
key
- the specific Key on the graph to computepublic final <KeyType extends ComputeKey<ResultType>,ResultType extends ComputeResult> ResultType computeUnchecked(KeyType key)
GraphTransformationEngine
computeUnchecked
in interface GraphTransformationEngine
key
- the specific Key on the graph to computepublic final <KeyType extends ComputeKey<ResultType>,ResultType extends ComputeResult> com.google.common.collect.ImmutableMap<KeyType,Future<ResultType>> computeAll(Set<KeyType> keys)
GraphTransformationEngine
computeAll
in interface GraphTransformationEngine
keys
- iterable of keys to compute on the graphpublic final <KeyType extends ComputeKey<ResultType>,ResultType extends ComputeResult> com.google.common.collect.ImmutableMap<KeyType,ResultType> computeAllUnchecked(Set<KeyType> keys)
GraphTransformationEngine
computeAllUnchecked
in interface GraphTransformationEngine
keys
- iterable of the keys to compute on the graph