You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@subversion.apache.org by cm...@apache.org on 2012/05/08 17:06:18 UTC
svn commit: r1335566 - in /subversion/trunk/subversion: include/svn_io.h
libsvn_subr/stream.c
Author: cmpilato
Date: Tue May 8 15:06:18 2012
New Revision: 1335566
URL: http://svn.apache.org/viewvc?rev=1335566&view=rev
Log:
Introduce a generic (and as-yet-unused) lazy-open wrapper stream.
* subversion/include/svn_io.h
(svn_stream_lazyopen_func_t): New callback function type.
(svn_stream_lazyopen_create): New function.
* subversion/libsvn_subr/stream.c
(lazyopen_baton_t): New private baton type.
(lazyopen_if_unopened, read_handler_lazyopen, skip_handler_lazyopen,
write_handler_lazyopen, close_handler_lazyopen,
mark_handler_lazyopen, seek_handler_lazyopen): New helper
functions, machinery for...
(svn_stream_lazyopen_create): ... this new API implemention.
Suggested by: gstein
Modified:
subversion/trunk/subversion/include/svn_io.h
subversion/trunk/subversion/libsvn_subr/stream.c
Modified: subversion/trunk/subversion/include/svn_io.h
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/include/svn_io.h?rev=1335566&r1=1335565&r2=1335566&view=diff
==============================================================================
--- subversion/trunk/subversion/include/svn_io.h (original)
+++ subversion/trunk/subversion/include/svn_io.h Tue May 8 15:06:18 2012
@@ -1329,6 +1329,34 @@ svn_string_from_stream(svn_string_t **re
apr_pool_t *scratch_pool);
+/** A function type provided for use as a callback from
+ * @c svn_stream_lazyopen_create().
+ *
+ * @since New in 1.8.
+ */
+typedef svn_error_t *
+(*svn_stream_lazyopen_func_t)(svn_stream_t **stream,
+ void *baton,
+ apr_pool_t *result_pool,
+ apr_pool_t *scratch_pool);
+
+
+/** Set @a *stream to a generic stream which wraps another primary
+ * stream, delaying the "opening" of that stream until the first time
+ * the stream is accessed.
+ *
+ * @a open_func and @a open_baton are a callback function/baton pair
+ * invoked upon the first read of @a *stream which are used to open the
+ * "real" source stream.
+ *
+ * @since New in 1.8.
+ */
+svn_error_t *
+svn_stream_lazyopen_create(svn_stream_t **stream,
+ svn_stream_lazyopen_func_t open_func,
+ void *open_baton,
+ apr_pool_t *result_pool);
+
/** @} */
/** Set @a *result to a string containing the contents of @a
Modified: subversion/trunk/subversion/libsvn_subr/stream.c
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_subr/stream.c?rev=1335566&r1=1335565&r2=1335566&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_subr/stream.c (original)
+++ subversion/trunk/subversion/libsvn_subr/stream.c Tue May 8 15:06:18 2012
@@ -1659,3 +1659,148 @@ svn_stream_buffered(apr_pool_t *result_p
return svn_stream__from_spillbuf(BUFFER_BLOCK_SIZE, BUFFER_MAX_SIZE,
result_pool);
}
+
+
+
+/*** Lazyopen Streams ***/
+
+/* Custom baton for lazyopen-style wrapper streams. */
+typedef struct lazyopen_baton_t {
+
+ /* Callback function and baton for opening the wrapped stream. */
+ svn_stream_lazyopen_func_t open_func;
+ void *open_baton;
+
+ /* The wrapped stream, or NULL if the stream hasn't yet been
+ opened. */
+ svn_stream_t *real_stream;
+ apr_pool_t *pool;
+
+} lazyopen_baton_t;
+
+
+/* Use B->open_func/baton to create and set B->real_stream iff it
+ isn't already set. */
+static svn_error_t *
+lazyopen_if_unopened(lazyopen_baton_t *b)
+{
+ if (b->real_stream == NULL)
+ {
+ svn_stream_t *stream;
+ apr_pool_t *scratch_pool = svn_pool_create(b->pool);
+
+ SVN_ERR(b->open_func(&stream, b->open_baton,
+ b->pool, scratch_pool));
+
+ svn_pool_destroy(scratch_pool);
+
+ b->real_stream = stream;
+ }
+
+ return SVN_NO_ERROR;
+}
+
+/* Implements svn_read_fn_t */
+static svn_error_t *
+read_handler_lazyopen(void *baton,
+ char *buffer,
+ apr_size_t *len)
+{
+ lazyopen_baton_t *b = baton;
+
+ SVN_ERR(lazyopen_if_unopened(b));
+ SVN_ERR(svn_stream_read(b->real_stream, buffer, len));
+
+ return SVN_NO_ERROR;
+}
+
+/* Implements svn_stream_skip_fn_t */
+static svn_error_t *
+skip_handler_lazyopen(void *baton,
+ apr_size_t len)
+{
+ lazyopen_baton_t *b = baton;
+
+ SVN_ERR(lazyopen_if_unopened(b));
+ SVN_ERR(svn_stream_skip(b->real_stream, len));
+
+ return SVN_NO_ERROR;
+}
+
+/* Implements svn_write_fn_t */
+static svn_error_t *
+write_handler_lazyopen(void *baton,
+ const char *data,
+ apr_size_t *len)
+{
+ lazyopen_baton_t *b = baton;
+
+ SVN_ERR(lazyopen_if_unopened(b));
+ SVN_ERR(svn_stream_write(b->real_stream, data, len));
+
+ return SVN_NO_ERROR;
+}
+
+/* Implements svn_close_fn_t */
+static svn_error_t *
+close_handler_lazyopen(void *baton)
+{
+ lazyopen_baton_t *b = baton;
+
+ SVN_ERR(lazyopen_if_unopened(b));
+ SVN_ERR(svn_stream_close(b->real_stream));
+
+ return SVN_NO_ERROR;
+}
+
+/* Implements svn_stream_mark_fn_t */
+static svn_error_t *
+mark_handler_lazyopen(void *baton,
+ svn_stream_mark_t **mark,
+ apr_pool_t *pool)
+{
+ lazyopen_baton_t *b = baton;
+
+ SVN_ERR(lazyopen_if_unopened(b));
+ SVN_ERR(svn_stream_mark(b->real_stream, mark, pool));
+
+ return SVN_NO_ERROR;
+}
+
+/* Implements svn_stream_seek_fn_t */
+static svn_error_t *
+seek_handler_lazyopen(void *baton,
+ const svn_stream_mark_t *mark)
+{
+ lazyopen_baton_t *b = baton;
+
+ SVN_ERR(lazyopen_if_unopened(b));
+ SVN_ERR(svn_stream_seek(b->real_stream, mark));
+
+ return SVN_NO_ERROR;
+}
+
+/* Implements svn_stream_stream_fn_t */
+svn_error_t *
+svn_stream_lazyopen_create(svn_stream_t **stream,
+ svn_stream_lazyopen_func_t open_func,
+ void *open_baton,
+ apr_pool_t *result_pool)
+{
+ lazyopen_baton_t *lob = apr_pcalloc(result_pool, sizeof(*lob));
+
+ lob->open_func = open_func;
+ lob->open_baton = open_baton;
+ lob->real_stream = NULL;
+ lob->pool = result_pool;
+
+ *stream = svn_stream_create(lob, result_pool);
+ svn_stream_set_read(*stream, read_handler_lazyopen);
+ svn_stream_set_skip(*stream, skip_handler_lazyopen);
+ svn_stream_set_write(*stream, write_handler_lazyopen);
+ svn_stream_set_close(*stream, close_handler_lazyopen);
+ svn_stream_set_mark(*stream, mark_handler_lazyopen);
+ svn_stream_set_seek(*stream, seek_handler_lazyopen);
+
+ return SVN_NO_ERROR;
+}
Re: svn commit: r1335566 - in /subversion/trunk/subversion:
include/svn_io.h libsvn_subr/stream.c
Posted by Ivan Zhakov <iv...@visualsvn.com>.
On Tue, May 8, 2012 at 11:10 PM, C. Michael Pilato <cm...@collab.net> wrote:
> On 05/08/2012 02:20 PM, Ivan Zhakov wrote:
>>> +/* Implements svn_close_fn_t */
>>> +static svn_error_t *
>>> +close_handler_lazyopen(void *baton)
>>> +{
>>> + lazyopen_baton_t *b = baton;
>>> +
>>> + SVN_ERR(lazyopen_if_unopened(b));
>>> + SVN_ERR(svn_stream_close(b->real_stream));
>>
>> I think we should just no-op in case if wrapped stream is not opened. I meant:
>> [[[
>> if (opened)
>> {
>> SVN_ERR(svn_stream_close(b->real_stream));
>> }
>> return SVN_NO_ERROR;
>> ]]]
>
> I'm 50/50 on this change of behavior, but you clearly have an opinion, so
> I've changed (and documented) it. See r1335704.
>
Thanks Mike!
--
Ivan Zhakov
Re: svn commit: r1335566 - in /subversion/trunk/subversion: include/svn_io.h
libsvn_subr/stream.c
Posted by "C. Michael Pilato" <cm...@collab.net>.
On 05/08/2012 02:20 PM, Ivan Zhakov wrote:
>> +/* Implements svn_close_fn_t */
>> +static svn_error_t *
>> +close_handler_lazyopen(void *baton)
>> +{
>> + lazyopen_baton_t *b = baton;
>> +
>> + SVN_ERR(lazyopen_if_unopened(b));
>> + SVN_ERR(svn_stream_close(b->real_stream));
>
> I think we should just no-op in case if wrapped stream is not opened. I meant:
> [[[
> if (opened)
> {
> SVN_ERR(svn_stream_close(b->real_stream));
> }
> return SVN_NO_ERROR;
> ]]]
I'm 50/50 on this change of behavior, but you clearly have an opinion, so
I've changed (and documented) it. See r1335704.
--
C. Michael Pilato <cm...@collab.net>
CollabNet <> www.collab.net <> Enterprise Cloud Development
Re: svn commit: r1335566 - in /subversion/trunk/subversion:
include/svn_io.h libsvn_subr/stream.c
Posted by Ivan Zhakov <iv...@visualsvn.com>.
On Tue, May 8, 2012 at 7:06 PM, <cm...@apache.org> wrote:
> Author: cmpilato
> Date: Tue May 8 15:06:18 2012
> New Revision: 1335566
>
> URL: http://svn.apache.org/viewvc?rev=1335566&view=rev
> Log:
> Introduce a generic (and as-yet-unused) lazy-open wrapper stream.
>
> * subversion/include/svn_io.h
> (svn_stream_lazyopen_func_t): New callback function type.
> (svn_stream_lazyopen_create): New function.
>
> * subversion/libsvn_subr/stream.c
> (lazyopen_baton_t): New private baton type.
> (lazyopen_if_unopened, read_handler_lazyopen, skip_handler_lazyopen,
> write_handler_lazyopen, close_handler_lazyopen,
> mark_handler_lazyopen, seek_handler_lazyopen): New helper
> functions, machinery for...
> (svn_stream_lazyopen_create): ... this new API implemention.
>
> Suggested by: gstein
>
[...]
> +/* Implements svn_close_fn_t */
> +static svn_error_t *
> +close_handler_lazyopen(void *baton)
> +{
> + lazyopen_baton_t *b = baton;
> +
> + SVN_ERR(lazyopen_if_unopened(b));
> + SVN_ERR(svn_stream_close(b->real_stream));
I think we should just no-op in case if wrapped stream is not opened. I meant:
[[[
if (opened)
{
SVN_ERR(svn_stream_close(b->real_stream));
}
return SVN_NO_ERROR;
]]]
[...]
--
Ivan Zhakov
Re: svn commit: r1335566 - in /subversion/trunk/subversion:
include/svn_io.h libsvn_subr/stream.c
Posted by Greg Stein <gs...@gmail.com>.
On Tue, May 8, 2012 at 2:38 PM, C. Michael Pilato <cm...@collab.net> wrote:
> On 05/08/2012 02:25 PM, Greg Stein wrote:
>...
>> Per my suggestion on IRC, I don't see how this function can ever
>> error, or ever need a scratch_pool (no matter how we might
>> extend/upgrade it). I'd suggest updating the signature accordingly.
>
> I removed the scratch_pool from svn_stream_lazyopen_create() already, but
> forgot about the "don't return an error" bit. Will fix.
>
> I *did* preserve the scratchpool in the open_func() callback, though, and
> the one implementation thereof uses it to pass to
> svn_wc__db_pristine_read(). But if you think that's unnecessary too, I can
> pull it.
Nope. I completely agree with your design on that part!
Cheers,
-g
Re: svn commit: r1335566 - in /subversion/trunk/subversion: include/svn_io.h
libsvn_subr/stream.c
Posted by "C. Michael Pilato" <cm...@collab.net>.
On 05/08/2012 02:25 PM, Greg Stein wrote:
> On Tue, May 8, 2012 at 11:06 AM, <cm...@apache.org> wrote:
>> ...
>> +++ subversion/trunk/subversion/libsvn_subr/stream.c Tue May 8 15:06:18 2012
>> ...
>> +/* Implements svn_stream_stream_fn_t */
>> +svn_error_t *
>> +svn_stream_lazyopen_create(svn_stream_t **stream,
>> + svn_stream_lazyopen_func_t open_func,
>> + void *open_baton,
>> + apr_pool_t *result_pool)
>
> Implements what?! That comment seems like a copy/paste-o.
Well, sorta. More like a poorly composed emacs macro. :-)
>> +{
>> + lazyopen_baton_t *lob = apr_pcalloc(result_pool, sizeof(*lob));
>> +
>> + lob->open_func = open_func;
>> + lob->open_baton = open_baton;
>> + lob->real_stream = NULL;
>> + lob->pool = result_pool;
>> +
>> + *stream = svn_stream_create(lob, result_pool);
>> + svn_stream_set_read(*stream, read_handler_lazyopen);
>> + svn_stream_set_skip(*stream, skip_handler_lazyopen);
>> + svn_stream_set_write(*stream, write_handler_lazyopen);
>> + svn_stream_set_close(*stream, close_handler_lazyopen);
>> + svn_stream_set_mark(*stream, mark_handler_lazyopen);
>> + svn_stream_set_seek(*stream, seek_handler_lazyopen);
>> +
>> + return SVN_NO_ERROR;
>> +}
>
> Per my suggestion on IRC, I don't see how this function can ever
> error, or ever need a scratch_pool (no matter how we might
> extend/upgrade it). I'd suggest updating the signature accordingly.
I removed the scratch_pool from svn_stream_lazyopen_create() already, but
forgot about the "don't return an error" bit. Will fix.
I *did* preserve the scratchpool in the open_func() callback, though, and
the one implementation thereof uses it to pass to
svn_wc__db_pristine_read(). But if you think that's unnecessary too, I can
pull it.
--
C. Michael Pilato <cm...@collab.net>
CollabNet <> www.collab.net <> Enterprise Cloud Development
Re: svn commit: r1335566 - in /subversion/trunk/subversion:
include/svn_io.h libsvn_subr/stream.c
Posted by Greg Stein <gs...@gmail.com>.
On Tue, May 8, 2012 at 11:06 AM, <cm...@apache.org> wrote:
>...
> +++ subversion/trunk/subversion/libsvn_subr/stream.c Tue May 8 15:06:18 2012
>...
> +/* Implements svn_stream_stream_fn_t */
> +svn_error_t *
> +svn_stream_lazyopen_create(svn_stream_t **stream,
> + svn_stream_lazyopen_func_t open_func,
> + void *open_baton,
> + apr_pool_t *result_pool)
Implements what?! That comment seems like a copy/paste-o.
> +{
> + lazyopen_baton_t *lob = apr_pcalloc(result_pool, sizeof(*lob));
> +
> + lob->open_func = open_func;
> + lob->open_baton = open_baton;
> + lob->real_stream = NULL;
> + lob->pool = result_pool;
> +
> + *stream = svn_stream_create(lob, result_pool);
> + svn_stream_set_read(*stream, read_handler_lazyopen);
> + svn_stream_set_skip(*stream, skip_handler_lazyopen);
> + svn_stream_set_write(*stream, write_handler_lazyopen);
> + svn_stream_set_close(*stream, close_handler_lazyopen);
> + svn_stream_set_mark(*stream, mark_handler_lazyopen);
> + svn_stream_set_seek(*stream, seek_handler_lazyopen);
> +
> + return SVN_NO_ERROR;
> +}
Per my suggestion on IRC, I don't see how this function can ever
error, or ever need a scratch_pool (no matter how we might
extend/upgrade it). I'd suggest updating the signature accordingly.
Cheers,
-g