0

My client-server application simulates a showcase (like a Facebook one). As with every client server application, the server might crash, or go down for electrical deseases or whatever, therefore it was crucial to elaborate a backup system. As long as I have to write stuff on the showcase everything's fine: you write the message on the single-linked-list I use to store the messages and I also write them down on the backup file (which is a text file... yea, I know, but we didn't have time to see other types of file in the course, but still...). Assume the file is like this:

MEX1:
TEXT1
MEX2:
TEXT2
MEX3:
TEXT3

And so on...

The problem is when a user wants do delete the message. Deleting a node from a single linked list is child's play, the problem is that the same message should be deleted from the backup file, too.

Now, reading here on Stack Overflow and on every other web site I found about the subject, seems like I must:

  1. Create another backup file;
  2. Copy everything until the portion of the file I do not need;
  3. Skip it;
  4. Start copying again until the end;
  5. Delete old file;
  6. Use new file as backup file;

Which would be fine... unless I have to delete messages from the application very often. My professor is like a sniper: if I give him the copy-paste solution he WILL say "What if a famous person dies and everyone starts posting and deleting stuff from the showcase?".

And he's right, you know. I may have to delete tens of messages per minute, which is not ok with the copy-paste solution. If I have 1000 messages of 100 lines each on the file and I have to delete one message only I'll have to copy 999000 lines. Do it for ten times in a minute, it's crazy.

Another way around would be that of using more files instead of only one. But then I'll need a sort of sorting in order to make information accessible in short time, and eventually these files will grow bigger. Therefore I would need to split them, but how do I decide when to split them?

And, by the way, the problem stays still. I have reduced the amount of information to copy and then paste, but eventually the problem will rise again. And, after all, I'll have my server machine filled with backup files.

So, could you suggest me some kind of solution to just delete a specific portion of the file, without copy-pasting everything else?

In the above example, I want to delete Text2 message. So I want to delete from MEX2: to the end of TEXT2. It should look like this:

MEX1:
TEXT1
MEX3:
TEXT3

Every suggestion is appriciated. Thanks.

5
  • I'd use SQLite or another database instead of a text flatfile. That's probably outside the scope of your homework though. Commented Nov 3, 2022 at 8:30
  • Yes, that's outside the scope of my project, thought the Professor will surely appriciate an innovative approach. But is there a way to integrate SQLite files with C? Sorry for the noob question, but I have never used SQLite, but if it can help me to solve the problem, why not Commented Nov 3, 2022 at 8:32
  • Perhaps your professor wants you to write a rudimentary database from scratch, rather than using an off-the-shelf one. Commented Nov 3, 2022 at 8:48
  • 2
    One idea: make all the records stored in the file a single, fixed size. Then when deleting one, fill that record's spot in the file with a special value that gets skipped when reading the file later. And if the last record in the file is deleted, just truncate the file. To add new records, either reuse deleted blocks or just append to the end. Commented Nov 3, 2022 at 8:48
  • Usually back up don't have to save exactly everything till the second before a disaster happened. So you can select a frequency on which the backup is updated, say 10 minutes, 1 hour etc.. and when you update the backup you do the copy/paste approach, but every change is squeezed in, so it's one copy, one modification with everything that happened in the last X minutes, and one paste. Commented Nov 3, 2022 at 10:14

1 Answer 1

1

Observation: you update your backup file constantly, but use it very rarely. This means that you can defer the cost of maintaining that backup up until you need it, or once in a while.

I suggest to store transactions in that backup, specifying that some message was added, edited, deleted. Then you can "play it back" to reconstruct your data.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, I then settled for this solution! In fact, I mark the unwanted mex on the backup file as "SKIP" with an ESCAPE sequence, then I update a variable every delete request. As soon as the number of delete requests are equal to half the total number of messages, I delete everything. This way should be a pretty roubust architecture. Thank you for your support!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.