Creating an efficient index file seems to be an interesting problem. Let me describe some of my program’s functionalities to give you an idea of the scope of problems connected with rebuilding the code to work with an index file.
Current version of my program does the following:
1. Copies and deletes selected games.
2. Moves selected games to a specified position in the list.
3. Adds new games and changes the contents of the existing games.
4. Allows user to access any game in the list specifying the game number.
5. Performs search by any field or a combination of fields specified by user (e.g., player’s name, place, result, etc.)
Those features are not the purpose of my program though, as it serves mainly for showing the games (in animated mode using special chess annotation) and not for the games management. But I’ve provided basic management features for the user’s convenience.
So now I have to preserve all those features reorienting my program to work with an index file.
I think that the main requirement to the index file, given the possible big size of the main files, is its size and hence the time needed to build it. That’s why I think that a 3-field structure representing each game is too expensive. 35 bytes for each record, it makes 70 MB index file if we have 2,000,000 games in the main file. I think the index file should by one-dimensional – just the offset for each game. The end of the game block is the offset for the next game. To split the block into separate records is nothing in terms of machine time. In this case I will have 8MB index file for 2,000,000 games. If I need to modify a game for example, I can do the following:
1. Copy the part of the file after the game being changed into a temporary file.
2. Delete the old game and all subsequent games in the main file.
3. Append the modified game.
4. Append the temporary file.
5. Update offsets for the games after the modified game by adding or subtracting the size difference between the deleted block and the inserted block.
In this case I don’t need the “inuse” flag as all my records are active. I think such index file should work OK.
My other concern is whether to keep the index file after closing the main file or create a new one every time I open the file. The former would be perfect, but the problem is that other programs may access the main file and make changes to it. In this case my index file would work incorrectly. I think this can be solved using FileAge. If timestamp is wrong the program will create a new index file.
Would appreciate your opinion, guys.