Changeset: ca74ecb40ed1 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/ca74ecb40ed1
Removed Files:
        common/stream/bs2.c
Modified Files:
        common/stream/CMakeLists.txt
        common/stream/stream.c
        common/stream/stream.h
Branch: default
Log Message:

Get rid of bs2.c


diffs (truncated from 722 to 300 lines):

diff --git a/common/stream/CMakeLists.txt b/common/stream/CMakeLists.txt
--- a/common/stream/CMakeLists.txt
+++ b/common/stream/CMakeLists.txt
@@ -21,7 +21,6 @@ target_sources(stream
   rw.c
   bstream.c
   bs.c
-  bs2.c
   stdio_stream.c
   winio.c
   compressed.c
diff --git a/common/stream/bs2.c b/common/stream/bs2.c
deleted file mode 100644
--- a/common/stream/bs2.c
+++ /dev/null
@@ -1,670 +0,0 @@
-/*
- * This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0.  If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/.
- *
- * Copyright 1997 - July 2008 CWI, August 2008 - 2022 MonetDB B.V.
- */
-
-#include "monetdb_config.h"
-#include "stream.h"
-#include "stream_internal.h"
-
-
-/* ------------------------------------------------------------------ */
-typedef struct bs2 {
-       stream *s;              /* underlying stream */
-       size_t nr;              /* how far we got in buf */
-       size_t itotal;          /* amount available in current read block */
-       size_t bufsiz;
-       size_t readpos;
-       compression_method comp;
-       char *compbuf;
-       size_t compbufsiz;
-       char *buf;
-} bs2;
-
-
-static ssize_t
-compress_stream_data(bs2 *s)
-{
-       assert(s->comp != COMPRESSION_NONE);
-       if (s->comp == COMPRESSION_SNAPPY) {
-#ifdef HAVE_SNAPPY
-               size_t compressed_length = s->compbufsiz;
-               snappy_status ret;
-               if ((ret = snappy_compress(s->buf, s->nr, s->compbuf, 
&compressed_length)) != SNAPPY_OK) {
-                       return -1;
-               }
-               return compressed_length;
-#else
-               assert(0);
-               return -1;
-#endif
-       } else if (s->comp == COMPRESSION_LZ4) {
-#ifdef HAVE_LIBLZ4
-               int compressed_length = (int) s->compbufsiz;
-               assert(s->nr < INT_MAX);
-               if ((compressed_length = LZ4_compress_fast(s->buf, s->compbuf, 
(int)s->nr, compressed_length, 1)) == 0) {
-                       return -1;
-               }
-               return compressed_length;
-#else
-               assert(0);
-               return -1;
-#endif
-       }
-       return -1;
-}
-
-
-static ssize_t
-decompress_stream_data(bs2 *s)
-{
-       assert(s->comp != COMPRESSION_NONE);
-       if (s->comp == COMPRESSION_SNAPPY) {
-#ifdef HAVE_SNAPPY
-               snappy_status ret;
-               size_t uncompressed_length = s->bufsiz;
-               if ((ret = snappy_uncompress(s->compbuf, s->itotal, s->buf, 
&uncompressed_length)) != SNAPPY_OK) {
-                       return -1;
-               }
-               return (ssize_t) uncompressed_length;
-#else
-               assert(0);
-               return -1;
-#endif
-       } else if (s->comp == COMPRESSION_LZ4) {
-#ifdef HAVE_LIBLZ4
-               int uncompressed_length = (int) s->bufsiz;
-               assert(s->itotal < INT_MAX);
-               if ((uncompressed_length = LZ4_decompress_safe(s->compbuf, 
s->buf, (int)s->itotal, uncompressed_length)) <= 0) {
-                       return -1;
-               }
-               return uncompressed_length;
-#else
-               assert(0);
-               return -1;
-#endif
-       }
-       return -1;
-}
-
-static ssize_t
-compression_size_bound(bs2 *s)
-{
-       if (s->comp == COMPRESSION_NONE) {
-               return 0;
-       } else if (s->comp == COMPRESSION_SNAPPY) {
-#ifndef HAVE_SNAPPY
-               return -1;
-#else
-               return snappy_max_compressed_length(s->bufsiz);
-#endif
-       } else if (s->comp == COMPRESSION_LZ4) {
-#ifndef HAVE_LIBLZ4
-               return -1;
-#else
-               assert(s->bufsiz < INT_MAX);
-               return LZ4_compressBound((int)s->bufsiz);
-#endif
-       }
-       return -1;
-}
-
-static bs2 *
-bs2_create(stream *s, size_t bufsiz, compression_method comp)
-{
-       /* should be a binary stream */
-       bs2 *ns;
-       ssize_t compress_bound = 0;
-
-       if ((ns = malloc(sizeof(*ns))) == NULL)
-               return NULL;
-       *ns = (bs2) {
-               .buf = malloc(bufsiz),
-               .s = s,
-               .bufsiz = bufsiz,
-               .comp = comp,
-       };
-       if (ns->buf == NULL) {
-               free(ns);
-               return NULL;
-       }
-
-       compress_bound = compression_size_bound(ns);
-       if (compress_bound > 0) {
-               ns->compbufsiz = (size_t) compress_bound;
-               ns->compbuf = malloc(ns->compbufsiz);
-               if (!ns->compbuf) {
-                       free(ns->buf);
-                       free(ns);
-                       return NULL;
-               }
-       } else if (compress_bound < 0) {
-               free(ns->buf);
-               free(ns);
-               return NULL;
-       }
-       return ns;
-}
-
-/* Collect data until the internal buffer is filled, then write the
- * filled buffer to the underlying stream.
- * Struct field usage:
- * s - the underlying stream;
- * buf - the buffer in which data is collected;
- * nr - how much of buf is already filled (if nr == sizeof(buf) the
- *      data is written to the underlying stream, so upon entry nr <
- *      sizeof(buf));
- * itotal - unused.
- */
-ssize_t
-bs2_write(stream *restrict ss, const void *restrict buf, size_t elmsize, 
size_t cnt)
-{
-       bs2 *s;
-       size_t todo = cnt * elmsize;
-       int64_t blksize;
-       char *writebuf;
-       size_t writelen;
-
-       s = (bs2 *) ss->stream_data.p;
-       if (s == NULL)
-               return -1;
-       assert(!ss->readonly);
-       assert(s->nr < s->bufsiz);
-       while (todo > 0) {
-               size_t n = s->bufsiz - s->nr;
-
-               if (todo < n)
-                       n = todo;
-               memcpy(s->buf + s->nr, buf, n);
-               s->nr += n;
-               todo -= n;
-               buf = ((const char *) buf + n);
-               /* block is full, write it to the stream */
-               if (s->nr == s->bufsiz) {
-
-#ifdef BSTREAM_DEBUG
-                       {
-                               size_t i;
-
-                               fprintf(stderr, "W %s %zu \"", ss->name, s->nr);
-                               for (i = 0; i < s->nr; i++)
-                                       if (' ' <= s->buf[i] && s->buf[i] < 127)
-                                               putc(s->buf[i], stderr);
-                                       else
-                                               fprintf(stderr, "\\%03o", 
(unsigned char) s->buf[i]);
-                               fprintf(stderr, "\"\n");
-                       }
-#endif
-
-                       writelen = s->nr;
-                       blksize = (int64_t) s->nr;
-                       writebuf = s->buf;
-
-                       if (s->comp != COMPRESSION_NONE) {
-                               ssize_t compressed_length = 
compress_stream_data(s);
-                               if (compressed_length < 0) {
-                                       return -1;
-                               }
-                               writebuf = s->compbuf;
-                               blksize = (int64_t) compressed_length;
-                               writelen = (size_t) compressed_length;
-                       }
-
-
-                       /* the last bit tells whether a flush is in there, it's 
not
-                        * at this moment, so shift it to the left */
-                       blksize <<= 1;
-                       if (!mnstr_writeLng(s->s, blksize) ||
-                           s->s->write(s->s, writebuf, 1, writelen) != 
(ssize_t) writelen) {
-                               mnstr_copy_error(ss, s->s);
-                               return -1;
-                       }
-                       s->nr = 0;
-               }
-       }
-       return (ssize_t) cnt;
-}
-
-/* If the internal buffer is partially filled, write it to the
- * underlying stream.  Then in any case write an empty buffer to the
- * underlying stream to indicate to the receiver that the data was
- * flushed.
- */
-static int
-bs2_flush(stream *ss, mnstr_flush_level flush_level)
-{
-       int64_t blksize;
-       bs2 *s;
-       char *writebuf;
-       size_t writelen;
-
-       s = (bs2 *) ss->stream_data.p;
-       if (s == NULL)
-               return -1;
-       assert(!ss->readonly);
-       assert(s->nr < s->bufsiz);
-       if (!ss->readonly) {
-               /* flush the rest of buffer (if s->nr > 0), then set the
-                * last bit to 1 to to indicate user-instigated flush */
-#ifdef BSTREAM_DEBUG
-               if (s->nr > 0) {
-                       size_t i;
-
-                       fprintf(stderr, "W %s %zu \"", ss->name, s->nr);
-                       for (i = 0; i < s->nr; i++)
-                               if (' ' <= s->buf[i] && s->buf[i] < 127)
-                                       putc(s->buf[i], stderr);
-                               else
-                                       fprintf(stderr, "\\%03o", (unsigned 
char) s->buf[i]);
-                       fprintf(stderr, "\"\n");
-                       fprintf(stderr, "W %s 0\n", ss->name);
-               }
-#endif
-
-               writelen = s->nr;
-               blksize = (int64_t) s->nr;
-               writebuf = s->buf;
-
-               if (s->nr > 0 && s->comp != COMPRESSION_NONE) {
-                       ssize_t compressed_length = compress_stream_data(s);
-                       if (compressed_length < 0) {
-                               return -1;
-                       }
-                       writebuf = s->compbuf;
-                       blksize = (int64_t) compressed_length;
-                       writelen = (size_t) compressed_length;
-               }
-
-               /* indicate that this is the last buffer of a block by
-                * setting the low-order bit */
-               blksize <<= 1;
-               blksize |= 1;
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to