ca.odell.glazedlists.impl.pmap.AddChunk Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of glazedlists_java15 Show documentation
Show all versions of glazedlists_java15 Show documentation
Event-driven lists for dynamically filtered and sorted tables
/* Glazed Lists (c) 2003-2006 */
/* http://publicobject.com/glazedlists/ publicobject.com,*/
/* O'Dell Engineering Ltd.*/
package ca.odell.glazedlists.impl.pmap;
// NIO is used for CTP
import ca.odell.glazedlists.impl.nio.*;
import ca.odell.glazedlists.impl.io.*;
import java.util.*;
import java.nio.*;
import java.nio.channels.*;
import java.text.ParseException;
import java.io.*;
// logging
import java.util.logging.*;
/**
* Adds a chunk to the persistent map.
*
* @author Jesse Wilson
*/
class AddChunk implements Runnable {
/** logging */
private static Logger logger = Logger.getLogger(AddChunk.class.toString());
/** the host map */
private final PersistentMap persistentMap;
/** the value to write out */
private final Chunk newValue;
/** the value to erase */
private final Chunk oldValue;
/**
* Create a new AddChunk.
*/
public AddChunk(PersistentMap persistentMap, Chunk newValue, Chunk oldValue) {
this.persistentMap = persistentMap;
this.newValue = newValue;
this.oldValue = oldValue;
}
/**
* Write the chunk to disk.
*
* This is a multiple stage procedure:
*
* - Figure out how many bytes are needed for this chunk:
*
- Allocate that many bytes in the file for the new section
*
- Clean up the new section: mark it as empty and of the new size (flush 1)
*
- Fill the new section (flush 2)
*
- Mark the new section as not empty any more (flush 3)
*
*/
public void run() {
FileChannel fileChannel = persistentMap.getFileChannel();
try {
// allocate
persistentMap.allocate(newValue);
// get a sequence id
newValue.setSequenceId(persistentMap.nextSequenceId());
// write out the data
newValue.writeData();
// clear the old value
if(oldValue != null) {
oldValue.delete();
}
logger.info("Successfully wrote value for key \"" + newValue.getKey() + "\"");
} catch(IOException e) {
persistentMap.fail(e, "Failed to write to file " + persistentMap.getFile().getPath());
}
}
}