You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@subversion.apache.org by st...@apache.org on 2013/10/15 07:29:39 UTC

svn commit: r1532193 - in /subversion/trunk/subversion: include/svn_io.h libsvn_fs_fs/cached_data.c libsvn_subr/stream.c tests/libsvn_subr/stream-test.c

Author: stefan2
Date: Tue Oct 15 05:29:39 2013
New Revision: 1532193

URL: http://svn.apache.org/r1532193
Log:
Introduce svn_stringbuf_from_stream() as symmetric counterpart to
svn_stream_from_stringbuf() and use it in one place.

* subversion/include/svn_io.h
  (svn_stringbuf_from_stream): declare new public API function

* subversion/libsvn_subr/stream.c
  (svn_stringbuf_from_stream): implement new API function

* subversion/libsvn_fs_fs/cached_data.c
  (get_dir_contents): use the new API

* develop/trunk/subversion/tests/libsvn_subr/stream-test.c
  (test_stringbuf_from_stream): new test case for new API
  (test_funcs): register new test case

Modified:
    subversion/trunk/subversion/include/svn_io.h
    subversion/trunk/subversion/libsvn_fs_fs/cached_data.c
    subversion/trunk/subversion/libsvn_subr/stream.c
    subversion/trunk/subversion/tests/libsvn_subr/stream-test.c

Modified: subversion/trunk/subversion/include/svn_io.h
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/include/svn_io.h?rev=1532193&r1=1532192&r2=1532193&view=diff
==============================================================================
--- subversion/trunk/subversion/include/svn_io.h (original)
+++ subversion/trunk/subversion/include/svn_io.h Tue Oct 15 05:29:39 2013
@@ -1070,6 +1070,20 @@ svn_error_t *
 svn_stream_for_stdout(svn_stream_t **out,
                       apr_pool_t *pool);
 
+/** Set @a *str to a string buffer allocated in @a pool that contains all
+ * data from the current position in @a stream to its end.  @a len_hint
+ * specifies the initial capacity of the string buffer and may be 0.  The
+ * buffer gets automatically resized to fit the actual amount of data being
+ * read from @a stream.
+ *
+ * @since New in 1.9.
+ */
+svn_error_t *
+svn_stringbuf_from_stream(svn_stringbuf_t **str,
+                          svn_stream_t *stream,
+                          apr_size_t len_hint,
+                          apr_pool_t *pool);
+
 /** Return a generic stream connected to stringbuf @a str.  Allocate the
  * stream in @a pool.
  */

