diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2017-06-02 13:15:05 +0200 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2017-06-06 09:12:38 +0200 |
commit | 02a2201dbce24db2586d94f37a6093192a0da691 (patch) | |
tree | 9324059701e251966093eda356c8e3040897ec95 /document | |
parent | 8e0c48091d4c944bec4a4c56ae49f25125262c82 (diff) |
checkpoint for slimmer SerializableArray
Diffstat (limited to 'document')
-rw-r--r-- | document/src/vespa/document/fieldvalue/serializablearray.cpp | 36 | ||||
-rw-r--r-- | document/src/vespa/document/fieldvalue/serializablearray.h | 52 |
2 files changed, 57 insertions, 31 deletions
diff --git a/document/src/vespa/document/fieldvalue/serializablearray.cpp b/document/src/vespa/document/fieldvalue/serializablearray.cpp index 7a174f1ab52..77204490dc0 100644 --- a/document/src/vespa/document/fieldvalue/serializablearray.cpp +++ b/document/src/vespa/document/fieldvalue/serializablearray.cpp @@ -1,16 +1,27 @@ // Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include "serializablearray.h" #include <vespa/document/util/serializableexceptions.h> +#include <vespa/document/util/bytebuffer.h> #include <vespa/vespalib/stllike/hash_map.hpp> #include <vespa/log/log.h> LOG_SETUP(".document.serializable-array"); - using std::vector; namespace document { +namespace serializablearray { + +using BufferMapT = vespalib::hash_map<int, ByteBuffer::UP>; + +class BufferMap : public BufferMapT { +public: + using BufferMapT::BufferMapT; +}; + +} + SerializableArray::Statistics SerializableArray::_stats; SerializableArray::SerializableArray() @@ -19,6 +30,13 @@ SerializableArray::SerializableArray() { } +serializablearray::BufferMap & ensure(std::unique_ptr<serializablearray::BufferMap> & owned) { + if (!owned) { + owned = std::make_unique<serializablearray::BufferMap>(); + } + return *owned; +} + SerializableArray::SerializableArray(const SerializableArray& other) : Cloneable(), _entries(other._entries), @@ -34,7 +52,7 @@ SerializableArray::SerializableArray(const SerializableArray& other) // Pointing to a buffer in the _owned structure. ByteBuffer::UP buf(ByteBuffer::copyBuffer(e.getBuffer(_uncompSerData.get()), e.size())); e.setBuffer(buf->getBuffer()); - _owned[e.id()] = std::move(buf); + ensure(_owned)[e.id()] = std::move(buf); } else { // If not it is relative to the buffer _uncompSerData, and hence it is valid as is. } @@ -79,7 +97,7 @@ SerializableArray::set(int id, ByteBuffer::UP buffer) { maybeDecompress(); Entry e(id, buffer->getRemaining(), buffer->getBuffer()); - _owned[id] = std::move(buffer); + ensure(_owned)[id] = std::move(buffer); EntryMap::iterator it = find(id); if (it == _entries.end()) { _entries.push_back(e); @@ -149,7 +167,7 @@ SerializableArray::clear(int id) EntryMap::iterator it = find(id); if (it != _entries.end()) { _entries.erase(it); - _owned.erase(id); + _owned->erase(id); invalidate(); } } @@ -215,4 +233,14 @@ void SerializableArray::assign(EntryMap & entries, } } +CompressionInfo +SerializableArray::getCompressionInfo() const { + return CompressionInfo(_uncompressedLength, _compSerData->getRemaining()); +} + +const char * +SerializableArray::Entry::getBuffer(const ByteBuffer * readOnlyBuffer) const { + return hasBuffer() ? _data._buffer : readOnlyBuffer->getBuffer() + getOffset(); +} + } // document diff --git a/document/src/vespa/document/fieldvalue/serializablearray.h b/document/src/vespa/document/fieldvalue/serializablearray.h index 9c0978f1476..1d211a50ab3 100644 --- a/document/src/vespa/document/fieldvalue/serializablearray.h +++ b/document/src/vespa/document/fieldvalue/serializablearray.h @@ -16,20 +16,22 @@ #pragma once -#include <vespa/document/util/bytebuffer.h> #include <vespa/document/util/compressionconfig.h> -#include <vespa/document/util/compressor.h> -#include <vespa/document/util/serializable.h> -#include <vector> #include <vespa/vespalib/objects/cloneable.h> -#include <vespa/vespalib/stllike/hash_map.h> #include <vespa/vespalib/util/buffer.h> -#include <vespa/fastos/dynamiclibrary.h> +#include <vespa/vespalib/util/memory.h> +#include <vector> -namespace document -{ +#define VESPA_DLL_LOCAL __attribute__ ((visibility("hidden"))) + +namespace document { class SerializableArrayIterator; +class ByteBuffer; + +namespace serializablearray { + class BufferMap; +} class SerializableArray : public vespalib::Cloneable { @@ -71,7 +73,7 @@ public: bool operator < (const Entry & e) const { return cmp(e) < 0; } int cmp(const Entry & e) const { return _id - e._id; } void setBuffer(const char * buffer) { _data._buffer = buffer; _sz |= BUFFER_MASK; } - const char * getBuffer(const ByteBuffer * readOnlyBuffer) const { return hasBuffer() ? _data._buffer : readOnlyBuffer->getBuffer() + getOffset(); } + VESPA_DLL_LOCAL const char * getBuffer(const ByteBuffer * readOnlyBuffer) const; private: uint32_t getOffset() const { return _data._offset; } enum { BUFFER_MASK=0x80000000 }; @@ -100,12 +102,11 @@ public: private: static Statistics _stats; - typedef vespalib::hash_map<int, uint32_t> HashMap; - public: static Statistics& getStatistics() { return _stats; } - typedef vespalib::CloneablePtr<SerializableArray> CP; - typedef std::unique_ptr<SerializableArray> UP; + using CP = vespalib::CloneablePtr<SerializableArray>; + using UP = std::unique_ptr<SerializableArray>; + using ByteBufferUP = std::unique_ptr<ByteBuffer>; SerializableArray(); virtual ~SerializableArray(); @@ -122,7 +123,7 @@ public: void set(int id, const char* value, int len); /** Stores a value in the array. */ - void set(int id, std::unique_ptr<ByteBuffer> buffer); + void set(int id, ByteBufferUP buffer); /** * Gets a value from the array. This is the faster version of the above. @@ -152,19 +153,17 @@ public: void clear(); CompressionConfig::Type getCompression() const { return _serializedCompression; } - CompressionInfo getCompressionInfo() const { - return CompressionInfo(_uncompressedLength, _compSerData->getRemaining()); - } + CompressionInfo getCompressionInfo() const; /** * Sets the serialized data that is the basis for this object's * content. This is used by deserialization. Any existing entries * are cleared. */ - VESPA_DLL_LOCAL void assign(EntryMap &entries, - ByteBuffer::UP buffer, - CompressionConfig::Type comp_type, - uint32_t uncompressed_length); + void assign(EntryMap &entries, + ByteBufferUP buffer, + CompressionConfig::Type comp_type, + uint32_t uncompressed_length); bool empty() const { return _entries.empty(); } @@ -189,22 +188,22 @@ private: return false; } - VESPA_DLL_LOCAL bool deCompressAndCatch() const; + bool deCompressAndCatch() const; void maybeDecompress() const { if ( shouldDecompress() ) { const_cast<SerializableArray *>(this)->deCompress(); } } - VESPA_DLL_LOCAL void deCompress(); // throw (DeserializeException); + void deCompress(); // throw (DeserializeException); /** Contains the stored attributes, with reference to the real data.. */ EntryMap _entries; /** The buffers we own. */ - vespalib::hash_map<int, ByteBuffer::UP > _owned; + std::unique_ptr<serializablearray::BufferMap> _owned; /** Data we deserialized from, if applicable. */ - ByteBuffer::UP _uncompSerData; - ByteBuffer::UP _compSerData; + ByteBufferUP _uncompSerData; + ByteBufferUP _compSerData; CompressionConfig::Type _serializedCompression; uint32_t _uncompressedLength; @@ -215,4 +214,3 @@ private: }; } // document - |