Storing a MAP container to the database

I need to store and fetch data to/from the database. The data is in a complicated format: It is stored in a Map object (the C++ standard library's Map), where each key is by itself a complicated object. I don't want to serialize down every key and every item of the Map because of performance considerations. On the other hand, I don't want to memcpy the Map into a binary buffer, since the application is supposed to be machine independent. Is there a half-way solution that assumes some knowledge about the Map and how the keys and the items are set in memory? Is there a different container that can be stored in an easier way?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Can you elaborate your problem a little bit more by giving details of the key and item stored in the map. Also tell what level of performance you want to achieve.
You could try to manipulate with maps allocator arg. But I do not think this will help reconstructing the map when reading. Simple serialization is probably the most portable solution. Perhaps use the map only to store references to the actual objects (and keys), which can be stored, saved and loaded in more conventional manner.
If the data starts out in the database and you want it to end up there as well, then you can simply have the map store the offsets to the data in the file or the data's record number in the file (if that database has record numbers)  So the map doesn't store the DB data directly, it stores data used to find the DB data.  

The map is probably going to have to store the key, or at least what you tell it to use as a key.  There is no way around this really, since that is part of the implimentation of the map.   But you could tell it to use some small class as a key.  And that class could again contain the offset or record number of the data in the database.  Then these classes are compared, then could obtain the data  from the database and use it in the comparison.  However this is likely to provide poor performance.  (The 1st option is not.)
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

sephiAuthor Commented:
By reading the above comments, I think the best solution would be to map the keys to an offset of the record in the database, where all the information is saved in a binary format.

To save double mechnism, I would like to know the hashing algorithm of Map. That will allow me to translate the key into an index by myself.

Alternately, I would like to get from the Map the index of each pair (meaning, which key is first, which is second and so forth). Is that possible?

The Key object contains a Vector of simple type variables.
>> I would like to know the hashing algorithm of Map.
A map won't use a hashing.  It must store the items in order, so hashing is not sufficient for the map's general usage (hashing may be sufficient for many ways in which the map is used, but not all the ways).  In any case, the inner workings of map<> or any other STL class are implimentation defined.  So some implimentations may use binary trees while others might use sorted arrays.  It just depends n how it was written.

>> I would like to get from the Map the index of each
>> pair (meaning, which key is first, which is second
>> and so forth).
Yes--because it doesn't use hashing.  use begin() to get an iterator to the first item in the map.  Then you can increment the iterator to get the other items in the map.  (same as with any other STL container.)  If the iterator equals the return value from end(), tnen you've reached the end of the map.  (If the map is empty, begin() will return an iterator that equals end().)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FYI, the common STL implementations use a balanced binary tree to implement the map. This allows for the forward and backward iterators to traverse along the elements in an ordered fashion.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.