Contents Hide
These two methods are called upon opening and closing the sync writer script respectively. Typically, they are used to open and close any resources the writer needs to use throughout its execution. An example of this is follows:
out;
openWriter()
{
out = new BufferedWriter(new FileWriter("filename", true));
// ...
}
closeWriter()
{
// ...
out.close();
}
Note:
out is the output buffered writer for the file writer. It is stored as a global variable, as to be accessed by other methods, and closed once the write script is completed, signalled by closeWriter().
When writing data, the write script is given a parameter named data which is a vector of RecordSets. In order to consume this data correctly, it must be correctly parsed and then processed by the sync writer script. An example is detailed below.
write(data)
{
for (i = 0; i < data.size(); i++)
{
rs = data.get(i) ;
for (j = 0; j < rs.size(); j++)
{
record = rs.get(j) ;
// Consume ...
if ( record.getStatus() == Record.ADDED)
{
v.add( record.get(0) );
v.add( record.get(1) + "/" + record.get(2) );
v.add( "\n" );
} else if ( ...
//...
}
}
}
}
Note:
data is a set of record sets, and is effectively the group of tables to be iterated through (using variables i and j)
The current table being processed through iterating through the data set is the RecordSet rs. This set of records may have their records and then fields further accessed and consumed via similar iterations; record = rs.get(j) and record.get(n) respectively.
This example assumes that at there is always at least three fields presented when a record is to be added.
This example explicitly defines operators to handle the Record.ADDED status of the records inspected.
commit() is called by the transaction manager to make the write commands’ changes permanent. It also marks the end of a transaction, and this should be reflected in the code.
write(data)
{
for (i = 0; i < v.size; i++)
{
out.write(v.get(i) + ",");
if (v.get(i) == "\n")
out.newLine();
}
}
rollback() is called by the transaction manager to revert any write changes that took place and restore the state to what it was after BeginTransaction() was called.
rollback()
{
v = new Vector();
}
isInTransaction() is called by the transaction manager to query whether write operations are in progress.
isInTransaction()
{
return (v.size() > 0);
}
Note:
In this example, write commands are stored in a cache v, which is a vector. This temporary store is then used when the commit is called later on.
When a script is instructed to rollback(), it will reset this cache.
If a transaction is in proceess, ie, if data values are to be changed to be written, the array will reflect this state by its size. This is what the isInTransaction() method achieves.