Hello. I write a daemon program (in C ++), a packet sniffer that tracks every IP and the number of packets that came from it. Sometimes you will need to display statistics about the information collected. Statistics output should be resistant to restarting the daemon or even restarting the computer. That's why I'm going to store this information in a file (for example, in json format). So, as I need to implement the show [ip] count command, the easiest way in my case is to store the data in the map, where the key is the string IP, the value is the number of packets from this IP. Question: how (if not in json) is it most convenient to store this information?

It is closed due to the fact that it is necessary to reformulate the question so that it is possible to give an objectively correct answer by the participants

The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    And how are you going to keep it in RAM? You will probably need to quickly find the record you need to update ... it means b-trees, hashes ... And from this you can already dance how to store on disk, you need to be able to load it into an indexed structure ... And with a record to the file you need to be careful, and then suddenly there will be a restart / reboot at the moment of writing the file. And in json you have to delete the previous file and write a whole new one ... - Mike
  • one
    And the most important thing is the key, which one address is ipv4, i.e. 4 bytes and how much data is stored on one key, how many all counters for one key are occupied - Mike
  • one
    I would suggest to collect information in portions, for example, by time. And write to the ever-growing file. But of course not because of each package, so the disk is not enough. When the file reaches a certain volume - start the next one. And an external program that groups the information on these files so that you can delete old files. And even when recording, you should be careful, any record can be recorded half. I have a similar info written in XML in a similar format. In addition, each data block is written with a checksum so that you can find the file corruption - Mike
  • one
    And you didn’t say if you need statistics on time as a result. Because as if stupidly counters from the birth of Christ, then you can make a file on the Unix file system (ext3 / 4, xfs), which allows you to write a file with holes. The counter, for example, takes 4 bytes, we make a 16GB binary file, the offset in the file is ip in binary form multiplied by 4. when recording, we use an aimed recording, i.e. fseek to ip position * 4, read-write 4 bytes. then physically the file will occupy less on the disk, only those blocks where there is data - Mike
  • 2
    IMHO, sqlite is much better and faster than json - vasily-vm

0