From simon.urbanek at r-project.org  Sun Nov 17 04:09:48 2013
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Sat, 16 Nov 2013 22:09:48 -0500
Subject: [Rd] serialization for external pointers
In-Reply-To: <5287736A.6000407@r-enthusiasts.com>
References: <5287736A.6000407@r-enthusiasts.com>
Message-ID: <865E17BF-9ABD-47D7-ADC4-1D4384BC09FC@r-project.org>

On Nov 16, 2013, at 8:30 AM, Romain Francois <romain at r-enthusiasts.com> wrote:

> Hello,
> 
> Are there any recipe to handle serialization / deserialization of external pointers.
> 
> I'm thinking about something similar in spirit to the way we handle finalization of external pointers.
> 

See refhook in serialize/unserialize.


> Currently, if we create an external pointer, save the session, quit R, then load the session, we get a null pointer.
> 

Saving the session is a different story than serialization, because it also involves loading the right code in addition to the data and you cannot use refhooks. I recall discussing this with Luke a few years ago, and the main problem is that R has no way of knowing what is needed to call the particular hooks, because the package is not even loaded, so it cannot install hooks. AFAIR the net result was that the proper way to do it is to handle the NULL pointer code when you first access the pointer  to restore the object based on additional info that you store in the object. E.g. , in rJava we store the Java serialization of the object so it can be restored on first use of the new session.

One piece that is still missing it the ability to set a hook to update the save object on save() - since we don?t necessarily want to add the extra information every time the object is created or the serialization may become stale over time. That would still be very useful to add ?

Cheers,
Simon


> One way I'm thinking of is to have an environment in the "protected" part of the xp, then have an active binding there, since apparently active bindings:
> - are "get" during serialization
> - lose their active ness when reloaded:
> 
> $ R
> [...]
> > f <- local( {
> +     x <- 1
> +     function(v) {
> +        if (missing(v))
> +            cat("get\n")
> +        else {
> +            cat("set\n")
> +            x <<- v
> +        }
> +        x
> +     }
> + })
> > makeActiveBinding("fred", f, .GlobalEnv)
> > bindingIsActive("fred", .GlobalEnv)
> [1] TRUE
> >
> > q("yes")
> get
> get
> 
> 
> romain at naxos /tmp $ R
> [..]
> > fred
> [1] 1
> > bindingIsActive("fred", .GlobalEnv)
> [1] FALSE
> 
> Is this possible ? Is there any other hook to handle serialization, unserialization of external pointers ?
> 
> Romain
> 
> -- 
> Romain Francois
> Professional R Enthusiast
> +33(0) 6 28 91 30 30
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 



[Rd] serialization for external pointers
Simon Urbanek