Changeset: fbb1cbcf5fe5 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=fbb1cbcf5fe5
Modified Files:
        common/stream/stream.c
        common/stream/stream.h
Branch: protocol
Log Message:

Fix uncompressed prot10 by always reading into a buffer.

Previously blockstream2 only read into a buffer when using compression, because 
we wrongfully assumed the blockstream read into a buffer and simply copied that 
code.


diffs (174 lines):

diff --git a/common/stream/stream.c b/common/stream/stream.c
--- a/common/stream/stream.c
+++ b/common/stream/stream.c
@@ -4233,34 +4233,41 @@ bs2_read(stream *ss, void *buf, size_t e
                /* store whether this was the last block or not */
                s->nr = blksize & 1;
 
-
-               if (s->itotal > 0 && s->comp == COMPRESSION_SNAPPY) {
-#ifdef HAVE_LIBSNAPPY
+               if (s->itotal > 0) {
                        // read everything into the comp buf
                        size_t uncompressed_length = s->bufsiz;
                        size_t m = 0;
+                       char *buf = s->buf;
+                       if (s->comp != COMPRESSION_NONE) {
+                               buf = s->compbuf;
+                       }
                        snappy_status ret;
 
                        while (m < s->itotal) {
                                ssize_t bytes_read = 0;
-                               bytes_read = s->s->read(s->s, s->compbuf + m, 
1, s->itotal - m);
+                               bytes_read = s->s->read(s->s, buf + m, 1, 
s->itotal - m);
                                if (bytes_read <= 0) {
                                        ss->errnr = s->s->errnr;
                                        return -1;
                                }
                                m += bytes_read;
                        }
-                       if ((ret = snappy_uncompress(s->compbuf, s->itotal, 
s->buf, &uncompressed_length)) != SNAPPY_OK) {
-                               ss->errnr = (int) ret;
-                               return -1;
+                       if (s->comp == COMPRESSION_SNAPPY) {
+#ifdef HAVE_LIBSNAPPY
+                               if ((ret = snappy_uncompress(s->compbuf, 
s->itotal, s->buf, &uncompressed_length)) != SNAPPY_OK) {
+                                       ss->errnr = (int) ret;
+                                       return -1;
+                               }
+#else
+               assert(0);
+               return -1;
+#endif
+                       } else {
+                               uncompressed_length = m;
                        }
                        s->itotal = uncompressed_length;
                        s->readpos = 0;
                }
-#else
-               assert(0);
-               return 0;
-#endif
        }
 
        /* Fill the caller's buffer. */
@@ -4269,45 +4276,14 @@ bs2_read(stream *ss, void *buf, size_t e
                /* there is more data waiting in the current block, so
                 * read it */
                n = todo < s->itotal ? todo : s->itotal;
-               if (s->comp == COMPRESSION_SNAPPY) {
-                       memcpy(buf, s->buf + s->readpos, n);
-                       buf = (void *) ((char *) buf + n);
-                       cnt += n;
-                       todo -= n;
-                       s->readpos += n;
-                       s->itotal -= n;
-
-               } else {
-                       while (n > 0) {
-                               ssize_t m = s->s->read(s->s, buf, 1, n);
-
-                               if (m <= 0) {
-                                       ss->errnr = s->s->errnr;
-                                       return -1;
-                               }
-
-
-#ifdef BSTREAM_DEBUG
-                               {
-                                       ssize_t i;
-
-                                       fprintf(stderr, "R2 '%s' %zd \"", 
ss->name, m);
-                                       for (i = 0; i < m; i++)
-                                               if (' ' <= ((char *) buf)[i] && 
((char *) buf)[i] < 127)
-                                                       putc(((char *) buf)[i], 
stderr);
-                                               else
-                                                       fprintf(stderr, 
"\\%03o", ((char *) buf)[i]);
-                                       fprintf(stderr, "\"\n");
-                               }
-#endif
-
-                               buf = (void *) ((char *) buf + m);
-                               cnt += m;
-                               n -= m;
-                               s->itotal -= m;
-                               todo -= m;
-                       }
-               }
+               
+               memcpy(buf, s->buf + s->readpos, n);
+               buf = (void *) ((char *) buf + n);
+               cnt += n;
+               todo -= n;
+               s->readpos += n;
+               s->itotal -= n;
+
                if (s->itotal == 0) {
                        lng blksize = 0;
 
@@ -4339,33 +4315,41 @@ bs2_read(stream *ss, void *buf, size_t e
                        /* store whether this was the last block or not */
                        s->nr = blksize & 1;
 
-                       if (s->itotal > 0 && s->comp == COMPRESSION_SNAPPY) {
-#ifdef HAVE_LIBSNAPPY
+                       if (s->itotal > 0) {
                                // read everything into the comp buf
                                size_t uncompressed_length = s->bufsiz;
                                size_t m = 0;
+                               char *buf = s->buf;
+                               if (s->comp != COMPRESSION_NONE) {
+                                       buf = s->compbuf;
+                               }
                                snappy_status ret;
 
                                while (m < s->itotal) {
                                        ssize_t bytes_read = 0;
-                                       bytes_read = s->s->read(s->s, 
s->compbuf + m, 1, s->itotal - m);
+                                       bytes_read = s->s->read(s->s, buf + m, 
1, s->itotal - m);
                                        if (bytes_read <= 0) {
                                                ss->errnr = s->s->errnr;
                                                return -1;
                                        }
                                        m += bytes_read;
                                }
-                               if ((ret = snappy_uncompress(s->compbuf, 
s->itotal, s->buf, &uncompressed_length)) != SNAPPY_OK) {
-                                       ss->errnr = (int) ret;
-                                       return -1;
+                               if (s->comp == COMPRESSION_SNAPPY) {
+#ifdef HAVE_LIBSNAPPY
+                                       if ((ret = 
snappy_uncompress(s->compbuf, s->itotal, s->buf, &uncompressed_length)) != 
SNAPPY_OK) {
+                                               ss->errnr = (int) ret;
+                                               return -1;
+                                       }
+#else
+                       assert(0);
+                       return -1;
+#endif
+                               } else {
+                                       uncompressed_length = m;
                                }
                                s->itotal = uncompressed_length;
                                s->readpos = 0;
                        }
-#else
-                       assert(0);
-                       return -1;
-#endif
                }
        }
        /* if we got an empty block with the end-of-sequence marker
diff --git a/common/stream/stream.h b/common/stream/stream.h
--- a/common/stream/stream.h
+++ b/common/stream/stream.h
@@ -8,7 +8,7 @@
 
 #ifndef _STREAM_H_
 #define _STREAM_H_
-
+ 
 /*
  * File: stream.h
  * Auteur: Niels J. Nes
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to