diff options
19 files changed, 118 insertions, 186 deletions
diff --git a/document/src/tests/documenttestcase.cpp b/document/src/tests/documenttestcase.cpp index a8d4829d355..4f769841d72 100644 --- a/document/src/tests/documenttestcase.cpp +++ b/document/src/tests/documenttestcase.cpp @@ -567,14 +567,14 @@ TEST(DocumentTest, testReadSerializedFile) int fd = open(TEST_PATH("data/serializejava.dat").c_str(), O_RDONLY); size_t len = lseek(fd,0,SEEK_END); - ByteBuffer buf(len); + vespalib::alloc::Alloc buf = vespalib::alloc::Alloc::alloc(len); lseek(fd,0,SEEK_SET); - if (read(fd, buf.getBuffer(), len) != (ssize_t)len) { + if (read(fd, buf.get(), len) != (ssize_t)len) { throw vespalib::Exception("read failed"); } close(fd); - nbostream stream(buf.getBufferAtPos(), len); + nbostream stream(buf.get(), len); Document doc(repo, stream); verifyJavaDocument(doc); @@ -586,7 +586,7 @@ TEST(DocumentTest, testReadSerializedFile) EXPECT_TRUE(buf2.empty()); buf2.rp(0); EXPECT_EQ(len, buf2.size()); - EXPECT_TRUE(memcmp(buf2.peek(), buf.getBuffer(), buf2.size()) == 0); + EXPECT_TRUE(memcmp(buf2.peek(), buf.get(), buf2.size()) == 0); doc2.setValue("stringfield", StringFieldValue("hei")); @@ -603,14 +603,14 @@ TEST(DocumentTest, testReadSerializedFileCompressed) int fd = open(TEST_PATH("data/serializejava-compressed.dat").c_str(), O_RDONLY); int len = lseek(fd,0,SEEK_END); - ByteBuffer buf(len); + vespalib::alloc::Alloc buf = vespalib::alloc::Alloc::alloc(len); lseek(fd,0,SEEK_SET); - if (read(fd, buf.getBuffer(), len) != len) { + if (read(fd, buf.get(), len) != len) { throw vespalib::Exception("read failed"); } close(fd); - nbostream stream(buf.getBufferAtPos(), len); + nbostream stream(buf.get(), len); Document doc(repo, stream); verifyJavaDocument(doc); } @@ -753,14 +753,14 @@ TEST(DocumentTest,testReadSerializedAllVersions) } int fd = open(tests[i]._dataFile.c_str(), O_RDONLY); int len = lseek(fd,0,SEEK_END); - ByteBuffer buf(len); + vespalib::alloc::Alloc buf = vespalib::alloc::Alloc::alloc(len); lseek(fd,0,SEEK_SET); - if (read(fd, buf.getBuffer(), len) != len) { - throw vespalib::Exception("read failed"); - } + if (read(fd, buf.get(), len) != len) { + throw vespalib::Exception("read failed"); + } close(fd); - nbostream stream(buf.getBufferAtPos(), len); + nbostream stream(buf.get(), len); Document doc(repo, stream); IntFieldValue intVal; @@ -1181,14 +1181,14 @@ TEST(DocumentTest, testAnnotationDeserialization) int fd = open(TEST_PATH("data/serializejavawithannotations.dat").c_str(), O_RDONLY); int len = lseek(fd,0,SEEK_END); - ByteBuffer buf(len); + vespalib::alloc::Alloc buf = vespalib::alloc::Alloc::alloc(len); lseek(fd,0,SEEK_SET); - if (read(fd, buf.getBuffer(), len) != len) { + if (read(fd, buf.get(), len) != len) { throw vespalib::Exception("read failed"); } close(fd); - nbostream stream1(buf.getBufferAtPos(), len); + nbostream stream1(buf.get(), len); Document doc(repo, stream1); StringFieldValue strVal; EXPECT_TRUE(doc.getValue(doc.getField("story"), strVal)); @@ -1228,14 +1228,6 @@ TEST(DocumentTest, testAnnotationDeserialization) EXPECT_EQ((int64_t)2384LL, longVal.getAsLong()); } -TEST(DocumentTest, testGetSerializedSize) -{ - TestDocMan testDocMan; - Document::UP doc = testDocMan.createDocument(); - - EXPECT_EQ(getSerializedSize(*doc), doc->getSerializedSize()); -} - TEST(DocumentTest, testDeserializeMultiple) { TestDocRepo testDocRepo; diff --git a/document/src/tests/documentupdatetestcase.cpp b/document/src/tests/documentupdatetestcase.cpp index 9ffe30c1080..a1ce861855c 100644 --- a/document/src/tests/documentupdatetestcase.cpp +++ b/document/src/tests/documentupdatetestcase.cpp @@ -43,14 +43,13 @@ namespace document { namespace { -ByteBuffer::UP serializeHEAD(const DocumentUpdate & update) +nbostream +serializeHEAD(const DocumentUpdate & update) { nbostream stream; VespaDocumentSerializer serializer(stream); serializer.writeHEAD(update); - ByteBuffer::UP retVal(new ByteBuffer(stream.size())); - retVal->putBytes(stream.peek(), stream.size()); - return retVal; + return stream; } nbostream serialize(const ValueUpdate & update) @@ -83,25 +82,26 @@ void testRoundtripSerialize(const UpdateType& update, const DataType &type) { } void -writeBufferToFile(const ByteBuffer &buf, const vespalib::string &fileName) +writeBufferToFile(const nbostream &buf, const vespalib::string &fileName) { auto file = std::fstream(fileName, std::ios::out | std::ios::binary); - file.write(buf.getBuffer(), buf.getPos()); + file.write(buf.c_str(), buf.size()); assert(file.good()); file.close(); } -ByteBuffer::UP +nbostream readBufferFromFile(const vespalib::string &fileName) { auto file = std::fstream(fileName, std::ios::in | std::ios::binary | std::ios::ate); auto size = file.tellg(); auto result = std::make_unique<ByteBuffer>(size); file.seekg(0); - file.read(result->getBuffer(), size); + vespalib::alloc::Alloc buf = vespalib::alloc::Alloc::alloc(size); + file.read(static_cast<char *>(buf.get()), size); assert(file.good()); file.close(); - return result; + return nbostream(std::move(buf), size); } } @@ -132,9 +132,8 @@ TEST(DocumentUpdateTest, testSimpleUsage) // Test that a document update can be serialized DocumentUpdate docUpdate(repo, *docType, DocumentId("id:ns:test::1")); docUpdate.addUpdate(fieldUpdateCopy); - ByteBuffer::UP docBuf = serializeHEAD(docUpdate); - docBuf->flip(); - auto docUpdateCopy(DocumentUpdate::createHEAD(repo, nbostream(docBuf->getBufferAtPos(), docBuf->getRemaining()))); + nbostream docBuf = serializeHEAD(docUpdate); + auto docUpdateCopy(DocumentUpdate::createHEAD(repo, docBuf)); // Create a test document Document doc(*docType, DocumentId("id:ns:test::1")); @@ -236,7 +235,7 @@ TEST(DocumentUpdateTest, testUpdateArray) // Create a document. TestDocMan docMan; Document::UP doc(docMan.createDocument()); - EXPECT_EQ((document::FieldValue*)NULL, doc->getValue(doc->getField("tags")).get()); + EXPECT_EQ((document::FieldValue*)nullptr, doc->getValue(doc->getField("tags")).get()); // Assign array field. ArrayFieldValue myarray(doc->getType().getField("tags").getDataType()); @@ -459,8 +458,7 @@ TEST(DocumentUpdateTest, testReadSerializedFile) const std::string file_name = "data/crossplatform-java-cpp-doctypes.cfg"; DocumentTypeRepo repo(readDocumenttypesConfig(file_name)); - auto buf = readBufferFromFile("data/serializeupdatejava.dat"); - nbostream is(buf->getBufferAtPos(), buf->getRemaining()); + auto is = readBufferFromFile("data/serializeupdatejava.dat"); DocumentUpdate::UP updp(DocumentUpdate::createHEAD(repo, is)); DocumentUpdate& upd(*updp); @@ -539,8 +537,8 @@ TEST(DocumentUpdateTest, testGenerateSerializedFile) ArithmeticValueUpdate(ArithmeticValueUpdate::Add, 2))) .addUpdate(MapValueUpdate(StringFieldValue("foo"), ArithmeticValueUpdate(ArithmeticValueUpdate::Mul, 2)))); - ByteBuffer::UP buf(serializeHEAD(upd)); - writeBufferToFile(*buf, "data/serializeupdatecpp.dat"); + nbostream buf(serializeHEAD(upd)); + writeBufferToFile(buf, "data/serializeupdatecpp.dat"); } @@ -549,7 +547,7 @@ TEST(DocumentUpdateTest, testSetBadFieldTypes) // Create a test document TestDocMan docMan; Document::UP doc(docMan.createDocument()); - EXPECT_EQ((document::FieldValue*)NULL, doc->getValue(doc->getField("headerval")).get()); + EXPECT_EQ((document::FieldValue*)nullptr, doc->getValue(doc->getField("headerval")).get()); // Assign a float value to an int field. DocumentUpdate update(docMan.getTypeRepo(), *doc->getDataType(), doc->getId()); @@ -561,7 +559,7 @@ TEST(DocumentUpdateTest, testSetBadFieldTypes) update.applyTo(*doc); // Verify that the field is NOT set in the document. - EXPECT_EQ((document::FieldValue*)NULL, + EXPECT_EQ((document::FieldValue*)nullptr, doc->getValue(doc->getField("headerval")).get()); } @@ -569,7 +567,7 @@ TEST(DocumentUpdateTest, testUpdateApplyNoParams) { TestDocMan docMan; Document::UP doc(docMan.createDocument()); - EXPECT_EQ((document::FieldValue*)NULL, doc->getValue(doc->getField("tags")).get()); + EXPECT_EQ((document::FieldValue*)nullptr, doc->getValue(doc->getField("tags")).get()); DocumentUpdate update(docMan.getTypeRepo(), *doc->getDataType(), doc->getId()); update.addUpdate(FieldUpdate(doc->getField("tags")).addUpdate(AssignValueUpdate())); @@ -1095,13 +1093,12 @@ struct TensorUpdateSerializeFixture { } void serializeUpdateToFile(const DocumentUpdate &update, const vespalib::string &fileName) { - ByteBuffer::UP buf = serializeHEAD(update); - writeBufferToFile(*buf, fileName); + nbostream buf = serializeHEAD(update); + writeBufferToFile(buf, fileName); } DocumentUpdate::UP deserializeUpdateFromFile(const vespalib::string &fileName) { - auto buf = readBufferFromFile(fileName); - nbostream stream(buf->getBufferAtPos(), buf->getRemaining()); + auto stream = readBufferFromFile(fileName); return DocumentUpdate::createHEAD(*repo, stream); } @@ -1181,10 +1178,9 @@ TEST(DocumentUpdateTest, testThatCreateIfNonExistentFlagIsSerializedAndDeseriali { CreateIfNonExistentFixture f; - ByteBuffer::UP buf(serializeHEAD(*f.update)); - buf->flip(); + nbostream buf(serializeHEAD(*f.update)); - DocumentUpdate::UP deserialized = DocumentUpdate::createHEAD(f.docMan.getTypeRepo(), *buf); + DocumentUpdate::UP deserialized = DocumentUpdate::createHEAD(f.docMan.getTypeRepo(), buf); EXPECT_EQ(*f.update, *deserialized); EXPECT_TRUE(deserialized->getCreateIfNonExistent()); } @@ -1216,9 +1212,8 @@ TEST(DocumentUpdateTest, array_element_update_can_be_roundtrip_serialized) ArrayUpdateFixture f; auto buffer = serializeHEAD(*f.update); - buffer->flip(); - auto deserialized = DocumentUpdate::createHEAD(f.doc_man.getTypeRepo(), *buffer); + auto deserialized = DocumentUpdate::createHEAD(f.doc_man.getTypeRepo(), buffer); EXPECT_EQ(*f.update, *deserialized); } diff --git a/document/src/tests/fieldpathupdatetestcase.cpp b/document/src/tests/fieldpathupdatetestcase.cpp index 82443f13716..39360119766 100644 --- a/document/src/tests/fieldpathupdatetestcase.cpp +++ b/document/src/tests/fieldpathupdatetestcase.cpp @@ -19,6 +19,7 @@ #include <gtest/gtest.h> using vespalib::Identifiable; +using vespalib::nbostream; using namespace document::config_builder; namespace document { @@ -133,23 +134,21 @@ createTestDocument(const DocumentTypeRepo &repo) return doc; } -ByteBuffer::UP serializeHEAD(const DocumentUpdate & update) +nbostream +serializeHEAD(const DocumentUpdate & update) { vespalib::nbostream stream; VespaDocumentSerializer serializer(stream); serializer.writeHEAD(update); - ByteBuffer::UP retVal(new ByteBuffer(stream.size())); - retVal->putBytes(stream.peek(), stream.size()); - return retVal; + return stream; } void testSerialize(const DocumentTypeRepo& repo, const DocumentUpdate& a) { try{ - ByteBuffer::UP bb(serializeHEAD(a)); - bb->flip(); - DocumentUpdate::UP b(DocumentUpdate::createHEAD(repo, *bb)); + auto bb(serializeHEAD(a)); + DocumentUpdate::UP b(DocumentUpdate::createHEAD(repo, bb)); - EXPECT_EQ(size_t(0), bb->getRemaining()); + EXPECT_EQ(size_t(0), bb.size()); EXPECT_EQ(a.getId().toString(), b->getId().toString()); EXPECT_EQ(a.getUpdates().size(), b->getUpdates().size()); for (size_t i(0); i < a.getUpdates().size(); i++) { @@ -157,8 +156,7 @@ void testSerialize(const DocumentTypeRepo& repo, const DocumentUpdate& a) { const FieldUpdate & ub = b->getUpdates()[i]; EXPECT_EQ(&ua.getField(), &ub.getField()); - EXPECT_EQ(ua.getUpdates().size(), - ub.getUpdates().size()); + EXPECT_EQ(ua.getUpdates().size(), ub.getUpdates().size()); for (size_t j(0); j < ua.getUpdates().size(); j++) { EXPECT_EQ(ua.getUpdates()[j]->getType(), ub.getUpdates()[j]->getType()); } @@ -1073,14 +1071,14 @@ TEST_F(FieldPathUpdateTestCase, testReadSerializedFile) int fd = open(TEST_PATH("data/serialize-fieldpathupdate-java.dat").c_str(), O_RDONLY); int len = lseek(fd,0,SEEK_END); - ByteBuffer buf(len); + vespalib::alloc::Alloc buf = vespalib::alloc::Alloc::alloc(len); lseek(fd,0,SEEK_SET); - if (read(fd, buf.getBuffer(), len) != len) { + if (read(fd, buf.get(), len) != len) { throw vespalib::Exception("read failed"); } close(fd); - DocumentUpdate::UP updp(DocumentUpdate::createHEAD(repo, buf)); + DocumentUpdate::UP updp(DocumentUpdate::createHEAD(repo, nbostream(std::move(buf), len))); DocumentUpdate& upd(*updp); DocumentUpdate::UP compare(createDocumentUpdateForSerialization(repo)); @@ -1094,11 +1092,11 @@ TEST_F(FieldPathUpdateTestCase, testGenerateSerializedFile) // Tests nothing, only generates a file for java test DocumentUpdate::UP upd(createDocumentUpdateForSerialization(repo)); - ByteBuffer::UP buf(serializeHEAD(*upd)); + nbostream buf(serializeHEAD(*upd)); int fd = open(TEST_PATH("data/serialize-fieldpathupdate-cpp.dat").c_str(), O_WRONLY | O_TRUNC | O_CREAT, 0644); - if (write(fd, buf->getBuffer(), buf->getPos()) != (ssize_t)buf->getPos()) { + if (write(fd, buf.c_str(), buf.size()) != (ssize_t)buf.size()) { throw vespalib::Exception("write failed"); } close(fd); diff --git a/document/src/vespa/document/fieldvalue/document.cpp b/document/src/vespa/document/fieldvalue/document.cpp index b137f4bac2e..48059c7038d 100644 --- a/document/src/vespa/document/fieldvalue/document.cpp +++ b/document/src/vespa/document/fieldvalue/document.cpp @@ -256,13 +256,6 @@ void Document::deserializeBody(const DocumentTypeRepo& repo, vespalib::nbostream deserializer.readStructNoReset(getFields()); } -size_t -Document::getSerializedSize() const -{ - // Temporary non-optimal (but guaranteed correct) implementation. - return serialize().size(); -} - StructuredFieldValue::StructuredIterator::UP Document::getIterator(const Field* first) const { diff --git a/document/src/vespa/document/fieldvalue/document.h b/document/src/vespa/document/fieldvalue/document.h index 23b97a2a56d..6a111fe6782 100644 --- a/document/src/vespa/document/fieldvalue/document.h +++ b/document/src/vespa/document/fieldvalue/document.h @@ -96,8 +96,6 @@ public: /** Deserialize document contained in given bytebuffers. */ void deserialize(const DocumentTypeRepo& repo, vespalib::nbostream & body, vespalib::nbostream & header); - size_t getSerializedSize() const; - /** Undo fieldvalue's toXml override for document. */ std::string toXml() const { return toXml(""); } std::string toXml(const std::string& indent) const override; diff --git a/document/src/vespa/document/update/documentupdate.cpp b/document/src/vespa/document/update/documentupdate.cpp index b43b7a59c5b..f289e6a8f27 100644 --- a/document/src/vespa/document/update/documentupdate.cpp +++ b/document/src/vespa/document/update/documentupdate.cpp @@ -7,7 +7,6 @@ #include <vespa/document/util/serializableexceptions.h> #include <vespa/vespalib/objects/nbostream.h> #include <vespa/document/util/bufferexceptions.h> -#include <vespa/document/util/bytebuffer.h> #include <vespa/document/base/exceptions.h> #include <vespa/document/datatype/documenttype.h> #include <vespa/document/repo/documenttyperepo.h> @@ -231,20 +230,18 @@ DocumentUpdate::serializeFlags(int size_) const } DocumentUpdate::UP -DocumentUpdate::createHEAD(const DocumentTypeRepo& repo, ByteBuffer& buffer) +DocumentUpdate::createHEAD(const DocumentTypeRepo& repo, vespalib::nbostream && stream) { - vespalib::nbostream is(buffer.getBufferAtPos(), buffer.getRemaining()); auto update = std::make_unique<DocumentUpdate>(); - update->initHEAD(repo, is); - buffer.setPos(buffer.getPos() + is.rp()); + update->initHEAD(repo, std::move(stream)); return update; } DocumentUpdate::UP -DocumentUpdate::createHEAD(const DocumentTypeRepo& repo, vespalib::nbostream stream) +DocumentUpdate::createHEAD(const DocumentTypeRepo& repo, vespalib::nbostream & stream) { auto update = std::make_unique<DocumentUpdate>(); - update->initHEAD(repo, std::move(stream)); + update->initHEAD(repo, stream); return update; } diff --git a/document/src/vespa/document/update/documentupdate.h b/document/src/vespa/document/update/documentupdate.h index cb9113f5039..fb3d6c0f7a3 100644 --- a/document/src/vespa/document/update/documentupdate.h +++ b/document/src/vespa/document/update/documentupdate.h @@ -51,8 +51,8 @@ public: /** * Create new style document update, possibly with field path updates. */ - static DocumentUpdate::UP createHEAD(const DocumentTypeRepo & repo, vespalib::nbostream stream); - static DocumentUpdate::UP createHEAD(const DocumentTypeRepo & repo, ByteBuffer & buffer); + static DocumentUpdate::UP createHEAD(const DocumentTypeRepo & repo, vespalib::nbostream & stream); + static DocumentUpdate::UP createHEAD(const DocumentTypeRepo & repo, vespalib::nbostream && stream); DocumentUpdate(); /** @@ -125,8 +125,8 @@ private: bool _needHardReserialize; int deserializeFlags(int sizeAndFlags); - void initHEAD(const DocumentTypeRepo & repo, vespalib::nbostream && stream); void initHEAD(const DocumentTypeRepo & repo, vespalib::nbostream & stream); + void initHEAD(const DocumentTypeRepo & repo, vespalib::nbostream && stream); void deserializeBody(const DocumentTypeRepo &repo, vespalib::nbostream &stream); void lazyDeserialize(const DocumentTypeRepo & repo, vespalib::nbostream & stream); void ensureDeserialized() const; diff --git a/document/src/vespa/document/util/bytebuffer.cpp b/document/src/vespa/document/util/bytebuffer.cpp index c909ca5fe61..468f8d653ab 100644 --- a/document/src/vespa/document/util/bytebuffer.cpp +++ b/document/src/vespa/document/util/bytebuffer.cpp @@ -89,10 +89,11 @@ ByteBuffer::ByteBuffer(const ByteBuffer& rhs) : _ownedBuffer() { if (rhs._len > 0 && rhs._buffer) { - Alloc::alloc(rhs._len + 1).swap(_ownedBuffer); - _buffer = static_cast<char *>(_ownedBuffer.get()); - memcpy(_buffer, rhs._buffer, rhs._len); - _buffer[rhs._len] = 0; + Alloc buf = Alloc::alloc(rhs._len + 1); + memcpy(buf.get(), rhs._buffer, rhs._len); + static_cast<char *>(buf.get())[rhs._len] = 0; + _ownedBuffer = std::move(buf); + _buffer = static_cast<const char *>(_ownedBuffer.get()); } } @@ -110,16 +111,6 @@ ByteBuffer* ByteBuffer::copyBuffer(const char* buffer, size_t len) } } -void -ByteBuffer::setPos(size_t pos) // throw (BufferOutOfBoundsException) -{ - if (pos > _len) { - throwOutOfBounds(pos, _len); - } else { - _pos=pos; - } -} - void ByteBuffer::incPos(size_t pos) { if (_pos + pos > _len) { @@ -133,7 +124,7 @@ void ByteBuffer::getNumeric(uint8_t & v) { if (__builtin_expect(getRemaining() < sizeof(v), 0)) { throwOutOfBounds(getRemaining(), sizeof(v)); } else { - v = *(uint8_t *) getBufferAtPos(); + v = *reinterpret_cast<const uint8_t *>(getBufferAtPos()); incPosNoCheck(sizeof(v)); } } @@ -142,7 +133,7 @@ void ByteBuffer::getNumericNetwork(int16_t & v) { if (__builtin_expect(getRemaining() < sizeof(v), 0)) { throwOutOfBounds(getRemaining(), sizeof(v)); } else { - uint16_t val = *(uint16_t *) (void *) getBufferAtPos(); + uint16_t val = *reinterpret_cast<const uint16_t *>(getBufferAtPos()); v = ntohs(val); incPosNoCheck(sizeof(v)); } @@ -152,7 +143,7 @@ void ByteBuffer::getNumericNetwork(int32_t & v) { if (__builtin_expect(getRemaining() < sizeof(v), 0)) { throwOutOfBounds(getRemaining(), sizeof(v)); } else { - uint32_t val = *(uint32_t *) (void *) getBufferAtPos(); + uint32_t val = *reinterpret_cast<const uint32_t *>(getBufferAtPos()); v = ntohl(val); incPosNoCheck(sizeof(v)); } @@ -162,7 +153,7 @@ void ByteBuffer::getNumeric(int64_t& v) { if (__builtin_expect(getRemaining() < sizeof(v), 0)) { throwOutOfBounds(getRemaining(), sizeof(v)); } else { - v = *(int64_t *) (void *) getBufferAtPos(); + v = *reinterpret_cast<const int64_t *>(getBufferAtPos()); incPosNoCheck(sizeof(v)); } } @@ -181,13 +172,5 @@ void ByteBuffer::getBytes(void *buffer, size_t count) incPos(count); memcpy(buffer, v, count); } -void ByteBuffer::putBytes(const void *buf, size_t count) { - if (__builtin_expect(getRemaining() < count, 0)) { - throwOutOfBounds(getRemaining(), sizeof(count)); - } else { - memcpy(getBufferAtPos(), buf, count); - incPosNoCheck(count); - } -} } // document diff --git a/document/src/vespa/document/util/bytebuffer.h b/document/src/vespa/document/util/bytebuffer.h index ce01231602d..bb11f009cc6 100644 --- a/document/src/vespa/document/util/bytebuffer.h +++ b/document/src/vespa/document/util/bytebuffer.h @@ -62,13 +62,13 @@ public: static ByteBuffer* copyBuffer(const char* buffer, size_t len); /** @return Returns the buffer pointed to by this object (at position 0) */ - char* getBuffer() const { return _buffer; } + const char* getBuffer() const { return _buffer; } /** @return Returns the length of the buffer pointed to by this object. */ size_t getLength() const { return _len; } /** @return Returns a pointer to the current position in the buffer. */ - char* getBufferAtPos() const { return _buffer + _pos; } + const char* getBufferAtPos() const { return _buffer + _pos; } /** @return Returns the index of the current position in the buffer. */ size_t getPos() const { return _pos; } @@ -80,13 +80,6 @@ public: size_t getRemaining() const { return _len -_pos; } /** - * Changes the position in the buffer. - * - * @throws BufferOutOfBoundsException; - */ - void setPos(size_t pos); - - /** * Moves the position in the buffer. * * @param pos The number of bytes to move the position. The new position @@ -98,14 +91,6 @@ public: */ void incPos(size_t pos); - /** - * Resets pos to 0, and sets limit to old pos. Use this before reading - * from a buffer you have written to - */ - void flip() { - _pos = 0; - } - void getNumeric(uint8_t & v); void getNumericNetwork(int16_t & v); void getNumericNetwork(int32_t & v); @@ -133,24 +118,15 @@ public: */ void getBytes(void *buffer, size_t count); - /** - * Writes the given number of bytes into the ByteBuffer at the current - * position, and updates the positition accordingly - * - * @param buf the bytes to store - * @param count number of bytes to store - */ - 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; + const char * _buffer; + size_t _len; + size_t _pos; vespalib::alloc::Alloc _ownedBuffer; }; diff --git a/documentapi/src/vespa/documentapi/messagebus/messages/documentstate.cpp b/documentapi/src/vespa/documentapi/messagebus/messages/documentstate.cpp index 718611dfc04..6c8394b1b4c 100644 --- a/documentapi/src/vespa/documentapi/messagebus/messages/documentstate.cpp +++ b/documentapi/src/vespa/documentapi/messagebus/messages/documentstate.cpp @@ -15,12 +15,11 @@ DocumentState::DocumentState(const DocumentState& o) : _gid(o._gid), _timestamp(o._timestamp), _removeEntry(o._removeEntry) { if (o._docId.get() != 0) { - _docId.reset(new document::DocumentId(*o._docId)); + _docId = std::make_unique<document::DocumentId>(*o._docId); } } -DocumentState::DocumentState(const document::DocumentId& id, - uint64_t timestamp, bool removeEntry) +DocumentState::DocumentState(const document::DocumentId& id, uint64_t timestamp, bool removeEntry) : _docId(new document::DocumentId(id)), _gid(_docId->getGlobalId()), _timestamp(timestamp), @@ -28,8 +27,7 @@ DocumentState::DocumentState(const document::DocumentId& id, { } -DocumentState::DocumentState(const document::GlobalId& gid, - uint64_t timestamp, bool removeEntry) +DocumentState::DocumentState(const document::GlobalId& gid, uint64_t timestamp, bool removeEntry) : _gid(gid), _timestamp(timestamp), _removeEntry(removeEntry) {} DocumentState::DocumentState(document::ByteBuffer& buf) @@ -39,10 +37,10 @@ DocumentState::DocumentState(document::ByteBuffer& buf) buf.getByte(hasDocId); if (hasDocId) { vespalib::nbostream stream(buf.getBufferAtPos(), buf.getRemaining()); - _docId.reset(new document::DocumentId(stream)); + _docId = std::make_unique<document::DocumentId>(stream); buf.incPos(stream.rp()); } - char* gid = buf.getBufferAtPos(); + const char* gid = buf.getBufferAtPos(); buf.incPos(document::GlobalId::LENGTH); _gid.set(gid); buf.getLongNetwork((int64_t&) _timestamp); @@ -55,8 +53,8 @@ DocumentState& DocumentState::operator=(const DocumentState& other) { _docId.reset(); - if (other._docId.get() != 0) { - _docId.reset(new document::DocumentId(*other._docId)); + if (other._docId) { + _docId = std::make_unique<document::DocumentId>(*other._docId); } _gid = other._gid; _timestamp = other._timestamp; diff --git a/documentapi/src/vespa/documentapi/messagebus/routablefactories60.cpp b/documentapi/src/vespa/documentapi/messagebus/routablefactories60.cpp index 2fe9ffa3da9..58367410ddc 100644 --- a/documentapi/src/vespa/documentapi/messagebus/routablefactories60.cpp +++ b/documentapi/src/vespa/documentapi/messagebus/routablefactories60.cpp @@ -438,14 +438,12 @@ RoutableFactories60::GetBucketStateReplyFactory::doEncode(const DocumentReply &o DocumentMessage::UP RoutableFactories60::GetDocumentMessageFactory::doDecode(document::ByteBuffer &buf) const { - return DocumentMessage::UP( - new GetDocumentMessage(decodeDocumentId(buf), - decodeString(buf))); + document::DocumentId docId = decodeDocumentId(buf); + return std::make_unique<GetDocumentMessage>(docId, decodeString(buf)); } bool -RoutableFactories60::GetDocumentMessageFactory::doEncode(const DocumentMessage &obj, - vespalib::GrowableByteBuffer &buf) const +RoutableFactories60::GetDocumentMessageFactory::doEncode(const DocumentMessage &obj, vespalib::GrowableByteBuffer &buf) const { const GetDocumentMessage &msg = static_cast<const GetDocumentMessage&>(obj); @@ -526,7 +524,7 @@ void RoutableFactories60::PutDocumentMessageFactory::decodeInto(PutDocumentMessage & msg, document::ByteBuffer & buf) const { vespalib::nbostream stream(buf.getBufferAtPos(), buf.getRemaining()); msg.setDocument(make_shared<document::Document>(_repo, stream)); - buf.incPos(buf.getRemaining() - stream.size()); + buf.incPos(stream.rp()); msg.setTimestamp(static_cast<uint64_t>(decodeLong(buf))); decodeTasCondition(msg, buf); } @@ -550,9 +548,6 @@ RoutableFactories60::PutDocumentReplyFactory::doDecode(document::ByteBuffer &buf { auto reply = make_unique<WriteDocumentReply>(DocumentProtocol::REPLY_PUTDOCUMENT); reply->setHighestModificationTimestamp(decodeLong(buf)); - - // Doing an explicit move here to force converting result to an rvalue. - // This is done automatically in GCC >= 5. return reply; } @@ -768,7 +763,9 @@ RoutableFactories60::StatDocumentReplyFactory::doEncode(const DocumentReply &, v void RoutableFactories60::UpdateDocumentMessageFactory::decodeInto(UpdateDocumentMessage & msg, document::ByteBuffer & buf) const { - msg.setDocumentUpdate(document::DocumentUpdate::createHEAD(_repo, buf)); + vespalib::nbostream stream(buf.getBufferAtPos(), buf.getRemaining()); + msg.setDocumentUpdate(document::DocumentUpdate::createHEAD(_repo, stream)); + buf.incPos(stream.rp()); msg.setOldTimestamp(static_cast<uint64_t>(decodeLong(buf))); msg.setNewTimestamp(static_cast<uint64_t>(decodeLong(buf))); decodeTasCondition(msg, buf); diff --git a/persistence/src/vespa/persistence/conformancetest/conformancetest.cpp b/persistence/src/vespa/persistence/conformancetest/conformancetest.cpp index 10f03b8f8e4..5c8b5b029d2 100644 --- a/persistence/src/vespa/persistence/conformancetest/conformancetest.cpp +++ b/persistence/src/vespa/persistence/conformancetest/conformancetest.cpp @@ -260,7 +260,7 @@ verifyDocs(const std::vector<DocAndTimestamp>& wanted, << entry.getDocument()->toString(true); } EXPECT_EQ(wanted[wantedIdx].timestamp, entry.getTimestamp()); - size_t serSize = wanted[wantedIdx].doc->getSerializedSize(); + size_t serSize = wanted[wantedIdx].doc->serialize().size(); EXPECT_EQ(serSize + sizeof(DocEntry), size_t(entry.getSize())); EXPECT_EQ(serSize, size_t(entry.getDocumentSize())); ++wantedIdx; diff --git a/persistence/src/vespa/persistence/spi/docentry.cpp b/persistence/src/vespa/persistence/spi/docentry.cpp index d482d144d73..f46be6f3a25 100644 --- a/persistence/src/vespa/persistence/spi/docentry.cpp +++ b/persistence/src/vespa/persistence/spi/docentry.cpp @@ -2,6 +2,7 @@ #include "docentry.h" #include <vespa/document/fieldvalue/document.h> +#include <vespa/vespalib/objects/nbostream.h> #include <sstream> #include <cassert> @@ -10,16 +11,13 @@ namespace storage::spi { DocEntry::DocEntry(Timestamp t, int metaFlags, DocumentUP doc) : _timestamp(t), _metaFlags(metaFlags), - _persistedDocumentSize(doc->getSerializedSize()), + _persistedDocumentSize(doc->serialize().size()), _size(_persistedDocumentSize + sizeof(DocEntry)), _documentId(), _document(std::move(doc)) { } -DocEntry::DocEntry(Timestamp t, - int metaFlags, - DocumentUP doc, - size_t serializedDocumentSize) +DocEntry::DocEntry(Timestamp t, int metaFlags, DocumentUP doc, size_t serializedDocumentSize) : _timestamp(t), _metaFlags(metaFlags), _persistedDocumentSize(serializedDocumentSize), diff --git a/searchcore/src/vespa/searchcore/proton/persistenceengine/document_iterator.cpp b/searchcore/src/vespa/searchcore/proton/persistenceengine/document_iterator.cpp index e658bc0cfa0..1bcbe4e9683 100644 --- a/searchcore/src/vespa/searchcore/proton/persistenceengine/document_iterator.cpp +++ b/searchcore/src/vespa/searchcore/proton/persistenceengine/document_iterator.cpp @@ -31,7 +31,7 @@ DocEntry *createDocEntry(Timestamp timestamp, bool removed, Document::UP doc, ss if (removed) { return new DocEntry(timestamp, storage::spi::REMOVE_ENTRY, doc->getId()); } else { - ssize_t serializedSize = defaultSerializedSize >= 0 ? defaultSerializedSize : doc->getSerializedSize(); + ssize_t serializedSize = defaultSerializedSize >= 0 ? defaultSerializedSize : doc->serialize().size(); return new DocEntry(timestamp, storage::spi::NONE, std::move(doc), serializedSize); } } else { diff --git a/searchlib/src/tests/transactionlogstress/translogstress.cpp b/searchlib/src/tests/transactionlogstress/translogstress.cpp index 36ce43cc52a..76587d78b69 100644 --- a/searchlib/src/tests/transactionlogstress/translogstress.cpp +++ b/searchlib/src/tests/transactionlogstress/translogstress.cpp @@ -17,7 +17,7 @@ LOG_SETUP("translogstress"); -using document::ByteBuffer; +using vespalib::nbostream; using search::Runnable; using vespalib::Monitor; using vespalib::MonitorGuard; @@ -47,10 +47,10 @@ public: BufferGenerator(uint32_t minStrLen, uint32_t maxStrLen) : _rnd(), _minStrLen(minStrLen), _maxStrLen(maxStrLen) {} void setSeed(long seed) { _rnd.srand48(seed); } - ByteBuffer getRandomBuffer(); + nbostream getRandomBuffer(); }; -ByteBuffer +nbostream BufferGenerator::getRandomBuffer() { size_t len = _minStrLen + _rnd.lrand48() % (_maxStrLen - _minStrLen); @@ -59,9 +59,8 @@ BufferGenerator::getRandomBuffer() char c = 'a' + _rnd.lrand48() % ('z' - 'a' + 1); str.push_back(c); } - ByteBuffer buf(str.size() + 1); - buf.putBytes(str.c_str(), str.size() + 1); - buf.flip(); + nbostream buf(str.size() + 1); + buf.write(str.c_str(), str.size() + 1); return buf; } @@ -75,8 +74,8 @@ private: Rand48 _rnd; long _baseSeed; BufferGenerator _bufferGenerator; - const std::vector<document::ByteBuffer> * _buffers; - ByteBuffer _lastGeneratedBuffer; + const std::vector<nbostream> * _buffers; + nbostream _lastGeneratedBuffer; public: EntryGenerator(long baseSeed, const BufferGenerator & bufferGenerator) : @@ -95,7 +94,7 @@ public: SerialNum getRandomSerialNum(SerialNum begin, SerialNum end); Packet::Entry getRandomEntry(SerialNum num); Rand48 & getRnd() { return _rnd; } - void setBuffers(const std::vector<ByteBuffer> & buffers) { + void setBuffers(const std::vector<nbostream> & buffers) { _buffers = &buffers; } }; @@ -118,12 +117,12 @@ EntryGenerator::getRandomEntry(SerialNum num) _rnd.srand48(_baseSeed + num); if (_buffers != NULL) { size_t i = _rnd.lrand48() % _buffers->size(); - const ByteBuffer& buffer = (*_buffers)[i]; - return Packet::Entry(num, 1024, ConstBufferRef(buffer.getBuffer(), buffer.getLength())); + const nbostream& buffer = (*_buffers)[i]; + return Packet::Entry(num, 1024, ConstBufferRef(buffer.c_str(), buffer.size())); } else { _bufferGenerator.setSeed(_baseSeed + num); - _lastGeneratedBuffer = std::move(_bufferGenerator.getRandomBuffer()); - return Packet::Entry(num, 1024, ConstBufferRef(_lastGeneratedBuffer.getBuffer(), _lastGeneratedBuffer.getLength())); + _lastGeneratedBuffer = _bufferGenerator.getRandomBuffer(); + return Packet::Entry(num, 1024, ConstBufferRef(_lastGeneratedBuffer.c_str(), _lastGeneratedBuffer.size())); } } @@ -708,7 +707,7 @@ TransLogStress::Main() BufferGenerator bufferGenerator(_cfg.minStrLen, _cfg.maxStrLen); bufferGenerator.setSeed(_cfg.baseSeed); - std::vector<ByteBuffer> buffers; + std::vector<nbostream> buffers; for (uint32_t i = 0; i < _cfg.numPreGeneratedBuffers; ++i) { buffers.push_back(bufferGenerator.getRandomBuffer()); } diff --git a/storage/src/tests/visiting/visitormanagertest.cpp b/storage/src/tests/visiting/visitormanagertest.cpp index 2544fcc9a4b..62dbce890cc 100644 --- a/storage/src/tests/visiting/visitormanagertest.cpp +++ b/storage/src/tests/visiting/visitormanagertest.cpp @@ -20,6 +20,7 @@ #include <vespa/documentapi/messagebus/messages/visitor.h> #include <vespa/config/common/exceptions.h> #include <vespa/vespalib/gtest/gtest.h> +#include <vespa/vespalib/objects/nbostream.h> #include <gmock/gmock.h> #include <optional> #include <thread> @@ -337,7 +338,7 @@ int getTotalSerializedSize(const std::vector<document::Document::SP>& docs) { int total = 0; for (size_t i = 0; i < docs.size(); ++i) { - total += int(docs[i]->getSerializedSize()); + total += int(docs[i]->serialize().size()); } return total; } diff --git a/storage/src/vespa/storage/visiting/recoveryvisitor.cpp b/storage/src/vespa/storage/visiting/recoveryvisitor.cpp index 8bac2bac10a..80e74e890a1 100644 --- a/storage/src/vespa/storage/visiting/recoveryvisitor.cpp +++ b/storage/src/vespa/storage/visiting/recoveryvisitor.cpp @@ -2,7 +2,7 @@ #include "recoveryvisitor.h" - +#include <vespa/vespalib/objects/nbostream.h> #include <vespa/documentapi/messagebus/messages/visitor.h> #include <vespa/vespalib/text/stringtokenizer.h> #include <vespa/vespalib/stllike/hash_map.hpp> @@ -70,7 +70,7 @@ RecoveryVisitor::handleDocuments(const document::BucketId& bid, } } - hitCounter.addHit(doc->getId(), doc->getSerializedSize()); + hitCounter.addHit(doc->getId(), doc->serialize().size()); int64_t timestamp = doc->getLastModified(); cmd->getDocuments().push_back(documentapi::DocumentListMessage::Entry( diff --git a/storageapi/src/vespa/storageapi/message/persistence.cpp b/storageapi/src/vespa/storageapi/message/persistence.cpp index 8b8dedda231..bd8195c2feb 100644 --- a/storageapi/src/vespa/storageapi/message/persistence.cpp +++ b/storageapi/src/vespa/storageapi/message/persistence.cpp @@ -60,7 +60,7 @@ PutCommand::print(std::ostream& out, bool verbose, const std::string& indent) co { out << "Put(" << getBucketId() << ", " << _doc->getId() << ", timestamp " << _timestamp << ", size " - << _doc->getSerializedSize() << ")"; + << _doc->serialize().size() << ")"; if (verbose) { out << " {\n" << indent << " "; _doc->print(out, verbose, indent + " "); diff --git a/storageapi/src/vespa/storageapi/messageapi/returncode.cpp b/storageapi/src/vespa/storageapi/messageapi/returncode.cpp index 9b497a297a1..634a7166d74 100644 --- a/storageapi/src/vespa/storageapi/messageapi/returncode.cpp +++ b/storageapi/src/vespa/storageapi/messageapi/returncode.cpp @@ -26,7 +26,14 @@ vespalib::string ReturnCode::getResultString(Result result) { vespalib::string ReturnCode::toString() const { - return getResultString(_result) + " : " + _message; + vespalib::string ret = "ReturnCode("; + ret += getResultString(_result); + if ( ! _message.empty()) { + ret += ", "; + ret += _message; + } + ret += ")"; + return ret; } bool |