% Trivial resource set - the resource set used for SPMD(0) calls


% Copyright 2008-2022 The MathWorks, Inc.
classdef TrivialResourceSet < spmdlang.AbstractResourceSet
    methods ( Access = public, Hidden )
        
        function tf  = isValid( ~ )
            tf = true;
        end
        
        function tf  = canAccessWorkers( ~ )
            tf = true;
        end

        function obj = TrivialResourceSet()
            obj@spmdlang.AbstractResourceSet( 1 );
        end
        
        function tf = satisfiesConstraints( ~, minN, ~ )
        % Trivial resource set only satisfies constraints if minN is zero
            tf = ( minN == 0 );
        end
        
        function blockEx = buildBlockExecutor( obj, bodyF, assignOutF, getOutF, unpackInF, initialOuts )
            blockEx = spmdlang.LocalSpmdExecutor( spmdlang.ResourceSetHolder( obj ), ...
                                                 bodyF, assignOutF, getOutF, unpackInF, initialOuts );
        end
        
        function val = getFromWorker( ~, workerIdx, key )
            if workerIdx ~= 1
                error(message('parallel:lang:spmd:TrivialResourceUnexpectedGetIdx', workerIdx));
            end
            val = spmdlang.ValueStore.retrieve( key );
            % Ensure that hidden Composites are turned into broken
            % Composites. Serializing and de-serializing in this way may be
            % somewhat inefficient, but it's the only way to ensure
            % consistency between local and remote operation.
            val = parallel.internal.pool.mxSerializeDeserialize( val );
        end
        
        function newKey = setOnWorker( ~, workerIdx, newValue )
            if workerIdx ~= 1
                error(message('parallel:lang:spmd:TrivialResourceUnexpectedSetIdx', workerIdx));
            end
            newKey = spmdlang.ValueStore.store( newValue );
        end
        
        function keyUnreferenced( ~, workerIdx, key )
            if workerIdx ~= 1
                error(message('parallel:lang:spmd:TrivialResourceUnexpectedKeyIdx', workerIdx));
            end
            spmdlang.ValueStore.remove( key );
        end
        
        function forceClear( ~ )
            spmdlang.ValueStore.clear( );
        end
        
        function tf = shouldRetryIfFileNotFound( ~ )
        % Never retry the computation a file not found error is thrown by the resource set.
            tf = false;
        end
    end
end
