KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > persistence > btreeimpl > btreestorage > CachedPage


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.mdr.persistence.btreeimpl.btreestorage;
20
21 import java.io.*;
22
23 import org.netbeans.mdr.persistence.*;
24
25 /**
26 * This represents a page fetched from the cache.
27 */

28 public class CachedPage extends IntrusiveList.Member {
29
30     /** Description of which page this is */
31     PageID key;
32     /** How many times this page has been pinned and not unpinned */
33     private int pinCount;
34     /** true if this page has been modified */
35     boolean isDirty;
36     /** true if the log file must be flushed before this page can be written */
37     boolean heldForLog;
38
39     /** the contents of the page */
40     public byte contents[];
41
42     /* cache we were created by */
43     private FileCache owner;
44
45     /** create a page of the specified size
46     * @param size the page size for the cache
47     */

48     CachedPage(int size) {
49         key = null;
50         pinCount = 0;
51         isDirty = false;
52         heldForLog = false;
53         owner = null;
54         contents = new byte[size];
55     }
56     
57     public FileCache getOwner() {
58         return owner;
59     }
60
61     /** Make this page writable. If it was not writable previously,
62     * this causes it to be logged. This must be called before the page
63     * is modified. If the cache is not currently in a transaction,
64     * this implicitly begins one.
65     * @exception StorageException I/O error logging the page
66     */

67     public synchronized void setWritable() throws StorageException{
68         owner.setWritable(this);
69     }
70
71     /** reinitialize thie object to point to a different file page
72     * @param id the file and page number this will become
73     */

74     void reInit(FileCache owner, PageID id) {
75         this.owner = owner;
76         key = id;
77         pinCount = 0;
78         isDirty = false;
79         heldForLog = false;
80     }
81     
82     public int pin(FileCache owner) {
83         assert pinCount == 0 || this.owner == owner;
84         this.owner = owner;
85         return pinCount++;
86     }
87     
88     public int getPinCount() {
89         return pinCount;
90     }
91     
92     public int innerUnpin() {
93         pinCount--;
94         return pinCount;
95     }
96         
97     /** client calls this when it is done with the page */
98     public void unpin() throws StorageException {
99         owner.unpin(this);
100     }
101
102     /** format debugging info */
103     public String JavaDoc toString() {
104         StringBuffer JavaDoc debug = new StringBuffer JavaDoc("" + key);
105         if (pinCount > 0)
106             debug.append(" pin count: " + pinCount);
107         if (isDirty)
108             debug.append(" dirty");
109         if (heldForLog)
110             debug.append(" held");
111         debug.append("\n");
112
113         int j = 0;
114         for (int i = 0; i < contents.length; i++, j++) {
115             if (j >= 16) {
116                 debug.append("\n");
117                 j = 0;
118             }
119             int data = (int)(contents[i]) & 0xFF;
120             debug.append(Integer.toHexString(data));
121             debug.append(" ");
122         }
123         debug.append("\n");
124
125         return debug.toString();
126     }
127                 
128
129 }
130
Popular Tags