%Rdd
% Wrapper around an RDD ID that ensures cleanup of the remote RDD resource
% once this object falls out of scope.
%

%   Copyright 2016 The MathWorks, Inc.

classdef (Sealed) Rdd < handle
    properties (SetAccess = immutable)
        % The ID for the RDD.
        Id;
        
        % The underlying communication object for accessing Spark.
        DriverClient;
    end
    
    methods
        % The main constructor.
        function obj = Rdd(id, driverClient)
            obj.Id = id;
            obj.DriverClient = driverClient;
        end

        % Cleanup the RDD on deletion.
        function delete(obj)
            if obj.DriverClient.isValid()
                import com.mathworks.toolbox.parallel.mapreduce.sparkdriver.messages.RemoveRddRequest;
                
                obj.DriverClient.removeRdd(RemoveRddRequest() ...
                    .withRddId(obj.Id));
            end
        end
    end
end
