Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streamreader #421

Merged
merged 29 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
bd6b210
Introduce SharedBuffer.
mgautierfr Sep 14, 2020
da21218
Dropped MemoryViewBuffer
veloman-yunkan Sep 9, 2020
2767c04
Dropped MemoryBuffer
veloman-yunkan Sep 9, 2020
269c942
Dropped MMapBuffer
veloman-yunkan Sep 9, 2020
fe9754f
Remove SharedBuffer and make Buffer the only class to contain data.
mgautierfr Sep 14, 2020
6382f68
Blob do not depend of Buffer.
mgautierfr Sep 14, 2020
2a025ec
Do not use external shared_ptr to keep buffer memory alive.
mgautierfr Sep 14, 2020
f19fd25
Introduced zim::IDataStream
veloman-yunkan Aug 28, 2020
86ef980
IStreamReader allow to get a reader.
mgautierfr Sep 14, 2020
a4ed832
Enter DecodedDataStream
veloman-yunkan Aug 30, 2020
695fb9f
Adapt DecoderStreamReader to wrap a Reader instead of a InputStream.
mgautierfr Sep 15, 2020
227df39
zim::ReaderDataStreamWrapper
veloman-yunkan Aug 29, 2020
9c469f8
Adapt RawStreamReader to wrap a reader.
mgautierfr Sep 15, 2020
b8f3eb7
Enter BufDataStream
veloman-yunkan Aug 30, 2020
d796085
Adapt BufferStreamer to wrap a `Buffer` instead of raw data.
mgautierfr Sep 16, 2020
480780a
Got rid of read_size() in cluster.cpp
veloman-yunkan Aug 29, 2020
1b5f8e7
Make the Cluster use `IStreamReader`.
mgautierfr Sep 15, 2020
76c60b4
Make `Dirent` use BufferStreamer.
mgautierfr Sep 15, 2020
9d358d4
Make FileHeader use `BufferStreamer`.
mgautierfr Sep 15, 2020
8b83dc1
Faster Blob/Buffer constructor for non-owned data case
veloman-yunkan Sep 8, 2020
04c4020
fixup! Adapt BufferStreamer to wrap a `Buffer` instead of raw data.
mgautierfr Sep 17, 2020
39533c7
fixup! Adapt DecoderStreamReader to wrap a Reader instead of a InputS…
mgautierfr Sep 17, 2020
04843d9
fixup! Adapt RawStreamReader to wrap a reader
mgautierfr Sep 17, 2020
4672b19
Move `BufferReader` to its own file.
mgautierfr Sep 17, 2020
b3e64fe
Remove `Buffer.as` method.
mgautierfr Sep 17, 2020
8a816f2
fixup! Do not use external shared_ptr to keep buffer memo
mgautierfr Sep 23, 2020
12218e2
Rename tempfile.(cpp|h) to tools.(cpp|h)
mgautierfr Sep 23, 2020
f5e682d
Move `write_to_buffer` test function to a generic helper function.
mgautierfr Sep 23, 2020
004afcb
Remove a few useless empty lines
kelson42 Sep 23, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 0 additions & 51 deletions src/bufdatastream.h

This file was deleted.

78 changes: 78 additions & 0 deletions src/bufferstreamer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (C) 2020 Veloman Yunkan
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
* NON-INFRINGEMENT. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/

#ifndef ZIM_BUFFERSTREAMER_H
#define ZIM_BUFFERSTREAMER_H

#include "debug.h"

#include <string.h>

namespace zim
{

class BufferStreamer
{
public: // functions
explicit BufferStreamer(const Buffer& buffer, zsize_t size)
mgautierfr marked this conversation as resolved.
Show resolved Hide resolved
: m_buffer(buffer),
m_current(buffer.data()),
m_size(size)
{}

explicit BufferStreamer(const Buffer& buffer)
: BufferStreamer(buffer, buffer.size())
{}

// Reads a value of the said type from the stream
//
// For best portability this function should be used with types of known
// bit-width (int32_t, uint16_t, etc) rather than builtin types with
// unknown bit-width (int, unsigned, etc).
template<typename T> T read()
{
const size_t N(sizeof(T));
char buf[N];
memcpy(buf, m_current, N);
skip(zsize_t(N));
return fromLittleEndian<T>(buf); // XXX: This handles only integral types
}

const char* current() const {
return m_current;
}

zsize_t left() const {
return m_size;
}

void skip(zsize_t nbBytes) {
m_current += nbBytes.v;
m_size -= nbBytes;
}

private: // data
const Buffer m_buffer;
const char* m_current;
zsize_t m_size;
};

} // namespace zim

