Storing a MAP container to the database

Posted on 1999-12-06
Medium Priority
Last Modified: 2010-04-01
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?
Question by:sephi

Expert Comment

ID: 2258180
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.

Expert Comment

ID: 2258249
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.
LVL 22

Expert Comment

ID: 2258375
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.)
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.


Author Comment

ID: 2259035
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.
LVL 22

Accepted Solution

nietod earned 900 total points
ID: 2259082
>> 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().)

Expert Comment

ID: 2259119
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.

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question