Simplify blob encoding

This commit is contained in:
Nik Bougalis
2016-01-11 00:29:14 -08:00
parent a7a30396be
commit 1c9577a1ac
2 changed files with 27 additions and 25 deletions

View File

@@ -19,36 +19,27 @@
#include <BeastConfig.h> #include <BeastConfig.h>
#include <ripple/nodestore/impl/EncodedBlob.h> #include <ripple/nodestore/impl/EncodedBlob.h>
#include <beast/ByteOrder.h> #include <cstring>
namespace ripple { namespace ripple {
namespace NodeStore { namespace NodeStore {
void void
EncodedBlob::prepare (std::shared_ptr<NodeObject> const& object) EncodedBlob::prepare (
std::shared_ptr<NodeObject> const& object)
{ {
m_key = object->getHash().begin (); m_key = object->getHash().begin ();
// This is how many bytes we need in the flat data auto ret = m_data.alloc(object->getData ().size () + 9);
m_size = object->getData ().size () + 9;
m_data.ensureSize(m_size); // the first 8 bytes are unused
memset (ret, 0, 8);
{ ret[8] = static_cast<std::uint8_t> (object->getType ());
// these 8 bytes are unused
std::uint64_t* buf = static_cast <
std::uint64_t*>(m_data.getData ());
*buf = 0;
}
{ memcpy (ret + 9,
unsigned char* buf = static_cast < object->getData ().data(),
unsigned char*> (m_data.getData ());
buf [8] = static_cast <
unsigned char> (object->getType ());
memcpy (&buf [9], object->getData ().data(),
object->getData ().size()); object->getData ().size());
}
} }
} }

View File

@@ -20,8 +20,8 @@
#ifndef RIPPLE_NODESTORE_ENCODEDBLOB_H_INCLUDED #ifndef RIPPLE_NODESTORE_ENCODEDBLOB_H_INCLUDED
#define RIPPLE_NODESTORE_ENCODEDBLOB_H_INCLUDED #define RIPPLE_NODESTORE_ENCODEDBLOB_H_INCLUDED
#include <ripple/basics/Buffer.h>
#include <ripple/nodestore/NodeObject.h> #include <ripple/nodestore/NodeObject.h>
#include <beast/module/core/memory/MemoryBlock.h>
#include <cstddef> #include <cstddef>
namespace ripple { namespace ripple {
@@ -35,14 +35,25 @@ struct EncodedBlob
{ {
public: public:
void prepare (std::shared_ptr<NodeObject> const& object); void prepare (std::shared_ptr<NodeObject> const& object);
void const* getKey () const noexcept { return m_key; }
std::size_t getSize () const noexcept { return m_size; } void const* getKey () const noexcept
void const* getData () const noexcept { return m_data.getData (); } {
return m_key;
}
std::size_t getSize () const noexcept
{
return m_data.size();
}
void const* getData () const noexcept
{
return reinterpret_cast<void const*>(m_data.data());
}
private: private:
void const* m_key; void const* m_key;
beast::MemoryBlock m_data; Buffer m_data;
std::size_t m_size;
}; };
} }