KickJava   Java API By Example, From Geeks To Geeks.

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


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.ByteArrayInputStream JavaDoc;
22 import org.netbeans.mdr.persistence.*;
23 import org.netbeans.mdr.persistence.btreeimpl.btreeindex.*;
24
25 /**
26  * BtreePageSource whose pages are MOF repository objects
27  *
28  * @author Dana Bergen
29  * @version 1.0
30  */

31 public class BtreeMDRSource extends Object JavaDoc implements BtreePageSource {
32
33     BtreeStorage storage;
34     MofidGenerator gen;
35     private int pageSize;
36     private EntryTypeInfo pageIdInfo;
37     private byte[] noPageId;
38
39     public BtreeMDRSource(BtreeStorage storage, int pageSize) throws StorageException {
40         this.storage = storage;
41     this.pageSize = pageSize;
42     gen = ((BtreeDatabase) storage.getPrimaryIndex()).getMofidGenerator();
43     pageIdInfo = EntryTypeInfo.getEntryTypeInfo(Storage.EntryType.MOFID, storage);
44     noPageId = this.storage.getMOFIDData (BtreeFactory.nullMOFID);
45     }
46
47     public BigKeyPage newBigKeyPage(Btree btree) throws StorageException {
48         BigKeyPage page = new BigKeyPage();
49     initNewPage(page, btree);
50     return page;
51     }
52
53     public BtreePage newPage(Btree btree) throws StorageException {
54
55     BtreePage page = btree.pageFactory();
56     initNewPage(page, btree);
57     return page;
58     }
59
60     private void initNewPage(BtreePage page, Btree btree) throws StorageException {
61
62     MOFID pageMOFID = new MOFID((BtreeStorage)this.storage);
63         byte[] mofBytes = this.storage.getMOFIDData (pageMOFID);
64     page.init(btree, mofBytes, new byte[pageSize], true);
65         BtreeDatabase repos = (BtreeDatabase) storage.getPrimaryIndex();
66     repos.add(pageMOFID, page);
67     repos.objectStateChanged(pageMOFID);
68     }
69
70     public BtreePage getPage(byte[] pageId, Btree btree)
71                         throws StorageException {
72     BtreePage page;
73         MOFID mofId = this.storage.readMOFIDData (new ByteArrayInputStream JavaDoc (pageId));
74     page = (BtreePage) ((BtreeDatabase) storage.getPrimaryIndex()).get(mofId);
75     page.init(btree, pageId, page.pageBuffer, false);
76     return page;
77     }
78
79     public BtreePage getRootPage(Btree btree) throws StorageException {
80         return newPage(btree);
81     }
82     
83     public void unpinPage(BtreePage page) {
84         /* nothing we need to do here */
85     }
86
87     public void unpinPage(BigKeyPage page) {
88         /* nothing we need to do here */
89     }
90
91     public void dirtyPage(BtreePage page) throws StorageException {
92         MOFID mofId = this.storage.readMOFIDData (new ByteArrayInputStream JavaDoc (page.pageId));
93         ((BtreeDatabase) storage.getPrimaryIndex()).objectStateChanged(mofId);
94     }
95
96     public EntryTypeInfo getPageIdInfo() {
97         return pageIdInfo;
98     }
99
100     public int getPageIdLength() {
101         return MOFID.LENGTH;
102     }
103
104     public int getPageSize() {
105         return pageSize;
106     }
107
108     /**
109      * Set the passed-in pageId to contain the special value noPageId
110      */

111     public void setNoPage(byte[] pageId) {
112
113         System.arraycopy(noPageId, 0, pageId, 0, pageId.length);
114     }
115
116     /**
117      * Test whether the passed-in pageId contains the special value noPageId
118      */

119     public boolean isNoPage(byte[] pageId) {
120     
121     for (int i = 0; i < pageId.length; i++) {
122         if (pageId[i] != noPageId[i]) {
123             return false;
124         }
125     }
126     return true;
127     }
128     
129     /** get the next unique ID for this repository */
130     public long getNextMofid() {
131         return gen.getNextMofid();
132     }
133
134     /** Get the prefix for this repository */
135     public String JavaDoc getMofidPrefix() {
136         return gen.getMofidPrefix();
137     }
138
139     public BtreeStorage getStorage() {
140         return storage;
141     }
142     
143 }
144
Popular Tags