educative.io

How much memory is needed to store the quadtree index?

How can we efficiently retrieve a mapping between Places and QuadTree server? We have to build a reverse index that will map all the Places to their QuadTree server. We can have a separate QuadTree Index server that will hold this information. We will need to build a HashMap where the ‘key’ is the QuadTree server number and the ‘value’ is a HashSet containing all the Places being kept on that QuadTree server. We need to store LocationID and Lat/Long with each place because information servers can build their QuadTrees through this. Notice that we are keeping Places’ data in a HashSet, this will enable us to add/remove Places from our index quickly. So now, whenever a QuadTree server needs to rebuild itself, it can simply ask the QuadTree Index server for all the Places it needs to store. This approach will surely be quite fast. We should also have a replica of the QuadTree Index server for fault tolerance. If a QuadTree Index server dies, it can always rebuild its index from iterating through the database.

How much memory is needed to store the quadtree index? Let’s assume our search radius is 10 miles; given that the total area of the earth is around 200 million square miles, we will have 20 million grids. Is bellow correct assuning 4 bytes per grid id, 8 bytes per location id, and 500 locations per grid.

20M * (4B + 500 * 8B) ~ 40 GB

@Dewey_Munoz

Yes, if we assume a key maps to a grid and in a HashSet there are 500 locations along with their LocationID and Lat/Long – I am assuming you assigned 8 bytes to them combined – and there are total 20 million grids. The memory allocated to each element is system-dependent, but the mathematics is correct.