diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2017-08-28 13:53:07 +0200 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2017-08-28 13:55:47 +0200 |
commit | 2868058bbb1427bc16a941e453314ee856303c4e (patch) | |
tree | f23b807bf20443f721c436f27a435efd9009ad94 /staging_vespalib | |
parent | ccf572d02b2552f033f2811666dc7a5cb9546fa6 (diff) |
Moved databuffer and compresssion to vespalib
Diffstat (limited to 'staging_vespalib')
10 files changed, 3 insertions, 934 deletions
diff --git a/staging_vespalib/CMakeLists.txt b/staging_vespalib/CMakeLists.txt index 5a169416775..d59529df828 100644 --- a/staging_vespalib/CMakeLists.txt +++ b/staging_vespalib/CMakeLists.txt @@ -13,7 +13,6 @@ vespa_define_module( src/tests/bits src/tests/clock src/tests/crc - src/tests/databuffer src/tests/directio src/tests/encoding/base64 src/tests/fileheader diff --git a/staging_vespalib/src/tests/databuffer/.gitignore b/staging_vespalib/src/tests/databuffer/.gitignore deleted file mode 100644 index e7b0e69c372..00000000000 --- a/staging_vespalib/src/tests/databuffer/.gitignore +++ /dev/null @@ -1 +0,0 @@ -staging_vespalib_databuffer_test_app diff --git a/staging_vespalib/src/tests/databuffer/CMakeLists.txt b/staging_vespalib/src/tests/databuffer/CMakeLists.txt deleted file mode 100644 index 0c96a8cdf3e..00000000000 --- a/staging_vespalib/src/tests/databuffer/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -# Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -vespa_add_executable(staging_vespalib_databuffer_test_app TEST - SOURCES - databuffer_test.cpp - DEPENDS - staging_vespalib -) -vespa_add_test(NAME staging_vespalib_databuffer_test_app COMMAND staging_vespalib_databuffer_test_app) diff --git a/staging_vespalib/src/tests/databuffer/databuffer_test.cpp b/staging_vespalib/src/tests/databuffer/databuffer_test.cpp deleted file mode 100644 index f440ca1e15c..00000000000 --- a/staging_vespalib/src/tests/databuffer/databuffer_test.cpp +++ /dev/null @@ -1,142 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. - -#include <vespa/vespalib/testkit/testapp.h> -#include <vespa/vespalib/data/databuffer.h> -#include <iostream> - -using namespace vespalib; - -class Test : public vespalib::TestApp { -private: - void testBasic(); -public: - int Main() override { - TEST_INIT("databuffer_test"); - - testBasic(); TEST_FLUSH(); - - TEST_DONE(); - } -}; - -TEST_APPHOOK(Test); - -void -Test::testBasic() -{ - DataBuffer a(50); - EXPECT_EQUAL(256u, a.getBufSize()); - EXPECT_EQUAL(a.getFreeLen(), a.getBufSize()); - a.ensureFree(1000); - EXPECT_EQUAL(1024u, a.getBufSize()); - EXPECT_EQUAL(a.getFreeLen(), a.getBufSize()); - EXPECT_EQUAL(0u, a.getDeadLen()); - EXPECT_EQUAL(0u, a.getDataLen()); - EXPECT_EQUAL(a.getData(), a.getDead()); - EXPECT_EQUAL(a.getData(), a.getFree()); - EXPECT_EQUAL(a.getBufSize(), a.getFreeLen()); - a.assertValid(); - - a.writeInt16(7); - EXPECT_EQUAL(0u, a.getDeadLen()); - EXPECT_EQUAL(2u, a.getDataLen()); - EXPECT_EQUAL(a.getBufSize()-2, a.getFreeLen()); - EXPECT_EQUAL(a.getData(), a.getDead()); - EXPECT_EQUAL(a.getData()+2, a.getFree()); - a.clear(); - EXPECT_EQUAL(0u, a.getDeadLen()); - EXPECT_EQUAL(0u, a.getDataLen()); - EXPECT_EQUAL(a.getBufSize(), a.getFreeLen()); - - a.writeInt8(0xaau); - EXPECT_EQUAL(1u, a.getDataLen()); - EXPECT_EQUAL(0xaau, a.peekInt8(0)); - EXPECT_EQUAL(1u, a.getDataLen()); - EXPECT_EQUAL(0xaau, a.readInt8()); - EXPECT_EQUAL(0u, a.getDataLen()); - - a.writeInt16(0xaabbu); - EXPECT_EQUAL(2u, a.getDataLen()); - EXPECT_EQUAL(0xaabbu, a.peekInt16(0)); - EXPECT_EQUAL(2u, a.getDataLen()); - EXPECT_EQUAL(0xaabbu, a.readInt16()); - EXPECT_EQUAL(0u, a.getDataLen()); - a.writeInt16(0xaabbu); - EXPECT_EQUAL(2u, a.getDataLen()); - EXPECT_EQUAL(0xbbaau, a.peekInt16Reverse(0)); - EXPECT_EQUAL(2u, a.getDataLen()); - EXPECT_EQUAL(0xbbaau, a.readInt16Reverse()); - EXPECT_EQUAL(0u, a.getDataLen()); - - a.writeInt32(0xaabbccddu); - EXPECT_EQUAL(4u, a.getDataLen()); - EXPECT_EQUAL(0xaabbccddu, a.peekInt32(0)); - EXPECT_EQUAL(4u, a.getDataLen()); - EXPECT_EQUAL(0xaabbccddu, a.readInt32()); - EXPECT_EQUAL(0u, a.getDataLen()); - a.writeInt32(0xaabbccddu); - EXPECT_EQUAL(4u, a.getDataLen()); - EXPECT_EQUAL(0xddccbbaau, a.peekInt32Reverse(0)); - EXPECT_EQUAL(4u, a.getDataLen()); - EXPECT_EQUAL(0xddccbbaau, a.readInt32Reverse()); - EXPECT_EQUAL(0u, a.getDataLen()); - - a.writeInt64(0xaabbccddeeff9988ul); - EXPECT_EQUAL(8u, a.getDataLen()); - EXPECT_EQUAL(0xaabbccddeeff9988ul, a.peekInt64(0)); - EXPECT_EQUAL(8u, a.getDataLen()); - EXPECT_EQUAL(0xaabbccddeeff9988ul, a.readInt64()); - EXPECT_EQUAL(0u, a.getDataLen()); - a.writeInt64(0xaabbccddeeff9988ul); - EXPECT_EQUAL(8u, a.getDataLen()); - EXPECT_EQUAL(0x8899ffeeddccbbaaul, a.peekInt64Reverse(0)); - EXPECT_EQUAL(8u, a.getDataLen()); - EXPECT_EQUAL(0x8899ffeeddccbbaaul, a.readInt64Reverse()); - EXPECT_EQUAL(0u, a.getDataLen()); - - a.writeFloat(8.9f); - EXPECT_EQUAL(4u, a.getDataLen()); - EXPECT_EQUAL(8.9f, a.readFloat()); - EXPECT_EQUAL(0u, a.getDataLen()); - - a.writeDouble(8.9); - EXPECT_EQUAL(8u, a.getDataLen()); - EXPECT_EQUAL(8.9, a.readDouble()); - EXPECT_EQUAL(0u, a.getDataLen()); - - const char *c = "abc"; - char b[3]; - a.writeBytes(c, 3); - EXPECT_EQUAL(3u, a.getDataLen()); - EXPECT_EQUAL(0, memcmp(c, a.getData(), a.getDataLen())); - a.peekBytes(b, 3, 0); - EXPECT_EQUAL(3u, a.getDataLen()); - EXPECT_EQUAL(0, memcmp(c, b, sizeof(b))); - a.readBytes(b, sizeof(b)); - EXPECT_EQUAL(0u, a.getDataLen()); - EXPECT_EQUAL(0, memcmp(c, b, sizeof(b))); - - a.writeInt64(67); - EXPECT_EQUAL(8u, a.getDataLen()); - EXPECT_FALSE(a.shrink(1025)); - EXPECT_FALSE(a.shrink(7)); - EXPECT_TRUE(a.shrink(16)); - EXPECT_EQUAL(8u, a.getDataLen()); - EXPECT_EQUAL(16u, a.getBufSize()); - - a.writeInt64(89); - EXPECT_EQUAL(16u, a.getDataLen()); - EXPECT_EQUAL(16u, a.getBufSize()); - EXPECT_EQUAL(0u, a.getDeadLen()); - EXPECT_EQUAL(67u, a.readInt64()); - EXPECT_EQUAL(8u, a.getDataLen()); - EXPECT_EQUAL(8u, a.getDeadLen()); - EXPECT_EQUAL(16u, a.getBufSize()); - a.pack(16); - EXPECT_EQUAL(8u, a.getDataLen()); - EXPECT_EQUAL(0u, a.getDeadLen()); - EXPECT_EQUAL(256u, a.getBufSize()); - EXPECT_EQUAL(89u, a.readInt64()); - EXPECT_EQUAL(0u, a.getDataLen()); - EXPECT_EQUAL(256u, a.getBufSize()); -} diff --git a/staging_vespalib/src/tests/fileheader/fileheader_test.cpp b/staging_vespalib/src/tests/fileheader/fileheader_test.cpp index b895b92db4c..f6f9c19b48c 100644 --- a/staging_vespalib/src/tests/fileheader/fileheader_test.cpp +++ b/staging_vespalib/src/tests/fileheader/fileheader_test.cpp @@ -2,6 +2,7 @@ #include <vespa/vespalib/testkit/testapp.h> #include <vespa/vespalib/data/fileheader.h> +#include <vespa/vespalib/data/databuffer.h> #include <vespa/fastos/file.h> #include <iostream> diff --git a/staging_vespalib/src/vespa/vespalib/data/CMakeLists.txt b/staging_vespalib/src/vespa/vespalib/data/CMakeLists.txt index 3fbece6211d..8ca70d2a63d 100644 --- a/staging_vespalib/src/vespa/vespalib/data/CMakeLists.txt +++ b/staging_vespalib/src/vespa/vespalib/data/CMakeLists.txt @@ -1,7 +1,6 @@ # Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. vespa_add_library(staging_vespalib_vespalib_data OBJECT SOURCES - databuffer.cpp fileheader.cpp DEPENDS ) diff --git a/staging_vespalib/src/vespa/vespalib/data/databuffer.cpp b/staging_vespalib/src/vespa/vespalib/data/databuffer.cpp deleted file mode 100644 index 529e475e987..00000000000 --- a/staging_vespalib/src/vespa/vespalib/data/databuffer.cpp +++ /dev/null @@ -1,171 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -#include "databuffer.h" - -namespace vespalib { - -namespace { -size_t padbefore(size_t alignment, const char *buf) { - return (alignment - (size_t(buf) % alignment)) % alignment; -} -} - -DataBuffer::DataBuffer(size_t len, size_t alignment, const Alloc & initial) - : _alignment(alignment), - _externalBuf(NULL), - _bufstart(NULL), - _bufend(NULL), - _datapt(NULL), - _freept(NULL), - _buffer(initial.create(0)) -{ - assert(_alignment > 0); - if (len > 0) { - // avoid very small buffers for performance reasons: - size_t bufsize = std::max(256ul, roundUp2inN(len + (_alignment - 1))); - Alloc newBuf(initial.create(bufsize)); - _bufstart = static_cast<char *>(newBuf.get()); - _buffer.swap(newBuf); - - _datapt = _bufstart + padbefore(alignment, _bufstart); - _freept = _datapt; - _bufend = _bufstart + bufsize; - assert(_bufstart != NULL); - } -} - - -void -DataBuffer::moveFreeToData(size_t len) -{ - assert(getFreeLen() >= len); - _freept += len; -} - - -void -DataBuffer::moveDeadToData(size_t len) -{ - assert(getDeadLen() >= len); - _datapt -= len; - if (_bufstart != _externalBuf) { - assert(getDeadLen() >= padbefore(_alignment, _bufstart)); // Do not move ahead of alignment. - } -} - - -void -DataBuffer::moveDataToFree(size_t len) -{ - assert(getDataLen() >= len); - _freept -= len; -} - - -bool -DataBuffer::shrink(size_t newsize) -{ - if (getBufSize() <= newsize || getDataLen() > newsize) { - return false; - } - char *newbuf = NULL; - char *newdata = NULL; - newsize += (_alignment - 1); - Alloc newBuf(_buffer.create(newsize)); - if (newsize != 0) { - newbuf = static_cast<char *>(newBuf.get()); - newdata = newbuf + padbefore(_alignment, newbuf); - memcpy(newdata, _datapt, getDataLen()); - } - _buffer.swap(newBuf); - _bufstart = newbuf; - _freept = newdata + getDataLen(); - _datapt = newdata; - _bufend = newbuf + newsize; - return true; -} - - -void -DataBuffer::pack(size_t needbytes) -{ - needbytes += (_alignment - 1); - size_t dataLen = getDataLen(); - - if ((getDeadLen() + getFreeLen()) < needbytes || - (getDeadLen() + getFreeLen()) * 4 < dataLen) - { - size_t bufsize = std::max(256ul, roundUp2inN(needbytes+dataLen)); - Alloc newBuf(_buffer.create(bufsize)); - char *newbuf = static_cast<char *>(newBuf.get()); - char *newdata = newbuf + padbefore(_alignment, newbuf); - memcpy(newdata, _datapt, dataLen); - _bufstart = newbuf; - _datapt = newdata; - _freept = newdata + dataLen; - _bufend = newbuf + bufsize; - _buffer.swap(newBuf); - } else { - char *datapt = _bufstart + padbefore(_alignment, _bufstart); - memmove(datapt, _datapt, dataLen); - _datapt = datapt; - _freept = _datapt + dataLen; - } -} - - -bool -DataBuffer::equals(DataBuffer *other) -{ - if (getDataLen() != other->getDataLen()) - return false; - return memcmp(getData(), other->getData(), getDataLen()) == 0; -} - - -void -DataBuffer::hexDump() -{ - char *pt = _datapt; - printf("*** DataBuffer HexDump BEGIN ***\n"); - uint32_t i = 0; - while (pt < _freept) { - printf("%x ", (unsigned char) *pt++); - if ((++i % 16) == 0) - printf("\n"); - } - if ((i % 16) != 0) - printf("\n"); - printf("*** DataBuffer HexDump END ***\n"); -} - - -void -DataBuffer::swap(DataBuffer &other) -{ - _buffer.swap(other._buffer); - std::swap(_alignment, other._alignment); - std::swap(_externalBuf, other._externalBuf); - std::swap(_bufstart, other._bufstart); - std::swap(_bufend, other._bufend); - std::swap(_datapt, other._datapt); - std::swap(_freept, other._freept); -} - -vespalib::alloc::Alloc -DataBuffer::stealBuffer() -{ - assert( ! referencesExternalData() ); - _externalBuf = nullptr; - _bufstart = nullptr; - _bufend = nullptr; - _datapt = nullptr; - _freept = nullptr; - return std::move(_buffer); -} - -bool -DataBuffer::referencesExternalData() const { - return (_externalBuf == _bufstart) && (getBufSize() > 0); -} - -} // namespace vespalib diff --git a/staging_vespalib/src/vespa/vespalib/data/databuffer.h b/staging_vespalib/src/vespa/vespalib/data/databuffer.h deleted file mode 100644 index f7707a3ea56..00000000000 --- a/staging_vespalib/src/vespa/vespalib/data/databuffer.h +++ /dev/null @@ -1,609 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -#pragma once - -#include <cstring> -#include <cassert> -#include <vespa/vespalib/util/alloc.h> - -namespace vespalib { - -/** - * This is a buffer that may hold the stream representation of - * packets. It has helper methods in order to simplify and standardize - * packet encoding and decoding. The default byte order for - * reading/writing integers is internet order (big endian). The - * methods with a 'Reverse' suffix assume that the data in the buffer - * is stored in reverse internet order (little endian). - * - * A databuffer covers a continuous chunk of memory that is split into - * 3 parts; 'dead', 'data' and 'free'. 'dead' denotes the space at the - * beginning of the buffer that may not currently be utilized, 'data' - * denotes the part that contains the actual data and 'free' denotes - * the free space at the end of the buffer. Initially, the 'dead' and - * 'data' parts are empty, and the 'free' part spans the entire - * buffer. When writing to the buffer, bytes are transfered from the - * 'free' part to the 'data' part of the buffer. When reading from the - * buffer, bytes are transferred from the 'data' part to the 'dead' - * part of the buffer. If the 'free' part of the buffer becomes empty, - * the data will be relocated within the buffer and/or a bigger buffer - * will be allocated. - **/ -class DataBuffer -{ -private: - using Alloc = alloc::Alloc; - size_t _alignment; - char *_externalBuf; - char *_bufstart; - char *_bufend; - char *_datapt; - char *_freept; - Alloc _buffer; - -public: - typedef std::unique_ptr<DataBuffer> UP; - DataBuffer(const DataBuffer &) = delete; - DataBuffer &operator=(const DataBuffer &) = delete; - - /** - * Construct a databuffer. - * - * @param len the initial size of the buffer. - * @param alignment required memory alignment for data start - **/ - DataBuffer(size_t len = 1024, size_t alignment = 1, const Alloc & initial = Alloc::alloc(0)); - - /** - * Construct a databuffer using externally allocated memory. Note - * that the externally allocated memory will not be freed by the - * databuffer. - * - * @param buf pointer to preallocated memory - * @param len length of preallocated memory - **/ - DataBuffer(char *buf, size_t len) : - _alignment(1), - _externalBuf(buf), - _bufstart(buf), - _bufend(buf + len), - _datapt(_bufstart), - _freept(_bufstart), - _buffer(Alloc::alloc(0)) - { } - - DataBuffer(const char *buf, size_t len) : - _alignment(1), - _externalBuf(const_cast<char *>(buf)), - _bufstart(_externalBuf), - _bufend(_bufstart + len), - _datapt(_bufstart), - _freept(_bufend), - _buffer(Alloc::alloc(0)) - { } - - /** - * @return a pointer to the dead part of this buffer. - **/ - char *getDead() const { return _bufstart; } - - /** - * @return a pointer to the data part of this buffer. - **/ - char *getData() const { return _datapt; } - - /** - * @return a pointer to the free part of this buffer. - **/ - char *getFree() const { return _freept; } - - /** - * @return the length of the dead part of this buffer. - **/ - size_t getDeadLen() const { return _datapt - _bufstart; } - - /** - * @return the length of the data part of this buffer. - **/ - size_t getDataLen() const { return _freept - _datapt; } - - /** - * @return the length of the free part of this buffer. - **/ - size_t getFreeLen() const { return _bufend - _freept; } - - /** - * @return the length of the entire buffer. - **/ - size_t getBufSize() const { return _bufend - _bufstart; } - - - /** - * 'Move' bytes from the free part to the data part of this buffer. - * This will have the same effect as if the data already located in - * the free part of this buffer was written to the buffer. - * - * @param len number of bytes to 'move'. - **/ - void moveFreeToData(size_t len); - - /** - * 'Move' bytes from the data part to the dead part of this buffer. - * This will have the effect of discarding data without having to - * read it. - * - * @param len number of bytes to 'move'. - **/ - void moveDataToDead(size_t len) { _datapt += len; } - - - /** - * 'Move' bytes from the dead part to the data part of this buffer. - * This may be used to undo a read operation (un-discarding - * data). Note that writing to the buffer may result in - * reorganization making the data part of the buffer disappear. - * - * @param len number of bytes to 'move'. - **/ - void moveDeadToData(size_t len); - - /** - * 'Move' bytes from the data part to the free part of this buffer. - * This may be used to undo a write operation; discarding the data - * most recently written. - * - * @param len number of bytes to 'move'. - **/ - void moveDataToFree(size_t len); - - - /** - * Clear this buffer. - **/ - void clear() { _datapt = _freept = _bufstart; } - - - /** - * Shrink this buffer. The given value is the new wanted size of - * this buffer. If the buffer is already smaller or equal in size - * compared to the given value, no resizing is performed and false - * is returned (Use the @ref ensureFree method to ensure free - * space). If the buffer currently contains more data than can be - * held in a buffer of the wanted size, no resizing is performed and - * false is returned. - * - * @param newsize the wanted new size of this buffer (in bytes). - * @return true if the buffer was shrunk, false otherwise. - **/ - bool shrink(size_t newsize); - - /** - * Reorganize this buffer such that the dead part becomes empty and - * the free part contains at least the given number of - * bytes. Allocate a bigger buffer if needed. - * - * @param needbytes required size of free part. - **/ - void pack(size_t needbytes); - - /** - * Ensure that the free part contains at least the given number of - * bytes. This method invokes the @ref Pack method if the free part - * of the buffer is too small. - * - * @param needbytes required size of free part. - **/ - void ensureFree(size_t needbytes) - { - if (needbytes > getFreeLen()) - pack(needbytes); - } - - - /** - * Write an 8-bit unsigned integer to this buffer. - * - * @param n the integer to write. - **/ - void writeInt8(uint8_t n) - { - ensureFree(1); - *_freept++ = (char)n; - } - - /** - * Write a 16-bit unsigned integer to this buffer. - * - * @param n the integer to write. - **/ - void writeInt16(uint16_t n) - { - ensureFree(2); - _freept[1] = (char)n; - n >>= 8; - _freept[0] = (char)n; - _freept += 2; - } - - /** - * Write a 32-bit unsigned integer to this buffer. - * - * @param n the integer to write. - **/ - void writeInt32(uint32_t n) - { - ensureFree(4); - _freept[3] = (char)n; - n >>= 8; - _freept[2] = (char)n; - n >>= 8; - _freept[1] = (char)n; - n >>= 8; - _freept[0] = (char)n; - _freept += 4; - } - - /** - * Write a 64-bit unsigned integer to this buffer. - * - * @param n the integer to write. - **/ - void writeInt64(uint64_t n) - { - ensureFree(8); - _freept[7] = (char)n; - n >>= 8; - _freept[6] = (char)n; - n >>= 8; - _freept[5] = (char)n; - n >>= 8; - _freept[4] = (char)n; - n >>= 8; - _freept[3] = (char)n; - n >>= 8; - _freept[2] = (char)n; - n >>= 8; - _freept[1] = (char)n; - n >>= 8; - _freept[0] = (char)n; - _freept += 8; - } - - - - /** - * Read an 8-bit unsigned integer from this buffer. - * - * @return the integer that has been read. - **/ - uint8_t readInt8() - { - return (unsigned char)(*_datapt++); - } - - /** - * Read a 16-bit unsigned integer from this buffer. - * - * @return the integer that has been read. - **/ - uint16_t readInt16() - { - unsigned char *tmp = (unsigned char *)(_datapt); - _datapt += 2; - return ((*tmp << 8) + *(tmp + 1)); - } - - /** - * Read a 16-bit unsigned integer stored in reverse internet order - * from this buffer. - * - * @return the integer that has been read. - **/ - uint16_t readInt16Reverse() - { - unsigned char *tmp = (unsigned char *)(_datapt); - _datapt += 2; - return ((*(tmp + 1) << 8) + *tmp); - } - - /** - * Read a 32-bit unsigned integer from this buffer. - * - * @return the integer that has been read. - **/ - uint32_t readInt32() - { - unsigned char *tmp = (unsigned char *)(_datapt); - _datapt += 4; - return - ((((((uint32_t)(*tmp << 8) + *(tmp + 1)) << 8) - + *(tmp + 2)) << 8) + *(tmp + 3)); - } - - /** - * Read a 32-bit unsigned integer stored in reverse internet order - * from this buffer. - * - * @return the integer that has been read. - **/ - uint32_t readInt32Reverse() - { - unsigned char *tmp = (unsigned char *)(_datapt); - _datapt += 4; - return - ((((((uint32_t)(*(tmp + 3) << 8) + *(tmp + 2)) << 8) - + *(tmp + 1)) << 8) + *tmp); - } - - /** - * Read a 64-bit unsigned integer from this buffer. - * - * @return the integer that has been read. - **/ - uint64_t readInt64() - { - unsigned char *tmp = (unsigned char *)(_datapt); - _datapt += 8; - return - ((((((((((((((uint64_t)(*tmp << 8) + *(tmp + 1)) << 8) - + *(tmp + 2)) << 8) + *(tmp + 3)) << 8) - + *(tmp + 4)) << 8) + *(tmp + 5)) << 8) - + *(tmp + 6)) << 8) + *(tmp + 7)); - } - - /** - * Read a 64-bit unsigned integer stored in reverse internet order - * from this buffer. - * - * @return the integer that has been read. - **/ - uint64_t readInt64Reverse() - { - unsigned char *tmp = (unsigned char *)(_datapt); - _datapt += 8; - return - ((((((((((((((uint64_t)(*(tmp + 7) << 8) + *(tmp + 6)) << 8) - + *(tmp + 5)) << 8) + *(tmp + 4)) << 8) - + *(tmp + 3)) << 8) + *(tmp + 2)) << 8) - + *(tmp + 1)) << 8) + *tmp); - } - - float readFloat() - { - float f; - uint32_t i = readInt32(); - memcpy(&f, &i, sizeof(f)); - return f; - } - - double readDouble() - { - double f; - uint64_t i = readInt64(); - memcpy(&f, &i, sizeof(f)); - return f; - } - - void writeFloat(float f) - { - uint32_t i; - memcpy(&i, &f, sizeof(f)); - writeInt32(i); - } - - void writeDouble(double f) - { - uint64_t i; - memcpy(&i, &f, sizeof(f)); - writeInt64(i); - } - - - /** - * Peek at an 8-bit unsigned integer in this buffer. Unlike a read - * operation, this will not modify the buffer. - * - * @param offset offset of the integer to access. - * @return value of the accessed integer. - **/ - uint8_t peekInt8(size_t offset) - { - assert(getDataLen() >= offset + 1); - return (uint8_t) *(_datapt + offset); - } - - /** - * Peek at a 16-bit unsigned integer in this buffer. Unlike a read - * operation, this will not modify the buffer. - * - * @param offset offset of the integer to access. - * @return value of the accessed integer. - **/ - uint16_t peekInt16(size_t offset) - { - assert(getDataLen() >= offset + 2); - unsigned char *tmp = (unsigned char *)(_datapt + offset); - return (uint16_t) ((*tmp << 8) + *(tmp + 1)); - } - - /** - * Peek at a 16-bit unsigned integer stored in reverse internet - * order in this buffer. Unlike a read operation, this will not - * modify the buffer. - * - * @param offset offset of the integer to access. - * @return value of the accessed integer. - **/ - uint16_t peekInt16Reverse(size_t offset) - { - assert(getDataLen() >= offset + 2); - unsigned char *tmp = (unsigned char *)(_datapt + offset); - return (uint16_t) ((*(tmp + 1) << 8) + *tmp); - } - - /** - * Peek at a 32-bit unsigned integer in this buffer. Unlike a read - * operation, this will not modify the buffer. - * - * @param offset offset of the integer to access. - * @return value of the accessed integer. - **/ - uint32_t peekInt32(size_t offset) - { - assert(getDataLen() >= offset + 4); - unsigned char *tmp = (unsigned char *)(_datapt + offset); - return - ((((((uint32_t)(*tmp << 8) + *(tmp + 1)) << 8) - + *(tmp + 2)) << 8) + *(tmp + 3)); - } - - /** - * Peek at a 32-bit unsigned integer stored in reverse internet - * order in this buffer. Unlike a read operation, this will not - * modify the buffer. - * - * @param offset offset of the integer to access. - * @return value of the accessed integer. - **/ - uint32_t peekInt32Reverse(size_t offset) - { - assert(getDataLen() >= offset + 4); - unsigned char *tmp = (unsigned char *)(_datapt + offset); - return - ((((((uint32_t)(*(tmp + 3) << 8) + *(tmp + 2)) << 8) - + *(tmp + 1)) << 8) + *tmp); - } - - /** - * Peek at a 64-bit unsigned integer in this buffer. Unlike a read - * operation, this will not modify the buffer. - * - * @param offset offset of the integer to access. - * @return value of the accessed integer. - **/ - uint64_t peekInt64(size_t offset) - { - assert(getDataLen() >= offset + 8); - unsigned char *tmp = (unsigned char *)(_datapt + offset); - return - ((((((((((((((uint64_t)(*tmp << 8) + *(tmp + 1)) << 8) - + *(tmp + 2)) << 8) + *(tmp + 3)) << 8) - + *(tmp + 4)) << 8) + *(tmp + 5)) << 8) - + *(tmp + 6)) << 8) + *(tmp + 7)); - } - - /** - * Peek at a 64-bit unsigned integer stored in reverse internet - * order in this buffer. Unlike a read operation, this will not - * modify the buffer. - * - * @param offset offset of the integer to access. - * @return value of the accessed integer. - **/ - uint64_t peekInt64Reverse(size_t offset) - { - assert(getDataLen() >= offset + 8); - unsigned char *tmp = (unsigned char *)(_datapt + offset); - return - ((((((((((((((uint64_t)(*(tmp + 7) << 8) + *(tmp + 6)) << 8) - + *(tmp + 5)) << 8) + *(tmp + 4)) << 8) - + *(tmp + 3)) << 8) + *(tmp + 2)) << 8) - + *(tmp + 1)) << 8) + *tmp); - } - - - /** - * Write bytes to this buffer. - * - * @param src source byte buffer. - * @param len number of bytes to write. - **/ - void writeBytes(const void *src, size_t len) - { - ensureFree(len); - memcpy(_freept, src, len); - _freept += len; - } - - /** - * Fill buffer with zero-bytes. - * - * @param len number of zero-bytes to write. - **/ - void zeroFill(size_t len) - { - ensureFree(len); - memset(_freept, 0, len); - _freept += len; - } - - /** - * Read bytes from this buffer. - * - * @param dst destination byte buffer. - * @param len number of bytes to read. - **/ - void readBytes(void *dst, size_t len) - { - memcpy(dst, _datapt, len); - _datapt += len; - } - - /** - * Peek at bytes in this buffer. Unlike a read operation, this will - * not modify the buffer. - * - * @param dst destination byte buffer. - * @param len number of bytes to extract. - * @param offset byte offset into the buffer. - **/ - void peekBytes(void *dst, size_t len, size_t offset) - { - assert(_freept >= _datapt + offset + len); - memcpy(dst, _datapt + offset, len); - } - - /** - * Check if the data stored in this buffer equals the data stored in - * another buffer. - * - * @return true(equal)/false(not equal) - * @param other the other buffer. - **/ - bool equals(DataBuffer *other); - - /** - * Print a human-readable representation of this buffer to - * stdout. This method may be used for debugging purposes. - **/ - void hexDump(); - - /** - * Run some asserts to verify that this databuffer is in a legal - * state. - **/ - void assertValid() - { - assert(_bufstart <= _datapt); - assert(_datapt <= _freept); - assert(_freept <= _bufend); - } - - /** - * @return true if this buffer is referencing external data. - **/ - bool referencesExternalData() const; - - /** - * Swap the data stored in this buffer with the data stored in - * another buffer. Neither buffer may use externally allocated - * memory when swap is called. - * - * @param other the other buffer. - **/ - void swap(DataBuffer &other); - - Alloc stealBuffer(); -}; - -} // namespace vespalib - diff --git a/staging_vespalib/src/vespa/vespalib/data/fileheader.cpp b/staging_vespalib/src/vespa/vespalib/data/fileheader.cpp index 3cd8190d894..a9e131b0c63 100644 --- a/staging_vespalib/src/vespa/vespalib/data/fileheader.cpp +++ b/staging_vespalib/src/vespa/vespalib/data/fileheader.cpp @@ -1,6 +1,7 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include "fileheader.h" #include <vespa/vespalib/stllike/asciistream.h> +#include <vespa/vespalib/data/databuffer.h> #include <vespa/fastos/file.h> #include <vespa/log/log.h> diff --git a/staging_vespalib/src/vespa/vespalib/data/fileheader.h b/staging_vespalib/src/vespa/vespalib/data/fileheader.h index 157703ef0a8..e4449a0c36a 100644 --- a/staging_vespalib/src/vespa/vespalib/data/fileheader.h +++ b/staging_vespalib/src/vespa/vespalib/data/fileheader.h @@ -1,7 +1,6 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #pragma once -#include "databuffer.h" #include <vespa/vespalib/util/exception.h> #include <map> @@ -9,6 +8,7 @@ class FastOS_FileInterface; namespace vespalib { +class DataBuffer; class asciistream; /** |