Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7097963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T10:59:56+00:00 2026-05-28T10:59:56+00:00

I’m using leveldb to store key-value pairs of integer and MyClass objects. Actually, a

  • 0

I’m using leveldb to store key-value pairs of integer and MyClass objects. Actually, a key can contain more then one of theses objects.
The problem I have appears when retrieving the data from the database. It compiles, however the values of the MyClass members are not the one I put into the database.

std::string value;
leveldb::Slice keySlice = ANYKEY;
levelDBObj->Get(leveldb::ReadOptions(), keySlice, &value);

The std::string value1 can now contain only one MyClass object or more. So how do I get them?

I already tried the following which didn’t work;

1.) directly typecasting and memcpy

std::vector<MyClass> vObjects;
MyClass* obj = (MyClass*)malloc( value.size());
memcpy((void*)obj, (void*) (value.c_str()), value.size());
MyClass dummyObj;
int numValues = value.size()/sizeof(MyClass);
for( int i=0; i<numValues; ++i) {
    dummyObj = *(obj+i);
    vObjects.push_back(dummyObj);
}

2.) reinterpret_cast to void pointer

MyClass* obj = (MyClass*)malloc( value.size());
const void* vobj = reinterpret_cast<const void*>( value.c_str() );
int numValues = value.size()/sizeof(MyClass);
for( int i=0; i<numValues; ++i) {
    const MyClass dummyObj = *(reinterpret_cast<const MyClass*>(vobj)+i);
    vObjects.push_back(dummyObj);
}

MyClass is a collection of several public members, e.g. unsigned int and unsigned char and it has a stable size.

I know that there are similar problems with only one object. But in my case the vector can contain more then one and it comes from the leveldb database.

EDIT: SOLUTION

I wrote (de)serialization method for MyClass which then made it working. Thanks for the hint!

void MyClass::serialize( char* outBuff ) {
    memcpy(outBuff, (const void*) &aVar, sizeof(aVar));
    unsigned int c = sizeof(aVar);
    memcpy(outBuff+c, (const void*) &bVar, sizeof(bVar));
    c += sizeof(bVAr);
    /* and so on */
}

void MyClass::deserialize( const char* inBuff ) {
    memcpy((void*) &aVar, inBuff, sizeof(aVar));
    unsigned int c = sizeof(aVar);
    memcpy((void*) &aVar, inBuff+c, sizeof(aVar));
    c += sizeof(aVar);
    /* and so on */
}

The get method is as follows (put analogously):

int getValues(leveldb::Slice keySlice, std::vector<MyObj>& values) const {
    std::string value;  
    leveldb::Status status = levelDBObj->Get(leveldb::ReadOptions(), keySlice, &value);
    if (!status.ok()) {
        values.clear();
            return -1;
    }

    int nValues = value1.size()/sizeof(CHit);
    MyObj dummyObj;
    for( int i=0; i<nValues; ++i) {
        dummyObj.deserialize(value.c_str()+i*sizeof(MyObj));
        values.push_back(dummyObj);
    }

    return 0;
}               
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T10:59:56+00:00Added an answer on May 28, 2026 at 10:59 am

    You have to serialize your class… otherwise, you’re just taking some memory and writing it in leveldb. Whatever you get back is not only going to be different, but it will probably be completely useless too. Check out this question for more info on serialization: How do you serialize an object in C++?

    LevelDB does support multiple objects under one key, however, try to avoid doing that unless you have a really good reason. I would recommend that you hash each object with a unique hash (see Google’s CityHash if you want a hashing function) and store the serialized objects with their corresponding hash. If your objects is a collection in itself, then you have to serialize all of your objects to an array of bytes and have some method that allows you to determine where each object begins/ends.

    Update

    A serializable class would look something like this:

    class MyClass
    {
    private:
        int _numeric;
        string _text;
    public:
        // constructors 
    
        // mutators
        void SetNumeric(int num);
        void SetText(string text);
    
        static unsigned int SerializableSize()
        {
            // returns the serializable size of the class with the schema:
            // 4 bytes for the numeric (integer) 
            // 4 bytes for the unsigned int (the size of the text)
            // n bytes for the text (it has a variable size)
            return sizeof(int) + sizeof(unsigned int) + _text.size();
        }
    
        // serialization
        int Serialize(const char* buffer, const unsigned int bufferLen, const unsigned int position)
        {
            // check if the object can be serialized in the available buffer space
            if(position+SerializableSize()>bufferLen)
            {
                // don't write anything and return -1 signaling that there was an error
                return -1;
            }
    
            unsigned int finalPosition = position;
    
            // write the numeric value
            *(int*)(buffer + finalPosition) = _numeric;
    
            // move the final position past the numeric value
            finalPosition += sizeof(int);
    
            // write the size of the text
            *(unsigned int*)(buffer + finalPosition) = (unsigned int)_text.size();
    
            // move the final position past the size of the string
            finalPosition += sizeof(unsigned int);
    
            // write the string
            memcpy((void*)(buffer+finalPosition), _text.c_str(), (unsigned int)_text.size());
    
            // move the final position past the end of the string
            finalPosition += (unsigned int)_text.size();
    
            // return the number of bytes written to the buffer
            return finalPosition-position;
        }
    
        // deserialization
        static int Deserialize(MyClass& myObject, 
            const char* buffer, 
            const unsigned int buffSize, 
            const unsigned int position)
        {
            insigned int currPosition = position;
    
            // copy the numeric value
            int numeric = *(int*)(buffer + currentPosition);
    
            // increment the current position past the numeric value
            currentPosition += sizeof(int);
    
            // copy the size of the text
            unsigned int textSize = *(unsigned int*)(buffer + currentPosition);
    
            // increment the current position past the size of the text
            currentPosition += sizeof(unsigned int);
    
            // copy the text
            string text((buffer+currentPosition), textSize);
    
            if(currentPosition > buffSize)
            {
                // you decide what to do here
            }
    
            // Set your object's values
            myObject.SetNumeric(numeric);
            myObject.SetText(text);
    
            // return the number of bytes deserialized
            return currentPosition - position;
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.