%PartitionMetadata
% An object that represents how a partitioned array has been partitioned.

%   Copyright 2016-2020 The MathWorks, Inc.

classdef PartitionMetadata < handle
    properties (SetAccess = immutable)
        % The underlying partition strategy object that defines how the
        % execution environment will partition evaluation.
        Strategy;
    end
    
    methods
        function obj = PartitionMetadata(strategy)
            % Create a partition metadata object from the given strategy
            % object. The strategy object can be either:
            %  - A PartitionStrategy object
            %  - A datastore
            %  - The desired number of partitions
            %  - Empty, which indicates arbitrary partitioning.
            
            import matlab.bigdata.internal.executor.PartitionStrategy;
            if ~isa(strategy, 'matlab.bigdata.internal.executor.PartitionStrategy')
                strategy = matlab.bigdata.internal.executor.PartitionStrategy.create(strategy);
            end
            obj.Strategy = strategy;
        end
        
        function obj = vertcatPartitionMetadata(varargin)
            % Build partition metadata that represents the vertical
            % concatenation of all partitions across all input partition
            % strategies.
            import matlab.bigdata.internal.executor.PartitionStrategy;
            import matlab.bigdata.internal.PartitionMetadata;
            allPartitionMetadata = vertcat(PartitionMetadata.empty(), varargin{:});
            strategy = PartitionStrategy.vertcatPartitionMetadata(allPartitionMetadata.Strategy);
            obj = PartitionMetadata(strategy);
        end
        
    end
    
    methods (Static)
        function obj = align(varargin)
            % Align several partition metadata objects to form one that will
            % work for all the partitioned arrays.
            import matlab.bigdata.internal.executor.PartitionStrategy;
            import matlab.bigdata.internal.PartitionMetadata;
            allPartitionMetadata = vertcat(PartitionMetadata.empty(), varargin{:});
            strategy = PartitionStrategy.align(allPartitionMetadata.Strategy);
            obj = PartitionMetadata(strategy);
        end
    end
end