#endif // ZIM_BUFDATASTREAM_H
35 changes: 0 additions & 35 deletions src/istreamreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#include "istreamreader.h"
#include "file_reader.h"
mgautierfr marked this conversation as resolved.
Show resolved Hide resolved
#include "bufdatastream.h"

namespace zim
{
Expand All @@ -36,38 +35,4 @@ IStreamReader::sub_reader(zsize_t size)
return std::unique_ptr<Reader>(new BufferReader(buffer));
}

////////////////////////////////////////////////////////////////////////////////
// BufDataStream
////////////////////////////////////////////////////////////////////////////////

void
BufDataStream::readImpl(void* buf, size_t nbytes)
{
ASSERT(nbytes, <=, size_);
memcpy(buf, data_, nbytes);
data_ += nbytes;
size_ -= nbytes;
}

namespace
{

struct NoDelete
{
template<class T> void operator()(T*) {}
};

} // unnamed namespace

IDataStream::Blob
BufDataStream::readBlobImpl(size_t nbytes)
{
ASSERT(nbytes, <=, size_);
const IDataStream::Blob::DataPtr dataPtr(data_, NoDelete());
const IDataStream::Blob blob(dataPtr, nbytes);
data_ += nbytes;
size_ -= nbytes;
return blob;
}

} // namespace zim
64 changes: 64 additions & 0 deletions test/bufferstreamer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2020 Veloman Yunkan
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
* NON-INFRINGEMENT. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/

#include "buffer.h"
#include "bufferstreamer.h"
#include "endian_tools.h"

#include "gtest/gtest.h"

namespace
{

using namespace zim;

////////////////////////////////////////////////////////////////////////////////
// BufferStreamer
////////////////////////////////////////////////////////////////////////////////

std::string toString(const Buffer& buffer)
{
return std::string(buffer.data(), buffer.size().v);
}

TEST(BufferStreamer, shouldJustWork)
{
char data[] = "abcdefghijklmnopqrstuvwxyz";
zim::toLittleEndian(uint32_t(1234), data);
zim::toLittleEndian(int64_t(-987654321), data+18);

auto buffer = Buffer::makeBuffer(data, zsize_t(sizeof(data)));
zim::BufferStreamer bds(buffer, zsize_t(sizeof(data)));

ASSERT_EQ(1234, bds.read<uint32_t>());

ASSERT_EQ(data + 4, bds.current());
const auto blob1 = std::string(bds.current(), 4);
bds.skip(zsize_t(4));
ASSERT_EQ("efgh", blob1);

ASSERT_EQ(data + 8, bds.current());
const auto blob2 = std::string(bds.current(), 10);
bds.skip(zsize_t(10));
ASSERT_EQ("ijklmnopqr", blob2);

ASSERT_EQ(-987654321, bds.read<int64_t>());
}

} // unnamed namespace
34 changes: 1 addition & 33 deletions test/istreamreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
*/

#include "istreamreader.h"
#include "bufdatastream.h"
#include "endian_tools.h"

#include "gtest/gtest.h"

namespace
{

using zim::IStreamReader;
using namespace zim;

////////////////////////////////////////////////////////////////////////////////
// IDataStream
Expand Down Expand Up @@ -64,35 +63,4 @@ TEST(IStreamReader, sub_reader)
EXPECT_EQ(0, memcmp(buffer.data(), zerobuf, N));
}

////////////////////////////////////////////////////////////////////////////////
// BufDataStream
////////////////////////////////////////////////////////////////////////////////

std::string toString(const IDataStream::Blob& blob)
{
return std::string(blob.data(), blob.size());
}

TEST(BufDataStream, shouldJustWork)
{
char data[] = "abcdefghijklmnopqrstuvwxyz";
zim::toLittleEndian(uint32_t(1234), data);
zim::toLittleEndian(int64_t(-987654321), data+18);

zim::BufDataStream bds(data, sizeof(data));
IDataStream& ids = bds;

ASSERT_EQ(1234, ids.read<uint32_t>());

const auto blob1 = ids.readBlob(4);
ASSERT_EQ("efgh", toString(blob1));
ASSERT_EQ(data + 4, blob1.data());

const auto blob2 = ids.readBlob(10);
ASSERT_EQ("ijklmnopqr", toString(blob2));
ASSERT_EQ(data + 8, blob2.data());

ASSERT_EQ(-987654321, ids.read<int64_t>());
}

} // unnamed namespace
3 changes: 2 additions & 1 deletion test/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ tests = [
'impl_find',
'istreamreader',
'decoderstreamreader',
'rawstreamreader'
'rawstreamreader',
'bufferstreamer'
]

if gtest_dep.found() and not meson.is_cross_build()
Expand Down