gusucode.com > bigdata 工具箱 matlab源码程序 > bigdata/+matlab/+bigdata/+internal/+executor/PartitionedArrayExecutorReference.m

    %PartitionedArrayExecutorReference
% An implementation of PartitionedArrayExecutor that wraps around a soft
% reference to another PartitionedArrayExecutor.
%
% This exists so that PartitionedArrays objects can hold a reference to an
% instance of PartitionedArrayExecutor without preventing the execution
% environment being destroyed if the mapreducer is explicitly changed. It
% contains the means to recreate an executor if it is safe to do this.
%

%   Copyright 2016 The MathWorks, Inc.

classdef (Sealed) PartitionedArrayExecutorReference < matlab.bigdata.internal.executor.PartitionedArrayExecutor
    properties (SetAccess = private)
        % The underlying soft reference to the executor.
        ExecutorRef;
    end
        
    properties (SetAccess = immutable)
        % A memento struct that can recreate the underlying mapreducer
        % object when this object requires to recreate an execution
        % environment
        MapReducerMemento;
    end
    
    methods
        function obj = PartitionedArrayExecutorReference(executor, memento)
            obj.ExecutorRef = hGetSoftReference(executor);
            if nargin >= 2
                obj.MapReducerMemento = memento;
            end
        end
        
        %EXECUTE Execute the provided graph of tasks.
        function varargout = execute(obj, taskGraph)
            executor = obj.getOrCreate();
            [varargout{1 : nargout}] = executor.execute(taskGraph);
        end
        
        %COUNTNUMPASSES Count the number of passes required to execute the provided graph of tasks.
        function numPasses = countNumPasses(obj, taskGraph)
            executor = obj.getOrCreate();
            numPasses = executor.countNumPasses(taskGraph);
        end
    end
    
    methods
        % Check whether this executor is valid.
        function tf = checkIsValid(obj)
            tf = obj.checkIsValidNow();
            
            if ~tf && ~isempty(obj.MapReducerMemento)
                mr = gcmr('nocreate');
                tf =  isempty(mr) ...
                    || isequal(obj.MapReducerMemento, mr.getMemento());
            end
        end
        
        % Check whether this executor is valid right now.
        function tf = checkIsValidNow(obj)
            executor = obj.ExecutorRef.get();
            tf = ~isempty(executor) && executor.checkIsValid();
        end
        
        %CHECKDATASTORESUPPORT Check whether the provided datastore is supported.
        % The default is to do nothing. Implementations will are allowed to
        % issue errors from here if the datastore is not supported.
        function checkDatastoreSupport(obj, ds)
            executor = obj.getOrCreate();
            executor.checkDatastoreSupport(ds);
        end
        
        %CHECKSAMEEXECUTOR Check whether the two executor objects represent
        % the same underlying execution environment.
        function tf = checkSameExecutor(obj1, obj2)
            tf = (obj1.ExecutorRef == obj2.ExecutorRef);
            if ~tf && strcmp(class(obj1), class(obj2))
                try
                    tf = obj1.getOrCreate() == obj2.getOrCreate();
                catch
                    tf = false;
                end
            end
        end
        
        %KEEPALIVE Notify to the executor that operations have just been
        %performed and it should reset any idle timeouts.
        function keepAlive(obj)
            executor = obj.getOrCreate();
            executor.keepAlive();
        end
        
        %REQUIRESSEQUENCEFILEFORMAT A flag that specifies if tall/write
        %should always generate sequence files.
        function tf = requiresSequenceFileFormat(obj)
            executor = obj.getOrCreate();
            tf = executor.requiresSequenceFileFormat();
        end
        
        %SUPPORTSSINGLEPARTITION A flag that specifies if the executor
        %supports the single partition optimization.
        function tf = supportsSinglePartition(obj)
            executor = obj.getOrCreate();
            tf = executor.supportsSinglePartition();
        end
    end
    
    methods (Access = private)
        % Get or create the underlying executor.
        function executor = getOrCreate(obj)
            executor = obj.ExecutorRef.get();
            if ~isempty(executor) && executor.checkIsValid()
                return;
            end
            
            mr = gcmr('nocreate');
            if isempty(mr)
                mr = matlab.mapreduce.MapReducer.createFromMemento(obj.MapReducerMemento);
                if isempty(mr)
                    error(message('MATLAB:bigdata:array:InvalidTall'));
                end
                mr = mapreducer(mr);
            else
                if ~isequal(obj.MapReducerMemento, mr.getMemento())
                    error(message('MATLAB:bigdata:array:InvalidTall'));
                end
            end
            executor = mr.getPartitionedArrayExecutor();
            obj.ExecutorRef = hGetSoftReference(executor);
        end
    end
end