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

    function info = getArrayInfo(t)
%getArrayInfo Retrieve information about tall array.
%   S = getArrayInfo(T) returns in S a struct describing what is known and what
%   is not known about tall array T. (It is an error if T is not tall).
%
%   The fields of S are:
%   'Class'    - the underlying type of T, or '' if not known
%   'Ndims'    - the number of dimensions of T, or NaN if not known
%   'Size'     - the underlying size of T. If 'Ndims' is NaN, this will be empty,
%                otherwise it is a vector of length Ndims. Some elements will be
%                NaN if they are not known.
%   'Gathered' - logical scalar indicating whether the value has already been
%                gathered. When 'Gathered' is TRUE, this implies that calling
%                GATHER is "free".
%   'IsPreviewAvailable' - logical scalar indicating whether 'PreviewData' is valid
%   'PreviewData' - the actual preview data
%   'IsPreviewTruncated' - whether the preview data has been truncated
%   'Error'    - if an error was encountered attempting to gather information, the
%                relevant MException is here. This error might well indicate that an
%                error would be thrown during GATHER.

% Copyright 2016 The MathWorks, Inc.

assert(istall(t), 'getArrayInfo is valid only for tall arrays.');

s = struct('Class', '', ...
    'Ndims', NaN, ...
    'Size', [], ...
    'Gathered', false, ...
    'IsPreviewAvailable', false, ...
    'PreviewData', [], ...
    'IsPreviewTruncated', true, ...
    'Error', MException.empty());
try
    info = iGatherInfo(s, t);
catch E
    info = s;
    info.Error = E;
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Function to actually gather the information
function s = iGatherInfo(s, t)

partitionedArray = hGetValueImpl(t);
s.Gathered = matlab.bigdata.internal.util.isGathered(partitionedArray);

numPreviewRows = matlab.bigdata.internal.util.defaultHeadTailRows(); % Number of rows to preview

if s.Gathered
    % The Adaptor doesn't get updated when things are gathered, but we can simply
    % query the underlying value.
    value = partitionedArray.ValueFuture.Value;
    isDataFullSize = true;
    s = iUpdateFromPreviewData(s, numPreviewRows, value, isDataFullSize, t);
elseif hasCachedPreviewData(partitionedArray)
    [previewData, isTruncated] = getCachedPreviewData(partitionedArray);
    isDataFullSize = ~isTruncated;
    s = iUpdateFromPreviewData(s, numPreviewRows, previewData, isDataFullSize, t);
elseif matlab.bigdata.internal.util.isPreviewCheap(partitionedArray)
    % Preview is cheap - use this to get better size information to match what the
    % object display can show
    cheapPreviewGuard = matlab.bigdata.internal.lazyeval.CheapPreviewGuard(); %#ok
    previewData = gather(matlab.bigdata.internal.lazyeval.extractHead(partitionedArray, numPreviewRows+1));
    % Under certain circumstances, the act of previewing can cause the
    % array to become gathered - in which case, we switch over to using the
    % gathered data.
    if matlab.bigdata.internal.util.isGathered(partitionedArray)
        s.Gathered = true;
        previewData = partitionedArray.ValueFuture.Value;
    end
    
    isDataFullSize = s.Gathered || size(previewData, 1) <= numPreviewRows;
    
    s = iUpdateFromPreviewData(s, numPreviewRows, previewData, isDataFullSize, t);
    
    if ~s.Gathered
        setCachedPreviewData(partitionedArray, s.PreviewData, s.IsPreviewTruncated);
    end
else
    adaptor = hGetAdaptor(t);
    s.Class = adaptor.Class;
    s.Ndims = adaptor.NDims;
    s.Size  = adaptor.Size;
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [data, truncated] = iTruncate(data, szInDim1)
truncated = size(data, 1) > szInDim1;
if truncated
    shouldBeComplex = isnumeric(data) && ~isreal(data);
    colonSubscripts = repmat({':'}, 1, ndims(data) - 1);
    data = data(1:szInDim1, colonSubscripts{:});
    if shouldBeComplex && isreal(data)
        data = complex(data);
    end
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function sz1 = iDeriveSize1FromAdaptor(t)
adaptor = hGetAdaptor(t);
if ~isnan(adaptor.NDims)
    sz1 = adaptor.Size(1);
else
    sz1 = NaN;
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function s = iUpdateFromPreviewData(s, numPreviewRows, previewData, isDataFullSize, tallArray)

s.IsPreviewAvailable = true;

s.Class = class(previewData);
s.Ndims = ndims(previewData);
s.Size  = size(previewData);

[s.PreviewData, s.IsPreviewTruncated] = iTruncate(previewData, numPreviewRows);

if ~isDataFullSize
    s.Size(1) = iDeriveSize1FromAdaptor(tallArray);
    % If the data is not full-size, it must be truncated.
    s.IsPreviewTruncated = true;
end
end