Modified: subversion/trunk/subversion/libsvn_fs_fs/cached_data.c
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_fs_fs/cached_data.c?rev=1532193&r1=1532192&r2=1532193&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_fs_fs/cached_data.c (original)
+++ subversion/trunk/subversion/libsvn_fs_fs/cached_data.c Tue Oct 15 05:29:39 2013
@@ -1751,12 +1751,12 @@ get_dir_contents(apr_hash_t *entries,
       apr_size_t len = noderev->data_rep->expanded_size
                      ? (apr_size_t)noderev->data_rep->expanded_size
                      : (apr_size_t)noderev->data_rep->size;
-      svn_stringbuf_t *text = svn_stringbuf_create_ensure(len, text_pool);
-      text->len = len;
+      svn_stringbuf_t *text;
 
       /* The representation is immutable.  Read it normally. */
-      SVN_ERR(svn_fs_fs__get_contents(&contents, fs, noderev->data_rep, text_pool));
-      SVN_ERR(svn_stream_read(contents, text->data, &text->len));
+      SVN_ERR(svn_fs_fs__get_contents(&contents, fs, noderev->data_rep,
+                                      text_pool));
+      SVN_ERR(svn_stringbuf_from_stream(&text, contents, len, text_pool));
       SVN_ERR(svn_stream_close(contents));
 
       /* de-serialize hash */

Modified: subversion/trunk/subversion/libsvn_subr/stream.c
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_subr/stream.c?rev=1532193&r1=1532192&r2=1532193&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_subr/stream.c (original)
+++ subversion/trunk/subversion/libsvn_subr/stream.c Tue Oct 15 05:29:39 2013
@@ -1379,6 +1379,36 @@ svn_stream_checksummed(svn_stream_t *str
 
 
 /* Miscellaneous stream functions. */
+
+svn_error_t *
+svn_stringbuf_from_stream(svn_stringbuf_t **str,
+                          svn_stream_t *stream,
+                          apr_size_t len_hint,
+                          apr_pool_t *pool)
+{
+#define MIN_READ_SIZE 64
+
+  apr_size_t to_read = 0;
+  svn_stringbuf_t *text
+    = svn_stringbuf_create_ensure(len_hint ? len_hint : MIN_READ_SIZE, pool);
+
+  do
+    {
+      to_read = text->blocksize - 1 - text->len;
+      SVN_ERR(svn_stream_read(stream, text->data + text->len, &to_read));
+      text->len += to_read;
+
+      if (to_read && text->blocksize < text->len + MIN_READ_SIZE)
+        svn_stringbuf_ensure(text, text->blocksize * 2);
+    }
+  while (to_read);
+
+  text->data[text->len] = '\0';
+  *str = text;
+
+  return SVN_NO_ERROR;
+}
+
 struct stringbuf_stream_baton
 {
   svn_stringbuf_t *str;

Modified: subversion/trunk/subversion/tests/libsvn_subr/stream-test.c
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/tests/libsvn_subr/stream-test.c?rev=1532193&r1=1532192&r2=1532193&view=diff
==============================================================================
--- subversion/trunk/subversion/tests/libsvn_subr/stream-test.c (original)
+++ subversion/trunk/subversion/tests/libsvn_subr/stream-test.c Tue Oct 15 05:29:39 2013
@@ -727,6 +727,50 @@ test_stream_base64_2(apr_pool_t *pool)
   return SVN_NO_ERROR;
 }
 
+static svn_error_t *
+test_stringbuf_from_stream(apr_pool_t *pool)
+{
+  const char *test_cases[] =
+    {
+      "",
+      "x",
+      "this string is longer than the default 64 minimum block size used"
+      "by the function under test",
+      NULL
+    };
+
+  const char **test_case;
+  for (test_case = test_cases; *test_case; ++test_case)
+    {
+      svn_stringbuf_t *result1, *result2, *result3, *result4;
+      svn_stringbuf_t *original = svn_stringbuf_create(*test_case, pool);
+
+      svn_stream_t *stream1 = svn_stream_from_stringbuf(original, pool);
+      svn_stream_t *stream2 = svn_stream_from_stringbuf(original, pool);
+
+      SVN_ERR(svn_stringbuf_from_stream(&result1, stream1, 0, pool));
+      SVN_ERR(svn_stringbuf_from_stream(&result2, stream1, 0, pool));
+      SVN_ERR(svn_stringbuf_from_stream(&result3, stream2, original->len,
+                                        pool));
+      SVN_ERR(svn_stringbuf_from_stream(&result4, stream2, original->len,
+                                        pool));
+
+      /* C-string contents must match */
+      SVN_TEST_STRING_ASSERT(result1->data, original->data);
+      SVN_TEST_STRING_ASSERT(result2->data, "");
+      SVN_TEST_STRING_ASSERT(result3->data, original->data);
+      SVN_TEST_STRING_ASSERT(result4->data, "");
+
+      /* assumed length must match */
+      SVN_TEST_ASSERT(result1->len == original->len);
+      SVN_TEST_ASSERT(result2->len == 0);
+      SVN_TEST_ASSERT(result3->len == original->len);
+      SVN_TEST_ASSERT(result4->len == 0);
+    }
+
+  return SVN_NO_ERROR;
+}
+
 /* The test table.  */
 
 struct svn_test_descriptor_t test_funcs[] =
@@ -752,5 +796,7 @@ struct svn_test_descriptor_t test_funcs[
                    "test base64 encoding/decoding streams"),
     SVN_TEST_PASS2(test_stream_base64_2,
                    "base64 decoding allocation problem"),
+    SVN_TEST_PASS2(test_stringbuf_from_stream,
+                   "test svn_stringbuf_from_stream"),
     SVN_TEST_NULL
   };



Re: svn commit: r1532193 - in /subversion/trunk/subversion: include/svn_io.h libsvn_fs_fs/cached_data.c libsvn_subr/stream.c tests/libsvn_subr/stream-test.c

Posted by Stefan Fuhrmann <st...@wandisco.com>.
On Tue, Oct 15, 2013 at 7:34 AM, Branko Čibej <br...@wandisco.com> wrote:

> On 15.10.2013 07:29, stefan2@apache.org wrote:
> > Author: stefan2
> > Date: Tue Oct 15 05:29:39 2013
> > New Revision: 1532193
> >
> > URL: http://svn.apache.org/r1532193
> > Log:
> > Introduce svn_stringbuf_from_stream() as symmetric counterpart to
> > svn_stream_from_stringbuf() and use it in one place.
>
> On general principles, I'd prefer not adding a public API if the
> implementation happens to be used in only one place in our code. It
> would be better IMO to make this a private function, and only expose it
> to the public if we find a real need for it. We're allowed to use
> private APIs between our own libraries, and our public API is already
> far too verbose.
>

There will be more usages before I leave for London.

-- Stefan^2.

Re: svn commit: r1532193 - in /subversion/trunk/subversion: include/svn_io.h libsvn_fs_fs/cached_data.c libsvn_subr/stream.c tests/libsvn_subr/stream-test.c

Posted by Branko Čibej <br...@wandisco.com>.
On 15.10.2013 07:29, stefan2@apache.org wrote:
> Author: stefan2
> Date: Tue Oct 15 05:29:39 2013
> New Revision: 1532193
>
> URL: http://svn.apache.org/r1532193
> Log:
> Introduce svn_stringbuf_from_stream() as symmetric counterpart to
> svn_stream_from_stringbuf() and use it in one place.

On general principles, I'd prefer not adding a public API if the
implementation happens to be used in only one place in our code. It
would be better IMO to make this a private function, and only expose it
to the public if we find a real need for it. We're allowed to use
private APIs between our own libraries, and our public API is already
far too verbose.

-- Brane


-- 
Branko Čibej | Director of Subversion
WANdisco // Non-Stop Data
e. brane@wandisco.com