aboutsummaryrefslogtreecommitdiffstats
path: root/document
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2020-01-20 13:04:49 +0000
committerHenning Baldersheim <balder@yahoo-inc.com>2020-01-20 13:04:49 +0000
commit1d3fe1bedb648cfd497eeee61478fa45f332255b (patch)
tree7519f9f9d87dd9e89a788a596ca865bd5330cac5 /document
parent5eaae9afb93ad82a931e117a14babdbb271762c6 (diff)
GC a load of unused code. ByteBuffer towards read only.
Diffstat (limited to 'document')
-rw-r--r--document/src/tests/documenttestcase.cpp7
-rw-r--r--document/src/tests/testbytebuffer.cpp264
-rw-r--r--document/src/vespa/document/fieldvalue/fieldvalue.cpp5
-rw-r--r--document/src/vespa/document/fieldvalue/fieldvalue.h9
-rw-r--r--document/src/vespa/document/util/CMakeLists.txt2
-rw-r--r--document/src/vespa/document/util/bufferexceptions.h8
-rw-r--r--document/src/vespa/document/util/bytebuffer.cpp315
-rw-r--r--document/src/vespa/document/util/bytebuffer.h165
-rw-r--r--document/src/vespa/document/util/serializable.cpp72
-rw-r--r--document/src/vespa/document/util/serializable.h97
-rw-r--r--document/src/vespa/document/util/serializableexceptions.cpp21
-rw-r--r--document/src/vespa/document/util/serializableexceptions.h8
12 files changed, 76 insertions, 897 deletions
diff --git a/document/src/tests/documenttestcase.cpp b/document/src/tests/documenttestcase.cpp
index 39a92352a5e..a8d4829d355 100644
--- a/document/src/tests/documenttestcase.cpp
+++ b/document/src/tests/documenttestcase.cpp
@@ -891,14 +891,11 @@ TEST(DocumentTest, testGenerateSerializedFile)
CompressionConfig newCfg(CompressionConfig::LZ4, 9, 95);
const_cast<StructDataType &>(doc.getType().getFieldsType()).setCompressionConfig(newCfg);
- ByteBuffer lz4buf(getSerializedSize(doc));
-
- doc.serialize(lz4buf);
- lz4buf.flip();
+ nbostream lz4buf = doc.serialize();
fd = open((serializedDir + "/serializecpp-lz4-level9.dat").c_str(),
O_WRONLY | O_TRUNC | O_CREAT, 0644);
- if (write(fd, lz4buf.getBufferAtPos(), lz4buf.getRemaining()) != (ssize_t)lz4buf.getRemaining()) {
+ if (write(fd, lz4buf.c_str(), lz4buf.size()) != (ssize_t)lz4buf.size()) {
throw vespalib::Exception("write failed");
}
close(fd);
diff --git a/document/src/tests/testbytebuffer.cpp b/document/src/tests/testbytebuffer.cpp
index b90db980683..b4ad558b4ad 100644
--- a/document/src/tests/testbytebuffer.cpp
+++ b/document/src/tests/testbytebuffer.cpp
@@ -3,12 +3,14 @@
#include <vespa/document/util/stringutil.h>
#include <vespa/document/util/bytebuffer.h>
#include <vespa/document/fieldvalue/serializablearray.h>
-#include <iostream>
-#include <vespa/vespalib/util/macro.h>
#include <vespa/document/util/bufferexceptions.h>
+#include <vespa/vespalib/util/macro.h>
+#include <vespa/vespalib/util/growablebytebuffer.h>
#include <gtest/gtest.h>
+
using namespace document;
+using vespalib::GrowableByteBuffer;
namespace {
@@ -29,9 +31,10 @@ TEST(ByteBuffer_Test, test_constructors)
TEST(ByteBuffer_Test, test_copy_constructor)
{
try {
- ByteBuffer b1(100);
- b1.putInt(1);
- b1.putInt(2);
+ GrowableByteBuffer gb(100);
+ gb.putInt(1);
+ gb.putInt(2);
+ ByteBuffer b1(gb.getBuffer(), gb.position());
ByteBuffer b2(b1);
@@ -40,10 +43,9 @@ TEST(ByteBuffer_Test, test_copy_constructor)
EXPECT_EQ(b1.getRemaining(),b2.getRemaining());
int test = 0;
- b2.flip();
- b2.getInt(test);
+ b2.getIntNetwork(test);
EXPECT_EQ(1,test);
- b2.getInt(test);
+ b2.getIntNetwork(test);
EXPECT_EQ(2,test);
} catch (std::exception &e) {
@@ -51,252 +53,6 @@ TEST(ByteBuffer_Test, test_copy_constructor)
}
}
-TEST(ByteBuffer_Test, test_putGetFlip)
-{
- ByteBuffer newBuf(100);
-
- try {
- newBuf.putInt(10);
- int test;
- newBuf.flip();
-
- newBuf.getInt(test);
- EXPECT_EQ(test, 10);
-
- newBuf.clear();
- newBuf.putDouble(3.35);
- newBuf.flip();
- EXPECT_EQ(newBuf.getRemaining(), 100);
- double test2;
- newBuf.getDouble(test2);
- EXPECT_TRUE(test2==3.35);
-
- newBuf.clear();
- newBuf.putBytes("heisann",8);
- newBuf.putInt(4);
- EXPECT_EQ(newBuf.getPos(), 12);
- EXPECT_EQ(newBuf.getLength(), 100);
- newBuf.flip();
- EXPECT_EQ(newBuf.getRemaining(), 100);
-
- char testStr[12];
- newBuf.getBytes(testStr, 8);
- EXPECT_TRUE(strcmp(testStr,"heisann")==0);
- newBuf.getInt(test);
- EXPECT_TRUE(test==4);
- } catch (std::exception &e) {
- FAIL() << "Unexpected exception at " << VESPA_STRLOC << ": \"" << e.what() << "\"";
- }
-}
-
-
-TEST(ByteBuffer_Test, test_NumberEncodings)
-{
- ByteBuffer buf(1024);
-
- // Check 0
- buf.putInt1_2_4Bytes(124);
- buf.putInt2_4_8Bytes(124);
- buf.putInt1_4Bytes(124);
- // Check 1
- buf.putInt1_2_4Bytes(127);
- buf.putInt2_4_8Bytes(127);
- buf.putInt1_4Bytes(127);
- // Check 2
- buf.putInt1_2_4Bytes(128);
- buf.putInt2_4_8Bytes(128);
- buf.putInt1_4Bytes(128);
- // Check 3
- buf.putInt1_2_4Bytes(255);
- buf.putInt2_4_8Bytes(255);
- buf.putInt1_4Bytes(255);
- // Check 4
- buf.putInt1_2_4Bytes(256);
- buf.putInt2_4_8Bytes(256);
- buf.putInt1_4Bytes(256);
- // Check 5
- buf.putInt1_2_4Bytes(0);
- buf.putInt2_4_8Bytes(0);
- buf.putInt1_4Bytes(0);
- // Check 6
- buf.putInt1_2_4Bytes(1);
- buf.putInt2_4_8Bytes(1);
- buf.putInt1_4Bytes(1);
-
- // Check 7
- try {
- buf.putInt1_2_4Bytes(0x7FFFFFFF);
- FAIL() << "Expected input out of range exception";
- } catch (InputOutOfRangeException& e) { }
- buf.putInt2_4_8Bytes(0x7FFFFFFFll);
- buf.putInt1_4Bytes(0x7FFFFFFF);
-
- try {
- buf.putInt2_4_8Bytes(0x7FFFFFFFFFFFFFFFll);
- FAIL() << "Expected input out of range exception";
- } catch (InputOutOfRangeException& e) { }
-
- buf.putInt1_2_4Bytes(0x7FFF);
- // Check 8
- buf.putInt2_4_8Bytes(0x7FFFll);
- buf.putInt1_4Bytes(0x7FFF);
- buf.putInt1_2_4Bytes(0x7F);
- // Check 9
- buf.putInt2_4_8Bytes(0x7Fll);
- buf.putInt1_4Bytes(0x7F);
-
- try {
- buf.putInt1_2_4Bytes(-1);
- FAIL() << "Expected input out of range exception";
- } catch (InputOutOfRangeException& e) { }
- try {
- buf.putInt2_4_8Bytes(-1);
- FAIL() << "Expected input out of range exception";
- } catch (InputOutOfRangeException& e) { }
- try {
- buf.putInt1_4Bytes(-1);
- FAIL() << "Expected input out of range exception";
- } catch (InputOutOfRangeException& e) { }
-
- try {
- buf.putInt1_2_4Bytes(-0x7FFFFFFF);
- FAIL() << "Expected input out of range exception";
- } catch (InputOutOfRangeException& e) { }
- try {
- buf.putInt2_4_8Bytes(-0x7FFFFFFF);
- FAIL() << "Expected input out of range exception";
- } catch (InputOutOfRangeException& e) { }
- try {
- buf.putInt1_4Bytes(-0x7FFFFFFF);
- FAIL() << "Expected input out of range exception";
- } catch (InputOutOfRangeException& e) { }
-
- try {
- buf.putInt2_4_8Bytes(-0x7FFFFFFFFFFFFFFFll);
- FAIL() << "Expected input out of range exception";
- } catch (InputOutOfRangeException& e) { }
-
- uint32_t endWritePos = buf.getPos();
- buf.setPos(0);
-
- int32_t tmp32;
- int64_t tmp64;
-
- // Check 0
- buf.getInt1_2_4Bytes(tmp32);
- EXPECT_EQ(124, tmp32);
- buf.getInt2_4_8Bytes(tmp64);
- EXPECT_EQ((int64_t)124, tmp64);
- buf.getInt1_4Bytes(tmp32);
- EXPECT_EQ(124, tmp32);
- // Check 1
- buf.getInt1_2_4Bytes(tmp32);
- EXPECT_EQ(127, tmp32);
- buf.getInt2_4_8Bytes(tmp64);
- EXPECT_EQ((int64_t)127, tmp64);
- buf.getInt1_4Bytes(tmp32);
- EXPECT_EQ(127, tmp32);
- // Check 2
- buf.getInt1_2_4Bytes(tmp32);
- EXPECT_EQ(128, tmp32);
- buf.getInt2_4_8Bytes(tmp64);
- EXPECT_EQ((int64_t)128, tmp64);
- buf.getInt1_4Bytes(tmp32);
- EXPECT_EQ(128, tmp32);
- // Check 3
- buf.getInt1_2_4Bytes(tmp32);
- EXPECT_EQ(255, tmp32);
- buf.getInt2_4_8Bytes(tmp64);
- EXPECT_EQ((int64_t)255, tmp64);
- buf.getInt1_4Bytes(tmp32);
- EXPECT_EQ(255, tmp32);
- // Check 4
- buf.getInt1_2_4Bytes(tmp32);
- EXPECT_EQ(256, tmp32);
- buf.getInt2_4_8Bytes(tmp64);
- EXPECT_EQ((int64_t)256, tmp64);
- buf.getInt1_4Bytes(tmp32);
- EXPECT_EQ(256, tmp32);
- // Check 5
- buf.getInt1_2_4Bytes(tmp32);
- EXPECT_EQ(0, tmp32);
- buf.getInt2_4_8Bytes(tmp64);
- EXPECT_EQ((int64_t)0, tmp64);
- buf.getInt1_4Bytes(tmp32);
- EXPECT_EQ(0, tmp32);
- // Check 6
- buf.getInt1_2_4Bytes(tmp32);
- EXPECT_EQ(1, tmp32);
- buf.getInt2_4_8Bytes(tmp64);
- EXPECT_EQ((int64_t)1, tmp64);
- buf.getInt1_4Bytes(tmp32);
- EXPECT_EQ(1, tmp32);
- // Check 7
- buf.getInt2_4_8Bytes(tmp64);
- EXPECT_EQ((int64_t)0x7FFFFFFF, tmp64);
- buf.getInt1_4Bytes(tmp32);
- EXPECT_EQ(0x7FFFFFFF, tmp32);
- buf.getInt1_2_4Bytes(tmp32);
- EXPECT_EQ(0x7FFF, tmp32);
- // Check 8
- buf.getInt2_4_8Bytes(tmp64);
- EXPECT_EQ((int64_t)0x7FFF, tmp64);
- buf.getInt1_4Bytes(tmp32);
- EXPECT_EQ(0x7FFF, tmp32);
- buf.getInt1_2_4Bytes(tmp32);
- EXPECT_EQ(0x7F, tmp32);
- // Check 9
- buf.getInt2_4_8Bytes(tmp64);
- EXPECT_EQ((int64_t)0x7F, tmp64);
- buf.getInt1_4Bytes(tmp32);
- EXPECT_EQ(0x7F, tmp32);
-
- uint32_t endReadPos = buf.getPos();
- EXPECT_EQ(endWritePos, endReadPos);
-
-}
-
-TEST(ByteBuffer_Test, test_NumberLengths)
-{
- EXPECT_EQ((size_t) 1, ByteBuffer::getSerializedSize1_4Bytes(0));
- EXPECT_EQ((size_t) 1, ByteBuffer::getSerializedSize1_4Bytes(1));
- EXPECT_EQ((size_t) 1, ByteBuffer::getSerializedSize1_4Bytes(4));
- EXPECT_EQ((size_t) 1, ByteBuffer::getSerializedSize1_4Bytes(31));
- EXPECT_EQ((size_t) 1, ByteBuffer::getSerializedSize1_4Bytes(126));
- EXPECT_EQ((size_t) 1, ByteBuffer::getSerializedSize1_4Bytes(127));
- EXPECT_EQ((size_t) 4, ByteBuffer::getSerializedSize1_4Bytes(128));
- EXPECT_EQ((size_t) 4, ByteBuffer::getSerializedSize1_4Bytes(129));
- EXPECT_EQ((size_t) 4, ByteBuffer::getSerializedSize1_4Bytes(255));
- EXPECT_EQ((size_t) 4, ByteBuffer::getSerializedSize1_4Bytes(256));
- EXPECT_EQ((size_t) 4, ByteBuffer::getSerializedSize1_4Bytes(0x7FFFFFFF));
-
- EXPECT_EQ((size_t) 2, ByteBuffer::getSerializedSize2_4_8Bytes(0));
- EXPECT_EQ((size_t) 2, ByteBuffer::getSerializedSize2_4_8Bytes(1));
- EXPECT_EQ((size_t) 2, ByteBuffer::getSerializedSize2_4_8Bytes(4));
- EXPECT_EQ((size_t) 2, ByteBuffer::getSerializedSize2_4_8Bytes(31));
- EXPECT_EQ((size_t) 2, ByteBuffer::getSerializedSize2_4_8Bytes(126));
- EXPECT_EQ((size_t) 2, ByteBuffer::getSerializedSize2_4_8Bytes(127));
- EXPECT_EQ((size_t) 2, ByteBuffer::getSerializedSize2_4_8Bytes(128));
- EXPECT_EQ((size_t) 2, ByteBuffer::getSerializedSize2_4_8Bytes(32767));
- EXPECT_EQ((size_t) 4, ByteBuffer::getSerializedSize2_4_8Bytes(32768));
- EXPECT_EQ((size_t) 4, ByteBuffer::getSerializedSize2_4_8Bytes(32769));
- EXPECT_EQ((size_t) 4, ByteBuffer::getSerializedSize2_4_8Bytes(1030493));
- EXPECT_EQ((size_t) 4, ByteBuffer::getSerializedSize2_4_8Bytes(0x3FFFFFFF));
- EXPECT_EQ((size_t) 8, ByteBuffer::getSerializedSize2_4_8Bytes(0x40000000));
- EXPECT_EQ((size_t) 8, ByteBuffer::getSerializedSize2_4_8Bytes(0x40000001));
-
- EXPECT_EQ((size_t) 1, ByteBuffer::getSerializedSize1_2_4Bytes(0));
- EXPECT_EQ((size_t) 1, ByteBuffer::getSerializedSize1_2_4Bytes(1));
- EXPECT_EQ((size_t) 1, ByteBuffer::getSerializedSize1_2_4Bytes(4));
- EXPECT_EQ((size_t) 1, ByteBuffer::getSerializedSize1_2_4Bytes(31));
- EXPECT_EQ((size_t) 1, ByteBuffer::getSerializedSize1_2_4Bytes(126));
- EXPECT_EQ((size_t) 1, ByteBuffer::getSerializedSize1_2_4Bytes(127));
- EXPECT_EQ((size_t) 2, ByteBuffer::getSerializedSize1_2_4Bytes(128));
- EXPECT_EQ((size_t) 2, ByteBuffer::getSerializedSize1_2_4Bytes(16383));
- EXPECT_EQ((size_t) 4, ByteBuffer::getSerializedSize1_2_4Bytes(16384));
- EXPECT_EQ((size_t) 4, ByteBuffer::getSerializedSize1_2_4Bytes(16385));
-}
-
TEST(ByteBuffer_Test, test_SerializableArray)
{
SerializableArray array;
diff --git a/document/src/vespa/document/fieldvalue/fieldvalue.cpp b/document/src/vespa/document/fieldvalue/fieldvalue.cpp
index c41f6cbb616..49efbd83c61 100644
--- a/document/src/vespa/document/fieldvalue/fieldvalue.cpp
+++ b/document/src/vespa/document/fieldvalue/fieldvalue.cpp
@@ -35,11 +35,6 @@ void FieldValue::serialize(nbostream &stream) const {
serializer.write(*this);
}
-void FieldValue::serialize(ByteBuffer& buffer) const {
- nbostream stream = serialize();
- buffer.putBytes(stream.peek(), stream.size());
-}
-
nbostream
FieldValue::serialize() const {
nbostream stream;
diff --git a/document/src/vespa/document/fieldvalue/fieldvalue.h b/document/src/vespa/document/fieldvalue/fieldvalue.h
index 4b754f09cd6..9ed8d522b17 100644
--- a/document/src/vespa/document/fieldvalue/fieldvalue.h
+++ b/document/src/vespa/document/fieldvalue/fieldvalue.h
@@ -19,15 +19,11 @@
#include <vespa/vespalib/objects/identifiable.h>
#include <vespa/vespalib/util/polymorphicarraybase.h>
-namespace vespalib {
- class nbostream;
-}
+namespace vespalib { class nbostream; }
namespace document {
-namespace fieldvalue {
- class IteratorHandler;
-}
+namespace fieldvalue { class IteratorHandler; }
class ByteBuffer;
class DataType;
@@ -73,7 +69,6 @@ public:
virtual bool isA(const FieldValue& other) const;
void serialize(vespalib::nbostream &stream) const;
- void serialize(ByteBuffer& buffer) const;
vespalib::nbostream serialize() const;
/**
diff --git a/document/src/vespa/document/util/CMakeLists.txt b/document/src/vespa/document/util/CMakeLists.txt
index 8cb148abe25..48ca7bd36d7 100644
--- a/document/src/vespa/document/util/CMakeLists.txt
+++ b/document/src/vespa/document/util/CMakeLists.txt
@@ -3,7 +3,7 @@ vespa_add_library(document_util OBJECT
SOURCES
bytebuffer.cpp
printable.cpp
- serializable.cpp
+ serializableexceptions.cpp
stringutil.cpp
DEPENDS
AFTER
diff --git a/document/src/vespa/document/util/bufferexceptions.h b/document/src/vespa/document/util/bufferexceptions.h
index 8a3215f6c79..aee7f3ae568 100644
--- a/document/src/vespa/document/util/bufferexceptions.h
+++ b/document/src/vespa/document/util/bufferexceptions.h
@@ -15,13 +15,5 @@ public:
VESPA_DEFINE_EXCEPTION_SPINE(BufferOutOfBoundsException)
};
-class InputOutOfRangeException : public vespalib::IoException {
-public:
- InputOutOfRangeException(const vespalib::string& msg,
- const vespalib::string& location = "");
-
- VESPA_DEFINE_EXCEPTION_SPINE(InputOutOfRangeException)
-};
-
}
diff --git a/document/src/vespa/document/util/bytebuffer.cpp b/document/src/vespa/document/util/bytebuffer.cpp
index 415aeebf969..c909ca5fe61 100644
--- a/document/src/vespa/document/util/bytebuffer.cpp
+++ b/document/src/vespa/document/util/bytebuffer.cpp
@@ -13,11 +13,42 @@
#include <arpa/inet.h>
using vespalib::alloc::Alloc;
+using vespalib::make_string;
namespace document {
+namespace {
+
+static void throwOutOfBounds(size_t want, size_t has) __attribute__((noinline, noreturn));
+
+void throwOutOfBounds(size_t want, size_t has)
+{
+ throw BufferOutOfBoundsException(want, has, VESPA_STRLOC);
+}
+
+}
+
+#if defined(__i386__) || defined(__x86_64__)
+
+template<typename T>
+void
+ByteBuffer::getDoubleLongNetwork(T &val) {
+ //TODO: Change this if we move to big-endian hardware
+ if (__builtin_expect(getRemaining() < (int)sizeof(T), 0)) {
+ throwOutOfBounds(sizeof(T), getRemaining());
+ }
+
+ auto * data = reinterpret_cast<unsigned char*>(&val);
+ for (int i=sizeof(T)-1; i>=0; --i) {
+ getByte(data[i]);
+ }
+}
+
+#else
+#error "getDoubleLongNetwork is undefined for this arcitecture"
+#endif
+
VESPA_IMPLEMENT_EXCEPTION_SPINE(BufferOutOfBoundsException);
-VESPA_IMPLEMENT_EXCEPTION_SPINE(InputOutOfRangeException);
vespalib::string BufferOutOfBoundsException::createMessage(size_t pos, size_t len) {
vespalib::asciistream ost;
@@ -25,18 +56,11 @@ vespalib::string BufferOutOfBoundsException::createMessage(size_t pos, size_t le
return ost.str();
}
-BufferOutOfBoundsException::BufferOutOfBoundsException(
- size_t pos, size_t len, const vespalib::string& location)
+BufferOutOfBoundsException::BufferOutOfBoundsException(size_t pos, size_t len, const vespalib::string& location)
: IoException(createMessage(pos, len), IoException::NO_SPACE, location, 1)
{
}
-InputOutOfRangeException::InputOutOfRangeException(
- const vespalib::string& msg, const vespalib::string& location)
- : IoException(msg, IoException::INTERNAL_FAILURE, location, 1)
-{
-}
-
ByteBuffer::ByteBuffer(size_t len) :
ByteBuffer(Alloc::alloc(len), len)
{
@@ -74,11 +98,6 @@ ByteBuffer::ByteBuffer(const ByteBuffer& rhs) :
ByteBuffer::~ByteBuffer() = default;
-void ByteBuffer::throwOutOfBounds(size_t want, size_t has)
-{
- throw BufferOutOfBoundsException(want, has, VESPA_STRLOC);
-}
-
ByteBuffer* ByteBuffer::copyBuffer(const char* buffer, size_t len)
{
if (buffer && len) {
@@ -119,15 +138,6 @@ void ByteBuffer::getNumeric(uint8_t & v) {
}
}
-void ByteBuffer::putNumeric(uint8_t v) {
- if (__builtin_expect(getRemaining() < sizeof(v), 0)) {
- throwOutOfBounds(getRemaining(), sizeof(v));
- } else {
- *(uint8_t *) getBufferAtPos() = v;
- incPosNoCheck(sizeof(v));
- }
-}
-
void ByteBuffer::getNumericNetwork(int16_t & v) {
if (__builtin_expect(getRemaining() < sizeof(v), 0)) {
throwOutOfBounds(getRemaining(), sizeof(v));
@@ -138,16 +148,6 @@ void ByteBuffer::getNumericNetwork(int16_t & v) {
}
}
-void ByteBuffer::putNumericNetwork(int16_t v) {
- if (__builtin_expect(getRemaining() < sizeof(v), 0)) {
- throwOutOfBounds(getRemaining(), sizeof(v));
- } else {
- uint16_t val = htons(v);
- *(uint16_t *) (void *) getBufferAtPos() = val;
- incPosNoCheck(sizeof(v));
- }
-}
-
void ByteBuffer::getNumericNetwork(int32_t & v) {
if (__builtin_expect(getRemaining() < sizeof(v), 0)) {
throwOutOfBounds(getRemaining(), sizeof(v));
@@ -158,44 +158,6 @@ void ByteBuffer::getNumericNetwork(int32_t & v) {
}
}
-void ByteBuffer::getNumeric(int32_t & v) {
- if (__builtin_expect(getRemaining() < sizeof(v), 0)) {
- throwOutOfBounds(getRemaining(), sizeof(v));
- } else {
- v = *(int32_t *) (void *) getBufferAtPos();
- incPosNoCheck(sizeof(v));
- }
-}
-
-
-void ByteBuffer::putNumericNetwork(int32_t v) {
- if (__builtin_expect(getRemaining() < sizeof(v), 0)) {
- throwOutOfBounds(getRemaining(), sizeof(v));
- } else {
- uint32_t val = htonl(v);
- *(uint32_t *) (void *) getBufferAtPos() = val;
- incPosNoCheck(sizeof(v));
- }
-}
-
-void ByteBuffer::putNumeric(int32_t v) {
- if (__builtin_expect(getRemaining() < sizeof(v), 0)) {
- throwOutOfBounds(getRemaining(), sizeof(v));
- } else {
- *(int32_t *) (void *) getBufferAtPos() = v;
- incPosNoCheck(sizeof(v));
- }
-}
-
-void ByteBuffer::getNumeric(float & v) {
- if (__builtin_expect(getRemaining() < sizeof(v), 0)) {
- throwOutOfBounds(getRemaining(), sizeof(v));
- } else {
- v = *(float *) (void *) getBufferAtPos();
- incPosNoCheck(sizeof(v));
- }
-}
-
void ByteBuffer::getNumeric(int64_t& v) {
if (__builtin_expect(getRemaining() < sizeof(v), 0)) {
throwOutOfBounds(getRemaining(), sizeof(v));
@@ -205,223 +167,14 @@ void ByteBuffer::getNumeric(int64_t& v) {
}
}
-void ByteBuffer::getNumeric(double& v) {
- if (__builtin_expect(getRemaining() < sizeof(v), 0)) {
- throwOutOfBounds(getRemaining(), sizeof(v));
- } else {
- v = *(double *) (void *) getBufferAtPos();
- incPosNoCheck(sizeof(v));
- }
-}
-void ByteBuffer::putNumeric(double v) {
- if (__builtin_expect(getRemaining() < sizeof(v), 0)) {
- throwOutOfBounds(getRemaining(), sizeof(v));
- } else {
- *(double *) (void *) getBufferAtPos() = v;
- incPosNoCheck(sizeof(v));
- }
-}
-
void ByteBuffer::getNumericNetwork(double & v) {
getDoubleLongNetwork(v);
}
-void ByteBuffer::putNumericNetwork(int64_t v) {
- putDoubleLongNetwork(v);
-}
-void ByteBuffer::putNumericNetwork(double v) {
- putDoubleLongNetwork(v);
-}
+
void ByteBuffer::getNumericNetwork(int64_t & v) {
getDoubleLongNetwork(v);
}
-void ByteBuffer::putInt2_4_8Bytes(int64_t number, size_t len) {
- if (number < 0ll) {
- throw InputOutOfRangeException(vespalib::make_string(
- "Cannot encode negative number."), VESPA_STRLOC);
- } else if (number > 0x3FFFFFFFFFFFFFFFll) {
- throw InputOutOfRangeException(vespalib::make_string(
- "Cannot encode number larger than 2^62."), VESPA_STRLOC);
- }
-
- if (len == 0) {
- if (number < 0x8000ll) {
- //length 2 bytes
- putShortNetwork((int16_t) number);
- } else if (number < 0x40000000ll) {
- //length 4 bytes
- putIntNetwork(((int32_t) number) | 0x80000000);
- } else {
- //length 8 bytes
- putLongNetwork(number | 0xC000000000000000ll);
- }
- } else if (len == 2) {
- //length 2 bytes
- putShortNetwork((int16_t) number);
- } else if (len == 4) {
- //length 4 bytes
- putIntNetwork(((int32_t) number) | 0x80000000);
- } else if (len == 8) {
- //length 8 bytes
- putLongNetwork(number | 0xC000000000000000ll);
- } else {
- throw InputOutOfRangeException(vespalib::make_string(
- "Cannot encode number using %d bytes.", (int)len), VESPA_STRLOC);
- }
-}
-
-void ByteBuffer::getInt2_4_8Bytes(int64_t & v) {
- if (getRemaining() >= 2) {
- uint8_t flagByte = peekByte();
-
- if (flagByte & 0x80) {
- if (flagByte & 0x40) {
- //length 8 bytes
- int64_t tmp;
- getLongNetwork(tmp);
- v = tmp & 0x3FFFFFFFFFFFFFFFll;
- } else {
- //length 4 bytes
- int32_t tmp;
- getIntNetwork(tmp);
- v = (int64_t) (tmp & 0x3FFFFFFF);
- }
- } else {
- //length 2 bytes
- int16_t tmp;
- getShortNetwork(tmp);
- v = (int64_t) tmp;
- }
- } else {
- throwOutOfBounds(getRemaining(), 2);
- }
-}
-
-size_t ByteBuffer::getSerializedSize2_4_8Bytes(int64_t number) {
- if (number < 0ll) {
- throw InputOutOfRangeException(vespalib::make_string(
- "Cannot encode negative number."), VESPA_STRLOC);
- } else if (number > 0x3FFFFFFFFFFFFFFFll) {
- throw InputOutOfRangeException(vespalib::make_string(
- "Cannot encode number larger than 2^62."), VESPA_STRLOC);
- }
-
- if (number < 0x8000ll) {
- return 2;
- } else if (number < 0x40000000ll) {
- return 4;
- } else {
- return 8;
- }
-}
-
-void ByteBuffer::putInt1_2_4Bytes(int32_t number) {
- if (number < 0) {
- throw InputOutOfRangeException(vespalib::make_string(
- "Cannot encode negative number."), VESPA_STRLOC);
- } else if (number > 0x3FFFFFFF) {
- throw InputOutOfRangeException(vespalib::make_string(
- "Cannot encode number larger than 2^30."), VESPA_STRLOC);
- }
-
- if (number < 0x80) {
- putByte((unsigned char) number);
- } else if (number < 0x4000) {
- putShortNetwork((int16_t) (((int16_t)number) | ((int16_t) 0x8000)));
- } else {
- putIntNetwork(number | 0xC0000000);
- }
-}
-
-void ByteBuffer::getInt1_2_4Bytes(int32_t & v) {
- if (getRemaining() >= 1) {
- unsigned char flagByte = peekByte();
-
- if (flagByte & 0x80) {
- if (flagByte & 0x40) {
- //length 4 bytes
- int32_t tmp;
- getIntNetwork(tmp);
- v = tmp & 0x3FFFFFFF;
- } else {
- //length 2 bytes
- int16_t tmp;
- getShortNetwork(tmp);
- v = (int32_t) (tmp & ((int16_t) 0x3FFF));
- }
- } else {
- v = (int32_t) flagByte;
- incPosNoCheck(1);
- }
- } else {
- throwOutOfBounds(getRemaining(), 1);
- }
-}
-
-size_t ByteBuffer::getSerializedSize1_2_4Bytes(int32_t number) {
- if (number < 0) {
- throw InputOutOfRangeException(vespalib::make_string(
- "Cannot encode negative number."), VESPA_STRLOC);
- } else if (number > 0x3FFFFFFF) {
- throw InputOutOfRangeException(vespalib::make_string(
- "Cannot encode number larger than 2^30."), VESPA_STRLOC);
- }
-
- if (number < 0x80) {
- return 1;
- } else if (number < 0x4000) {
- return 2;
- } else {
- return 4;
- }
-}
-void ByteBuffer::putInt1_4Bytes(int32_t number) {
- if (number < 0) {
- throw InputOutOfRangeException(vespalib::make_string(
- "Cannot encode negative number."), VESPA_STRLOC);
- } else if (number > 0x7FFFFFFF) {
- throw InputOutOfRangeException(vespalib::make_string(
- "Cannot encode number larger than 2^31."), VESPA_STRLOC);
- }
-
- if (number < 0x80) {
- putByte((unsigned char) number);
- } else {
- putIntNetwork(number | 0x80000000);
- }
-}
-void ByteBuffer::getInt1_4Bytes(int32_t & v) {
- if (getRemaining() >= 1) {
- unsigned char flagByte = peekByte();
-
- if (flagByte & 0x80) {
- //length 4 bytes
- int32_t tmp;
- getIntNetwork(tmp);
- v = tmp & 0x7FFFFFFF;
- } else {
- v = (int32_t) flagByte;
- incPosNoCheck(1);
- }
- } else {
- throwOutOfBounds(getRemaining(), 1);
- }
-}
-size_t ByteBuffer::getSerializedSize1_4Bytes(int32_t number) {
- if (number < 0) {
- throw InputOutOfRangeException(vespalib::make_string(
- "Cannot encode negative number."), VESPA_STRLOC);
- } else if (number > 0x7FFFFFFF) {
- throw InputOutOfRangeException(vespalib::make_string(
- "Cannot encode number larger than 2^31."), VESPA_STRLOC);
- }
-
- if (number < 0x80) {
- return 1;
- } else {
- return 4;
- }
-}
void ByteBuffer::getBytes(void *buffer, size_t count)
{
const char *v = getBufferAtPos();
diff --git a/document/src/vespa/document/util/bytebuffer.h b/document/src/vespa/document/util/bytebuffer.h
index b97c9ecee1a..ce01231602d 100644
--- a/document/src/vespa/document/util/bytebuffer.h
+++ b/document/src/vespa/document/util/bytebuffer.h
@@ -98,10 +98,6 @@ public:
*/
void incPos(size_t pos);
- void incPosNoCheck(size_t pos) {
- _pos += pos;
- }
-
/**
* Resets pos to 0, and sets limit to old pos. Use this before reading
* from a buffer you have written to
@@ -110,175 +106,21 @@ public:
_pos = 0;
}
- /**
- * Sets pos to 0 and limit to length. Use this to start writing from the
- * start of the buffer.
- */
- void clear() {
- _pos=0;
- }
-
void getNumeric(uint8_t & v);
- void putNumeric(uint8_t v);
void getNumericNetwork(int16_t & v);
- void putNumericNetwork(int16_t v);
void getNumericNetwork(int32_t & v);
- void getNumeric(int32_t & v);
- void putNumericNetwork(int32_t v);
- void putNumeric(int32_t v);
- void getNumeric(float & v);
void getNumericNetwork(int64_t & v);
void getNumeric(int64_t& v);
- void putNumericNetwork(int64_t v);
void getNumericNetwork(double & v);
- void getNumeric(double& v);
- void putNumericNetwork(double v);
- void putNumeric(double v);
+ void getChar(char & val) { unsigned char t;getByte(t); val=t; }
void getByte(uint8_t & v) { getNumeric(v); }
- void putByte(uint8_t v) { putNumeric(v); }
void getShortNetwork(int16_t & v) { getNumericNetwork(v); }
- void putShortNetwork(int16_t v) { putNumericNetwork(v); }
void getIntNetwork(int32_t & v) { getNumericNetwork(v); }
- void getInt(int32_t & v) { getNumeric(v); }
- void putIntNetwork(int32_t v) { putNumericNetwork(v); }
- void putInt(int32_t v) { putNumeric(v); }
- void getFloat(float & v) { getNumeric(v); }
void getLongNetwork(int64_t & v) { getNumericNetwork(v); }
void getLong(int64_t& v) { getNumeric(v); }
- void putLongNetwork(int64_t v) { putNumericNetwork(v); }
void getDoubleNetwork(double & v) { getNumericNetwork(v); }
- void getDouble(double& v) { getNumeric(v); }
- void putDoubleNetwork(double v) { putNumericNetwork(v); }
- void putDouble(double v) { putNumeric(v); }
-
- private:
- void throwOutOfBounds(size_t want, size_t has) __attribute__((noinline,noreturn));
- uint8_t peekByte() const { return *getBufferAtPos(); }
-
-#if defined(__i386__) || defined(__x86_64__)
-
- template<typename T>
- void putDoubleLongNetwork(T val) {
- //TODO: Change this if we move to big-endian hardware
- if (__builtin_expect(getRemaining() < (int)sizeof(T), 0)) {
- throwOutOfBounds(sizeof(T), getRemaining());
- }
- unsigned char* data = reinterpret_cast<unsigned char*>(&val);
- for (int i=sizeof(T)-1; i>=0; --i) {
- putByte(data[i]);
- }
- }
-
- template<typename T>
- void getDoubleLongNetwork(T &val) {
- //TODO: Change this if we move to big-endian hardware
- if (__builtin_expect(getRemaining() < (int)sizeof(T), 0)) {
- throwOutOfBounds(sizeof(T), getRemaining());
- }
-
- unsigned char* data = reinterpret_cast<unsigned char*>(&val);
- for (int i=sizeof(T)-1; i>=0; --i) {
- getByte(data[i]);
- }
- }
-#else
- #error "getDoubleLongNetwork is undefined for this arcitecture"
-#endif
-
- public:
- /**
- * Writes a 62-bit positive integer to the buffer, using 2, 4, or 8 bytes.
- *
- * @param number the integer to write
- */
- void putInt2_4_8Bytes(int64_t number) {
- putInt2_4_8Bytes(number, 0);
- }
-
- /**
- * Writes a 62-bit positive integer to the buffer, using 2, 4, or 8 bytes.
- *
- * @param number the integer to write
- * @param len if non-zero, force writing number using len bytes, possibly
- * with truncation
- */
- void putInt2_4_8Bytes(int64_t number, size_t len);
-
- /**
- * Reads a 62-bit positive integer from the buffer, which was written using
- * 2, 4, or 8 bytes.
- *
- * @param v the integer read
- */
- void getInt2_4_8Bytes(int64_t & v);
-
- /**
- * Computes the size used for storing the given integer using 2, 4 or 8
- * bytes.
- *
- * @param number the integer to check length of
- * @return the number of bytes used to store it; 2, 4 or 8
- */
- static size_t getSerializedSize2_4_8Bytes(int64_t number);
-
- /**
- * Writes a 30-bit positive integer to the buffer, using 1, 2, or 4 bytes.
- *
- * @param number the integer to write
- */
- void putInt1_2_4Bytes(int32_t number);
-
- /**
- * Reads a 30-bit positive integer from the buffer, which was written using
- * 1, 2, or 4 bytes.
- *
- * @param v the integer read
- */
- void getInt1_2_4Bytes(int32_t & v);
-
- /**
- * Computes the size used for storing the given integer using 1, 2 or 4
- * bytes.
- *
- * @param number the integer to check length of
- * @return the number of bytes used to store it; 1, 2 or 4
- */
- static size_t getSerializedSize1_2_4Bytes(int32_t number);
-
- /**
- * Writes a 31-bit positive integer to the buffer, using 1 or 4 bytes.
- *
- * @param number the integer to write
- */
- void putInt1_4Bytes(int32_t number);
-
- /**
- * Reads a 31-bit positive integer from the buffer, which was written using
- * 1 or 4 bytes.
- *
- * @param v the integer read
- */
- void getInt1_4Bytes(int32_t & v);
-
- /**
- * Computes the size used for storing the given integer using 1 or 4 bytes.
- *
- * @param number the integer to check length of
- * @return the number of bytes used to store it; 1 or 4
- */
- static size_t getSerializedSize1_4Bytes(int32_t number);
-
- /**
- * Writes a 8 bit integer to the buffer at the current position, and
- * increases the positition accordingly.
- *
- * @param val the int to store
- * @return True if the value could be stored, false if end of buffer is
- * reached
- */
- void getChar(char & val) { unsigned char t;getByte(t); val=t; }
/**
* Reads the given number of bytes into the given pointer, and updates the
@@ -301,6 +143,11 @@ public:
void putBytes(const void *buf, size_t count);
private:
+ template<typename T>
+ void getDoubleLongNetwork(T &val);
+
+ void incPosNoCheck(size_t pos) { _pos += pos; }
+
char * _buffer;
size_t _len;
size_t _pos;
diff --git a/document/src/vespa/document/util/serializable.cpp b/document/src/vespa/document/util/serializable.cpp
deleted file mode 100644
index e6881c598ac..00000000000
--- a/document/src/vespa/document/util/serializable.cpp
+++ /dev/null
@@ -1,72 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-
-#include "serializable.h"
-#include "bufferexceptions.h"
-#include "serializableexceptions.h"
-#include "bytebuffer.h"
-
-namespace document {
-
-IMPLEMENT_IDENTIFIABLE_ABSTRACT(Serializable, vespalib::Identifiable);
-IMPLEMENT_IDENTIFIABLE_ABSTRACT(Deserializable, Serializable);
-VESPA_IMPLEMENT_EXCEPTION_SPINE(DeserializeException);
-VESPA_IMPLEMENT_EXCEPTION_SPINE(SerializeException);
-
-std::unique_ptr<ByteBuffer>
-Serializable::serialize() const
-{
- size_t len = getSerializedSize();
- std::unique_ptr<ByteBuffer> retVal(new ByteBuffer(len));
- serialize(*retVal.get());
- return retVal;
-}
-
-DeserializeException::DeserializeException(const vespalib::string& msg, const vespalib::string& location)
- : IoException(msg, IoException::CORRUPT_DATA, location, 1)
-{
-}
-
-DeserializeException::DeserializeException(
- const vespalib::string& msg, const vespalib::Exception& cause,
- const vespalib::string& location)
- : IoException(msg, IoException::CORRUPT_DATA, cause, location, 1)
-{
-}
-
-SerializeException::SerializeException(const vespalib::string& msg, const vespalib::string& location)
- : IoException(msg, IoException::CORRUPT_DATA, location, 1)
-{
-}
-
-SerializeException::SerializeException(
- const vespalib::string& msg, const vespalib::Exception& cause,
- const vespalib::string& location)
- : IoException(msg, IoException::CORRUPT_DATA, cause, location, 1)
-{
-}
-
-void
-Serializable::serialize(ByteBuffer& buffer) const {
- int pos = buffer.getPos();
- try{
- onSerialize(buffer);
- } catch (...) {
- buffer.setPos(pos);
- throw;
- }
-}
-
-void
-Deserializable::deserialize(const DocumentTypeRepo &repo, ByteBuffer& buffer) {
- int pos = buffer.getPos();
- try {
- onDeserialize(repo, buffer);
- } catch (const DeserializeException &) {
- buffer.setPos(pos);
- throw;
- } catch (const BufferOutOfBoundsException &) {
- buffer.setPos(pos);
- throw;
- }
-}
-}
diff --git a/document/src/vespa/document/util/serializable.h b/document/src/vespa/document/util/serializable.h
deleted file mode 100644
index 8039b3cd90b..00000000000
--- a/document/src/vespa/document/util/serializable.h
+++ /dev/null
@@ -1,97 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-/**
- * @file serializable.h
- * @ingroup document
- *
- * @brief Interfaces to be used for serializing of objects.
- *
- * @author Thomas F. Gundersen, H�kon Humberset
- * @date 2004-03-15
- * @version $Id$
- */
-
-#pragma once
-
-#include "bytebuffer.h"
-#include "identifiableid.h"
-#include <vespa/vespalib/objects/cloneable.h>
-#include <vespa/vespalib/objects/identifiable.h>
-
-namespace document {
-class DocumentTypeRepo;
-
-/**
- * Base class for classes that can be converted into a bytestream,
- * normally used later to create a similar instance.
- */
-
-class Serializable : public vespalib::Identifiable
-{
-protected:
- virtual void onSerialize(ByteBuffer& buffer) const = 0;
-public:
- DECLARE_IDENTIFIABLE_ABSTRACT(Serializable);
-
- virtual ~Serializable() {}
-
- /**
- * @return An upper limit to how many bytes serialization of this instance
- * need, providing instance is not altered before serialization.
- */
- virtual size_t getSerializedSize() const = 0;
-
- /**
- * Serializes the instance into the buffer given. Use getSerializedSize()
- * before calling this method to be sure buffer is big enough.
- * On success, the given buffers position will be just past the serialized
- * version of this instance, on failure, position will be reset to whatever
- * it was prior to calling this function.
- *
- * @throw SerializeException If for some reason instance cannot be
- * serialized.
- * @throw BufferOutOfBoundsException If buffer does not have enough space.
- */
- void serialize(ByteBuffer& buffer) const;
-
- /**
- * Creates a bytebuffer with enough space to serialize this instance
- * and serialize this instance into it.
- *
- * @return The created bytebuffer, positioned after the serialization.
- *
- * @throw SerializeException If for some reason instance cannot be
- * serialized.
- * @throw BufferOutOfBoundsException If buffer does not have enough space.
- */
- std::unique_ptr<ByteBuffer> serialize() const;
-};
-
-/**
- * Base class for instances that can be overwritten from a bytestream,
- * given that the bytestream is created from a similar instance.
- */
-class Deserializable : public vespalib::Cloneable, public Serializable
-{
-protected:
- virtual void onDeserialize(const DocumentTypeRepo &repo, ByteBuffer& buffer) = 0;
-
-public:
- DECLARE_IDENTIFIABLE_ABSTRACT(Deserializable);
- virtual ~Deserializable() {}
-
- /**
- * Overwrite this object with the object represented by the given
- * bytestream. On success, buffer will be positioned after the bytestream
- * representing the instance we've just deserialized, on failure, bytebuffer
- * will be pointing to where it was pointing before calling this function.
- *
- * @throw DeserializeException If read data doesn't represent a legal object
- * of this type.
- * @throw BufferOutOfBoundsException If instance wants to read more data
- * than is available in the buffer.
- */
- void deserialize(const DocumentTypeRepo &repo, ByteBuffer& buffer);
-};
-
-}
-
diff --git a/document/src/vespa/document/util/serializableexceptions.cpp b/document/src/vespa/document/util/serializableexceptions.cpp
new file mode 100644
index 00000000000..e80e38015e8
--- /dev/null
+++ b/document/src/vespa/document/util/serializableexceptions.cpp
@@ -0,0 +1,21 @@
+// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#include "serializableexceptions.h"
+
+namespace document {
+
+VESPA_IMPLEMENT_EXCEPTION_SPINE(DeserializeException);
+
+DeserializeException::DeserializeException(const vespalib::string& msg, const vespalib::string& location)
+ : IoException(msg, IoException::CORRUPT_DATA, location, 1)
+{
+}
+
+DeserializeException::DeserializeException(
+ const vespalib::string& msg, const vespalib::Exception& cause,
+ const vespalib::string& location)
+ : IoException(msg, IoException::CORRUPT_DATA, cause, location, 1)
+{
+}
+
+}
diff --git a/document/src/vespa/document/util/serializableexceptions.h b/document/src/vespa/document/util/serializableexceptions.h
index fcfed810bfc..1b692aa27b7 100644
--- a/document/src/vespa/document/util/serializableexceptions.h
+++ b/document/src/vespa/document/util/serializableexceptions.h
@@ -24,12 +24,4 @@ public:
VESPA_DEFINE_EXCEPTION_SPINE(DeserializeException)
};
-class SerializeException : public vespalib::IoException {
-public:
- SerializeException(const vespalib::string& msg, const vespalib::string& location = "");
- SerializeException(const vespalib::string& msg, const vespalib::Exception& cause,
- const vespalib::string& location = "");
- VESPA_DEFINE_EXCEPTION_SPINE(SerializeException)
-};
-
}