KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 package org.netbeans.mdr.persistence.btreeimpl.btreestorage;
21
22 import java.text.MessageFormat JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Set JavaDoc;
25 import org.netbeans.mdr.persistence.MOFID;
26 import org.netbeans.mdr.persistence.SinglevaluedIndex;
27 import org.netbeans.mdr.persistence.Storage;
28 import org.netbeans.mdr.persistence.StorageBadRequestException;
29 import org.netbeans.mdr.persistence.StorageException;
30
31
32 /**
33  *
34  * @author Tomas Hurka
35  */

36 public class PrimaryIndex implements SinglevaluedIndex {
37
38     private FileCache fileCache; // source of pages from file
39
private int fileId; // file where our pages are stored,
40
// for making FileCache requests
41
private Storage.EntryType keyType;
42     private Storage.EntryType dataType;
43     private String JavaDoc indexName;
44     private int valuesInPage;
45     private final long firstMofIdSerial;
46     private static final int NO_VALUE=0;
47     private static final int sizeof_int=4;
48     private static final int reservedValues=FileHeader.HEADER_SIZE/sizeof_int;
49     
50     private class ValInfo {
51         private int index;
52         private CachedPage page;
53         
54         ValInfo(Object JavaDoc mofId) throws StorageException {
55             MOFID mid=(MOFID)mofId;
56             long serial=mid.getSerialNumber();
57             
58             if (serial>=BtreeFactory.FIRST_EXTERNAL_ID) {
59                 assert serial>=firstMofIdSerial;
60                 serial-=firstMofIdSerial-BtreeFactory.FIRST_EXTERNAL_ID;
61             }
62             serial+=reservedValues;
63             long pageNum=serial/valuesInPage;
64             page=fileCache.getPage(fileId, (int)pageNum);
65             
66             index=(int)(serial%valuesInPage)*sizeof_int;
67         }
68     }
69     
70     PrimaryIndex(String JavaDoc name, int id, FileCache cache, long firstSerial, Storage.EntryType key, Storage.EntryType data) {
71         indexName=name;
72         fileId=id;
73         fileCache=cache;
74         firstMofIdSerial=firstSerial;
75         keyType=key;
76         dataType=data;
77         valuesInPage=cache.getPageSize()/sizeof_int;
78         
79     }
80
81     public void add(Object JavaDoc key, Object JavaDoc value) throws StorageException {
82         put(key, value);
83     }
84
85     public Object JavaDoc get(Object JavaDoc key) throws StorageException, StorageBadRequestException {
86         Object JavaDoc result = getIfExists(key);
87         if (result == null) {
88             throw new StorageBadRequestException (
89                 MessageFormat.format("Key {0} not found in index",
90                     new Object JavaDoc[] {key}));
91         }
92         return result;
93     }
94
95     public Object JavaDoc getIfExists(Object JavaDoc key) throws StorageException {
96         ValInfo info=new ValInfo(key);
97         int value = Converter.readInt(info.page.contents,info.index);
98         
99         fileCache.unpin(info.page);
100         if (value==NO_VALUE)
101             return null;
102         return new Integer JavaDoc(value);
103    }
104
105     public Storage.EntryType getKeyType() throws StorageException {
106         return keyType;
107     }
108
109     public String JavaDoc getName() throws StorageException {
110         return indexName;
111     }
112
113     public Object JavaDoc getObject(Object JavaDoc key, SinglevaluedIndex repos) throws StorageException {
114         Object JavaDoc result = getObjectIfExists(key, repos);
115         if (result == null) {
116             throw new StorageBadRequestException (
117                 MessageFormat.format("Key {0} not found in index",
118                     new Object JavaDoc[] {key}));
119         }
120         return result;
121     }
122
123     public Object JavaDoc getObjectIfExists(Object JavaDoc key, SinglevaluedIndex repos) throws StorageException {
124         throw new UnsupportedOperationException JavaDoc();
125     }
126
127     public Storage.EntryType getValueType() throws StorageException {
128         return dataType;
129     }
130
131     public Set JavaDoc keySet() throws StorageException {
132         throw new UnsupportedOperationException JavaDoc();
133     }
134
135     public boolean put(Object JavaDoc key, Object JavaDoc value) throws StorageException {
136         ValInfo info=new ValInfo(key);
137         int oldVal = Converter.readInt(info.page.contents,info.index);
138         int val=((Integer JavaDoc)value).intValue();
139         
140         if (oldVal!=val) {
141             fileCache.setWritable(info.page);
142             Converter.writeInt(info.page.contents,info.index, val);
143         }
144         fileCache.unpin(info.page);
145         return oldVal!=NO_VALUE;
146     }
147
148     public Collection JavaDoc queryByKeyPrefix(Object JavaDoc prefix, SinglevaluedIndex repos) throws StorageException {
149         throw new UnsupportedOperationException JavaDoc();
150     }
151
152     public boolean remove(Object JavaDoc key) throws StorageException {
153         ValInfo info=new ValInfo(key);
154         int val=Converter.readInt(info.page.contents,info.index);
155         
156         if (val!=NO_VALUE) {
157             fileCache.setWritable(info.page);
158             Converter.writeInt(info.page.contents,info.index, NO_VALUE);
159         }
160         fileCache.unpin(info.page);
161         return val!=NO_VALUE;
162     }
163
164     public void replace(Object JavaDoc key, Object JavaDoc value) throws StorageException, StorageBadRequestException {
165         ValInfo info=new ValInfo(key);
166         int oldVal = Converter.readInt(info.page.contents,info.index);
167         
168         if (oldVal==NO_VALUE) {
169             throw new StorageBadRequestException (
170                 MessageFormat.format("Key {0} not found in index",
171                     new Object JavaDoc[] {key}));
172         }
173         int val=((Integer JavaDoc)value).intValue();
174         fileCache.setWritable(info.page);
175         Converter.writeInt(info.page.contents,info.index, val);
176         fileCache.unpin(info.page);
177     }
178
179     public Collection JavaDoc values() throws StorageException {
180         throw new UnsupportedOperationException JavaDoc();
181     }
182     
183 }
184
Popular Tags