function currentFileStore = getCurrentFileStore
% getCurrentFileStore Get the store of files for the current job.
%
%    store = getCurrentFileStore returns a reference to the FileStore
%    object for the current job or pool.
%
%    If the function is executed in a MATLAB session that is not a worker,
%    you get an empty result.
%
%    Example:
%    store = getCurrentFileStore;
%    store.copyFileToStore("/user/workerResult.csv", "myResult");
%
% See also parallel.FileStore, getCurrentValueStore

%  Copyright 2021-2023 The MathWorks, Inc.

if parallel.internal.pool.isPoolThreadWorker
    try
        % Throws if FileStore not supported for this worker.
        currentFileStore = getCurrentStoreForThreadWorker();
    catch E
        throw(E);
    end
    return;
end

job = getCurrentJob();
if isempty(job)
    currentFileStore = [];
else
    currentFileStore = job.FileStore;
end
end

function store = getCurrentStoreForThreadWorker()
import parallel.internal.pool.ThreadsKeyValueStoreAdapter
store = ThreadsKeyValueStoreAdapter.getWorkerInstance().getFileStore();
end


