ChessPro wrote:
"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.)
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."
Okay, let's look at how index file(s) - nothing says this 3-field structure has to exist in all of them. It depends
on what you're looking for in your functionality.
1. OK, fine. This is where you "inuse" field works. Copy the game out, or delete it by setting the flag. You don't have to restructure the main file.
2. Perfect for an index file. You can relate a game number as a key field to a record offset.
3. Good for your index files. Append the new game to the end of the main file, and adjust the index files accordingly.
4. Load your game number/offset index, find the appropriate game number.
5. This one is much trickier. It might be more preferential to just do a sequential search through the main file.
There's really a lot of logic and thought that could be applied here. In a lot of ways, the DBMS is better for that
purpose - it's going to handle all of this in the best way for you. The main question is if you're going to want the DBMS overhead.
ChessPro wrote:
"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."
It doesn't need to be that involved. Simply open the file in read/write mode, seek to the appropriate record, and
overwrite it. If you want to add a record, seek to the end, and do the write.
The main point for the indexes is to minimize the amount of reading of the disk that you do (which takes a ton of time compared to memory writing) - I keep having these scary visions of you trying to copy around 400MB of data every time you want to make a change, or reading through 400MB of data every time you do a search. Not good, especially since you said multiple PROGRAMS are possibly going to access this.
Especially if you do a lookup, if you can get it done by reading an 8MB index file and then seeking to the exact spot you need in the 400MB file, then you're saving time. It seems an example of binary file manipulation might be in order, especially since there's a bit of confusion.
If I get the time, I'll try to come up with a quick example of what we're talking about. Depending on what your exact requirements are, though, it probably wouldn't hurt in the end for you to just go with the DBMS.