Game Development Reference
In-Depth Information
5.2.3 Incremental Update: Main DataBase
and Transactions
Let's suppose the system successfully gathered all metadata and saved them to a
database file. At the next run of the editor, some asset changes have been detected.
In that case, the system should properly update the database. However, naively
updating the database in memory and resaving it on each occasion can be very
time consuming and block the user.
We adopt the concept of differential backup [Wiki 11c]. Basically, it means
that after the initial full gathering, the system saves changes like data update,
addition, and deletion as a transaction file separate from the main data base file.
The generated transaction files (there can be one transaction file or several for
each asset type) in one run are merged at the start of the next run of the editor.
Using this approach, the asset management system can append each change in-
stead of saving the whole updated database to a file every time a new metadata is
cached.
Let's suppose that we use XML format for the transaction file. When a change
should be saved, it doesn't parse the file as an XML file at all. Instead, it just goes
to the end of file, deletes some end tags, appends data related to the change, and
restores previously deleted end tags. This approach is essential since, in the first
run, the transaction file(s) can become quite large (it's the first time, so there is
nothing in the main database), and it'll cause a stall if one tries to parse it as XML
every time a new asset is cached. See Listing 5.1 for a code sample.
FILE *transactionFile = fopen(fullPath.c_str(), "r+t");
if(transactionFile == NULL)
return false;
const char endTag[] = "</Transactions >";
// To delete the end tag of "</Transactions>"
int result = fseek(transactionFile , -( (long)sizeof( endTag ) )
+ 1, SEEK_END);
if(result != 0)
// It means this is the first transaction saved.
{
fseek(transactionFile , 0, SEEK_SET);
fprintf(transactionFile , "<Transactions >\n");
}
fprintf(transactionFile , "%s%s",
newTransactionNode->getXML().c_str(), endTag);
fclose(transactionFile);
Listing 5.1. A code sample of appending new data to the transaction file.
Search WWH ::




Custom Search