KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > persistence > btreeimpl > btreeindex > BtreeCacheSource


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.btreeindex;
20
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import org.netbeans.mdr.persistence.StorageException;
24 import org.netbeans.mdr.persistence.Storage;
25 import org.netbeans.mdr.persistence.MOFID;
26 import org.netbeans.mdr.persistence.btreeimpl.btreestorage.*;
27
28 /**
29  * In-memory page source for unit testing
30  */

31 public class BtreeCacheSource implements BtreePageSource {
32
33     private MDRCache cache;
34     private int pageSize;
35     private EntryTypeInfo pageIdInfo;
36     private byte[] noPageId;
37     private long nextID = 0;
38     private BtreeStorage storage;
39
40     public BtreeCacheSource(MDRCache cache, int pageSize, BtreeStorage storage)
41                 throws StorageException {
42
43     this.cache = cache;
44     this.pageSize = pageSize;
45         this.storage = storage;
46     pageIdInfo =
47         EntryTypeInfo.getEntryTypeInfo(Storage.EntryType.MOFID, storage);
48     noPageId = this.storage.getMOFIDData (BtreeFactory.nullMOFID);
49     }
50
51     public BtreePage getPage(byte[] pageId, Btree btree) throws StorageException {
52         MOFID pageMofId = this.storage.readMOFIDData (new ByteArrayInputStream JavaDoc (pageId));
53         return (BtreePage) cache.get(pageMofId);
54     }
55
56     public BigKeyPage newBigKeyPage(Btree btree) throws StorageException {
57         BigKeyPage page = new BigKeyPage();
58     initNewPage(page, btree);
59     return page;
60     }
61
62     public BtreePage newPage(Btree btree) throws StorageException {
63
64     BtreePage page;
65
66     page = btree.pageFactory();
67     initNewPage(page, btree);
68     return page;
69     }
70
71     private void initNewPage(BtreePage page, Btree btree) throws StorageException {
72     MOFID pageMOFID = new MOFID(this.storage);
73         byte[] mofidBytes = this.storage.getMOFIDData (pageMOFID);
74     page.init(btree, mofidBytes, new byte[pageSize], true);
75     cache.put(pageMOFID, page);
76     cache.setDirty(pageMOFID);
77     }
78
79     public void unpinPage(BtreePage page) {
80         /* nothing we need to do here */
81     }
82
83     public void unpinPage(BigKeyPage page) {
84         /* nothing we need to do here */
85     }
86
87     public void dirtyPage(BtreePage page) throws StorageException {
88         MOFID dirtyPageMOFId = this.storage.readMOFIDData (new ByteArrayInputStream JavaDoc(page.pageId));
89         cache.setDirty(dirtyPageMOFId);
90     }
91
92     public BtreePage getRootPage(Btree btree) throws StorageException {
93         return newPage(btree);
94     }
95
96     public EntryTypeInfo getPageIdInfo() {
97         return pageIdInfo;
98     }
99
100     public int getPageIdLength() {
101         return pageIdInfo.getLength();
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     public synchronized long getNextMofid() {
130         nextID++;
131         return nextID;
132     }
133
134     public String JavaDoc getMofidPrefix() {
135         return "01234567-89AB-CDEF-GHIJ-KLMNOPQRSTUV";
136     }
137     
138     public BtreeStorage getStorage() {
139         return storage;
140     }
141
142 }
143
Popular Tags