File: AddChunk.java

package info (click to toggle)
libglazedlists-java 1.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,024 kB
  • ctags: 4,252
  • sloc: java: 22,561; xml: 818; sh: 51; makefile: 5
file content (71 lines) | stat: -rw-r--r-- 2,227 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* 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 java.io.IOException;
import java.util.logging.Logger;

/**
 * Adds a chunk to the persistent map.
 *
 * @author <a href="mailto:jesse@swank.ca">Jesse Wilson</a>
 */
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.
     *
     * <p>This is a multiple stage procedure:
     * <ol>
     *   <li>Figure out how many bytes are needed for this chunk:
     *   <li>Allocate that many bytes in the file for the new section
     *   <li>Clean up the new section: mark it as empty and of the new size (flush 1)
     *   <li>Fill the new section (flush 2)
     *   <li>Mark the new section as not empty any more (flush 3)
     * </ol>
     */
    public void run() {
        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());
        }
    }
}