Sync Writer Examples

Contents Hide

  

Example 1. Opening/Closing the Writer

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:

Example 2. Reading the data parameter

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:

Example 3. Handling Rollbacks and Commits

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: